Refactor TUI input handling and layout management

This commit is contained in:
scawful
2024-12-29 22:01:49 -05:00
parent a9cb2700ef
commit e92f486efa
2 changed files with 22 additions and 18 deletions

View File

@@ -8,23 +8,24 @@
namespace yaze { namespace yaze {
namespace cli { namespace cli {
using namespace ftxui;
namespace { namespace {
bool HandleInput(ftxui::Event &event, int &selected) { bool HandleInput(ftxui::Event &event, int &selected) {
using namespace ftxui;
if (event == Event::ArrowDown || event == Event::Character('j')) { if (event == Event::ArrowDown || event == Event::Character('j')) {
selected++; selected++;
return true; return true;
} }
if (event == Event::ArrowUp || event == Event::Character('k')) { if (event == Event::ArrowUp || event == Event::Character('k')) {
if (selected != 0) selected--; if (selected != 0)
selected--;
return true; return true;
} }
return false; return false;
} }
} // namespace } // namespace
void ShowMain() { void ShowMain() {
using namespace ftxui;
Context context; Context context;
std::vector<std::string> entries = { std::vector<std::string> entries = {
@@ -34,14 +35,18 @@ void ShowMain() {
MenuOption option; MenuOption option;
auto menu = Menu(&entries, &selected, option); auto menu = Menu(&entries, &selected, option);
menu = CatchEvent(
menu, [&selected](Event event) { return HandleInput(event, selected); });
Element main_document = gridbox({ auto layout = Container::Vertical({
{text("z3ed: The Legend of Zelda: A Link to the Past") | bold | flex}, menu,
{menu->Render() | border | flex},
}); });
auto main_component = Renderer(layout, [&] {
auto main_component = Renderer([&] { return main_document; }); return vbox({
auto screen = ScreenInteractive::TerminalOutput(); menu->Render(),
});
});
auto screen = ScreenInteractive::FitComponent();
// Exit the loop when "Exit" is selected // Exit the loop when "Exit" is selected
main_component = CatchEvent(main_component, [&](Event event) { main_component = CatchEvent(main_component, [&](Event event) {
@@ -53,17 +58,13 @@ void ShowMain() {
screen.ExitLoopClosure()(); screen.ExitLoopClosure()();
return true; return true;
} }
return HandleInput(event, selected); return false;
}); });
screen.Loop(main_component); screen.Loop(main_component);
} }
void DrawPaletteEditor(Rom *rom) { void DrawPaletteEditor(Rom *rom) { auto palette_groups = rom->palette_group(); }
using namespace ftxui;
auto palette_groups = rom->palette_group(); } // namespace cli
} } // namespace yaze
} // namespace cli
} // namespace yaze

View File

@@ -12,6 +12,9 @@ namespace cli {
struct Context { struct Context {
bool is_loaded = false; bool is_loaded = false;
ftxui::Component layout;
ftxui::Component main_component;
}; };
void ShowMain(); void ShowMain();