From c1c6afc0baa5dd957b023f4a1784ea709405af8f Mon Sep 17 00:00:00 2001 From: Clement Roblot Date: Fri, 2 Aug 2024 22:28:56 +0700 Subject: [PATCH] We can select in every directions --- examples/component/input.cpp | 7 ++++--- src/ftxui/dom/selected.cpp | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/component/input.cpp b/examples/component/input.cpp index 1a0bcaa..61b3c0c 100644 --- a/examples/component/input.cpp +++ b/examples/component/input.cpp @@ -72,9 +72,10 @@ int main() { }); // TODO: Make the textToCopy a callback called every times the selected text change + // TODO: Is there a way for me to embedd the catchEvent in the selected decorator? // TODO: Implement the double click on word to select the word // TODO: Implement the double click and drag to select word by word (optional) - // TODO: Is there a way for me to embedd the catchEvent in the selected decorator? + // TODO: Add a "selectable" flag in the pixel class and take it into account when selecting things renderer |= CatchEvent([&](Event event) { if (event.is_mouse()) { @@ -85,7 +86,7 @@ int main() { { selection.startx = mouse.x; selection.starty = mouse.y; - selection.endx = mouse.x-1; + selection.endx = mouse.x; selection.endy = mouse.y; } else if (mouse.motion == Mouse::Released) @@ -95,7 +96,7 @@ int main() { } else if (mouse.motion == Mouse::Moved) { - selection.endx = mouse.x-1; + selection.endx = mouse.x; selection.endy = mouse.y; } diff --git a/src/ftxui/dom/selected.cpp b/src/ftxui/dom/selected.cpp index dec2253..9adfe44 100644 --- a/src/ftxui/dom/selected.cpp +++ b/src/ftxui/dom/selected.cpp @@ -22,8 +22,8 @@ class Selected : public NodeDecorator { void Render(Screen& screen) override { Node::Render(screen); destination_ = ""; - for (int y = selection_.starty; y <= selection_.endy; ++y) { - for (int x = selection_.startx; x <= selection_.endx; ++x) { + for (int y = std::min(selection_.starty, selection_.endy); y <= std::max(selection_.starty, selection_.endy); ++y) { + for (int x = std::min(selection_.startx, selection_.endx); x <= std::max(selection_.startx, selection_.endx)-1; ++x) { screen.PixelAt(x, y).inverted ^= true; destination_ += screen.PixelAt(x, y).character; }