summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/editors/EditorDrawTabletUI.java
blob: 83d89c9a515005f92cc8bd204f37209941b4aba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.editors;

import android.app.ActionBar;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.gallery3d.R;
import com.android.gallery3d.filtershow.colorpicker.ColorCompareView;
import com.android.gallery3d.filtershow.colorpicker.ColorHueView;
import com.android.gallery3d.filtershow.colorpicker.ColorListener;
import com.android.gallery3d.filtershow.colorpicker.ColorOpacityView;
import com.android.gallery3d.filtershow.colorpicker.ColorSVRectView;
import com.android.gallery3d.filtershow.controller.BasicParameterInt;
import com.android.gallery3d.filtershow.controller.BasicParameterStyle;
import com.android.gallery3d.filtershow.controller.ParameterColor;
import com.android.gallery3d.filtershow.filters.FilterDrawRepresentation;

import java.util.Arrays;

public class EditorDrawTabletUI {
    private EditorDraw mEditorDraw;
    private int[] mBrushIcons;
    private static int sIconDim = 120;
    private int mSelectedColorButton;
    private int mSelectedStyleButton;
    private FilterDrawRepresentation mRep;
    private Button[] mColorButton;
    private ImageButton[] mStyleButton;
    private ColorHueView mHueView;
    private ColorSVRectView mSatValView;
    private ColorOpacityView mOpacityView;
    private ColorCompareView mColorCompareView;
    private TextView mDrawSizeValue;

    private int[] mBasColors;
    private int mSelected;
    private int mTransparent;
    private SeekBar mdrawSizeSeekBar;
    private int[] ids = {
            R.id.draw_color_button01,
            R.id.draw_color_button02,
            R.id.draw_color_button03,
            R.id.draw_color_button04,
            R.id.draw_color_button05,
    };

    public void setDrawRepresentation(FilterDrawRepresentation rep) {
        mRep = rep;
        BasicParameterInt size;
        size = (BasicParameterInt) mRep.getParam(FilterDrawRepresentation.PARAM_SIZE);
        mdrawSizeSeekBar.setMax(size.getMaximum() - size.getMinimum());
        mdrawSizeSeekBar.setProgress(size.getValue());

        ParameterColor color;
        color = (ParameterColor) mRep.getParam(FilterDrawRepresentation.PARAM_COLOR);
        color.setValue(mBasColors[mSelectedColorButton]);
        BasicParameterStyle style;
        style = (BasicParameterStyle) mRep.getParam(FilterDrawRepresentation.PARAM_STYLE);
        style.setSelected(mSelectedStyleButton);
    }

