OSDN Git Service

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