GUI is now in its own unit.
This commit is contained in:
parent
6d4aa9390a
commit
ceba46c658
6 changed files with 127 additions and 89 deletions
99
gui.cpp
Normal file
99
gui.cpp
Normal file
|
@ -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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue