src/gui/question.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Display a text during the game, waiting for input by the user
00020  *****************************************************************************/
00021 
00022 #include "question.h"
00023 #include <SDL.h>
00024 #include "../graphic/text.h"
00025 #include "../graphic/font.h"
00026 #include "../graphic/video.h"
00027 #include "../include/app.h"
00028 #include "../interface/interface.h"
00029 #include "../interface/mouse.h"
00030 #include "../map/map.h"
00031 #include "../tool/resource_manager.h"
00032 
00033 Question::Question()
00034 {
00035   background = NULL;
00036 }
00037 
00038 Question::~Question()
00039 {
00040   if(background != NULL)
00041     delete background;
00042 }
00043 
00044 int Question::TreatsKey (SDL_Event &event){
00045 
00046   // Tests the key
00047   choice_iterator it=choices.begin(), end=choices.end();
00048   for (; it != end; ++it){
00049     if (event.key.keysym.sym == it -> key()) {
00050       return it -> val();
00051     }
00052   }
00053 
00054   // No key corresponding to the correct choice, so we use default choice
00055   if (default_choice.active) {
00056     return default_choice.value;
00057   }
00058 
00059   return -1;
00060 }
00061 
00062 void Question::Draw() const{
00063   AppWormux * app = AppWormux::GetInstance();
00064 
00065   if(background != NULL)
00066   {
00067     background->Blit(app->video.window,  app->video.window.GetSize() / 2 - background->GetSize() / 2);
00068   }
00069   if(message != "")
00070   {
00071     DrawTmpBoxTextWithReturns (*Font::GetInstance(Font::FONT_BIG),
00072                                app->video.window.GetSize() / 2,
00073                                message, 10);
00074   }
00075 }
00076 
00077 int Question::Ask () {
00078   SDL_Event event;
00079 
00080   int answer = default_choice.value;
00081   bool end_of_boucle = false;
00082 
00083   Draw();
00084   Mouse::pointer_t prev_pointer = Mouse::GetInstance()->SetPointer(Mouse::POINTER_STANDARD);
00085   do{
00086     while( SDL_PollEvent( &event) ){
00087       if ( (event.type == SDL_QUIT || event.type == SDL_MOUSEBUTTONDOWN) &&
00088           default_choice.active ){
00089         answer = default_choice.value;
00090         end_of_boucle = true;
00091       }
00092 
00093       if (event.type == SDL_KEYUP) {
00094         answer = TreatsKey(event);
00095         if (answer != -1) 
00096           end_of_boucle = true;
00097       }
00098     } // SDL_PollEvent
00099 
00100     AppWormux::GetInstance()->video.Flip();
00101   } while (!end_of_boucle);
00102 
00103   Mouse::GetInstance()->SetPointer(prev_pointer);
00104 
00105   return answer;
00106 }
00107 
00108 void Question::Set (const std::string &pmessage,
00109                     bool default_active, int default_value,const std::string& bg_sprite){
00110   message = pmessage;
00111   default_choice.active = default_active;
00112   default_choice.value = default_value;
00113 
00114   if(background != NULL)
00115   {
00116     delete background;
00117     background = NULL;
00118   }
00119 
00120   if(bg_sprite != "")
00121   {
00122     Profile *res = resource_manager.LoadXMLProfile( "graphism.xml", false);
00123     background = new Sprite(resource_manager.LoadImage(res,bg_sprite));
00124     background->cache.EnableLastFrameCache();
00125     background->ScaleSize(AppWormux::GetInstance()->video.window.GetSize());
00126     resource_manager.UnLoadXMLProfile( res);
00127   }
00128 }

Generated on Mon Jan 1 13:10:57 2007 for Wormux by  doxygen 1.4.7