OSDN Git Service

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