119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford/*
219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * Copyright (C) 2013 The Android Open Source Project
319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford *
419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * Licensed under the Apache License, Version 2.0 (the "License");
519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * you may not use this file except in compliance with the License.
619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * You may obtain a copy of the License at
719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford *
819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford *      http://www.apache.org/licenses/LICENSE-2.0
919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford *
1019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * Unless required by applicable law or agreed to in writing, software
1119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * distributed under the License is distributed on an "AS IS" BASIS,
1219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * See the License for the specific language governing permissions and
1419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford * limitations under the License.
1519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford */
1619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
1719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordpackage com.android.gallery3d.filtershow.filters;
1819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
19f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport android.graphics.Color;
2019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport android.graphics.Path;
21370bd3d1678c7b214b28320913c7a632ebba47b8John Hofordimport android.graphics.PathMeasure;
22370bd3d1678c7b214b28320913c7a632ebba47b8John Hofordimport android.util.JsonReader;
23370bd3d1678c7b214b28320913c7a632ebba47b8John Hofordimport android.util.JsonWriter;
2499baf61387ab1ef15bb9db5fa3b2b55591e87059John Hofordimport android.util.Log;
2599baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford
266900cad45d240c9a54b92991538b6a33652e766cnicolasroardimport com.android.gallery3d.R;
27f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.BasicParameterInt;
28f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.BasicParameterStyle;
29f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.Parameter;
30f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterBrightness;
31a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport com.android.gallery3d.filtershow.controller.ParameterColor;
32f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterHue;
33f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterOpacity;
34f4b659334750a5aa75f929d18857a2ab93c9d939John Hofordimport com.android.gallery3d.filtershow.controller.ParameterSaturation;
356900cad45d240c9a54b92991538b6a33652e766cnicolasroardimport com.android.gallery3d.filtershow.editors.EditorDraw;
3619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
37370bd3d1678c7b214b28320913c7a632ebba47b8John Hofordimport java.io.IOException;
38370bd3d1678c7b214b28320913c7a632ebba47b8John Hofordimport java.util.Arrays;
39a4f38c0f8f70c998391780b672eea5d5e49277c7John Hofordimport java.util.Iterator;
4019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordimport java.util.Vector;
4119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
4219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hofordpublic class FilterDrawRepresentation extends FilterRepresentation {
4319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    private static final String LOGTAG = "FilterDrawRepresentation";
4419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
45f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public static final int PARAM_SIZE = 0;
46a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    public static final int PARAM_STYLE = 1;
47a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    public static final int PARAM_COLOR = 2;
486125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford    private BasicParameterInt mParamSize = new BasicParameterInt(PARAM_SIZE, 30, 2, 300);
49d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford    private BasicParameterStyle mParamStyle = new BasicParameterStyle(PARAM_STYLE, 5);
5049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public static int DEFAULT_MENU_COLOR1 = Color.RED & 0x80FFFFFF;
5149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public static int DEFAULT_MENU_COLOR2 = Color.GREEN & 0x80FFFFFF;
5249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public static int DEFAULT_MENU_COLOR3 = Color.BLUE & 0x80FFFFFF;
5349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public static int DEFAULT_MENU_COLOR4 = Color.BLACK & 0x80FFFFFF;
5449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public static int DEFAULT_MENU_COLOR5 = Color.WHITE & 0x80FFFFFF;
5549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    ParameterColor mParamColor = new ParameterColor(PARAM_COLOR,DEFAULT_MENU_COLOR1);
56f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    int mParamMode;
57f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    Parameter mCurrentParam = mParamSize;
58a7836003e689af28725930ba6a2c191275876577John Hoford    private static final String SERIAL_COLOR = "color";
59a7836003e689af28725930ba6a2c191275876577John Hoford    private static final String SERIAL_RADIUS = "radius";
60a7836003e689af28725930ba6a2c191275876577John Hoford    private static final String SERIAL_TYPE = "type";
61a7836003e689af28725930ba6a2c191275876577John Hoford    private static final String SERIAL_POINTS_COUNT = "point_count";
62a7836003e689af28725930ba6a2c191275876577John Hoford    private static final String SERIAL_POINTS = "points";
63a7836003e689af28725930ba6a2c191275876577John Hoford    private static final String SERIAL_PATH =  "path";
64a7836003e689af28725930ba6a2c191275876577John Hoford
65f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
66f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private Parameter[] mAllParam = {
67f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            mParamSize,
68a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mParamStyle,
69a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mParamColor
70f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    };
71f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
72f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public void setPramMode(int mode) {
73f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mParamMode = mode;
74f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        mCurrentParam = mAllParam[mParamMode];
75f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
76f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
774e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford    public int getParamMode() {
784e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford        return mParamMode;
794e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford    }
804e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford
81f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public Parameter getCurrentParam() {
82f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        return  mAllParam[mParamMode];
83f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
84f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
85f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    public Parameter getParam(int type) {
86f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        return  mAllParam[type];
87f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
88f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
8919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public static class StrokeData implements Cloneable {
9019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public byte mType;
9119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public Path mPath;
9219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public float mRadius;
9319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public int mColor;
9499baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        public int noPoints = 0;
95a7836003e689af28725930ba6a2c191275876577John Hoford        public float[] mPoints = new float[20];
96f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
97a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        public StrokeData() {
98a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        }
99a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
100a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        public StrokeData(StrokeData copy) {
101a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mType = copy.mType;
102a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mPath = new Path(copy.mPath);
103a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mRadius = copy.mRadius;
104a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mColor = copy.mColor;
105a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            noPoints = copy.noPoints;
106a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            mPoints = Arrays.copyOf(copy.mPoints, copy.mPoints.length);
107a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        }
108a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
109a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        @Override
110a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        public boolean equals(Object o) {
111a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            if (!(o instanceof StrokeData)) {
112a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                return false;
113a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            }
114a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            StrokeData sd = (StrokeData) o;
115a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            if (mType != sd.mType
116a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                    || mRadius != sd.mRadius
117a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                    || noPoints != sd.noPoints
118a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                    || mColor != sd.mColor) {
119a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                return false;
120a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            }
121a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            return mPath.equals(sd.mPath);
122a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        }
123a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
12419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        @Override
12519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public String toString() {
12619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return "stroke(" + mType + ", path(" + (mPath) + "), " + mRadius + " , "
12719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                    + Integer.toHexString(mColor) + ")";
12819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
129f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
13019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        @Override
13119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        public StrokeData clone() throws CloneNotSupportedException {
13219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return (StrokeData) super.clone();
13319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
13419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
13519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
136a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    static String colorHexString(int val) {
137a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        String str = "00000000" + Integer.toHexString(val);
138a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        str = "0x" + str.substring(str.length() - 8);
139a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        return str;
140a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    }
141f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
142a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford    public String getValueString() {
143a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        int val;
144f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        switch (mParamMode) {
145a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            case PARAM_COLOR:
146a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                val = ((ParameterColor) mAllParam[mParamMode]).getValue();
147fddcb482a89e6c30f74f0c8cf7d9d1e6e31a5789John Hoford                return "";
148f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            case PARAM_SIZE:
149a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                val = ((BasicParameterInt) mAllParam[mParamMode]).getValue();
150f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford                return ((val > 0) ? " +" : " ") + val;
151f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            case PARAM_STYLE:
152f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford                return "";
153f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        }
154f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        return "";
155f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
156f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
15719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    private Vector<StrokeData> mDrawing = new Vector<StrokeData>();
15819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    private StrokeData mCurrent; // used in the currently drawing style
15919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
16019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public FilterDrawRepresentation() {
16119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        super("Draw");
1626900cad45d240c9a54b92991538b6a33652e766cnicolasroard        setFilterClass(ImageFilterDraw.class);
1633cb106ddd7ad0f8494ac9c7a33660831c1694295Ruben Brunk        setSerializationName("DRAW");
1643cb106ddd7ad0f8494ac9c7a33660831c1694295Ruben Brunk        setFilterType(FilterRepresentation.TYPE_VIGNETTE);
1656900cad45d240c9a54b92991538b6a33652e766cnicolasroard        setTextId(R.string.imageDraw);
1666900cad45d240c9a54b92991538b6a33652e766cnicolasroard        setEditorId(EditorDraw.ID);
16738fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        setOverlayId(R.drawable.filtershow_drawing);
16838fc044a6d45e7283f05e6fe800bc5faf9f2e573nicolasroard        setOverlayOnly(true);
16919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
17019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
17119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
17219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public String toString() {
17319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return getName() + " : strokes=" + mDrawing.size()
17499baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                + ((mCurrent == null) ? " no current "
175f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford                : ("draw=" + mCurrent.mType + " " + mCurrent.noPoints));
17619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
17719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
17819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public Vector<StrokeData> getDrawing() {
17919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mDrawing;
18019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
18119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
18219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public StrokeData getCurrentDrawing() {
18319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return mCurrent;
18419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
18519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
18619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
1873f0034140c165b86fbaf5c0369f39431548308e9John Hoford    public FilterRepresentation copy() {
1883f0034140c165b86fbaf5c0369f39431548308e9John Hoford        FilterDrawRepresentation representation = new FilterDrawRepresentation();
1893f0034140c165b86fbaf5c0369f39431548308e9John Hoford        copyAllParameters(representation);
19019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return representation;
19119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
19219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
19319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
1943f0034140c165b86fbaf5c0369f39431548308e9John Hoford    protected void copyAllParameters(FilterRepresentation representation) {
1953f0034140c165b86fbaf5c0369f39431548308e9John Hoford        super.copyAllParameters(representation);
1963f0034140c165b86fbaf5c0369f39431548308e9John Hoford        representation.useParametersFrom(this);
1973f0034140c165b86fbaf5c0369f39431548308e9John Hoford    }
1983f0034140c165b86fbaf5c0369f39431548308e9John Hoford
1993f0034140c165b86fbaf5c0369f39431548308e9John Hoford    @Override
200856acb652fad51d650723a890cac08c536363fcdnicolasroard    public boolean isNil() {
201856acb652fad51d650723a890cac08c536363fcdnicolasroard        return getDrawing().isEmpty();
202856acb652fad51d650723a890cac08c536363fcdnicolasroard    }
203856acb652fad51d650723a890cac08c536363fcdnicolasroard
204856acb652fad51d650723a890cac08c536363fcdnicolasroard    @Override
20519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void useParametersFrom(FilterRepresentation a) {
20619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (a instanceof FilterDrawRepresentation) {
20719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            FilterDrawRepresentation representation = (FilterDrawRepresentation) a;
208ae068f80895d7d075064a5ded59069425b72271fJohn Hoford            mParamColor.copyPalletFrom(representation.mParamColor);
20919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            try {
21019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                if (representation.mCurrent != null) {
21119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                    mCurrent = (StrokeData) representation.mCurrent.clone();
21219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                } else {
21319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                    mCurrent = null;
21419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                }
21519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                if (representation.mDrawing != null) {
216a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                    mDrawing = new Vector<StrokeData>();
217a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                    for (Iterator<StrokeData> elem = representation.mDrawing.iterator(); elem.hasNext(); ) {
218a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                        StrokeData next =  elem.next();
219a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                        mDrawing.add(new StrokeData(next));
220a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                    }
221573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                } else {
222573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford                    mDrawing = null;
22319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                }
224573295249304781fdd5ff5e42c729455f5a1dc7cJohn Hoford
22519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            } catch (CloneNotSupportedException e) {
22619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford                e.printStackTrace();
22719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            }
22899baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford        } else {
22999baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            Log.v(LOGTAG, "cannot use parameters from " + a);
23019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
23119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
232856acb652fad51d650723a890cac08c536363fcdnicolasroard
23319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    @Override
23419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public boolean equals(FilterRepresentation representation) {
23519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (!super.equals(representation)) {
23619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford            return false;
23719161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
23819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        if (representation instanceof FilterDrawRepresentation) {
23999baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            FilterDrawRepresentation fdRep = (FilterDrawRepresentation) representation;
24099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            if (fdRep.mDrawing.size() != mDrawing.size())
24199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                return false;
242a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            if (fdRep.mCurrent == null ^ (mCurrent == null || mCurrent.mPath == null)) {
243a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                return false;
24499baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            }
245a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
246a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
247f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford            if (fdRep.mCurrent != null && mCurrent != null && mCurrent.mPath != null) {
24899baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                if (fdRep.mCurrent.noPoints == mCurrent.noPoints) {
24999baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                    return true;
25099baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                }
25199baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford                return false;
25299baf61387ab1ef15bb9db5fa3b2b55591e87059John Hoford            }
253a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford
254a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        int n = mDrawing.size();
255a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        for (int i = 0; i < n; i++) {
256a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            StrokeData a = mDrawing.get(i);
257a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            StrokeData b = mDrawing.get(i);
258a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            if (!a.equals(b)){
259a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford                return false;
260a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford            }
261a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        }
262a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        return true;
26319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        }
26419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        return false;
26519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
26619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
267f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    private int computeCurrentColor(){
268a4f38c0f8f70c998391780b672eea5d5e49277c7John Hoford        return mParamColor.getValue();
269f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford    }
270f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford
2714e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford    public void fillStrokeParameters(StrokeData sd){
272f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        byte type = (byte) mParamStyle.getSelected();
273f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        int color = computeCurrentColor();
274f4b659334750a5aa75f929d18857a2ab93c9d939John Hoford        float size = mParamSize.getValue();
2754e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford        sd.mColor = color;
2764e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford        sd.mRadius = size;
2774e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford        sd.mType = type;
2784e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford    }
279d3ca24707a0a0ab019dec0a17b0d540b685e459aJohn Hoford
2804e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford    public void startNewSection(float x, float y) {
28119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrent = new StrokeData();
2824e289659f746cf659e300c9fcd2048960e115f0cJohn Hoford        fillStrokeParameters(mCurrent);
28319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrent.mPath = new Path();
28419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrent.mPath.moveTo(x, y);
285370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        mCurrent.mPoints[0] = x;
286370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        mCurrent.mPoints[1] = y;
287370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        mCurrent.noPoints = 1;
28819161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
28919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
29019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void addPoint(float x, float y) {
291370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        int len = mCurrent.noPoints * 2;
29219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrent.mPath.lineTo(x, y);
293370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        if ((len+2) > mCurrent.mPoints.length) {
294370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            mCurrent.mPoints = Arrays.copyOf(mCurrent.mPoints, mCurrent.mPoints.length * 2);
295370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        }
296370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        mCurrent.mPoints[len] = x;
297370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        mCurrent.mPoints[len + 1] = y;
298370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        mCurrent.noPoints++;
29919161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
30019161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
30119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void endSection(float x, float y) {
302370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        addPoint(x, y);
30319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mDrawing.add(mCurrent);
30419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrent = null;
30519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
30619161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
3072fc7efdcd75279649091471dda3ee600db25a995John Hoford    public void clearCurrentSection() {
3082fc7efdcd75279649091471dda3ee600db25a995John Hoford        mCurrent = null;
3092fc7efdcd75279649091471dda3ee600db25a995John Hoford    }
3102fc7efdcd75279649091471dda3ee600db25a995John Hoford
31119161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    public void clear() {
31219161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mCurrent = null;
31319161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford        mDrawing.clear();
31419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford    }
315370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
316370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford    @Override
317370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford    public void serializeRepresentation(JsonWriter writer) throws IOException {
318370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        writer.beginObject();
319370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        int len = mDrawing.size();
320370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        int count = 0;
321370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        float[] mPosition = new float[2];
322370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        float[] mTan = new float[2];
323370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
324370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        PathMeasure mPathMeasure = new PathMeasure();
325370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        for (int i = 0; i < len; i++) {
326a7836003e689af28725930ba6a2c191275876577John Hoford            writer.name(SERIAL_PATH + i);
327370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.beginObject();
328370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            StrokeData mark = mDrawing.get(i);
329370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.name(SERIAL_COLOR).value(mark.mColor);
330370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.name(SERIAL_RADIUS).value(mark.mRadius);
331370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.name(SERIAL_TYPE).value(mark.mType);
332370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.name(SERIAL_POINTS_COUNT).value(mark.noPoints);
333370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.name(SERIAL_POINTS);
334370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
335370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.beginArray();
336370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            int npoints = mark.noPoints * 2;
337370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            for (int j = 0; j < npoints; j++) {
338370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                writer.value(mark.mPoints[j]);
339370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            }
340370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.endArray();
341370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            writer.endObject();
342370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        }
343370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        writer.endObject();
344370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford    }
34519161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford
346370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford    @Override
347370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford    public void deSerializeRepresentation(JsonReader sreader) throws IOException {
348370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        sreader.beginObject();
349370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        Vector<StrokeData> strokes = new Vector<StrokeData>();
350370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
351370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        while (sreader.hasNext()) {
352370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            sreader.nextName();
353370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            sreader.beginObject();
354370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            StrokeData stroke = new StrokeData();
355370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
356370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            while (sreader.hasNext()) {
357370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                String name = sreader.nextName();
358370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                if (name.equals(SERIAL_COLOR)) {
359370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    stroke.mColor = sreader.nextInt();
360370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                } else if (name.equals(SERIAL_RADIUS)) {
361370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    stroke.mRadius = (float) sreader.nextDouble();
362370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                } else if (name.equals(SERIAL_TYPE)) {
363370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    stroke.mType = (byte) sreader.nextInt();
364370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                } else if (name.equals(SERIAL_POINTS_COUNT)) {
365370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    stroke.noPoints = sreader.nextInt();
366370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                } else if (name.equals(SERIAL_POINTS)) {
367370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
368370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    int count = 0;
369370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    sreader.beginArray();
370370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    while (sreader.hasNext()) {
371370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                        if ((count + 1) > stroke.mPoints.length) {
372370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                            stroke.mPoints = Arrays.copyOf(stroke.mPoints, count * 2);
373370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                        }
374370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                        stroke.mPoints[count++] = (float) sreader.nextDouble();
375370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    }
376370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    stroke.mPath = new Path();
377370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    stroke.mPath.moveTo(stroke.mPoints[0], stroke.mPoints[1]);
378370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    for (int i = 0; i < count; i += 2) {
379370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                        stroke.mPath.lineTo(stroke.mPoints[i], stroke.mPoints[i + 1]);
380370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    }
381370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    sreader.endArray();
382370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    strokes.add(stroke);
383370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                } else {
384370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                    sreader.skipValue();
385370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford                }
386370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            }
387370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford            sreader.endObject();
388370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        }
389370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
390370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        mDrawing = strokes;
391370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford
392370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford        sreader.endObject();
393370bd3d1678c7b214b28320913c7a632ebba47b8John Hoford    }
39419161944e0efb1ffe23274d7cf5315ce047e9dacJohn Hoford}
395