MenuScreen_Shell_Save.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. const static int NUM_SAVE_OPTIONS = 10;
  24. /*
  25. ========================
  26. idMenuScreen_Shell_Save::Initialize
  27. ========================
  28. */
  29. void idMenuScreen_Shell_Save::Initialize( idMenuHandler * data ) {
  30. idMenuScreen::Initialize( data );
  31. if ( data != NULL ) {
  32. menuGUI = data->GetGUI();
  33. }
  34. SetSpritePath( "menuSave" );
  35. saveInfo = new (TAG_SWF) idMenuWidget_Shell_SaveInfo();
  36. saveInfo->SetSpritePath( GetSpritePath(), "info", "details" );
  37. saveInfo->Initialize( data );
  38. saveInfo->SetForSaveScreen( true );
  39. options = new (TAG_SWF) idMenuWidget_DynamicList();
  40. options->SetNumVisibleOptions( NUM_SAVE_OPTIONS );
  41. options->SetSpritePath( GetSpritePath(), "info", "options" );
  42. options->SetWrappingAllowed( true );
  43. while ( options->GetChildren().Num() < NUM_SAVE_OPTIONS ) {
  44. idMenuWidget_Button * const buttonWidget = new (TAG_SWF) idMenuWidget_Button();
  45. buttonWidget->AddEventAction( WIDGET_EVENT_PRESS ).Set( WIDGET_ACTION_PRESS_FOCUSED, options->GetChildren().Num() );
  46. buttonWidget->RegisterEventObserver( saveInfo );
  47. buttonWidget->Initialize( data );
  48. options->AddChild( buttonWidget );
  49. }
  50. options->Initialize( data );
  51. AddChild( options );
  52. AddChild( saveInfo );
  53. btnBack = new (TAG_SWF) idMenuWidget_Button();
  54. btnBack->Initialize( data );
  55. btnBack->SetLabel( "#str_swf_pause_menu" );
  56. btnBack->SetSpritePath( GetSpritePath(), "info", "btnBack" );
  57. btnBack->AddEventAction( WIDGET_EVENT_PRESS ).Set( WIDGET_ACTION_GO_BACK );
  58. AddChild( btnBack );
  59. btnDelete = new idMenuWidget_Button();
  60. btnDelete->Initialize( data );
  61. btnDelete->SetLabel( "" );
  62. btnDelete->AddEventAction( WIDGET_EVENT_PRESS ).Set( WIDGET_ACTION_JOY3_ON_PRESS );
  63. btnDelete->SetSpritePath( GetSpritePath(), "info", "btnDelete" );
  64. AddChild( btnDelete );
  65. options->AddEventAction( WIDGET_EVENT_SCROLL_DOWN ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_SCROLL_DOWN_START_REPEATER, WIDGET_EVENT_SCROLL_DOWN ) );
  66. options->AddEventAction( WIDGET_EVENT_SCROLL_UP ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_SCROLL_UP_START_REPEATER, WIDGET_EVENT_SCROLL_UP ) );
  67. options->AddEventAction( WIDGET_EVENT_SCROLL_DOWN_RELEASE ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_STOP_REPEATER, WIDGET_EVENT_SCROLL_DOWN_RELEASE ) );
  68. options->AddEventAction( WIDGET_EVENT_SCROLL_UP_RELEASE ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_STOP_REPEATER, WIDGET_EVENT_SCROLL_UP_RELEASE ) );
  69. options->AddEventAction( WIDGET_EVENT_SCROLL_DOWN_LSTICK ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_SCROLL_DOWN_START_REPEATER, WIDGET_EVENT_SCROLL_DOWN_LSTICK ) );
  70. options->AddEventAction( WIDGET_EVENT_SCROLL_UP_LSTICK ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_SCROLL_UP_START_REPEATER, WIDGET_EVENT_SCROLL_UP_LSTICK ) );
  71. options->AddEventAction( WIDGET_EVENT_SCROLL_DOWN_LSTICK_RELEASE ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_STOP_REPEATER, WIDGET_EVENT_SCROLL_DOWN_LSTICK_RELEASE ) );
  72. options->AddEventAction( WIDGET_EVENT_SCROLL_UP_LSTICK_RELEASE ).Set( new (TAG_SWF) idWidgetActionHandler( options, WIDGET_ACTION_EVENT_STOP_REPEATER, WIDGET_EVENT_SCROLL_UP_LSTICK_RELEASE ) );
  73. }
  74. /*
  75. ========================
  76. idMenuScreen_Shell_Save::Update
  77. ========================
  78. */
  79. void idMenuScreen_Shell_Save::Update() {
  80. UpdateSaveEnumerations();
  81. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  82. if ( BindSprite( root ) ) {
  83. idSWFTextInstance * heading = GetSprite()->GetScriptObject()->GetNestedText( "info", "txtHeading" );
  84. if ( heading != NULL ) {
  85. heading->SetText( "#str_02179" ); // SAVE GAME
  86. heading->SetStrokeInfo( true, 0.75f, 1.75f );
  87. }
  88. idSWFSpriteInstance * gradient = GetSprite()->GetScriptObject()->GetNestedSprite( "info", "gradient" );
  89. if ( gradient != NULL && heading != NULL ) {
  90. gradient->SetXPos( heading->GetTextLength() );
  91. }
  92. }
  93. if ( btnBack != NULL ) {
  94. btnBack->BindSprite( root );
  95. }
  96. idMenuScreen::Update();
  97. }
  98. /*
  99. ========================
  100. idMenuScreen_Shell_Save::UpdateSaveEnumerations
  101. ========================
  102. */
  103. void idMenuScreen_Shell_Save::UpdateSaveEnumerations() {
  104. const saveGameDetailsList_t & saveGameInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
  105. sortedSaves = saveGameInfo;
  106. idList< idList< idStr, TAG_IDLIB_LIST_MENU >, TAG_IDLIB_LIST_MENU > saveList;
  107. int newSaveOffset = 1;
  108. bool hasAutosave = false;
  109. if ( session->GetSaveGameManager().IsWorking() ) {
  110. idList< idStr > saveName;
  111. saveName.Append( "#str_dlg_refreshing" );
  112. saveList.Append( saveName );
  113. if ( options != NULL ) {
  114. options->SetListData( saveList );
  115. options->Update();
  116. }
  117. } else {
  118. for ( int i = 0; i < saveGameInfo.Num(); ++i ) {
  119. if ( saveGameInfo[i].slotName.Icmp( "autosave" ) == 0 ) {
  120. hasAutosave = true;
  121. break;
  122. }
  123. }
  124. if ( saveGameInfo.Num() == MAX_SAVEGAMES || ( !hasAutosave && saveGameInfo.Num() == MAX_SAVEGAMES - 1 ) ) {
  125. newSaveOffset = 0;
  126. }
  127. if ( newSaveOffset != 0 ) {
  128. idList< idStr > newSave;
  129. newSave.Append( "#str_swf_new_save_game" );
  130. saveList.Append( newSave );
  131. }
  132. if ( options != NULL ) {
  133. sortedSaves.Sort( idSort_SavesByDate() );
  134. for ( int slot = 0; slot < sortedSaves.Num(); ++slot ) {
  135. const idSaveGameDetails & details = sortedSaves[slot];
  136. if ( details.slotName.Icmp( "autosave" ) == 0 ) {
  137. sortedSaves.RemoveIndex( slot );
  138. slot--;
  139. }
  140. }
  141. // +1 because the first item is "New Save"
  142. saveList.SetNum( sortedSaves.Num() + newSaveOffset );
  143. for ( int slot = 0; slot < sortedSaves.Num(); ++slot ) {
  144. idStr & slotSaveName = saveList[ slot + newSaveOffset ].Alloc();
  145. const idSaveGameDetails & details = sortedSaves[slot];
  146. if ( details.damaged ) {
  147. slotSaveName = va( S_COLOR_RED "%s", idLocalization::GetString( "#str_swf_corrupt_file" ) );
  148. } else if ( details.GetSaveVersion() > BUILD_NUMBER ) {
  149. slotSaveName = va( S_COLOR_RED "%s", idLocalization::GetString( "#str_swf_wrong_version" ) );
  150. } else {
  151. if ( details.slotName.Icmp( "autosave" ) == 0 ) {
  152. slotSaveName.Append( S_COLOR_YELLOW );
  153. } else if ( details.slotName.Icmp( "quick" ) == 0 ) {
  154. slotSaveName.Append( S_COLOR_ORANGE );
  155. }
  156. slotSaveName.Append( details.GetMapName() );
  157. }
  158. }
  159. options->SetListData( saveList );
  160. options->Update();
  161. }
  162. }
  163. if ( menuData != NULL ) {
  164. idMenuWidget_CommandBar * cmdBar = menuData->GetCmdBar();
  165. if ( cmdBar != NULL ) {
  166. cmdBar->ClearAllButtons();
  167. idMenuWidget_CommandBar::buttonInfo_t * buttonInfo;
  168. buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY2 );
  169. if ( menuData->GetPlatform() != 2 ) {
  170. buttonInfo->label = "#str_00395"; // BACK
  171. }
  172. buttonInfo->action.Set( WIDGET_ACTION_GO_BACK );
  173. if ( !session->GetSaveGameManager().IsWorking() ) {
  174. buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY1 );
  175. if ( menuData->GetPlatform() != 2 ) {
  176. buttonInfo->label = "#str_02179"; // SAVE GAME
  177. }
  178. buttonInfo->action.Set( WIDGET_ACTION_PRESS_FOCUSED );
  179. if ( options != NULL ) {
  180. if ( options->GetViewIndex() != 0 || ( options->GetViewIndex() == 0 && newSaveOffset == 0 ) ) {
  181. buttonInfo = cmdBar->GetButton( idMenuWidget_CommandBar::BUTTON_JOY3 );
  182. if ( menuData->GetPlatform() != 2 ) {
  183. buttonInfo->label = "#str_02315"; // DELETE
  184. }
  185. buttonInfo->action.Set( WIDGET_ACTION_JOY3_ON_PRESS );
  186. if ( btnDelete != NULL ) {
  187. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  188. if ( btnDelete->BindSprite( root ) ) {
  189. if ( menuData->GetPlatform() != 2 ) {
  190. btnDelete->SetLabel( "" );
  191. } else {
  192. btnDelete->GetSprite()->SetVisible( true );
  193. btnDelete->SetLabel( "#str_02315" );
  194. }
  195. }
  196. btnDelete->Update();
  197. }
  198. } else {
  199. if ( btnDelete != NULL ) {
  200. idSWFScriptObject & root = GetSWFObject()->GetRootObject();
  201. if ( btnDelete->BindSprite( root ) ) {
  202. btnDelete->SetLabel( "" );
  203. btnDelete->Update();
  204. }
  205. }
  206. }
  207. }
  208. }
  209. cmdBar->Update();
  210. }
  211. }
  212. if ( saveInfo != NULL ) {
  213. saveInfo->Update();
  214. }
  215. if ( options != NULL && options->GetTotalNumberOfOptions() > 0 && options->GetViewIndex() >= options->GetTotalNumberOfOptions() ) {
  216. options->SetViewIndex( options->GetTotalNumberOfOptions() - newSaveOffset );
  217. if ( options->GetViewOffset() > options->GetViewIndex() ) {
  218. options->SetViewOffset( options->GetViewIndex() );
  219. }
  220. options->SetFocusIndex( options->GetViewIndex() - options->GetViewOffset() );
  221. }
  222. }
  223. /*
  224. ========================
  225. idMenuScreen_Shell_Save::ShowScreen
  226. ========================
  227. */
  228. void idMenuScreen_Shell_Save::ShowScreen( const mainMenuTransition_t transitionType ) {
  229. idMenuScreen::ShowScreen( transitionType );
  230. }
  231. /*
  232. ========================
  233. idMenuScreen_Shell_Save::HideScreen
  234. ========================
  235. */
  236. void idMenuScreen_Shell_Save::HideScreen( const mainMenuTransition_t transitionType ) {
  237. idMenuScreen::HideScreen( transitionType );
  238. }
  239. /*
  240. ========================
  241. idMenuScreen_Shell_Save::SaveGame
  242. ========================
  243. */
  244. void idMenuScreen_Shell_Save::SaveGame( int index ) {
  245. const saveGameDetailsList_t & saveGameInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
  246. int newSaveOffset = 1;
  247. bool hasAutosave = false;
  248. for ( int i = 0; i < saveGameInfo.Num(); ++i ) {
  249. if ( saveGameInfo[i].slotName.Icmp( "autosave" ) == 0 ) {
  250. hasAutosave = true;
  251. break;
  252. }
  253. }
  254. if ( saveGameInfo.Num() == MAX_SAVEGAMES || ( ( saveGameInfo.Num() == MAX_SAVEGAMES - 1 ) && !hasAutosave ) ) {
  255. newSaveOffset = 0;
  256. }
  257. if ( index == 0 && newSaveOffset == 1 ) {
  258. // New save...
  259. // Scan all the savegames for the first doom3_xxx slot.
  260. const idStr savePrefix = "doom3_";
  261. uint64 slotMask = 0;
  262. for ( int slot = 0; slot < saveGameInfo.Num(); ++slot ) {
  263. const idSaveGameDetails & details = saveGameInfo[slot];
  264. if ( details.slotName.Icmp( "autosave" ) == 0 ) {
  265. continue;
  266. }
  267. idStr name = details.slotName;
  268. name.ToLower(); // PS3 saves are uppercase ... we need to lower case-ify them for comparison here
  269. name.StripLeading( savePrefix.c_str() );
  270. if ( name.IsNumeric() ) {
  271. int slotNumber = atoi( name.c_str() );
  272. slotMask |= ( 1ULL << slotNumber );
  273. }
  274. }
  275. int slotNumber = 0;
  276. for ( slotNumber = 0; slotNumber < ( sizeof( slotMask ) * 8 ); slotNumber++ ) {
  277. // If the slot isn't used, grab it
  278. if ( !( slotMask & ( 1ULL << slotNumber ) ) ) {
  279. break;
  280. }
  281. }
  282. assert( slotNumber < ( sizeof( slotMask ) * 8 ) );
  283. idStr name = va( "%s%d", savePrefix.c_str(), slotNumber );
  284. cmdSystem->AppendCommandText( va( "savegame %s\n", name.c_str() ) );
  285. // Throw up the saving message...
  286. common->Dialog().ShowSaveIndicator( true );
  287. } else {
  288. class idSWFScriptFunction_OverwriteSave : public idSWFScriptFunction_RefCounted {
  289. public:
  290. idSWFScriptFunction_OverwriteSave( gameDialogMessages_t _msg, bool _accept, int _index, idMenuScreen_Shell_Save * _screen ) {
  291. msg = _msg;
  292. accept = _accept;
  293. index = _index;
  294. screen = _screen;
  295. }
  296. idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ) {
  297. common->Dialog().ClearDialog( msg );
  298. if ( accept && screen != NULL ) {
  299. // Replace the save
  300. if ( index < screen->GetSortedSaves().Num() ) {
  301. idStr name = screen->GetSortedSaves()[ index ].slotName;
  302. cmdSystem->AppendCommandText( va( "savegame %s\n", name.c_str() ) );
  303. // Throw up the saving message...
  304. common->Dialog().ShowSaveIndicator( true );
  305. }
  306. }
  307. return idSWFScriptVar();
  308. }
  309. private:
  310. gameDialogMessages_t msg;
  311. int index;
  312. bool accept;
  313. idMenuScreen_Shell_Save * screen;
  314. };
  315. if ( newSaveOffset == 1 ) {
  316. index--;
  317. }
  318. common->Dialog().AddDialog( GDM_OVERWRITE_SAVE, DIALOG_ACCEPT_CANCEL, new idSWFScriptFunction_OverwriteSave( GDM_OVERWRITE_SAVE, true, index, this ), new idSWFScriptFunction_OverwriteSave( GDM_OVERWRITE_SAVE, false, index, this ), false );
  319. }
  320. }
  321. /*
  322. ========================
  323. idMenuScreen_Shell_Save::DeleteGame
  324. ========================
  325. */
  326. void idMenuScreen_Shell_Save::DeleteGame( int index ) {
  327. class idSWFScriptFunction_DeleteGame : public idSWFScriptFunction_RefCounted {
  328. public:
  329. idSWFScriptFunction_DeleteGame( gameDialogMessages_t _msg, bool _accept, int _index, idMenuScreen_Shell_Save * _screen ) {
  330. msg = _msg;
  331. accept = _accept;
  332. index = _index;
  333. screen = _screen;
  334. }
  335. idSWFScriptVar Call( idSWFScriptObject * thisObject, const idSWFParmList & parms ) {
  336. common->Dialog().ClearDialog( msg );
  337. if ( accept && screen != NULL ) {
  338. if ( index < screen->GetSortedSaves().Num() ) {
  339. session->DeleteSaveGameSync( screen->GetSortedSaves()[ index ].slotName );
  340. }
  341. }
  342. return idSWFScriptVar();
  343. }
  344. private:
  345. gameDialogMessages_t msg;
  346. int index;
  347. bool accept;
  348. idMenuScreen_Shell_Save * screen;
  349. };
  350. bool hasAutosave = false;
  351. const saveGameDetailsList_t & saveInfo = session->GetSaveGameManager().GetEnumeratedSavegames();
  352. for ( int i = 0; i < saveInfo.Num(); ++i ) {
  353. if ( saveInfo[i].slotName.Icmp( "autosave" ) == 0 ) {
  354. hasAutosave = true;
  355. break;
  356. }
  357. }
  358. if ( ( saveInfo.Num() < MAX_SAVEGAMES - 1 ) || ( ( saveInfo.Num() == MAX_SAVEGAMES - 1 ) && hasAutosave ) ) {
  359. index--; // Subtract 1 from the index for 'New Game'
  360. }
  361. common->Dialog().AddDialog( GDM_DELETE_SAVE, DIALOG_ACCEPT_CANCEL, new idSWFScriptFunction_DeleteGame( GDM_DELETE_SAVE, true, index, this ), new idSWFScriptFunction_DeleteGame( GDM_DELETE_SAVE, false, index, this ), false );
  362. }
  363. /*
  364. ========================
  365. idMenuScreen_Shell_Save::HandleAction
  366. ========================
  367. */
  368. bool idMenuScreen_Shell_Save::HandleAction( idWidgetAction & action, const idWidgetEvent & event, idMenuWidget * widget, bool forceHandled ) {
  369. if ( menuData == NULL ) {
  370. return true;
  371. }
  372. if ( menuData->ActiveScreen() != SHELL_AREA_SAVE ) {
  373. return false;
  374. }
  375. widgetAction_t actionType = action.GetType();
  376. const idSWFParmList & parms = action.GetParms();
  377. switch ( actionType ) {
  378. case WIDGET_ACTION_JOY4_ON_PRESS: {
  379. return true;
  380. }
  381. case WIDGET_ACTION_JOY3_ON_PRESS: {
  382. if ( options == NULL ) {
  383. return true;
  384. }
  385. DeleteGame( options->GetViewIndex() );
  386. return true;
  387. }
  388. case WIDGET_ACTION_GO_BACK: {
  389. menuData->SetNextScreen( SHELL_AREA_ROOT, MENU_TRANSITION_SIMPLE );
  390. return true;
  391. }
  392. case WIDGET_ACTION_PRESS_FOCUSED: {
  393. if ( options == NULL ) {
  394. return true;
  395. }
  396. if ( session->GetSaveGameManager().IsWorking() ) {
  397. return true;
  398. }
  399. if ( parms.Num() == 1 ) {
  400. int selectionIndex = parms[0].ToInteger();
  401. if ( selectionIndex != options->GetFocusIndex() ) {
  402. options->SetViewIndex( options->GetViewOffset() + selectionIndex );
  403. options->SetFocusIndex( selectionIndex );
  404. return true;
  405. }
  406. }
  407. SaveGame( options->GetViewIndex() );
  408. return true;
  409. }
  410. case WIDGET_ACTION_SCROLL_VERTICAL: {
  411. return true;
  412. }
  413. }
  414. return idMenuWidget::HandleAction( action, event, widget, forceHandled );
  415. }