00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef MENU_MANAGER_H_
00015 #define MENU_MANAGER_H_
00016
00017 #include "../Config.h"
00018 #include "SDL.h"
00019 #ifdef HAVE_SDL_TTF
00020 #include "SDL_ttf.h"
00021 #endif
00022
00023 #include <vector>
00024 #include <string>
00025 #include <map>
00026
00030 namespace Menu {
00031 #ifdef HAVE_SDL_TTF
00032
00033 const int DEFAULT_FONT_SIZE=24;
00035 const int TEXT_CENTER_X=200;
00037 const int TITLE_OFFSET_FROM_TOP=50;
00039 const int VERTICAL_SPACING=DEFAULT_FONT_SIZE/2;
00041 const int INPUT_HORIZ_SPACE=DEFAULT_FONT_SIZE*6;
00042
00044 const char DEFAULT_FONT[]="GameData/Fonts/bboron.ttf";
00046 const SDL_Color DEFAULT_SELECTED_COL = {255, 0, 0, 0};
00048 const SDL_Color DEFAULT_DESELECTED_COL = {0, 255, 0, 0};
00050 const SDL_Color DEFAULT_ACTIVE_COL = {255, 255, 255, 0};
00052 const SDL_Color DEFAULT_BG_COL = {0, 0, 0, 0};
00053
00055 enum MenuHandleResult {
00056 MENU_QUIT,
00057 EVENT_HANDLED,
00058 EVENT_PASSED
00059 };
00060
00062 class MenuItem {
00063 public:
00065 virtual MenuHandleResult handleEvent(SDL_Event* e)=0;
00067 virtual void draw(SDL_Surface* surf, TTF_Font* font, int centerX, int centerY)=0;
00068
00072 virtual void select()=0;
00073
00075 virtual void deselect()=0;
00076
00077 virtual ~MenuItem() { }
00078 };
00079
00083 class DefaultMenuItem : public MenuItem {
00084 public:
00086 virtual void select();
00088 virtual void deselect();
00089 virtual ~DefaultMenuItem() { }
00091 void draw(SDL_Surface* surf, TTF_Font* font, int centerX, int centerY);
00092 protected:
00097 DefaultMenuItem(const std::string& name,
00098 const SDL_Color& selected=DEFAULT_SELECTED_COL,
00099 const SDL_Color& unselected=DEFAULT_DESELECTED_COL);
00101 virtual const SDL_Color& getCurrentColor();
00102 private:
00103 std::string name;
00104 SDL_Color selectedColor;
00105 SDL_Color deselectedColor;
00106 bool isSelected;
00107 };
00108
00112 class MenuManager {
00113 public:
00118 MenuManager(const std::string& title, const SDL_Color& titleCol, const SDL_Color& bg=DEFAULT_BG_COL);
00119
00124 bool run(SDL_Surface* screen);
00125
00129 void addMenuItem(MenuItem* item);
00130
00131 ~MenuManager();
00132 private:
00133 std::string title;
00134 int fontSize;
00135 TTF_Font* font;
00136 SDL_Color titleCol;
00137 SDL_Color bgCol;
00138 std::vector<MenuItem*> itemVec;
00139 size_t currentSelection;
00140 };
00141
00142
00143 class BooleanItem : public DefaultMenuItem {
00144 public:
00146 BooleanItem(const std::string& name, bool& b,
00147 const SDL_Color& sel=DEFAULT_SELECTED_COL, const SDL_Color& unsel=DEFAULT_DESELECTED_COL);
00151 MenuHandleResult handleEvent(SDL_Event* e);
00152 void draw(SDL_Surface* surf, TTF_Font* font, int centerX, int centerY);
00153 private:
00154 bool& optionEnabled;
00155 };
00156
00158 class GotoSubMenu : public DefaultMenuItem {
00159 public:
00165 GotoSubMenu(const std::string& title, MenuManager& m, SDL_Surface* screen,
00166 const SDL_Color& selCol=DEFAULT_SELECTED_COL,
00167 const SDL_Color& nonSelCol=DEFAULT_DESELECTED_COL);
00169 MenuHandleResult handleEvent(SDL_Event* e);
00170 private:
00171 MenuManager& menu;
00172 SDL_Surface* screen;
00173 };
00174
00182 class InputGetter : public DefaultMenuItem {
00183 public:
00187 MenuHandleResult handleEvent(SDL_Event* e);
00188 protected:
00192 InputGetter(const std::string& name,
00193 const SDL_Color& selectedCol=DEFAULT_SELECTED_COL,
00194 const SDL_Color& unselectedCol=DEFAULT_DESELECTED_COL,
00195 const SDL_Color& activeCol=DEFAULT_ACTIVE_COL);
00197 const SDL_Color& getCurrentColor();
00199 bool isActive() { return active; }
00200 private:
00201 SDL_Color activeCol;
00202 bool active;
00203 };
00204
00206 class KeyInputter : public InputGetter {
00207 public:
00209 KeyInputter(const std::string& name, SDLKey& key,
00210 const SDL_Color& selected=DEFAULT_SELECTED_COL,
00211 const SDL_Color& unselected=DEFAULT_DESELECTED_COL,
00212 const SDL_Color& active=DEFAULT_ACTIVE_COL);
00216 MenuHandleResult handleEvent(SDL_Event* e);
00218 virtual void draw(SDL_Surface* surf, TTF_Font* font, int centerX, int centerY);
00219 private:
00220 SDLKey& key;
00221 };
00222
00224 class IntInputter : public InputGetter {
00225 public:
00226
00227
00228
00229
00230 IntInputter(const std::string& name, int& i, int min, int max,
00231 const SDL_Color& s=DEFAULT_SELECTED_COL,
00232 const SDL_Color& u=DEFAULT_DESELECTED_COL,
00233 const SDL_Color& a=DEFAULT_ACTIVE_COL);
00238 MenuHandleResult handleEvent(SDL_Event* e);
00239
00241 virtual void draw(SDL_Surface* surf, TTF_Font* font, int centerX, int centerY);
00242 private:
00243 int& storageInt;
00244 int min;
00245 int max;
00246 };
00247 #endif // ifdef HAVE_SDL_TTF
00248
00249 typedef std::map<std::string, SDLKey> StringToKeyMap_t;
00250 typedef std::map<SDLKey, std::string> KeyToStringMap_t;
00251
00256 class KeyMapContainer {
00257 public:
00258 static const StringToKeyMap_t& getStringToKeyMap();
00259 static const KeyToStringMap_t& getKeyToStringMap();
00260 private:
00261 static void init();
00262 static void addPair(const std::string& s, const SDLKey& k );
00263 static bool initialized;
00264 static KeyToStringMap_t keyStringMap;
00265 static StringToKeyMap_t stringKeyMap;
00266 };
00267 };
00268
00269 #endif // ifndef MENU_MANAGER_H_