mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-28 17:58:07 +08:00
Add gauge and frame.
This commit is contained in:
57
ftxui/src/ftxui/core/dom/frame.cpp
Normal file
57
ftxui/src/ftxui/core/dom/frame.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
static wchar_t charset[] = L"┌┐└┘─│";
|
||||
|
||||
class Frame : public Node {
|
||||
public:
|
||||
Frame(Child child) : Node(unpack(std::move(child))) {}
|
||||
~Frame() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
children[0]->ComputeRequirement();
|
||||
requirement_ = children[0]->requirement();
|
||||
requirement_.min.x += 2;
|
||||
requirement_.min.y += 2;
|
||||
}
|
||||
|
||||
void SetBox(Box box) override {
|
||||
Node::SetBox(box);
|
||||
box.left++;
|
||||
box.right--;
|
||||
box.top++;
|
||||
box.bottom--;
|
||||
children[0]->SetBox(box);
|
||||
}
|
||||
|
||||
void Render(Screen& screen) override {
|
||||
if (box_.left >= box_.right || box_.top >= box_.bottom)
|
||||
return;
|
||||
|
||||
screen.at(box_.left, box_.top) = charset[0];
|
||||
screen.at(box_.right, box_.top) = charset[1];
|
||||
screen.at(box_.left, box_.bottom) = charset[2];
|
||||
screen.at(box_.right, box_.bottom) = charset[3];
|
||||
for(float x = box_.left + 1; x<box_.right; ++x) {
|
||||
screen.at(x, box_.top) = charset[4];
|
||||
screen.at(x, box_.bottom) = charset[4];
|
||||
}
|
||||
for(float y = box_.top + 1; y<box_.bottom; ++y) {
|
||||
screen.at(box_.left, y) = charset[5];
|
||||
screen.at(box_.right,y) = charset[5];
|
||||
}
|
||||
children[0]->Render(screen);
|
||||
}
|
||||
private:
|
||||
float progress_;
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> frame(Child child) {
|
||||
return std::make_unique<Frame>(std::move(child));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
@@ -4,10 +4,12 @@
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
static wchar_t charset[] = L" ▏▎▍▌▋▊▉█";
|
||||
|
||||
class Gauge : public Node {
|
||||
public:
|
||||
Gauge(float progress) : progress_(progress) {}
|
||||
~Gauge() {}
|
||||
~Gauge() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
requirement_.flex.x = 1;
|
||||
@@ -16,9 +18,14 @@ class Gauge : public Node {
|
||||
|
||||
void Render(Screen& screen) override {
|
||||
float y = box_.top;
|
||||
int limit = box_.left + progress_ * (box_.right - box_.left);
|
||||
for(int i = box_.left; i<=limit; ++i)
|
||||
screen.at(i, y) = 'X';
|
||||
float limit = box_.left + progress_ * (box_.right - box_.left + 1);
|
||||
int limit_int = limit;
|
||||
int x = box_.left;
|
||||
while (x < limit_int)
|
||||
screen.at(x++, y) = charset[9];
|
||||
screen.at(x++, y) = charset[int(9*(limit-limit_int))];
|
||||
while (x <= box_.right)
|
||||
screen.at(x++, y) = charset[0];
|
||||
}
|
||||
private:
|
||||
float progress_;
|
||||
|
@@ -37,7 +37,7 @@ class VBox : public Node {
|
||||
|
||||
int y = box.top;
|
||||
for (auto& child : children) {
|
||||
if (y > box.right)
|
||||
if (y > box.bottom)
|
||||
break;
|
||||
|
||||
Box child_box = box;
|
||||
|
Reference in New Issue
Block a user