Home | History | Annotate | Download | only in assetstudiolib
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
      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 package com.android.assetstudiolib;
     17 
     18 import com.android.assetstudiolib.Util.Effect;
     19 import com.android.assetstudiolib.Util.FillEffect;
     20 import com.android.assetstudiolib.Util.ShadowEffect;
     21 
     22 import java.awt.Color;
     23 import java.awt.GradientPaint;
     24 import java.awt.Graphics2D;
     25 import java.awt.Rectangle;
     26 import java.awt.image.BufferedImage;
     27 import java.util.Map;
     28 
     29 
     30 /**
     31  * Generate icons for tabs
     32  */
     33 public class TabIconGenerator extends GraphicGenerator {
     34     /** Creates a new {@link TabIconGenerator} */
     35     public TabIconGenerator() {
     36     }
     37 
     38     @Override
     39     public BufferedImage generate(GraphicGeneratorContext context, Options options) {
     40         Rectangle iconSizeHdpi = new Rectangle(0, 0, 48, 48);
     41         Rectangle targetRectHdpi = new Rectangle(3, 3, 42, 42);
     42         final float scaleFactor = GraphicGenerator.getHdpiScaleFactor(options.density);
     43         Rectangle imageRect = Util.scaleRectangle(iconSizeHdpi, scaleFactor);
     44         Rectangle targetRect = Util.scaleRectangle(targetRectHdpi, scaleFactor);
     45         BufferedImage outImage = Util.newArgbBufferedImage(imageRect.width, imageRect.height);
     46         Graphics2D g = (Graphics2D) outImage.getGraphics();
     47 
     48         BufferedImage tempImage = Util.newArgbBufferedImage(
     49                 imageRect.width, imageRect.height);
     50         Graphics2D g2 = (Graphics2D) tempImage.getGraphics();
     51         Util.drawCenterInside(g2, options.sourceImage, targetRect);
     52 
     53         TabOptions tabOptions = (TabOptions) options;
     54         if (tabOptions.selected) {
     55             if (tabOptions.oldStyle) {
     56                 Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
     57                         new FillEffect(
     58                                 new GradientPaint(
     59                                         0, 0,
     60                                         new Color(0xa3a3a3),
     61                                         0, imageRect.height,
     62                                         new Color(0x787878))),
     63                         new ShadowEffect(
     64                                 0,
     65                                 3 * scaleFactor,
     66                                 3 * scaleFactor,
     67                                 Color.BLACK,
     68                                 0.2,
     69                                 true),
     70                         new ShadowEffect(
     71                                 0,
     72                                 1,
     73                                 0,
     74                                 Color.BLACK,
     75                                 0.35,
     76                                 true),
     77                         new ShadowEffect(
     78                                 0,
     79                                 -1,
     80                                 0,
     81                                 Color.WHITE,
     82                                 0.35,
     83                                 true),
     84                 });
     85             } else {
     86                 Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
     87                         new FillEffect(Color.WHITE),
     88                         new ShadowEffect(
     89                                 0,
     90                                 0,
     91                                 5 * scaleFactor,
     92                                 Color.BLACK,
     93                                 0.25,
     94                                 false),
     95                 });
     96             }
     97         } else {
     98             // Unselected
     99             if (tabOptions.oldStyle) {
    100                 Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
    101                         new FillEffect(
    102                                 new GradientPaint(
    103                                         0, 0.25f * imageRect.height,
    104                                         new Color(0xf9f9f9),
    105                                         0, imageRect.height,
    106                                         new Color(0xdfdfdf))),
    107                         new ShadowEffect(
    108                                 0,
    109                                 3 * scaleFactor,
    110                                 3 * scaleFactor,
    111                                 Color.BLACK,
    112                                 0.1,
    113                                 true),
    114                         new ShadowEffect(
    115                                 0,
    116                                 1,
    117                                 0,
    118                                 Color.BLACK,
    119                                 0.35,
    120                                 true),
    121                         new ShadowEffect(
    122                                 0,
    123                                 -1,
    124                                 0,
    125                                 Color.WHITE,
    126                                 0.35,
    127                                 true),
    128                 });
    129             } else {
    130                 Util.drawEffects(g, tempImage, 0, 0, new Effect[] {
    131                         new FillEffect(new Color(0x808080)),
    132                 });
    133             }
    134         }
    135 
    136         g.dispose();
    137         g2.dispose();
    138 
    139         return outImage;
    140     }
    141 
    142     @Override
    143     public void generate(String category, Map<String, Map<String, BufferedImage>> categoryMap,
    144             GraphicGeneratorContext context, Options baseOptions, String name) {
    145         TabOptions options = (TabOptions) baseOptions;
    146         // Generate all permutations of tabOptions.selected and tabOptions.oldStyle
    147         options.selected = true;
    148         options.oldStyle = false;
    149 
    150         String selectedLabelV5 = "Selected (v5+)";
    151         String unselectedLabelV5 = "Unselected (v5+)";
    152         String selectedLabel = "Selected";
    153         String unselectedLabel = "Unselected";
    154 
    155         boolean generateOldStyle = options.minSdk < 5;
    156         if (generateOldStyle) {
    157             options.oldStyle = true;
    158             options.selected = true;
    159             super.generate(selectedLabel, categoryMap, context, options, name);
    160             options.selected = false;
    161             super.generate(unselectedLabel, categoryMap, context, options, name);
    162         }
    163 
    164         options.oldStyle = false;
    165         options.selected = true;
    166         super.generate(generateOldStyle ? unselectedLabelV5 : unselectedLabel,
    167                 categoryMap, context, options, name);
    168         options.selected = false;
    169         super.generate(generateOldStyle ? selectedLabelV5 : selectedLabel,
    170                 categoryMap, context, options, name);
    171     }
    172 
    173     @Override
    174     protected String getIconFolder(Options options) {
    175         String folder = super.getIconFolder(options);
    176 
    177         TabOptions tabOptions = (TabOptions) options;
    178         if (tabOptions.oldStyle || options.minSdk >= 5) {
    179             return folder;
    180         } else {
    181             return folder + "-v5"; //$NON-NLS-1$
    182         }
    183     }
    184 
    185     @Override
    186     protected String getIconName(Options options, String name) {
    187         TabOptions tabOptions = (TabOptions) options;
    188         if (tabOptions.selected) {
    189             return name + "_selected.png"; //$NON-NLS-1$
    190         } else {
    191             return name + "_unselected.png"; //$NON-NLS-1$
    192         }
    193     }
    194 
    195     /** Options specific to generating tab icons */
    196     public static class TabOptions extends GraphicGenerator.Options {
    197         /** Generate icon in the style used prior to v5 */
    198         public boolean oldStyle;
    199         /** Generate "selected" icon if true, and "unselected" icon if false */
    200         public boolean selected = true;
    201     }
    202 }
    203