Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/profiles.cpp
00001 /*
00002  * profiles.cpp - Kukatz 3D
00003  * Copyright (c) 2012 - 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 "profiles.hpp"
00026 
00027 #include "guiimage.hpp"
00028 #include "profilemanager.hpp"
00029 #include "statemanager.hpp"
00030 #include "editprofile.hpp"
00031 
00032 Profiles::Profiles(unsigned int w, unsigned int h):
00033         StateBase(w, h), viewport(w, h),
00034         profiles(ProfileManager::instance().get_profiles()), selected(-1),
00035         start(0), table_needs_update(false)
00036 {
00037         resize(sf::Vector2i(w, h));
00038         
00039         gui.attach(new GUIImage("GROUND_2", 0.5f), 1, GUIElement::SCALE_MODE_BIGGER, 0.5f, 0.5f);
00040         
00041         up_arrow = new GUIImage("UP_ARROW", 1.0f);
00042         down_arrow = new GUIImage("DOWN_ARROW", 1.0f);
00043         
00044         title = new GUIString(L"Játékosok", "#44");
00045         add = new GUIString(L"Új játékos", "#44");
00046         
00047         GUIElementContainer* square = new GUIElementContainer(4.0f / 3.0f);
00048         gui.attach(square, 1.0f, GUIElement::SCALE_MODE_SMALLER, 0.5f, 0.5f);
00049         
00050         square->attach(title, 0.2, GUIString::SCALE_MODE_VERTICAL, 0.5, 1);
00051         square->attach(add, 0.125, GUIString::SCALE_MODE_VERTICAL, 0.25, 0.8);
00052         
00053         table = new GUIElementContainer(1.16f);
00054         square->attach(table, 0.65, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0.1);
00055         
00056         namebox = new GUIElementContainer(1.0f);
00057         table->attach(namebox, 1.0, GUIElement::SCALE_MODE_VERTICAL, 1.0f, 0.5f);
00058         
00059         table->attach(up_arrow, 1.f / 6.0f, GUIElement::SCALE_MODE_VERTICAL, -1.0f / 6.0f, 1.0f);
00060         table->attach(down_arrow, 1.f / 6.0f, GUIElement::SCALE_MODE_VERTICAL, -1.0f / 6.0f, 0.0f);
00061         
00062         set_table();
00063 }
00064 
00065 void Profiles::set_table()
00066 {
00067         for (unsigned int i = 0; i < namebox->children.size(); ++i)
00068         {
00069                 delete namebox->children[i];
00070                 namebox->children.clear();
00071         }
00072         
00073         for (unsigned int i = 3; i < table->children.size(); ++i)
00074         {
00075                 delete table->children[i];
00076                 table->children.resize(3);
00077         }
00078         
00079         for (unsigned int i = start; (i < (start + 6)) && (i < profiles.size()); ++i)
00080         {
00081                 table->attach(new GUIImage(profiles[i]->is_human?"PERSON":"CIRCUIT"),
00082                         1.0f / 6.0f, GUIElement::SCALE_MODE_VERTICAL, 0.0f, 1.0f - ((float)(i - start) / 5.0f));
00083                 
00084                 GUIString* str_ptr = new GUIString(profiles[i]->name, "#44");
00085                 namebox->attach(str_ptr, 1.0f / 6.0f,
00086                         GUIElement::SCALE_MODE_VERTICAL, 0.0f,
00087                         1.0f - ((float)(i - start) / 5.0f));
00088         }
00089         
00090         up_arrow->visible = (start > 0);
00091         down_arrow->visible = (profiles.size() > 6) && (start < profiles.size() - 6);
00092         
00093         if (selected >= 0)
00094         {
00095                 if ((start + selected) >= profiles.size())
00096                 {
00097                         if (selected >= (int)profiles.size())
00098                         {
00099                                 start = 0;
00100                                 selected = profiles.size() - 1;
00101                         }
00102                         else
00103                         {
00104                                 start = profiles.size() - selected - 1;
00105                         }
00106                 }
00107         }
00108         
00109         if (selected >= 0)
00110         {
00111                 GUIString* str_ptr = dynamic_cast< GUIString* >(namebox->children[selected]);
00112                 if (str_ptr)
00113                 {
00114                         str_ptr->set_color(255, 0, 0);
00115                 }
00116         }
00117         else
00118         {
00119                 if (selected == -1)
00120                 {
00121                         add->set_color(255, 0, 0);
00122                 }
00123         }
00124 }
00125 
00126 void Profiles::process_events(std::vector< sf::Event >& events)
00127 {
00128         for (unsigned int i = 0; i < events.size(); ++i)
00129         {
00130                 switch (events[i].Type)
00131                 {
00132                         case sf::Event::KeyPressed:
00133                         {
00134                                 switch (events[i].Key.Code)
00135                                 {
00136                                         case sf::Key::Escape:
00137                                         {
00138                                                 pop();
00139                                         }
00140                                         break;
00141                                         case sf::Key::Up:
00142                                         {
00143                                                 if (selected >= 0)
00144                                                 {
00145                                                         GUIString* str_ptr = dynamic_cast< GUIString* >(namebox->children[selected]);
00146                                                         if (str_ptr)
00147                                                         {
00148                                                                 str_ptr->set_color(255, 255, 255);
00149                                                         }
00150                                                 }
00151                                                 else
00152                                                 {
00153                                                         if (selected == -1)
00154                                                         {
00155                                                                 add->set_color(255, 255, 255);
00156                                                         }
00157                                                 }
00158                                                 
00159                                                 if (selected > -1)
00160                                                 {
00161                                                         --selected;
00162                                                 }
00163                                                 
00164                                                 if (selected >= 0)
00165                                                 {
00166                                                         GUIString* str_ptr = dynamic_cast< GUIString* >(namebox->children[selected]);
00167                                                         if (str_ptr)
00168                                                         {
00169                                                                 str_ptr->set_color(255, 0, 0);
00170                                                         }
00171                                                 }
00172                                                 else
00173                                                 {
00174                                                         if (selected == -1)
00175                                                         {
00176                                                                 if (start > 0)
00177                                                                 {
00178                                                                         --start;
00179                                                                         ++selected;
00180                                                                         set_table();
00181                                                                 }
00182                                                                 else
00183                                                                 {
00184                                                                         add->set_color(255, 0, 0);
00185                                                                 }
00186                                                         }
00187                                                 }
00188                                         }
00189                                         break;
00190                                         case sf::Key::Down:
00191                                         {
00192                                                 if (selected >= 0)
00193                                                 {
00194                                                         GUIString* str_ptr = dynamic_cast< GUIString* >(namebox->children[selected]);
00195                                                         if (str_ptr)
00196                                                         {
00197                                                                 str_ptr->set_color(255, 255, 255);
00198                                                         }
00199                                                 }
00200                                                 else
00201                                                 {
00202                                                         if (selected == -1)
00203                                                         {
00204                                                                 add->set_color(255, 255, 255);
00205                                                         }
00206                                                 }
00207                                                 
00208                                                 if (selected < 5)
00209                                                 {
00210                                                         ++selected;
00211                                                         if (selected >= (int)profiles.size())
00212                                                         {
00213                                                                 --selected;
00214                                                         }
00215                                                 }
00216                                                 else
00217                                                 {
00218                                                         if (profiles.size() > (selected + start + 1))
00219                                                         {
00220                                                                 ++start;
00221                                                                 set_table();
00222                                                         }
00223                                                 }
00224                                                 
00225                                                 if (selected >= 0)
00226                                                 {
00227                                                         GUIString* str_ptr = dynamic_cast< GUIString* >(namebox->children[selected]);
00228                                                         if (str_ptr)
00229                                                         {
00230                                                                 str_ptr->set_color(255, 0, 0);
00231                                                         }
00232                                                 }
00233                                                 else
00234                                                 {
00235                                                         if (selected == -1)
00236                                                         {
00237                                                                 add->set_color(255, 0, 0);
00238                                                         }
00239                                                 }
00240                                         }
00241                                         break;
00242                                         case sf::Key::Return:
00243                                         {
00244                                                 if (selected >= 0)
00245                                                 {
00246                                                         StateManager::instance().push_state(new EditProfile(size.x, size.y, profiles[selected + start]));
00247                                                         table_needs_update = true;
00248                                                 }
00249                                                 else
00250                                                 {
00251                                                         if (selected == -1)
00252                                                         {
00253                                                                 StateManager::instance().push_state(new EditProfile(size.x, size.y));
00254                                                                 table_needs_update = true;
00255                                                         }
00256                                                 }
00257                                         }
00258                                         break;
00259                                         case sf::Key::Delete:
00260                                         {
00261                                                 if (selected >= 0)
00262                                                 {
00263                                                         ProfileManager::instance().delete_profile(profiles[selected + start]);
00264                                                         ProfileManager::instance().save();
00265                                                         set_table();
00266                                                 }
00267                                         }
00268                                         break;
00269                                         default:
00270                                         {
00271                                                 
00272                                         }
00273                                         break;
00274                                 }
00275                         }
00276                         break;
00277                         default:
00278                         {
00279                                 
00280                         }
00281                         break;
00282                 }
00283         }
00284 }
00285 
00286 void Profiles::render()
00287 {
00288         viewport.look_2d();
00289         gui.render();
00290 }
00291 
00292 void Profiles::update(float)
00293 {
00294         if (table_needs_update)
00295         {
00296                 set_table();
00297                 table_needs_update = false;
00298         }
00299 }
00300 
00301 void Profiles::resize(const sf::Vector2i& s)
00302 {
00303         size = s;
00304         viewport.size = s;
00305         gui.size = s;
00306 }
00307 
00308 Profiles::~Profiles()
00309 {
00310         
00311 }
 Összes Osztályok