1/*
2 * Copyright (C) 2012 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
17package com.android.gallery3d.ui;
18
19import android.content.Context;
20import android.graphics.Rect;
21
22import com.android.gallery3d.R;
23import com.android.gallery3d.glrenderer.FadeOutTexture;
24import com.android.gallery3d.glrenderer.GLCanvas;
25import com.android.gallery3d.glrenderer.NinePatchTexture;
26import com.android.gallery3d.glrenderer.ResourceTexture;
27import com.android.gallery3d.glrenderer.Texture;
28
29public abstract class AbstractSlotRenderer implements SlotView.SlotRenderer {
30
31    private final ResourceTexture mVideoOverlay;
32    private final ResourceTexture mVideoPlayIcon;
33    private final ResourceTexture mPanoramaIcon;
34    private final NinePatchTexture mFramePressed;
35    private final NinePatchTexture mFrameSelected;
36    private FadeOutTexture mFramePressedUp;
37
38    protected AbstractSlotRenderer(Context context) {
39        mVideoOverlay = new ResourceTexture(context, R.drawable.ic_video_thumb);
40        mVideoPlayIcon = new ResourceTexture(context, R.drawable.ic_gallery_play);
41        mPanoramaIcon = new ResourceTexture(context, R.drawable.ic_360pano_holo_light);
42        mFramePressed = new NinePatchTexture(context, R.drawable.grid_pressed);
43        mFrameSelected = new NinePatchTexture(context, R.drawable.grid_selected);
44    }
45
46    protected void drawContent(GLCanvas canvas,
47            Texture content, int width, int height, int rotation) {
48        canvas.save(GLCanvas.SAVE_FLAG_MATRIX);
49
50        // The content is always rendered in to the largest square that fits
51        // inside the slot, aligned to the top of the slot.
52        width = height = Math.min(width, height);
53        if (rotation != 0) {
54            canvas.translate(width / 2, height / 2);
55            canvas.rotate(rotation, 0, 0, 1);
56            canvas.translate(-width / 2, -height / 2);
57        }
58
59        // Fit the content into the box
60        float scale = Math.min(
61                (float) width / content.getWidth(),
62                (float) height / content.getHeight());
63        canvas.scale(scale, scale, 1);
64        content.draw(canvas, 0, 0);
65
66        canvas.restore();
67    }
68
69    protected void drawVideoOverlay(GLCanvas canvas, int width, int height) {
70        // Scale the video overlay to the height of the thumbnail and put it
71        // on the left side.
72        ResourceTexture v = mVideoOverlay;
73        float scale = (float) height / v.getHeight();
74        int w = Math.round(scale * v.getWidth());
75        int h = Math.round(scale * v.getHeight());
76        v.draw(canvas, 0, 0, w, h);
77
78        int s = Math.min(width, height) / 6;
79        mVideoPlayIcon.draw(canvas, (width - s) / 2, (height - s) / 2, s, s);
80    }
81
82    protected void drawPanoramaIcon(GLCanvas canvas, int width, int height) {
83        int iconSize = Math.min(width, height) / 6;
84        mPanoramaIcon.draw(canvas, (width - iconSize) / 2, (height - iconSize) / 2,
85                iconSize, iconSize);
86    }
87
88    protected boolean isPressedUpFrameFinished() {
89        if (mFramePressedUp != null) {
90            if (mFramePressedUp.isAnimating()) {
91                return false;
92            } else {
93                mFramePressedUp = null;
94            }
95        }
96        return true;
97    }
98
99    protected void drawPressedUpFrame(GLCanvas canvas, int width, int height) {
100        if (mFramePressedUp == null) {
101            mFramePressedUp = new FadeOutTexture(mFramePressed);
102        }
103        drawFrame(canvas, mFramePressed.getPaddings(), mFramePressedUp, 0, 0, width, height);
104    }
105
106    protected void drawPressedFrame(GLCanvas canvas, int width, int height) {
107        drawFrame(canvas, mFramePressed.getPaddings(), mFramePressed, 0, 0, width, height);
108    }
109
110    protected void drawSelectedFrame(GLCanvas canvas, int width, int height) {
111        drawFrame(canvas, mFrameSelected.getPaddings(), mFrameSelected, 0, 0, width, height);
112    }
113
114    protected static void drawFrame(GLCanvas canvas, Rect padding, Texture frame,
115            int x, int y, int width, int height) {
116        frame.draw(canvas, x - padding.left, y - padding.top, width + padding.left + padding.right,
117                 height + padding.top + padding.bottom);
118    }
119}
120