MapSelectMenu.cpp 16 KB

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