00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
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 }
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 }