1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera.ui; 18 19 import static com.android.camera.ui.GLRootView.dpToPixel; 20 import android.content.Context; 21 import android.graphics.Color; 22 import android.graphics.Rect; 23 import android.view.animation.Transformation; 24 25 import com.android.camera.R; 26 27 import javax.microedition.khronos.opengles.GL11; 28 29 class GLOptionItem extends GLView { 30 private static final int FONT_COLOR = Color.WHITE; 31 private static final float FONT_SIZE = 18; 32 33 private static final int MINIMAL_WIDTH = 120; 34 private static final int MINIMAL_HEIGHT = 32; 35 36 private static final int NO_ICON_LEADING_SPACE = 10; 37 private static final int TEXT_LEFT_PADDING = 6; 38 private static final int TEXT_RIGHT_PADDING = 10; 39 40 private static final float ENABLED_ALPHA = 1f; 41 private static final float DISABLED_ALPHA = 0.3f; 42 43 private static final int HORIZONTAL_PADDINGS = 4; 44 private static final int VERTICAL_PADDINGS = 2; 45 46 private static ResourceTexture sCheckOn; 47 private static ResourceTexture sCheckOff; 48 49 private static int sNoIconLeadingSpace; 50 private static int sTextLeftPadding; 51 private static int sTextRightPadding; 52 private static int sMinimalWidth; 53 private static int sMinimalHeight; 54 private static float sFontSize; 55 private static int sHorizontalPaddings = -1; 56 private static int sVerticalPaddings; 57 58 private final ResourceTexture mIcon; 59 private final StringTexture mText; 60 private boolean mEnabled = true; 61 62 private ResourceTexture mCheckBox; 63 64 65 private static void initializeStaticVariables(Context context) { 66 if (sCheckOn != null) return; 67 68 sCheckOn = new ResourceTexture(context, R.drawable.ic_menuselect_on); 69 sCheckOff = new ResourceTexture(context, R.drawable.ic_menuselect_off); 70 71 sNoIconLeadingSpace = dpToPixel(context, NO_ICON_LEADING_SPACE); 72 sTextLeftPadding = dpToPixel(context, TEXT_LEFT_PADDING); 73 sTextRightPadding = dpToPixel(context, TEXT_RIGHT_PADDING); 74 sMinimalWidth = dpToPixel(context, MINIMAL_WIDTH); 75 sMinimalHeight = dpToPixel(context, MINIMAL_HEIGHT); 76 sHorizontalPaddings = dpToPixel(context, HORIZONTAL_PADDINGS); 77 sVerticalPaddings = dpToPixel(context, VERTICAL_PADDINGS); 78 79 sFontSize = dpToPixel(context, FONT_SIZE); 80 } 81 82 public GLOptionItem(Context context, int iconId, String title) { 83 initializeStaticVariables(context); 84 mIcon = iconId == 0 ? null : new ResourceTexture(context, iconId); 85 mText = StringTexture.newInstance(title, sFontSize, FONT_COLOR); 86 mCheckBox = sCheckOff; 87 setPaddings(sHorizontalPaddings, 88 sVerticalPaddings, sHorizontalPaddings, sVerticalPaddings); 89 } 90 91 @Override 92 protected void onMeasure(int widthSpec, int heightSpec) { 93 int width = mIcon == null ? sNoIconLeadingSpace : mIcon.getWidth(); 94 width += mText.getWidth() + mCheckBox.getWidth(); 95 width += sTextRightPadding + sTextLeftPadding; 96 97 int height = Math.max(Math.max(mIcon == null ? 0 : mIcon.getHeight(), 98 mText.getHeight()), mCheckBox.getHeight()); 99 100 width = Math.max(sMinimalWidth, width); 101 height = Math.max(sMinimalHeight, height); 102 103 new MeasureHelper(this) 104 .setPreferredContentSize(width, height) 105 .measure(widthSpec, heightSpec); 106 } 107 108 @Override 109 protected void render(GLRootView root, GL11 gl) { 110 Rect p = mPaddings; 111 112 int width = getWidth() - p.left - p.right; 113 int height = getHeight() - p.top - p.bottom; 114 115 int xoffset = p.left; 116 117 Transformation trans = root.getTransformation(); 118 float oldAlpha = trans.getAlpha(); 119 trans.setAlpha(oldAlpha * (mEnabled ? ENABLED_ALPHA : DISABLED_ALPHA)); 120 121 ResourceTexture icon = mIcon; 122 if (icon != null) { 123 icon.draw(root, xoffset, 124 p.top + (height - icon.getHeight()) / 2); 125 xoffset += icon.getWidth(); 126 } else { 127 xoffset += sNoIconLeadingSpace; 128 } 129 130 StringTexture title = mText; 131 xoffset += sTextLeftPadding; 132 int yoffset = p.top + (height - title.getHeight()) / 2; 133 //TODO: cut the text if it is too long 134 title.draw(root, xoffset, yoffset); 135 136 ResourceTexture checkbox = mCheckBox; 137 yoffset = p.top + (height - checkbox.getHeight()) / 2; 138 checkbox.draw(root, width - checkbox.getWidth(), yoffset); 139 trans.setAlpha(oldAlpha); 140 } 141 142 public void setChecked(boolean checked) { 143 mCheckBox = checked ? sCheckOn : sCheckOff; 144 invalidate(); 145 } 146 147 public void setEnabled(boolean enabled) { 148 if (mEnabled == enabled) return; 149 mEnabled = enabled; 150 invalidate(); 151 } 152 } 153