1 /* 2 * Copyright (C) 2012 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.gallery3d.filtershow.editors; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import android.view.LayoutInflater; 22 import android.view.MenuItem; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.FrameLayout; 26 import android.widget.LinearLayout; 27 import android.widget.PopupMenu; 28 import android.widget.SeekBar; 29 import android.widget.TextView; 30 31 import com.android.gallery3d.R; 32 import com.android.gallery3d.filtershow.FilterShowActivity; 33 import com.android.gallery3d.filtershow.controller.BasicParameterInt; 34 import com.android.gallery3d.filtershow.controller.Parameter; 35 import com.android.gallery3d.filtershow.filters.FilterVignetteRepresentation; 36 import com.android.gallery3d.filtershow.filters.FilterRepresentation; 37 import com.android.gallery3d.filtershow.imageshow.ImageVignette; 38 39 public class EditorVignette extends ParametricEditor { 40 public static final int ID = R.id.vignetteEditor; 41 private static final String LOGTAG = "EditorVignettePlanet"; 42 ImageVignette mImageVignette; 43 44 private SeekBar mVignetteBar; 45 private SeekBar mExposureBar; 46 private SeekBar mSaturationBar; 47 private SeekBar mContrastBar; 48 private SeekBar mFalloffBar; 49 50 51 private TextView mVignetteValue; 52 private TextView mExposureValue; 53 private TextView mSaturationValue; 54 private TextView mContrastValue; 55 private TextView mFalloffValue; 56 57 private SwapButton mButton; 58 private final Handler mHandler = new Handler(); 59 60 int[] mMenuStrings = { 61 R.string.vignette_main, 62 R.string.vignette_exposure, 63 R.string.vignette_saturation, 64 R.string.vignette_contrast, 65 R.string.vignette_falloff, 66 }; 67 68 String mCurrentlyEditing = null; 69 70 71 public EditorVignette() { 72 super(ID, R.layout.filtershow_vignette_editor, R.id.imageVignette); 73 } 74 75 @Override 76 public void createEditor(Context context, FrameLayout frameLayout) { 77 super.createEditor(context, frameLayout); 78 mImageVignette = (ImageVignette) mImageShow; 79 mImageVignette.setEditor(this); 80 } 81 82 @Override 83 public void reflectCurrentFilter() { 84 if (useCompact(mContext)) { 85 super.reflectCurrentFilter(); 86 87 FilterRepresentation rep = getLocalRepresentation(); 88 if (rep != null && getLocalRepresentation() instanceof FilterVignetteRepresentation) { 89 FilterVignetteRepresentation drawRep = (FilterVignetteRepresentation) rep; 90 mImageVignette.setRepresentation(drawRep); 91 } 92 updateText(); 93 return; 94 } 95 mLocalRepresentation = null; 96 if (getLocalRepresentation() != null 97 && getLocalRepresentation() instanceof FilterVignetteRepresentation) { 98 FilterVignetteRepresentation rep = 99 (FilterVignetteRepresentation) getLocalRepresentation(); 100 int min; 101 int []mode = { 102 FilterVignetteRepresentation.MODE_VIGNETTE, 103 FilterVignetteRepresentation.MODE_EXPOSURE, 104 FilterVignetteRepresentation.MODE_SATURATION, 105 FilterVignetteRepresentation.MODE_CONTRAST, 106 FilterVignetteRepresentation.MODE_FALLOFF 107 }; 108 SeekBar []sliders = { 109 mVignetteBar, 110 mExposureBar, 111 mSaturationBar, 112 mContrastBar, 113 mFalloffBar 114 }; 115 TextView []label = { 116 mVignetteValue, 117 mExposureValue, 118 mSaturationValue, 119 mContrastValue, 120 mFalloffValue 121 }; 122 for (int i = 0; i < mode.length; i++) { 123 BasicParameterInt p = (BasicParameterInt) rep.getFilterParameter(mode[i]); 124 int value = p.getValue(); 125 sliders[i].setMax(p.getMaximum() - p.getMinimum()); 126 sliders[i].setProgress(value - p.getMinimum()); 127 label[i].setText("" + value); 128 } 129 130 mImageVignette.setRepresentation(rep); 131 String text = mContext.getString(rep.getTextId()).toUpperCase(); 132 mFilterTitle.setText(text); 133 updateText(); 134 } 135 } 136 137 138 139 @Override 140 public String calculateUserMessage(Context context, String effectName, Object parameterValue) { 141 FilterRepresentation rep = getLocalRepresentation(); 142 if (rep == null || !(rep instanceof FilterVignetteRepresentation)) { 143 return ""; 144 } 145 FilterVignetteRepresentation csrep = (FilterVignetteRepresentation) rep; 146 int mode = csrep.getParameterMode(); 147 String paramString; 148 149 paramString = mContext.getString(mMenuStrings[mode]); 150 151 int val = csrep.getCurrentParameter(); 152 return paramString + ((val > 0) ? " +" : " ") + val; 153 } 154 155 @Override 156 public void openUtilityPanel(final LinearLayout accessoryViewList) { 157 mButton = (SwapButton) accessoryViewList.findViewById(R.id.applyEffect); 158 mButton.setText(mContext.getString(R.string.vignette_main)); 159 160 if (useCompact(mContext)) { 161 final PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), mButton); 162 163 popupMenu.getMenuInflater().inflate(R.menu.filtershow_menu_vignette, 164 popupMenu.getMenu()); 165 166 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 167 @Override 168 public boolean onMenuItemClick(MenuItem item) { 169 selectMenuItem(item); 170 return true; 171 } 172 }); 173 mButton.setOnClickListener(new View.OnClickListener() { 174 @Override 175 public void onClick(View arg0) { 176 popupMenu.show(); 177 ((FilterShowActivity)mContext).onShowMenu(popupMenu); 178 } 179 }); 180 mButton.setListener(this); 181 182 FilterVignetteRepresentation csrep = getVignetteRep(); 183 String menuString = mContext.getString(mMenuStrings[0]); 184 switchToMode(csrep, FilterVignetteRepresentation.MODE_VIGNETTE, menuString); 185 } else { 186 mButton.setText(mContext.getString(R.string.vignette_main)); 187 } 188 } 189 190 @Override 191 public void setUtilityPanelUI(View actionButton, View editControl) { 192 if (useCompact(mContext)) { 193 super.setUtilityPanelUI(actionButton, editControl); 194 return; 195 } 196 mActionButton = actionButton; 197 mEditControl = editControl; 198 mEditTitle.setCompoundDrawables(null, null, null, null); 199 LinearLayout group = (LinearLayout) editControl; 200 LayoutInflater inflater = 201 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 202 LinearLayout controls = (LinearLayout) inflater.inflate( 203 R.layout.filtershow_vignette_controls, group, false); 204 ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams( 205 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 206 controls.setLayoutParams(lp); 207 group.removeAllViews(); 208 group.addView(controls); 209 210 mVignetteBar = (SeekBar) controls.findViewById(R.id.mainVignetteSeekbar); 211 mVignetteBar.setMax(200); 212 mVignetteBar.setOnSeekBarChangeListener(this); 213 mVignetteValue = (TextView) controls.findViewById(R.id.mainVignetteValue); 214 mExposureBar = (SeekBar) controls.findViewById(R.id.exposureSeekBar); 215 mExposureBar.setMax(200); 216 mExposureBar.setOnSeekBarChangeListener(this); 217 mExposureValue = (TextView) controls.findViewById(R.id.exposureValue); 218 mSaturationBar = (SeekBar) controls.findViewById(R.id.saturationSeekBar); 219 mSaturationBar.setMax(200); 220 mSaturationBar.setOnSeekBarChangeListener(this); 221 mSaturationValue = (TextView) controls.findViewById(R.id.saturationValue); 222 mContrastBar = (SeekBar) controls.findViewById(R.id.contrastSeekBar); 223 mContrastBar.setMax(200); 224 mContrastBar.setOnSeekBarChangeListener(this); 225 mContrastValue = (TextView) controls.findViewById(R.id.contrastValue); 226 mFalloffBar = (SeekBar) controls.findViewById(R.id.falloffSeekBar); 227 mFalloffBar.setMax(200); 228 mFalloffBar.setOnSeekBarChangeListener(this); 229 mFalloffValue = (TextView) controls.findViewById(R.id.falloffValue); 230 } 231 232 public int getParameterIndex(int id) { 233 switch (id) { 234 case R.id.editor_vignette_main: 235 return FilterVignetteRepresentation.MODE_VIGNETTE; 236 case R.id.editor_vignette_saturation: 237 return FilterVignetteRepresentation.MODE_SATURATION; 238 case R.id.editor_vignette_contrast: 239 return FilterVignetteRepresentation.MODE_CONTRAST; 240 case R.id.editor_vignette_exposure: 241 return FilterVignetteRepresentation.MODE_EXPOSURE; 242 case R.id.editor_vignette_falloff: 243 return FilterVignetteRepresentation.MODE_FALLOFF; 244 } 245 return -1; 246 } 247 248 @Override 249 public void detach() { 250 if (mButton == null) { 251 return; 252 } 253 mButton.setListener(null); 254 mButton.setOnClickListener(null); 255 } 256 257 private void updateSeekBar(FilterVignetteRepresentation rep) { 258 mControl.updateUI(); 259 } 260 261 @Override 262 protected Parameter getParameterToEdit(FilterRepresentation rep) { 263 if (rep instanceof FilterVignetteRepresentation) { 264 FilterVignetteRepresentation csrep = (FilterVignetteRepresentation) rep; 265 Parameter param = csrep.getFilterParameter(csrep.getParameterMode()); 266 267 return param; 268 } 269 return null; 270 } 271 272 private FilterVignetteRepresentation getVignetteRep() { 273 FilterRepresentation rep = getLocalRepresentation(); 274 if (rep != null 275 && rep instanceof FilterVignetteRepresentation) { 276 FilterVignetteRepresentation csrep = (FilterVignetteRepresentation) rep; 277 return csrep; 278 } 279 return null; 280 } 281 282 protected void selectMenuItem(MenuItem item) { 283 if (getLocalRepresentation() != null 284 && getLocalRepresentation() instanceof FilterVignetteRepresentation) { 285 FilterVignetteRepresentation csrep = 286 (FilterVignetteRepresentation) getLocalRepresentation(); 287 288 switchToMode(csrep, getParameterIndex(item.getItemId()), item.getTitle().toString()); 289 } 290 } 291 292 protected void switchToMode(FilterVignetteRepresentation csrep, int mode, String title) { 293 if (csrep == null) { 294 return; 295 } 296 csrep.setParameterMode(mode); 297 mCurrentlyEditing = title; 298 mButton.setText(mCurrentlyEditing); 299 { 300 Parameter param = getParameterToEdit(csrep); 301 302 control(param, mEditControl); 303 } 304 updateSeekBar(csrep); 305 mView.invalidate(); 306 } 307 308 @Override 309 public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) { 310 FilterVignetteRepresentation rep = getVignetteRep(); 311 int value = progress; 312 BasicParameterInt p; 313 switch (sbar.getId()) { 314 case R.id.mainVignetteSeekbar: 315 rep.setParameterMode(FilterVignetteRepresentation.MODE_VIGNETTE); 316 p = rep.getFilterParameter(rep.getParameterMode()); 317 value += p.getMinimum(); 318 mVignetteValue.setText("" + value); 319 break; 320 case R.id.exposureSeekBar: 321 rep.setParameterMode(FilterVignetteRepresentation.MODE_EXPOSURE); 322 p = rep.getFilterParameter(rep.getParameterMode()); 323 value += p.getMinimum(); 324 mExposureValue.setText("" + value); 325 break; 326 case R.id.saturationSeekBar: 327 rep.setParameterMode(FilterVignetteRepresentation.MODE_SATURATION); 328 p = rep.getFilterParameter(rep.getParameterMode()); 329 value += p.getMinimum(); 330 mSaturationValue.setText("" + value); 331 break; 332 case R.id.contrastSeekBar: 333 rep.setParameterMode(FilterVignetteRepresentation.MODE_CONTRAST); 334 p = rep.getFilterParameter(rep.getParameterMode()); 335 value += p.getMinimum(); 336 mContrastValue.setText("" + value); 337 break; 338 case R.id.falloffSeekBar: 339 rep.setParameterMode(FilterVignetteRepresentation.MODE_FALLOFF); 340 p = rep.getFilterParameter(rep.getParameterMode()); 341 value += p.getMinimum(); 342 mFalloffValue.setText("" + value); 343 break; 344 } 345 rep.setCurrentParameter(value); 346 commitLocalRepresentation(); 347 } 348 349 @Override 350 public void swapLeft(MenuItem item) { 351 super.swapLeft(item); 352 mButton.setTranslationX(0); 353 mButton.animate().translationX(mButton.getWidth()).setDuration(SwapButton.ANIM_DURATION); 354 Runnable updateButton = new Runnable() { 355 @Override 356 public void run() { 357 mButton.animate().cancel(); 358 mButton.setTranslationX(0); 359 } 360 }; 361 mHandler.postDelayed(updateButton, SwapButton.ANIM_DURATION); 362 selectMenuItem(item); 363 } 364 365 @Override 366 public void swapRight(MenuItem item) { 367 super.swapRight(item); 368 mButton.setTranslationX(0); 369 mButton.animate().translationX(-mButton.getWidth()).setDuration(SwapButton.ANIM_DURATION); 370 Runnable updateButton = new Runnable() { 371 @Override 372 public void run() { 373 mButton.animate().cancel(); 374 mButton.setTranslationX(0); 375 } 376 }; 377 mHandler.postDelayed(updateButton, SwapButton.ANIM_DURATION); 378 selectMenuItem(item); 379 } 380 } 381