Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package com.android.gallery3d.ui; |
| 17 | |
| 18 | import com.android.gallery3d.R; |
| 19 | import com.android.gallery3d.data.MediaObject; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | |
| 23 | public abstract class IconDrawer extends SelectionDrawer { |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 24 | private static final String TAG = "IconDrawer"; |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 25 | private static final int LABEL_BACKGROUND_COLOR = 0x99000000; // 60% black |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 26 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 27 | private final ResourceTexture mLocalSetIcon; |
| 28 | private final ResourceTexture mCameraIcon; |
| 29 | private final ResourceTexture mPicasaIcon; |
| 30 | private final ResourceTexture mMtpIcon; |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 31 | private final NinePatchTexture mFramePressed; |
Chih-Chung Chang | e3312ff | 2011-09-22 14:55:32 +0800 | [diff] [blame^] | 32 | private final NinePatchTexture mFrameSelected; |
| 33 | private final NinePatchTexture mDarkStrip; |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 34 | private final ResourceTexture mPanoramaBorder; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 35 | private final Texture mVideoOverlay; |
| 36 | private final Texture mVideoPlayIcon; |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 37 | private final int mIconSize; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 38 | |
| 39 | public static class IconDimension { |
| 40 | int x; |
| 41 | int y; |
| 42 | int width; |
| 43 | int height; |
| 44 | } |
| 45 | |
| 46 | public IconDrawer(Context context) { |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 47 | mLocalSetIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_folder); |
| 48 | mCameraIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_camera); |
| 49 | mPicasaIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_picasa); |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 50 | mMtpIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_ptp); |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 51 | mVideoOverlay = new ResourceTexture(context, R.drawable.ic_video_thumb); |
| 52 | mVideoPlayIcon = new ResourceTexture(context, R.drawable.ic_gallery_play); |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 53 | mPanoramaBorder = new ResourceTexture(context, R.drawable.ic_pan_thumb); |
| 54 | mFramePressed = new NinePatchTexture(context, R.drawable.grid_pressed); |
Chih-Chung Chang | e3312ff | 2011-09-22 14:55:32 +0800 | [diff] [blame^] | 55 | mFrameSelected = new NinePatchTexture(context, R.drawable.grid_selected); |
| 56 | mDarkStrip = new NinePatchTexture(context, R.drawable.dark_strip); |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 57 | mIconSize = context.getResources().getDimensionPixelSize( |
| 58 | R.dimen.albumset_icon_size); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void prepareDrawing() { |
| 63 | } |
| 64 | |
| 65 | protected IconDimension drawIcon(GLCanvas canvas, int width, int height, |
| 66 | int dataSourceType) { |
| 67 | ResourceTexture icon = getIcon(dataSourceType); |
| 68 | |
| 69 | if (icon != null) { |
| 70 | IconDimension id = getIconDimension(icon, width, height); |
| 71 | icon.draw(canvas, id.x, id.y, id.width, id.height); |
| 72 | return id; |
| 73 | } |
| 74 | return null; |
| 75 | } |
| 76 | |
| 77 | protected ResourceTexture getIcon(int dataSourceType) { |
| 78 | ResourceTexture icon = null; |
| 79 | switch (dataSourceType) { |
| 80 | case DATASOURCE_TYPE_LOCAL: |
| 81 | icon = mLocalSetIcon; |
| 82 | break; |
| 83 | case DATASOURCE_TYPE_PICASA: |
| 84 | icon = mPicasaIcon; |
| 85 | break; |
| 86 | case DATASOURCE_TYPE_CAMERA: |
| 87 | icon = mCameraIcon; |
| 88 | break; |
| 89 | case DATASOURCE_TYPE_MTP: |
| 90 | icon = mMtpIcon; |
| 91 | break; |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | return icon; |
| 97 | } |
| 98 | |
| 99 | protected IconDimension getIconDimension(ResourceTexture icon, int width, |
| 100 | int height) { |
| 101 | IconDimension id = new IconDimension(); |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 102 | float scale = (float) mIconSize / icon.getWidth(); |
| 103 | id.width = Math.round(scale * icon.getWidth()); |
| 104 | id.height = Math.round(scale * icon.getHeight()); |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 105 | id.x = -width / 2; |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 106 | id.y = (height + 1) / 2 - id.height; |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 107 | return id; |
| 108 | } |
| 109 | |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 110 | protected void drawMediaTypeOverlay(GLCanvas canvas, int mediaType, |
| 111 | boolean isPanorama, int x, int y, int width, int height, |
| 112 | int topIndex) { |
| 113 | if (mediaType == MediaObject.MEDIA_TYPE_VIDEO) { |
| 114 | drawVideoOverlay(canvas, x, y, width, height, topIndex); |
| 115 | } |
| 116 | if (isPanorama) { |
| 117 | drawPanoramaBorder(canvas, x, y, width, height); |
| 118 | } |
| 119 | } |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 120 | |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 121 | protected void drawVideoOverlay(GLCanvas canvas, int x, int y, |
| 122 | int width, int height, int topIndex) { |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 123 | // Scale the video overlay to the height of the thumbnail and put it |
| 124 | // on the left side. |
| 125 | float scale = (float) height / mVideoOverlay.getHeight(); |
| 126 | int w = Math.round(scale * mVideoOverlay.getWidth()); |
| 127 | int h = Math.round(scale * mVideoOverlay.getHeight()); |
| 128 | mVideoOverlay.draw(canvas, x, y, w, h); |
| 129 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 130 | if (topIndex == 0) { |
| 131 | int side = Math.min(width, height) / 6; |
| 132 | mVideoPlayIcon.draw(canvas, -side / 2, -side / 2, side, side); |
| 133 | } |
| 134 | } |
| 135 | |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 136 | protected void drawPanoramaBorder(GLCanvas canvas, int x, int y, |
| 137 | int width, int height) { |
| 138 | float scale = (float) width / mPanoramaBorder.getWidth(); |
| 139 | int w = Math.round(scale * mPanoramaBorder.getWidth()); |
| 140 | int h = Math.round(scale * mPanoramaBorder.getHeight()); |
| 141 | // draw at the top |
| 142 | mPanoramaBorder.draw(canvas, x, y, w, h); |
| 143 | // draw at the bottom |
| 144 | mPanoramaBorder.draw(canvas, x, y + width - h, w, h); |
| 145 | } |
| 146 | |
| 147 | protected void drawLabelBackground(GLCanvas canvas, int width, int height, |
| 148 | int drawLabelBackground) { |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 149 | int x = -width / 2; |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 150 | int y = (height + 1) / 2 - drawLabelBackground; |
Chih-Chung Chang | e3312ff | 2011-09-22 14:55:32 +0800 | [diff] [blame^] | 151 | drawFrame(canvas, mDarkStrip, x, y, width, drawLabelBackground); |
Chih-Chung Chang | 70a73a7 | 2011-09-19 11:09:39 +0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | protected void drawPressedFrame(GLCanvas canvas, int x, int y, int width, |
| 155 | int height) { |
| 156 | drawFrame(canvas, mFramePressed, x, y, width, height); |
Chih-Chung Chang | 07069de | 2011-09-14 20:50:28 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Chih-Chung Chang | e3312ff | 2011-09-22 14:55:32 +0800 | [diff] [blame^] | 159 | protected void drawSelectedFrame(GLCanvas canvas, int x, int y, int width, |
| 160 | int height) { |
| 161 | drawFrame(canvas, mFrameSelected, x, y, width, height); |
| 162 | } |
| 163 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 164 | @Override |
| 165 | public void drawFocus(GLCanvas canvas, int width, int height) { |
| 166 | } |
| 167 | } |