1/*
2 * Copyright (C) 2007 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.example.android.snake;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Bitmap;
22import android.graphics.Canvas;
23import android.graphics.Paint;
24import android.graphics.drawable.Drawable;
25import android.util.AttributeSet;
26import android.view.View;
27
28
29/**
30 * TileView: a View-variant designed for handling arrays of "icons" or other
31 * drawables.
32 *
33 */
34public class TileView extends View {
35
36    /**
37     * Parameters controlling the size of the tiles and their range within view.
38     * Width/Height are in pixels, and Drawables will be scaled to fit to these
39     * dimensions. X/Y Tile Counts are the number of tiles that will be drawn.
40     */
41
42    protected static int mTileSize;
43
44    protected static int mXTileCount;
45    protected static int mYTileCount;
46
47    private static int mXOffset;
48    private static int mYOffset;
49
50
51    /**
52     * A hash that maps integer handles specified by the subclasser to the
53     * drawable that will be used for that reference
54     */
55    private Bitmap[] mTileArray;
56
57    /**
58     * A two-dimensional array of integers in which the number represents the
59     * index of the tile that should be drawn at that locations
60     */
61    private int[][] mTileGrid;
62
63    private final Paint mPaint = new Paint();
64
65    public TileView(Context context, AttributeSet attrs, int defStyle) {
66        super(context, attrs, defStyle);
67
68        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
69
70        mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
71
72        a.recycle();
73    }
74
75    public TileView(Context context, AttributeSet attrs) {
76        super(context, attrs);
77
78        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);
79
80        mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
81
82        a.recycle();
83    }
84
85
86
87    /**
88     * Rests the internal array of Bitmaps used for drawing tiles, and
89     * sets the maximum index of tiles to be inserted
90     *
91     * @param tilecount
92     */
93
94    public void resetTiles(int tilecount) {
95    	mTileArray = new Bitmap[tilecount];
96    }
97
98
99    @Override
100    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
101        mXTileCount = (int) Math.floor(w / mTileSize);
102        mYTileCount = (int) Math.floor(h / mTileSize);
103
104        mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
105        mYOffset = ((h - (mTileSize * mYTileCount)) / 2);
106
107        mTileGrid = new int[mXTileCount][mYTileCount];
108        clearTiles();
109    }
110
111    /**
112     * Function to set the specified Drawable as the tile for a particular
113     * integer key.
114     *
115     * @param key
116     * @param tile
117     */
118    public void loadTile(int key, Drawable tile) {
119        Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
120        Canvas canvas = new Canvas(bitmap);
121        tile.setBounds(0, 0, mTileSize, mTileSize);
122        tile.draw(canvas);
123
124        mTileArray[key] = bitmap;
125    }
126
127    /**
128     * Resets all tiles to 0 (empty)
129     *
130     */
131    public void clearTiles() {
132        for (int x = 0; x < mXTileCount; x++) {
133            for (int y = 0; y < mYTileCount; y++) {
134                setTile(0, x, y);
135            }
136        }
137    }
138
139    /**
140     * Used to indicate that a particular tile (set with loadTile and referenced
141     * by an integer) should be drawn at the given x/y coordinates during the
142     * next invalidate/draw cycle.
143     *
144     * @param tileindex
145     * @param x
146     * @param y
147     */
148    public void setTile(int tileindex, int x, int y) {
149        mTileGrid[x][y] = tileindex;
150    }
151
152
153    @Override
154    public void onDraw(Canvas canvas) {
155        super.onDraw(canvas);
156        for (int x = 0; x < mXTileCount; x += 1) {
157            for (int y = 0; y < mYTileCount; y += 1) {
158                if (mTileGrid[x][y] > 0) {
159                    canvas.drawBitmap(mTileArray[mTileGrid[x][y]],
160                    		mXOffset + x * mTileSize,
161                    		mYOffset + y * mTileSize,
162                    		mPaint);
163                }
164            }
165        }
166
167    }
168
169}
170