Skip to content

Commit

Permalink
callback test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Apr 1, 2024
1 parent 449cb70 commit ee2d42d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
13 changes: 11 additions & 2 deletions src/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// Headers
#include <cassert>
#include "async_handler.h"
#include "input_buttons.h"
#include "scene.h"
#include "graphics.h"
#include "input.h"
Expand Down Expand Up @@ -258,9 +259,17 @@ void Scene::Update() {
}
auto* sel_window = static_cast<Window_Selectable*>(window);
int index = sel_window->CursorHitTest({mouse_pos.x - window->GetX(), mouse_pos.y - window->GetY()});
if (index >= 0 && index != sel_window->GetIndex()) {
if (index >= 0) {
// FIXME: Index changed callback?
sel_window->SetIndex(index);
//sel_window->SetIndex(index);
if (sel_window->DecisionFn && Input::IsTriggered(Input::MOUSE_LEFT)) {
sel_window->SetIndex(index);
sel_window->DecisionFn(index);
}
if (sel_window->CancelFn && Input::IsTriggered(Input::MOUSE_RIGHT)) {
sel_window->SetIndex(index);
sel_window->CancelFn(index);
}
}
}

Expand Down
54 changes: 26 additions & 28 deletions src/scene_title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ void Scene_Title::Start() {
CreateCommandWindow();
CreateTranslationWindow();
CreateHelpWindow();

command_window->DecisionFn = [&](int index) {
if (index == indices.new_game) { // New Game
CommandNewGame();
} else if (index == indices.continue_game) { // Load Game
CommandContinue();
} else if (index == indices.import) { // Import (multi-part games)
CommandImport();
} else if (index == indices.settings) {
CommandSettings();
} else if (index == indices.translate) { // Choose a Translation (Language)
CommandTranslation();
} else if (index == indices.exit) { // Exit Game
CommandShutdown();
}
};

translate_window->DecisionFn = [&](int index) {
ChangeLanguage(lang_dirs.at(index));
};

translate_window->CancelFn = [&](int index) {
// Switch back
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
HideTranslationWindow();
};
}

void Scene_Title::CreateHelpWindow() {
Expand Down Expand Up @@ -158,34 +184,6 @@ void Scene_Title::vUpdate() {
} else {
translate_window->Update();
}

if (Input::IsTriggered(Input::DECISION)) {
if (active_window == 0) {
int index = command_window->GetIndex();
if (index == indices.new_game) { // New Game
CommandNewGame();
} else if (index == indices.continue_game) { // Load Game
CommandContinue();
} else if (index == indices.import) { // Import (multi-part games)
CommandImport();
} else if (index == indices.settings) {
CommandSettings();
} else if (index == indices.translate) { // Choose a Translation (Language)
CommandTranslation();
} else if (index == indices.exit) { // Exit Game
CommandShutdown();
}
} else if (active_window == 1) {
int index = translate_window->GetIndex();
ChangeLanguage(lang_dirs.at(index));
}
} else if (Input::IsTriggered(Input::CANCEL)) {
if (active_window == 1) {
// Switch back
Main_Data::game_system->SePlay(Main_Data::game_system->GetSystemSE(Main_Data::game_system->SFX_Cancel));
HideTranslationWindow();
}
}
}

void Scene_Title::Refresh() {
Expand Down
10 changes: 10 additions & 0 deletions src/window_selectable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "window_selectable.h"
#include "game_system.h"
#include "input.h"
#include "input_buttons.h"
#include "util_macro.h"
#include "bitmap.h"

Expand Down Expand Up @@ -237,7 +238,16 @@ void Window_Selectable::Update() {
return;
}
}

if (Input::IsTriggered(Input::DECISION) && DecisionFn) {
DecisionFn(index);
}
if (Input::IsTriggered(Input::CANCEL) && CancelFn) {
CancelFn(index);
}

}

if (active && help_window != NULL) {
UpdateHelp();
}
Expand Down
4 changes: 3 additions & 1 deletion src/window_selectable.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Window_Selectable: public Window_Base {
*/
virtual Rect GetItemRect(int index);

std::function<void(int)> DecisionFn;
std::function<void(int)> CancelFn;

/**
* Function called by the base UpdateHelp() implementation.
* Passes in the Help Window and the current selected index
Expand All @@ -74,7 +77,6 @@ class Window_Selectable: public Window_Base {
*/
void SetHelpWindow(Window_Help* nhelp_window);


/**
* Returns a rectangle indicating the cursor location for the passed index.
*
Expand Down

0 comments on commit ee2d42d

Please sign in to comment.