Skip to content

Commit d2d92eb

Browse files
committed
Scene_Title: Refactor handling of command options:
- Make options code more dynamic, similar to Scene_Menu, so the option positions could theoretically be swapped or removed entirely - Added static field "force_cursor_index" to allow overriding the default cursor position
1 parent 05ad681 commit d2d92eb

File tree

2 files changed

+90
-22
lines changed

2 files changed

+90
-22
lines changed

src/scene_title.cpp

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include "baseui.h"
4646
#include <lcf/reader_util.h>
4747

48+
Scene_Title::CommandOptionType Scene_Title::force_cursor_index = Scene_Title::CommandOption_None;
49+
4850
Scene_Title::Scene_Title() {
4951
type = Scene::Title;
5052
}
@@ -108,7 +110,7 @@ void Scene_Title::Continue(SceneType prev_scene) {
108110
}
109111

110112
void Scene_Title::TransitionIn(SceneType prev_scene) {
111-
if (Game_Battle::battle_test.enabled || !Check2k3ShowTitle() || Player::game_config.new_game.Get())
113+
if (Game_Battle::battle_test.enabled || CheckStartNewGame())
112114
return;
113115

114116
if (prev_scene == Scene::Load || Player::hide_title_flag) {
@@ -133,7 +135,7 @@ void Scene_Title::vUpdate() {
133135
return;
134136
}
135137

136-
if (!Check2k3ShowTitle() || Player::game_config.new_game.Get()) {
138+
if (CheckStartNewGame()) {
137139
Player::SetupNewGame();
138140
if (Player::debug_flag && Player::hide_title_flag) {
139141
Scene::Push(std::make_shared<Scene_Load>());
@@ -170,12 +172,26 @@ void Scene_Title::vUpdate() {
170172
void Scene_Title::Refresh() {
171173
// Enable load game if available
172174
continue_enabled = FileFinder::HasSavegame();
173-
if (continue_enabled) {
175+
176+
if (force_cursor_index != CommandOption_None) {
177+
if (auto idx = GetCommandIndex(force_cursor_index); idx != -1) {
178+
command_window->SetIndex(idx);
179+
}
180+
force_cursor_index = CommandOption_None;
181+
} else if (continue_enabled) {
174182
command_window->SetIndex(1);
175183
}
176184
command_window->SetItemEnabled(1, continue_enabled);
177185
}
178186

187+
int Scene_Title::GetCommandIndex(CommandOptionType cmd) const {
188+
auto it = std::find(command_options.begin(), command_options.end(), cmd);
189+
if (it != command_options.end()) {
190+
return (it - command_options.begin());
191+
}
192+
return -1;
193+
}
194+
179195
void Scene_Title::OnTranslationChanged() {
180196
Start();
181197

@@ -211,35 +227,59 @@ void Scene_Title::RepositionWindow(Window_Command& window, bool center_vertical)
211227
void Scene_Title::CreateCommandWindow() {
212228
// Create Options Window
213229
std::vector<std::string> options;
214-
options.push_back(ToString(lcf::Data::terms.new_game));
215-
options.push_back(ToString(lcf::Data::terms.load_game));
230+
231+
using Cmd = CommandOptionType;
216232

217233
// Reset index to fix issues on reuse.
218234
indices = CommandIndices();
219235

236+
command_options.push_back(Cmd::NewGame);
237+
command_options.push_back(Cmd::ContinueGame);
238+
220239
// Set "Import" based on metadata
221240
if (Player::meta->IsImportEnabled()) {
222-
options.push_back(Player::meta->GetExVocabImportSaveTitleText());
223-
indices.import = indices.exit;
224-
indices.exit++;
241+
command_options.push_back(Cmd::Import);
225242
}
226-
227243
// Set "Settings" based on the configuration
228244
if (Player::player_config.settings_in_title.Get()) {
229-
// FIXME: Translation? Not shown by default though
230-
options.push_back("Settings");
231-
indices.settings = indices.exit;
232-
indices.exit++;
245+
command_options.push_back(Cmd::Settings);
233246
}
234-
235247
// Set "Translate" based on metadata
236248
if (Player::translation.HasTranslations() && Player::player_config.lang_select_in_title.Get()) {
237-
options.push_back(Player::meta->GetExVocabTranslateTitleText());
238-
indices.translate = indices.exit;
239-
indices.exit++;
249+
command_options.push_back(Cmd::Translate);
240250
}
241251

242-
options.push_back(ToString(lcf::Data::terms.exit_game));
252+
command_options.push_back(Cmd::Exit);
253+
254+
for (int i = 0; i < command_options.size(); ++i) {
255+
switch (command_options[i]) {
256+
case Cmd::NewGame:
257+
indices.new_game = i;
258+
options.push_back(ToString(lcf::Data::terms.new_game));
259+
break;
260+
case Cmd::ContinueGame:
261+
indices.continue_game = i;
262+
options.push_back(ToString(lcf::Data::terms.load_game));
263+
break;
264+
case Cmd::Import:
265+
indices.import = i;
266+
options.push_back(Player::meta->GetExVocabImportSaveTitleText());
267+
break;
268+
case Cmd::Settings:
269+
indices.settings = i;
270+
// FIXME: Translation? Not shown by default though
271+
options.push_back("Settings");
272+
break;
273+
case Cmd::Translate:
274+
indices.translate = i;
275+
options.push_back(Player::meta->GetExVocabTranslateTitleText());
276+
break;
277+
case Cmd::Exit:
278+
indices.exit = i;
279+
options.push_back(ToString(lcf::Data::terms.exit_game));
280+
break;
281+
}
282+
}
243283

244284
command_window.reset(new Window_Command(options));
245285
RepositionWindow(*command_window, Player::hide_title_flag);
@@ -267,7 +307,7 @@ void Scene_Title::PlayTitleMusic() {
267307

268308
bool Scene_Title::CheckEnableTitleGraphicAndMusic() {
269309
return Check2k3ShowTitle() &&
270-
!Player::game_config.new_game.Get() &&
310+
!CheckStartNewGame() &&
271311
!Game_Battle::battle_test.enabled &&
272312
!Player::hide_title_flag;
273313
}
@@ -280,6 +320,10 @@ bool Scene_Title::CheckValidPlayerLocation() {
280320
return (lcf::Data::treemap.start.party_map_id > 0);
281321
}
282322

323+
bool Scene_Title::CheckStartNewGame() {
324+
return !Check2k3ShowTitle() || Player::game_config.new_game.Get();
325+
}
326+
283327
void Scene_Title::CommandNewGame() {
284328
if (!CheckValidPlayerLocation()) {
285329
Output::Warning("The game has no start location set.");

src/scene_title.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ class Scene_Title : public Scene {
7979
*/
8080
bool CheckValidPlayerLocation();
8181

82+
/**
83+
* Checks whether to immediately start into a new game.
84+
*
85+
* @return true if the title screen should be skipped
86+
*/
87+
bool CheckStartNewGame();
88+
8289
/**
8390
* Option New Game.
8491
* Starts a new game.
@@ -119,6 +126,20 @@ class Scene_Title : public Scene {
119126
*/
120127
void OnGameStart();
121128

129+
enum class CommandOptionType {
130+
NewGame = 1,
131+
ContinueGame,
132+
Import,
133+
Settings,
134+
Translate,
135+
Exit
136+
};
137+
138+
int GetCommandIndex(CommandOptionType cmd) const;
139+
140+
static const CommandOptionType CommandOption_None = static_cast<CommandOptionType>(0);
141+
static CommandOptionType force_cursor_index;
142+
122143
/**
123144
* Moves a window (typically the New/Continue/Quit menu) to the middle or bottom-center of the screen.
124145
* @param window The window to resposition.
@@ -142,15 +163,18 @@ class Scene_Title : public Scene {
142163
* Stored in a struct for easy resetting, as Scene_Title can be reused.
143164
*/
144165
struct CommandIndices {
145-
int new_game = 0;
146-
int continue_game = 1;
166+
int new_game = 0;
167+
int continue_game = 1;
147168
int import = -1;
148169
int settings = -1;
149170
int translate = -1;
150-
int exit = 2;
171+
int exit = 2;
151172
};
152173
CommandIndices indices;
153174

175+
/** Options available in the menu. */
176+
std::vector<CommandOptionType> command_options;
177+
154178
/** Contains the state of continue button. */
155179
bool continue_enabled = false;
156180

0 commit comments

Comments
 (0)