Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/mainmenu.cpp
00001 /*
00002  * mainmenu.cpp - Kukatz 3D
00003  * Copyright (c) 2010, 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include "mainmenu.hpp"
00026 
00027 #include <iostream>
00028 
00029 #include "statemanager.hpp"
00030 #include "resourcemanager.hpp"
00031 #include "newgamemenu.hpp"
00032 #include "optionsmenu.hpp"
00033 #include "misc.hpp"
00034 #include "guistring.hpp"
00035 #include "guiimage.hpp"
00036 #include "highscores.hpp"
00037 #include "profiles.hpp"
00038 #include "credits.hpp"
00039 
00040 MainMenu::MainMenu(unsigned int w, unsigned int h): StateBase(w, h),
00041         viewport(w, h), current_item(0)
00042 {
00043         resize(sf::Vector2i(w, h));
00044         
00045         gui.attach(new GUIImage("GROUND_2", 0.5f), 1, GUIElement::SCALE_MODE_BIGGER, 0.5f, 0.5f);
00046         
00047         items.push_back(new GUIString(L"Új játék", "#44"));
00048         items.push_back(new GUIString(L"Játékosok", "#44"));
00049         items.push_back(new GUIString(L"Toplista", "#44"));
00050         items.push_back(new GUIString(L"Beállítások", "#44"));
00051         items.push_back(new GUIString(L"KreditZ", "#44"));
00052         items.push_back(new GUIString(L"Kilépés", "#44"));
00053         
00054         title = new GUIString(L"Kukatz \xC\x01\xFF\xFF\xFF""3""\xC\xFF\x01\x01\xFF""D", "#44");
00055         
00056         GUIElementContainer* square = new GUIElementContainer(4.0f / 3.0f);
00057         gui.attach(square, 1.0f, GUIElement::SCALE_MODE_SMALLER, 0.5f, 0.5f);
00058         
00059         for (unsigned int i = 0; i < items.size(); ++i)
00060         {
00061                 square->attach(items[i], 0.6f / (float)(items.size()),
00062                         GUIElement::SCALE_MODE_VERTICAL, 0.5,
00063                         0.9f - ((float)i + 2.0f) / (float)(items.size() + 2.0f));
00064         }
00065         
00066         square->attach(title, 1.2f / (float)(items.size() - 1),
00067                 GUIElement::SCALE_MODE_VERTICAL, 0.5f,
00068                 1.0f - (0.5f / (float)(items.size() + 1)));
00069         
00070         items[current_item]->set_color(sf::Color::Red);
00071 }
00072 
00073 void MainMenu::resize(const sf::Vector2i& s)
00074 {
00075         viewport.size = s;
00076         gui.size = s;
00077         size = s;
00078 }
00079 
00080 void MainMenu::process_events(std::vector< sf::Event >& events)
00081 {
00082         for (size_t i = 0; i < events.size(); ++i)
00083         {
00084                 if ((events[i].Type == sf::Event::KeyPressed))
00085                 {
00086                         switch(events[i].Key.Code)
00087                         {
00088                                 case sf::Key::Escape:
00089                                 {
00090                                         pop();
00091                                 }
00092                                 break;
00093                                 case sf::Key::Return:
00094                                 {
00095                                         switch (current_item)
00096                                         {
00097                                                 case 0:
00098                                                 {
00099                                                         StateManager::instance().push_state(new NewGameMenu(size.x, size.y));
00100                                                 }
00101                                                 break;
00102                                                 case 1:
00103                                                 {
00104                                                         StateManager::instance().push_state(new Profiles(size.x, size.y));
00105                                                 }
00106                                                 break;
00107                                                 case 2:
00108                                                 {
00109                                                         StateManager::instance().push_state(new HighScores(size.x, size.y));
00110                                                 }
00111                                                 break;
00112                                                 case 3:
00113                                                 {
00114                                                         StateManager::instance().push_state(new OptionsMenu(size.x, size.y));
00115                                                 }
00116                                                 break;
00117                                                 case 4:
00118                                                 {
00119                                                         StateManager::instance().push_state(new Credits(size.x, size.y));
00120                                                 }
00121                                                 break;
00122                                                 case 5:
00123                                                 {
00124                                                         pop();
00125                                                 }
00126                                                 break;
00127                                         }
00128                                 }
00129                                 break;
00130                                 case sf::Key::Up:
00131                                 {
00132                                         items[current_item]->set_color(sf::Color::White);
00133                                         if (current_item == 0)
00134                                         {
00135                                                 current_item = items.size() - 1;
00136                                         }
00137                                         else
00138                                         {
00139                                                 --current_item;
00140                                         }
00141                                         items[current_item]->set_color(sf::Color::Red);
00142                                 }
00143                                 break;
00144                                 case sf::Key::Down:
00145                                 {
00146                                         items[current_item]->set_color(sf::Color::White);
00147                                         if (current_item == items.size() - 1)
00148                                         {
00149                                                 current_item = 0;
00150                                         }
00151                                         else
00152                                         {
00153                                                 ++current_item;
00154                                         }
00155                                         items[current_item]->set_color(sf::Color::Red);
00156                                 }
00157                                 break;
00158                                 default:
00159                                 {
00160                                         
00161                                 }
00162                                 break;
00163                         }
00164                 }
00165         }
00166 }
00167 
00168 void MainMenu::update(float)
00169 {
00170         
00171 }
00172 
00173 void MainMenu::render()
00174 {
00175         viewport.look_2d();
00176         gui.render();
00177 }
00178 
00179 MainMenu::~MainMenu()
00180 {
00181         // no need to delete the items, the gui will delete them
00182 }
 Összes Osztályok