diff options
Diffstat (limited to 'src/ui/ui_base.hpp')
-rw-r--r-- | src/ui/ui_base.hpp | 116 |
1 files changed, 109 insertions, 7 deletions
diff --git a/src/ui/ui_base.hpp b/src/ui/ui_base.hpp index e57af530..c26bfe0e 100644 --- a/src/ui/ui_base.hpp +++ b/src/ui/ui_base.hpp @@ -8,7 +8,7 @@ namespace cru::ui { - struct Point final + struct Point { constexpr static Point Zero() { @@ -33,7 +33,7 @@ namespace cru::ui } - struct Size final + struct Size { constexpr static Size Zero() { @@ -47,12 +47,12 @@ namespace cru::ui float height = 0; }; - constexpr Size operator+(const Size& left, const Size& right) + constexpr Size operator + (const Size& left, const Size& right) { return Size(left.width + right.width, left.height + right.height); } - constexpr Size operator-(const Size& left, const Size& right) + constexpr Size operator - (const Size& left, const Size& right) { return Size(left.width - right.width, left.height - right.height); } @@ -68,7 +68,7 @@ namespace cru::ui } - struct Thickness final + struct Thickness { constexpr static Thickness Zero() { @@ -122,7 +122,21 @@ namespace cru::ui float bottom; }; - struct Rect final + constexpr bool operator == (const Thickness& left, const Thickness& right) + { + return left.left == right.left && + left.top == right.top && + left.right == right.right && + left.bottom == right.bottom; + } + + constexpr bool operator != (const Thickness& left, const Thickness& right) + { + return !(left == right); + } + + + struct Rect { constexpr Rect() = default; constexpr Rect(const float left, const float top, const float width, const float height) @@ -135,6 +149,11 @@ namespace cru::ui return Rect(left, top, right - left, bottom - top); } + constexpr static Rect FromCenter(const Point& center, const float width, const float height) + { + return Rect(center.x - width / 2.0f, center.y - height / 2.0f, width, height); + } + constexpr float GetRight() const { return left + width; @@ -165,6 +184,11 @@ namespace cru::ui return Point(left + width, top); } + constexpr Point GetCenter() const + { + return Point(left + width / 2.0f, top + height / 2.0f); + } + constexpr Size GetSize() const { return Size(width, height); @@ -190,8 +214,82 @@ namespace cru::ui float height = 0.0f; }; + constexpr bool operator==(const Rect& left, const Rect& right) + { + return left.left == right.left && + left.top == right.top && + left.width == right.width && + left.height == right.height; + } + + constexpr bool operator!=(const Rect& left, const Rect& right) + { + return !(left == right); + } + + + struct RoundedRect + { + constexpr RoundedRect() = default; + constexpr RoundedRect(const Rect& rect, const float radius_x, const float radius_y) + : rect(rect), radius_x(radius_x), radius_y(radius_y) { } + + Rect rect{}; + float radius_x = 0.0f; + float radius_y = 0.0f; + }; + + constexpr bool operator == (const RoundedRect& left, const RoundedRect& right) + { + return left.rect == right.rect && left.radius_x == right.radius_x && left.radius_y == right.radius_y; + } - struct TextRange final + constexpr bool operator != (const RoundedRect& left, const RoundedRect& right) + { + return !(left == right); + } + + struct Ellipse + { + constexpr Ellipse() = default; + constexpr Ellipse(const Point& center, const float radius_x, const float radius_y) + : center(center), radius_x(radius_x), radius_y(radius_y) { } + + constexpr static Ellipse FromRect(const Rect& rect) + { + return Ellipse(rect.GetCenter(), rect.width / 2.0f, rect.height / 2.0f); + } + + constexpr Rect GetBoundRect() const + { + return Rect::FromCenter(center, radius_x * 2.0f, radius_y * 2.0f); + } + + Point center{}; + float radius_x = 0.0f; + float radius_y = 0.0f; + }; + + constexpr bool operator == (const Ellipse& left, const Ellipse& right) + { + return left.center == right.center && left.radius_x == right.radius_x && left.radius_y == right.radius_y; + } + + constexpr bool operator != (const Ellipse& left, const Ellipse& right) + { + return !(left == right); + } + + + enum class MouseButton + { + Left, + Right, + Middle + }; + + + struct TextRange { constexpr static std::optional<TextRange> FromTwoSides(unsigned first, unsigned second) { @@ -219,4 +317,8 @@ namespace cru::ui unsigned position = 0; unsigned count = 0; }; + + bool IsKeyDown(int virtual_code); + bool IsKeyToggled(int virtual_code); + bool IsAnyMouseButtonDown(); } |