GUI is now in its own unit.

master
Zed A. Shaw 3 months ago
parent 6d4aa9390a
commit ceba46c658
  1. 2
      dbc.hpp
  2. 102
      escape_turings_tarpit.cpp
  3. 2
      game_engine.hpp
  4. 99
      gui.cpp
  5. 10
      gui.hpp
  6. 1
      meson.build

@ -1,3 +1,5 @@
#pragma once
#include <string> #include <string>
#include <fmt/core.h> #include <fmt/core.h>
#include <functional> #include <functional>

@ -1,4 +1,5 @@
#include "dbc.hpp" #include "dbc.hpp"
#include "gui.hpp"
#include "game_engine.hpp" #include "game_engine.hpp"
#include <chrono> // for milliseconds #include <chrono> // for milliseconds
#include <efsw/efsw.hpp> #include <efsw/efsw.hpp>
@ -35,14 +36,8 @@ namespace fs = std::filesystem;
const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);
vector<string> lines;
#define BUF_MAX 1024 #define BUF_MAX 1024
void output(const string &msg) {
lines.push_back(msg);
}
class UpdateListener : public efsw::FileWatchListener { class UpdateListener : public efsw::FileWatchListener {
public: public:
bool changes = false; 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[]) int main(int argc, char *argv[])
{ {
git_repository* repo = nullptr; git_repository* repo = nullptr;
@ -239,7 +152,18 @@ int main(int argc, char *argv[])
GameEngine game{100}; 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."); dbc::check(rc == 0, "Invalid return from main_loop.");
git_libgit2_shutdown(); git_libgit2_shutdown();

@ -1,3 +1,5 @@
#pragma once
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>

@ -0,0 +1,99 @@
#include "gui.hpp"
#include <stdlib.h> // for EXIT_SUCCESS
#include <chrono> // for milliseconds
#include <fmt/core.h>
#include <ftxui/component/event.hpp> // for Event
#include <ftxui/component/mouse.hpp> // for ftxui
#include <ftxui/dom/elements.hpp> // for text, separator, Element, operator|, vbox, border
#include <memory> // for allocator, shared_ptr
#include <string> // for operator+, to_string
#include <thread> // for sleep_for
#include "ftxui/component/component.hpp" // for CatchEvent, Renderer, operator|=
#include "ftxui/component/loop.hpp" // for Loop
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include <vector>
using namespace ftxui;
using namespace std;
using namespace fmt;
namespace fs = std::filesystem;
vector<string> lines;
void output(const string &msg) {
lines.push_back(msg);
}
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(GameEngine &game, std::function<bool()> runner) {
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()) {
int run_error = runner();
loop.RunOnce();
screen.Post(Event::Custom);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return EXIT_SUCCESS;
}

@ -0,0 +1,10 @@
#pragma once
#include <efsw/efsw.hpp>
#include "game_engine.hpp"
#include <filesystem>
#include <string>
void output(const string &msg);
int main_loop(GameEngine &game, std::function<bool()> runner);

@ -29,6 +29,7 @@ dependencies = [
executable('escape_turings_tarpit', executable('escape_turings_tarpit',
['game_engine.cpp', ['game_engine.cpp',
'gui.cpp',
'escape_turings_tarpit.cpp'], 'escape_turings_tarpit.cpp'],
dependencies: dependencies) dependencies: dependencies)

Loading…
Cancel
Save