aboutsummaryrefslogtreecommitdiff
path: root/include/cru/platform/graph
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-12-12 19:53:17 +0800
committercrupest <crupest@outlook.com>2019-12-12 19:53:17 +0800
commite69911a8b161b81ce3f7b209175766da2b7b3d4b (patch)
tree76b1f4b6f9f5ad6111578771be783ee456aeb912 /include/cru/platform/graph
parent154b5b838edfdcef93cd0a33c013ad7f5f9d7337 (diff)
downloadcru-e69911a8b161b81ce3f7b209175766da2b7b3d4b.tar.gz
cru-e69911a8b161b81ce3f7b209175766da2b7b3d4b.tar.bz2
cru-e69911a8b161b81ce3f7b209175766da2b7b3d4b.zip
...
Diffstat (limited to 'include/cru/platform/graph')
-rw-r--r--include/cru/platform/graph/base.hpp4
-rw-r--r--include/cru/platform/graph/brush.hpp45
-rw-r--r--include/cru/platform/graph/factory.hpp27
-rw-r--r--include/cru/platform/graph/font.hpp16
-rw-r--r--include/cru/platform/graph/geometry.hpp37
-rw-r--r--include/cru/platform/graph/graph_factory.hpp62
-rw-r--r--include/cru/platform/graph/painter.hpp37
-rw-r--r--include/cru/platform/graph/resource.hpp10
-rw-r--r--include/cru/platform/graph/text_layout.hpp28
-rw-r--r--include/cru/platform/graph/util/painter.hpp (renamed from include/cru/platform/graph/util/painter_util.hpp)7
10 files changed, 76 insertions, 197 deletions
diff --git a/include/cru/platform/graph/base.hpp b/include/cru/platform/graph/base.hpp
new file mode 100644
index 00000000..8c2a2d2f
--- /dev/null
+++ b/include/cru/platform/graph/base.hpp
@@ -0,0 +1,4 @@
+#pragma once
+#include "../graph_base.hpp"
+#include "../matrix.hpp"
+#include "../resource.hpp"
diff --git a/include/cru/platform/graph/brush.hpp b/include/cru/platform/graph/brush.hpp
index d292ae82..af7a1dec 100644
--- a/include/cru/platform/graph/brush.hpp
+++ b/include/cru/platform/graph/brush.hpp
@@ -1,46 +1,11 @@
#pragma once
-#include "../graphic_base.hpp"
-#include "../native_resource.hpp"
+#include "resource.hpp"
namespace cru::platform::graph {
-class Brush : public NativeResource {
- protected:
- Brush() = default;
+struct IBrush : virtual IGraphResource {};
- 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;
-
- protected:
- Color color_ = colors::black;
+struct ISolidColorBrush : virtual IBrush {
+ virtual Color GetColor() = 0;
+ virtual void SetColor(const Color& color) = 0;
};
} // namespace cru::platform::graph
diff --git a/include/cru/platform/graph/factory.hpp b/include/cru/platform/graph/factory.hpp
new file mode 100644
index 00000000..2c52cbb8
--- /dev/null
+++ b/include/cru/platform/graph/factory.hpp
@@ -0,0 +1,27 @@
+#pragma once
+#include "base.hpp"
+
+#include "brush.hpp"
+#include "font.hpp"
+#include "geometry.hpp"
+#include "text_layout.hpp"
+
+#include <memory>
+#include <string>
+#include <string_view>
+#include <utility>
+
+namespace cru::platform::graph {
+// Entry point of the graph module.
+struct IGraphFactory : virtual INativeResource {
+ virtual std::unique_ptr<ISolidColorBrush> CreateSolidColorBrush() = 0;
+
+ virtual std::unique_ptr<IGeometryBuilder> CreateGeometryBuilder() = 0;
+
+ virtual std::unique_ptr<IFont> CreateFont(const std::string_view& font_family,
+ float font_size) = 0;
+
+ virtual std::unique_ptr<ITextLayout> CreateTextLayout(
+ std::shared_ptr<IFont> font, std::string text) = 0;
+};
+} // namespace cru::platform::graph
diff --git a/include/cru/platform/graph/font.hpp b/include/cru/platform/graph/font.hpp
index bd470256..98ce80e7 100644
--- a/include/cru/platform/graph/font.hpp
+++ b/include/cru/platform/graph/font.hpp
@@ -1,18 +1,6 @@
#pragma once
-#include "../native_resource.hpp"
+#include "resource.hpp"
namespace cru::platform::graph {
-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;
-};
+struct IFont : virtual IGraphResource {};
} // namespace cru::platform::graph
diff --git a/include/cru/platform/graph/geometry.hpp b/include/cru/platform/graph/geometry.hpp
index d31b3b27..689b2ab9 100644
--- a/include/cru/platform/graph/geometry.hpp
+++ b/include/cru/platform/graph/geometry.hpp
@@ -1,45 +1,22 @@
#pragma once
-#include "../graphic_base.hpp"
-#include "../native_resource.hpp"
+#include "resource.hpp"
-namespace cru::platform::graph {
-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;
+#include <memory>
- public:
+namespace cru::platform::graph {
+struct IGeometry : virtual IGraphResource {
virtual bool FillContains(const Point& 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;
+// After called Build, calling every method will throw a
- public:
+class IGeometryBuilder : virtual IGraphResource {
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;
+ virtual std::unique_ptr<IGeometry> Build() = 0;
};
} // namespace cru::platform::graph
diff --git a/include/cru/platform/graph/graph_factory.hpp b/include/cru/platform/graph/graph_factory.hpp
deleted file mode 100644
index 0b1034cc..00000000
--- a/include/cru/platform/graph/graph_factory.hpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#pragma once
-#include "../graphic_base.hpp"
-#include "../native_resource.hpp"
-
-#include "brush.hpp"
-#include "font.hpp"
-#include "geometry.hpp"
-#include "text_layout.hpp"
-
-#include <memory>
-#include <string>
-#include <string_view>
-
-namespace cru::platform::graph {
-// Entry point of the graph module.
-// If you create a IUiApplication instance, then you should not create
-// IGraphFactory manually. IUiApplication will call
-// 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.
-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 GraphFactory* CreateInstance();
-
- // Get the global instance. If it is not created, then return nullptr.
- 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;
-
- virtual bool IsAutoDelete() const = 0;
- virtual void SetAutoDelete(bool value) = 0;
-};
-} // namespace cru::platform::graph
diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp
index 97d4b4cf..3ae9cf1c 100644
--- a/include/cru/platform/graph/painter.hpp
+++ b/include/cru/platform/graph/painter.hpp
@@ -1,42 +1,27 @@
#pragma once
-#include "../graphic_base.hpp"
-#include "../matrix.hpp"
-#include "../native_resource.hpp"
+#include "base.hpp"
namespace cru::platform::graph {
-class Brush;
-class Geometry;
-class TextLayout;
+struct IBrush;
+struct IGeometry;
+struct ITextLayout;
-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;
-
- public:
+struct IPainter : virtual INativeResource {
virtual Matrix GetTransform() = 0;
virtual void SetTransform(const Matrix& matrix) = 0;
virtual void Clear(const Color& color) = 0;
- virtual void StrokeRectangle(const Rect& rectangle, Brush* brush,
+ virtual void StrokeRectangle(const Rect& rectangle, IBrush* brush,
float width) = 0;
- virtual void FillRectangle(const Rect& rectangle, Brush* brush) = 0;
+ virtual void FillRectangle(const Rect& rectangle, IBrush* brush) = 0;
- virtual void StrokeGeometry(Geometry* geometry, Brush* brush,
+ virtual void StrokeGeometry(IGeometry* geometry, IBrush* brush,
float width) = 0;
- virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0;
+ virtual void FillGeometry(IGeometry* geometry, IBrush* brush) = 0;
- virtual void DrawText(const Point& offset, TextLayout* text_layout,
- Brush* brush) = 0;
+ virtual void DrawText(const Point& offset, ITextLayout* text_layout,
+ IBrush* brush) = 0;
virtual void EndDraw() = 0;
};
diff --git a/include/cru/platform/graph/resource.hpp b/include/cru/platform/graph/resource.hpp
new file mode 100644
index 00000000..255865eb
--- /dev/null
+++ b/include/cru/platform/graph/resource.hpp
@@ -0,0 +1,10 @@
+#pragma once
+#include "base.hpp"
+
+namespace cru::platform::graph {
+struct IGraphFactory;
+
+struct IGraphResource : virtual INativeResource {
+ virtual IGraphFactory* GetGraphFactory() = 0;
+};
+} // namespace cru::platform::graph
diff --git a/include/cru/platform/graph/text_layout.hpp b/include/cru/platform/graph/text_layout.hpp
index 56943098..4f6e81e1 100644
--- a/include/cru/platform/graph/text_layout.hpp
+++ b/include/cru/platform/graph/text_layout.hpp
@@ -1,33 +1,19 @@
#pragma once
-#include "../graphic_base.hpp"
-#include "../native_resource.hpp"
+#include "resource.hpp"
#include <memory>
#include <string>
#include <vector>
namespace cru::platform::graph {
-class Font;
+struct IFont;
-class TextLayout : public NativeResource {
- protected:
- TextLayout() = default;
+struct ITextLayout : virtual IGraphResource {
+ virtual std::string GetText() = 0;
+ virtual void SetText(std::string new_text) = 0;
- public:
- TextLayout(const TextLayout& other) = delete;
- TextLayout& operator=(const TextLayout& other) = delete;
-
- TextLayout(TextLayout&& other) = delete;
- TextLayout& operator=(TextLayout&& other) = delete;
-
- ~TextLayout() override = default;
-
- public:
- virtual std::wstring GetText() = 0;
- virtual void SetText(std::wstring new_text) = 0;
-
- virtual std::shared_ptr<Font> GetFont() = 0;
- virtual void SetFont(std::shared_ptr<Font> font) = 0;
+ virtual std::shared_ptr<IFont> GetFont() = 0;
+ virtual void SetFont(std::shared_ptr<IFont> font) = 0;
virtual void SetMaxWidth(float max_width) = 0;
virtual void SetMaxHeight(float max_height) = 0;
diff --git a/include/cru/platform/graph/util/painter_util.hpp b/include/cru/platform/graph/util/painter.hpp
index 7a655a34..72d96bc1 100644
--- a/include/cru/platform/graph/util/painter_util.hpp
+++ b/include/cru/platform/graph/util/painter.hpp
@@ -6,13 +6,12 @@
namespace cru::platform::graph::util {
template <typename Fn>
-inline void WithTransform(Painter* painter, const Matrix& matrix,
- const Fn& action) {
- static_assert(std::is_invocable_v<decltype(action), Painter*>,
+void WithTransform(IPainter* painter, const Matrix& matrix, const Fn& action) {
+ static_assert(std::is_invocable_v<decltype(action), IPainter*>,
"Action must can be be invoked with painter.");
const auto old = painter->GetTransform();
painter->SetTransform(old * matrix);
action(painter);
painter->SetTransform(old);
}
-} // namespace cru::platform::util
+} // namespace cru::platform::graph::util