Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2009 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.cooliris.media;
     18 
     19 import java.util.HashMap;
     20 
     21 import javax.microedition.khronos.opengles.GL11;
     22 
     23 import com.cooliris.app.App;
     24 import com.cooliris.app.Res;
     25 
     26 public final class GridDrawables {
     27     // The display primitives.
     28     public static GridQuad sGrid;
     29     public static GridQuadFrame sFrame;
     30     public static GridQuad sTextGrid;
     31     public static GridQuad sSelectedGrid;
     32     public static GridQuad sVideoGrid;
     33     public static GridQuad sLocationGrid;
     34     public static GridQuad sSourceIconGrid;
     35     public static final GridQuad[] sFullscreenGrid = new GridQuad[3];
     36 
     37     // All the resource Textures.
     38     private static final int TEXTURE_FRAME = Res.drawable.stack_frame;
     39     private static final int TEXTURE_GRID_FRAME = Res.drawable.grid_frame;
     40     private static final int TEXTURE_FRAME_FOCUS = Res.drawable.stack_frame_focus;
     41     private static final int TEXTURE_FRAME_PRESSED = Res.drawable.stack_frame_gold;
     42     private static final int TEXTURE_LOCATION = Res.drawable.btn_location_filter_unscaled;
     43     private static final int TEXTURE_VIDEO = Res.drawable.videooverlay;
     44     private static final int TEXTURE_CHECKMARK_ON = Res.drawable.grid_check_on;
     45     private static final int TEXTURE_CHECKMARK_OFF = Res.drawable.grid_check_off;
     46     private static final int TEXTURE_CAMERA_SMALL = Res.drawable.icon_camera_small_unscaled;
     47     private static final int TEXTURE_PICASA_SMALL = Res.drawable.icon_picasa_small_unscaled;
     48     public static final int[] TEXTURE_SPINNER = new int[8];
     49     private static final int TEXTURE_TRANSPARENT = Res.drawable.transparent;
     50     private static final int TEXTURE_PLACEHOLDER = Res.drawable.grid_placeholder;
     51 
     52     public Texture mTextureFrame;
     53     public Texture mTextureGridFrame;
     54     public Texture mTextureFrameFocus;
     55     public Texture mTextureFramePressed;
     56     public Texture mTextureLocation;
     57     public Texture mTextureVideo;
     58     public Texture mTextureCheckmarkOn;
     59     public Texture mTextureCheckmarkOff;
     60     public Texture mTextureCameraSmall;
     61     public Texture mTexturePicasaSmall;
     62     public Texture[] mTextureSpinner = new Texture[8];
     63     public Texture mTextureTransparent;
     64     public Texture mTexturePlaceholder;
     65 
     66     // The textures generated from strings.
     67     public static final HashMap<String, StringTexture> sStringTextureTable = new HashMap<String, StringTexture>(128);
     68 
     69     static {
     70         // We first populate the spinner textures.
     71         final int[] textureSpinner = TEXTURE_SPINNER;
     72         textureSpinner[0] = Res.drawable.ic_spinner1;
     73         textureSpinner[1] = Res.drawable.ic_spinner2;
     74         textureSpinner[2] = Res.drawable.ic_spinner3;
     75         textureSpinner[3] = Res.drawable.ic_spinner4;
     76         textureSpinner[4] = Res.drawable.ic_spinner5;
     77         textureSpinner[5] = Res.drawable.ic_spinner6;
     78         textureSpinner[6] = Res.drawable.ic_spinner7;
     79         textureSpinner[7] = Res.drawable.ic_spinner8;
     80     }
     81 
     82     public GridDrawables(final int itemWidth, final int itemHeight) {
     83         if (sGrid == null) {
     84             final float height = 1.0f;
     85             final float width = (float) (height * itemWidth) / (float) itemHeight;
     86             final float aspectRatio = (float) itemWidth / (float) itemHeight;
     87             final float oneByAspect = 1.0f / aspectRatio;
     88 
     89             // We create the grid quad.
     90             sGrid = GridQuad.createGridQuad(width, height, 0, 0, 1.0f, oneByAspect, true);
     91 
     92             // We create the quads used in fullscreen.
     93             sFullscreenGrid[0] = GridQuad.createGridQuad(width, height, 0, 0, 1.0f, oneByAspect, false);
     94             sFullscreenGrid[0].setDynamic(true);
     95             sFullscreenGrid[1] = GridQuad.createGridQuad(width, height, 0, 0, 1.0f, oneByAspect, false);
     96             sFullscreenGrid[1].setDynamic(true);
     97             sFullscreenGrid[2] = GridQuad.createGridQuad(width, height, 0, 0, 1.0f, oneByAspect, false);
     98             sFullscreenGrid[2].setDynamic(true);
     99 
    100             // We create supplementary quads for the checkmarks, video overlay
    101             // and location button
    102             float sizeOfSelectedIcon = 32 * App.PIXEL_DENSITY; // In pixels.
    103             sizeOfSelectedIcon /= itemHeight;
    104             float sizeOfLocationIcon = 52 * App.PIXEL_DENSITY; // In pixels.
    105             sizeOfLocationIcon /= itemHeight;
    106             float sizeOfSourceIcon = 76 * App.PIXEL_DENSITY; // In pixels.
    107             sizeOfSourceIcon /= itemHeight;
    108             sSelectedGrid = GridQuad.createGridQuad(sizeOfSelectedIcon, sizeOfSelectedIcon, -0.5f, 0.25f, 1.0f, 1.0f, false);
    109             sVideoGrid = GridQuad.createGridQuad(sizeOfSelectedIcon, sizeOfSelectedIcon, -0.08f, -0.09f, 1.0f, 1.0f, false);
    110             sLocationGrid = GridQuad.createGridQuad(sizeOfLocationIcon, sizeOfLocationIcon, 0, 0, 1.0f, 1.0f, false);
    111             sSourceIconGrid = GridQuad.createGridQuad(sizeOfSourceIcon, sizeOfSourceIcon, 0, 0, 1.0f, 1.0f, false);
    112 
    113             // We create the quad for the text label.
    114             float seedTextWidth = (App.PIXEL_DENSITY < 1.5f) ? 128.0f : 256.0f;
    115             float textWidth = (seedTextWidth / (float) itemWidth) * width;
    116             float textHeightPow2 = (App.PIXEL_DENSITY < 1.5f) ? 32.0f : 64.0f;
    117             float textHeight = (textHeightPow2 / (float) itemHeight) * height;
    118             float textOffsetY = 0.0f;
    119             sTextGrid = GridQuad.createGridQuad(textWidth, textHeight, 0, textOffsetY, 1.0f, 1.0f, false);
    120 
    121             // We finally create the frame around every grid item
    122             sFrame = GridQuadFrame.createFrame(width, height, itemWidth, itemHeight);
    123         }
    124     }
    125 
    126     public void onSurfaceCreated(RenderView view, GL11 gl) {
    127         // The grid quad.
    128         sGrid.freeHardwareBuffers(gl);
    129         sGrid.generateHardwareBuffers(gl);
    130 
    131         // The fullscreen quads.
    132         sFullscreenGrid[0].freeHardwareBuffers(gl);
    133         sFullscreenGrid[1].freeHardwareBuffers(gl);
    134         sFullscreenGrid[2].freeHardwareBuffers(gl);
    135         sFullscreenGrid[0].generateHardwareBuffers(gl);
    136         sFullscreenGrid[1].generateHardwareBuffers(gl);
    137         sFullscreenGrid[2].generateHardwareBuffers(gl);
    138 
    139         // Supplementary quads.
    140         sSelectedGrid.freeHardwareBuffers(gl);
    141         sVideoGrid.freeHardwareBuffers(gl);
    142         sLocationGrid.freeHardwareBuffers(gl);
    143         sSourceIconGrid.freeHardwareBuffers(gl);
    144         sSelectedGrid.generateHardwareBuffers(gl);
    145         sVideoGrid.generateHardwareBuffers(gl);
    146         sLocationGrid.generateHardwareBuffers(gl);
    147         sSourceIconGrid.generateHardwareBuffers(gl);
    148 
    149         // Text quads.
    150         sTextGrid.freeHardwareBuffers(gl);
    151         sTextGrid.generateHardwareBuffers(gl);
    152 
    153         // Frame mesh.
    154         sFrame.freeHardwareBuffers(gl);
    155         sFrame.generateHardwareBuffers(gl);
    156 
    157         // Clear the string table.
    158         sStringTextureTable.clear();
    159 
    160         // Regenerate all the textures.
    161         mTextureFrame = view.getResource(TEXTURE_FRAME, false);
    162         mTextureGridFrame = view.getResource(TEXTURE_GRID_FRAME, false);
    163         mTextureFrameFocus = view.getResource(TEXTURE_FRAME_FOCUS, false);
    164         mTextureFramePressed = view.getResource(TEXTURE_FRAME_PRESSED, false);
    165         mTextureLocation = view.getResource(TEXTURE_LOCATION, false);
    166         mTextureVideo = view.getResource(TEXTURE_VIDEO, false);
    167         mTextureCheckmarkOn = view.getResource(TEXTURE_CHECKMARK_ON, false);
    168         mTextureCheckmarkOff = view.getResource(TEXTURE_CHECKMARK_OFF, false);
    169         mTextureCameraSmall = view.getResource(TEXTURE_CAMERA_SMALL, false);
    170         mTexturePicasaSmall = view.getResource(TEXTURE_PICASA_SMALL, false);
    171         mTextureTransparent = view.getResource(TEXTURE_TRANSPARENT, false);
    172         mTexturePlaceholder = view.getResource(TEXTURE_PLACEHOLDER, false);
    173         view.loadTexture(mTextureFrame);
    174         view.loadTexture(mTextureGridFrame);
    175         view.loadTexture(mTextureFrameFocus);
    176         view.loadTexture(mTextureFramePressed);
    177 
    178         mTextureSpinner[0] = view.getResource(Res.drawable.ic_spinner1);
    179         mTextureSpinner[1] = view.getResource(Res.drawable.ic_spinner2);
    180         mTextureSpinner[2] = view.getResource(Res.drawable.ic_spinner3);
    181         mTextureSpinner[3] = view.getResource(Res.drawable.ic_spinner4);
    182         mTextureSpinner[4] = view.getResource(Res.drawable.ic_spinner5);
    183         mTextureSpinner[5] = view.getResource(Res.drawable.ic_spinner6);
    184         mTextureSpinner[6] = view.getResource(Res.drawable.ic_spinner7);
    185         mTextureSpinner[7] = view.getResource(Res.drawable.ic_spinner8);
    186     }
    187 
    188     public int getIconForSet(MediaSet set, boolean scaled) {
    189         // We return the scaled version for HUD rendering and the unscaled
    190         // version for 3D rendering.
    191         if (scaled) {
    192             if (set == null) {
    193                 return Res.drawable.icon_folder_small;
    194             }
    195             if (set.mPicasaAlbumId != Shared.INVALID) {
    196                 return Res.drawable.icon_picasa_small;
    197             } else if (set.mId == LocalDataSource.CAMERA_BUCKET_ID) {
    198                 return Res.drawable.icon_camera_small;
    199             } else {
    200                 return Res.drawable.icon_folder_small;
    201             }
    202         } else {
    203             if (set == null) {
    204                 return Res.drawable.icon_folder_small_unscaled;
    205             }
    206             if (set.mPicasaAlbumId != Shared.INVALID) {
    207                 return Res.drawable.icon_picasa_small_unscaled;
    208             } else if (set.mId == LocalDataSource.CAMERA_BUCKET_ID) {
    209                 return Res.drawable.icon_camera_small_unscaled;
    210             } else {
    211                 return Res.drawable.icon_folder_small_unscaled;
    212             }
    213         }
    214     }
    215 }
    216