1ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin/* 2ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * Copyright (C) 2010 The Android Open Source Project 3ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * 4ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * Licensed under the Apache License, Version 2.0 (the "License"); 5ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * you may not use this file except in compliance with the License. 6ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * You may obtain a copy of the License at 7ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * 8ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * http://www.apache.org/licenses/LICENSE-2.0 9ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * 10ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * Unless required by applicable law or agreed to in writing, software 11ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * distributed under the License is distributed on an "AS IS" BASIS, 12ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * See the License for the specific language governing permissions and 14ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin * limitations under the License. 15ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin */ 16ec1dfcb7314d10ff243a922fe4eff66ea699dd77Owen Lin 174de149ceb47f2c251f646419907424bfb67d2b64Owen Linpackage com.android.camera.ui; 184de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 19281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Linimport static com.android.camera.ui.GLRootView.dpToPixel; 204de149ceb47f2c251f646419907424bfb67d2b64Owen Linimport android.content.Context; 214de149ceb47f2c251f646419907424bfb67d2b64Owen Linimport android.graphics.Color; 224de149ceb47f2c251f646419907424bfb67d2b64Owen Linimport android.graphics.Rect; 234de149ceb47f2c251f646419907424bfb67d2b64Owen Linimport android.view.animation.Transformation; 244de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 25e480f77352d670b2892a9b7b7cd503838b71ca9fRomain Guyimport com.android.camera.R; 264de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 274de149ceb47f2c251f646419907424bfb67d2b64Owen Linimport javax.microedition.khronos.opengles.GL11; 284de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 294de149ceb47f2c251f646419907424bfb67d2b64Owen Linpublic class GLOptionItem extends GLView { 304de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private static final int FONT_COLOR = Color.WHITE; 31281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static final float FONT_SIZE = 18; 32281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 33281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static final int MINIMAL_WIDTH = 120; 34281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static final int MINIMAL_HEIGHT = 32; 35281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 36281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static final int NO_ICON_LEADING_SPACE = 10; 37281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static final int TEXT_LEFT_PADDING = 6; 38281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static final int TEXT_RIGHT_PADDING = 10; 39281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 404de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private static final float ENABLED_ALPHA = 1f; 414de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private static final float DISABLED_ALPHA = 0.3f; 424de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 4333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin private static final int HORIZONTAL_PADDINGS = 4; 4433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin private static final int VERTICAL_PADDINGS = 2; 4533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin 464de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private static ResourceTexture sCheckOn; 474de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private static ResourceTexture sCheckOff; 484de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 49281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static int sNoIconLeadingSpace; 50281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static int sTextLeftPadding; 51281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static int sTextRightPadding; 52281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static int sMinimalWidth; 53281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static int sMinimalHeight; 54281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static float sFontSize; 5533be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin private static int sHorizontalPaddings = -1; 5633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin private static int sVerticalPaddings; 57281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 584de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private final ResourceTexture mIcon; 594de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private final StringTexture mText; 604de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private boolean mEnabled = true; 614de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 624de149ceb47f2c251f646419907424bfb67d2b64Owen Lin private ResourceTexture mCheckBox; 634de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 64281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 65281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin private static void initializeStaticVariables(Context context) { 664de149ceb47f2c251f646419907424bfb67d2b64Owen Lin if (sCheckOn != null) return; 67281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 684de149ceb47f2c251f646419907424bfb67d2b64Owen Lin sCheckOn = new ResourceTexture(context, R.drawable.ic_menuselect_on); 694de149ceb47f2c251f646419907424bfb67d2b64Owen Lin sCheckOff = new ResourceTexture(context, R.drawable.ic_menuselect_off); 70281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 71281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin sNoIconLeadingSpace = dpToPixel(context, NO_ICON_LEADING_SPACE); 72281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin sTextLeftPadding = dpToPixel(context, TEXT_LEFT_PADDING); 73281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin sTextRightPadding = dpToPixel(context, TEXT_RIGHT_PADDING); 74281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin sMinimalWidth = dpToPixel(context, MINIMAL_WIDTH); 75281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin sMinimalHeight = dpToPixel(context, MINIMAL_HEIGHT); 7633be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin sHorizontalPaddings = dpToPixel(context, HORIZONTAL_PADDINGS); 7733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin sVerticalPaddings = dpToPixel(context, VERTICAL_PADDINGS); 78281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 79281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin sFontSize = dpToPixel(context, FONT_SIZE); 804de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } 814de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 824de149ceb47f2c251f646419907424bfb67d2b64Owen Lin public GLOptionItem(Context context, int iconId, String title) { 83281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin initializeStaticVariables(context); 844de149ceb47f2c251f646419907424bfb67d2b64Owen Lin mIcon = iconId == 0 ? null : new ResourceTexture(context, iconId); 85281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin mText = StringTexture.newInstance(title, sFontSize, FONT_COLOR); 864de149ceb47f2c251f646419907424bfb67d2b64Owen Lin mCheckBox = sCheckOff; 8733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin setPaddings(sHorizontalPaddings, 8833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin sVerticalPaddings, sHorizontalPaddings, sVerticalPaddings); 894de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } 904de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 914de149ceb47f2c251f646419907424bfb67d2b64Owen Lin @Override 924de149ceb47f2c251f646419907424bfb67d2b64Owen Lin protected void onMeasure(int widthSpec, int heightSpec) { 93281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin int width = mIcon == null ? sNoIconLeadingSpace : mIcon.getWidth(); 94281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin width += mText.getWidth() + mCheckBox.getWidth(); 95281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin width += sTextRightPadding + sTextLeftPadding; 96281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin 974de149ceb47f2c251f646419907424bfb67d2b64Owen Lin int height = Math.max(Math.max(mIcon == null ? 0 : mIcon.getHeight(), 984de149ceb47f2c251f646419907424bfb67d2b64Owen Lin mText.getHeight()), mCheckBox.getHeight()); 994de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 100281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin width = Math.max(sMinimalWidth, width); 101281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin height = Math.max(sMinimalHeight, height); 1024de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1034de149ceb47f2c251f646419907424bfb67d2b64Owen Lin new MeasureHelper(this) 104d5be45065c4939d5a12b2294cb83230c1aaa8246Owen Lin .setPreferredContentSize(width, height) 1054de149ceb47f2c251f646419907424bfb67d2b64Owen Lin .measure(widthSpec, heightSpec); 1064de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } 1074de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1084de149ceb47f2c251f646419907424bfb67d2b64Owen Lin @Override 1094de149ceb47f2c251f646419907424bfb67d2b64Owen Lin protected void render(GLRootView root, GL11 gl) { 1104de149ceb47f2c251f646419907424bfb67d2b64Owen Lin Rect p = mPaddings; 1114de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1124de149ceb47f2c251f646419907424bfb67d2b64Owen Lin int width = getWidth() - p.left - p.right; 1134de149ceb47f2c251f646419907424bfb67d2b64Owen Lin int height = getHeight() - p.top - p.bottom; 1144de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1154de149ceb47f2c251f646419907424bfb67d2b64Owen Lin int xoffset = p.left; 1164de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1174de149ceb47f2c251f646419907424bfb67d2b64Owen Lin Transformation trans = root.getTransformation(); 1184de149ceb47f2c251f646419907424bfb67d2b64Owen Lin float oldAlpha = trans.getAlpha(); 1194de149ceb47f2c251f646419907424bfb67d2b64Owen Lin trans.setAlpha(oldAlpha * (mEnabled ? ENABLED_ALPHA : DISABLED_ALPHA)); 1204de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1214de149ceb47f2c251f646419907424bfb67d2b64Owen Lin ResourceTexture icon = mIcon; 1224de149ceb47f2c251f646419907424bfb67d2b64Owen Lin if (icon != null) { 12333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin icon.draw(root, xoffset, 12433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin p.top + (height - icon.getHeight()) / 2); 1254de149ceb47f2c251f646419907424bfb67d2b64Owen Lin xoffset += icon.getWidth(); 1264de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } else { 127281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin xoffset += sNoIconLeadingSpace; 1284de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } 1294de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1304de149ceb47f2c251f646419907424bfb67d2b64Owen Lin StringTexture title = mText; 131281be5337852cb4b5d24e0a0440c426cb96bbebdOwen Lin xoffset += sTextLeftPadding; 13233be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin int yoffset = p.top + (height - title.getHeight()) / 2; 13333be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin //TODO: cut the text if it is too long 13433be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin title.draw(root, xoffset, yoffset); 1354de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1364de149ceb47f2c251f646419907424bfb67d2b64Owen Lin ResourceTexture checkbox = mCheckBox; 13733be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin yoffset = p.top + (height - checkbox.getHeight()) / 2; 13833be45a1abaf6a8d611fb87b18945c84ab76c229Owen Lin checkbox.draw(root, width - checkbox.getWidth(), yoffset); 1394de149ceb47f2c251f646419907424bfb67d2b64Owen Lin trans.setAlpha(oldAlpha); 1404de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } 1414de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1424de149ceb47f2c251f646419907424bfb67d2b64Owen Lin public void setChecked(boolean checked) { 1434de149ceb47f2c251f646419907424bfb67d2b64Owen Lin mCheckBox = checked ? sCheckOn : sCheckOff; 1444de149ceb47f2c251f646419907424bfb67d2b64Owen Lin invalidate(); 1454de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } 1464de149ceb47f2c251f646419907424bfb67d2b64Owen Lin 1474de149ceb47f2c251f646419907424bfb67d2b64Owen Lin public void setEnabled(boolean enabled) { 1484de149ceb47f2c251f646419907424bfb67d2b64Owen Lin if (mEnabled == enabled) return; 1494de149ceb47f2c251f646419907424bfb67d2b64Owen Lin mEnabled = enabled; 1504de149ceb47f2c251f646419907424bfb67d2b64Owen Lin invalidate(); 1514de149ceb47f2c251f646419907424bfb67d2b64Owen Lin } 1524de149ceb47f2c251f646419907424bfb67d2b64Owen Lin} 153