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
17package com.android.gallery3d.ui;
18
19import com.android.gallery3d.R;
20import com.android.gallery3d.data.Path;
21
22import android.content.Context;
23import android.graphics.Color;
24import android.text.Layout;
25
26public class GridDrawer extends IconDrawer {
27    private Texture mImportLabel;
28    private int mGridWidth;
29    private final SelectionManager mSelectionManager;
30    private final Context mContext;
31    private final int IMPORT_FONT_SIZE = 14;
32    private final int IMPORT_FONT_COLOR = Color.WHITE;
33    private final int IMPORT_LABEL_MARGIN = 10;
34    private boolean mSelectionMode;
35
36    public GridDrawer(Context context, SelectionManager selectionManager) {
37        super(context);
38        mContext = context;
39        mSelectionManager = selectionManager;
40    }
41
42    @Override
43    public void prepareDrawing() {
44        mSelectionMode = mSelectionManager.inSelectionMode();
45    }
46
47    @Override
48    public void draw(GLCanvas canvas, Texture content, int width,
49            int height, int rotation, Path path,
50            int dataSourceType, int mediaType, boolean isPanorama,
51            int labelBackgroundHeight, boolean wantCache, boolean isCaching) {
52
53        int x = -width / 2;
54        int y = -height / 2;
55
56        drawWithRotation(canvas, content, x, y, width, height, rotation);
57
58        if (((rotation / 90) & 0x01) == 1) {
59            int temp = width;
60            width = height;
61            height = temp;
62            x = -width / 2;
63            y = -height / 2;
64        }
65
66        drawMediaTypeOverlay(canvas, mediaType, isPanorama, x, y, width, height);
67        drawLabelBackground(canvas, width, height, labelBackgroundHeight);
68        drawIcon(canvas, width, height, dataSourceType);
69        if (dataSourceType == DATASOURCE_TYPE_MTP) {
70            drawImportLabel(canvas, width, height);
71        }
72
73        if (mSelectionManager.isPressedPath(path)) {
74            drawPressedFrame(canvas, x, y, width, height);
75        } else if (mSelectionMode && mSelectionManager.isItemSelected(path)) {
76            drawSelectedFrame(canvas, x, y, width, height);
77        }
78    }
79
80    // Draws the "click to import" label at the center of the frame
81    private void drawImportLabel(GLCanvas canvas, int width, int height) {
82        if (mImportLabel == null || mGridWidth != width) {
83            mGridWidth = width;
84            mImportLabel = MultiLineTexture.newInstance(
85                    mContext.getString(R.string.click_import),
86                    width - 2 * IMPORT_LABEL_MARGIN,
87                    IMPORT_FONT_SIZE, IMPORT_FONT_COLOR,
88                    Layout.Alignment.ALIGN_CENTER);
89        }
90        int w = mImportLabel.getWidth();
91        int h = mImportLabel.getHeight();
92        mImportLabel.draw(canvas, -w / 2, -h / 2);
93    }
94
95    @Override
96    public void drawFocus(GLCanvas canvas, int width, int height) {
97    }
98}
99