A weird game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
turings-tarpit/gui.cpp

102 lines
2.8 KiB

#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;
void GUI::output(const string &msg) {
lines.push_back(msg);
}
int GUI::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({
text(fmt::format("HP {} | Hits Taken {} | Round {} | Streak {}", game.hit_points, game.hits_taken, game.rounds, game.streak)),
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 game_stuff = Renderer([&] {
return vbox({
text("Welcome to...Turing's Tarpit!"),
separator(),
hbox({
text("left") | border | flex ,
text("middle") | border | flex,
text("right") | border | flex,
}) | yflex_grow
});
});
auto build_log = Renderer(content,
[&, content] {
return content->Render()
| focusPositionRelative(scroll_x, scroll_y)
| frame | flex;
});
auto component = Renderer(build_log, [&] {
return vbox({
game_stuff->Render() | flex | size(HEIGHT, GREATER_THAN, 20),
separator(),
build_log->Render() | vscroll_indicator | yframe | yflex_grow,
separator(),
status->Render()
}) | border;
});
component |= CatchEvent([&](Event) -> bool {
return false;
});
Loop loop(&screen, component);
while (!loop.HasQuitted()) {
int run_error = runner();
if(run_error != 0) output("RUNNER ERROR!!!! CATASTROPHIC!!!");
loop.RunOnce();
screen.Post(Event::Custom);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return EXIT_SUCCESS;
}