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.filters;
18
19import android.graphics.Path;
20import android.util.Log;
21
22import com.android.gallery3d.R;
23import com.android.gallery3d.filtershow.editors.EditorDraw;
24
25import java.util.Vector;
26
27public class FilterDrawRepresentation extends FilterRepresentation {
28    private static final String LOGTAG = "FilterDrawRepresentation";
29
30    public static class StrokeData implements Cloneable {
31        public byte mType;
32        public Path mPath;
33        public float mRadius;
34        public int mColor;
35        public int noPoints = 0;
36        @Override
37        public String toString() {
38            return "stroke(" + mType + ", path(" + (mPath) + "), " + mRadius + " , "
39                    + Integer.toHexString(mColor) + ")";
40        }
41        @Override
42        public StrokeData clone() throws CloneNotSupportedException {
43            return (StrokeData) super.clone();
44        }
45    }
46
47    private Vector<StrokeData> mDrawing = new Vector<StrokeData>();
48    private StrokeData mCurrent; // used in the currently drawing style
49
50    public FilterDrawRepresentation() {
51        super("Draw");
52        setFilterClass(ImageFilterDraw.class);
53        setPriority(FilterRepresentation.TYPE_VIGNETTE);
54        setTextId(R.string.imageDraw);
55        setButtonId(R.id.drawOnImageButton);
56        setEditorId(EditorDraw.ID);
57        setOverlayId(R.drawable.filtershow_drawing);
58        setOverlayOnly(true);
59    }
60
61    @Override
62    public String toString() {
63        return getName() + " : strokes=" + mDrawing.size()
64                + ((mCurrent == null) ? " no current "
65                        : ("draw=" + mCurrent.mType + " " + mCurrent.noPoints));
66    }
67
68    public Vector<StrokeData> getDrawing() {
69        return mDrawing;
70    }
71
72    public StrokeData getCurrentDrawing() {
73        return mCurrent;
74    }
75
76    @Override
77    public FilterRepresentation clone() throws CloneNotSupportedException {
78        FilterDrawRepresentation representation = (FilterDrawRepresentation) super.clone();
79        return representation;
80    }
81
82    @Override
83    public boolean isNil() {
84        return getDrawing().isEmpty();
85    }
86
87    @Override
88    public void useParametersFrom(FilterRepresentation a) {
89        if (a instanceof FilterDrawRepresentation) {
90            FilterDrawRepresentation representation = (FilterDrawRepresentation) a;
91            try {
92                if (representation.mCurrent != null) {
93                    mCurrent = (StrokeData) representation.mCurrent.clone();
94                } else {
95                    mCurrent = null;
96                }
97                if (representation.mDrawing != null) {
98                    mDrawing = (Vector<StrokeData>) representation.mDrawing.clone();
99                } else {
100                    mDrawing = null;
101                }
102
103            } catch (CloneNotSupportedException e) {
104                e.printStackTrace();
105            }
106        } else {
107            Log.v(LOGTAG, "cannot use parameters from " + a);
108        }
109    }
110
111    @Override
112    public boolean equals(FilterRepresentation representation) {
113        if (!super.equals(representation)) {
114            return false;
115        }
116        if (representation instanceof FilterDrawRepresentation) {
117            FilterDrawRepresentation fdRep = (FilterDrawRepresentation) representation;
118            if (fdRep.mDrawing.size() != mDrawing.size())
119                return false;
120            if (fdRep.mCurrent == null && mCurrent.mPath == null) {
121                return true;
122            }
123            if (fdRep.mCurrent != null && mCurrent.mPath != null) {
124                if (fdRep.mCurrent.noPoints == mCurrent.noPoints) {
125                    return true;
126                }
127                return false;
128            }
129        }
130        return false;
131    }
132
133    public void startNewSection(byte type, int color, float size, float x, float y) {
134        mCurrent = new StrokeData();
135        mCurrent.mColor = color;
136        mCurrent.mRadius = size;
137        mCurrent.mType = type;
138        mCurrent.mPath = new Path();
139        mCurrent.mPath.moveTo(x, y);
140        mCurrent.noPoints = 0;
141    }
142
143    public void addPoint(float x, float y) {
144        mCurrent.noPoints++;
145        mCurrent.mPath.lineTo(x, y);
146    }
147
148    public void endSection(float x, float y) {
149        mCurrent.mPath.lineTo(x, y);
150        mCurrent.noPoints++;
151        mDrawing.add(mCurrent);
152        mCurrent = null;
153    }
154
155    public void clearCurrentSection() {
156        mCurrent = null;
157    }
158
159    public void clear() {
160        mCurrent = null;
161        mDrawing.clear();
162    }
163
164}
165