Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/optionsmenu.cpp
00001 /*
00002  * optionsmenu.cpp - Kukatz 3D
00003  * Copyright (c) 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 "optionsmenu.hpp"
00026 
00027 #include <sstream>
00028 
00029 #include "configmanager.hpp"
00030 #include "windowmanager.hpp"
00031 #include "statemanager.hpp"
00032 #include "guistring.hpp"
00033 #include "selector.hpp"
00034 #include "guiimage.hpp"
00035 
00036 OptionsMenu::OptionsMenu(unsigned int w, unsigned int h): StateBase(w, h),
00037         current_item(0), video_modes(WindowManager::instance().video_modes),
00038         viewport(w, h)
00039 {
00040         resize(sf::Vector2i(w, h));
00041         
00042         gui.attach(new GUIImage("GROUND_2", 0.5f), 1, GUIElement::SCALE_MODE_BIGGER, 0.5f, 0.5f);
00043         
00044         GUIElementContainer* square = new GUIElementContainer(4.0f / 3.0f);
00045         gui.attach(square, 1.0f, GUIElement::SCALE_MODE_SMALLER, 0.5f, 0.5f);
00046         
00047         GUIString* str = new GUIString(L"Beállítások", "#44");
00048         square->attach(str, 0.2, GUIString::SCALE_MODE_VERTICAL, 0.5, 1);
00049         
00050         
00051         std::vector< sf::Unicode::Text > opt;
00052         std::stringstream ss;
00053         
00054         for(int i = video_modes.size() - 1; i >= 0; --i)
00055         { // i must not be unsigned to be able to be < 0
00056                 ss.clear();
00057                 ss.str("");
00058                 ss << video_modes[i].Width << "x" << video_modes[i].Height;
00059                 opt.push_back(ss.str());
00060         }
00061         
00062         Selector* sel = new Selector(L"Felbontás:", opt,
00063                 WindowManager::instance().video_modes.size() -
00064                         WindowManager::instance().current_video_mode - 1, false, 16.0f, 0.6f);
00065         sel->set_color(255, 0, 0);
00066         items.push_back(sel);
00067         square->attach(sel, 0.8f, GUIElement::SCALE_MODE_HORIZONTAL, 0.5, 0.8);
00068         
00069         opt.clear();
00070         opt.push_back("Nem");
00071         opt.push_back("Igen");
00072         
00073         sel = new Selector(L"Teljes képernyő:", opt,
00074                 WindowManager::instance().is_win_fullscreen, true, 16.0f, 0.6f);
00075         items.push_back(sel);
00076         square->attach(sel, 0.8f, GUIElement::SCALE_MODE_HORIZONTAL, 0.5f, 0.7f);
00077         
00078         opt.clear();
00079         opt.push_back("Kikapcsolva");
00080         opt.push_back("Anaglif");
00081         opt.push_back("Kancsal");
00082         
00083         unsigned int mode = 0;
00084         std::string config_string = ConfigManager::instance().get_string("3D");
00085         
00086         if (config_string == "anaglyph")
00087         {
00088                 mode = 1;
00089         }
00090         else
00091         {
00092                 if (config_string == "cross-eyed")
00093                 {
00094                         mode = 2;
00095                 }
00096         }
00097         
00098         sel = new Selector(L"3D:", opt, mode, false, 16.0f, 0.6f);
00099         items.push_back(sel);
00100         square->attach(sel, 0.8f, GUIElement::SCALE_MODE_HORIZONTAL, 0.5f, 0.6f);
00101         
00102         opt.clear();
00103         opt.push_back("Ki");
00104         opt.push_back("Be");
00105         
00106         sel = new Selector(L"Gépi játékosok kamerája:", opt,
00107                 ConfigManager::instance().get_bool("show_ai_cameras"), true, 16.0f, 0.6f);
00108         items.push_back(sel);
00109         square->attach(sel, 0.8f, GUIElement::SCALE_MODE_HORIZONTAL, 0.5f, 0.5f);
00110         
00111         sel = new Selector(L"Más játékosok nevei:", opt,
00112                 ConfigManager::instance().get_bool("show_player_names"), true, 16.0f, 0.6f);
00113         items.push_back(sel);
00114         square->attach(sel, 0.8f, GUIElement::SCALE_MODE_HORIZONTAL, 0.5f, 0.4f);
00115 }
00116 
00117 void OptionsMenu::resize(const sf::Vector2i& s)
00118 {
00119         viewport.size = s;
00120         gui.size = s;
00121         size = s;
00122 }
00123 
00124 void OptionsMenu::process_events(std::vector< sf::Event >& events)
00125 {
00126         for (unsigned int i = 0; i < events.size(); ++i)
00127         {
00128                 if (events[i].Type == sf::Event::KeyPressed)
00129                 {
00130                         switch(events[i].Key.Code)
00131                         {
00132                                 case sf::Key::Escape:
00133                                 {
00134                                         pop();
00135                                 }
00136                                 break;
00137                                 case sf::Key::Down:
00138                                 {
00139                                         items[current_item]->set_color(255, 255, 255);
00140                                         if (current_item == items.size()-1)
00141                                         {
00142                                                 current_item = 0;
00143                                         }
00144                                         else
00145                                         {
00146                                                 ++current_item;
00147                                         }
00148                                         items[current_item]->set_color(255, 0, 0);
00149                                 }
00150                                 break;
00151                                 case sf::Key::Up:
00152                                 {
00153                                         items[current_item]->set_color(255, 255, 255);
00154                                         if (current_item == 0)
00155                                         {
00156                                                 current_item = items.size() - 1;
00157                                         }
00158                                         else
00159                                         {
00160                                                 --current_item;
00161                                         }
00162                                         items[current_item]->set_color(255, 0, 0);
00163                                 }
00164                                 break;
00165                                 case sf::Key::Left:
00166                                 {
00167                                         items[current_item]->prev();
00168                                 }
00169                                 break;
00170                                 case sf::Key::Right:
00171                                 {
00172                                         items[current_item]->next();
00173                                 }
00174                                 break;
00175                                 case sf::Key::Return:
00176                                 {
00177                                         WindowManager::instance().set_videomode(
00178                                                 video_modes.size() - items[0]->get_option() - 1,
00179                                                 items[1]->get_option());
00180                                         
00181                                         std::string names[] = {"off", "anaglyph", "cross-eyed"};
00182                                         ConfigManager::instance().set("3D", names[items[2]->get_option()]);
00183                                         
00184                                         ConfigManager::instance().set("show_ai_cameras", items[3]->get_option());
00185                                         ConfigManager::instance().set("show_player_names", items[4]->get_option());
00186                                         ConfigManager::instance().save();
00187                                         
00188                                 }
00189                                 break;
00190                                 default:
00191                                 {
00192                                         
00193                                 }
00194                                 break;
00195                         }
00196                 }
00197         }
00198 }
00199 
00200 void OptionsMenu::update(float)
00201 {
00202         
00203 }
00204 
00205 void OptionsMenu::render()
00206 {
00207         viewport.look_2d();
00208         gui.render();
00209 }
00210 
00211 OptionsMenu::~OptionsMenu()
00212 {
00213         // no need to delete the items, the gui will delete them
00214 }
 Összes Osztályok