• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.badlogic.gdx.maps.tiled;
18 
19 import java.util.Iterator;
20 
21 import com.badlogic.gdx.utils.Array;
22 
23 /** @brief Collection of {@link TiledMapTileSet} */
24 public class TiledMapTileSets implements Iterable<TiledMapTileSet> {
25 
26 	private Array<TiledMapTileSet> tilesets;
27 
28 	/** Creates an empty collection of tilesets. */
TiledMapTileSets()29 	public TiledMapTileSets () {
30 		tilesets = new Array<TiledMapTileSet>();
31 	}
32 
33 	/** @param index index to get the desired {@link TiledMapTileSet} at.
34 	 * @return tileset at index */
getTileSet(int index)35 	public TiledMapTileSet getTileSet (int index) {
36 		return tilesets.get(index);
37 	}
38 
39 	/** @param name Name of the {@link TiledMapTileSet} to retrieve.
40 	 * @return tileset with matching name, null if it doesn't exist */
getTileSet(String name)41 	public TiledMapTileSet getTileSet (String name) {
42 		for (TiledMapTileSet tileset : tilesets) {
43 			if (name.equals(tileset.getName())) {
44 				return tileset;
45 			}
46 		}
47 		return null;
48 	}
49 
50 	/** @param tileset set to be added to the collection */
addTileSet(TiledMapTileSet tileset)51 	public void addTileSet (TiledMapTileSet tileset) {
52 		tilesets.add(tileset);
53 	}
54 
55 	/** Removes tileset at index
56 	 *
57 	 * @param index index at which to remove a tileset. */
removeTileSet(int index)58 	public void removeTileSet (int index) {
59 		tilesets.removeIndex(index);
60 	}
61 
62 	/** @param tileset set to be removed */
removeTileSet(TiledMapTileSet tileset)63 	public void removeTileSet (TiledMapTileSet tileset) {
64 		tilesets.removeValue(tileset, true);
65 	}
66 
67 	/** @param id id of the {@link TiledMapTile} to get.
68 	 * @return tile with matching id, null if it doesn't exist */
getTile(int id)69 	public TiledMapTile getTile (int id) {
70 		// The purpose of backward iteration here is to maintain backwards compatibility
71 		// with maps created with earlier versions of a shared tileset.  The assumption
72 		// is that the tilesets are in order of ascending firstgid, and by backward
73 		// iterating precedence for conflicts is given to later tilesets in the list,
74 		// which are likely to be the earlier version of any given gid.
75 		// See TiledMapModifiedExternalTilesetTest for example of this issue.
76 		for (int i = tilesets.size-1; i >= 0; i--) {
77 			TiledMapTileSet tileset = tilesets.get(i);
78 			TiledMapTile tile = tileset.getTile(id);
79 			if (tile != null) {
80 				return tile;
81 			}
82 		}
83 		return null;
84 	}
85 
86 	/** @return iterator to tilesets */
87 	@Override
iterator()88 	public Iterator<TiledMapTileSet> iterator () {
89 		return tilesets.iterator();
90 	}
91 
92 }
93