GUI is now in its own unit.

This commit is contained in:
Zed A. Shaw 2024-08-20 18:02:46 -04:00
parent 6d4aa9390a
commit ceba46c658
6 changed files with 127 additions and 89 deletions

View file

@ -1,4 +1,5 @@
#include "dbc.hpp"
#include "gui.hpp"
#include "game_engine.hpp"
#include <chrono> // for milliseconds
#include <efsw/efsw.hpp>
@ -35,14 +36,8 @@ namespace fs = std::filesystem;
const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);
vector<string> lines;
#define BUF_MAX 1024
void output(const string &msg) {
lines.push_back(msg);
}
class UpdateListener : public efsw::FileWatchListener {
public:
bool changes = false;
@ -131,88 +126,6 @@ void run_build(GameEngine &game, const char* command) {
}
ButtonOption Style() {
auto option = ButtonOption::Animated();
option.transform = [](const EntryState& s) {
auto element = paragraph(s.label);
if (s.focused) {
element |= bold;
}
return element | center | borderEmpty | flex;
};
return option;
}
int main_loop(UpdateListener* listener, efsw::FileWatcher* fileWatcher, GameEngine &game, const char *build_cmd) {
auto screen = ScreenInteractive::Fullscreen();
screen.TrackMouse(true);
// Create a component counting the number of frames drawn and event handled.
float scroll_x = 0.1;
float scroll_y = 1.0;
auto status = Renderer([&] {
return vbox({
paragraph(fmt::format("HP {}", game.hit_points)),
separator(),
hbox({
text("HP"),
gauge(game.hit_points / 100.0f),
}),
});
});
auto content = Renderer([&] {
vector<Element> output;
for(const auto line : lines) {
output.push_back(text(line));
}
return vbox(output);
});
auto scrollable_content = Renderer(content,
[&, content] {
return content->Render()
| focusPositionRelative(scroll_x, scroll_y)
| frame | flex;
});
auto component = Renderer(scrollable_content, [&] {
return vbox({
status->Render(),
separator(),
scrollable_content->Render() | vscroll_indicator | yframe | size(HEIGHT, LESS_THAN, 20),
}) |
border;
});
component |= CatchEvent([&](Event) -> bool {
return false;
});
Loop loop(&screen, component);
while (!loop.HasQuitted()) {
fileWatcher->watch();
if(listener->changes) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
output(format("CHANGES! Running build {}", build_cmd));
run_build(game, build_cmd);
listener->reset_state();
}
loop.RunOnce();
screen.Post(Event::Custom);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
git_repository* repo = nullptr;
@ -239,7 +152,18 @@ int main(int argc, char *argv[])
GameEngine game{100};
int rc = main_loop(listener, fileWatcher, game, build_cmd);
int rc = main_loop(game, [&] {
fileWatcher->watch();
if(listener->changes) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
output(format("CHANGES! Running build {}", build_cmd));
run_build(game, build_cmd);
listener->reset_state();
}
return 0;
});
dbc::check(rc == 0, "Invalid return from main_loop.");
git_libgit2_shutdown();