From 718ff6db33dcd6528287842e22416b1b5937093a Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 25 Nov 2021 11:20:21 +0100 Subject: [PATCH 1/3] Optimize RectangleUtil --- src/Utils/RectangleUtil.cpp | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Utils/RectangleUtil.cpp b/src/Utils/RectangleUtil.cpp index 1cdbf2b..04121f6 100644 --- a/src/Utils/RectangleUtil.cpp +++ b/src/Utils/RectangleUtil.cpp @@ -5,13 +5,25 @@ using namespace engine; using namespace spic; -std::vector RectangleUtil::Rotate(const Point& center, double width, double height, double angle) { +std::vector RectangleUtil::Rotate(const Point& center, double width, double height, double angle) +{ std::vector points{}; - points.push_back({center.x - ((width / 2) * cos(angle)) - ((height / 2) * sin(angle)), center.y - ((width / 2) * sin(angle)) + ((height / 2) * cos(angle))}); - points.push_back({center.x + ((width / 2) * cos(angle)) - ((height / 2) * sin(angle)), center.y + ((width / 2) * sin(angle)) + ((height / 2) * cos(angle))}); - points.push_back({center.x + ((width / 2) * cos(angle)) + ((height / 2) * sin(angle)), center.y + ((width / 2) * sin(angle)) - ((height / 2) * cos(angle))}); - points.push_back({center.x - ((width / 2) * cos(angle)) + ((height / 2) * sin(angle)), center.y - ((width / 2) * sin(angle)) - ((height / 2) * cos(angle))}); + auto wDiff = width * 0.5; + auto hDiff = height * 0.5; + + auto cAngle = cos(angle); + auto wCos = wDiff * cAngle; + auto hCos = hDiff * cAngle; + + auto sAngle = sin(angle); + auto wSin = wDiff * sAngle; + auto hSin = hDiff * sAngle; + + points.push_back({center.x - wCos - hSin, center.y - wSin + hCos}); + points.push_back({center.x + wCos - hSin, center.y + wSin + hCos}); + points.push_back({center.x + wCos + hSin, center.y + wSin - hCos}); + points.push_back({center.x - wCos + hSin, center.y - wSin - hCos}); return points; } From b847b343b1bf2c25c7833af7a4beb52cbe5b4df0 Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 25 Nov 2021 11:20:29 +0100 Subject: [PATCH 2/3] Format ZoneUtil --- src/Utils/ZoneUtil.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Utils/ZoneUtil.cpp b/src/Utils/ZoneUtil.cpp index 0f0ad29..5c03bdf 100644 --- a/src/Utils/ZoneUtil.cpp +++ b/src/Utils/ZoneUtil.cpp @@ -5,7 +5,8 @@ using namespace engine; using namespace spic; -bool ZoneUtil::InZone(const Point& point, const std::vector& points) { +bool ZoneUtil::InZone(const Point& point, const std::vector& points) +{ if (points.empty()) return false; auto center = GetCenter(points); @@ -16,7 +17,8 @@ bool ZoneUtil::InZone(const Point& point, const std::vector& points) { return false; } -Point ZoneUtil::GetCenter(const std::vector& points) { +Point ZoneUtil::GetCenter(const std::vector& points) +{ Point avgPoint{0, 0}; if (points.empty()) return avgPoint; @@ -33,7 +35,8 @@ Point ZoneUtil::GetCenter(const std::vector& points) { return avgPoint; } -double ZoneUtil::GetMaxLength(const std::vector& points, const Point& center) { +double ZoneUtil::GetMaxLength(const std::vector& points, const Point& center) +{ double maxLength = 0; if (points.empty()) return maxLength; @@ -46,7 +49,8 @@ double ZoneUtil::GetMaxLength(const std::vector& points, const Point& cen return maxLength; } -int ZoneUtil::WindPnPoly(const Point& point, const std::vector& points) { +int ZoneUtil::WindPnPoly(const Point& point, const std::vector& points) +{ auto result = 0; int pointsCount = points.size(); for (int i = 0; i < pointsCount; ++i) @@ -67,10 +71,12 @@ int ZoneUtil::WindPnPoly(const Point& point, const std::vector& points) { return result; } -double ZoneUtil::GetDistance(const Point& point1, const Point& point2) { +double ZoneUtil::GetDistance(const Point& point1, const Point& point2) +{ return sqrt(pow(point2.x - point1.x, 2) + pow(point2.y - point1.y, 2)); } -double ZoneUtil::IsLeft(const Point& point1, const Point& point2, const Point& point) { +double ZoneUtil::IsLeft(const Point& point1, const Point& point2, const Point& point) +{ return (point1.x - point.x) * (point2.y - point.y) - (point2.x - point.x) * (point1.y - point.y); } From 06df0521d25beaeb71f2e194e4211c00593b94ff Mon Sep 17 00:00:00 2001 From: Sander Jochems Date: Thu, 25 Nov 2021 11:20:39 +0100 Subject: [PATCH 3/3] Update ButtonMouseListener.cpp --- src/Listeners/ButtonMouseListener.cpp | 33 ++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Listeners/ButtonMouseListener.cpp b/src/Listeners/ButtonMouseListener.cpp index afda755..6076980 100644 --- a/src/Listeners/ButtonMouseListener.cpp +++ b/src/Listeners/ButtonMouseListener.cpp @@ -10,10 +10,12 @@ using namespace engine; using namespace spic; -ButtonMouseListener::ButtonMouseListener(Button* button) : _button{button} { +ButtonMouseListener::ButtonMouseListener(Button* button) : _button{button} +{ } -void ButtonMouseListener::OnMouseReleased() { +void ButtonMouseListener::OnMouseReleased() +{ if (Input::GetMouseButtonUp(Input::MouseButton::LEFT)) { if (_button != nullptr) @@ -23,12 +25,31 @@ void ButtonMouseListener::OnMouseReleased() { auto transform = _button->AbsoluteTransform(); auto width = _button->Width() * transform.scale; auto height = _button->Height() * transform.scale; + auto mousePos = Input::MousePosition(); - auto points = RectangleUtil::Rotate(transform.position, width, height, std::fmod(transform.rotation, 360)); - - if (ZoneUtil::InZone(Input::MousePosition(), points)) + if (transform.rotation == 0) + { + auto hDiff = height * 0.5; + auto wDiff = width * 0.5; + + auto top = transform.position.y - hDiff; + auto right = transform.position.x + wDiff; + auto bottom = transform.position.y + hDiff; + auto left = transform.position.x - wDiff; + + if (mousePos.y >= top && mousePos.y <= bottom && mousePos.x >= left && mousePos.x <= right) + { + _button->Click(); + } + } + else { - _button->Click(); + auto points = RectangleUtil::Rotate(transform.position, width, height, std::fmod(transform.rotation, 360)); + + if (ZoneUtil::InZone(mousePos, points)) + { + _button->Click(); + } } } }