1/*
2 * Copyright (C) 2013 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 */
16package com.android.gallery3d.filtershow.editors;
17
18import android.content.Context;
19import android.view.LayoutInflater;
20import android.view.MenuItem;
21import android.view.View;
22import android.view.View.OnClickListener;
23import android.view.ViewGroup;
24import android.widget.Button;
25import android.widget.FrameLayout;
26import android.widget.LinearLayout;
27import android.widget.PopupMenu;
28import android.widget.SeekBar;
29import android.widget.SeekBar.OnSeekBarChangeListener;
30import android.widget.TextView;
31import android.widget.ToggleButton;
32
33import com.android.gallery3d.R;
34import com.android.gallery3d.filtershow.FilterShowActivity;
35import com.android.gallery3d.filtershow.controller.Control;
36import com.android.gallery3d.filtershow.controller.FilterView;
37import com.android.gallery3d.filtershow.controller.Parameter;
38import com.android.gallery3d.filtershow.controller.ParameterActionAndInt;
39import com.android.gallery3d.filtershow.filters.FilterGradRepresentation;
40import com.android.gallery3d.filtershow.filters.FilterRepresentation;
41import com.android.gallery3d.filtershow.imageshow.ImageGrad;
42import com.android.gallery3d.filtershow.imageshow.MasterImage;
43
44public class EditorGrad extends ParametricEditor
45        implements OnSeekBarChangeListener, ParameterActionAndInt {
46    private static final String LOGTAG = "EditorGrad";
47    public static final int ID = R.id.editorGrad;
48    PopupMenu mPopupMenu;
49    ToggleButton mAddModeButton;
50    String mEffectName = "";
51    private static final int MODE_BRIGHTNESS = FilterGradRepresentation.PARAM_BRIGHTNESS;
52    private static final int MODE_SATURATION = FilterGradRepresentation.PARAM_SATURATION;
53    private static final int MODE_CONTRAST = FilterGradRepresentation.PARAM_CONTRAST;
54    private static final int ADD_ICON = R.drawable.ic_grad_add;
55    private static final int DEL_ICON = R.drawable.ic_grad_del;
56    private int mSliderMode = MODE_BRIGHTNESS;
57    ImageGrad mImageGrad;
58    ParamAdapter []mAdapters = new ParamAdapter[3];
59    public EditorGrad() {
60        super(ID, R.layout.filtershow_grad_editor, R.id.gradEditor);
61    }
62
63    @Override
64    public void createEditor(Context context, FrameLayout frameLayout) {
65        super.createEditor(context, frameLayout);
66        mImageGrad = (ImageGrad) mImageShow;
67        mImageGrad.setEditor(this);
68
69    }
70
71    public void clearAddMode() {
72        mAddModeButton.setChecked(false);
73        FilterRepresentation tmpRep = getLocalRepresentation();
74        if (tmpRep instanceof FilterGradRepresentation) {
75            updateMenuItems((FilterGradRepresentation) tmpRep);
76        }
77    }
78
79    @Override
80    public void reflectCurrentFilter() {
81        super.reflectCurrentFilter();
82        FilterRepresentation tmpRep = getLocalRepresentation();
83        if (tmpRep instanceof FilterGradRepresentation) {
84            FilterGradRepresentation rep = (FilterGradRepresentation) tmpRep;
85            boolean f = rep.showParameterValue();
86
87            mImageGrad.setRepresentation(rep);
88        }
89    }
90
91    public void updateSeekBar(FilterGradRepresentation rep) {
92        if (ParametricEditor.useCompact(mContext)) {
93            mControl.updateUI();
94        } else {
95            updateParameters();
96        }
97    }
98
99    @Override
100    public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
101        FilterRepresentation tmpRep = getLocalRepresentation();
102        if (tmpRep instanceof FilterGradRepresentation) {
103            FilterGradRepresentation rep = (FilterGradRepresentation) tmpRep;
104            int min = rep.getParameterMin(mSliderMode);
105            int value = progress + min;
106            rep.setParameter(mSliderMode, value);
107            mView.invalidate();
108            commitLocalRepresentation();
109        }
110    }
111
112    @Override
113    public void openUtilityPanel(final LinearLayout accessoryViewList) {
114        Button view = (Button) accessoryViewList.findViewById(R.id.applyEffect);
115        if (useCompact(mContext)) {
116            view.setText(mContext.getString(R.string.editor_grad_brightness));
117            view.setOnClickListener(new OnClickListener() {
118                @Override
119                public void onClick(View arg0) {
120                    showPopupMenu(accessoryViewList);
121                }
122            });
123
124            setUpPopupMenu(view);
125            setEffectName();
126        } else {
127            view.setText(mContext.getString(R.string.grad));
128        }
129    }
130
131    private void updateMenuItems(FilterGradRepresentation rep) {
132        int n = rep.getNumberOfBands();
133    }
134
135    public void setEffectName() {
136        if (mPopupMenu != null) {
137            MenuItem item = mPopupMenu.getMenu().findItem(R.id.editor_grad_brightness);
138            mEffectName = item.getTitle().toString();
139        }
140    }
141
142    @Override
143    public void setUtilityPanelUI(View actionButton, View editControl) {
144        if (ParametricEditor.useCompact(mContext)) {
145            super.setUtilityPanelUI(actionButton, editControl);
146            return;
147        }
148        mSeekBar = (SeekBar) editControl.findViewById(R.id.primarySeekBar);
149        if (mSeekBar != null) {
150            mSeekBar.setVisibility(View.GONE);
151        }
152        LayoutInflater inflater =
153                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
154        LinearLayout lp = (LinearLayout) inflater.inflate(
155                R.layout.filtershow_grad_ui, (ViewGroup) editControl, true);
156
157        mAdapters[0] = new ParamAdapter(R.id.gradContrastSeekBar, R.id.gradContrastValue,
158                lp, MODE_CONTRAST);
159        mAdapters[1] = new ParamAdapter(R.id.gradBrightnessSeekBar, R.id.gradBrightnessValue,
160                lp, MODE_BRIGHTNESS);
161        mAdapters[2] = new ParamAdapter(R.id.gradSaturationSeekBar, R.id.gradSaturationValue,
162                lp, MODE_SATURATION);
163        lp.findViewById(R.id.gradAddButton).setOnClickListener(new OnClickListener() {
164            @Override
165            public void onClick(View view) {
166                fireLeftAction();
167            }
168        });
169        lp.findViewById(R.id.gradDelButton).setOnClickListener(new OnClickListener() {
170            @Override
171            public void onClick(View view) {
172                fireRightAction();
173            }
174        });
175        setMenuIcon(false);
176    }
177
178    public void updateParameters() {
179        FilterGradRepresentation rep = getGradRepresentation();
180        for (int i = 0; i < mAdapters.length; i++) {
181            mAdapters[i].updateValues(rep);
182        }
183    }
184
185    private class ParamAdapter implements OnSeekBarChangeListener {
186        SeekBar mSlider;
187        TextView mTextView;
188        int mMin = -100;
189        int mMax = 100;
190        int mMode;
191
192        public ParamAdapter(int seekId, int textId, LinearLayout layout, int mode) {
193            mSlider = (SeekBar) layout.findViewById(seekId);
194            mTextView = (TextView) layout.findViewById(textId);
195            mSlider.setMax(mMax - mMin);
196            mMode = mode;
197            FilterGradRepresentation rep = getGradRepresentation();
198            if (rep != null){
199                updateValues(rep);
200            }
201            mSlider.setOnSeekBarChangeListener(this);
202        }
203
204        public void updateValues(FilterGradRepresentation rep) {
205            int value = rep.getParameter(mMode);
206            mTextView.setText(Integer.toString(value));
207            mSlider.setProgress(value - mMin);
208        }
209
210        @Override
211        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
212            FilterGradRepresentation rep = getGradRepresentation();
213            int value = progress + mMin;
214            rep.setParameter(mMode, value);
215            if (mSliderMode != mMode) {
216                mSliderMode = mMode;
217                mEffectName = mContext.getResources().getString(getModeNameid(mMode));
218                mEffectName = mEffectName.toUpperCase();
219            }
220            mTextView.setText(Integer.toString(value));
221            mView.invalidate();
222            commitLocalRepresentation();
223        }
224
225        private int getModeNameid(int mode) {
226            switch (mode) {
227                case MODE_CONTRAST:
228                    return R.string.editor_grad_contrast;
229                case MODE_BRIGHTNESS:
230                    return R.string.editor_grad_brightness;
231                case MODE_SATURATION:
232                    return R.string.editor_grad_saturation;
233            }
234            return 0;
235        }
236
237        @Override
238        public void onStartTrackingTouch(SeekBar seekBar) {
239
240        }
241
242        @Override
243        public void onStopTrackingTouch(SeekBar seekBar) {
244
245        }
246    }
247
248    private void showPopupMenu(LinearLayout accessoryViewList) {
249        Button button = (Button) accessoryViewList.findViewById(R.id.applyEffect);
250        if (button == null) {
251            return;
252        }
253
254        if (mPopupMenu == null) {
255            setUpPopupMenu(button);
256        }
257        mPopupMenu.show();
258        ((FilterShowActivity)mContext).onShowMenu(mPopupMenu);
259    }
260
261    private void setUpPopupMenu(Button button) {
262        mPopupMenu = new PopupMenu(mImageShow.getActivity(), button);
263        mPopupMenu.getMenuInflater()
264                .inflate(R.menu.filtershow_menu_grad, mPopupMenu.getMenu());
265        FilterGradRepresentation rep = (FilterGradRepresentation) getLocalRepresentation();
266        if (rep == null) {
267            return;
268        }
269        updateMenuItems(rep);
270        hackFixStrings(mPopupMenu.getMenu());
271        setEffectName();
272        updateText();
273
274        mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
275            @Override
276            public boolean onMenuItemClick(MenuItem item) {
277                FilterRepresentation tmpRep = getLocalRepresentation();
278
279                if (tmpRep instanceof FilterGradRepresentation) {
280                    FilterGradRepresentation rep = (FilterGradRepresentation) tmpRep;
281                    int cmdID = item.getItemId();
282                    switch (cmdID) {
283                        case R.id.editor_grad_brightness:
284                            mSliderMode = MODE_BRIGHTNESS;
285                            mEffectName = item.getTitle().toString();
286                            break;
287                        case R.id.editor_grad_contrast:
288                            mSliderMode = MODE_CONTRAST;
289                            mEffectName = item.getTitle().toString();
290                            break;
291                        case R.id.editor_grad_saturation:
292                            mSliderMode = MODE_SATURATION;
293                            mEffectName = item.getTitle().toString();
294                            break;
295                    }
296                    updateMenuItems(rep);
297                    updateSeekBar(rep);
298
299                    commitLocalRepresentation();
300                    mView.invalidate();
301                }
302                return true;
303            }
304        });
305    }
306
307    @Override
308    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
309        FilterGradRepresentation rep = getGradRepresentation();
310        if (rep == null) {
311            return mEffectName;
312        }
313        int val = rep.getParameter(mSliderMode);
314        return mEffectName.toUpperCase() + ((val > 0) ? " +" : " ") + val;
315    }
316
317    private FilterGradRepresentation getGradRepresentation() {
318        FilterRepresentation tmpRep = getLocalRepresentation();
319        if (tmpRep instanceof FilterGradRepresentation) {
320            return (FilterGradRepresentation) tmpRep;
321        }
322        return null;
323    }
324
325    @Override
326    public int getMaximum() {
327        FilterGradRepresentation rep = getGradRepresentation();
328        if (rep == null) {
329            return 0;
330        }
331        return rep.getParameterMax(mSliderMode);
332    }
333
334    @Override
335    public int getMinimum() {
336        FilterGradRepresentation rep = getGradRepresentation();
337        if (rep == null) {
338            return 0;
339        }
340        return rep.getParameterMin(mSliderMode);
341    }
342
343    @Override
344    public int getDefaultValue() {
345        return 0;
346    }
347
348    @Override
349    public int getValue() {
350        FilterGradRepresentation rep = getGradRepresentation();
351        if (rep == null) {
352            return 0;
353        }
354        return rep.getParameter(mSliderMode);
355    }
356
357    @Override
358    public String getValueString() {
359        return null;
360    }
361
362    @Override
363    public void setValue(int value) {
364        FilterGradRepresentation rep = getGradRepresentation();
365        if (rep == null) {
366            return;
367        }
368        rep.setParameter(mSliderMode, value);
369    }
370
371    @Override
372    public String getParameterName() {
373        return mEffectName;
374    }
375
376    @Override
377    public String getParameterType() {
378        return sParameterType;
379    }
380
381    @Override
382    public void setController(Control c) {
383
384    }
385
386    @Override
387    public void fireLeftAction() {
388        FilterGradRepresentation rep = getGradRepresentation();
389        if (rep == null) {
390            return;
391        }
392        rep.addBand(MasterImage.getImage().getOriginalBounds());
393        updateMenuItems(rep);
394        updateSeekBar(rep);
395
396        commitLocalRepresentation();
397        mView.invalidate();
398    }
399
400    @Override
401    public int getLeftIcon() {
402        return ADD_ICON;
403    }
404
405    @Override
406    public void fireRightAction() {
407        FilterGradRepresentation rep = getGradRepresentation();
408        if (rep == null) {
409            return;
410        }
411        rep.deleteCurrentBand();
412
413        updateMenuItems(rep);
414        updateSeekBar(rep);
415        commitLocalRepresentation();
416        mView.invalidate();
417    }
418
419    @Override
420    public int getRightIcon() {
421        return DEL_ICON;
422    }
423
424    @Override
425    public void setFilterView(FilterView editor) {
426
427    }
428
429    @Override
430    public void copyFrom(Parameter src) {
431
432    }
433}
434