PopupReplay.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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: PopupReplay.cpp /////////////////////////////////////////////////////////
  24. //-----------------------------------------------------------------------------
  25. //
  26. // Electronic Arts Pacific.
  27. //
  28. // Confidential Information
  29. // Copyright (C) 2002 - All Rights Reserved
  30. //
  31. //-----------------------------------------------------------------------------
  32. //
  33. // Project: Generals
  34. //
  35. // File name: PopupReplay.cpp
  36. //
  37. // Created: Matthew D. Campbell, November 2002
  38. //
  39. // Desc: the Replay Save window control
  40. //
  41. //-----------------------------------------------------------------------------
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // INCLUDES ///////////////////////////////////////////////////////////////////////////////////////
  44. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  45. #include "Common/LocalFileSystem.h"
  46. #include "Common/MessageStream.h"
  47. #include "Common/Recorder.h"
  48. #include "GameClient/GadgetListBox.h"
  49. #include "GameClient/GadgetTextEntry.h"
  50. #include "GameClient/GameText.h"
  51. #include "GameClient/GameWindowManager.h"
  52. #include "GameClient/GUICallbacks.h"
  53. #include "GameClient/MessageBox.h"
  54. #include "GameClient/Shell.h"
  55. #include "GameLogic/GameLogic.h"
  56. #ifdef _INTERNAL
  57. // for occasional debugging...
  58. //#pragma optimize("", off)
  59. //#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
  60. #endif
  61. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  62. static NameKeyType buttonBackKey = NAMEKEY_INVALID;
  63. static NameKeyType buttonSaveKey = NAMEKEY_INVALID;
  64. static NameKeyType listboxGamesKey = NAMEKEY_INVALID;
  65. static NameKeyType textEntryReplayNameKey = NAMEKEY_INVALID;
  66. static GameWindow *parent = NULL;
  67. static GameWindow *replaySavedParent = NULL;
  68. static time_t s_fileSavePopupStartTime = 0;
  69. static const time_t s_fileSavePopupDuration = 1000;
  70. // PUBLIC FUNCTIONS ///////////////////////////////////////////////////////////////////////////////
  71. extern void PopulateReplayFileListbox(GameWindow *listbox);
  72. extern void ScoreScreenEnableControls(Bool enable);
  73. extern UnicodeString GetReplayFilenameFromListbox(GameWindow *listbox, Int index);
  74. extern std::string LastReplayFileName;
  75. //-------------------------------------------------------------------------------------------------
  76. /** Show or hide the "Replay Saved" popup */
  77. //-------------------------------------------------------------------------------------------------
  78. void ShowReplaySavedPopup(Bool show)
  79. {
  80. if (replaySavedParent != NULL) {
  81. if (show) {
  82. replaySavedParent->winHide(FALSE);
  83. } else {
  84. replaySavedParent->winHide(TRUE);
  85. }
  86. }
  87. }
  88. // ------------------------------------------------------------------------------------------------
  89. /** Close the save/load menu */
  90. // ------------------------------------------------------------------------------------------------
  91. static void closeSaveMenu( GameWindow *window )
  92. {
  93. WindowLayout *layout = window->winGetLayout();
  94. if( layout )
  95. layout->hide( TRUE );
  96. } // end closeSaveMenu
  97. //-------------------------------------------------------------------------------------------------
  98. /** Initialize the SaveLoad menu */
  99. //-------------------------------------------------------------------------------------------------
  100. void PopupReplayInit( WindowLayout *layout, void *userData )
  101. {
  102. // get ids for our children controls
  103. buttonBackKey = NAMEKEY( "PopupReplay.wnd:ButtonBack" );
  104. buttonSaveKey = NAMEKEY( "PopupReplay.wnd:ButtonSave" );
  105. listboxGamesKey = NAMEKEY( "PopupReplay.wnd:ListboxGames" );
  106. textEntryReplayNameKey = NAMEKEY( "PopupReplay.wnd:TextEntryReplayName" );
  107. //set keyboard focus to main parent and set modal
  108. NameKeyType parentID = TheNameKeyGenerator->nameToKey("PopupReplay.wnd:PopupReplayMenu");
  109. parent = TheWindowManager->winGetWindowFromId( NULL, parentID );
  110. TheWindowManager->winSetFocus( parent );
  111. NameKeyType replaySavedParentID = TheNameKeyGenerator->nameToKey("PopupReplay.wnd:PopupReplaySaved");
  112. replaySavedParent = TheWindowManager->winGetWindowFromId( NULL, replaySavedParentID);
  113. if (replaySavedParent == NULL) {
  114. DEBUG_CRASH(("replaySavedParent == NULL"));
  115. }
  116. ShowReplaySavedPopup(FALSE);
  117. // enable the menu action buttons
  118. GameWindow *buttonFrame = TheWindowManager->winGetWindowFromId( parent, NAMEKEY( "PopupReplay.wnd:MenuButtonFrame" ) );
  119. buttonFrame->winEnable( TRUE );
  120. // get the listbox that will have the save games in it
  121. GameWindow *listboxGames = TheWindowManager->winGetWindowFromId( NULL, listboxGamesKey );
  122. DEBUG_ASSERTCRASH( listboxGames != NULL, ("PopupReplayInit - Unable to find games listbox\n") );
  123. // populate the listbox with the save games on disk
  124. PopulateReplayFileListbox(listboxGames);
  125. GameWindow *textEntryReplayName = TheWindowManager->winGetWindowFromId( parent, textEntryReplayNameKey );
  126. GadgetTextEntrySetText(textEntryReplayName, UnicodeString::TheEmptyString);
  127. TheWindowManager->winSetFocus( textEntryReplayName );
  128. //Disable the button immediately as the code above us starts off with an empty string.
  129. GameWindow *control = TheWindowManager->winGetWindowFromId( parent, buttonSaveKey );
  130. if( control )
  131. {
  132. control->winEnable( FALSE );
  133. }
  134. } // end SaveLoadMenuInit
  135. //-------------------------------------------------------------------------------------------------
  136. /** SaveLoad menu shutdown method */
  137. //-------------------------------------------------------------------------------------------------
  138. void PopupReplayShutdown( WindowLayout *layout, void *userData )
  139. {
  140. parent = NULL;
  141. } // end SaveLoadMenuShutdown
  142. //-------------------------------------------------------------------------------------------------
  143. /** SaveLoad menu update method */
  144. //-------------------------------------------------------------------------------------------------
  145. void PopupReplayUpdate( WindowLayout *layout, void *userData )
  146. {
  147. if (s_fileSavePopupStartTime != 0)
  148. {
  149. // the replay save confirmation popup is up
  150. // check to see if its time to take it down.
  151. if ((timeGetTime() - s_fileSavePopupStartTime) >= s_fileSavePopupDuration)
  152. {
  153. ShowReplaySavedPopup(FALSE);
  154. // close the save/load menu
  155. closeSaveMenu( parent );
  156. ScoreScreenEnableControls(TRUE);
  157. // reset the timer to 0 cause we have to.
  158. s_fileSavePopupStartTime = 0;
  159. }
  160. }
  161. } // end SaveLoadMenuUpdate
  162. // ------------------------------------------------------------------------------------------------
  163. // ------------------------------------------------------------------------------------------------
  164. WindowMsgHandledType PopupReplayInput( GameWindow *window, UnsignedInt msg, WindowMsgData mData1, WindowMsgData mData2 )
  165. {
  166. switch( msg )
  167. {
  168. // --------------------------------------------------------------------------------------------
  169. case GWM_CHAR:
  170. {
  171. UnsignedByte key = mData1;
  172. UnsignedByte state = mData2;
  173. switch( key )
  174. {
  175. // ----------------------------------------------------------------------------------------
  176. case KEY_ESC:
  177. {
  178. //
  179. // send a simulated selected event to the parent window of the
  180. // back/exit button
  181. //
  182. if( BitTest( state, KEY_STATE_UP ) )
  183. {
  184. GameWindow *button = TheWindowManager->winGetWindowFromId( parent, buttonBackKey );
  185. TheWindowManager->winSendSystemMsg( window, GBM_SELECTED,
  186. (WindowMsgData)button, buttonBackKey );
  187. } // end if
  188. // don't let key fall through anywhere else
  189. return MSG_HANDLED;
  190. } // end escape
  191. } // end switch( key )
  192. } // end char
  193. } // end switch( msg )
  194. return MSG_IGNORED;
  195. }
  196. static void reallySaveReplay(void);
  197. static std::string replayPath;
  198. // ------------------------------------------------------------------------------------------------
  199. /** Save the replay */
  200. // ------------------------------------------------------------------------------------------------
  201. static GameWindow *messageBoxWin = NULL;
  202. static void saveReplay( UnicodeString filename )
  203. {
  204. AsciiString translated;
  205. if (filename == TheGameText->fetch("GUI:LastReplay"))
  206. {
  207. translated = TheRecorder->getLastReplayFileName();
  208. }
  209. else
  210. {
  211. translated.translate(filename);
  212. }
  213. AsciiString fullPath = TheRecorder->getReplayDir();
  214. fullPath.concat(translated);
  215. fullPath.concat(TheRecorder->getReplayExtention());
  216. replayPath = fullPath.str();
  217. messageBoxWin = NULL;
  218. if (TheLocalFileSystem->doesFileExist(fullPath.str()))
  219. {
  220. messageBoxWin = MessageBoxOkCancel(TheGameText->fetch("GUI:OverwriteReplayTitle"), TheGameText->fetch("GUI:OverwriteReplay"), reallySaveReplay, NULL);
  221. }
  222. else
  223. {
  224. reallySaveReplay();
  225. }
  226. }
  227. void reallySaveReplay(void)
  228. {
  229. AsciiString filename = replayPath.c_str();
  230. AsciiString oldFilename;
  231. oldFilename = TheRecorder->getReplayDir();
  232. oldFilename.concat(LastReplayFileName.c_str());
  233. oldFilename.concat(TheRecorder->getReplayExtention());
  234. if (oldFilename == filename)
  235. return;
  236. if (TheLocalFileSystem->doesFileExist(filename.str()))
  237. {
  238. if(DeleteFile(filename.str()) == 0)
  239. {
  240. wchar_t buffer[1024];
  241. FormatMessageW ( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, buffer, sizeof(buffer), NULL);
  242. UnicodeString errorStr;
  243. errorStr.set(buffer);
  244. errorStr.trim();
  245. if(messageBoxWin)
  246. {
  247. TheWindowManager->winUnsetModal(messageBoxWin);
  248. messageBoxWin = NULL;
  249. }
  250. MessageBoxOk(TheGameText->fetch("GUI:Error"),errorStr, NULL);
  251. // get the listbox that will have the save games in it
  252. GameWindow *listboxGames = TheWindowManager->winGetWindowFromId( parent, listboxGamesKey );
  253. DEBUG_ASSERTCRASH( listboxGames != NULL, ("reallySaveReplay - Unable to find games listbox\n") );
  254. // populate the listbox with the save games on disk
  255. PopulateReplayFileListbox(listboxGames);
  256. return;
  257. }
  258. }
  259. // copy the replay to the right place
  260. if(CopyFile(oldFilename.str(),filename.str(), FALSE) == 0)
  261. {
  262. wchar_t buffer[1024];
  263. FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, buffer, sizeof(buffer), NULL);
  264. UnicodeString errorStr;
  265. errorStr.set(buffer);
  266. errorStr.trim();
  267. if(messageBoxWin)
  268. {
  269. TheWindowManager->winUnsetModal(messageBoxWin);
  270. messageBoxWin = NULL;
  271. }
  272. MessageBoxOk(TheGameText->fetch("GUI:Error"),errorStr, NULL);
  273. return;
  274. }
  275. // get the listbox that will have the save games in it
  276. GameWindow *listboxGames = TheWindowManager->winGetWindowFromId( parent, listboxGamesKey );
  277. DEBUG_ASSERTCRASH( listboxGames != NULL, ("reallySaveReplay - Unable to find games listbox\n") );
  278. // populate the listbox with the save games on disk
  279. PopulateReplayFileListbox(listboxGames);
  280. ShowReplaySavedPopup(TRUE);
  281. s_fileSavePopupStartTime = timeGetTime();
  282. }
  283. //-------------------------------------------------------------------------------------------------
  284. /** SaveLoad menu system callback */
  285. //-------------------------------------------------------------------------------------------------
  286. WindowMsgHandledType PopupReplaySystem( GameWindow *window, UnsignedInt msg,
  287. WindowMsgData mData1, WindowMsgData mData2 )
  288. {
  289. switch( msg )
  290. {
  291. // --------------------------------------------------------------------------------------------
  292. case GWM_CREATE:
  293. {
  294. break;
  295. } // end create
  296. //---------------------------------------------------------------------------------------------
  297. case GWM_DESTROY:
  298. {
  299. break;
  300. } // end case
  301. //----------------------------------------------------------------------------------------------
  302. case GWM_INPUT_FOCUS:
  303. {
  304. // if we're givin the opportunity to take the keyboard focus we must say we want it
  305. if( mData1 == TRUE )
  306. *(Bool *)mData2 = TRUE;
  307. break;
  308. } // end input
  309. // --------------------------------------------------------------------------------------------
  310. case GLM_SELECTED:
  311. {
  312. GameWindow *control = (GameWindow *)mData1;
  313. GameWindow *listboxGames = TheWindowManager->winGetWindowFromId( window, listboxGamesKey );
  314. DEBUG_ASSERTCRASH( listboxGames != NULL, ("PopupReplaySystem - Unable to find games listbox\n") );
  315. //
  316. // handle games listbox, when certain items are selected in the listbox only some
  317. // commands are available
  318. //
  319. if( control == listboxGames )
  320. {
  321. int rowSelected = mData2;
  322. if (rowSelected >= 0)
  323. {
  324. UnicodeString filename;
  325. filename = GadgetListBoxGetText(listboxGames, rowSelected);
  326. GameWindow *textEntryReplayName = TheWindowManager->winGetWindowFromId( window, textEntryReplayNameKey );
  327. DEBUG_ASSERTCRASH( textEntryReplayName != NULL, ("PopupReplaySystem - Unable to find text entry\n") );
  328. GadgetTextEntrySetText(textEntryReplayName, filename);
  329. }
  330. }
  331. break;
  332. } // end selected
  333. //---------------------------------------------------------------------------------------------
  334. case GEM_EDIT_DONE:
  335. {
  336. GameWindow *control = (GameWindow *)mData1;
  337. Int controlID = control->winGetWindowId();
  338. if( controlID == textEntryReplayNameKey )
  339. {
  340. UnicodeString filename = GadgetTextEntryGetText( control );
  341. if (filename.isEmpty())
  342. break;
  343. saveReplay(filename);
  344. }
  345. break;
  346. } // end selected
  347. //---------------------------------------------------------------------------------------------
  348. case GBM_SELECTED:
  349. {
  350. GameWindow *control = (GameWindow *)mData1;
  351. Int controlID = control->winGetWindowId();
  352. if( controlID == buttonSaveKey )
  353. {
  354. // get the filename, and see if we are overwriting
  355. GameWindow *textEntryReplayName = TheWindowManager->winGetWindowFromId( window, textEntryReplayNameKey );
  356. DEBUG_ASSERTCRASH( textEntryReplayName != NULL, ("PopupReplaySystem - Unable to find text entry\n") );
  357. UnicodeString filename = GadgetTextEntryGetText( textEntryReplayName );
  358. if (filename.isEmpty())
  359. break;
  360. saveReplay(filename);
  361. }
  362. else if( controlID == buttonBackKey )
  363. {
  364. // close the save/load menu
  365. closeSaveMenu( window );
  366. ScoreScreenEnableControls(TRUE);
  367. } // end if
  368. break;
  369. } // end selected
  370. case GEM_UPDATE_TEXT:
  371. {
  372. //Kris:
  373. //Enable or disable the save button -- disabled when empty.
  374. GameWindow *control = TheWindowManager->winGetWindowFromId( parent, textEntryReplayNameKey );
  375. if( control )
  376. {
  377. UnicodeString filename;
  378. filename.set( GadgetTextEntryGetText( control ) );
  379. control = TheWindowManager->winGetWindowFromId( parent, buttonSaveKey );
  380. if( control )
  381. {
  382. if( filename.isEmpty() )
  383. {
  384. control->winEnable( FALSE );
  385. }
  386. else
  387. {
  388. control->winEnable( TRUE );
  389. }
  390. }
  391. }
  392. }
  393. default:
  394. return MSG_IGNORED;
  395. } // end switch
  396. return MSG_HANDLED;
  397. }