initial commit, lordsawar source, slightly modified
[lordsawar] / src / shieldsetlist.h
1 //  Copyright (C) 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 SHIELDSETLIST_H
19 #define SHIELDSETLIST_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 "shield.h"
29 #include "shieldset.h"
30
31
32 //! A list of Shieldset objects available to the game.
33 /** 
34  * This class holds all of the shield themes that are located in shield/.
35  * It is implemented as a singleton.
36  *
37  * Other classes use it to lookup Shield and Shieldset objects.
38  */
39 class Shieldsetlist : public std::list<Shieldset*>, public sigc::trackable
40 {
41     public:
42
43         // Methods that operate on the class data but do not modify the class.
44
45         //! Returns the names of all Shieldset objects available to the game.
46         std::list<std::string> getNames() const;
47
48         //! Return the directory of a specific Shieldset by name.
49         /**
50          * Scan all of the Shieldset objects in the list for one with the 
51          * given name.
52          *
53          * @param name   The name of the shieldset to search for.
54          *
55          * @return The name of the directory that holds the shieldset, or an
56          *         empty string if a shieldset by that name could not be found.
57          *         This value relates to Shieldset::d_dir.
58          */
59         std::string getShieldsetDir(std::string name) const;
60
61         //! Return a particular Shield object from a given shieldset.
62         /**
63          * Scan the given shieldset for a Shield of the given type and colour.
64          *
65          * @param shieldset  The id the shieldset.  This value relates to the 
66          *                   Shieldset::d_id member.
67          * @param type       The size of the shield to search for.  This value
68          *                   relates to the Shield::ShieldType enumeration.
69          * @param colour     The player of the shield.  This value relates to
70          *                   the Shield::ShieldColour enumeration.
71          *
72          * @return A pointer to a Shield in the given shieldset that matches
73          *         the given parameters.  If no shield could be found, NULL
74          *         is returned.
75          */
76         ShieldStyle *getShield(guint32 shieldset, guint32 type, guint32 colour) const;
77
78         //! Return the Shieldset object that is in the given directory.
79         Shieldset *getShieldset(std::string dir) const;
80
81         Gdk::Color getColor(guint32 shieldset, guint32 owner) const;
82
83         //! Return the Shieldset object by the id.
84         /**
85          * @param id   A unique numeric identifier that identifies the 
86          *             shieldset among all shieldsets in the shieldsetlist.
87          */
88         Shieldset *getShieldset(guint32 id) const;
89
90
91         // Methods that operate on the class data and modify the class.
92
93         //! Add a shieldset to the list.  Use this instead of push_back.
94         void add(Shieldset *shieldset);
95
96         //! Destroy all of the images associated with shieldsets in this list.
97         void uninstantiateImages();
98
99         //! Load all of the images associated with all of the shieldsets.
100         void instantiateImages();
101
102         //! Add the given shieldset to the list, and copy files into place.
103         /**
104          * This method tries hard to add the shieldset to this list.  The 
105          * subdir name could be changed, or the id might also be changed so 
106          * that it doesn't conflict with any other shieldsets in the list.
107          *
108          * @return Returns true if it was added successfully, and the
109          *         new_subdir and new_id parameters updated to reflect the
110          *         changed subdir and id.
111          */
112         bool addToPersonalCollection(Shieldset *shieldset, std::string &new_subdir, guint32 &new_id);
113
114
115         // Static Methods
116
117         //! Return the singleton instance of this class.
118         static Shieldsetlist* getInstance();
119
120         //! Explicitly delete the singleton instance of this class.
121         static void deleteInstance();
122
123         //! Return a unique id for a shieldset.
124         static int getNextAvailableId(int after);
125
126     private:
127         //! Default Constructor.
128         /**
129          * Loads all shieldsets it can find in the shield/ directory, and
130          * makes a new Shieldsetlist object from what it finds.
131          */
132         Shieldsetlist();
133         
134         //! Destructor.
135         ~Shieldsetlist();
136
137         //! Loads a specific shieldset.
138         Shieldset *loadShieldset(std::string name);
139
140         //! Loads a bunch of shieldsets and puts them in this list.
141         void loadShieldsets(std::list<std::string> shieldsets);
142         
143         // DATA
144
145         typedef std::map<std::string, std::string> DirMap;
146         typedef std::map<std::string, Shieldset*> ShieldsetMap;
147         typedef std::map<guint32, Shieldset*> ShieldsetIdMap;
148
149         //! A map that provides a subdirectory when supplying a Shieldset name.
150         DirMap d_dirs;
151
152         //! A map that provides a Shieldset when supplying a subdirectory name.
153         ShieldsetMap d_shieldsets;
154
155         //! A map that provides a Shieldset when supplying a shieldset id.
156         ShieldsetIdMap d_shieldsetids;
157
158         //! A static pointer for the singleton instance.
159         static Shieldsetlist* s_instance;
160 };
161
162 #endif // SHIELDSETLIST_H
163