OSDN Git Service

fix RGB text
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / editors / Editor.java
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.util.AttributeSet;
21 import android.view.LayoutInflater;
22 import android.view.Menu;
23 import android.view.MenuItem;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.Button;
27 import android.widget.FrameLayout;
28 import android.widget.LinearLayout;
29 import android.widget.PopupMenu;
30 import android.widget.SeekBar;
31 import android.widget.SeekBar.OnSeekBarChangeListener;
32
33 import com.android.gallery3d.R;
34 import com.android.gallery3d.filtershow.controller.Control;
35 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
36 import com.android.gallery3d.filtershow.imageshow.ImageShow;
37 import com.android.gallery3d.filtershow.imageshow.MasterImage;
38 import com.android.gallery3d.filtershow.pipeline.ImagePreset;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42
43 /**
44  * Base class for Editors Must contain a mImageShow and a top level view
45  */
46 public class Editor implements OnSeekBarChangeListener, SwapButton.SwapButtonListener {
47     protected Context mContext;
48     protected View mView;
49     protected ImageShow mImageShow;
50     protected FrameLayout mFrameLayout;
51     protected SeekBar mSeekBar;
52     Button mEditTitle;
53     protected Button mFilterTitle;
54     protected int mID;
55     private final String LOGTAG = "Editor";
56     protected boolean mChangesGeometry = false;
57     protected FilterRepresentation mLocalRepresentation = null;
58     protected byte mShowParameter = SHOW_VALUE_UNDEFINED;
59     private Button mButton;
60     public static byte SHOW_VALUE_UNDEFINED = -1;
61     public static byte SHOW_VALUE_OFF = 0;
62     public static byte SHOW_VALUE_INT = 1;
63
64     public static void hackFixStrings(Menu menu) {
65         int count = menu.size();
66         for (int i = 0; i < count; i++) {
67             MenuItem item = menu.getItem(i);
68             item.setTitle(item.getTitle().toString().toUpperCase());
69         }
70     }
71
72     public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
73         return effectName.toUpperCase() + " " + parameterValue;
74     }
75
76     protected Editor(int id) {
77         mID = id;
78     }
79
80     public int getID() {
81         return mID;
82     }
83
84     public byte showParameterValue() {
85         return mShowParameter;
86     }
87
88     public boolean showsSeekBar() {
89         return true;
90     }
91
92     public void setUpEditorUI(View actionButton, View editControl,
93                               Button editTitle, Button stateButton) {
94         mEditTitle = editTitle;
95         mFilterTitle = stateButton;
96         mButton = editTitle;
97         MasterImage.getImage().resetGeometryImages();
98         setMenuIcon(true);
99         setUtilityPanelUI(actionButton, editControl);
100     }
101
102     public boolean showsPopupIndicator() {
103         return true;
104     }
105
106     /**
107      * @param actionButton the would be the area for menu etc
108      * @param editControl this is the black area for sliders etc
109      */
110     public void setUtilityPanelUI(View actionButton, View editControl) {
111
112         AttributeSet aset;
113         Context context = editControl.getContext();
114         LayoutInflater inflater =
115                 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
116         LinearLayout lp = (LinearLayout) inflater.inflate(
117                 R.layout.filtershow_seekbar, (ViewGroup) editControl, true);
118         mSeekBar = (SeekBar) lp.findViewById(R.id.primarySeekBar);
119         mSeekBar.setOnSeekBarChangeListener(this);
120         mSeekBar.setVisibility(View.GONE);
121
122         if (mButton != null) {
123             if (showsPopupIndicator()) {
124                 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0,
125                         R.drawable.filtershow_menu_marker, 0);
126             } else {
127                 mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
128             }
129         }
130     }
131
132     @Override
133     public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) {
134
135     }
136
137     public void setPanel() {
138
139     }
140
141     public void createEditor(Context context, FrameLayout frameLayout) {
142         mContext = context;
143         mFrameLayout = frameLayout;
144         mLocalRepresentation = null;
145     }
146
147     protected void unpack(int viewid, int layoutid) {
148
149         if (mView == null) {
150             mView = mFrameLayout.findViewById(viewid);
151             if (mView == null) {
152                 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService
153                         (Context.LAYOUT_INFLATER_SERVICE);
154                 mView = inflater.inflate(layoutid, mFrameLayout, false);
155                 mFrameLayout.addView(mView, mView.getLayoutParams());
156             }
157         }
158         mImageShow = findImageShow(mView);
159     }
160
161     private ImageShow findImageShow(View view) {
162         if (view instanceof ImageShow) {
163             return (ImageShow) view;
164         }
165         if (!(view instanceof ViewGroup)) {
166             return null;
167         }
168         ViewGroup vg = (ViewGroup) view;
169         int n = vg.getChildCount();
170         for (int i = 0; i < n; i++) {
171             View v = vg.getChildAt(i);
172             if (v instanceof ImageShow) {
173                 return (ImageShow) v;
174             } else if (v instanceof ViewGroup) {
175                 return findImageShow(v);
176             }
177         }
178         return null;
179     }
180
181     public View getTopLevelView() {
182         return mView;
183     }
184
185     public ImageShow getImageShow() {
186         return mImageShow;
187     }
188
189     public void setVisibility(int visible) {
190         mView.setVisibility(visible);
191     }
192
193     public FilterRepresentation getLocalRepresentation() {
194         if (mLocalRepresentation == null) {
195             ImagePreset preset = MasterImage.getImage().getPreset();
196             FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation();
197             mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation);
198             if (mShowParameter == SHOW_VALUE_UNDEFINED && filterRepresentation != null) {
199                 boolean show = filterRepresentation.showParameterValue();
200                 mShowParameter = show ? SHOW_VALUE_INT : SHOW_VALUE_OFF;
201             }
202
203         }
204         return mLocalRepresentation;
205     }
206
207     /**
208      * Call this to update the preset in MasterImage with the current representation
209      * returned by getLocalRepresentation.  This causes the preview bitmap to be
210      * regenerated.
211      */
212     public void commitLocalRepresentation() {
213         commitLocalRepresentation(getLocalRepresentation());
214     }
215
216     /**
217      * Call this to update the preset in MasterImage with a given representation.
218      * This causes the preview bitmap to be regenerated.
219      */
220     public void commitLocalRepresentation(FilterRepresentation rep) {
221         ArrayList<FilterRepresentation> filter = new ArrayList<FilterRepresentation>(1);
222         filter.add(rep);
223         commitLocalRepresentation(filter);
224     }
225
226     /**
227      * Call this to update the preset in MasterImage with a collection of FilterRepresentations.
228      * This causes the preview bitmap to be regenerated.
229      */
230     public void commitLocalRepresentation(Collection<FilterRepresentation> reps) {
231         ImagePreset preset = MasterImage.getImage().getPreset();
232         preset.updateFilterRepresentations(reps);
233         if (mButton != null) {
234             updateText();
235         }
236         if (mChangesGeometry) {
237             // Regenerate both the filtered and the geometry-only bitmaps
238             MasterImage.getImage().resetGeometryImages();
239         }
240         // Regenerate the filtered bitmap.
241         MasterImage.getImage().invalidateFiltersOnly();
242         preset.fillImageStateAdapter(MasterImage.getImage().getState());
243     }
244
245     /**
246      * This is called in response to a click to apply and leave the editor.
247      */
248     public void finalApplyCalled() {
249         commitLocalRepresentation();
250     }
251
252     protected void updateText() {
253         String s = "";
254         if (mLocalRepresentation != null) {
255             s = mContext.getString(mLocalRepresentation.getTextId());
256         }
257         mButton.setText(calculateUserMessage(mContext, s, ""));
258     }
259
260     /**
261      * called after the filter is set and the select is called
262      */
263     public void reflectCurrentFilter() {
264         mLocalRepresentation = null;
265         FilterRepresentation representation = getLocalRepresentation();
266         if (representation != null && mFilterTitle != null && representation.getTextId() != 0) {
267             String text = mContext.getString(representation.getTextId()).toUpperCase();
268             mFilterTitle.setText(text);
269             updateText();
270         }
271     }
272
273     public boolean useUtilityPanel() {
274         return true;
275     }
276
277     public void openUtilityPanel(LinearLayout mAccessoryViewList) {
278         setMenuIcon(false);
279         if (mImageShow != null) {
280             mImageShow.openUtilityPanel(mAccessoryViewList);
281         }
282     }
283
284     protected void setMenuIcon(boolean on) {
285         mEditTitle.setCompoundDrawablesRelativeWithIntrinsicBounds(
286                 0, 0, on ? R.drawable.filtershow_menu_marker : 0, 0);
287     }
288
289     protected void createMenu(int[] strId, View button) {
290         PopupMenu pmenu = new PopupMenu(mContext, button);
291         Menu menu = pmenu.getMenu();
292         for (int i = 0; i < strId.length; i++) {
293             menu.add(Menu.NONE, Menu.FIRST + i, 0, mContext.getString(strId[i]));
294         }
295         setMenuIcon(true);
296
297     }
298
299     public Control[] getControls() {
300         return null;
301     }
302
303     @Override
304     public void onStartTrackingTouch(SeekBar arg0) {
305
306     }
307
308     @Override
309     public void onStopTrackingTouch(SeekBar arg0) {
310
311     }
312
313     @Override
314     public void swapLeft(MenuItem item) {
315
316     }
317
318     @Override
319     public void swapRight(MenuItem item) {
320
321     }
322
323     public void detach() {
324     }
325 }