Implement noconfirmation hotkey for deleting savegames in the save dialog. Refs ...
[0ad.git] / binaries / data / mods / public / gui / savedgames / save.js
blobc702b96dc40783effcd1358450ea26c004010b7a
1 var g_Descriptions;
2 var g_SavedGameData;
4 function selectDescription()
6         let gameSelection = Engine.GetGUIObjectByName("gameSelection");
7         if (gameSelection.selected == -1)
8                 return;
10         let gameID = gameSelection.list_data[gameSelection.selected];
11         Engine.GetGUIObjectByName("deleteGameButton").enabled = true;
12         Engine.GetGUIObjectByName("saveGameDesc").caption = g_Descriptions[gameID];
15 function init(data)
17         g_SavedGameData = data && data.savedGameData || {};
18         let simulationState = Engine.GuiInterfaceCall("GetSimulationState");
19         g_SavedGameData.timeElapsed = simulationState.timeElapsed;
20         g_SavedGameData.states = simulationState.players.map(pState => pState.state);
22         let gameSelection = Engine.GetGUIObjectByName("gameSelection");
23         Engine.GetGUIObjectByName("deleteGameButton").enabled = false;
25         let savedGames = Engine.GetSavedGames().sort(sortDecreasingDate);
26         if (!savedGames.length)
27         {
28                 gameSelection.list = [translate("No saved games found")];
29                 gameSelection.selected = -1;
30                 return;
31         }
33         g_Descriptions = {};
34         for (let game of savedGames)
35                 g_Descriptions[game.id] = game.metadata.description || "";
37         gameSelection.list = savedGames.map(game => generateLabel(game.metadata));
38         gameSelection.list_data = savedGames.map(game => game.id);
39         gameSelection.selected = -1;
41         Engine.GetGUIObjectByName("deleteGameButton").tooltip = deleteTooltip();
44 function saveGame()
46         let gameSelection = Engine.GetGUIObjectByName("gameSelection");
47         let gameLabel = gameSelection.list[gameSelection.selected];
48         let gameID = gameSelection.list_data[gameSelection.selected];
49         let desc = Engine.GetGUIObjectByName("saveGameDesc").caption;
50         let name = gameID || "savegame";
52         if (gameSelection.selected == -1)
53         {
54                 reallySaveGame(name, desc, true);
55                 return;
56         }
58         messageBox(
59                 500, 200,
60                 sprintf(translate("\"%(label)s\""), { "label": gameLabel }) + "\n" +
61                         translate("Saved game will be permanently overwritten, are you sure?"),
62                 translate("OVERWRITE SAVE"),
63                 [translate("No"), translate("Yes")],
64                 [null, function(){ reallySaveGame(name, desc, false); }]
65         );
68 function reallySaveGame(name, desc, nameIsPrefix)
70         if (nameIsPrefix)
71                 Engine.SaveGamePrefix(name, desc, g_SavedGameData);
72         else
73                 Engine.SaveGame(name, desc, g_SavedGameData);
75         closeSave();
78 function closeSave()
80         Engine.PopGuiPageCB(0);
83 // HACK: Engine.SaveGame* expects this function to be defined on the current page.
84 // That's why we have to pass the data to this page even if we don't need it.
85 function getSavedGameData()
87         return g_SavedGameData;