1/*
2 * Copyright (C) 2010 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.photoeditor.actions;
18
19import android.graphics.Color;
20import android.graphics.Matrix;
21import android.graphics.Paint;
22import android.graphics.Path;
23import android.graphics.PointF;
24import android.graphics.RectF;
25import android.os.Parcel;
26import android.os.Parcelable;
27
28import java.util.Vector;
29
30/**
31 * Doodle that consists of a color and doodling path for drawing.
32 */
33public class Doodle implements Parcelable {
34
35    private final int color;
36    private final Path normalizedPath = new Path();
37    private final Vector<PointF> points = new Vector<PointF>();
38
39    /**
40     * Creates paint for doodles.
41     */
42    public static Paint createPaint() {
43        Paint paint = new Paint(Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
44        paint.setStyle(Paint.Style.STROKE);
45        paint.setStrokeJoin(Paint.Join.ROUND);
46        paint.setStrokeCap(Paint.Cap.ROUND);
47        paint.setStrokeWidth(15);
48        return paint;
49    }
50
51    public Doodle(int color, PointF startPoint) {
52        this.color = Color.argb(192, Color.red(color), Color.green(color), Color.blue(color));
53        normalizedPath.moveTo(startPoint.x, startPoint.y);
54        points.add(startPoint);
55    }
56
57    /**
58     * Adds control points whose coordinates range from 0 to 1 to construct the doodle path.
59     *
60     * @return true if the constructed path is in (0, 0, 1, 1) bounds; otherwise, false.
61     */
62    public boolean addControlPoint(PointF point) {
63        PointF last = points.lastElement();
64        normalizedPath.quadTo(last.x, last.y, (last.x + point.x) / 2, (last.y + point.y) / 2);
65        points.add(point);
66
67        RectF r = new RectF();
68        normalizedPath.computeBounds(r, false);
69        return r.intersects(0, 0, 1, 1);
70    }
71
72    public int getColor() {
73        return color;
74    }
75
76    public boolean isEmpty() {
77        return normalizedPath.isEmpty();
78    }
79
80    /**
81     * Gets the drawing path from the normalized doodle path.
82     */
83    public void getDrawingPath(Matrix matrix, Path path) {
84        path.set(normalizedPath);
85        path.transform(matrix);
86    }
87
88    @Override
89    public int describeContents() {
90        return 0;
91    }
92
93    @Override
94    public void writeToParcel(Parcel dest, int flags) {
95        dest.writeInt(color);
96        dest.writeInt(points.size());
97        for (PointF point : points) {
98            dest.writeParcelable(point, 0);
99        }
100    }
101
102    public static final Parcelable.Creator<Doodle> CREATOR = new Parcelable.Creator<Doodle>() {
103
104        @Override
105        public Doodle createFromParcel(Parcel source) {
106            int color = source.readInt();
107            int size = source.readInt();
108            if (size > 0) {
109                Doodle doodle = new Doodle(color, (PointF) source.readParcelable(null));
110                for (int i = 1; i < size; i++) {
111                    doodle.addControlPoint((PointF) source.readParcelable(null));
112                }
113                return doodle;
114            }
115            return new Doodle(color, new PointF(0, 0));
116        }
117
118        @Override
119        public Doodle[] newArray(int size) {
120            return new Doodle[size];
121        }};
122}
123