diff options
Diffstat (limited to 'include/cru/platform')
-rw-r--r-- | include/cru/platform/graph/brush.hpp | 49 | ||||
-rw-r--r-- | include/cru/platform/graph/font.hpp | 16 | ||||
-rw-r--r-- | include/cru/platform/graph/geometry.hpp | 48 | ||||
-rw-r--r-- | include/cru/platform/graph/graph_factory.hpp | 48 | ||||
-rw-r--r-- | include/cru/platform/graph/painter.hpp | 45 | ||||
-rw-r--r-- | include/cru/platform/graph/text_layout.hpp | 36 | ||||
-rw-r--r-- | include/cru/platform/graphic_base.hpp | 257 | ||||
-rw-r--r-- | include/cru/platform/matrix.hpp | 4 | ||||
-rw-r--r-- | include/cru/platform/native_resource.hpp | 22 |
9 files changed, 460 insertions, 65 deletions
diff --git a/include/cru/platform/graph/brush.hpp b/include/cru/platform/graph/brush.hpp index 7688ec4a..d292ae82 100644 --- a/include/cru/platform/graph/brush.hpp +++ b/include/cru/platform/graph/brush.hpp @@ -1,13 +1,46 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IBrush : public virtual Interface {}; +class Brush : public NativeResource { + protected: + Brush() = default; + + public: + Brush(const Brush& other) = delete; + Brush& operator=(const Brush& other) = delete; + + Brush(Brush&& other) = delete; + Brush& operator=(Brush&& other) = delete; + + ~Brush() override = default; +}; + +class SolidColorBrush : public Brush { + protected: + SolidColorBrush() = default; + + public: + SolidColorBrush(const SolidColorBrush& other) = delete; + SolidColorBrush& operator=(const SolidColorBrush& other) = delete; + + SolidColorBrush(SolidColorBrush&& other) = delete; + SolidColorBrush& operator=(SolidColorBrush&& other) = delete; + + ~SolidColorBrush() = default; + + public: + Color GetColor() { return color_; } + void SetColor(const Color& color) { + color_ = color; + OnSetColor(color); + } + + protected: + virtual void OnSetColor(const Color& color) = 0; -struct ISolidColorBrush : public virtual IBrush { - virtual ui::Color GetColor() = 0; - virtual void SetColor(const ui::Color& color) = 0; + protected: + Color color_ = colors::black; }; -} // namespace cru::platform +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/font.hpp b/include/cru/platform/graph/font.hpp index f74dc7db..bd470256 100644 --- a/include/cru/platform/graph/font.hpp +++ b/include/cru/platform/graph/font.hpp @@ -1,8 +1,18 @@ #pragma once -#include "cru/common/base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IFontDescriptor : virtual Interface { +class Font : public NativeResource { + protected: + Font() = default; + public: + Font(const Font& other) = delete; + Font& operator=(const Font& other) = delete; + + Font(Font&& other) = delete; + Font& operator=(Font&& other) = delete; + + ~Font() override = default; }; -} +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/geometry.hpp b/include/cru/platform/graph/geometry.hpp index 71bf8c73..d31b3b27 100644 --- a/include/cru/platform/graph/geometry.hpp +++ b/include/cru/platform/graph/geometry.hpp @@ -1,19 +1,45 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/endable.hpp" -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IGeometry : virtual Interface { - virtual bool FillContains(const ui::Point& point) = 0; +class Geometry : public NativeResource { + protected: + Geometry() = default; + + public: + Geometry(const Geometry& other) = delete; + Geometry& operator=(const Geometry& other) = delete; + + Geometry(Geometry&& other) = delete; + Geometry& operator=(Geometry&& other) = delete; + + ~Geometry() override = default; + + public: + virtual bool FillContains(const Point& point) = 0; }; -struct IGeometryBuilder : virtual Interface, virtual IEndable<IGeometry*> { - virtual void BeginFigure(const ui::Point& point) = 0; - virtual void LineTo(const ui::Point& point) = 0; - virtual void QuadraticBezierTo(const ui::Point& control_point, - const ui::Point& end_point) = 0; +class GeometryBuilder : public NativeResource { + protected: + GeometryBuilder() = default; + + public: + GeometryBuilder(const GeometryBuilder& other) = delete; + GeometryBuilder& operator=(const GeometryBuilder& other) = delete; + + GeometryBuilder(GeometryBuilder&& other) = delete; + GeometryBuilder& operator=(GeometryBuilder&& other) = delete; + + ~GeometryBuilder() override = default; + + public: + virtual void BeginFigure(const Point& point) = 0; + virtual void LineTo(const Point& point) = 0; + virtual void QuadraticBezierTo(const Point& control_point, + const Point& end_point) = 0; virtual void CloseFigure(bool close) = 0; + + virtual Geometry* Build() = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/graph_factory.hpp b/include/cru/platform/graph/graph_factory.hpp index 60d4ed8a..69afc7b3 100644 --- a/include/cru/platform/graph/graph_factory.hpp +++ b/include/cru/platform/graph/graph_factory.hpp @@ -1,8 +1,6 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/auto_delete.hpp" -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" #include "brush.hpp" #include "font.hpp" @@ -20,20 +18,42 @@ namespace cru::platform::graph { // IGraphFactory::CreateInstance and set auto-delete to true. // The manual creation method of IGraphFactory provides a you a way to use graph // related tools without interact with actual ui like window system. -struct IGraphFactory : virtual Interface, virtual IAutoDelete { +class GraphFactory : public NativeResource { + public: // Create a platform-specific instance and save it as the global instance. // Do not create the instance twice. Implements should assert for that. // After creating, get the instance by GetInstance. - static IGraphFactory* CreateInstance(); + static GraphFactory* CreateInstance(); // Get the global instance. If it is not created, then return nullptr. - static IGraphFactory* GetInstance(); - - virtual ISolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; - virtual IGeometryBuilder* CreateGeometryBuilder() = 0; - virtual IFontDescriptor* CreateFontDescriptor( - const std::wstring_view& font_family, float font_size) = 0; - virtual ITextLayout* CreateTextLayout(std::shared_ptr<IFontDescriptor> font, - std::wstring text) = 0; + static GraphFactory* GetInstance(); + + protected: + GraphFactory() = default; + + public: + GraphFactory(const GraphFactory& other) = delete; + GraphFactory& operator=(const GraphFactory& other) = delete; + + GraphFactory(GraphFactory&& other) = delete; + GraphFactory& operator=(GraphFactory&& other) = delete; + + ~GraphFactory() override = default; + + public: + virtual SolidColorBrush* CreateSolidColorBrush() = 0; + SolidColorBrush* CreateSolidColorBrush(const Color& color) { + const auto brush = CreateSolidColorBrush(); + brush->SetColor(color); + return brush; + } + + virtual GeometryBuilder* CreateGeometryBuilder() = 0; + + virtual Font* CreateFont(const std::wstring_view& font_family, + float font_size) = 0; + + virtual TextLayout* CreateTextLayout(std::shared_ptr<Font> font, + std::wstring text) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp index 34c895d4..1096aa7c 100644 --- a/include/cru/platform/graph/painter.hpp +++ b/include/cru/platform/graph/painter.hpp @@ -1,26 +1,41 @@ #pragma once -#include "cru/common/base.hpp" - +#include "../graphic_base.hpp" #include "../matrix.hpp" -#include "cru/common/endable.hpp" -#include "cru/common/ui_base.hpp" +#include "../native_resource.hpp" namespace cru::platform::graph { -struct IBrush; -struct IGeometry; -struct ITextLayout; +class Brush; +class Geometry; +class TextLayout; + +class Painter : public NativeResource { + protected: + Painter() = default; + + public: + Painter(const Painter& other) = delete; + Painter& operator=(const Painter& other) = delete; + + Painter(Painter&& other) = delete; + Painter& operator=(Painter&& other) = delete; + + ~Painter() override = default; -struct IPainter : virtual Interface, virtual IEndable<void> { + public: virtual Matrix GetTransform() = 0; virtual void SetTransform(const Matrix& matrix) = 0; - virtual void Clear(const ui::Color& color) = 0; - virtual void StrokeRectangle(const ui::Rect& rectangle, IBrush* brush, + + virtual void Clear(const Color& color) = 0; + + virtual void StrokeRectangle(const Rect& rectangle, Brush* brush, float width) = 0; - virtual void FillRectangle(const ui::Rect& rectangle, IBrush* brush) = 0; - virtual void StrokeGeometry(IGeometry* geometry, IBrush* brush, + virtual void FillRectangle(const Rect& rectangle, Brush* brush) = 0; + + virtual void StrokeGeometry(Geometry* geometry, Brush* brush, float width) = 0; - virtual void FillGeometry(IGeometry* geometry, IBrush* brush) = 0; - virtual void DrawText(const ui::Point& offset, ITextLayout* text_layout, - IBrush* brush) = 0; + virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; + + virtual void DrawText(const Point& offset, TextLayout* text_layout, + Brush* brush) = 0; }; } // namespace cru::platform::graph diff --git a/include/cru/platform/graph/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp index 894c1408..56943098 100644 --- a/include/cru/platform/graph/text_layout.hpp +++ b/include/cru/platform/graph/text_layout.hpp @@ -1,24 +1,38 @@ #pragma once -#include "cru/common/base.hpp" - -#include "cru/common/ui_base.hpp" +#include "../graphic_base.hpp" +#include "../native_resource.hpp" #include <memory> #include <string> #include <vector> namespace cru::platform::graph { -struct IFontDescriptor; +class Font; + +class TextLayout : public NativeResource { + protected: + TextLayout() = default; + + public: + TextLayout(const TextLayout& other) = delete; + TextLayout& operator=(const TextLayout& other) = delete; + + TextLayout(TextLayout&& other) = delete; + TextLayout& operator=(TextLayout&& other) = delete; -struct ITextLayout : virtual Interface { + ~TextLayout() override = default; + + public: virtual std::wstring GetText() = 0; virtual void SetText(std::wstring new_text) = 0; - virtual std::shared_ptr<IFontDescriptor> GetFont() = 0; - virtual void SetFont(std::shared_ptr<IFontDescriptor> font) = 0; + + virtual std::shared_ptr<Font> GetFont() = 0; + virtual void SetFont(std::shared_ptr<Font> font) = 0; + virtual void SetMaxWidth(float max_width) = 0; virtual void SetMaxHeight(float max_height) = 0; - virtual ui::Rect GetTextBounds() = 0; - virtual std::vector<ui::Rect> TextRangeRect( - const ui::TextRange& text_range) = 0; + + virtual Rect GetTextBounds() = 0; + virtual std::vector<Rect> TextRangeRect(const TextRange& text_range) = 0; }; -} // namespace cru::platform +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphic_base.hpp b/include/cru/platform/graphic_base.hpp new file mode 100644 index 00000000..2aa4e2cb --- /dev/null +++ b/include/cru/platform/graphic_base.hpp @@ -0,0 +1,257 @@ +#pragma once +#include "cru/common/pre_config.hpp" + +#include <cstdint> +#include <optional> +#include <utility> + +namespace cru::platform { +struct Point final { + constexpr Point() = default; + constexpr Point(const float x, const float y) : x(x), y(y) {} + + float x = 0; + float y = 0; +}; + +constexpr bool operator==(const Point& left, const Point& right) { + return left.x == right.x && left.y == right.y; +} + +constexpr bool operator!=(const Point& left, const Point& right) { + return !(left == right); +} + +struct Size final { + 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 Thickness final { + constexpr Thickness() : Thickness(0) {} + + constexpr explicit Thickness(const float width) + : left(width), top(width), right(width), bottom(width) {} + + constexpr explicit Thickness(const float horizontal, const float vertical) + : left(horizontal), top(vertical), right(horizontal), bottom(vertical) {} + + constexpr Thickness(const float left, const float top, const float right, + const float bottom) + : left(left), top(top), right(right), bottom(bottom) {} + + constexpr float GetHorizontalTotal() const { return left + right; } + + constexpr float GetVerticalTotal() const { return top + bottom; } + + void SetLeftRight(const float value) { left = right = value; } + + void SetTopBottom(const float value) { top = bottom = value; } + + void SetAll(const float value) { left = top = right = bottom = value; } + + constexpr float Validate() const { + return left >= 0.0 && top >= 0.0 && right >= 0.0 && bottom >= 0.0; + } + + float left; + float top; + float right; + float bottom; +}; + +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 final { + 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 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; } + + 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 Point GetLeftBottom() const { return Point(left, top + height); } + + constexpr Point GetRightTop() const { 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); } + + constexpr Rect Shrink(const Thickness& thickness) const { + return Rect(left + thickness.left, top + thickness.top, + width - thickness.GetHorizontalTotal(), + height - thickness.GetVerticalTotal()); + } + + 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; +}; + +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 final { + 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; +} + +constexpr bool operator!=(const RoundedRect& left, const RoundedRect& right) { + return !(left == right); +} + +struct Ellipse final { + 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); +} + +struct TextRange final { + 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; +}; + +struct Color { + constexpr Color() : Color(0, 0, 0, 255) {} + constexpr Color(std::uint8_t red, std::uint8_t green, std::uint8_t blue, + std::uint8_t alpha = 255) + : red(red), green(green), blue(blue), alpha(alpha) {} + + constexpr static Color FromHex(std::uint32_t hex) { + return Color(hex & (0b11111111 << 16), hex & (0b11111111 << 8), + hex & (0b11111111), hex & (0b11111111 << 24)); + } + + std::uint8_t red; + std::uint8_t green; + std::uint8_t blue; + std::uint8_t alpha; +}; + +namespace colors { +constexpr Color black{0, 0, 0}; +constexpr Color white{255, 255, 255}; +constexpr Color skyblue = Color::FromHex(0x87ceeb); +} // namespace colors +} // namespace cru::ui diff --git a/include/cru/platform/matrix.hpp b/include/cru/platform/matrix.hpp index 37e4725e..b0165a88 100644 --- a/include/cru/platform/matrix.hpp +++ b/include/cru/platform/matrix.hpp @@ -1,7 +1,5 @@ #pragma once -#include "cru/common/pre_config.hpp" - -#include "cru/common/ui_base.hpp" +#include "graphic_base.hpp" #include <cmath> diff --git a/include/cru/platform/native_resource.hpp b/include/cru/platform/native_resource.hpp new file mode 100644 index 00000000..787c46a5 --- /dev/null +++ b/include/cru/platform/native_resource.hpp @@ -0,0 +1,22 @@ +#pragma once +#include "cru/common/base.hpp" + +#include <string_view> + +namespace cru::platform { +class NativeResource : public Object { + protected: + NativeResource() = default; + + public: + NativeResource(NativeResource&& other) = delete; + NativeResource& operator=(NativeResource&& other) = delete; + + NativeResource(NativeResource&& other) = delete; + NativeResource& operator=(NativeResource&& other) = delete; + ~NativeResource() override = default; + + public: + virtual std::wstring_view GetPlatformId() const = 0; +}; +} // namespace cru::platform |