initial commit, lordsawar source, slightly modified
[lordsawar] / src / tilesetlist.h
1 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
2 //
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.
7 //
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.
12 //
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 
16 //  02110-1301, USA.
17
18 #ifndef TILESETLIST_H
19 #define TILESETLIST_H
20
21 #include <gtkmm.h>
22 #include <string>
23 #include <map>
24 #include <vector>
25 #include <sigc++/trackable.h>
26
27 #include "xmlhelper.h"
28 #include "Tile.h"
29 #include "tileset.h"
30 #include "setlist.h"
31
32
33 //! A list of all Tileset objects available to the game.
34 /**
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.
37  *
38  * Tileset objects are usually referenced by the name of the subdirectory
39  * in which they reside on disk (inside the tilesets/ directory).
40  */
41 class Tilesetlist : public std::list<Tileset*>, public sigc::trackable, public SetList
42 {
43     public:
44
45         // Methods that operate on class data but do not modify the class.
46
47         //! Returns the names of all tilesets available to the game.
48         std::list<std::string> getNames() const;
49
50         //! Returns the names of tilesets that have the given tile size.
51         std::list<std::string> getNames(guint32 tilesize) const;
52
53         //! Returns the different tilesizes present in the tilesetlist.
54         void getSizes(std::list<guint32> &sizes) const;
55
56         //! Return the name of the subdirectory for a given tileset.
57         /** 
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.
60          *
61          * @return The name of the directory that holds the tileset.  See 
62          *         Tileset::d_dir for more information about the nature of 
63          *         the return value.
64          */
65         std::string getTilesetDir(std::string name, guint32 tilesize) const;
66
67         //! Return the Tileset object by the name of the subdir.
68         /**
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.
72          */
73         Tileset *getTileset(std::string dir) const;
74
75         //! Return the Tileset object by the id.
76         /**
77          * @param id   A unique numeric identifier that identifies the tileset
78          *             among all tilesets in the tilesetlist.
79          */
80         Tileset *getTileset(guint32 id) const;
81
82         // Methods that operate on the class data and modify the class.
83
84         //! Add a tileset to the list.  Use this instead of push_back.
85         void add(Tileset *tileset);
86
87         //! Destroy all of the tileset images in this list.
88         void uninstantiateImages();
89
90         //! Load the images for all tilesets in this list.
91         void instantiateImages();
92
93         //! Add the given tileset to the list, and copy files into place.
94         /**
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.
98          *
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.
102          */
103         bool addToPersonalCollection(Tileset *tileset, std::string &new_subdir, guint32 &new_id);
104
105         // Static Methods
106
107         //! Return the singleton instance of this class.
108         static Tilesetlist* getInstance();
109
110         //! Explicitly delete the singleton instance of this class.
111         static void deleteInstance();
112
113         //! Return an unused tileset number.
114         static int getNextAvailableId(int after = 0);
115
116     private:
117         //! Default constructor.  Loads all tilesets it can find.
118         /**
119          * The tilesets/ directory is scanned for Tileset directories.
120          */
121         Tilesetlist();
122         
123         //! Destructor.
124         ~Tilesetlist();
125
126         //! Loads a specific Tileset.
127         /**
128          * Load the Tileset from an tileset configuration file and add it to 
129          * this list of Tileset objects.
130          *
131          * @param name  The name of the subdirectory that the Tileset resides 
132          *              in.
133          *
134          * @return the Tileset.  NULL otherwise.
135          */
136         Tileset* loadTileset (std::string filename);
137
138         //! Load the given tilesets into the list.
139         void loadTilesets(std::list<std::string> tilesets);
140         
141         // DATA
142
143         typedef std::map<std::string, std::string> DirMap;
144         typedef std::map<std::string, Tileset*> TilesetMap;
145         typedef std::map<guint32, Tileset*> TilesetIdMap;
146
147         //! A map that provides a subdirectory when supplying a Tileset name.
148         DirMap d_dirs;
149
150         //! A map that provides a Tileset when supplying a subdirectory name.
151         TilesetMap d_tilesets;
152
153         //! A map that provides a Tileset when supplying a tileset id.
154         TilesetIdMap d_tilesetids;
155
156         //! A static pointer for the singleton instance.
157         static Tilesetlist* s_instance;
158 };
159
160 #endif // TILESETLIST_H
161