1 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <sigc++/trackable.h>
27 #include "xmlhelper.h"
33 //! A list of all Tileset objects available to the game.
35 * This class contains a list of all Tileset objects available to the game.
36 * Since several classes access this class, it is implemented as a singleton.
38 * Tileset objects are usually referenced by the name of the subdirectory
39 * in which they reside on disk (inside the tilesets/ directory).
41 class Tilesetlist : public std::list<Tileset*>, public sigc::trackable, public SetList
45 // Methods that operate on class data but do not modify the class.
47 //! Returns the names of all tilesets available to the game.
48 std::list<std::string> getNames() const;
50 //! Returns the names of tilesets that have the given tile size.
51 std::list<std::string> getNames(guint32 tilesize) const;
53 //! Returns the different tilesizes present in the tilesetlist.
54 void getSizes(std::list<guint32> &sizes) const;
56 //! Return the name of the subdirectory for a given tileset.
58 * @param tileset The name of the tileset to get the subdir of.
59 * @param tilesize The size of the tileset to get the subdir of.
61 * @return The name of the directory that holds the tileset. See
62 * Tileset::d_dir for more information about the nature of
65 std::string getTilesetDir(std::string name, guint32 tilesize) const;
67 //! Return the Tileset object by the name of the subdir.
69 * @param dir The directory where the Tileset resides on disk.
70 * This value does not contain any slashes, and is
71 * presumed to be found inside the tilesets/ directory.
73 Tileset *getTileset(std::string dir) const;
75 //! Return the Tileset object by the id.
77 * @param id A unique numeric identifier that identifies the tileset
78 * among all tilesets in the tilesetlist.
80 Tileset *getTileset(guint32 id) const;
82 // Methods that operate on the class data and modify the class.
84 //! Add a tileset to the list. Use this instead of push_back.
85 void add(Tileset *tileset);
87 //! Destroy all of the tileset images in this list.
88 void uninstantiateImages();
90 //! Load the images for all tilesets in this list.
91 void instantiateImages();
93 //! Add the given tileset to the list, and copy files into place.
95 * This method tries hard to add the tileset to this list. The subdir
96 * name could be changed, or the id might also be changed so that it
97 * doesn't conflict with any other tilesets in the list.
99 * @return Returns true if it was added successfully, and the
100 * new_subdir and new_id parameters updated to reflect the
101 * changed subdir and id.
103 bool addToPersonalCollection(Tileset *tileset, std::string &new_subdir, guint32 &new_id);
107 //! Return the singleton instance of this class.
108 static Tilesetlist* getInstance();
110 //! Explicitly delete the singleton instance of this class.
111 static void deleteInstance();
113 //! Return an unused tileset number.
114 static int getNextAvailableId(int after = 0);
117 //! Default constructor. Loads all tilesets it can find.
119 * The tilesets/ directory is scanned for Tileset directories.
126 //! Loads a specific Tileset.
128 * Load the Tileset from an tileset configuration file and add it to
129 * this list of Tileset objects.
131 * @param name The name of the subdirectory that the Tileset resides
134 * @return the Tileset. NULL otherwise.
136 Tileset* loadTileset (std::string filename);
138 //! Load the given tilesets into the list.
139 void loadTilesets(std::list<std::string> tilesets);
143 typedef std::map<std::string, std::string> DirMap;
144 typedef std::map<std::string, Tileset*> TilesetMap;
145 typedef std::map<guint32, Tileset*> TilesetIdMap;
147 //! A map that provides a subdirectory when supplying a Tileset name.
150 //! A map that provides a Tileset when supplying a subdirectory name.
151 TilesetMap d_tilesets;
153 //! A map that provides a Tileset when supplying a tileset id.
154 TilesetIdMap d_tilesetids;
156 //! A static pointer for the singleton instance.
157 static Tilesetlist* s_instance;
160 #endif // TILESETLIST_H