Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/selector.cpp
00001 /*
00002  * selector.cpp
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 "selector.hpp"
00026 
00027 Selector::Selector(const sf::Unicode::Text& l,
00028                 const std::vector< sf::Unicode::Text >& o,
00029                 unsigned int co, bool ta, float ar, float s):
00030         turnaround(ta), current_option(co), separator(s)
00031 {
00032         aspect_ratio = ar;
00033         
00034         if (current_option >= o.size())
00035         {
00036                 current_option = o.size() - 1;
00037         }
00038         
00039         label_container = new GUIElementContainer((aspect_ratio - 0.5f) * separator);
00040         attach(label_container, 1, SCALE_MODE_VERTICAL, 0, 0.5);
00041         
00042         label = new GUIString(l, "#44");
00043         label_container->attach(label, 1, SCALE_MODE_SMALLER, 0, 0.5);
00044         
00045         
00046         leftarrow_container = new GUIElementContainer(0.5f);
00047         attach(leftarrow_container, 1.0f, SCALE_MODE_VERTICAL, separator, 0.5f);
00048         
00049         //leftarrow_single = new GUIString(L"‹", "#44");
00050         leftarrow_single = new GUIImage("LEFT_ARROW", 1.0f);
00051         leftarrow_container->attach(leftarrow_single, 1.0f, SCALE_MODE_SMALLER,
00052                 0.5f, 0.5f);
00053         
00054         //leftarrow_double = new GUIString(L"«", "#44");
00055         leftarrow_double = new GUIImage("LEFT_ARROW", 1.0f);
00056         leftarrow_container->attach(leftarrow_double, 1.0f, SCALE_MODE_SMALLER,
00057                 0.5f, 0.5f);
00058         
00059         
00060         rightarrow_container = new GUIElementContainer(0.5f);
00061         attach(rightarrow_container, 1.0f, SCALE_MODE_VERTICAL, 1.0f, 0.5f);
00062         
00063         //rightarrow_single = new GUIString(L"›", "#44");
00064         rightarrow_single = new GUIImage("RIGHT_ARROW", 1.0f);
00065         rightarrow_container->attach(rightarrow_single, 1.0f, SCALE_MODE_SMALLER,
00066                 0.5f, 0.5f);
00067         
00068         //rightarrow_double = new GUIString(L"»", "#44");
00069         rightarrow_double = new GUIImage("RIGHT_ARROW", 1.0f);
00070         rightarrow_container->attach(rightarrow_double, 1.0f, SCALE_MODE_SMALLER,
00071                 0.5f, 0.5f);
00072         
00073         option_container = new GUIElementContainer(
00074                 (aspect_ratio - 0.5f) * (1.0f - separator) - 0.5f);
00075         attach(option_container, 1.0f, SCALE_MODE_VERTICAL,
00076                 (aspect_ratio / 2.0f + 0.25f) /
00077                         ((aspect_ratio + 0.75f) - aspect_ratio / 2.0f),
00078                 0.5);
00079         
00080         GUIString* str;
00081         for(size_t i = 0; i < o.size(); ++i)
00082         {
00083                 str = new GUIString(o[i], "#44");
00084                 str->visible = (i == current_option);
00085                 
00086                 options.push_back(str);
00087                 option_container->attach(str, 1, SCALE_MODE_SMALLER, 0.5, 0.5);
00088         }
00089         
00090         update_arrows();
00091 }
00092 
00093 void Selector::update_arrows()
00094 {
00095         leftarrow_single->visible = leftarrow_double->visible =
00096                 rightarrow_single->visible = rightarrow_double->visible = false;
00097         
00098         if (options.size() > 1)
00099         {
00100                 if (turnaround)
00101                 {
00102                         if (options.size() > 2)
00103                         {
00104                                 leftarrow_double->visible = true;
00105                                 rightarrow_double->visible = true;
00106                         }
00107                         else
00108                         {
00109                                 leftarrow_single->visible = true;
00110                                 rightarrow_single->visible = true;
00111                         }
00112                 }
00113                 else
00114                 {
00115                         if (current_option > 0)
00116                         {
00117                                 if (current_option == 1)
00118                                 {
00119                                         leftarrow_single->visible = true;
00120                                 }
00121                                 else
00122                                 {
00123                                         leftarrow_double->visible = true;
00124                                 }
00125                         }
00126                         
00127                         if (current_option < options.size() - 1)
00128                         {
00129                                 if(current_option == options.size() - 2)
00130                                 {
00131                                         rightarrow_single->visible = true;
00132                                 }
00133                                 else
00134                                 {
00135                                         rightarrow_double->visible = true;
00136                                 }
00137                         }
00138                 }
00139         }
00140 }
00141 
00142 void Selector::render()
00143 {
00144         if (visible)
00145         {
00146                 leftarrow_container->parent_align.x = separator;
00147                 leftarrow_container->child_align.x = separator;
00148                 
00149                 label_container->aspect_ratio = (aspect_ratio - 0.5f) * separator;
00150                 
00151                 option_container->aspect_ratio =
00152                         (aspect_ratio - 0.5f) * (1.0f - separator) - 0.5f;
00153                 
00154                 option_container->parent_align.x = option_container->child_align.x =
00155                         (option_container->aspect_ratio - aspect_ratio + 0.5f) / 
00156                         (option_container->aspect_ratio - aspect_ratio);
00157                 
00158                 GUIElementContainer::render();
00159         }
00160 }
00161 
00162 void Selector::set_color(sf::Uint8 r, sf::Uint8 g, sf::Uint8 b, sf::Uint8 a)
00163 {
00164         label->set_color(r, g, b, a);
00165         /*leftarrow_single->set_color(r, g, b, a);
00166         leftarrow_double->set_color(r, g, b, a);
00167         rightarrow_single->set_color(r, g, b, a);
00168         rightarrow_double->set_color(r, g, b, a);
00169         */
00170         
00171         leftarrow_single->color = sf::Color(r, g, b, a);
00172         leftarrow_double->color = sf::Color(r, g, b, a);
00173         rightarrow_single->color = sf::Color(r, g, b, a);
00174         rightarrow_double->color = sf::Color(r, g, b, a);
00175         
00176         for(size_t i = 0; i < options.size(); ++i)
00177         {
00178                 options[i]->set_color(r, g, b, a);
00179         }
00180 }
00181 
00182 unsigned int Selector::get_option()
00183 {
00184         return current_option;
00185 }
00186 
00187 void Selector::next()
00188 {
00189         options[current_option]->visible = false;
00190         if (current_option < options.size() - 1)
00191         {
00192                 ++current_option;
00193         }
00194         else
00195         {
00196                 if (turnaround)
00197                 {
00198                         current_option = 0;
00199                 }
00200         }
00201         options[current_option]->visible = true;
00202         
00203         update_arrows();
00204 }
00205 
00206 void Selector::prev()
00207 {
00208         options[current_option]->visible = false;
00209         if (current_option > 0)
00210         {
00211                 --current_option;
00212         }
00213         else
00214         {
00215                 if (turnaround)
00216                 {
00217                         current_option = options.size() -1;
00218                 }
00219         }
00220         options[current_option]->visible = true;
00221         
00222         update_arrows();
00223 }
00224 
00225 Selector::~Selector()
00226 {
00227         // no need to delete the items, the container destructor will delete them
00228 }
 Összes Osztályok