diff options
-rw-r--r-- | engine/CMakeLists.txt | 1 | ||||
-rw-r--r-- | engine/include/kurator/engine/Point.h | 1 | ||||
-rw-r--r-- | engine/include/kurator/engine/Rect.h | 27 | ||||
-rw-r--r-- | engine/src/Point.cpp | 7 | ||||
-rw-r--r-- | engine/src/Rect.cpp | 74 | ||||
-rw-r--r-- | kurator/src/Controller.cpp | 11 | ||||
-rw-r--r-- | kurator/src/Mouse.cpp | 7 | ||||
-rw-r--r-- | kurator/src/Mouse.h | 3 |
8 files changed, 118 insertions, 13 deletions
diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 7cb7f20..266e109 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -4,6 +4,7 @@ add_library( src/Camera.cpp src/Clock.cpp src/Point.cpp + src/Rect.cpp ) target_include_directories( ${PROJECT_NAME} diff --git a/engine/include/kurator/engine/Point.h b/engine/include/kurator/engine/Point.h index 4b352d6..eecf2c6 100644 --- a/engine/include/kurator/engine/Point.h +++ b/engine/include/kurator/engine/Point.h @@ -23,6 +23,7 @@ struct Point Point subtract(double _x, double _y) const; bool operator==(const Point& other) const; bool operator!=(const Point& other) const; + friend Point abs(const Point& point); }; diff --git a/engine/include/kurator/engine/Rect.h b/engine/include/kurator/engine/Rect.h new file mode 100644 index 0000000..d485937 --- /dev/null +++ b/engine/include/kurator/engine/Rect.h @@ -0,0 +1,27 @@ +#pragma once + +#include "Point.h" + + +namespace kurator +{ +namespace engine +{ + + +struct Rect +{ + Rect(); + Rect(const Point& first, const Point& second); + Point position; + Point size; + Point topleft() const; + Point bottomright() const; + Point center() const; + bool contains(const Point& point); + bool contains(const Point& circle, double radius); +}; + + +} // namespace engine +} // namespace kurator diff --git a/engine/src/Point.cpp b/engine/src/Point.cpp index e01de57..8dd50bd 100644 --- a/engine/src/Point.cpp +++ b/engine/src/Point.cpp @@ -96,5 +96,12 @@ Point::operator!=(const Point& other) const } +Point +abs(const Point& point) +{ + return {std::abs(point.x), std::abs(point.y)}; +} + + } // namespace engine } // namespace kurator diff --git a/engine/src/Rect.cpp b/engine/src/Rect.cpp new file mode 100644 index 0000000..293e099 --- /dev/null +++ b/engine/src/Rect.cpp @@ -0,0 +1,74 @@ +#include <kurator/engine/Rect.h> + +#include <algorithm> +#include <cmath> + +#include <raylib.h> + +#include <kurator/engine/Point.h> + + +using std::abs; + + +namespace kurator +{ +namespace engine +{ + + +Rect::Rect() : + position {0, 0}, + size {0, 0} +{ +} + + +Rect::Rect(const Point& first, const Point& second) : + position {std::min(first.x, second.x), std::min(first.y, second.y)}, + size {abs(second - first)} +{ +} + + +Point Rect::topleft() const +{ + return position; +} + + +Point Rect::bottomright() const +{ + return position + size; +} + + +Point Rect::center() const +{ + return position + size.scale(0.5); +} + + +bool Rect::contains(const Point& point) +{ + const auto br = bottomright(); + return point.x >= position.x && point.x <= br.x && point.y >= position.y && point.y <= br.y; +} + + +bool Rect::contains(const Point& circle, double radius) +{ + return CheckCollisionCircleRec( + {static_cast<float>(circle.x), static_cast<float>(circle.y)}, + radius, + { + static_cast<float>(position.x), + static_cast<float>(position.y), + static_cast<float>(size.x), + static_cast<float>(size.y) + }); +} + + +} // namespace engine +} // namespace kurator diff --git a/kurator/src/Controller.cpp b/kurator/src/Controller.cpp index d7efcdb..3512f3e 100644 --- a/kurator/src/Controller.cpp +++ b/kurator/src/Controller.cpp @@ -26,15 +26,8 @@ void Controller::update(sim::State& ctx) void Controller::draw(const sim::State&) const { if (mouse.is_dragging()) { - const auto pos = mouse.position(); - const auto start = mouse.start(); - const auto diff = start - pos; - DrawRectangleLines( - std::min(pos.x, start.x), - std::min(pos.y, start.y), - std::abs(diff.x), - std::abs(diff.y), - GRAY); + const auto rect = mouse.selection(); + DrawRectangleLines(rect.position.x, rect.position.y, rect.size.x, rect.size.y, GRAY); } } diff --git a/kurator/src/Mouse.cpp b/kurator/src/Mouse.cpp index e66d869..aeaa8e5 100644 --- a/kurator/src/Mouse.cpp +++ b/kurator/src/Mouse.cpp @@ -3,6 +3,7 @@ #include <raylib.h> #include <kurator/engine/Point.h> +#include <kurator/engine/Rect.h> namespace kurator @@ -30,10 +31,10 @@ Mouse::position() const } -engine::Point -Mouse::start() const +engine::Rect +Mouse::selection() const { - return start_; + return {start_, position()}; } diff --git a/kurator/src/Mouse.h b/kurator/src/Mouse.h index c5c1f0e..81afe2c 100644 --- a/kurator/src/Mouse.h +++ b/kurator/src/Mouse.h @@ -1,6 +1,7 @@ #pragma once #include <kurator/engine/Point.h> +#include <kurator/engine/Rect.h> namespace kurator @@ -13,7 +14,7 @@ public: Mouse(); void update(); engine::Point position() const; - engine::Point start() const; + engine::Rect selection() const; bool is_dragging() const; private: engine::Point start_; |