OSDN Git Service

remove clone
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / FilterDrawRepresentation.java
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
17 package com.android.gallery3d.filtershow.filters;
18
19 import android.graphics.Path;
20 import android.util.Log;
21
22 import com.android.gallery3d.R;
23 import com.android.gallery3d.filtershow.editors.EditorDraw;
24
25 import java.util.Vector;
26
27 public 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         setSerializationName("DRAW");
54         setFilterType(FilterRepresentation.TYPE_VIGNETTE);
55         setTextId(R.string.imageDraw);
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 copy() {
78         FilterDrawRepresentation representation = new FilterDrawRepresentation();
79         copyAllParameters(representation);
80         return representation;
81     }
82
83     @Override
84     protected void copyAllParameters(FilterRepresentation representation) {
85         super.copyAllParameters(representation);
86         representation.useParametersFrom(this);
87     }
88
89     @Override
90     public boolean isNil() {
91         return getDrawing().isEmpty();
92     }
93
94     @Override
95     public void useParametersFrom(FilterRepresentation a) {
96         if (a instanceof FilterDrawRepresentation) {
97             FilterDrawRepresentation representation = (FilterDrawRepresentation) a;
98             try {
99                 if (representation.mCurrent != null) {
100                     mCurrent = (StrokeData) representation.mCurrent.clone();
101                 } else {
102                     mCurrent = null;
103                 }
104                 if (representation.mDrawing != null) {
105                     mDrawing = (Vector<StrokeData>) representation.mDrawing.clone();
106                 } else {
107                     mDrawing = null;
108                 }
109
110             } catch (CloneNotSupportedException e) {
111                 e.printStackTrace();
112             }
113         } else {
114             Log.v(LOGTAG, "cannot use parameters from " + a);
115         }
116     }
117
118     @Override
119     public boolean equals(FilterRepresentation representation) {
120         if (!super.equals(representation)) {
121             return false;
122         }
123         if (representation instanceof FilterDrawRepresentation) {
124             FilterDrawRepresentation fdRep = (FilterDrawRepresentation) representation;
125             if (fdRep.mDrawing.size() != mDrawing.size())
126                 return false;
127             if (fdRep.mCurrent == null && mCurrent.mPath == null) {
128                 return true;
129             }
130             if (fdRep.mCurrent != null && mCurrent.mPath != null) {
131                 if (fdRep.mCurrent.noPoints == mCurrent.noPoints) {
132                     return true;
133                 }
134                 return false;
135             }
136         }
137         return false;
138     }
139
140     public void startNewSection(byte type, int color, float size, float x, float y) {
141         mCurrent = new StrokeData();
142         mCurrent.mColor = color;
143         mCurrent.mRadius = size;
144         mCurrent.mType = type;
145         mCurrent.mPath = new Path();
146         mCurrent.mPath.moveTo(x, y);
147         mCurrent.noPoints = 0;
148     }
149
150     public void addPoint(float x, float y) {
151         mCurrent.noPoints++;
152         mCurrent.mPath.lineTo(x, y);
153     }
154
155     public void endSection(float x, float y) {
156         mCurrent.mPath.lineTo(x, y);
157         mCurrent.noPoints++;
158         mDrawing.add(mCurrent);
159         mCurrent = null;
160     }
161
162     public void clearCurrentSection() {
163         mCurrent = null;
164     }
165
166     public void clear() {
167         mCurrent = null;
168         mDrawing.clear();
169     }
170
171 }