Home | History | Annotate | Download | only in filters
      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.Color;
     20 import android.util.JsonReader;
     21 import android.util.JsonWriter;
     22 
     23 import com.android.gallery3d.R;
     24 import com.android.gallery3d.filtershow.controller.BasicParameterInt;
     25 import com.android.gallery3d.filtershow.controller.Parameter;
     26 import com.android.gallery3d.filtershow.controller.ParameterColor;
     27 import com.android.gallery3d.filtershow.editors.EditorColorBorder;
     28 
     29 import java.io.IOException;
     30 
     31 public class FilterColorBorderRepresentation extends FilterRepresentation {
     32     private static final String LOGTAG = "FilterColorBorderRepresentation";
     33     private static final String SERIALIZATION_NAME = "COLORBORDER";
     34 
     35     public static final int PARAM_SIZE = 0;
     36     public static final int PARAM_RADIUS = 1;
     37     public static final int PARAM_COLOR = 2;
     38     public static int DEFAULT_MENU_COLOR1 = Color.WHITE;
     39     public static int DEFAULT_MENU_COLOR2 = Color.BLACK;
     40     public static int DEFAULT_MENU_COLOR3 = Color.GRAY;
     41     public static int DEFAULT_MENU_COLOR4 = 0xFFFFCCAA;
     42     public static int DEFAULT_MENU_COLOR5 = 0xFFAAAAAA;
     43     private BasicParameterInt mParamSize = new BasicParameterInt(PARAM_SIZE, 3, 2, 30);
     44     private BasicParameterInt mParamRadius = new BasicParameterInt(PARAM_RADIUS, 2, 0, 100);
     45     private ParameterColor mParamColor = new ParameterColor(PARAM_COLOR, DEFAULT_MENU_COLOR1);
     46 
     47     private Parameter[] mAllParam = {
     48             mParamSize,
     49             mParamRadius,
     50             mParamColor
     51     };
     52     private int mPramMode;
     53 
     54     public FilterColorBorderRepresentation(int color, int size, int radius) {
     55         super("ColorBorder");
     56         setSerializationName(SERIALIZATION_NAME);
     57         setFilterType(FilterRepresentation.TYPE_BORDER);
     58         setTextId(R.string.borders);
     59         setEditorId(EditorColorBorder.ID);
     60         setShowParameterValue(false);
     61         setFilterClass(ImageFilterColorBorder.class);
     62         mParamColor.setValue(color);
     63         mParamSize.setValue(size);
     64         mParamRadius.setValue(radius);
     65         mParamColor.setColorpalette(new int[]{
     66                 DEFAULT_MENU_COLOR1,
     67                 DEFAULT_MENU_COLOR2,
     68                 DEFAULT_MENU_COLOR3,
     69                 DEFAULT_MENU_COLOR4,
     70                 DEFAULT_MENU_COLOR5});
     71     }
     72 
     73     public String toString() {
     74         return "FilterBorder: " + getName();
     75     }
     76 
     77     @Override
     78     public FilterRepresentation copy() {
     79         FilterColorBorderRepresentation representation =
     80                 new FilterColorBorderRepresentation(0, 0, 0);
     81         copyAllParameters(representation);
     82         return representation;
     83     }
     84 
     85     @Override
     86     protected void copyAllParameters(FilterRepresentation representation) {
     87         super.copyAllParameters(representation);
     88         representation.useParametersFrom(this);
     89     }
     90 
     91     public void useParametersFrom(FilterRepresentation a) {
     92         if (a instanceof FilterColorBorderRepresentation) {
     93             FilterColorBorderRepresentation representation = (FilterColorBorderRepresentation) a;
     94             setName(representation.getName());
     95             setColor(representation.getColor());
     96             mParamColor.copyPalletFrom(representation.mParamColor);
     97             setBorderSize(representation.getBorderSize());
     98             setBorderRadius(representation.getBorderRadius());
     99         }
    100     }
    101 
    102     @Override
    103     public boolean equals(FilterRepresentation representation) {
    104         if (!super.equals(representation)) {
    105             return false;
    106         }
    107         if (representation instanceof FilterColorBorderRepresentation) {
    108             FilterColorBorderRepresentation border = (FilterColorBorderRepresentation) representation;
    109             if (border.mParamColor.getValue() == mParamColor.getValue()
    110                     && border.mParamRadius.getValue() == mParamRadius.getValue()
    111                     && border.mParamSize.getValue() == mParamSize.getValue()) {
    112 
    113                 return true;
    114             }
    115         }
    116         return false;
    117     }
    118 
    119     public boolean allowsSingleInstanceOnly() {
    120         return true;
    121     }
    122 
    123     public Parameter getParam(int mode) {
    124         return mAllParam[mode];
    125     }
    126 
    127     @Override
    128     public int getTextId() {
    129         if (super.getTextId() == 0) {
    130             return R.string.borders;
    131         }
    132         return super.getTextId();
    133     }
    134 
    135     public int getColor() {
    136         return mParamColor.getValue();
    137     }
    138 
    139     public void setColor(int color) {
    140         mParamColor.setValue(color);
    141     }
    142 
    143     public int getBorderSize() {
    144         return mParamSize.getValue();
    145     }
    146 
    147     public void setBorderSize(int borderSize) {
    148         mParamSize.setValue(borderSize);
    149     }
    150 
    151     public int getBorderRadius() {
    152         return mParamRadius.getValue();
    153     }
    154 
    155     public void setBorderRadius(int borderRadius) {
    156         mParamRadius.setValue(borderRadius);
    157     }
    158 
    159     public void setPramMode(int pramMode) {
    160         this.mPramMode = pramMode;
    161     }
    162 
    163     public Parameter getCurrentParam() {
    164         return mAllParam[mPramMode];
    165     }
    166 
    167     public String getValueString() {
    168         return "";
    169     }
    170 
    171     // Serialization...
    172 
    173     public void serializeRepresentation(JsonWriter writer) throws IOException {
    174         writer.beginObject();
    175         {
    176             writer.name("size");
    177             writer.value(mParamSize.getValue());
    178             writer.name("radius");
    179             writer.value(mParamRadius.getValue());
    180             writer.name("color");
    181             writer.value(mParamColor.getValue());
    182         }
    183         writer.endObject();
    184     }
    185 
    186     public void deSerializeRepresentation(JsonReader reader) throws IOException {
    187         reader.beginObject();
    188         while (reader.hasNext()) {
    189             String name = reader.nextName();
    190             if (name.equalsIgnoreCase("size")) {
    191                 mParamSize.setValue(reader.nextInt());
    192             } else if (name.equalsIgnoreCase("radius")) {
    193                 mParamRadius.setValue(reader.nextInt());
    194             } else if (name.equalsIgnoreCase("color")) {
    195                 mParamColor.setValue(reader.nextInt());
    196             } else {
    197                 reader.skipValue();
    198             }
    199         }
    200         reader.endObject();
    201     }
    202 }
    203