1
+ #include " SaveLoadMenuGUI.h"
2
+
3
+ #include " ActivityMan.h"
4
+ #include " PresetMan.h"
5
+ #include " WindowMan.h"
6
+
7
+ #include " GUI.h"
8
+ #include " AllegroScreen.h"
9
+ #include " GUIInputWrapper.h"
10
+ #include " GUICollectionBox.h"
11
+ #include " GUILabel.h"
12
+ #include " GUIButton.h"
13
+ #include " GUIListBox.h"
14
+ #include " GUITextBox.h"
15
+
16
+ namespace RTE {
17
+
18
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19
+
20
+ SaveLoadMenuGUI::SaveLoadMenuGUI (AllegroScreen *guiScreen, GUIInputWrapper *guiInput, bool createForPauseMenu) {
21
+ m_GUIControlManager = std::make_unique<GUIControlManager>();
22
+ RTEAssert (m_GUIControlManager->Create (guiScreen, guiInput, " Base.rte/GUIs/Skins/Menus" , " MainMenuSubMenuSkin.ini" ), " Failed to create GUI Control Manager and load it from Base.rte/GUIs/Skins/Menus/MainMenuSubMenuSkin.ini" );
23
+ m_GUIControlManager->Load (" Base.rte/GUIs/SaveLoadMenuGUI.ini" );
24
+
25
+ int rootBoxMaxWidth = g_WindowMan.FullyCoversAllDisplays () ? g_WindowMan.GetPrimaryWindowDisplayWidth () / g_WindowMan.GetResMultiplier () : g_WindowMan.GetResX ();
26
+
27
+ GUICollectionBox *rootBox = dynamic_cast <GUICollectionBox *>(m_GUIControlManager->GetControl (" root" ));
28
+ rootBox->Resize (rootBoxMaxWidth, g_WindowMan.GetResY ());
29
+
30
+ GUICollectionBox *saveGameMenuBox = dynamic_cast <GUICollectionBox *>(m_GUIControlManager->GetControl (" CollectionBoxSaveGameMenu" ));
31
+ saveGameMenuBox->CenterInParent (true , true );
32
+ saveGameMenuBox->SetPositionAbs (saveGameMenuBox->GetXPos (), (rootBox->GetHeight () < 540 ) ? saveGameMenuBox->GetYPos () - 15 : 140 );
33
+
34
+ m_BackToMainButton = dynamic_cast <GUIButton *>(m_GUIControlManager->GetControl (" ButtonBackToMainMenu" ));
35
+
36
+ if (createForPauseMenu) {
37
+ m_BackToMainButton->SetSize (120 , 20 );
38
+ m_BackToMainButton->SetText (" Back to Pause Menu" );
39
+ }
40
+ m_BackToMainButton->SetPositionAbs ((rootBox->GetWidth () - m_BackToMainButton->GetWidth ()) / 2 , saveGameMenuBox->GetYPos () + saveGameMenuBox->GetHeight () + 10 );
41
+
42
+ m_SaveGamesListBox = dynamic_cast <GUIListBox *>(m_GUIControlManager->GetControl (" ListBoxSaveGames" ));
43
+ m_SaveGamesListBox->SetMouseScrolling (true );
44
+ m_SaveGamesListBox->SetScrollBarThickness (15 );
45
+ m_SaveGamesListBox->SetScrollBarPadding (2 );
46
+
47
+ m_SaveGameName = dynamic_cast <GUITextBox *>(m_GUIControlManager->GetControl (" SaveGameName" ));
48
+ m_LoadButton = dynamic_cast <GUIButton *>(m_GUIControlManager->GetControl (" ButtonLoad" ));
49
+ m_CreateButton = dynamic_cast <GUIButton *>(m_GUIControlManager->GetControl (" ButtonCreate" ));
50
+ m_DescriptionLabel = dynamic_cast <GUILabel *>(m_GUIControlManager->GetControl (" LabelDescription" ));
51
+
52
+ m_SaveGamesFetched = false ;
53
+ }
54
+
55
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56
+
57
+ void SaveLoadMenuGUI::PopulateSaveGamesList () {
58
+ m_SaveGames.clear ();
59
+ m_SaveGamesListBox->ClearList ();
60
+
61
+ std::string saveFilePath = g_PresetMan.GetFullModulePath (c_UserScriptedSavesModuleName) + " /" ;
62
+ for (const auto &entry : std::filesystem::directory_iterator (saveFilePath)) {
63
+ if (entry.path ().extension () == " .ini" && entry.path ().filename () != " Index.ini" ) {
64
+ SaveRecord record;
65
+ record.SaveName = entry.path ().stem ().string ();
66
+ record.SaveDate = entry.last_write_time ();
67
+ m_SaveGames.push_back (record);
68
+ }
69
+ }
70
+
71
+ std::sort (m_SaveGames.begin (), m_SaveGames.end ());
72
+
73
+ for (int i = 0 ; i < m_SaveGames.size (); i++) {
74
+ m_SaveGamesListBox->AddItem (m_SaveGames.at (i).SaveName , std::string (), nullptr , nullptr , i);
75
+ }
76
+
77
+ m_SaveGamesListBox->ScrollToTop ();
78
+ m_SaveGamesFetched = true ;
79
+ }
80
+
81
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82
+
83
+ void SaveLoadMenuGUI::LoadSave () {
84
+ bool success = g_ActivityMan.LoadAndLaunchGame (m_SaveGameName->GetText ());
85
+ if (success) {
86
+ g_GUISound.ConfirmSound ()->Play ();
87
+ } else {
88
+ g_GUISound.UserErrorSound ()->Play ();
89
+ }
90
+ }
91
+
92
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93
+
94
+ void SaveLoadMenuGUI::CreateSave () {
95
+ bool success = g_ActivityMan.SaveCurrentGame (m_SaveGameName->GetText ());
96
+ if (success) {
97
+ g_GUISound.ConfirmSound ()->Play ();
98
+ } else {
99
+ g_GUISound.UserErrorSound ()->Play ();
100
+ }
101
+ PopulateSaveGamesList ();
102
+ }
103
+
104
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105
+
106
+ bool SaveLoadMenuGUI::HandleInputEvents () {
107
+ if (!ListsFetched ()) {
108
+ PopulateSaveGamesList ();
109
+ }
110
+ m_GUIControlManager->Update ();
111
+
112
+ GUIEvent guiEvent;
113
+ while (m_GUIControlManager->GetEvent (&guiEvent)) {
114
+ if (guiEvent.GetType () == GUIEvent::Command) {
115
+ if (guiEvent.GetControl () == m_BackToMainButton) {
116
+ return true ;
117
+ } else if (guiEvent.GetControl () == m_LoadButton) {
118
+ LoadSave ();
119
+ } else if (guiEvent.GetControl () == m_CreateButton) {
120
+ CreateSave ();
121
+ }
122
+ } else if (guiEvent.GetType () == GUIEvent::Notification) {
123
+ if (guiEvent.GetMsg () == GUIButton::Focused && dynamic_cast <GUIButton *>(guiEvent.GetControl ())) {
124
+ g_GUISound.SelectionChangeSound ()->Play ();
125
+ }
126
+
127
+ if (guiEvent.GetControl () == m_SaveGamesListBox && (guiEvent.GetMsg () == GUIListBox::Select && m_SaveGamesListBox->GetSelectedIndex () > -1 )) {
128
+ const SaveRecord &record = m_SaveGames.at (m_SaveGamesListBox->GetSelected ()->m_ExtraIndex );
129
+ // m_DescriptionLabel->SetText(record.GetDisplayString());
130
+ m_SaveGameName->SetText (record.SaveName );
131
+ }
132
+ }
133
+ }
134
+ return false ;
135
+ }
136
+
137
+ // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
138
+
139
+ void SaveLoadMenuGUI::Draw () const {
140
+ m_GUIControlManager->Draw ();
141
+ }
142
+ }
0 commit comments