WOLMapSelectMenu.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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: WOLMapSelectMenu.cpp ////////////////////////////////////////////////////////////////////////
  24. // Author: Matt Campbell, December 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/CustomMatchPreferences.h"
  30. #include "Common/GameEngine.h"
  31. #include "Common/MessageStream.h"
  32. #include "GameClient/WindowLayout.h"
  33. #include "GameClient/Gadget.h"
  34. #include "GameClient/GadgetRadioButton.h"
  35. #include "GameClient/Shell.h"
  36. #include "GameClient/GameWindowManager.h"
  37. #include "GameClient/GadgetListBox.h"
  38. #include "GameNetwork/GameSpy/PeerDefs.h"
  39. #include "GameNetwork/GameSpyOverlay.h"
  40. #include "GameClient/MapUtil.h"
  41. #include "GameNetwork/GUIUtil.h"
  42. #ifdef _INTERNAL
  43. // for occasional debugging...
  44. //#pragma optimize("", off)
  45. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  46. #endif
  47. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  48. static NameKeyType buttonBack = NAMEKEY_INVALID;
  49. static NameKeyType buttonOK = NAMEKEY_INVALID;
  50. static NameKeyType listboxMap = NAMEKEY_INVALID;
  51. static GameWindow *parent = NULL;
  52. static Bool raiseMessageBoxes = FALSE;
  53. static GameWindow *winMapPreview = NULL;
  54. static NameKeyType winMapPreviewID = NAMEKEY_INVALID;
  55. static NameKeyType radioButtonSystemMapsID = NAMEKEY_INVALID;
  56. static NameKeyType radioButtonUserMapsID = NAMEKEY_INVALID;
  57. extern WindowLayout *WOLMapSelectLayout; ///< Map selection overlay
  58. static GameWindow *mapList = NULL;
  59. static GameWindow *buttonMapStartPosition[MAX_SLOTS] = {NULL,NULL,NULL,NULL,
  60. NULL,NULL,NULL,NULL };
  61. static NameKeyType buttonMapStartPositionID[MAX_SLOTS] = { NAMEKEY_INVALID,NAMEKEY_INVALID,
  62. NAMEKEY_INVALID,NAMEKEY_INVALID,
  63. NAMEKEY_INVALID,NAMEKEY_INVALID,
  64. NAMEKEY_INVALID,NAMEKEY_INVALID };
  65. static GameWindow *winMapWindow = NULL;
  66. static void NullifyControls(void)
  67. {
  68. parent = NULL;
  69. winMapPreview = NULL;
  70. mapList = NULL;
  71. for (Int i=0; i<MAX_SLOTS; ++i)
  72. {
  73. buttonMapStartPosition[i] = NULL;
  74. }
  75. }
  76. static const char *layoutFilename = "GameSpyGameOptionsMenu.wnd";
  77. static const char *parentName = "GameSpyGameOptionsMenuParent";
  78. static const char *gadgetsToHide[] =
  79. {
  80. "MapWindow",
  81. //"StaticTextGameName",
  82. "StaticTextTeam",
  83. "StaticTextFaction",
  84. "StaticTextColor",
  85. "TextEntryMapDisplay",
  86. "ButtonSelectMap",
  87. "ButtonStart",
  88. "StaticTextMapPreview",
  89. NULL // keep this last
  90. };
  91. static const char *perPlayerGadgetsToHide[] =
  92. {
  93. "ComboBoxTeam",
  94. "ComboBoxColor",
  95. "ComboBoxPlayerTemplate",
  96. //"ButtonStartPosition",
  97. NULL // keep this last
  98. };
  99. void positionStartSpots( AsciiString mapName, GameWindow *buttonMapStartPositions[], GameWindow *mapWindow);
  100. static void showGameSpyGameOptionsUnderlyingGUIElements( Bool show )
  101. {
  102. ShowUnderlyingGUIElements( show, layoutFilename, parentName, gadgetsToHide, perPlayerGadgetsToHide );
  103. GameWindow *win = TheWindowManager->winGetWindowFromId( NULL, TheNameKeyGenerator->nameToKey("GameSpyGameOptionsMenu.wnd:ButtonBack") );
  104. if(win)
  105. win->winEnable( show );
  106. }
  107. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  108. //-------------------------------------------------------------------------------------------------
  109. /** Initialize the MapSelect menu */
  110. //-------------------------------------------------------------------------------------------------
  111. void WOLMapSelectMenuInit( WindowLayout *layout, void *userData )
  112. {
  113. // set keyboard focus to main parent
  114. AsciiString parentName( "WOLMapSelectMenu.wnd:WOLMapSelectMenuParent" );
  115. NameKeyType parentID = TheNameKeyGenerator->nameToKey( parentName );
  116. parent = TheWindowManager->winGetWindowFromId( NULL, parentID );
  117. TheWindowManager->winSetFocus( parent );
  118. CustomMatchPreferences pref;
  119. Bool usesSystemMapDir = pref.usesSystemMapDir();
  120. winMapPreviewID = TheNameKeyGenerator->nameToKey( AsciiString("WOLMapSelectMenu.wnd:WinMapPreview") );
  121. winMapPreview = TheWindowManager->winGetWindowFromId(parent, winMapPreviewID);
  122. const MapMetaData *mmd = TheMapCache->findMap(TheGameSpyGame->getMap());
  123. if (mmd)
  124. {
  125. usesSystemMapDir = mmd->m_isOfficial;
  126. }
  127. buttonBack = TheNameKeyGenerator->nameToKey( AsciiString("WOLMapSelectMenu.wnd:ButtonBack") );
  128. buttonOK = TheNameKeyGenerator->nameToKey( AsciiString("WOLMapSelectMenu.wnd:ButtonOK") );
  129. listboxMap = TheNameKeyGenerator->nameToKey( AsciiString("WOLMapSelectMenu.wnd:ListboxMap") );
  130. radioButtonSystemMapsID = TheNameKeyGenerator->nameToKey( "WOLMapSelectMenu.wnd:RadioButtonSystemMaps" );
  131. radioButtonUserMapsID = TheNameKeyGenerator->nameToKey( "WOLMapSelectMenu.wnd:RadioButtonUserMaps" );
  132. winMapWindow = TheWindowManager->winGetWindowFromId( parent, listboxMap );
  133. GameWindow *radioButtonSystemMaps = TheWindowManager->winGetWindowFromId( parent, radioButtonSystemMapsID );
  134. GameWindow *radioButtonUserMaps = TheWindowManager->winGetWindowFromId( parent, radioButtonUserMapsID );
  135. if (usesSystemMapDir)
  136. GadgetRadioSetSelection( radioButtonSystemMaps, FALSE );
  137. else
  138. GadgetRadioSetSelection( radioButtonUserMaps, FALSE );
  139. AsciiString tmpString;
  140. for (Int i = 0; i < MAX_SLOTS; i++)
  141. {
  142. tmpString.format("WOLMapSelectMenu.wnd:ButtonMapStartPosition%d", i);
  143. buttonMapStartPositionID[i] = TheNameKeyGenerator->nameToKey( tmpString );
  144. buttonMapStartPosition[i] = TheWindowManager->winGetWindowFromId( winMapPreview, buttonMapStartPositionID[i] );
  145. DEBUG_ASSERTCRASH(buttonMapStartPosition[i], ("Could not find the ButtonMapStartPosition[%d]",i ));
  146. buttonMapStartPosition[i]->winHide(TRUE);
  147. buttonMapStartPosition[i]->winEnable(FALSE);
  148. }
  149. raiseMessageBoxes = TRUE;
  150. showGameSpyGameOptionsUnderlyingGUIElements( FALSE );
  151. // get the listbox window
  152. AsciiString listString( "WOLMapSelectMenu.wnd:ListboxMap" );
  153. NameKeyType mapListID = TheNameKeyGenerator->nameToKey( listString );
  154. mapList = TheWindowManager->winGetWindowFromId( parent, mapListID );
  155. if( mapList )
  156. {
  157. if (TheMapCache)
  158. TheMapCache->updateCache();
  159. populateMapListbox( mapList, usesSystemMapDir, TRUE, TheGameSpyGame->getMap() );
  160. }
  161. } // end WOLMapSelectMenuInit
  162. //-------------------------------------------------------------------------------------------------
  163. /** MapSelect menu shutdown method */
  164. //-------------------------------------------------------------------------------------------------
  165. void WOLMapSelectMenuShutdown( WindowLayout *layout, void *userData )
  166. {
  167. NullifyControls();
  168. // hide menu
  169. layout->hide( TRUE );
  170. // our shutdown is complete
  171. TheShell->shutdownComplete( layout );
  172. } // end WOLMapSelectMenuShutdown
  173. //-------------------------------------------------------------------------------------------------
  174. /** MapSelect menu update method */
  175. //-------------------------------------------------------------------------------------------------
  176. void WOLMapSelectMenuUpdate( WindowLayout *layout, void *userData )
  177. {
  178. if (raiseMessageBoxes)
  179. {
  180. RaiseGSMessageBox();
  181. raiseMessageBoxes = false;
  182. }
  183. // No update because the game setup screen is up at the same
  184. // time and it does the update for us...
  185. } // end WOLMapSelectMenuUpdate
  186. //-------------------------------------------------------------------------------------------------
  187. /** Map select menu input callback */
  188. //-------------------------------------------------------------------------------------------------
  189. WindowMsgHandledType WOLMapSelectMenuInput( GameWindow *window, UnsignedInt msg,
  190. WindowMsgData mData1, WindowMsgData mData2 )
  191. {
  192. switch( msg )
  193. {
  194. // --------------------------------------------------------------------------------------------
  195. case GWM_CHAR:
  196. {
  197. UnsignedByte key = mData1;
  198. UnsignedByte state = mData2;
  199. switch( key )
  200. {
  201. // ----------------------------------------------------------------------------------------
  202. case KEY_ESC:
  203. {
  204. //
  205. // send a simulated selected event to the parent window of the
  206. // back/exit button
  207. //
  208. if( BitTest( state, KEY_STATE_UP ) )
  209. {
  210. AsciiString buttonName( "WOLMapSelectMenu.wnd:ButtonBack" );
  211. NameKeyType buttonID = TheNameKeyGenerator->nameToKey( buttonName );
  212. GameWindow *button = TheWindowManager->winGetWindowFromId( window, buttonID );
  213. TheWindowManager->winSendSystemMsg( window, GBM_SELECTED,
  214. (WindowMsgData)button, buttonID );
  215. } // end if
  216. // don't let key fall through anywhere else
  217. return MSG_HANDLED;
  218. } // end escape
  219. } // end switch( key )
  220. } // end char
  221. } // end switch( msg )
  222. return MSG_IGNORED;
  223. } // end WOLMapSelectMenuInput
  224. void WOLPositionStartSpots( void );
  225. //-------------------------------------------------------------------------------------------------
  226. /** MapSelect menu window system callback */
  227. //-------------------------------------------------------------------------------------------------
  228. WindowMsgHandledType WOLMapSelectMenuSystem( GameWindow *window, UnsignedInt msg,
  229. WindowMsgData mData1, WindowMsgData mData2 )
  230. {
  231. switch( msg )
  232. {
  233. // --------------------------------------------------------------------------------------------
  234. case GWM_CREATE:
  235. {
  236. break;
  237. } // end create
  238. //---------------------------------------------------------------------------------------------
  239. case GWM_DESTROY:
  240. {
  241. NullifyControls();
  242. break;
  243. } // end case
  244. // --------------------------------------------------------------------------------------------
  245. case GWM_INPUT_FOCUS:
  246. {
  247. // if we're givin the opportunity to take the keyboard focus we must say we want it
  248. if( mData1 == TRUE )
  249. *(Bool *)mData2 = TRUE;
  250. return MSG_HANDLED;
  251. } // end input
  252. //---------------------------------------------------------------------------------------------
  253. case GLM_DOUBLE_CLICKED:
  254. {
  255. GameWindow *control = (GameWindow *)mData1;
  256. Int controlID = control->winGetWindowId();
  257. if( controlID == listboxMap )
  258. {
  259. int rowSelected = mData2;
  260. if (rowSelected >= 0)
  261. {
  262. GadgetListBoxSetSelected( control, rowSelected );
  263. GameWindow *button = TheWindowManager->winGetWindowFromId( window, buttonOK );
  264. TheWindowManager->winSendSystemMsg( window, GBM_SELECTED,
  265. (WindowMsgData)button, buttonOK );
  266. }
  267. }
  268. break;
  269. }
  270. //---------------------------------------------------------------------------------------------
  271. case GLM_SELECTED:
  272. {
  273. GameWindow *control = (GameWindow *)mData1;
  274. Int controlID = control->winGetWindowId();
  275. if( controlID == listboxMap )
  276. {
  277. int rowSelected = mData2;
  278. if( rowSelected < 0 )
  279. {
  280. positionStartSpots( AsciiString::TheEmptyString, buttonMapStartPosition, winMapPreview);
  281. // winMapPreview->winClearStatus(WIN_STATUS_IMAGE);
  282. break;
  283. }
  284. winMapPreview->winSetStatus(WIN_STATUS_IMAGE);
  285. UnicodeString map;
  286. // get text of the map to load
  287. map = GadgetListBoxGetText( winMapWindow, rowSelected, 0 );
  288. // set the map name in the global data map name
  289. AsciiString asciiMap;
  290. const char *mapFname = (const char *)GadgetListBoxGetItemData( winMapWindow, rowSelected );
  291. DEBUG_ASSERTCRASH(mapFname, ("No map item data"));
  292. if (mapFname)
  293. asciiMap = mapFname;
  294. else
  295. asciiMap.translate( map );
  296. asciiMap.toLower();
  297. Image *image = getMapPreviewImage(asciiMap);
  298. winMapPreview->winSetUserData((void *)TheMapCache->findMap(asciiMap));
  299. if(image)
  300. {
  301. winMapPreview->winSetEnabledImage(0, image);
  302. }
  303. else
  304. {
  305. winMapPreview->winClearStatus(WIN_STATUS_IMAGE);
  306. }
  307. positionStartSpots( asciiMap, buttonMapStartPosition, winMapPreview);
  308. }
  309. break;
  310. }
  311. //---------------------------------------------------------------------------------------------
  312. case GBM_SELECTED:
  313. {
  314. GameWindow *control = (GameWindow *)mData1;
  315. Int controlID = control->winGetWindowId();
  316. if( controlID == buttonBack )
  317. {
  318. showGameSpyGameOptionsUnderlyingGUIElements( TRUE );
  319. WOLMapSelectLayout->destroyWindows();
  320. WOLMapSelectLayout->deleteInstance();
  321. WOLMapSelectLayout = NULL;
  322. WOLPositionStartSpots();
  323. } // end if
  324. else if ( controlID == radioButtonSystemMapsID )
  325. {
  326. if (TheMapCache)
  327. TheMapCache->updateCache();
  328. populateMapListbox( mapList, TRUE, TRUE, TheGameSpyGame->getMap() );
  329. CustomMatchPreferences pref;
  330. pref.setUsesSystemMapDir(TRUE);
  331. pref.write();
  332. }
  333. else if ( controlID == radioButtonUserMapsID )
  334. {
  335. if (TheMapCache)
  336. TheMapCache->updateCache();
  337. populateMapListbox( mapList, FALSE, TRUE, TheGameSpyGame->getMap() );
  338. CustomMatchPreferences pref;
  339. pref.setUsesSystemMapDir(FALSE);
  340. pref.write();
  341. }
  342. else if( controlID == buttonOK )
  343. {
  344. Int selected;
  345. UnicodeString map;
  346. // get the selected index
  347. GadgetListBoxGetSelected( winMapWindow, &selected );
  348. if( selected != -1 )
  349. {
  350. // get text of the map to load
  351. map = GadgetListBoxGetText( winMapWindow, selected, 0 );
  352. // set the map name in the global data map name
  353. AsciiString asciiMap;
  354. const char *mapFname = (const char *)GadgetListBoxGetItemData( winMapWindow, selected );
  355. DEBUG_ASSERTCRASH(mapFname, ("No map item data"));
  356. if (mapFname)
  357. asciiMap = mapFname;
  358. else
  359. asciiMap.translate( map );
  360. TheGameSpyGame->setMap(asciiMap);
  361. asciiMap.toLower();
  362. std::map<AsciiString, MapMetaData>::iterator it = TheMapCache->find(asciiMap);
  363. if (it != TheMapCache->end())
  364. {
  365. TheGameSpyGame->getGameSpySlot(0)->setMapAvailability(TRUE);
  366. TheGameSpyGame->setMapCRC( it->second.m_CRC );
  367. TheGameSpyGame->setMapSize( it->second.m_filesize );
  368. }
  369. TheGameSpyGame->adjustSlotsForMap(); // BGC- adjust the slots for the new map.
  370. TheGameSpyGame->resetAccepted();
  371. TheGameSpyGame->resetStartSpots();
  372. TheGameSpyInfo->setGameOptions();
  373. WOLDisplaySlotList();
  374. WOLDisplayGameOptions();
  375. WOLMapSelectLayout->destroyWindows();
  376. WOLMapSelectLayout->deleteInstance();
  377. WOLMapSelectLayout = NULL;
  378. showGameSpyGameOptionsUnderlyingGUIElements( TRUE );
  379. WOLPositionStartSpots();
  380. } // end if
  381. } // end else if
  382. break;
  383. } // end selected
  384. default:
  385. return MSG_IGNORED;
  386. } // end switch
  387. return MSG_HANDLED;
  388. } // end WOLMapSelectMenuSystem