Skip to content
This repository was archived by the owner on Jan 18, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/Listeners/ButtonMouseListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/Utils/RectangleUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
using namespace engine;
using namespace spic;

std::vector<Point> RectangleUtil::Rotate(const Point& center, double width, double height, double angle) {
std::vector<Point> RectangleUtil::Rotate(const Point& center, double width, double height, double angle)
{
std::vector<Point> 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;
}
18 changes: 12 additions & 6 deletions src/Utils/ZoneUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
using namespace engine;
using namespace spic;

bool ZoneUtil::InZone(const Point& point, const std::vector<Point>& points) {
bool ZoneUtil::InZone(const Point& point, const std::vector<Point>& points)
{
if (points.empty()) return false;

auto center = GetCenter(points);
Expand All @@ -16,7 +17,8 @@ bool ZoneUtil::InZone(const Point& point, const std::vector<Point>& points) {
return false;
}

Point ZoneUtil::GetCenter(const std::vector<Point>& points) {
Point ZoneUtil::GetCenter(const std::vector<Point>& points)
{
Point avgPoint{0, 0};
if (points.empty()) return avgPoint;

Expand All @@ -33,7 +35,8 @@ Point ZoneUtil::GetCenter(const std::vector<Point>& points) {
return avgPoint;
}

double ZoneUtil::GetMaxLength(const std::vector<Point>& points, const Point& center) {
double ZoneUtil::GetMaxLength(const std::vector<Point>& points, const Point& center)
{
double maxLength = 0;
if (points.empty()) return maxLength;

Expand All @@ -46,7 +49,8 @@ double ZoneUtil::GetMaxLength(const std::vector<Point>& points, const Point& cen
return maxLength;
}

int ZoneUtil::WindPnPoly(const Point& point, const std::vector<Point>& points) {
int ZoneUtil::WindPnPoly(const Point& point, const std::vector<Point>& points)
{
auto result = 0;
int pointsCount = points.size();
for (int i = 0; i < pointsCount; ++i)
Expand All @@ -67,10 +71,12 @@ int ZoneUtil::WindPnPoly(const Point& point, const std::vector<Point>& 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);
}