00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "menu.h"
00023 #include "../graphic/sprite.h"
00024 #include "../graphic/video.h"
00025 #include "../include/app.h"
00026 #include "../tool/resource_manager.h"
00027 #include "../sound/jukebox.h"
00028
00029 Menu::Menu(char * bg, t_action _actions) :
00030 actions(_actions)
00031 {
00032 close_menu = false ;
00033 AppWormux * app = AppWormux::GetInstance();
00034
00035 uint x = app->video.window.GetWidth() / 2;
00036 uint y = app->video.window.GetHeight() - 50;
00037
00038 Profile *res = resource_manager.LoadXMLProfile( "graphism.xml", false);
00039 background = new Sprite( resource_manager.LoadImage( res, bg));
00040 background->cache.EnableLastFrameCache();
00041
00042 b_ok = NULL;
00043 b_cancel = NULL;
00044 if (actions == vNo) {
00045 actions_buttons = NULL;
00046 } else {
00047
00048 actions_buttons = new HBox( Rectanglei(x, y, 1, 40), false);
00049
00050 if (actions == vOk || actions == vOkCancel) {
00051 b_ok = new Button( Point2i(0, 0), res, "menu/valider");
00052 actions_buttons->AddWidget(b_ok);
00053 }
00054
00055 if (actions == vCancel || actions == vOkCancel) {
00056 b_cancel = new Button( Point2i(0, 0), res, "menu/annuler");
00057 actions_buttons->AddWidget(b_cancel);
00058 }
00059
00060 widgets.AddWidget(actions_buttons);
00061 }
00062
00063 widgets.SetContainer(this);
00064 resource_manager.UnLoadXMLProfile(res);
00065 }
00066
00067 Menu::~Menu()
00068 {
00069 delete background;
00070 }
00071
00072 void Menu::sig_ok()
00073 {
00074 jukebox.Play("share", "menu/ok");
00075 __sig_ok();
00076 close_menu = true;
00077 }
00078
00079 void Menu::sig_cancel()
00080 {
00081 jukebox.Play("share", "menu/cancel");
00082 __sig_cancel();
00083 close_menu = true;
00084 }
00085
00086 bool Menu::BasicOnClic(const Point2i &mousePosition)
00087 {
00088 if( b_ok != NULL && b_ok->Contains(mousePosition) )
00089 sig_ok();
00090 else if( b_cancel != NULL && b_cancel->Contains(mousePosition) )
00091 sig_cancel();
00092 else
00093 return false;
00094
00095 return true;
00096 }
00097
00098 void Menu::DrawBackground(const Point2i &mousePosition)
00099 {
00100 background->ScaleSize(AppWormux::GetInstance()->video.window.GetSize());
00101 background->Blit(AppWormux::GetInstance()->video.window, 0, 0);
00102 }
00103
00104 void Menu::Redraw(const Rectanglei& rect, Surface& surf)
00105 {
00106 background->Blit(surf, rect, rect.GetPosition());
00107 }
00108
00109 void Menu::Run ()
00110 {
00111 int x=0, y=0;
00112
00113 close_menu = false;
00114
00115
00116 DrawBackground(Point2i(0,0));
00117
00118 do
00119 {
00120
00121 SDL_Event event;
00122
00123 while( SDL_PollEvent( &event) )
00124 {
00125 Point2i mousePosition(event.button.x, event.button.y);
00126
00127 if( event.type == SDL_QUIT )
00128 sig_cancel();
00129 else if( event.type == SDL_KEYDOWN )
00130 switch( event.key.keysym.sym)
00131 {
00132 case SDLK_ESCAPE:
00133 if(b_cancel != NULL)
00134 sig_cancel();
00135 else
00136 key_cancel();
00137 break;
00138 case SDLK_RETURN:
00139 if(b_ok != NULL)
00140 sig_ok();
00141 else
00142 key_ok();
00143 break;
00144 case SDLK_F10:
00145 AppWormux::GetInstance()->video.ToggleFullscreen();
00146 break;
00147 default:
00148 widgets.SendKey(event.key.keysym);
00149 break;
00150 }
00151 else if( event.type == SDL_MOUSEBUTTONUP)
00152 if( !BasicOnClic(mousePosition) )
00153 OnClic(mousePosition, event.button.button);
00154 }
00155
00156
00157 if (!close_menu) {
00158
00159 SDL_GetMouseState( &x, &y );
00160 Point2i mousePosition(x, y);
00161
00162 Display(mousePosition);
00163 }
00164
00165 } while (!close_menu);
00166 }
00167
00168 void Menu::Display(const Point2i& mousePosition)
00169 {
00170
00171 uint sleep_fps=0;
00172 uint delay = 0;
00173 uint start = SDL_GetTicks();
00174
00175 widgets.Update(mousePosition, AppWormux::GetInstance()->video.window);
00176 Draw(mousePosition);
00177 AppWormux::GetInstance()->video.Flip();
00178
00179
00180 delay = SDL_GetTicks()-start;
00181 if (delay < 200)
00182 sleep_fps = 200 - delay;
00183 else
00184 sleep_fps = 0;
00185 SDL_Delay(sleep_fps);
00186 }
00187
00188 void Menu::SetActionButtonsXY(int x, int y){
00189 if (actions_buttons != NULL)
00190 actions_buttons->SetSizePosition( Rectanglei(x, y, actions_buttons->GetSizeX(), actions_buttons->GetSizeY()) );
00191 }