Finally got the buttons working on a simple UI that also does other things behind the scenes. Need to study FTXUI more though as I didn't catch the error with Render().

master
Zed A. Shaw 5 months ago
parent aee2ac5b27
commit 20999eddd7
  1. 101
      ftxtest.cpp

@ -1,24 +1,83 @@
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/screen.hpp>
#include <iostream>
int main(void) {
using namespace ftxui;
// Define the document
Element document =
hbox({
text("left") | border,
text("middle") | border | flex,
text("right") | border,
});
auto screen = Screen::Create(
Dimension::Full(), // Width
Dimension::Fit(document) // Height
);
Render(screen, document);
screen.Print();
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#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
using namespace ftxui;
ButtonOption Style() {
auto option = ButtonOption::Animated();
option.transform = [](const EntryState& s) {
auto element = text(s.label);
if (s.focused) {
element |= bold;
}
return element | center | borderEmpty | flex;
};
return option;
}
int main() {
auto screen = ScreenInteractive::Fullscreen();
// Create a component counting the number of frames drawn and event handled.
int custom_loop_count = 0;
int frame_count = 0;
int event_count = 0;
int hp = 100;
int row = 0;
auto hit_button = Button("Hit", [&] { hp -= 1; }, Style());
auto hard_button = Button("Hard", [&] { hp -= 10; }, Style());
auto heal_button = Button("Heal", [&] { hp += 10; }, Style());
auto buttons = Container::Horizontal({
hit_button, hard_button, heal_button}, &row);
auto component = Renderer(buttons, [&] {
frame_count++;
return vbox({
paragraph("I'm baby mustache hammock squid, stumptown echo park lumbersexual PBR&B glossier iceland pabst irony mlkshk skateboard migas kombucha. Lyft meggings organic tacos. IPhone microdosing bodega boys, fit locavore jawn cloud bread neutral milk hotel trust fund live-edge selfies portland lyft vice. Pug swag af slow-carb."),
separator(),
paragraph(fmt::format("HP {} frames {} events {} custom {}",
hp, frame_count, event_count, custom_loop_count)),
separator(),
hbox({
text("HP"),
gauge(hp / 100.0f),
}),
separator(),
buttons->Render(),
}) |
border;
});
component |= CatchEvent([&](Event) -> bool {
event_count++;
return false;
});
Loop loop(&screen, component);
while (!loop.HasQuitted()) {
custom_loop_count++;
loop.RunOnce();
screen.Post(Event::Custom);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
return EXIT_SUCCESS;
}

Loading…
Cancel
Save