    public EditorDrawTabletUI(EditorDraw editorDraw, Context context, LinearLayout lp) {
        mEditorDraw = editorDraw;
        mBasColors = editorDraw.mBasColors;
        mBrushIcons = editorDraw.brushIcons;
        Resources res = context.getResources();
        sIconDim = res.getDimensionPixelSize(R.dimen.draw_style_icon_dim);
        LinearLayout buttonContainer = (LinearLayout) lp.findViewById(R.id.listStyles);

        mdrawSizeSeekBar = (SeekBar) lp.findViewById(R.id.drawSizeSeekBar);
        mDrawSizeValue = (TextView) lp.findViewById(R.id.drawSizeValue);

        Button clearButton = (Button) lp.findViewById(R.id.clearButton);
        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEditorDraw.clearDrawing();
            }
        });

        mdrawSizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                BasicParameterInt size;
                size = (BasicParameterInt) mRep.getParam(FilterDrawRepresentation.PARAM_SIZE);
                size.setValue(progress + size.getMinimum());
                mEditorDraw.commitLocalRepresentation();
                int val  = progress + size.getMinimum();
                mDrawSizeValue.setText(((val>0)?"+":"")+val);
            }
        });

        ActionBar.LayoutParams params = new ActionBar.LayoutParams(sIconDim, sIconDim);
        mStyleButton = new ImageButton[mBrushIcons.length];
        for (int i = 0; i < mBrushIcons.length; i++) {
            final ImageButton button = new ImageButton(context);
            mStyleButton[i] = button;
            button.setScaleType(ImageView.ScaleType.CENTER_CROP);
            button.setLayoutParams(params);
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), mBrushIcons[i]);
            button.setImageBitmap(bitmap);
            button.setBackgroundResource(android.R.color.transparent);
            buttonContainer.addView(button);
            final int current = i;
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mSelectedStyleButton = current;
                    if (mRep == null) {
                        return;
                    }
                    BasicParameterStyle style = (BasicParameterStyle)
                            mRep.getParam(FilterDrawRepresentation.PARAM_STYLE);
                    style.setSelected(current);
                    resetStyle();
                    mEditorDraw.commitLocalRepresentation();
                }
            });
        }

        final LinearLayout ctls = (LinearLayout) lp.findViewById(R.id.controls);
        final LinearLayout pick = (LinearLayout) lp.findViewById(R.id.colorPicker);
        Button b = (Button) lp.findViewById(R.id.draw_color_popupbutton);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean b = ctls.getVisibility() == View.VISIBLE;
                ctls.setVisibility((b) ? View.GONE : View.VISIBLE);
                pick.setVisibility((!b) ? View.GONE : View.VISIBLE);
            }
        }
        );

        mTransparent = res.getColor(R.color.color_chooser_unslected_border);
        mSelected = res.getColor(R.color.color_chooser_slected_border);

        mColorButton = new Button[ids.length];
        for (int i = 0; i < ids.length; i++) {
            mColorButton[i] = (Button) lp.findViewById(ids[i]);

            float[] hsvo = new float[4];
            Color.colorToHSV(mBasColors[i], hsvo);
            hsvo[3] = (0xFF & (mBasColors[i] >> 24)) / (float) 255;
            mColorButton[i].setTag(hsvo);
            GradientDrawable sd = ((GradientDrawable) mColorButton[i].getBackground());

            sd.setColor(mBasColors[i]);
            sd.setStroke(3, (0 == i) ? mSelected : mTransparent);

            final int buttonNo = i;
            mColorButton[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {

                    mSelectedColorButton = buttonNo;
                    float[] hsvo = Arrays.copyOf((float[]) mColorButton[buttonNo].getTag(), 4);
                    resetBorders();
                    if (mRep == null) {
                        return;
                    }
                    ParameterColor pram;
                    pram = (ParameterColor) mRep.getParam(FilterDrawRepresentation.PARAM_COLOR);
                    pram.setValue(mBasColors[mSelectedColorButton]);
                    mEditorDraw.commitLocalRepresentation();
                    mHueView.setColor(hsvo);
                    mSatValView.setColor(hsvo);
                    mOpacityView.setColor(hsvo);
                    mColorCompareView.setColor(hsvo);
                    mColorCompareView.setOrigColor(hsvo);
                }
            });
        }

        mHueView = (ColorHueView) lp.findViewById(R.id.ColorHueView);
        mSatValView = (ColorSVRectView) lp.findViewById(R.id.colorRectView);
        mOpacityView = (ColorOpacityView) lp.findViewById(R.id.colorOpacityView);
        mColorCompareView = (ColorCompareView) lp.findViewById(R.id.btnSelect);

        float[] hsvo = new float[4];
        Color.colorToHSV(mBasColors[0], hsvo);
        hsvo[3] = (0xFF & (mBasColors[0] >> 24)) / (float) 255;

        mColorCompareView.setOrigColor(hsvo);
        ColorListener[] colorViews = {mHueView, mSatValView, mOpacityView, mColorCompareView};
        for (int i = 0; i < colorViews.length; i++) {
            colorViews[i].setColor(hsvo);

            for (int j = 0; j < colorViews.length; j++) {
                if (i == j) {
                    continue;
                }
                colorViews[i].addColorListener(colorViews[j]);
            }
        }
        ColorListener colorListener = new ColorListener() {

            @Override
            public void setColor(float[] hsvo) {
                int color = Color.HSVToColor((int) (hsvo[3] * 255), hsvo);
                Button b = mColorButton[mSelectedColorButton];
                float[] f = (float[]) b.getTag();
                System.arraycopy(hsvo, 0, f, 0, 4);
                mBasColors[mSelectedColorButton] = color;
                GradientDrawable sd = ((GradientDrawable) b.getBackground());
                sd.setColor(color);
                resetBorders();
                ParameterColor pram;
                pram = (ParameterColor) mRep.getParam(FilterDrawRepresentation.PARAM_COLOR);
                pram.setValue(color);
                mEditorDraw.commitLocalRepresentation();
            }

            @Override
            public void addColorListener(ColorListener l) {
            }
        };

        for (int i = 0; i < colorViews.length; i++) {
            colorViews[i].addColorListener(colorListener);
        }

    }

    public void resetStyle() {
        for (int i = 0; i < mStyleButton.length; i++) {
            int rid = (i == mSelectedStyleButton) ? android.R.color.holo_blue_light :
                    android.R.color.transparent;
            mStyleButton[i].setBackgroundResource(rid);

        }
    }

    private void resetBorders() {
        for (int i = 0; i < ids.length; i++) {
            final Button button = mColorButton[i];

            GradientDrawable sd = ((GradientDrawable) button.getBackground());
            sd.setColor(mBasColors[i]);
            sd.setStroke(3, (mSelectedColorButton == i) ? mSelected : mTransparent);
        }
    }
}