diff options
Diffstat (limited to 'src/ui/ui_base.h')
-rw-r--r-- | src/ui/ui_base.h | 243 |
1 files changed, 120 insertions, 123 deletions
diff --git a/src/ui/ui_base.h b/src/ui/ui_base.h index bff30bfd..8daa43d7 100644 --- a/src/ui/ui_base.h +++ b/src/ui/ui_base.h @@ -3,158 +3,155 @@ #include <optional> -namespace cru +namespace cru::ui { - namespace ui + struct Point { - struct Point + constexpr static Point Zero() { - constexpr static Point Zero() - { - return Point(0, 0); - } + return Point(0, 0); + } + + constexpr Point() = default; + constexpr Point(const float x, const float y) : x(x), y(y) { } + + float x = 0; + float y = 0; + }; - constexpr Point() = default; - constexpr Point(const float x, const float y) : x(x), y(y) { } + constexpr bool operator==(const Point& left, const Point& right) + { + return left.x == right.x && left.y == right.y; + } - float x = 0; - float y = 0; - }; + constexpr bool operator!=(const Point& left, const Point& right) + { + return !(left == right); + } - constexpr bool operator==(const Point& left, const Point& right) + struct Size + { + constexpr static Size Zero() { - return left.x == right.x && left.y == right.y; + return Size(0, 0); } - constexpr bool operator!=(const Point& left, const Point& right) + constexpr Size() = default; + constexpr Size(const float width, const float height) : width(width), height(height) { } + + float width = 0; + float height = 0; + }; + + 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) + { + return Size(left.width - right.width, left.height - right.height); + } + + constexpr bool operator==(const Size& left, const Size& right) + { + return left.width == right.width && left.height == right.height; + } + + constexpr bool operator!=(const Size& left, const Size& right) + { + return !(left == right); + } + + struct Rect + { + constexpr Rect() = default; + constexpr Rect(const float left, const float top, const float width, const float height) + : left(left), top(top), width(width), height(height) { } + constexpr Rect(const Point& lefttop, const Size& size) + : left(lefttop.x), top(lefttop.y), width(size.width), height(size.height) { } + + constexpr static Rect FromVertices(const float left, const float top, const float right, const float bottom) { - return !(left == right); + return Rect(left, top, right - left, bottom - top); } - struct Size + constexpr float GetRight() const { - constexpr static Size Zero() - { - return Size(0, 0); - } - - constexpr Size() = default; - constexpr Size(const float width, const float height) : width(width), height(height) { } + return left + width; + } - float width = 0; - float height = 0; - }; + constexpr float GetBottom() const + { + return top + height; + } - constexpr Size operator + (const Size& left, const Size& right) + constexpr Point GetLeftTop() const { - return Size(left.width + right.width, left.height + right.height); + return Point(left, top); } - constexpr Size operator - (const Size& left, const Size& right) + constexpr Point GetRightBottom() const { - return Size(left.width - right.width, left.height - right.height); + return Point(left + width, top + height); } - constexpr bool operator==(const Size& left, const Size& right) + constexpr Size GetSize() const { - return left.width == right.width && left.height == right.height; + return Size(width, height); } - constexpr bool operator!=(const Size& left, const Size& right) + constexpr bool IsPointInside(const Point& point) const { - return !(left == right); + return + point.x >= left && + point.x < GetRight() && + point.y >= top && + point.y < GetBottom(); } - struct Rect + float left = 0.0f; + float top = 0.0f; + float width = 0.0f; + float height = 0.0f; + }; + + enum class MouseButton + { + Left, + Right, + Middle + }; + + struct TextRange + { + constexpr static std::optional<TextRange> FromTwoSides(unsigned first, unsigned second) { - constexpr Rect() = default; - constexpr Rect(const float left, const float top, const float width, const float height) - : left(left), top(top), width(width), height(height) { } - constexpr Rect(const Point& lefttop, const Size& size) - : left(lefttop.x), top(lefttop.y), width(size.width), height(size.height) { } - - constexpr static Rect FromVertices(const float left, const float top, const float right, const float bottom) - { - return Rect(left, top, right - left, bottom - top); - } - - constexpr float GetRight() const - { - return left + width; - } - - constexpr float GetBottom() const - { - return top + height; - } - - constexpr Point GetLeftTop() const - { - return Point(left, top); - } - - constexpr Point GetRightBottom() const - { - return Point(left + width, top + height); - } - - constexpr Size GetSize() const - { - return Size(width, height); - } - - constexpr bool IsPointInside(const Point& point) const - { - return - point.x >= left && - point.x < GetRight() && - point.y >= top && - point.y < GetBottom(); - } - - float left = 0.0f; - float top = 0.0f; - float width = 0.0f; - float height = 0.0f; - }; - - enum class MouseButton + if (first > second) + return std::make_optional<TextRange>(second, first - second); + if (first < second) + return std::make_optional<TextRange>(first, second - first); + return std::nullopt; + } + + constexpr static std::pair<unsigned, unsigned> ToTwoSides(std::optional<TextRange> text_range, unsigned default_position = 0) { - Left, - Right, - Middle - }; + if (text_range.has_value()) + return std::make_pair(text_range.value().position, text_range.value().position + text_range.value().count); + return std::make_pair(default_position, default_position); + } - struct TextRange + constexpr TextRange() = default; + constexpr TextRange(const unsigned position, const unsigned count) + : position(position), count(count) { - constexpr static std::optional<TextRange> FromTwoSides(unsigned first, unsigned second) - { - if (first > second) - return std::make_optional<TextRange>(second, first - second); - if (first < second) - return std::make_optional<TextRange>(first, second - first); - return std::nullopt; - } - - constexpr static std::pair<unsigned, unsigned> ToTwoSides(std::optional<TextRange> text_range, unsigned default_position = 0) - { - if (text_range.has_value()) - return std::make_pair(text_range.value().position, text_range.value().position + text_range.value().count); - return std::make_pair(default_position, default_position); - } - - constexpr TextRange() = default; - constexpr TextRange(const unsigned position, const unsigned count) - : position(position), count(count) - { - - } - - unsigned position = 0; - unsigned count = 0; - }; - - bool IsKeyDown(int virtual_code); - bool IsKeyToggled(int virtual_code); - } + + } + + unsigned position = 0; + unsigned count = 0; + }; + + bool IsKeyDown(int virtual_code); + bool IsKeyToggled(int virtual_code); } |