diff options
Diffstat (limited to 'include/cru/platform/graphics')
-rw-r--r-- | include/cru/platform/graphics/Base.hpp | 24 | ||||
-rw-r--r-- | include/cru/platform/graphics/Brush.hpp | 11 | ||||
-rw-r--r-- | include/cru/platform/graphics/Factory.hpp | 25 | ||||
-rw-r--r-- | include/cru/platform/graphics/Font.hpp | 8 | ||||
-rw-r--r-- | include/cru/platform/graphics/Geometry.hpp | 20 | ||||
-rw-r--r-- | include/cru/platform/graphics/Painter.hpp | 29 | ||||
-rw-r--r-- | include/cru/platform/graphics/Resource.hpp | 10 | ||||
-rw-r--r-- | include/cru/platform/graphics/TextLayout.hpp | 24 | ||||
-rw-r--r-- | include/cru/platform/graphics/util/Painter.hpp | 17 |
9 files changed, 168 insertions, 0 deletions
diff --git a/include/cru/platform/graphics/Base.hpp b/include/cru/platform/graphics/Base.hpp new file mode 100644 index 00000000..e751ebdb --- /dev/null +++ b/include/cru/platform/graphics/Base.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "../GraphBase.hpp" +#include "../Matrix.hpp" +#include "../Resource.hpp" + +#include <memory> + +namespace cru::platform::graphics { +// forward declarations +struct IGraphFactory; +struct IBrush; +struct ISolidColorBrush; +struct IFont; +struct IGeometry; +struct IGeometryBuilder; +struct IPainter; +struct ITextLayout; + +struct TextHitTestResult { + int position; + bool trailing; + bool insideText; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Brush.hpp b/include/cru/platform/graphics/Brush.hpp new file mode 100644 index 00000000..10c666b5 --- /dev/null +++ b/include/cru/platform/graphics/Brush.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { +struct IBrush : virtual IGraphResource {}; + +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/graphics/Factory.hpp b/include/cru/platform/graphics/Factory.hpp new file mode 100644 index 00000000..d1b37783 --- /dev/null +++ b/include/cru/platform/graphics/Factory.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "Resource.hpp" + +#include "Brush.hpp" +#include "Font.hpp" +#include "Geometry.hpp" +#include "TextLayout.hpp" + +#include <string> +#include <string_view> + +namespace cru::platform::graphics { +// 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(std::u16string font_family, + float font_size) = 0; + + virtual std::unique_ptr<ITextLayout> CreateTextLayout( + std::shared_ptr<IFont> font, std::u16string text) = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Font.hpp b/include/cru/platform/graphics/Font.hpp new file mode 100644 index 00000000..70392a69 --- /dev/null +++ b/include/cru/platform/graphics/Font.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { +struct IFont : virtual IGraphResource { + virtual float GetFontSize() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Geometry.hpp b/include/cru/platform/graphics/Geometry.hpp new file mode 100644 index 00000000..b0ce6ad9 --- /dev/null +++ b/include/cru/platform/graphics/Geometry.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { +struct IGeometry : virtual IGraphResource { + virtual bool FillContains(const Point& point) = 0; +}; + +// After called Build, calling every method will throw a + +struct 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 std::unique_ptr<IGeometry> Build() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Painter.hpp b/include/cru/platform/graphics/Painter.hpp new file mode 100644 index 00000000..76140c32 --- /dev/null +++ b/include/cru/platform/graphics/Painter.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graphics { + +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, IBrush* brush, + float width) = 0; + virtual void FillRectangle(const Rect& rectangle, IBrush* brush) = 0; + + virtual void StrokeGeometry(IGeometry* geometry, IBrush* brush, + float width) = 0; + virtual void FillGeometry(IGeometry* geometry, IBrush* brush) = 0; + + virtual void DrawText(const Point& offset, ITextLayout* text_layout, + IBrush* brush) = 0; + + virtual void PushLayer(const Rect& bounds) = 0; + + virtual void PopLayer() = 0; + + virtual void EndDraw() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/Resource.hpp b/include/cru/platform/graphics/Resource.hpp new file mode 100644 index 00000000..a1625ce4 --- /dev/null +++ b/include/cru/platform/graphics/Resource.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "Base.hpp" + +namespace cru::platform::graphics { +struct IGraphFactory; + +struct IGraphResource : virtual INativeResource { + virtual IGraphFactory* GetGraphFactory() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/TextLayout.hpp b/include/cru/platform/graphics/TextLayout.hpp new file mode 100644 index 00000000..efd017d6 --- /dev/null +++ b/include/cru/platform/graphics/TextLayout.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "Resource.hpp" + +#include <string> +#include <vector> + +namespace cru::platform::graphics { +struct ITextLayout : virtual IGraphResource { + virtual std::u16string GetText() = 0; + virtual std::u16string_view GetTextView() = 0; + virtual void SetText(std::u16string new_text) = 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; + + virtual Rect GetTextBounds() = 0; + virtual std::vector<Rect> TextRangeRect(const TextRange& text_range) = 0; + virtual Point TextSinglePoint(Index position, bool trailing) = 0; + virtual TextHitTestResult HitTest(const Point& point) = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graphics/util/Painter.hpp b/include/cru/platform/graphics/util/Painter.hpp new file mode 100644 index 00000000..af3a1997 --- /dev/null +++ b/include/cru/platform/graphics/util/Painter.hpp @@ -0,0 +1,17 @@ +#pragma once +#include "../Painter.hpp" + +#include <functional> +#include <type_traits> + +namespace cru::platform::graphics::util { +template <typename Fn> +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::graphics::util |