1/*
2 * Copyright (C) 2015 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 rs.example.android.com.healingbrush;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.Matrix;
24import android.graphics.Paint;
25import android.graphics.Path;
26import android.graphics.RectF;
27import android.graphics.drawable.Drawable;
28import android.util.AttributeSet;
29import android.view.View;
30import android.widget.ImageView;
31
32import java.util.ArrayList;
33import java.util.Arrays;
34
35public class DrawView extends View {
36    private static final String TAG = "DrawView";
37    private ImageView mImageView;
38    Path mPoints_backup = new Path();
39    float[] path = new float[200];
40    Path mPoints = new Path();
41    int len;
42    Paint mPaint1;
43    Paint mPaint2;
44    private boolean mDone;
45    ArrayList<Drawable> drawList = new ArrayList<Drawable>();
46
47    private void setup(Context context) {
48        mPaint1 = new Paint();
49        mPaint2 = new Paint();
50        mPaint1.setStyle(Paint.Style.STROKE);
51        mPaint1.setColor(Color.BLACK);
52        mPaint1.setStrokeWidth(2);
53        mPaint2.setStyle(Paint.Style.STROKE);
54        mPaint2.setColor(Color.YELLOW);
55    }
56
57    public DrawView(Context context) {
58        super(context);
59        setup(context);
60    }
61
62    public DrawView(Context context, AttributeSet attrs) {
63        super(context, attrs);
64        setup(context);
65    }
66
67    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
68        super(context, attrs, defStyleAttr);
69        setup(context);
70    }
71
72    /**
73     * Assumes imageView is using matrix mode
74     *
75     * @param imageView
76     */
77    public void setImageView(ImageView imageView) {
78        mImageView = imageView;
79    }
80
81    RectF rec = new RectF();
82
83    @Override
84    protected void onDraw(Canvas canvas) {
85        Matrix m = mImageView.getImageMatrix();
86        Drawable d = mImageView.getDrawable();
87        canvas.concat(m);
88
89        for (Drawable elem : drawList) {
90            elem.draw(canvas);
91        }
92        canvas.drawPath(mPoints, mPaint1);
93        canvas.drawPath(mPoints, mPaint2);
94    }
95
96    public Region getRegion(Bitmap img) {
97        Region ret = new Region(Arrays.copyOf(path, len), img);
98
99        invalidate();
100        return ret;
101    }
102
103    public void downPoint(float[] imgPoint) {
104        path[0] = imgPoint[0];
105        path[1] = imgPoint[1];
106        len = 2;
107        mPoints_backup.reset();
108        mPoints_backup.addPath(mPoints);
109        mPoints.reset();
110        mPoints.moveTo(imgPoint[0], imgPoint[1]);
111    }
112
113    public void undo() {
114        mPoints.reset();
115        mPoints.addPath(mPoints_backup);
116    }
117
118    public void movePoint(float[] imgMoveList, int size) {
119        if (len + size * 2 >= path.length) {
120            path = Arrays.copyOf(path, 2 * (len + size * 2));
121        }
122        for (int i = size * 2 - 2; i >= 0; i -= 2) {
123            mPoints.lineTo(imgMoveList[i], imgMoveList[i + 1]);
124            path[len] = imgMoveList[i];
125            path[len + 1] = imgMoveList[i + 1];
126            len += 2;
127        }
128    }
129
130    public void upPoint(float[] imgPoint) {
131        if (len + 2 >= path.length) {
132            path = Arrays.copyOf(path, 2 * (len + 2));
133        }
134        path[len] = imgPoint[0];
135        path[len + 1] = imgPoint[1];
136        len += 2;
137        mPoints.lineTo(imgPoint[0], imgPoint[1]);
138        mPoints.close();
139        mDone = true;
140    }
141
142    public void addDrawable(Drawable d) {
143        drawList.add(d);
144    }
145
146    public void clearDrawables() {
147        drawList.clear();
148    }
149}
150