We can select in 2D

This commit is contained in:
Clement Roblot 2024-08-02 20:38:17 +07:00
parent 73cd1d0190
commit 9dfbdd168b
3 changed files with 31 additions and 28 deletions

View File

@ -21,11 +21,7 @@ int main() {
std::string last_name; std::string last_name;
std::string password; std::string password;
std::string phoneNumber; std::string phoneNumber;
Region selection;
int select_startx = -1;
int select_starty = -1;
int select_endx = -1;
int select_endy = -1;
std::string textToCopy; std::string textToCopy;
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
@ -68,11 +64,11 @@ int main() {
text("Hello " + first_name + " " + last_name), text("Hello " + first_name + " " + last_name),
text("Your password is " + password), text("Your password is " + password),
text("Your phone number is " + phoneNumber), text("Your phone number is " + phoneNumber),
text("select_start " + std::to_string(select_startx) + ";" + std::to_string(select_starty)), text("select_start " + std::to_string(selection.startx) + ";" + std::to_string(selection.starty)),
text("select_end " + std::to_string(select_endx) + ";" + std::to_string(select_endy)), text("select_end " + std::to_string(selection.endx) + ";" + std::to_string(selection.endy)),
text("textToCopy " + textToCopy) text("textToCopy " + textToCopy)
}) | }) |
border | selected(select_startx, select_endx); border | selected(selection);
}); });
@ -81,17 +77,17 @@ int main() {
auto& mouse = event.mouse(); auto& mouse = event.mouse();
if (mouse.button == Mouse::Left) { if (mouse.button == Mouse::Left) {
if (mouse.motion == Mouse::Pressed) { if (mouse.motion == Mouse::Pressed) {
select_startx = mouse.x; selection.startx = mouse.x;
select_starty = mouse.y; selection.starty = mouse.y;
select_endx = mouse.x; selection.endx = mouse.x;
select_endy = mouse.y; selection.endy = mouse.y;
} else if (mouse.motion == Mouse::Released) { } else if (mouse.motion == Mouse::Released) {
select_endx = mouse.x; selection.endx = mouse.x;
select_endy = mouse.y; selection.endy = mouse.y;
} }
else if (mouse.motion == Mouse::Moved) { else if (mouse.motion == Mouse::Moved) {
select_endx = mouse.x; selection.endx = mouse.x;
select_endy = mouse.y; selection.endy = mouse.y;
} }
screen.PostEvent(Event::Custom); screen.PostEvent(Event::Custom);

View File

@ -33,6 +33,14 @@ enum BorderStyle {
EMPTY, EMPTY,
}; };
typedef struct {
int startx;
int endx;
int starty;
int endy;
} Region;
// Pipe elements into decorator togethers. // Pipe elements into decorator togethers.
// For instance the next lines are equivalents: // For instance the next lines are equivalents:
// -> text("ftxui") | bold | underlined // -> text("ftxui") | bold | underlined
@ -96,8 +104,8 @@ Element canvas(std::function<void(Canvas&)>);
Element bold(Element); Element bold(Element);
Element dim(Element); Element dim(Element);
Element inverted(Element); Element inverted(Element);
Element selected(int &start, int &end, Element); Element selected(Region &selection, Element);
Decorator selected(int &start, int &end); Decorator selected(Region &selection);
Element underlined(Element); Element underlined(Element);
Element underlinedDouble(Element); Element underlinedDouble(Element);
Element blink(Element); Element blink(Element);

View File

@ -16,21 +16,20 @@ namespace {
class Selected : public NodeDecorator { class Selected : public NodeDecorator {
public: public:
using NodeDecorator::NodeDecorator; using NodeDecorator::NodeDecorator;
Selected(Element child, int &start, int &end) Selected(Element child, Region &selection)
: NodeDecorator(std::move(child)), startx_(start), endx_(end) {} : NodeDecorator(std::move(child)), selection_(selection) {}
void Render(Screen& screen) override { void Render(Screen& screen) override {
Node::Render(screen); Node::Render(screen);
for (int y = box_.y_min; y <= box_.y_max; ++y) { for (int y = selection_.starty; y <= selection_.endy; ++y) {
for (int x = startx_; x <= endx_; ++x) { for (int x = selection_.startx; x <= selection_.endx; ++x) {
screen.PixelAt(x, y).inverted ^= true; screen.PixelAt(x, y).inverted ^= true;
} }
} }
} }
private: private:
int &startx_; Region &selection_;
int &endx_;
}; };
} // namespace } // namespace
@ -38,12 +37,12 @@ private:
/// colors. /// colors.
/// @ingroup dom /// @ingroup dom
Element selected(int &start, int &end, Element child) { Element selected(Region &selection, Element child) {
return std::make_shared<Selected>(std::move(child), start, end); return std::make_shared<Selected>(std::move(child), selection);
} }
Decorator selected(int &start, int &end) { Decorator selected(Region &selection) {
return [&start, &end](Element child) { return selected(start, end, std::move(child)); }; return [&selection](Element child) { return selected(selection, std::move(child)); };
} }
} // namespace ftxui } // namespace ftxui