mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-28 22:26:28 +08:00
We can select in 2D
This commit is contained in:
parent
73cd1d0190
commit
9dfbdd168b
@ -21,11 +21,7 @@ int main() {
|
||||
std::string last_name;
|
||||
std::string password;
|
||||
std::string phoneNumber;
|
||||
|
||||
int select_startx = -1;
|
||||
int select_starty = -1;
|
||||
int select_endx = -1;
|
||||
int select_endy = -1;
|
||||
Region selection;
|
||||
std::string textToCopy;
|
||||
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
@ -68,11 +64,11 @@ int main() {
|
||||
text("Hello " + first_name + " " + last_name),
|
||||
text("Your password is " + password),
|
||||
text("Your phone number is " + phoneNumber),
|
||||
text("select_start " + std::to_string(select_startx) + ";" + std::to_string(select_starty)),
|
||||
text("select_end " + std::to_string(select_endx) + ";" + std::to_string(select_endy)),
|
||||
text("select_start " + std::to_string(selection.startx) + ";" + std::to_string(selection.starty)),
|
||||
text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)),
|
||||
text("textToCopy " + textToCopy)
|
||||
}) |
|
||||
border | selected(select_startx, select_endx);
|
||||
border | selected(selection);
|
||||
});
|
||||
|
||||
|
||||
@ -81,17 +77,17 @@ int main() {
|
||||
auto& mouse = event.mouse();
|
||||
if (mouse.button == Mouse::Left) {
|
||||
if (mouse.motion == Mouse::Pressed) {
|
||||
select_startx = mouse.x;
|
||||
select_starty = mouse.y;
|
||||
select_endx = mouse.x;
|
||||
select_endy = mouse.y;
|
||||
selection.startx = mouse.x;
|
||||
selection.starty = mouse.y;
|
||||
selection.endx = mouse.x;
|
||||
selection.endy = mouse.y;
|
||||
} else if (mouse.motion == Mouse::Released) {
|
||||
select_endx = mouse.x;
|
||||
select_endy = mouse.y;
|
||||
selection.endx = mouse.x;
|
||||
selection.endy = mouse.y;
|
||||
}
|
||||
else if (mouse.motion == Mouse::Moved) {
|
||||
select_endx = mouse.x;
|
||||
select_endy = mouse.y;
|
||||
selection.endx = mouse.x;
|
||||
selection.endy = mouse.y;
|
||||
}
|
||||
|
||||
screen.PostEvent(Event::Custom);
|
||||
|
@ -33,6 +33,14 @@ enum BorderStyle {
|
||||
EMPTY,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
|
||||
int startx;
|
||||
int endx;
|
||||
int starty;
|
||||
int endy;
|
||||
} Region;
|
||||
|
||||
// Pipe elements into decorator togethers.
|
||||
// For instance the next lines are equivalents:
|
||||
// -> text("ftxui") | bold | underlined
|
||||
@ -96,8 +104,8 @@ Element canvas(std::function<void(Canvas&)>);
|
||||
Element bold(Element);
|
||||
Element dim(Element);
|
||||
Element inverted(Element);
|
||||
Element selected(int &start, int &end, Element);
|
||||
Decorator selected(int &start, int &end);
|
||||
Element selected(Region &selection, Element);
|
||||
Decorator selected(Region &selection);
|
||||
Element underlined(Element);
|
||||
Element underlinedDouble(Element);
|
||||
Element blink(Element);
|
||||
|
@ -16,21 +16,20 @@ namespace {
|
||||
class Selected : public NodeDecorator {
|
||||
public:
|
||||
using NodeDecorator::NodeDecorator;
|
||||
Selected(Element child, int &start, int &end)
|
||||
: NodeDecorator(std::move(child)), startx_(start), endx_(end) {}
|
||||
Selected(Element child, Region &selection)
|
||||
: NodeDecorator(std::move(child)), selection_(selection) {}
|
||||
|
||||
void Render(Screen& screen) override {
|
||||
Node::Render(screen);
|
||||
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
||||
for (int x = startx_; x <= endx_; ++x) {
|
||||
for (int y = selection_.starty; y <= selection_.endy; ++y) {
|
||||
for (int x = selection_.startx; x <= selection_.endx; ++x) {
|
||||
screen.PixelAt(x, y).inverted ^= true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
int &startx_;
|
||||
int &endx_;
|
||||
Region &selection_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
@ -38,12 +37,12 @@ private:
|
||||
/// colors.
|
||||
/// @ingroup dom
|
||||
|
||||
Element selected(int &start, int &end, Element child) {
|
||||
return std::make_shared<Selected>(std::move(child), start, end);
|
||||
Element selected(Region &selection, Element child) {
|
||||
return std::make_shared<Selected>(std::move(child), selection);
|
||||
}
|
||||
|
||||
Decorator selected(int &start, int &end) {
|
||||
return [&start, &end](Element child) { return selected(start, end, std::move(child)); };
|
||||
Decorator selected(Region &selection) {
|
||||
return [&selection](Element child) { return selected(selection, std::move(child)); };
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
|
Loading…
Reference in New Issue
Block a user