FilterStraightenRepresentation.java revision 8f442fae60e0154867d2a6927eb9a35bcb7014e6
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.util.JsonReader;
20import android.util.JsonWriter;
21import android.util.Log;
22
23import com.android.gallery3d.R;
24import com.android.gallery3d.filtershow.editors.EditorStraighten;
25
26import java.io.IOException;
27
28public class FilterStraightenRepresentation extends FilterRepresentation {
29    public static final String SERIALIZATION_NAME = "STRAIGHTEN";
30    public static final String SERIALIZATION_STRAIGHTEN_VALUE = "value";
31    private static final String TAG = FilterStraightenRepresentation.class.getSimpleName();
32
33    float mStraighten;
34
35    public FilterStraightenRepresentation(float straighten) {
36        super(FilterStraightenRepresentation.class.getSimpleName());
37        setSerializationName(SERIALIZATION_NAME);
38        setShowParameterValue(true);
39        setFilterClass(FilterStraightenRepresentation.class);
40        setFilterType(FilterRepresentation.TYPE_GEOMETRY);
41        setTextId(R.string.straighten);
42        setEditorId(EditorStraighten.ID);
43        setStraighten(straighten);
44    }
45
46    public FilterStraightenRepresentation(FilterStraightenRepresentation s) {
47        this(s.getStraighten());
48    }
49
50    public FilterStraightenRepresentation() {
51        this(0);
52    }
53
54    public void set(FilterStraightenRepresentation r) {
55        mStraighten = r.mStraighten;
56    }
57
58    @Override
59    public boolean equals(FilterRepresentation rep) {
60        if (!(rep instanceof FilterStraightenRepresentation)) {
61            return false;
62        }
63        FilterStraightenRepresentation straighten = (FilterStraightenRepresentation) rep;
64        if (straighten.mStraighten != mStraighten) {
65            return false;
66        }
67        return true;
68    }
69
70    public float getStraighten() {
71        return mStraighten;
72    }
73
74    public void setStraighten(float straighten) {
75        if (!rangeCheck(straighten)) {
76            straighten = Math.min(Math.max(straighten, -45), 45);
77        }
78        mStraighten = straighten;
79    }
80
81    @Override
82    public boolean allowsSingleInstanceOnly() {
83        return true;
84    }
85
86    @Override
87    public FilterRepresentation copy() {
88        return new FilterStraightenRepresentation(this);
89    }
90
91    @Override
92    protected void copyAllParameters(FilterRepresentation representation) {
93        if (!(representation instanceof FilterStraightenRepresentation)) {
94            throw new IllegalArgumentException("calling copyAllParameters with incompatible types!");
95        }
96        super.copyAllParameters(representation);
97        representation.useParametersFrom(this);
98    }
99
100    @Override
101    public void useParametersFrom(FilterRepresentation a) {
102        if (!(a instanceof FilterStraightenRepresentation)) {
103            throw new IllegalArgumentException("calling useParametersFrom with incompatible types!");
104        }
105        setStraighten(((FilterStraightenRepresentation) a).getStraighten());
106    }
107
108    @Override
109    public boolean isNil() {
110        return mStraighten == 0;
111    }
112
113    @Override
114    public void serializeRepresentation(JsonWriter writer) throws IOException {
115        writer.beginObject();
116        writer.name(SERIALIZATION_STRAIGHTEN_VALUE).value(mStraighten);
117        writer.endObject();
118    }
119
120    @Override
121    public void deSerializeRepresentation(JsonReader reader) throws IOException {
122        boolean unset = true;
123        reader.beginObject();
124        while (reader.hasNext()) {
125            String name = reader.nextName();
126            if (SERIALIZATION_STRAIGHTEN_VALUE.equals(name)) {
127                int s = reader.nextInt();
128                if (rangeCheck(s)) {
129                    setStraighten(s);
130                    unset = false;
131                }
132            } else {
133                reader.skipValue();
134            }
135        }
136        if (unset) {
137            Log.w(TAG, "WARNING: bad value when deserializing " + SERIALIZATION_NAME);
138        }
139        reader.endObject();
140    }
141
142    private boolean rangeCheck(float s) {
143        if (s < -45 || s > 45) {
144            return false;
145        }
146        return true;
147    }
148}
149