MapSelectMenu.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: MapSelectMenu.cpp ////////////////////////////////////////////////////////////////////////
  24. // Author: Colin Day, October 2001
  25. // Description: MapSelect menu window callbacks
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  28. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  29. #include "Common/GameEngine.h"
  30. #include "Common/MessageStream.h"
  31. #include "Common/RandomValue.h"
  32. #include "Common/UserPreferences.h"
  33. #include "GameLogic/GameLogic.h"
  34. #include "GameLogic/ScriptEngine.h"
  35. #include "GameClient/AnimateWindowManager.h"
  36. #include "GameClient/CampaignManager.h"
  37. #include "GameClient/WindowLayout.h"
  38. #include "GameClient/Gadget.h"
  39. #include "GameClient/Shell.h"
  40. #include "GameClient/GameWindowManager.h"
  41. #include "GameClient/GadgetListBox.h"
  42. #include "GameClient/GadgetRadioButton.h"
  43. #include "GameClient/MapUtil.h"
  44. #include "GameClient/Mouse.h"
  45. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  46. static NameKeyType radioButtonSystemMapsID = NAMEKEY_INVALID;
  47. static NameKeyType radioButtonUserMapsID = NAMEKEY_INVALID;
  48. static GameWindow *mapList = NULL;
  49. static Bool showSoloMaps = true;
  50. static Bool isShuttingDown = false;
  51. static Bool startGame = false;
  52. static Bool buttonPushed = false;
  53. static GameDifficulty s_AIDiff = DIFFICULTY_NORMAL;
  54. static void setupGameStart(AsciiString mapName)
  55. {
  56. startGame = true;
  57. TheWritableGlobalData->m_pendingFile = mapName;
  58. TheShell->reverseAnimatewindow();
  59. }
  60. static void doGameStart( void )
  61. {
  62. startGame = false;
  63. if (TheGameLogic->isInGame())
  64. TheGameLogic->clearGameData();
  65. //TheScriptEngine->setGlobalDifficulty(s_AIDiff); // CANNOT DO THIS! REPLAYS WILL BREAK!!!
  66. // send a message to the logic for a new game
  67. GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
  68. msg->appendIntegerArgument(GAME_SINGLE_PLAYER);
  69. msg->appendIntegerArgument(s_AIDiff);
  70. msg->appendIntegerArgument(0);
  71. /// @todo: when Campaign & skirmish are separated, make campaign have fixed seed and skirmish random.
  72. InitRandom(0);
  73. /*
  74. if (TheGlobalData->m_fixedSeed >= 0)
  75. InitGameLogicRandom(TheGlobalData->m_fixedSeed);
  76. else
  77. InitGameLogicRandom(GameClientRandomValue(0, INT_MAX - 1));
  78. */
  79. isShuttingDown = true;
  80. }
  81. //-------------------------------------------------------------------------------------------------
  82. /** This is called when a shutdown is complete for this menu */
  83. //-------------------------------------------------------------------------------------------------
  84. static void shutdownComplete( WindowLayout *layout )
  85. {
  86. isShuttingDown = false;
  87. // hide the layout
  88. layout->hide( TRUE );
  89. // our shutdown is complete
  90. TheShell->shutdownComplete( layout );
  91. } // end if
  92. void SetDifficultyRadioButton( void )
  93. {
  94. AsciiString parentName( "MapSelectMenu.wnd:MapSelectMenuParent" );
  95. NameKeyType parentID = TheNameKeyGenerator->nameToKey( parentName );
  96. GameWindow *parent = TheWindowManager->winGetWindowFromId( NULL, parentID );
  97. if (!TheScriptEngine)
  98. {
  99. s_AIDiff = DIFFICULTY_EASY;
  100. }
  101. else
  102. {
  103. switch (TheScriptEngine->getGlobalDifficulty())
  104. {
  105. case DIFFICULTY_EASY:
  106. {
  107. NameKeyType radioButtonEasyAIID = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:RadioButtonEasyAI") );
  108. GameWindow *radioButtonEasyAI = TheWindowManager->winGetWindowFromId( parent, radioButtonEasyAIID );
  109. GadgetRadioSetSelection(radioButtonEasyAI, FALSE);
  110. s_AIDiff = DIFFICULTY_EASY;
  111. break;
  112. }
  113. case DIFFICULTY_NORMAL:
  114. {
  115. NameKeyType radioButtonMediumAIID = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:RadioButtonMediumAI") );
  116. GameWindow *radioButtonMediumAI = TheWindowManager->winGetWindowFromId( parent, radioButtonMediumAIID );
  117. GadgetRadioSetSelection(radioButtonMediumAI, FALSE);
  118. s_AIDiff = DIFFICULTY_NORMAL;
  119. break;
  120. }
  121. case DIFFICULTY_HARD:
  122. {
  123. NameKeyType radioButtonHardAIID = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:RadioButtonHardAI") );
  124. GameWindow *radioButtonHardAI = TheWindowManager->winGetWindowFromId( parent, radioButtonHardAIID );
  125. GadgetRadioSetSelection(radioButtonHardAI, FALSE);
  126. s_AIDiff = DIFFICULTY_HARD;
  127. break;
  128. }
  129. default:
  130. {
  131. DEBUG_CRASH(("unrecognized difficulty level in the script engine"));
  132. }
  133. }
  134. } // if (TheScriptEngine)
  135. }
  136. //-------------------------------------------------------------------------------------------------
  137. /** Initialize the MapSelect menu */
  138. //-------------------------------------------------------------------------------------------------
  139. void MapSelectMenuInit( WindowLayout *layout, void *userData )
  140. {
  141. showSoloMaps = true;
  142. buttonPushed = false;
  143. isShuttingDown = false;
  144. startGame = false;
  145. TheShell->showShellMap(TRUE);
  146. // show menu
  147. layout->hide( FALSE );
  148. OptionPreferences pref;
  149. Bool usesSystemMapDir = pref.usesSystemMapDir();
  150. // get the listbox window
  151. AsciiString listString( "MapSelectMenu.wnd:ListboxMap" );
  152. NameKeyType mapListID = TheNameKeyGenerator->nameToKey( listString );
  153. mapList = TheWindowManager->winGetWindowFromId( NULL, mapListID );
  154. if( mapList )
  155. {
  156. if (TheMapCache)
  157. TheMapCache->updateCache();
  158. populateMapListbox( mapList, usesSystemMapDir, !showSoloMaps );
  159. }
  160. // set keyboard focus to main parent
  161. AsciiString parentName( "MapSelectMenu.wnd:MapSelectMenuParent" );
  162. NameKeyType parentID = TheNameKeyGenerator->nameToKey( parentName );
  163. GameWindow *parent = TheWindowManager->winGetWindowFromId( NULL, parentID );
  164. TheWindowManager->winSetFocus( parent );
  165. NameKeyType buttonBackID = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:ButtonBack") );
  166. GameWindow *buttonBack = TheWindowManager->winGetWindowFromId( NULL, buttonBackID );
  167. NameKeyType buttonOKID = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:ButtonOK") );
  168. GameWindow *buttonOK = TheWindowManager->winGetWindowFromId( NULL, buttonOKID );
  169. TheShell->registerWithAnimateManager(buttonBack, WIN_ANIMATION_SLIDE_RIGHT, TRUE,0);
  170. TheShell->registerWithAnimateManager(buttonOK, WIN_ANIMATION_SLIDE_LEFT, TRUE, 0);
  171. SetDifficultyRadioButton();
  172. radioButtonSystemMapsID = TheNameKeyGenerator->nameToKey( "MapSelectMenu.wnd:RadioButtonSystemMaps" );
  173. radioButtonUserMapsID = TheNameKeyGenerator->nameToKey( "MapSelectMenu.wnd:RadioButtonUserMaps" );
  174. GameWindow *radioButtonSystemMaps = TheWindowManager->winGetWindowFromId( parent, radioButtonSystemMapsID );
  175. GameWindow *radioButtonUserMaps = TheWindowManager->winGetWindowFromId( parent, radioButtonUserMapsID );
  176. if (usesSystemMapDir)
  177. GadgetRadioSetSelection( radioButtonSystemMaps, FALSE );
  178. else
  179. GadgetRadioSetSelection( radioButtonUserMaps, FALSE );
  180. } // end MapSelectMenuInit
  181. //-------------------------------------------------------------------------------------------------
  182. /** MapSelect menu shutdown method */
  183. //-------------------------------------------------------------------------------------------------
  184. void MapSelectMenuShutdown( WindowLayout *layout, void *userData )
  185. {
  186. if (!startGame)
  187. isShuttingDown = true;
  188. // if we are shutting down for an immediate pop, skip the animations
  189. Bool popImmediate = *(Bool *)userData;
  190. if( popImmediate )
  191. {
  192. shutdownComplete( layout );
  193. return;
  194. } //end if
  195. if (!startGame)
  196. TheShell->reverseAnimatewindow();
  197. } // end MapSelectMenuShutdown
  198. //-------------------------------------------------------------------------------------------------
  199. /** MapSelect menu update method */
  200. //-------------------------------------------------------------------------------------------------
  201. void MapSelectMenuUpdate( WindowLayout *layout, void *userData )
  202. {
  203. if (startGame && TheShell->isAnimFinished())
  204. doGameStart();
  205. // We'll only be successful if we've requested to
  206. if(isShuttingDown && TheShell->isAnimFinished())
  207. shutdownComplete(layout);
  208. } // end MapSelectMenuUpdate
  209. //-------------------------------------------------------------------------------------------------
  210. /** Map select menu input callback */
  211. //-------------------------------------------------------------------------------------------------
  212. WindowMsgHandledType MapSelectMenuInput( GameWindow *window, UnsignedInt msg,
  213. WindowMsgData mData1, WindowMsgData mData2 )
  214. {
  215. switch( msg )
  216. {
  217. // --------------------------------------------------------------------------------------------
  218. case GWM_CHAR:
  219. {
  220. UnsignedByte key = mData1;
  221. UnsignedByte state = mData2;
  222. if (buttonPushed)
  223. break;
  224. switch( key )
  225. {
  226. // ----------------------------------------------------------------------------------------
  227. case KEY_ESC:
  228. {
  229. //
  230. // send a simulated selected event to the parent window of the
  231. // back/exit button
  232. //
  233. if( BitTest( state, KEY_STATE_UP ) )
  234. {
  235. AsciiString buttonName( "MapSelectMenu.wnd:ButtonBack" );
  236. NameKeyType buttonID = TheNameKeyGenerator->nameToKey( buttonName );
  237. GameWindow *button = TheWindowManager->winGetWindowFromId( window, buttonID );
  238. TheWindowManager->winSendSystemMsg( window, GBM_SELECTED,
  239. (WindowMsgData)button, buttonID );
  240. } // end if
  241. // don't let key fall through anywhere else
  242. return MSG_HANDLED;
  243. } // end escape
  244. } // end switch( key )
  245. } // end char
  246. } // end switch( msg )
  247. return MSG_IGNORED;
  248. } // end MapSelectMenuInput
  249. //-------------------------------------------------------------------------------------------------
  250. /** MapSelect menu window system callback */
  251. //-------------------------------------------------------------------------------------------------
  252. WindowMsgHandledType MapSelectMenuSystem( GameWindow *window, UnsignedInt msg,
  253. WindowMsgData mData1, WindowMsgData mData2 )
  254. {
  255. static NameKeyType buttonBack = NAMEKEY_INVALID;
  256. static NameKeyType buttonOK = NAMEKEY_INVALID;
  257. static NameKeyType listboxMap = NAMEKEY_INVALID;
  258. static NameKeyType radioButtonEasyAI = NAMEKEY_INVALID;
  259. static NameKeyType radioButtonMediumAI = NAMEKEY_INVALID;
  260. static NameKeyType radioButtonHardAI = NAMEKEY_INVALID;
  261. switch( msg )
  262. {
  263. // --------------------------------------------------------------------------------------------
  264. case GWM_CREATE:
  265. {
  266. // get ids for our children controls
  267. buttonBack = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:ButtonBack") );
  268. buttonOK = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:ButtonOK") );
  269. listboxMap = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:ListboxMap") );
  270. radioButtonEasyAI = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:RadioButtonEasyAI") );
  271. radioButtonMediumAI = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:RadioButtonMediumAI") );
  272. radioButtonHardAI = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:RadioButtonHardAI") );
  273. break;
  274. } // end create
  275. //---------------------------------------------------------------------------------------------
  276. case GWM_DESTROY:
  277. {
  278. break;
  279. } // end case
  280. // --------------------------------------------------------------------------------------------
  281. case GWM_INPUT_FOCUS:
  282. {
  283. // if we're givin the opportunity to take the keyboard focus we must say we want it
  284. if( mData1 == TRUE )
  285. *(Bool *)mData2 = TRUE;
  286. return MSG_HANDLED;
  287. } // end input
  288. //---------------------------------------------------------------------------------------------
  289. case GBM_SELECTED:
  290. {
  291. if (buttonPushed)
  292. break;
  293. GameWindow *control = (GameWindow *)mData1;
  294. Int controlID = control->winGetWindowId();
  295. static NameKeyType singlePlayerID = NAMEKEY("MapSelectMenu.wnd:ButtonSinglePlayer");
  296. static NameKeyType multiplayerID = NAMEKEY("MapSelectMenu.wnd:ButtonMultiplayer");
  297. if ( controlID == singlePlayerID )
  298. {
  299. showSoloMaps = true;
  300. OptionPreferences pref;
  301. populateMapListbox( mapList, pref.usesSystemMapDir(), !showSoloMaps );
  302. }
  303. else if ( controlID == multiplayerID )
  304. {
  305. showSoloMaps = false;
  306. OptionPreferences pref;
  307. populateMapListbox( mapList, pref.usesSystemMapDir(), !showSoloMaps );
  308. }
  309. else if ( controlID == radioButtonSystemMapsID )
  310. {
  311. if (TheMapCache)
  312. TheMapCache->updateCache();
  313. populateMapListbox( mapList, TRUE, !showSoloMaps );
  314. OptionPreferences pref;
  315. pref["UseSystemMapDir"] = "yes";
  316. pref.write();
  317. }
  318. else if ( controlID == radioButtonUserMapsID )
  319. {
  320. if (TheMapCache)
  321. TheMapCache->updateCache();
  322. populateMapListbox( mapList, FALSE, !showSoloMaps );
  323. OptionPreferences pref;
  324. pref["UseSystemMapDir"] = "no";
  325. pref.write();
  326. }
  327. else if( controlID == buttonBack )
  328. {
  329. // go back one screen
  330. TheShell->pop();
  331. buttonPushed = true;
  332. } // end if
  333. else if( controlID == buttonOK )
  334. {
  335. Int selected;
  336. UnicodeString map;
  337. GameWindow *mapWindow = TheWindowManager->winGetWindowFromId( NULL, listboxMap );
  338. // get the selected index
  339. GadgetListBoxGetSelected( mapWindow, &selected );
  340. if( selected != -1 )
  341. {
  342. buttonPushed = true;
  343. // reset the campaign manager to empty
  344. if( TheCampaignManager )
  345. TheCampaignManager->setCampaign( AsciiString( "" ) );
  346. // get text of the map to load
  347. const char *mapFname = (const char *)GadgetListBoxGetItemData( mapWindow, selected );
  348. DEBUG_ASSERTCRASH(mapFname, ("No map item data"));
  349. if (mapFname)
  350. setupGameStart(mapFname);
  351. } // end if
  352. } // end else if
  353. else if( controlID == radioButtonEasyAI)
  354. {
  355. s_AIDiff = DIFFICULTY_EASY;
  356. }
  357. else if( controlID == radioButtonMediumAI)
  358. {
  359. s_AIDiff = DIFFICULTY_NORMAL;
  360. }
  361. else if( controlID == radioButtonHardAI)
  362. {
  363. s_AIDiff = DIFFICULTY_HARD;
  364. }
  365. break;
  366. } // end selected
  367. case GLM_DOUBLE_CLICKED:
  368. {
  369. if (buttonPushed)
  370. break;
  371. GameWindow *control = (GameWindow *)mData1;
  372. Int controlID = control->winGetWindowId();
  373. if( controlID == listboxMap )
  374. {
  375. int rowSelected = mData2;
  376. if (rowSelected >= 0)
  377. {
  378. //buttonPushed = true;
  379. GadgetListBoxSetSelected( control, rowSelected );
  380. NameKeyType buttonOKID = TheNameKeyGenerator->nameToKey( AsciiString("MapSelectMenu.wnd:ButtonOK") );
  381. GameWindow *buttonOK = TheWindowManager->winGetWindowFromId( NULL, buttonOKID );
  382. TheWindowManager->winSendSystemMsg( window, GBM_SELECTED,
  383. (WindowMsgData)buttonOK, buttonOKID );
  384. }
  385. }
  386. break;
  387. }
  388. default:
  389. return MSG_IGNORED;
  390. } // end switch
  391. return MSG_HANDLED;
  392. } // end MapSelectMenuSystem