blob: 6ae0157cb17edaa3ce27ada377be5f244a9031b3 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
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 */
16package com.android.gallery3d.ui;
17
18import com.android.gallery3d.R;
19import com.android.gallery3d.data.MediaObject;
20
21import android.content.Context;
22
23public abstract class IconDrawer extends SelectionDrawer {
Chih-Chung Chang07069de2011-09-14 20:50:28 +080024 private static final String TAG = "IconDrawer";
Chih-Chung Chang70a73a72011-09-19 11:09:39 +080025 private static final int LABEL_BACKGROUND_COLOR = 0x99000000; // 60% black
Chih-Chung Chang07069de2011-09-14 20:50:28 +080026
Owen Lina2fba682011-08-17 22:07:43 +080027 private final ResourceTexture mLocalSetIcon;
28 private final ResourceTexture mCameraIcon;
29 private final ResourceTexture mPicasaIcon;
30 private final ResourceTexture mMtpIcon;
Chih-Chung Chang70a73a72011-09-19 11:09:39 +080031 private final NinePatchTexture mFramePressed;
Chih-Chung Change3312ff2011-09-22 14:55:32 +080032 private final NinePatchTexture mFrameSelected;
33 private final NinePatchTexture mDarkStrip;
Chih-Chung Chang70a73a72011-09-19 11:09:39 +080034 private final ResourceTexture mPanoramaBorder;
Owen Lina2fba682011-08-17 22:07:43 +080035 private final Texture mVideoOverlay;
36 private final Texture mVideoPlayIcon;
Chih-Chung Chang07069de2011-09-14 20:50:28 +080037 private final int mIconSize;
Owen Lina2fba682011-08-17 22:07:43 +080038
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 Chang07069de2011-09-14 20:50:28 +080047 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 Chang70a73a72011-09-19 11:09:39 +080050 mMtpIcon = new ResourceTexture(context, R.drawable.frame_overlay_gallery_ptp);
Chih-Chung Chang07069de2011-09-14 20:50:28 +080051 mVideoOverlay = new ResourceTexture(context, R.drawable.ic_video_thumb);
52 mVideoPlayIcon = new ResourceTexture(context, R.drawable.ic_gallery_play);
Chih-Chung Chang70a73a72011-09-19 11:09:39 +080053 mPanoramaBorder = new ResourceTexture(context, R.drawable.ic_pan_thumb);
54 mFramePressed = new NinePatchTexture(context, R.drawable.grid_pressed);
Chih-Chung Change3312ff2011-09-22 14:55:32 +080055 mFrameSelected = new NinePatchTexture(context, R.drawable.grid_selected);
56 mDarkStrip = new NinePatchTexture(context, R.drawable.dark_strip);
Chih-Chung Chang07069de2011-09-14 20:50:28 +080057 mIconSize = context.getResources().getDimensionPixelSize(
58 R.dimen.albumset_icon_size);
Owen Lina2fba682011-08-17 22:07:43 +080059 }
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 Chang07069de2011-09-14 20:50:28 +0800102 float scale = (float) mIconSize / icon.getWidth();
103 id.width = Math.round(scale * icon.getWidth());
104 id.height = Math.round(scale * icon.getHeight());
Owen Lina2fba682011-08-17 22:07:43 +0800105 id.x = -width / 2;
Chih-Chung Chang70a73a72011-09-19 11:09:39 +0800106 id.y = (height + 1) / 2 - id.height;
Owen Lina2fba682011-08-17 22:07:43 +0800107 return id;
108 }
109
Chih-Chung Chang70a73a72011-09-19 11:09:39 +0800110 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 Chang07069de2011-09-14 20:50:28 +0800120
Chih-Chung Chang70a73a72011-09-19 11:09:39 +0800121 protected void drawVideoOverlay(GLCanvas canvas, int x, int y,
122 int width, int height, int topIndex) {
Chih-Chung Chang07069de2011-09-14 20:50:28 +0800123 // 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 Lina2fba682011-08-17 22:07:43 +0800130 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 Chang70a73a72011-09-19 11:09:39 +0800136 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 Chang07069de2011-09-14 20:50:28 +0800149 int x = -width / 2;
Chih-Chung Chang70a73a72011-09-19 11:09:39 +0800150 int y = (height + 1) / 2 - drawLabelBackground;
Chih-Chung Change3312ff2011-09-22 14:55:32 +0800151 drawFrame(canvas, mDarkStrip, x, y, width, drawLabelBackground);
Chih-Chung Chang70a73a72011-09-19 11:09:39 +0800152 }
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 Chang07069de2011-09-14 20:50:28 +0800157 }
158
Chih-Chung Change3312ff2011-09-22 14:55:32 +0800159 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 Lina2fba682011-08-17 22:07:43 +0800164 @Override
165 public void drawFocus(GLCanvas canvas, int width, int height) {
166 }
167}