Implement menu navigation and input handling in TUI

This commit is contained in:
scawful
2024-12-28 21:34:39 -05:00
parent 295f512826
commit c74e1e660c
2 changed files with 62 additions and 16 deletions

View File

@@ -1,29 +1,69 @@
#include "tui.h" #include "tui.h"
#include <ftxui/component/component.hpp> #include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
namespace yaze { namespace yaze {
namespace cli { namespace cli {
namespace tui {
namespace {
bool HandleInput(ftxui::Event &event, int &selected) {
using namespace ftxui;
if (event == Event::ArrowDown || event == Event::Character('j')) {
selected++;
return true;
}
if (event == Event::ArrowUp || event == Event::Character('k')) {
if (selected != 0) selected--;
return true;
}
return false;
}
} // namespace
void ShowMain() { void ShowMain() {
using namespace ftxui; using namespace ftxui;
Context context;
std::vector<std::string> entries = {
"Palette Editor", "Tile Editor", "Sprite Editor", "Map Editor", "Exit",
};
int selected = 0;
MenuOption option;
auto menu = Menu(&entries, &selected, option);
Element main_document = gridbox({ Element main_document = gridbox({
{text("z3ed: The Legend of Zelda: A Link to the Past") | bold | flex}, {text("z3ed: The Legend of Zelda: A Link to the Past") | bold | flex},
{text("left") | border, text("middle") | border | flex}, {menu->Render() | border | flex},
{text("left") | border, text("middle") | border | flex},
}); });
auto screen = Screen::Create(Dimension::Full(), // Width auto main_component = Renderer([&] { return main_document; });
Dimension::Fit(main_document) // Height auto screen = ScreenInteractive::TerminalOutput();
);
Render(screen, main_document); // Exit the loop when "Exit" is selected
screen.Print(); main_component = CatchEvent(main_component, [&](Event event) {
if (event == Event::Return && selected == 4) {
screen.ExitLoopClosure()();
return true;
}
if (event == Event::Character('q')) {
screen.ExitLoopClosure()();
return true;
}
return HandleInput(event, selected);
});
screen.Loop(main_component);
} }
} // namespace tui void DrawPaletteEditor(Rom *rom) {
} // namespace cli using namespace ftxui;
} // namespace yaze
auto palette_groups = rom->palette_group();
}
} // namespace cli
} // namespace yaze

View File

@@ -5,14 +5,20 @@
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp> #include <ftxui/screen/screen.hpp>
#include "app/rom.h"
namespace yaze { namespace yaze {
namespace cli { namespace cli {
namespace tui {
struct Context {
bool is_loaded = false;
};
void ShowMain(); void ShowMain();
} void DrawPaletteEditor(Rom *rom);
} // namespace cli
} // namespace yaze
#endif // YAZE_CLI_TUI_H } // namespace cli
} // namespace yaze
#endif // YAZE_CLI_TUI_H