00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "types.h"
00023 #include "image.h"
00024 #include "win_types.h"
00025 #include "win_manager.h"
00026
00027
00028
00029 win_manager* win_manager::active = NULL;
00030
00031 hash_map<string, win_theme *> win_manager::theme;
00032
00033 hash_map<string, win_ttf *> win_manager::font;
00034
00035 string win_manager::font_file;
00036
00037
00038 win_manager::win_manager ()
00039 {
00040
00041 prev = active;
00042
00043
00044 active = this;
00045
00046
00047 wnd_focus = NULL;
00048
00049
00050 current = wnd_list.end ();
00051 }
00052
00053 win_manager::~win_manager ()
00054 {
00055 destroy ();
00056
00057
00058 active = prev;
00059 }
00060
00061
00062 void win_manager::destroy()
00063 {
00064 list<win_base *>::iterator i;
00065
00066 for (i = wnd_list.begin(); i != wnd_list.end(); i++)
00067 {
00068 (*i)->set_manager (NULL);
00069
00070 }
00071
00072 wnd_list.clear ();
00073 wnd_focus = NULL;
00074 }
00075
00076 void win_manager::init (const string & font)
00077 {
00078 font_file = font;
00079 TTF_Init ();
00080 }
00081
00082
00083 void win_manager::cleanup ()
00084 {
00085
00086 for (hash_map <string, win_theme *>::iterator it = theme.begin ();
00087 it != theme.end (); it++)
00088 delete it->second;
00089 theme.clear ();
00090
00091
00092 for (hash_map <string, win_ttf *>::iterator ifo = font.begin ();
00093 ifo != font.end (); ifo++)
00094 delete ifo->second;
00095 font.clear ();
00096
00097 TTF_Quit ();
00098 }
00099
00100
00101 void win_manager::add (win_base *tmp)
00102 {
00103 wnd_list.push_back (tmp);
00104 tmp->set_manager (this);
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 void win_manager::remove (win_base *tmp)
00120 {
00121
00122 if (tmp->is_focus ())
00123 {
00124 tmp->set_focus (false);
00125 wnd_focus = NULL;
00126 }
00127
00128
00129
00130
00131 if (tmp == *current)
00132 {
00133
00134 current++;
00135 }
00136
00137
00138 wnd_list.remove (tmp);
00139 tmp->set_manager (NULL);
00140
00141
00142 if (!wnd_focus) set_focus (wnd_list.back ());
00143 }
00144
00145
00146 void win_manager::draw ()
00147 {
00148
00149 if (prev != NULL) prev->draw ();
00150
00151
00152 for (current = wnd_list.begin (); current != wnd_list.end(); current++)
00153 (*current)->draw ();
00154 }
00155
00156
00157 void win_manager::input_update ()
00158 {
00159
00160 if (wnd_focus) wnd_focus->input_update ();
00161 }
00162
00163
00164 void win_manager::update ()
00165 {
00166 for (current = wnd_list.begin (); current != wnd_list.end ();)
00167
00168 if (!(*current)->update ())
00169 {
00170
00171 win_base *tmp = *current;
00172
00173 remove (tmp);
00174 delete tmp;
00175 }
00176 else current++;
00177 }
00178
00179
00180 void win_manager::set_focus (win_base *tmp)
00181 {
00182
00183 if (!wnd_list.empty ())
00184 {
00185
00186 if (wnd_focus) wnd_focus->set_focus (false);
00187
00188
00189 wnd_focus = tmp;
00190 wnd_focus->set_focus (true);
00191 }
00192 }
00193
00194
00195 void win_manager::add_theme (string name)
00196 {
00197 theme[name] = new win_theme ((char *) name.c_str ());
00198 }
00199
00200
00201 bool win_manager::remove_theme (string name)
00202 {
00203 hash_map <string, win_theme *>::iterator it = theme.find (name);
00204 if (it == theme.end ()) return false;
00205
00206 delete it->second;
00207 theme.erase (it);
00208 return true;
00209 }
00210
00211
00212 win_theme * win_manager::get_theme (string name)
00213 {
00214 hash_map <string, win_theme *>::iterator it = theme.find (name);
00215
00216
00217 if (it == theme.end ())
00218 {
00219 add_theme (name);
00220 return get_theme (name);
00221 }
00222 else return it->second;
00223 }
00224
00225
00226 void win_manager::add_font (string name)
00227 {
00228 font[name] = new win_ttf ((char *) name.c_str (), font_file);
00229 }
00230
00231
00232 bool win_manager::remove_font (string name)
00233 {
00234 hash_map <string, win_ttf *>::iterator it = font.find (name);
00235 if (it == font.end ()) return false;
00236
00237 delete it->second;
00238 font.erase (it);
00239 return true;
00240 }
00241
00242
00243 win_font * win_manager::get_font (string name)
00244 {
00245 hash_map <string, win_ttf *>::iterator it = font.find (name);
00246
00247
00248 if (it == font.end ())
00249 {
00250 add_font (name);
00251 return get_font (name);
00252 }
00253 else return it->second;
00254 }