summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/ZoomController.java
blob: 063237fad0240ec001ad95247d91d0bc97e6f937 (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
279
280
281
282
283
284
285
286
/*
 * Copyright (C) 2010 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.camera.ui;

import static com.android.camera.ui.GLRootView.dpToPixel;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.android.camera.R;
import com.android.camera.Util;

import java.text.DecimalFormat;
import java.util.Arrays;

import javax.microedition.khronos.opengles.GL11;

public class ZoomController extends GLView {
    private static final int LABEL_COLOR = Color.WHITE;

    private static final DecimalFormat sZoomFormat = new DecimalFormat("#.#x");
    private static final int INVALID_POSITION = Integer.MAX_VALUE;

    private static final float LABEL_FONT_SIZE = 18;
    private static final int HORIZONTAL_PADDING = 3;
    private static final int VERTICAL_PADDING = 3;
    private static final int MINIMAL_HEIGHT = 150;
    private static final float TOLERANCE_RADIUS = 30;

    private static float sLabelSize;
    private static int sHorizontalPadding;
    private static int sVerticalPadding;
    private static int sMinimalHeight;
    private static float sToleranceRadius;

    private static NinePatchTexture sBackground;
    private static Texture sSlider;
    private static Texture sTickMark;
    private static Texture sFineTickMark;

    private StringTexture mTickLabels[];
    private float mRatios[];
    private int mIndex;

    private int mFineTickStep;
    private int mLabelStep;

    private int mMaxLabelWidth;
    private int mMaxLabelHeight;

    private int mSliderTop;
    private int mSliderBottom;
    private int mSliderLeft;
    private int mSliderPosition = INVALID_POSITION;
    private float mValueGap;
    private ZoomListener mZoomListener;

    public interface ZoomListener {
        public void onZoomChanged(int index, float ratio, boolean isMoving);
    }

    public ZoomController(Context context) {
        initializeStaticVariable(context);
    }

    private void onSliderMoved(int position, boolean isMoving) {
        position = Util.clamp(position,
                mSliderTop, mSliderBottom - sSlider.getHeight());
        mSliderPosition = position;
        invalidate();

        int index = mRatios.length - 1 - (int)
                ((position - mSliderTop) /  mValueGap + .5f);
        if (index != mIndex || !isMoving) {
            mIndex = index;
            if (mZoomListener != null) {
                mZoomListener.onZoomChanged(mIndex, mRatios[mIndex], isMoving);
            }
        }
    }

    private static void initializeStaticVariable(Context context) {
        if (sBackground != null) return;

        sLabelSize = dpToPixel(context, LABEL_FONT_SIZE);
        sHorizontalPadding = dpToPixel(context, HORIZONTAL_PADDING);
        sVerticalPadding = dpToPixel(context, VERTICAL_PADDING);
        sMinimalHeight = dpToPixel(context, MINIMAL_HEIGHT);
        sToleranceRadius = dpToPixel(context, TOLERANCE_RADIUS);

        sBackground = new NinePatchTexture(context, R.drawable.zoom_background);
        sSlider = new ResourceTexture(context, R.drawable.zoom_slider);
        sTickMark = new ResourceTexture(context, R.drawable.zoom_tickmark);
        sFineTickMark = new ResourceTexture(
                context, R.drawable.zoom_finetickmark);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (!changed) return;
        Rect p = mPaddings;
        int height = b - t - p.top - p.bottom;
        int margin = Math.max(sSlider.getHeight(), mMaxLabelHeight);
        mValueGap = (float) (height - margin) / (mRatios.length - 1);

        mSliderLeft = p.left + mMaxLabelWidth + sHorizontalPadding
                + sTickMark.getWidth() + sHorizontalPadding;

        mSliderTop = p.top + margin / 2 - sSlider.getHeight() / 2;
        mSliderBottom = mSliderTop + height - margin + sSlider.getHeight();
    }

    private boolean withInToleranceRange(float x, float y) {
        float sx = mSliderLeft + sSlider.getWidth() / 2;
        float sy = mSliderTop + (mRatios.length - 1 - mIndex) * mValueGap
                + sSlider.getHeight() / 2;
        float dist = Util.distance(x, y, sx, sy);
        return dist <= sToleranceRadius;
    }

    @Override
    protected boolean onTouch(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (withInToleranceRange(x, y)) {
                    onSliderMoved((int) (y - sSlider.getHeight()), true);
                }
                return true;
            case MotionEvent.ACTION_MOVE:
                if (mSliderPosition != INVALID_POSITION) {
                    onSliderMoved((int) (y - sSlider.getHeight()), true);
                }
                return true;
            case MotionEvent.ACTION_UP:
                if (mSliderPosition != INVALID_POSITION) {
                    onSliderMoved((int) (y - sSlider.getHeight()), false);
                    mSliderPosition = INVALID_POSITION;
                }
                return true;
        }
        return true;
    }

    public void setAvailableZoomRatios(float ratios[]) {
        if (Arrays.equals(ratios, mRatios)) return;
        mRatios = ratios;
        mLabelStep = getLabelStep(ratios.length);
        mTickLabels = new StringTexture[
                (ratios.length + mLabelStep - 1) / mLabelStep];
        for (int i = 0, n = mTickLabels.length; i < n; ++i) {
            mTickLabels[i] = StringTexture.newInstance(
                    sZoomFormat.format(ratios[i * mLabelStep]),
                    sLabelSize, LABEL_COLOR);
        }
        mFineTickStep = mLabelStep % 3 == 0
                ? mLabelStep / 3
                : mLabelStep %2 == 0 ? mLabelStep / 2 : 0;

        int maxHeight = 0;
        int maxWidth = 0;
        int labelCount = mTickLabels.length;
        for (int i = 0; i < labelCount; ++i) {
            maxWidth = Math.max(maxWidth, mTickLabels[i].getWidth());
            maxHeight = Math.max(maxHeight, mTickLabels[i].getHeight());
        }

        mMaxLabelHeight = maxHeight;
        mMaxLabelWidth = maxWidth;
        invalidate();
    }

    private int getLabelStep(final int valueCount) {
        if (valueCount < 5) return 1;
        for (int step = valueCount / 5;; ++step) {
            if (valueCount / step <= 5) return step;
        }
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        int labelCount = mTickLabels.length;
        int ratioCount = mRatios.length;

        int height = (mMaxLabelHeight + sVerticalPadding)
                * (labelCount - 1) * ratioCount / (mLabelStep * labelCount)
                + Math.max(sSlider.getHeight(), mMaxLabelHeight);

        int width = mMaxLabelWidth + sHorizontalPadding + sTickMark.getWidth()
                + sHorizontalPadding + sBackground.getIntrinsicWidth();
        height = Math.max(sMinimalHeight, height);

        new MeasureHelper(this)
                .setPreferredContentSize(width, height)
                .measure(widthSpec, heightSpec);
    }

    @Override
    protected void render(GLRootView root, GL11 gl) {
        renderTicks(root, gl);
        renderSlider(root, gl);
    }

    private void renderTicks(GLRootView root, GL11 gl) {
        float gap = mValueGap;
        int labelStep = mLabelStep;

        // render the tick labels
        int xoffset = mPaddings.left + mMaxLabelWidth;
        float yoffset = mSliderBottom - sSlider.getHeight() / 2;
        for (int i = 0, n = mTickLabels.length; i < n; ++i) {
            Texture t = mTickLabels[i];
            t.draw(root, xoffset - t.getWidth(),
                    (int) (yoffset - t.getHeight() / 2));
            yoffset -= labelStep * gap;
        }

        // render the main tick marks
        Texture tickMark = sTickMark;
        xoffset += sHorizontalPadding;
        yoffset = mSliderBottom - sSlider.getHeight() / 2;
        int halfHeight = tickMark.getHeight() / 2;
        for (int i = 0, n = mTickLabels.length; i < n; ++i) {
            tickMark.draw(root, xoffset, (int) (yoffset - halfHeight));
            yoffset -= labelStep * gap;
        }

        if (mFineTickStep > 0) {
            // render the fine tick marks
            tickMark = sFineTickMark;
            xoffset += sTickMark.getWidth() - tickMark.getWidth();
            yoffset = mSliderBottom - sSlider.getHeight() / 2;
            halfHeight = tickMark.getHeight() / 2;
            for (int i = 0, n = mRatios.length; i < n; ++i) {
                if (i % mLabelStep != 0) {
                    tickMark.draw(root, xoffset, (int) (yoffset - halfHeight));
                }
                yoffset -= gap;
            }
        }
    }

    private void renderSlider(GLRootView root, GL11 gl) {
        int left = mSliderLeft;
        int bottom = mSliderBottom;
        int top = mSliderTop;
        sBackground.setSize(sBackground.getIntrinsicWidth(), bottom - top);
        sBackground.draw(root, left, top);

        if (mSliderPosition == INVALID_POSITION) {
            sSlider.draw(root, left, (int)
                    (top + mValueGap * (mRatios.length - 1 - mIndex)));
        } else {
            sSlider.draw(root, left, mSliderPosition);
        }
    }

    public void setZoomListener(ZoomListener listener) {
        mZoomListener = listener;
    }

    public void setZoomIndex(int index) {
        index = Util.clamp(index, 0, mRatios.length - 1);
        if (mIndex == index) return;
        mIndex = index;
        if (mZoomListener != null) {
            mZoomListener.onZoomChanged(mIndex, mRatios[mIndex], false);
        }
    }
}