MenuHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #pragma hdrstop
  21. #include "../../idLib/precompiled.h"
  22. #include "../Game_local.h"
  23. extern idCVar in_useJoystick;
  24. /*
  25. ================================================
  26. idMenuHandler::~idMenuHandler
  27. ================================================
  28. */
  29. idMenuHandler::idMenuHandler() {
  30. scrollingMenu = false;
  31. scrollCounter = 0;
  32. activeScreen = -1;
  33. nextScreen = -1;
  34. transition = -1;
  35. platform = 0;
  36. gui = NULL;
  37. cmdBar = NULL;
  38. for ( int index = 0; index < MAX_SCREEN_AREAS; ++index ) {
  39. menuScreens[ index ] = NULL;
  40. }
  41. sounds.SetNum( NUM_GUI_SOUNDS );
  42. }
  43. /*
  44. ================================================
  45. idMenuHandler::~idMenuHandler
  46. ================================================
  47. */
  48. idMenuHandler::~idMenuHandler() {
  49. Cleanup();
  50. }
  51. /*
  52. ================================================
  53. idMenuHandler::Initialize
  54. ================================================
  55. */
  56. void idMenuHandler::Initialize( const char * swfFile, idSoundWorld * sw ) {
  57. Cleanup();
  58. gui = new idSWF( swfFile, sw );
  59. platform = 2;
  60. }
  61. /*
  62. ================================================
  63. idMenuHandler::AddChild
  64. ================================================
  65. */
  66. void idMenuHandler::AddChild( idMenuWidget * widget ) {
  67. widget->SetSWFObj( gui );
  68. widget->SetHandlerIsParent( true );
  69. children.Append( widget );
  70. widget->AddRef();
  71. }
  72. /*
  73. ================================================
  74. idMenuHandler::GetChildFromIndex
  75. ================================================
  76. */
  77. idMenuWidget * idMenuHandler::GetChildFromIndex( int index ) {
  78. if ( children.Num() == 0 ) {
  79. return NULL;
  80. }
  81. if ( index > children.Num() ) {
  82. return NULL;
  83. }
  84. return children[ index ];
  85. }
  86. /*
  87. ================================================
  88. idMenuHandler::GetPlatform
  89. ================================================
  90. */
  91. int idMenuHandler::GetPlatform( bool realPlatform ) {
  92. if ( platform == 2 && in_useJoystick.GetBool() && !realPlatform ) {
  93. return 0;
  94. }
  95. return platform;
  96. }
  97. /*
  98. ================================================
  99. idMenuHandler::GetPlatform
  100. ================================================
  101. */
  102. void idMenuHandler::PlaySound( menuSounds_t type, int channel ) {
  103. if ( gui == NULL ) {
  104. return;
  105. }
  106. if ( type >= sounds.Num() ) {
  107. return;
  108. }
  109. if ( sounds[ type ].IsEmpty() ) {
  110. return;
  111. }
  112. int c = SCHANNEL_ANY;
  113. if ( channel != -1 ) {
  114. c = channel;
  115. }
  116. gui->PlaySound( sounds[ type ], c );
  117. }
  118. /*
  119. ================================================
  120. idMenuHandler::StopSound
  121. ================================================
  122. */
  123. void idMenuHandler::StopSound( int channel ) {
  124. gui->StopSound();
  125. }
  126. /*
  127. ================================================
  128. idMenuHandler::Cleanup
  129. ================================================
  130. */
  131. void idMenuHandler::Cleanup() {
  132. for ( int index = 0; index < children.Num(); ++index ) {
  133. assert( children[ index ]->GetRefCount() > 0 );
  134. children[ index ]->Release();
  135. }
  136. children.Clear();
  137. for ( int index = 0; index < MAX_SCREEN_AREAS; ++index ) {
  138. if ( menuScreens[ index ] != NULL ) {
  139. menuScreens[ index ]->Release();
  140. }
  141. }
  142. delete gui;
  143. gui = NULL;
  144. }
  145. /*
  146. ================================================
  147. idMenuHandler::TriggerMenu
  148. ================================================
  149. */
  150. void idMenuHandler::TriggerMenu() {
  151. }
  152. /*
  153. ================================================
  154. idMenuHandler::IsActive
  155. ================================================
  156. */
  157. bool idMenuHandler::IsActive() {
  158. if ( gui == NULL ) {
  159. return false;
  160. }
  161. return gui->IsActive();
  162. }
  163. /*
  164. ================================================
  165. idMenuHandler::ActivateMenu
  166. ================================================
  167. */
  168. void idMenuHandler::ActivateMenu( bool show ) {
  169. if ( gui == NULL ) {
  170. return;
  171. }
  172. if ( !show ) {
  173. gui->Activate( show );
  174. return;
  175. }
  176. class idSWFScriptFunction_updateMenuDisplay : public idSWFScriptFunction_RefCounted {
  177. public:
  178. idSWFScriptFunction_updateMenuDisplay( idSWF * _gui, idMenuHandler * _handler ) {
  179. gui = _gui;
  180. handler = _handler;
  181. }
  182. idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ) {
  183. if ( handler != NULL ) {
  184. int screen = parms[0].ToInteger();
  185. handler->UpdateMenuDisplay( screen );
  186. }
  187. return idSWFScriptVar();
  188. }
  189. private:
  190. idSWF * gui;
  191. idMenuHandler * handler;
  192. };
  193. class idSWFScriptFunction_activateMenu : public idSWFScriptFunction_RefCounted {
  194. public:
  195. idSWFScriptFunction_activateMenu( idMenuHandler * _handler ) {
  196. handler = _handler;
  197. }
  198. idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ) {
  199. if ( handler != NULL ) {
  200. handler->TriggerMenu();
  201. }
  202. return idSWFScriptVar();
  203. }
  204. private:
  205. idMenuHandler * handler;
  206. };
  207. gui->SetGlobal( "updateMenuDisplay", new (TAG_SWF) idSWFScriptFunction_updateMenuDisplay( gui, this ) );
  208. gui->SetGlobal( "activateMenus", new (TAG_SWF) idSWFScriptFunction_activateMenu( this ) );
  209. gui->Activate( show );
  210. }
  211. /*
  212. ================================================
  213. idMenuHandler::Update
  214. ================================================
  215. */
  216. void idMenuHandler::Update() {
  217. PumpWidgetActionRepeater();
  218. if ( gui != NULL && gui->IsActive() ) {
  219. gui->Render( renderSystem, Sys_Milliseconds() );
  220. }
  221. }
  222. /*
  223. ================================================
  224. idMenuHandler::UpdateChildren
  225. ================================================
  226. */
  227. void idMenuHandler::UpdateChildren() {
  228. for ( int index = 0; index < children.Num(); ++index ) {
  229. if ( children[ index ] != NULL ) {
  230. children[index]->Update();
  231. }
  232. }
  233. }
  234. /*
  235. ================================================
  236. idMenuHandler::UpdateMenuDisplay
  237. ================================================
  238. */
  239. void idMenuHandler::UpdateMenuDisplay( int menu ) {
  240. if ( menuScreens[ menu ] != NULL ) {
  241. menuScreens[ menu ]->Update();
  242. }
  243. UpdateChildren();
  244. }
  245. /*
  246. ================================================
  247. idMenuHandler::Update
  248. ================================================
  249. */
  250. bool idMenuHandler::HandleGuiEvent( const sysEvent_t * sev ) {
  251. if ( gui != NULL && activeScreen != -1 ) {
  252. return gui->HandleEvent( sev );
  253. }
  254. return false;
  255. }
  256. /*
  257. ================================================
  258. idMenuHandler::Update
  259. ================================================
  260. */
  261. bool idMenuHandler::HandleAction( idWidgetAction & action, const idWidgetEvent & event, idMenuWidget * widget, bool forceHandled ) {
  262. widgetAction_t actionType = action.GetType();
  263. const idSWFParmList & parms = action.GetParms();
  264. switch ( actionType ) {
  265. case WIDGET_ACTION_ADJUST_FIELD: {
  266. if ( widget != NULL && widget->GetDataSource() != NULL ) {
  267. widget->GetDataSource()->AdjustField( widget->GetDataSourceFieldIndex(), parms[ 0 ].ToInteger() );
  268. widget->Update();
  269. }
  270. return true;
  271. }
  272. case WIDGET_ACTION_FUNCTION: {
  273. if ( verify( action.GetScriptFunction() != NULL ) ) {
  274. action.GetScriptFunction()->Call( event.thisObject, event.parms );
  275. }
  276. return true;
  277. }
  278. case WIDGET_ACTION_PRESS_FOCUSED: {
  279. idMenuScreen * const screen = menuScreens[ activeScreen ];
  280. if ( screen != NULL ) {
  281. idWidgetEvent pressEvent( WIDGET_EVENT_PRESS, 0, event.thisObject, idSWFParmList() );
  282. screen->ReceiveEvent( pressEvent );
  283. }
  284. return true;
  285. }
  286. case WIDGET_ACTION_START_REPEATER: {
  287. idWidgetAction repeatAction;
  288. widgetAction_t repeatActionType = static_cast< widgetAction_t >( parms[ 0 ].ToInteger() );
  289. assert( parms.Num() >= 2 );
  290. int repeatDelay = DEFAULT_REPEAT_TIME;
  291. if ( parms.Num() >= 3 ) {
  292. repeatDelay = parms[2].ToInteger();
  293. }
  294. repeatAction.Set( repeatActionType, parms[ 1 ], repeatDelay );
  295. StartWidgetActionRepeater( widget, repeatAction, event );
  296. return true;
  297. }
  298. case WIDGET_ACTION_STOP_REPEATER: {
  299. ClearWidgetActionRepeater();
  300. return true;
  301. }
  302. }
  303. if ( !widget->GetHandlerIsParent() ) {
  304. for ( int index = 0; index < children.Num(); ++index ) {
  305. if ( children[index] != NULL ) {
  306. if ( children[index]->HandleAction( action, event, widget, forceHandled ) ) {
  307. return true;
  308. }
  309. }
  310. }
  311. }
  312. return false;
  313. }
  314. /*
  315. ========================
  316. idMenuHandler::StartWidgetActionRepeater
  317. ========================
  318. */
  319. void idMenuHandler::StartWidgetActionRepeater( idMenuWidget * widget, const idWidgetAction & action, const idWidgetEvent & event ) {
  320. if ( actionRepeater.isActive && actionRepeater.action == action ) {
  321. return; // don't attempt to reactivate an already active repeater
  322. }
  323. actionRepeater.isActive = true;
  324. actionRepeater.action = action;
  325. actionRepeater.widget = widget;
  326. actionRepeater.event = event;
  327. actionRepeater.numRepetitions = 0;
  328. actionRepeater.nextRepeatTime = 0;
  329. actionRepeater.screenIndex = activeScreen; // repeaters are cleared between screens
  330. if ( action.GetParms().Num() == 2 ) {
  331. actionRepeater.repeatDelay = action.GetParms()[ 1 ].ToInteger();
  332. } else {
  333. actionRepeater.repeatDelay = DEFAULT_REPEAT_TIME;
  334. }
  335. // do the first event immediately
  336. PumpWidgetActionRepeater();
  337. }
  338. /*
  339. ========================
  340. idMenuHandler::PumpWidgetActionRepeater
  341. ========================
  342. */
  343. void idMenuHandler::PumpWidgetActionRepeater() {
  344. if ( !actionRepeater.isActive ) {
  345. return;
  346. }
  347. if ( activeScreen != actionRepeater.screenIndex || nextScreen != activeScreen ) { // || common->IsDialogActive() ) {
  348. actionRepeater.isActive = false;
  349. return;
  350. }
  351. if ( actionRepeater.nextRepeatTime > Sys_Milliseconds() ) {
  352. return;
  353. }
  354. // need to hold down longer on the first iteration before we continue to scroll
  355. if ( actionRepeater.numRepetitions == 0 ) {
  356. actionRepeater.nextRepeatTime = Sys_Milliseconds() + 400;
  357. } else {
  358. actionRepeater.nextRepeatTime = Sys_Milliseconds() + actionRepeater.repeatDelay;
  359. }
  360. if ( verify( actionRepeater.widget != NULL ) ) {
  361. actionRepeater.widget->HandleAction( actionRepeater.action, actionRepeater.event, actionRepeater.widget );
  362. actionRepeater.numRepetitions++;
  363. }
  364. }
  365. /*
  366. ========================
  367. idMenuHandler::ClearWidgetActionRepeater
  368. ========================
  369. */
  370. void idMenuHandler::ClearWidgetActionRepeater() {
  371. actionRepeater.isActive = false;
  372. }