Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/editprofile.cpp
00001 /*
00002  * editprofile.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 "editprofile.hpp"
00026 
00027 #include <iostream>
00028 #include <sstream>
00029 
00030 #include "guiimage.hpp"
00031 #include "profiles.hpp"
00032 #include "statemanager.hpp"
00033 #include "resourcemanager.hpp"
00034 #include "profilemanager.hpp"
00035 #include "misc.hpp"
00036 
00037 EditProfile::EditProfile(unsigned int w, unsigned int h, Profile* p):
00038         StateBase(w, h), viewport(w, h), profile(p), mode(MODE_SELECTION),
00039         selected(0), prev_selected(selected)
00040 {
00041         if (profile)
00042         {
00043                 temp_profile = new Profile(*profile);
00044                 name_is_placeholder = false;
00045         }
00046         else
00047         {
00048                 temp_profile = new Profile("", sf::Randomizer::Random(0, 2), (float)sf::Randomizer::Random(0, 10) / 10.0f);
00049                 name_is_placeholder = true;
00050                 temp_profile->name = L"<NÉV>";
00051         }
00052         resize(sf::Vector2i(w, h));
00053         
00054         gui.attach(new GUIImage("GROUND_2", 0.5f), 1, GUIElement::SCALE_MODE_BIGGER, 0.5f, 0.5f);
00055         
00056         title = new GUIString(L"Játékos adatai", "#44");
00057         
00058         GUIElementContainer* square = new GUIElementContainer(4.0f / 3.0f);
00059         gui.attach(square, 1.0f, GUIElement::SCALE_MODE_SMALLER, 0.5f, 0.5f);
00060         
00061         square->attach(title, 0.2, GUIString::SCALE_MODE_VERTICAL, 0.5, 1);
00062         
00063         table = new GUIElementContainer(1.2f);
00064         square->attach(table, 0.8, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0.1);
00065         
00066         name_label = new GUIString(L"Név:", "#44");
00067         table->attach(name_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0, 1);
00068         
00069         name_string = new GUIString(temp_profile->name, "#44");
00070         table->attach(name_string, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 1, 1);
00071         
00072         std::vector< sf::Unicode::Text > opt;
00073         
00074         opt.push_back(L"katona");
00075         opt.push_back(L"öcsi");
00076         opt.push_back(L"pacman");
00077         
00078         head_selector = new Selector(L"Fej:", opt, temp_profile->head_style, true, 12, 0.5f);
00079         table->attach(head_selector, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0.9);
00080         
00081         opt.clear();
00082         
00083         opt.push_back(L"gép");
00084         opt.push_back(L"ember");
00085         
00086         type_selector = new Selector(L"Típus:", opt, temp_profile->is_human?1:0, true, 12, 0.5f);
00087         table->attach(type_selector, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0.8);
00088         
00089         opt.clear();
00090         
00091         for (unsigned int i = 0; i <= 10; ++i)
00092         {
00093                 std::ostringstream ss;
00094                 ss.clear();
00095                 ss << i;
00096                 opt.push_back(ss.str());
00097         }
00098         
00099         skill_selector = new Selector(L"Ügyesség:", opt, temp_profile->skill * 10, false, 12, 0.5f);
00100         table->attach(skill_selector, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0.7);
00101         
00102         
00103         buttons_label = new GUIString(L"Gombkiosztás:", "#44");
00104         table->attach(buttons_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0.7);
00105         
00106         
00107         up_label = new GUIString(L"Fel:", "#44");
00108         table->attach(up_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.0, 0.6);
00109         up_string = new GUIString(get_key_name(temp_profile->key_up), "#44");
00110         table->attach(up_string, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 1.0, 0.6);
00111         
00112         left_label = new GUIString(L"Balra:", "#44");
00113         table->attach(left_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.0, 0.5);
00114         left_string = new GUIString(get_key_name(temp_profile->key_left), "#44");
00115         table->attach(left_string, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 1.0, 0.5);
00116         
00117         down_label = new GUIString(L"Le:", "#44");
00118         table->attach(down_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.0, 0.4);
00119         down_string = new GUIString(get_key_name(temp_profile->key_down), "#44");
00120         table->attach(down_string, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 1.0, 0.4);
00121         
00122         right_label = new GUIString(L"Jobbra:", "#44");
00123         table->attach(right_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.0, 0.3);
00124         right_string = new GUIString(get_key_name(temp_profile->key_right), "#44");
00125         table->attach(right_string, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 1.0, 0.3);
00126         
00127         
00128         save_label = new GUIString(L"Mentés", "#44");
00129         square->attach(save_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0.1f);
00130         
00131         delete_label = new GUIString(L"Játékos törlése", "#44");
00132         square->attach(delete_label, 0.1f, GUIElement::SCALE_MODE_VERTICAL, 0.5, 0);
00133         
00134         set_type();
00135         set_color();
00136 }
00137 
00138 void EditProfile::set_color()
00139 {
00140         switch (prev_selected)
00141         {
00142                 case 0:
00143                 {
00144                         name_label->set_color(255, 255, 255);
00145                         name_string->set_color(255, 255, 255);
00146                 }
00147                 break;
00148                 case 1:
00149                 {
00150                         head_selector->set_color(255, 255, 255);
00151                 }
00152                 break;
00153                 case 2:
00154                 {
00155                         type_selector->set_color(255, 255, 255);
00156                 }
00157                 break;
00158                 case 3:
00159                 {
00160                         skill_selector->set_color(255, 255, 255);
00161                 }
00162                 break;
00163                 case 4:
00164                 {
00165                         up_label->set_color(255, 255, 255);
00166                         up_string->set_color(255, 255, 255);
00167                 }
00168                 break;
00169                 case 5:
00170                 {
00171                         left_label->set_color(255, 255, 255);
00172                         left_string->set_color(255, 255, 255);
00173                 }
00174                 break;
00175                 case 6:
00176                 {
00177                         down_label->set_color(255, 255, 255);
00178                         down_string->set_color(255, 255, 255);
00179                 }
00180                 break;
00181                 case 7:
00182                 {
00183                         right_label->set_color(255, 255, 255);
00184                         right_string->set_color(255, 255, 255);
00185                 }
00186                 break;
00187                 case 8:
00188                 {
00189                         save_label->set_color(255, 255, 255);
00190                 }
00191                 break;
00192                 case 9:
00193                 {
00194                         delete_label->set_color(255, 255, 255);
00195                 }
00196                 break;
00197                 default:
00198                 {
00199                         
00200                 }
00201                 break;
00202         }
00203         
00204         switch (selected)
00205         {
00206                 case 0:
00207                 {
00208                         name_label->set_color(255, 0, 0);
00209                         name_string->set_color(255, 0, 0);
00210                 }
00211                 break;
00212                 case 1:
00213                 {
00214                         head_selector->set_color(255, 0, 0);
00215                 }
00216                 break;
00217                 case 2:
00218                 {
00219                         type_selector->set_color(255, 0, 0);
00220                 }
00221                 break;
00222                 case 3:
00223                 {
00224                         skill_selector->set_color(255, 0, 0);
00225                 }
00226                 break;
00227                 case 4:
00228                 {
00229                         up_label->set_color(255, 0, 0);
00230                         up_string->set_color(255, 0, 0);
00231                 }
00232                 break;
00233                 case 5:
00234                 {
00235                         left_label->set_color(255, 0, 0);
00236                         left_string->set_color(255, 0, 0);
00237                 }
00238                 break;
00239                 case 6:
00240                 {
00241                         down_label->set_color(255, 0, 0);
00242                         down_string->set_color(255, 0, 0);
00243                 }
00244                 break;
00245                 case 7:
00246                 {
00247                         right_label->set_color(255, 0, 0);
00248                         right_string->set_color(255, 0, 0);
00249                 }
00250                 break;
00251                 case 8:
00252                 {
00253                         save_label->set_color(255, 0, 0);
00254                 }
00255                 break;
00256                 case 9:
00257                 {
00258                         delete_label->set_color(255, 0, 0);
00259                 }
00260                 break;
00261                 default:
00262                 {
00263                         
00264                 }
00265                 break;
00266         }
00267         
00268         prev_selected = selected;
00269 }
00270 
00271 void EditProfile::set_type()
00272 {
00273         if (temp_profile->is_human)
00274         {
00275                 buttons_label->visible = true;
00276                 up_label->visible     = true;
00277                 up_string->visible    = true;
00278                 left_label->visible   = true;
00279                 left_string->visible  = true;
00280                 down_label->visible   = true;
00281                 down_string->visible  = true;
00282                 right_label->visible  = true;
00283                 right_string->visible = true;
00284                 
00285                 skill_selector->visible = false;
00286         }
00287         else
00288         {
00289                 skill_selector->visible = true;
00290                 
00291                 buttons_label->visible = false;
00292                 up_label->visible     = false;
00293                 up_string->visible    = false;
00294                 left_label->visible   = false;
00295                 left_string->visible  = false;
00296                 down_label->visible   = false;
00297                 down_string->visible  = false;
00298                 right_label->visible  = false;
00299                 right_string->visible = false;
00300         }
00301 }
00302 
00303 void EditProfile::process_events(std::vector< sf::Event >& events)
00304 {
00305         for (unsigned int i = 0; i < events.size(); ++i)
00306         {
00307                 switch (events[i].Type)
00308                 {
00309                         case sf::Event::KeyPressed:
00310                         {
00311                                 //std::cout << "key: " << events[i].Key.Code << " -> " << (std::string)get_key_name(events[i].Key.Code) << "\n";
00312                                 switch (mode)
00313                                 {
00314                                         case MODE_KEY_UP:
00315                                         {
00316                                                 temp_profile->key_up = events[i].Key.Code;
00317                                                 mode = MODE_SELECTION;
00318                                                 up_string->set_text(get_key_name(temp_profile->key_up));
00319                                         }
00320                                         break;
00321                                         case MODE_KEY_LEFT:
00322                                         {
00323                                                 temp_profile->key_left = events[i].Key.Code;
00324                                                 mode = MODE_SELECTION;
00325                                                 left_string->set_text(get_key_name(temp_profile->key_left));
00326                                         }
00327                                         break;
00328                                         case MODE_KEY_DOWN:
00329                                         {
00330                                                 temp_profile->key_down = events[i].Key.Code;
00331                                                 mode = MODE_SELECTION;
00332                                                 down_string->set_text(get_key_name(temp_profile->key_down));
00333                                         }
00334                                         break;
00335                                         case MODE_KEY_RIGHT:
00336                                         {
00337                                                 temp_profile->key_right = events[i].Key.Code;
00338                                                 mode = MODE_SELECTION;
00339                                                 right_string->set_text(get_key_name(temp_profile->key_right));
00340                                         }
00341                                         break;
00342                                         case MODE_SELECTION:
00343                                         {
00344                                                 switch (events[i].Key.Code)
00345                                                 {
00346                                                         case sf::Key::Up:
00347                                                         {
00348                                                                 if (selected > 0)
00349                                                                 {
00350                                                                         --selected;
00351                                                                         if (temp_profile->is_human)
00352                                                                         {
00353                                                                                 if (selected == 3)
00354                                                                                 {
00355                                                                                         selected = 2;
00356                                                                                 }
00357                                                                         }
00358                                                                         else
00359                                                                         {
00360                                                                                 if (selected == 7)
00361                                                                                 {
00362                                                                                         selected = 3;
00363                                                                                 }
00364                                                                         }
00365                                                                 }
00366                                                                 else
00367                                                                 {
00368                                                                         selected = 9;
00369                                                                 }
00370                                                         }
00371                                                         break;
00372                                                         case sf::Key::Down:
00373                                                         {
00374                                                                 if (selected < 9)
00375                                                                 {
00376                                                                         ++selected;
00377                                                                         if (temp_profile->is_human)
00378                                                                         {
00379                                                                                 if (selected == 3)
00380                                                                                 {
00381                                                                                         selected = 4;
00382                                                                                 }
00383                                                                         }
00384                                                                         else
00385                                                                         {
00386                                                                                 if (selected == 4)
00387                                                                                 {
00388                                                                                         selected = 8;
00389                                                                                 }
00390                                                                         }
00391                                                                 }
00392                                                                 else
00393                                                                 {
00394                                                                         selected = 0;
00395                                                                 }
00396                                                         }
00397                                                         break;
00398                                                         case sf::Key::Right:
00399                                                         {
00400                                                                 switch (selected)
00401                                                                 {
00402                                                                         case 1:
00403                                                                         {
00404                                                                                 head_selector->next();
00405                                                                                 temp_profile->head_style = head_selector->get_option();
00406                                                                         }
00407                                                                         break;
00408                                                                         case 2:
00409                                                                         {
00410                                                                                 type_selector->next();
00411                                                                                 temp_profile->is_human = (type_selector->get_option() == 1);
00412                                                                                 set_type();
00413                                                                         }
00414                                                                         break;
00415                                                                         case 3:
00416                                                                         {
00417                                                                                 skill_selector->next();
00418                                                                                 temp_profile->skill = skill_selector->get_option() * 0.1f;
00419                                                                         }
00420                                                                         break;
00421                                                                         default:
00422                                                                         {
00423                                                                                 
00424                                                                         }
00425                                                                         break;
00426                                                                 }
00427                                                         }
00428                                                         break;
00429                                                         case sf::Key::Left:
00430                                                         {
00431                                                                 switch (selected)
00432                                                                 {
00433                                                                         case 1:
00434                                                                         {
00435                                                                                 head_selector->prev();
00436                                                                                 temp_profile->head_style = head_selector->get_option();
00437                                                                         }
00438                                                                         break;
00439                                                                         case 2:
00440                                                                         {
00441                                                                                 type_selector->prev();
00442                                                                                 temp_profile->is_human = (type_selector->get_option() == 1);
00443                                                                                 set_type();
00444                                                                         }
00445                                                                         break;
00446                                                                         case 3:
00447                                                                         {
00448                                                                                 skill_selector->prev();
00449                                                                                 temp_profile->skill = skill_selector->get_option() * 0.1f;
00450                                                                         }
00451                                                                         break;
00452                                                                         default:
00453                                                                         {
00454                                                                                 
00455                                                                         }
00456                                                                         break;
00457                                                                 }
00458                                                         }
00459                                                         break;
00460                                                         case sf::Key::Escape:
00461                                                         {
00462                                                                 pop();
00463                                                         }
00464                                                         break;
00465                                                         case sf::Key::Return:
00466                                                         {
00467                                                                 switch (selected)
00468                                                                 {
00469                                                                         case 4:
00470                                                                         {
00471                                                                                 mode = MODE_KEY_UP;
00472                                                                                 up_string->set_text("????");
00473                                                                         }
00474                                                                         break;
00475                                                                         case 5:
00476                                                                         {
00477                                                                                 mode = MODE_KEY_LEFT;
00478                                                                                 left_string->set_text("????");
00479                                                                         }
00480                                                                         break;
00481                                                                         case 6:
00482                                                                         {
00483                                                                                 mode = MODE_KEY_DOWN;
00484                                                                                 down_string->set_text("????");
00485                                                                         }
00486                                                                         break;
00487                                                                         case 7:
00488                                                                         {
00489                                                                                 mode = MODE_KEY_RIGHT;
00490                                                                                 right_string->set_text("????");
00491                                                                         }
00492                                                                         break;
00493                                                                         case 8:
00494                                                                         {
00495                                                                                 if ((!name_is_placeholder) &&
00496                                                                                         (((sf::Unicode::UTF32String)temp_profile->name).length() > 0))
00497                                                                                 {
00498                                                                                         if (profile)
00499                                                                                         {
00500                                                                                                 *profile = *temp_profile;
00501                                                                                         }
00502                                                                                         else
00503                                                                                         {
00504                                                                                                 ProfileManager::instance().add_profile(temp_profile);
00505                                                                                         }
00506                                                                                         
00507                                                                                         ProfileManager::instance().save();
00508                                                                                         
00509                                                                                         pop();
00510                                                                                 }
00511                                                                                 else
00512                                                                                 {
00513                                                                                         selected = 0;
00514                                                                                         temp_profile->name = L"<NÉV>";
00515                                                                                         name_is_placeholder = true;
00516                                                                                         name_string->set_text(temp_profile->name);
00517                                                                                 }
00518                                                                         }
00519                                                                         break;
00520                                                                         case 9:
00521                                                                         {
00522                                                                                 ProfileManager::instance().delete_profile(profile);
00523                                                                                 ProfileManager::instance().save();
00524                                                                                 
00525                                                                                 pop();
00526                                                                         }
00527                                                                         break;
00528                                                                         default:
00529                                                                         {
00530                                                                                 
00531                                                                         }
00532                                                                         break;
00533                                                                 }
00534                                                         }
00535                                                         break;
00536                                                         case 279:
00537                                                         {
00538                                                                 if (selected == 0)
00539                                                                 {
00540                                                                         if (name_is_placeholder)
00541                                                                         {
00542                                                                                 temp_profile->name = "";
00543                                                                                 name_is_placeholder = false;
00544                                                                         }
00545                                                                         else
00546                                                                         {
00547                                                                                 temp_profile->name = ((sf::Unicode::UTF32String)temp_profile->name).substr(
00548                                                                                         0, ((sf::Unicode::UTF32String)temp_profile->name).length() - 1);
00549                                                                         }
00550                                                                         name_string->set_text(temp_profile->name);
00551                                                                 }
00552                                                         }
00553                                                         break;
00554                                                         default:
00555                                                         {
00556                                                                 
00557                                                         }
00558                                                         break;
00559                                                 }
00560                                         }
00561                                         break;
00562                                         default:
00563                                         {
00564                                                 
00565                                         }
00566                                         break;
00567                                 }
00568                         }
00569                         break;
00570                         case sf::Event::TextEntered:
00571                         {
00572                                 //std::cout << "text: " << events[i].Text.Unicode << "\n";
00573                                 if ((mode == MODE_SELECTION) && (selected == 0) &&
00574                                         (((sf::Unicode::UTF32String)temp_profile->name).length() < 10) &&
00575                                         (ResourceManager::instance().fonts["#44"]->glyphs.count(events[i].Text.Unicode) > 0) &&
00576                                         (events[i].Text.Unicode != 27))
00577                                 {
00578                                         if (name_is_placeholder)
00579                                         {
00580                                                 temp_profile->name = "";
00581                                                 name_is_placeholder = false;
00582                                         }
00583                                         
00584                                         temp_profile->name = ((sf::Unicode::UTF32String)temp_profile->name).append(
00585                                                 1, events[i].Text.Unicode);
00586                                         name_string->set_text(temp_profile->name);
00587                                 }
00588                         }
00589                         break;
00590                         default:
00591                         {
00592                                 
00593                         }
00594                         break;
00595                 }
00596         }
00597         
00598         set_color();
00599 }
00600 
00601 void EditProfile::render()
00602 {
00603         viewport.look_2d();
00604         gui.render();
00605 }
00606 
00607 void EditProfile::update(float)
00608 {
00609         
00610 }
00611 
00612 void EditProfile::resize(const sf::Vector2i& s)
00613 {
00614         size = s;
00615         viewport.size = s;
00616         gui.size = s;
00617 }
00618 
00619 EditProfile::~EditProfile()
00620 {
00621         if (profile) // not a typo
00622         {
00623                 delete temp_profile;
00624         }
00625 }
 Összes Osztályok