ImageVignette.java revision 0c1b4c6422a4d2d9b81cc0946d1c9675440a94e2
1/*
2 * Copyright (C) 2013 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
17package com.android.gallery3d.filtershow.imageshow;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Matrix;
22import android.util.AttributeSet;
23import android.util.Log;
24import android.view.MotionEvent;
25
26import com.android.gallery3d.filtershow.editors.EditorVignette;
27import com.android.gallery3d.filtershow.filters.FilterVignetteRepresentation;
28
29public class ImageVignette extends ImageShow {
30    private static final String LOGTAG = "ImageVignette";
31
32    private FilterVignetteRepresentation mVignetteRep;
33    private EditorVignette mEditorVignette;
34
35    private int mActiveHandle = -1;
36
37    EclipseControl mElipse;
38
39    public ImageVignette(Context context) {
40        super(context);
41        mElipse = new EclipseControl(context);
42    }
43
44    public ImageVignette(Context context, AttributeSet attrs) {
45        super(context, attrs);
46        mElipse = new EclipseControl(context);
47    }
48
49    @Override
50    public boolean onTouchEvent(MotionEvent event) {
51        int mask = event.getActionMasked();
52        if (mActiveHandle == -1) {
53            if (MotionEvent.ACTION_DOWN != mask) {
54                return super.onTouchEvent(event);
55            }
56            if (event.getPointerCount() == 1) {
57                mActiveHandle = mElipse.getCloseHandle(event.getX(), event.getY());
58            }
59            if (mActiveHandle == -1) {
60                return super.onTouchEvent(event);
61            }
62        } else {
63            switch (mask) {
64                case MotionEvent.ACTION_UP:
65                    mActiveHandle = -1;
66                    break;
67                case MotionEvent.ACTION_DOWN:
68                    break;
69            }
70        }
71        float x = event.getX();
72        float y = event.getY();
73
74        mElipse.setScrToImageMatrix(getScreenToImageMatrix(true));
75
76        boolean didComputeEllipses = false;
77        switch (mask) {
78            case (MotionEvent.ACTION_DOWN):
79                mElipse.actionDown(x, y, mVignetteRep);
80                break;
81            case (MotionEvent.ACTION_UP):
82            case (MotionEvent.ACTION_MOVE):
83                mElipse.actionMove(mActiveHandle, x, y, mVignetteRep);
84                setRepresentation(mVignetteRep);
85                didComputeEllipses = true;
86                break;
87        }
88        if (!didComputeEllipses) {
89            computeEllipses();
90        }
91        invalidate();
92        return true;
93    }
94
95    public void setRepresentation(FilterVignetteRepresentation vignetteRep) {
96        mVignetteRep = vignetteRep;
97        computeEllipses();
98    }
99
100    public void computeEllipses() {
101        if (mVignetteRep == null) {
102            return;
103        }
104        Matrix toImg = getScreenToImageMatrix(false);
105        Matrix toScr = new Matrix();
106        toImg.invert(toScr);
107
108        float[] c = new float[] {
109                mVignetteRep.getCenterX(), mVignetteRep.getCenterY() };
110        if (Float.isNaN(c[0])) {
111            float cx = MasterImage.getImage().getOriginalBounds().width() / 2;
112            float cy = MasterImage.getImage().getOriginalBounds().height() / 2;
113            float rx = Math.min(cx, cy) * .8f;
114            float ry = rx;
115            mVignetteRep.setCenter(cx, cy);
116            mVignetteRep.setRadius(rx, ry);
117
118            c[0] = cx;
119            c[1] = cy;
120            toScr.mapPoints(c);
121            if (getWidth() != 0) {
122                mElipse.setCenter(c[0], c[1]);
123                mElipse.setRadius(c[0] * 0.8f, c[1] * 0.8f);
124            }
125        } else {
126
127            toScr.mapPoints(c);
128
129            mElipse.setCenter(c[0], c[1]);
130            mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX()),
131                    toScr.mapRadius(mVignetteRep.getRadiusY()));
132        }
133        mEditorVignette.commitLocalRepresentation();
134    }
135
136    public void setEditor(EditorVignette editorVignette) {
137        mEditorVignette = editorVignette;
138    }
139
140    @Override
141    public void onSizeChanged(int w, int h, int oldw, int oldh) {
142        super.onSizeChanged(w,  h, oldw, oldh);
143        computeEllipses();
144    }
145
146    @Override
147    public void onDraw(Canvas canvas) {
148        super.onDraw(canvas);
149        if (mVignetteRep == null) {
150            return;
151        }
152        Matrix toImg = getScreenToImageMatrix(false);
153        Matrix toScr = new Matrix();
154        toImg.invert(toScr);
155        float[] c = new float[] {
156                mVignetteRep.getCenterX(), mVignetteRep.getCenterY() };
157        toScr.mapPoints(c);
158        mElipse.setCenter(c[0], c[1]);
159        mElipse.setRadius(toScr.mapRadius(mVignetteRep.getRadiusX()),
160                toScr.mapRadius(mVignetteRep.getRadiusY()));
161
162        mElipse.draw(canvas);
163    }
164
165}
166