diff --git a/examples/component/input.cpp b/examples/component/input.cpp index 67ef0bc..feafbba 100644 --- a/examples/component/input.cpp +++ b/examples/component/input.cpp @@ -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); diff --git a/include/ftxui/dom/elements.hpp b/include/ftxui/dom/elements.hpp index 1b74f0c..71eb376 100644 --- a/include/ftxui/dom/elements.hpp +++ b/include/ftxui/dom/elements.hpp @@ -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); 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); diff --git a/src/ftxui/dom/selected.cpp b/src/ftxui/dom/selected.cpp index f97b9bb..ebb9552 100644 --- a/src/ftxui/dom/selected.cpp +++ b/src/ftxui/dom/selected.cpp @@ -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(std::move(child), start, end); +Element selected(Region &selection, Element child) { + return std::make_shared(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