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 */
16
17package com.android.gallery3d.filtershow.editors;
18
19import android.app.ActionBar;
20import android.content.Context;
21import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.BitmapFactory;
24import android.graphics.Color;
25import android.graphics.drawable.GradientDrawable;
26import android.view.LayoutInflater;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.View.OnClickListener;
31import android.view.ViewGroup;
32import android.widget.Button;
33import android.widget.FrameLayout;
34import android.widget.ImageButton;
35import android.widget.ImageView;
36import android.widget.LinearLayout;
37import android.widget.PopupMenu;
38import android.widget.SeekBar;
39
40import com.android.gallery3d.R;
41import com.android.gallery3d.filtershow.FilterShowActivity;
42import com.android.gallery3d.filtershow.colorpicker.ColorHueView;
43import com.android.gallery3d.filtershow.colorpicker.ColorListener;
44import com.android.gallery3d.filtershow.colorpicker.ColorOpacityView;
45import com.android.gallery3d.filtershow.colorpicker.ColorSVRectView;
46import com.android.gallery3d.filtershow.controller.BitmapCaller;
47import com.android.gallery3d.filtershow.controller.ColorChooser;
48import com.android.gallery3d.filtershow.controller.FilterView;
49import com.android.gallery3d.filtershow.controller.ParameterColor;
50import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation;
51import com.android.gallery3d.filtershow.filters.FilterRepresentation;
52import com.android.gallery3d.filtershow.filters.ImageFilterDraw;
53import com.android.gallery3d.filtershow.imageshow.ImageDraw;
54
55public class EditorDraw extends ParametricEditor implements FilterView {
56    private static final String LOGTAG = "EditorDraw";
57    public static final int ID = R.id.editorDraw;
58    public ImageDraw mImageDraw;
59    private static final int MODE_SIZE = FilterDrawRepresentation.PARAM_SIZE;
60    private static final int MODE_SIZEE = FilterDrawRepresentation.PARAM_SIZE;
61    private static final int MODE_STYLE = FilterDrawRepresentation.PARAM_STYLE;
62    private static final int MODE_COLOR = FilterDrawRepresentation.PARAM_COLOR;
63    int[] brushIcons = {
64            R.drawable.brush_flat,
65            R.drawable.brush_round,
66            R.drawable.brush_gauss,
67            R.drawable.brush_marker,
68            R.drawable.brush_spatter
69    };
70
71    int[] mBasColors = {
72            FilterDrawRepresentation.DEFAULT_MENU_COLOR1,
73            FilterDrawRepresentation.DEFAULT_MENU_COLOR2,
74            FilterDrawRepresentation.DEFAULT_MENU_COLOR3,
75            FilterDrawRepresentation.DEFAULT_MENU_COLOR4,
76            FilterDrawRepresentation.DEFAULT_MENU_COLOR5,
77    };
78    private EditorDrawTabletUI mTabletUI;
79    private String mParameterString;
80    private int mSelectedColorButton;
81    private String mDrawString = null;
82
83    public EditorDraw() {
84        super(ID);
85    }
86
87    @Override
88    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
89        FilterDrawRepresentation rep = getDrawRep();
90        if (mDrawString != null) {
91            mImageDraw.displayDrawLook();
92            return mDrawString;
93        }
94        if (rep == null) {
95            return "";
96        }
97        if (!ParametricEditor.useCompact(mContext)) {
98
99        }
100        if (mParameterString == null) {
101            mParameterString = "";
102        }
103        String paramString;
104        String val = rep.getValueString();
105
106        mImageDraw.displayDrawLook();
107        return mParameterString + val;
108    }
109
110    @Override
111    public void createEditor(Context context, FrameLayout frameLayout) {
112        mView = mImageShow = mImageDraw = new ImageDraw(context);
113        super.createEditor(context, frameLayout);
114        mImageDraw.setEditor(this);
115
116    }
117
118    @Override
119    public void reflectCurrentFilter() {
120        super.reflectCurrentFilter();
121        FilterRepresentation rep = getLocalRepresentation();
122        if (rep != null && getLocalRepresentation() instanceof FilterDrawRepresentation) {
123            FilterDrawRepresentation drawRep = (FilterDrawRepresentation) getLocalRepresentation();
124            mImageDraw.setFilterDrawRepresentation(drawRep);
125            if (!ParametricEditor.useCompact(mContext)) {
126                if (mTabletUI != null) {
127
128                    mTabletUI.setDrawRepresentation(drawRep);
129                }
130                return;
131            }
132
133            drawRep.getParam(FilterDrawRepresentation.PARAM_STYLE).setFilterView(this);
134            drawRep.setPramMode(FilterDrawRepresentation.PARAM_COLOR);
135            mParameterString = mContext.getString(R.string.draw_color);
136            control(drawRep.getCurrentParam(), mEditControl);
137        }
138    }
139
140    @Override
141    public void openUtilityPanel(final LinearLayout accessoryViewList) {
142        Button view = (Button) accessoryViewList.findViewById(R.id.applyEffect);
143
144        view.setText(mContext.getString(R.string.draw_color));
145        view.setOnClickListener(new OnClickListener() {
146
147            @Override
148            public void onClick(View arg0) {
149                showPopupMenu(accessoryViewList);
150            }
151        });
152
153    }
154
155    @Override
156    public boolean showsSeekBar() {
157        return false;
158    }
159
160    private void showPopupMenu(LinearLayout accessoryViewList) {
161        final Button button = (Button) accessoryViewList.findViewById(
162                R.id.applyEffect);
163        if (button == null) {
164            return;
165        }
166        final PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), button);
167        popupMenu.getMenuInflater().inflate(R.menu.filtershow_menu_draw, popupMenu.getMenu());
168        if (!ParametricEditor.useCompact(mContext)) {
169            Menu menu = popupMenu.getMenu();
170            int count = menu.size();
171            for (int i = 0; i < count; i++) {
172                MenuItem item = menu.getItem(i);
173                if (item.getItemId() != R.id.draw_menu_clear) {
174                    item.setVisible(false);
175                }
176            }
177            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
178
179                @Override
180                public boolean onMenuItemClick(MenuItem item) {
181                    clearDrawing();
182                    return true;
183                }
184            });
185        } else {
186            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
187
188                @Override
189                public boolean onMenuItemClick(MenuItem item) {
190                    selectMenuItem(item);
191                    return true;
192                }
193            });
194        }
195        popupMenu.show();
196        ((FilterShowActivity)mContext).onShowMenu(popupMenu);
197    }
198
199    protected void selectMenuItem(MenuItem item) {
200        ImageFilterDraw filter = (ImageFilterDraw) mImageShow.getCurrentFilter();
201        FilterDrawRepresentation rep = getDrawRep();
202        if (rep == null) {
203            return;
204        }
205
206        switch (item.getItemId()) {
207            case R.id.draw_menu_clear:
208                clearDrawing();
209                break;
210            case R.id.draw_menu_size:
211                rep.setPramMode(FilterDrawRepresentation.PARAM_SIZE);
212                break;
213            case R.id.draw_menu_style:
214                rep.setPramMode(FilterDrawRepresentation.PARAM_STYLE);
215                break;
216            case R.id.draw_menu_color:
217                rep.setPramMode(FilterDrawRepresentation.PARAM_COLOR);
218                break;
219        }
220        if (item.getItemId() != R.id.draw_menu_clear) {
221            mParameterString = item.getTitle().toString();
222            updateText();
223        }
224        if (mControl instanceof ColorChooser) {
225            ColorChooser c = (ColorChooser) mControl;
226            mBasColors = c.getColorSet();
227        }
228        control(rep.getCurrentParam(), mEditControl);
229        if (mControl instanceof ColorChooser) {
230            ColorChooser c = (ColorChooser) mControl;
231            c.setColorSet(mBasColors);
232        }
233        mControl.updateUI();
234        mView.invalidate();
235    }
236
237    public void clearDrawing(){
238        ImageDraw idraw = (ImageDraw) mImageShow;
239        idraw.resetParameter();
240        commitLocalRepresentation();
241    }
242
243    @Override
244    public void setUtilityPanelUI(View actionButton, View editControl) {
245        if (ParametricEditor.useCompact(mContext)) {
246            super.setUtilityPanelUI(actionButton, editControl);
247            return;
248        }
249
250        mSeekBar = (SeekBar) editControl.findViewById(R.id.primarySeekBar);
251        if (mSeekBar != null) {
252            mSeekBar.setVisibility(View.GONE);
253        }
254        LayoutInflater inflater =
255                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
256        LinearLayout lp = (LinearLayout) inflater.inflate(
257                R.layout.filtershow_draw_ui, (ViewGroup) editControl, true);
258
259        mTabletUI = new EditorDrawTabletUI(this, mContext, lp);
260        mDrawString = mContext.getResources().getString(R.string.imageDraw).toUpperCase();
261        setMenuIcon(true);
262
263    }
264
265    FilterDrawRepresentation getDrawRep() {
266        FilterRepresentation rep = getLocalRepresentation();
267        if (rep instanceof FilterDrawRepresentation) {
268            return (FilterDrawRepresentation) rep;
269        }
270        return null;
271    }
272
273    @Override
274    public void computeIcon(int index, BitmapCaller caller) {
275        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), brushIcons[index]);
276        caller.available(bitmap);
277    }
278
279    public int getBrushIcon(int type) {
280        return brushIcons[type];
281    }
282
283}
284