1/*******************************************************************************
2 * Copyright 2011 See AUTHORS file.
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.badlogic.gdx.maps.tiled;
18
19import com.badlogic.gdx.maps.MapLayer;
20
21/** @brief Layer for a TiledMap */
22public class TiledMapTileLayer extends MapLayer {
23
24	private int width;
25	private int height;
26
27	private float tileWidth;
28	private float tileHeight;
29
30	private Cell[][] cells;
31
32	/** @return layer's width in tiles */
33	public int getWidth () {
34		return width;
35	}
36
37	/** @return layer's height in tiles */
38	public int getHeight () {
39		return height;
40	}
41
42	/** @return tiles' width in pixels */
43	public float getTileWidth () {
44		return tileWidth;
45	}
46
47	/** @return tiles' height in pixels */
48	public float getTileHeight () {
49		return tileHeight;
50	}
51
52	/** Creates TiledMap layer
53	 *
54	 * @param width layer width in tiles
55	 * @param height layer height in tiles
56	 * @param tileWidth tile width in pixels
57	 * @param tileHeight tile height in pixels */
58	public TiledMapTileLayer (int width, int height, int tileWidth, int tileHeight) {
59		super();
60		this.width = width;
61		this.height = height;
62		this.tileWidth = tileWidth;
63		this.tileHeight = tileHeight;
64		this.cells = new Cell[width][height];
65	}
66
67	/** @param x X coordinate
68	 * @param y Y coordinate
69	 * @return {@link Cell} at (x, y) */
70	public Cell getCell (int x, int y) {
71		if (x < 0 || x >= width) return null;
72		if (y < 0 || y >= height) return null;
73		return cells[x][y];
74	}
75
76	/** Sets the {@link Cell} at the given coordinates.
77	 *
78	 * @param x X coordinate
79	 * @param y Y coordinate
80	 * @param cell the {@link Cell} to set at the given coordinates. */
81	public void setCell (int x, int y, Cell cell) {
82		if (x < 0 || x >= width) return;
83		if (y < 0 || y >= height) return;
84		cells[x][y] = cell;
85	}
86
87	/** @brief represents a cell in a TiledLayer: TiledMapTile, flip and rotation properties. */
88	public static class Cell {
89
90		private TiledMapTile tile;
91
92		private boolean flipHorizontally;
93
94		private boolean flipVertically;
95
96		private int rotation;
97
98		/** @return The tile currently assigned to this cell. */
99		public TiledMapTile getTile () {
100			return tile;
101		}
102
103		/** Sets the tile to be used for this cell.
104		 *
105		 * @param tile the {@link TiledMapTile} to use for this cell.
106		 * @return this, for method chaining */
107		public Cell setTile (TiledMapTile tile) {
108			this.tile = tile;
109			return this;
110		}
111
112		/** @return Whether the tile should be flipped horizontally. */
113		public boolean getFlipHorizontally () {
114			return flipHorizontally;
115		}
116
117		/** Sets whether to flip the tile horizontally.
118		 *
119		 * @param flipHorizontally whether or not to flip the tile horizontally.
120		 * @return this, for method chaining */
121		public Cell setFlipHorizontally (boolean flipHorizontally) {
122			this.flipHorizontally = flipHorizontally;
123			return this;
124		}
125
126		/** @return Whether the tile should be flipped vertically. */
127		public boolean getFlipVertically () {
128			return flipVertically;
129		}
130
131		/** Sets whether to flip the tile vertically.
132		 *
133		 * @param flipVertically whether or not this tile should be flipped vertically.
134		 * @return this, for method chaining */
135		public Cell setFlipVertically (boolean flipVertically) {
136			this.flipVertically = flipVertically;
137			return this;
138		}
139
140		/** @return The rotation of this cell, in degrees. */
141		public int getRotation () {
142			return rotation;
143		}
144
145		/** Sets the rotation of this cell, in degrees.
146		 *
147		 * @param rotation the rotation in degrees.
148		 * @return this, for method chaining */
149		public Cell setRotation (int rotation) {
150			this.rotation = rotation;
151			return this;
152		}
153
154		public static final int ROTATE_0 = 0;
155		public static final int ROTATE_90 = 1;
156		public static final int ROTATE_180 = 2;
157		public static final int ROTATE_270 = 3;
158	}
159}
160