Reverse selection are now possible

This commit is contained in:
Clement Roblot 2024-10-31 21:49:31 +07:00 committed by ArthurSonzogni
parent 92586f76bc
commit 437439c945
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
4 changed files with 36 additions and 10 deletions

View File

@ -15,6 +15,7 @@ struct Box {
static auto Intersection(Box a, Box b) -> Box; static auto Intersection(Box a, Box b) -> Box;
static auto Union(Box a, Box b) -> Box; static auto Union(Box a, Box b) -> Box;
bool Contain(int x, int y) const; bool Contain(int x, int y) const;
Box Clean() const;
bool IsEmpty() const; bool IsEmpty() const;
bool operator==(const Box& other) const; bool operator==(const Box& other) const;
bool operator!=(const Box& other) const; bool operator!=(const Box& other) const;

View File

@ -65,6 +65,7 @@ class Screen : public Image {
bool selection_enabled = false; bool selection_enabled = false;
CapturedMouse selection_pending; CapturedMouse selection_pending;
Box mouse_selection_region;
Box selection_region; Box selection_region;
std::string selection_text; std::string selection_text;

View File

@ -843,10 +843,12 @@ bool ScreenInteractive::HandleSelection(Event event) {
return false; return false;
} }
selection_enabled = true; selection_enabled = true;
selection_region.x_min = mouse.x; mouse_selection_region.x_min = mouse.x;
selection_region.y_min = mouse.y; mouse_selection_region.y_min = mouse.y;
selection_region.x_max = mouse.x; mouse_selection_region.x_max = mouse.x;
selection_region.y_max = mouse.y; mouse_selection_region.y_max = mouse.y;
selection_region = mouse_selection_region.Clean();
return true; return true;
} }
@ -855,18 +857,22 @@ bool ScreenInteractive::HandleSelection(Event event) {
} }
if (mouse.motion == Mouse::Moved) { if (mouse.motion == Mouse::Moved) {
selection_region.x_max = mouse.x; mouse_selection_region.x_max = mouse.x;
selection_region.y_max = mouse.y; mouse_selection_region.y_max = mouse.y;
selection_region = mouse_selection_region.Clean();
return true; return true;
} }
if (mouse.motion == Mouse::Released) { if (mouse.motion == Mouse::Released) {
selection_region.x_max = mouse.x; mouse_selection_region.x_max = mouse.x;
selection_region.y_max = mouse.y; mouse_selection_region.y_max = mouse.y;
selection_pending = nullptr; selection_pending = nullptr;
if (selection_region.x_min == selection_region.x_max && selection_region = mouse_selection_region.Clean();
selection_region.y_min == selection_region.y_max) {
if (mouse_selection_region.x_min == mouse_selection_region.x_max &&
mouse_selection_region.y_min == mouse_selection_region.y_max) {
selection_enabled = false; selection_enabled = false;
return true; return true;
} }

View File

@ -39,6 +39,24 @@ bool Box::Contain(int x, int y) const {
y_max >= y; y_max >= y;
} }
/// @return a copy of box with the x_min <= x_max and y_min <= y_max.
/// @ingroup screen
Box Box::Clean() const {
Box newBox = *this;
if(newBox.x_min > newBox.x_max)
{
std::swap(newBox.x_min, newBox.x_max);
}
if(newBox.y_min > newBox.y_max)
{
std::swap(newBox.y_min, newBox.y_max);
}
return newBox;
}
/// @return whether the box is empty. /// @return whether the box is empty.
/// @ingroup screen /// @ingroup screen
bool Box::IsEmpty() const { bool Box::IsEmpty() const {