summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/imageshow/GradControl.java
blob: 964da99e9d6ff356b667403bfe88046e97ea860e (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
/*
 * Copyright (C) 2012 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.imageshow;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;

import com.android.gallery3d.R;

public class GradControl {
    private float mPoint1X = Float.NaN; // used to flag parameters have not been set
    private float mPoint1Y = 0;
    private float mPoint2X = 200;
    private float mPoint2Y = 300;
    private int mMinTouchDist = 80;// should be a resource & in dips

    private float[] handlex = new float[3];
    private float[] handley = new float[3];
    private int mSliderColor;
    private int mCenterDotSize;
    private float mDownX;
    private float mDownY;
    private float mDownPoint1X;
    private float mDownPoint1Y;
    private float mDownPoint2X;
    private float mDownPoint2Y;
    Rect mImageBounds;
    int mImageHeight;
    private Matrix mScrToImg;
    Paint mPaint = new Paint();
    DashPathEffect mDash = new DashPathEffect(new float[]{30, 30}, 0);
    private boolean mShowReshapeHandles = true;
    public final static int HAN_CENTER = 0;
    public final static int HAN_NORTH = 2;
    public final static int HAN_SOUTH = 1;
    private int[] mPointColorPatern;
    private int[] mGrayPointColorPatern;
    private float[] mPointRadialPos = new float[]{0, .3f, .31f, 1};
    private int mLineColor;
    private int mlineShadowColor;

    public GradControl(Context context) {

        Resources res = context.getResources();
        mCenterDotSize = (int) res.getDimension(R.dimen.gradcontrol_dot_size);
        mMinTouchDist = (int) res.getDimension(R.dimen.gradcontrol_min_touch_dist);
        int grayPointCenterColor = res.getColor(R.color.gradcontrol_graypoint_center);
        int grayPointEdgeColor = res.getColor(R.color.gradcontrol_graypoint_edge);
        int pointCenterColor = res.getColor(R.color.gradcontrol_point_center);
        int pointEdgeColor = res.getColor(R.color.gradcontrol_point_edge);
        int pointShadowStartColor = res.getColor(R.color.gradcontrol_point_shadow_start);
        int pointShadowEndColor = res.getColor(R.color.gradcontrol_point_shadow_end);
        mPointColorPatern = new int[]{
                pointCenterColor, pointEdgeColor, pointShadowStartColor, pointShadowEndColor};
        mGrayPointColorPatern = new int[]{
                grayPointCenterColor, grayPointEdgeColor, pointShadowStartColor, pointShadowEndColor};
        mSliderColor = Color.WHITE;
        mLineColor = res.getColor(R.color.gradcontrol_line_color);
        mlineShadowColor = res.getColor(R.color.gradcontrol_line_shadow);
    }

    public void setPoint2(float x, float y) {
        mPoint2X = x;
        mPoint2Y = y;
    }

    public void setPoint1(float x, float y) {
        mPoint1X = x;
        mPoint1Y = y;
    }

    public int getCloseHandle(float x, float y) {
        float min = Float.MAX_VALUE;
        int handle = -1;
        for (int i = 0; i < handlex.length; i++) {
            float dx = handlex[i] - x;
            float dy = handley[i] - y;
            float dist = dx * dx + dy * dy;
            if (dist < min) {
                min = dist;
                handle = i;
            }
        }

        if (min < mMinTouchDist * mMinTouchDist) {
            return handle;
        }
        for (int i = 0; i < handlex.length; i++) {
            float dx = handlex[i] - x;
            float dy = handley[i] - y;
            float dist = (float) Math.sqrt(dx * dx + dy * dy);
        }

        return -1;
    }

    public void setScrImageInfo(Matrix scrToImg, Rect imageBounds) {
        mScrToImg = scrToImg;
        mImageBounds = new Rect(imageBounds);
    }

    private boolean centerIsOutside(float x1, float y1, float x2, float y2) {
        return (!mImageBounds.contains((int) ((x1 + x2) / 2), (int) ((y1 + y2) / 2)));
    }

    public void actionDown(float x, float y, Line line) {
        float[] point = new float[]{
                x, y};
        mScrToImg.mapPoints(point);
        mDownX = point[0];
        mDownY = point[1];
        mDownPoint1X = line.getPoint1X();
        mDownPoint1Y = line.getPoint1Y();
        mDownPoint2X = line.getPoint2X();
        mDownPoint2Y = line.getPoint2Y();
    }

    public void actionMove(int handle, float x, float y, Line line) {
        float[] point = new float[]{
                x, y};
        mScrToImg.mapPoints(point);
        x = point[0];
        y = point[1];

        // Test if the matrix is swapping x and y
        point[0] = 0;
        point[1] = 1;
        mScrToImg.mapVectors(point);
        boolean swapxy = (point[0] > 0.0f);

        int sign = 1;

        float dx = x - mDownX;
        float dy = y - mDownY;
        switch (handle) {
            case HAN_CENTER:
                if (centerIsOutside(mDownPoint1X + dx, mDownPoint1Y + dy,
                        mDownPoint2X + dx, mDownPoint2Y + dy)) {
                    break;
                }
                line.setPoint1(mDownPoint1X + dx, mDownPoint1Y + dy);
                line.setPoint2(mDownPoint2X + dx, mDownPoint2Y + dy);
                break;
            case HAN_SOUTH:
                if (centerIsOutside(mDownPoint1X + dx, mDownPoint1Y + dy,
                        mDownPoint2X, mDownPoint2Y)) {
                    break;
                }
                line.setPoint1(mDownPoint1X + dx, mDownPoint1Y + dy);
                break;
            case HAN_NORTH:
                if (centerIsOutside(mDownPoint1X, mDownPoint1Y,
                        mDownPoint2X + dx, mDownPoint2Y + dy)) {
                    break;
                }
                line.setPoint2(mDownPoint2X + dx, mDownPoint2Y + dy);
                break;
        }
    }

    public void paintGrayPoint(Canvas canvas, float x, float y) {
        if (isUndefined()) {
            return;
        }

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        RadialGradient g = new RadialGradient(x, y, mCenterDotSize, mGrayPointColorPatern,
                mPointRadialPos, Shader.TileMode.CLAMP);
        paint.setShader(g);
        canvas.drawCircle(x, y, mCenterDotSize, paint);
    }

    public void paintPoint(Canvas canvas, float x, float y) {
        if (isUndefined()) {
            return;
        }

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        RadialGradient g = new RadialGradient(x, y, mCenterDotSize, mPointColorPatern,
                mPointRadialPos, Shader.TileMode.CLAMP);
        paint.setShader(g);
        canvas.drawCircle(x, y, mCenterDotSize, paint);
    }

    void paintLines(Canvas canvas, float p1x, float p1y, float p2x, float p2y) {
        if (isUndefined()) {
            return;
        }

        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setStrokeWidth(6);
        mPaint.setColor(mlineShadowColor);
        mPaint.setPathEffect(mDash);
        paintOvallines(canvas, mPaint, p1x, p1y, p2x, p2y);

        mPaint.setStrokeWidth(3);
        mPaint.setColor(mLineColor);
        mPaint.setPathEffect(mDash);
        paintOvallines(canvas, mPaint, p1x, p1y, p2x, p2y);
    }

    public void paintOvallines(
            Canvas canvas, Paint paint, float p1x, float p1y, float p2x, float p2y) {



        canvas.drawLine(p1x, p1y, p2x, p2y, paint);

        float cx = (p1x + p2x) / 2;
        float cy = (p1y + p2y) / 2;
        float dx = p1x - p2x;
        float dy = p1y - p2y;
        float len = (float) Math.sqrt(dx * dx + dy * dy);
        dx *= 2048 / len;
        dy *= 2048 / len;

        canvas.drawLine(p1x + dy, p1y - dx, p1x - dy, p1y + dx, paint);
        canvas.drawLine(p2x + dy, p2y - dx, p2x - dy, p2y + dx, paint);
    }

    public void fillHandles(Canvas canvas, float p1x, float p1y, float p2x, float p2y) {
        float cx = (p1x + p2x) / 2;
        float cy = (p1y + p2y) / 2;
        handlex[0] = cx;
        handley[0] = cy;
        handlex[1] = p1x;
        handley[1] = p1y;
        handlex[2] = p2x;
        handley[2] = p2y;

    }

    public void draw(Canvas canvas) {
        paintLines(canvas, mPoint1X, mPoint1Y, mPoint2X, mPoint2Y);
        fillHandles(canvas, mPoint1X, mPoint1Y, mPoint2X, mPoint2Y);
        paintPoint(canvas, mPoint2X, mPoint2Y);
        paintPoint(canvas, mPoint1X, mPoint1Y);
        paintPoint(canvas, (mPoint1X + mPoint2X) / 2, (mPoint1Y + mPoint2Y) / 2);
    }

    public boolean isUndefined() {
        return Float.isNaN(mPoint1X);
    }

    public void setShowReshapeHandles(boolean showReshapeHandles) {
        this.mShowReshapeHandles = showReshapeHandles;
    }
}