00001
00002
00003 #include <iostream>
00004 #include <iterator>
00005 #include <stdio.h>
00006 #include "vamosworld.h"
00007 #include "controls.h"
00008 #include "mouse.h"
00009 #include "utility.h"
00010 #include "gui/widget.h"
00011 #include "gui/page.h"
00012 #include "gui/gui.h"
00013 #include "gui/button.h"
00014 #include "gui/calibrate.h"
00015 #include "gui/controlgrab.h"
00016 #include "gui/image.h"
00017 #include "gui/label.h"
00018 #include "gui/multi_image.h"
00019 #include "gui/slider.h"
00020 #include "gui/spinning_car.h"
00021 #include "gui/textbox.h"
00022 #include "gui/toggle.h"
00023 #include "gui/wheel.h"
00024
00025 using namespace VGUI;
00026
00027 extern void Quit( int returnCode );
00028 extern void MainPause();
00029 extern void MainUnpause();
00030 extern void ReloadDisplay();
00031 extern void ResetWorld( bool fullreset );
00032 extern bool LoadWorld();
00033 extern bool UnloadWorld();
00034 extern bool verbose_output;
00035
00036
00037 Gui::Gui()
00038 {
00039 menu_path = settings.GetSkinPath() + "/menus";
00040 loaded = false;
00041 }
00042
00043 Gui::Gui( string path )
00044 {
00045 menu_path = path;
00046 loaded = false;
00047 }
00048
00049 Gui::~Gui()
00050 {
00051 }
00052
00053 void Gui::Init()
00054 {
00055 bool was_loaded = false;
00056 if( loaded )
00057 {
00058 DeInit();
00059 was_loaded = true;
00060 }
00061
00062 string full_menu_path = settings.GetDataDir() + "/" + menu_path;
00063 list<string> menu_list;
00064 utility.GetFolderIndex( full_menu_path, menu_list );
00065
00066 if( verbose_output ) cout << "menu_path: " << menu_path << endl;
00067 list<string>::iterator menu;
00068 for( menu = menu_list.begin(); menu != menu_list.end(); ++menu )
00069 {
00070 pages[*menu] = new Page( settings.GetFullDataPath( menu_path + "/" + *menu ) );
00071 }
00072
00073 menu_enabled = true;
00074 if( !was_loaded ) cur_page = "Main";
00075
00076
00077 settings.Get( "display.width", screen_width );
00078 settings.Get( "display.height", screen_height );
00079
00080
00081 ptr_w = 0.0555;
00082 ptr_h = 0.0740;
00083
00084
00085 mouse_pointer.Load( settings.GetSkinPath() + "/textures/mouse.png", false );
00086 is_locked = l=m=r=u=d=joy_l=joy_r=joy_u=joy_d=key_down=cancel_down = false;
00087
00088 loaded = true;
00089 if( !settings.Get( "joystick.calibrated", calibrated ) )
00090 {
00091 calibrated = false;
00092 settings.Set( "joystick.calibrated", calibrated );
00093 }
00094 }
00095
00096 void Gui::ReInit()
00097 {
00098 if( !loaded )
00099 return;
00100
00101 Init();
00102 menu_enabled = true;
00103 }
00104
00105 void Gui::DeInit()
00106 {
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 map<string,Page*>::iterator page;
00127 for( page = pages.begin(); page != pages.end(); ++page )
00128 {
00129 delete page->second;
00130 }
00131
00132 pages.clear();
00133
00134
00135
00136
00137 menu_enabled = false;
00138
00139 loaded = false;
00140 }
00141
00142 void Gui::UpdateScreenSize()
00143 {
00144
00145 settings.Get( "display.width", screen_width );
00146 settings.Get( "display.height", screen_height );
00147 }
00148
00149 void Gui::Draw()
00150 {
00151 float x, y;
00152 float last_page_opacity = 0.0;
00153 float this_page_opacity = 1.0;
00154
00155
00156
00157
00158 glDisable(GL_LIGHTING);
00159
00160
00161 if( !menu_enabled )
00162 return;
00163
00164 MouseUpdate();
00165
00166
00167 x = (float)mouse_x / (float)screen_width;
00168 y = (float)mouse_y / (float)screen_height;
00169
00170
00171 if( !is_locked )
00172 pages[cur_page]->MouseSelect( x, y );
00173
00174 if( last_page != "" )
00175 {
00176 float elapsed_switch_time = (float)( SDL_GetTicks() - page_switch_time ) / 1000.0;
00177 if( elapsed_switch_time <= 0.3 )
00178 {
00179
00180 this_page_opacity = ( elapsed_switch_time / 0.3 );
00181 last_page_opacity = 1.0 - this_page_opacity;
00182 pages[last_page]->Draw( last_page_opacity );
00183 }
00184 else
00185 {
00186 last_page = "";
00187 }
00188 }
00189
00190
00191 pages[cur_page]->Draw( this_page_opacity );
00192
00193
00194 if( is_locked == false )
00195 utility.Draw2D( x, y, x + ptr_w, y + ptr_h, &mouse_pointer, 0.0, 64, 1.0 );
00196
00197
00198
00199
00200 }
00201
00202 void Gui::KeyPress( SDLKey key )
00203 {
00204
00205
00206 if( !menu_enabled )
00207 return;
00208
00209 if( is_locked == true )
00210 {
00211 pages[cur_page]->ControlGrabKey( key );
00212 is_locked = false;
00213 }
00214 else
00215 {
00216 if( key == SDLK_UP )
00217 {
00218 pages[cur_page]->UpWidget();
00219 }
00220 else if( key == SDLK_DOWN )
00221 {
00222 pages[cur_page]->DownWidget();
00223 }
00224 else if( key == SDLK_LEFT )
00225 {
00226 pages[cur_page]->LeftWidgetPress();
00227 }
00228 else if( key == SDLK_RIGHT )
00229 {
00230 pages[cur_page]->RightWidgetPress();
00231 }
00232 else if( key == SDLK_BACKSPACE )
00233 {
00234 pages[cur_page]->BackspacePress();
00235 }
00236 else if( key == SDLK_DELETE )
00237 {
00238 pages[cur_page]->DeletePress();
00239 }
00240 else if( ( key == SDLK_RETURN ) ||
00241 ( key == SDLK_SPACE ) )
00242 {
00243 pages[cur_page]->KeyPress();
00244 }
00245 else if( key == SDLK_ESCAPE )
00246 {
00247 pages[cur_page]->CancelPress();
00248 }
00249 else
00250 {
00251 pages[cur_page]->Type( key );
00252 }
00253 }
00254 }
00255
00256 void Gui::KeyRelease( SDLKey key )
00257 {
00258 string action = "";
00259
00260
00261 if( !menu_enabled )
00262 return;
00263
00264 if( is_locked == true )
00265 return;
00266
00267 if( ( key == SDLK_RETURN ) ||
00268 ( key == SDLK_SPACE ) )
00269 {
00270 action = pages[cur_page]->KeyRelease();
00271 }
00272 else if( key == SDLK_ESCAPE )
00273 {
00274 action = pages[cur_page]->CancelRelease();
00275 }
00276 else if( key == SDLK_LEFT )
00277 {
00278 pages[cur_page]->LeftWidgetRelease();
00279 }
00280 else if( key == SDLK_RIGHT )
00281 {
00282 pages[cur_page]->RightWidgetRelease();
00283 }
00284 else if( key == SDLK_BACKSPACE )
00285 {
00286 pages[cur_page]->BackspaceRelease();
00287 }
00288 else if( key == SDLK_DELETE )
00289 {
00290 pages[cur_page]->DeleteRelease();
00291 }
00292
00293 ProcessAction( action );
00294 }
00295
00296 void Gui::MousePress()
00297 {
00298 float x, y;
00299
00300
00301 if( !menu_enabled )
00302 return;
00303
00304
00305 mouse.GetMouseButtons( l, m, r, u, d );
00306
00307 mouse.GetMousePos( mouse_x, mouse_y );
00308
00309 x = (float)mouse_x / (float)screen_width;
00310 y = (float)mouse_y / (float)screen_height;
00311
00312 if( is_locked == true )
00313 {
00314 int button_idx = 0;
00315 for( int i = 1; i <= 10; i++ )
00316 {
00317 if( mouse.IsPressed( i ) )
00318 {
00319 button_idx = i;
00320 break;
00321 }
00322 }
00323 if( button_idx == 0 )
00324 {
00325 cout << "Couldn't add button 0!" << endl;
00326 }
00327 else
00328 {
00329 pages[cur_page]->ControlGrabMouseButton( button_idx );
00330 }
00331 is_locked = false;
00332 }
00333 else
00334 {
00335
00336 pages[cur_page]->MouseSelect( x, y );
00337
00338 if( l && pages[cur_page]->MouseOver( x, y ) )
00339 {
00340 pages[cur_page]->MousePressWidget( x, y );
00341 }
00342 }
00343 }
00344
00345 void Gui::MouseRelease()
00346 {
00347 string action = "";
00348 float x, y;
00349
00350
00351 if( !menu_enabled )
00352 return;
00353
00354 if( is_locked == true )
00355 return;
00356
00357
00358 mouse.GetMousePos( mouse_x, mouse_y );
00359
00360 x = (float)mouse_x / (float)screen_width;
00361 y = (float)mouse_y / (float)screen_height;
00362
00363
00364 pages[cur_page]->MouseSelect( x, y );
00365
00366 if( l )
00367 {
00368 action = pages[cur_page]->MouseReleaseWidget( x, y );
00369 ProcessAction( action );
00370 }
00371
00372
00373 mouse.GetMouseButtons( l, m, r, u, d );
00374 }
00375
00376 void Gui::MouseUpdate()
00377 {
00378
00379 mouse.GetMousePos( mouse_x, mouse_y );
00380
00381 if( is_locked == true )
00382 {
00383 float mouse_cur_x = mouse_x / (float)screen_width;
00384 float mouse_cur_y = mouse_y / (float)screen_height;
00385 float lock_x = mouse_lock_x / (float)screen_width;
00386 float lock_y = mouse_lock_y / (float)screen_height;
00387 float moved_x = mouse_cur_x - lock_x;
00388 float moved_y = mouse_cur_y - lock_y;
00389 float distance_x = moved_x > 0 ? moved_x : -moved_x;
00390 float distance_y = moved_y > 0 ? moved_y : -moved_y;
00391
00392
00393 if( distance_x > distance_y )
00394 {
00395
00396 if( distance_x > 0.078 )
00397 {
00398 if( moved_x < 0 )
00399 {
00400 pages[cur_page]->ControlGrabMouseMove( "left" );
00401 }
00402 else
00403 {
00404 pages[cur_page]->ControlGrabMouseMove( "right" );
00405 }
00406 is_locked = false;
00407 }
00408 }
00409 else if( distance_y > distance_x )
00410 {
00411 if( distance_y > 0.097 )
00412 {
00413 if( moved_y < 0 )
00414 {
00415 pages[cur_page]->ControlGrabMouseMove( "up" );
00416 }
00417 else
00418 {
00419 pages[cur_page]->ControlGrabMouseMove( "down" );
00420 }
00421 is_locked = false;
00422 }
00423 }
00424 }
00425 }
00426
00427 void Gui::MouseReturn()
00428 {
00429 SDL_WarpMouse( mouse_x, mouse_y );
00430 }
00431
00432 void Gui::JoyPress( Uint8 joy_idx, Uint8 joy_btn )
00433 {
00434 if( !menu_enabled )
00435 return;
00436
00437 if( !calibrated )
00438 return;
00439
00440 if( is_locked == true )
00441 {
00442 pages[cur_page]->ControlGrabJoyButton( joy_idx, joy_btn );
00443 is_locked = false;
00444 return;
00445 }
00446
00447
00448
00449 switch( joy_btn % 2 )
00450 {
00451 case 0:
00452 key_down = true;
00453 pages[cur_page]->KeyPress();
00454 break;
00455 case 1:
00456 cancel_down = true;
00457 pages[cur_page]->CancelPress();
00458 break;
00459 }
00460 }
00461
00462 void Gui::JoyRelease( Uint8 joy_idx, Uint8 joy_btn )
00463 {
00464 if( !menu_enabled )
00465 return;
00466
00467 if( !calibrated )
00468 return;
00469
00470 if( is_locked == true )
00471 return;
00472
00473
00474
00475 string action = "";
00476
00477 switch( joy_btn % 2 )
00478 {
00479 case 0:
00480 if( key_down )
00481 {
00482 action = pages[cur_page]->KeyRelease();
00483 key_down = false;
00484 }
00485 break;
00486 case 1:
00487 if( cancel_down )
00488 {
00489 action = pages[cur_page]->CancelRelease();
00490 cancel_down = false;
00491 }
00492 break;
00493 }
00494
00495 ProcessAction( action );
00496 }
00497
00498 void Gui::JoyMove( Uint8 joy_idx, Uint8 joy_axis, float val )
00499 {
00500 if( !menu_enabled )
00501 return;
00502
00503 if( ( cur_page == "JoystickCalibrate" ) || ( cur_page == "InGameJoystickCalibrate" ) )
00504 {
00505 calibrated = false;
00506 pages[cur_page]->ControlGrabJoyMove( joy_idx, joy_axis, joy_positive, val );
00507 }
00508
00509 if( !calibrated )
00510 return;
00511
00512 joy_axis_value = val;
00513 last_axis = joy_axis;
00514 last_joy = joy_idx;
00515
00516 if( is_locked == true )
00517 {
00518 if( joy_axis_value < -0.2 )
00519 {
00520 joy_press_time = SDL_GetTicks();
00521 joy_positive = false;
00522 }
00523 else if( joy_axis_value > 0.2 )
00524 {
00525 joy_press_time = SDL_GetTicks();
00526 joy_positive = true;
00527 }
00528 }
00529
00530
00531
00532 switch( last_axis % 2 )
00533 {
00534 case 0:
00535 if( !joy_l && ( joy_axis_value < -0.2 ) )
00536 {
00537 joy_press_time = SDL_GetTicks();
00538 joy_l = true;
00539 if( !is_locked ) pages[cur_page]->LeftWidgetPress();
00540 }
00541 else if( !joy_r && ( joy_axis_value > 0.2 ) )
00542 {
00543 joy_press_time = SDL_GetTicks();
00544 joy_r = true;
00545 if( !is_locked ) pages[cur_page]->RightWidgetPress();
00546 }
00547 break;
00548 case 1:
00549 if( !joy_u && ( joy_axis_value < -0.2 ) )
00550 {
00551 joy_press_time = SDL_GetTicks();
00552 joy_u = true;
00553 if( !is_locked ) pages[cur_page]->UpWidget();
00554 }
00555 else if( !joy_d && ( joy_axis_value > 0.2 ) )
00556 {
00557 joy_press_time = SDL_GetTicks();
00558 joy_d = true;
00559 if( !is_locked ) pages[cur_page]->DownWidget();
00560 }
00561 break;
00562 }
00563 }
00564
00565 void Gui::JoyUpdate()
00566 {
00567 if( !calibrated )
00568 return;
00569
00570 Uint32 time_since_joy_press = SDL_GetTicks() - joy_press_time;
00571 float sec_since_move = (float)( (float)time_since_joy_press / 1000.0 );
00572
00573
00574 if( sec_since_move >= 0.3 )
00575 {
00576 if( ( is_locked == true ) && ( joy_u || joy_d || joy_l || joy_r ) )
00577 {
00578 pages[cur_page]->ControlGrabJoyMove( last_joy, last_axis, joy_positive, joy_axis_value );
00579 is_locked = joy_u = joy_d = joy_l = joy_r = false;
00580 return;
00581 }
00582
00583 if( joy_u )
00584 {
00585 joy_u = false;
00586 }
00587
00588 if( joy_d )
00589 {
00590 joy_d = false;
00591 }
00592
00593 if( joy_l )
00594 {
00595 pages[cur_page]->LeftWidgetRelease();
00596 joy_l = false;
00597 }
00598
00599 if( joy_r )
00600 {
00601 pages[cur_page]->RightWidgetRelease();
00602 joy_r = false;
00603 }
00604 }
00605 }
00606
00607 void Gui::ProcessAction( string action )
00608 {
00609 if( action == "" )
00610 {
00611 return;
00612 }
00613 else if( action == "Quit" )
00614 {
00615 Quit(0);
00616 }
00617 else if( action == "Lock" )
00618 {
00619
00620 mouse_lock_x = mouse_x;
00621 mouse_lock_y = mouse_y;
00622 is_locked = true;
00623 }
00624 else if( action == "Unlock" )
00625 {
00626 is_locked = false;
00627 }
00628 else if( action == "ReloadDisplay" )
00629 {
00630 ReloadDisplay();
00631
00632 if( cur_page == "DisplayOptions" )
00633 ChangePage( "Options" );
00634 else if( cur_page == "InGameDisplayOptions" )
00635 ChangePage( "InGameOptions" );
00636 }
00637 else if( action == "UpdateMouse" )
00638 {
00639 mouse.UpdateSettings();
00640
00641 if( cur_page == "MouseOptions" )
00642 ChangePage( "ControlsOptions" );
00643 else if( cur_page == "InGameMouseOptions" )
00644 ChangePage( "InGameControlsOptions" );
00645 }
00646 else if( action == "UpdateJoystick" )
00647 {
00648 gamecontrols.UpdateSettings();
00649
00650 if( cur_page == "JoystickOptions" )
00651 ChangePage( "ControlsOptions" );
00652 else if( cur_page == "InGameJoystickOptions" )
00653 ChangePage( "InGameControlsOptions" );
00654 }
00655 else if( action == "UpdateCalibrations" )
00656 {
00657 calibrated = true;
00658 settings.Set( "joystick.calibrated", calibrated );
00659
00660 if( cur_page == "JoystickCalibrate" )
00661 ChangePage( "JoystickOptions" );
00662 else if( cur_page == "InGameJoystickCalibrate" )
00663 ChangePage( "InGameJoystickOptions" );
00664 }
00665 else if( action == "UpdateControls" )
00666 {
00667 gamecontrols.UpdateSettings();
00668 world.UpdateSettings();
00669
00670 if( cur_page == "ControlsOptions" )
00671 ChangePage( "Options" );
00672 else if( cur_page == "InGameControlsOptions" )
00673 ChangePage( "InGameOptions" );
00674 }
00675 else if( action == "UpdateSound" )
00676 {
00677 sound.UpdateSettings();
00678 world.UpdateSettings();
00679
00680 if( cur_page == "SoundOptions" )
00681 ChangePage( "Options" );
00682 else if( cur_page == "InGameSoundOptions" )
00683 ChangePage( "InGameOptions" );
00684 }
00685 else if( action == "Pause" )
00686 {
00687 ChangePage( "InGameMenu" );
00688 last_page = "";
00689
00690 pages[cur_page]->SelectDefault();
00691
00692 menu_enabled = true;
00693 }
00694 else if( action == "StartPracticeGame" )
00695 {
00696 pages[cur_page]->SelectDefault();
00697 LoadWorld();
00698 state.SetGameState( STATE_PLAYING );
00699 menu_enabled = false;
00700 pages[cur_page]->StartGame();
00701 bool record_replay = false;
00702 if( settings.Get( "game.record", record_replay ) )
00703 {
00704 if( record_replay )
00705 {
00706 replay.Start();
00707 }
00708 }
00709 else
00710 {
00711 settings.Set( "game.record", false );
00712 }
00713 }
00714 else if( action == "StartNetworkGame" )
00715 {
00716 pages[cur_page]->SelectDefault();
00717 state.SetGameMode( MODE_NETMULTIFREE );
00718 pages[cur_page]->StartGame();
00719
00720 bool net_host, net_success;
00721 string net_ip;
00722 settings.Get( "network.host_game", net_host );
00723 settings.Get( "network.server_ip", net_ip );
00724
00725 multiplay.Disconnect();
00726
00727 if( !net_host )
00728 {
00729 net_success = multiplay.Connect( net_ip );
00730 }
00731 else
00732 {
00733 net_success = multiplay.Host();
00734 LoadWorld();
00735 }
00736
00737 if( net_success )
00738 {
00739 menu_enabled = false;
00740 state.SetGameState( STATE_PLAYING );
00741 }
00742 else
00743 {
00744 ChangePage( "ConnectionError" );
00745 }
00746 }
00747 else if( action == "StartReplay" )
00748 {
00749 pages[cur_page]->SelectDefault();
00750 menu_enabled = false;
00751 if( !replay.PlayStart("1") )
00752 {
00753 ChangePage( "ReplayStartError" );
00754 menu_enabled = true;
00755 }
00756 }
00757 else if( action == "ReturnToGame" )
00758 {
00759 pages[cur_page]->SelectDefault();
00760 menu_enabled = false;
00761 MainUnpause();
00762 }
00763 else if( action == "RestartGame" )
00764 {
00765 pages[cur_page]->SelectDefault();
00766 menu_enabled = false;
00767 MainUnpause();
00768 ResetWorld( true );
00769 }
00770 else if( action == "LeaveGame" )
00771 {
00772
00773
00774 replay.Stop();
00775 replay.PlayStop();
00776 MainUnpause();
00777 UnloadWorld();
00778 state.SetGameState( STATE_INITIALMENU );
00779 ChangePage( "Main" );
00780
00781 }
00782 else
00783 {
00784 pages[cur_page]->SelectDefault();
00785
00786 ChangePage( action );
00787 }
00788 }
00789
00790 void Gui::ChangePage( string page_name )
00791 {
00792 page_switch_time = SDL_GetTicks();
00793 last_page = cur_page;
00794 cur_page = page_name;
00795 pages[cur_page]->ClearSettings();
00796 }