| 1 | #include <iostream> |
| 2 | |
| 3 | #include "settings.h" |
| 4 | #include "canvas.h" |
| 5 | |
| 6 | #include "menusystem.h" |
| 7 | #include "menu.h" |
| 8 | |
| 9 | using namespace gcn; |
| 10 | using namespace std; |
| 11 | |
| 12 | MenuSystem::MenuSystem(SDLInput *input) |
| 13 | : currentMenu(0) |
| 14 | { |
| 15 | gui = new Gui(); |
| 16 | |
| 17 | gui->setInput(input); |
| 18 | gui->setGraphics(Canvas::getSDLGraphics()); |
| 19 | Image::setImageLoader(new SDLImageLoader()); |
| 20 | |
| 21 | menuKeyListener = new MenuSystem::KeyListener(*this); |
| 22 | gui->addGlobalKeyListener(menuKeyListener); |
| 23 | |
| 24 | normal = loadFont("normal.png"); |
| 25 | highlighted = loadFont("highlight.png"); |
| 26 | gcn::Widget::setGlobalFont(normal); |
| 27 | |
| 28 | top = new Container(); |
| 29 | top->setOpaque(false); |
| 30 | top->setVisible(true); |
| 31 | top->setPosition(0,0); |
| 32 | top->setDimension(Rectangle(0, 0, Settings::getScreenWidth(), Settings::getScreenHeight())); |
| 33 | |
| 34 | gui->setTop(top); |
| 35 | |
| 36 | menus = new map< Menu::Id, Menu * > (); |
| 37 | } |
| 38 | |
| 39 | MenuSystem::~MenuSystem() |
| 40 | { |
| 41 | delete highlighted; |
| 42 | delete normal; |
| 43 | |
| 44 | delete top; |
| 45 | |
| 46 | gui->removeGlobalKeyListener(menuKeyListener); |
| 47 | delete menuKeyListener; |
| 48 | |
| 49 | delete gui; |
| 50 | |
| 51 | delete menus; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | MenuSystem::render() |
| 56 | { |
| 57 | gui->draw(); |
| 58 | } |
| 59 | |
| 60 | bool |
| 61 | MenuSystem::update() |
| 62 | { |
| 63 | gui->logic(); |
| 64 | |
| 65 | if (currentMenu != nextMenu) |
| 66 | { |
| 67 | if (currentMenu) |
| 68 | currentMenu->leave(); |
| 69 | |
| 70 | if (nextMenu) |
| 71 | nextMenu->enter(); |
| 72 | |
| 73 | currentMenu = nextMenu; |
| 74 | } |
| 75 | |
| 76 | return currentMenu; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | MenuSystem::resize() |
| 81 | { |
| 82 | int w = Settings::getScreenWidth(); |
| 83 | int h = Settings::getScreenHeight(); |
| 84 | top->setSize(w, h); |
| 85 | |
| 86 | for (map< Menu::Id, Menu * >::iterator i = menus->begin(); i != menus->end(); i++) |
| 87 | { |
| 88 | i->second->resize(w, h); |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | MenuSystem::addMenu(Menu::Id menuId, Menu *menu) |
| 95 | { |
| 96 | (*menus)[menuId] = menu; |
| 97 | menu->registered(this); |
| 98 | top->add(menu->getContainer()); |
| 99 | } |
| 100 | |
| 101 | ImageFont * |
| 102 | MenuSystem::getNormalFont() |
| 103 | { |
| 104 | return normal; |
| 105 | } |
| 106 | |
| 107 | ImageFont * |
| 108 | MenuSystem::getHighlightedFont() |
| 109 | { |
| 110 | return highlighted; |
| 111 | } |
| 112 | |
| 113 | ImageFont * |
| 114 | MenuSystem::loadFont(string fileName) |
| 115 | { |
| 116 | ImageFont *font = 0; |
| 117 | std::string s = PKG_DATA_DIR; |
| 118 | s += fileName; |
| 119 | |
| 120 | try |
| 121 | { |
| 122 | font = new ImageFont(s, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""); |
| 123 | return font; |
| 124 | } |
| 125 | catch (gcn::Exception &e) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | s = "data"; |
| 130 | #ifdef WINDOWS |
| 131 | s += "\\"; |
| 132 | #else |
| 133 | s += "/"; |
| 134 | #endif |
| 135 | s += fileName; |
| 136 | |
| 137 | try |
| 138 | { |
| 139 | font = new ImageFont(s, " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""); |
| 140 | return font; |
| 141 | } |
| 142 | catch (gcn::Exception &e) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | cerr << "Could not load " << fileName << " from the following directories: " << endl |
| 147 | << PKG_DATA_DIR << endl |
| 148 | << "data" << endl; |
| 149 | |
| 150 | exit( 1 ); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | Image * |
| 156 | MenuSystem::loadImage(string fileName) |
| 157 | { |
| 158 | Image *image = 0; |
| 159 | std::string s = PKG_DATA_DIR; |
| 160 | s += fileName; |
| 161 | |
| 162 | try |
| 163 | { |
| 164 | image = Image::load(s); |
| 165 | return image; |
| 166 | } |
| 167 | catch (gcn::Exception &e) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | s = "data"; |
| 172 | #ifdef WINDOWS |
| 173 | s += "\\"; |
| 174 | #else |
| 175 | s += "/"; |
| 176 | #endif |
| 177 | s += fileName; |
| 178 | |
| 179 | try |
| 180 | { |
| 181 | image = Image::load(s); |
| 182 | return image; |
| 183 | } |
| 184 | catch (gcn::Exception &e) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | cerr << "Could not load " << fileName << " from the following directories: " << endl |
| 189 | << PKG_DATA_DIR << endl |
| 190 | << "data" << endl; |
| 191 | |
| 192 | exit( 1 ); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Makes the menu with the given id visible and hides the currently visible |
| 199 | * one (if there is one). |
| 200 | * |
| 201 | * If called with no argument the main menu is shown. |
| 202 | * |
| 203 | * If called with id == NONE the menus are made invisible. |
| 204 | * |
| 205 | * Actually the menusystem does not change to current visible menu at once. It |
| 206 | * just marks which menu it should show next and completes the request when |
| 207 | * update() is called next. |
| 208 | * Doing so prevents the menusystem from hiding right after it was made visible |
| 209 | * from main. |
| 210 | * |
| 211 | */ |
| 212 | void |
| 213 | MenuSystem::enter(Menu::Id id) |
| 214 | { |
| 215 | if (id != Menu::NONE) |
| 216 | { |
| 217 | nextMenu = (*menus)[id]; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | nextMenu = 0; |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
| 226 | bool |
| 227 | MenuSystem::isVisible() const |
| 228 | { |
| 229 | return currentMenu; |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | MenuSystem::invoke() |
| 234 | { |
| 235 | if (currentMenu) |
| 236 | currentMenu->invoke(); |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | MenuSystem::cancel() |
| 241 | { |
| 242 | if (currentMenu) |
| 243 | currentMenu->cancel(); |
| 244 | |
| 245 | } |
| 246 | |
| 247 | MenuSystem::KeyListener::KeyListener(MenuSystem &menuSystem) |
| 248 | : system(menuSystem) |
| 249 | { |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | MenuSystem::KeyListener::keyReleased(KeyEvent &keyEvent) |
| 254 | { |
| 255 | // MenuSystem does not handle making itself visible. This is done from main. |
| 256 | if (!system.currentMenu) |
| 257 | return; |
| 258 | |
| 259 | switch (keyEvent.getKey().getValue()) |
| 260 | { |
| 261 | case Key::BACKSPACE: |
| 262 | system.currentMenu->reset(); |
| 263 | break; |
| 264 | case Key::ESCAPE: |
| 265 | system.currentMenu->cancel(); |
| 266 | break; |
| 267 | case Key::ENTER: |
| 268 | system.currentMenu->invoke(); |
| 269 | break; |
| 270 | case Key::UP: |
| 271 | system.currentMenu->up(); |
| 272 | break; |
| 273 | case Key::DOWN: |
| 274 | system.currentMenu->down(); |
| 275 | break; |
| 276 | case Key::LEFT: |
| 277 | system.currentMenu->previous(); |
| 278 | break; |
| 279 | case Key::RIGHT: |
| 280 | system.currentMenu->next(); |
| 281 | break; |
| 282 | } |
| 283 | } |