From 06d1d0442276a05b6caad6e3468f4afb1e8ee5df Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 28 Jun 2020 00:03:11 +0800 Subject: ... --- include/cru/platform/Check.hpp | 40 ++++++++++ include/cru/platform/Exception.hpp | 28 +++++++ include/cru/platform/Matrix.hpp | 83 +++++++++++++++++++ include/cru/platform/Resource.hpp | 10 +++ include/cru/platform/check.hpp | 40 ---------- include/cru/platform/exception.hpp | 28 ------- include/cru/platform/graph/Base.hpp | 24 ++++++ include/cru/platform/graph/Brush.hpp | 11 +++ include/cru/platform/graph/Factory.hpp | 25 ++++++ include/cru/platform/graph/Font.hpp | 8 ++ include/cru/platform/graph/Geometry.hpp | 20 +++++ include/cru/platform/graph/Painter.hpp | 29 +++++++ include/cru/platform/graph/Resource.hpp | 10 +++ include/cru/platform/graph/base.hpp | 24 ------ include/cru/platform/graph/brush.hpp | 11 --- include/cru/platform/graph/factory.hpp | 25 ------ include/cru/platform/graph/font.hpp | 8 -- include/cru/platform/graph/geometry.hpp | 20 ----- include/cru/platform/graph/painter.hpp | 29 ------- include/cru/platform/graph/resource.hpp | 10 --- include/cru/platform/graph/util/Painter.hpp | 17 ++++ include/cru/platform/graph/util/painter.hpp | 17 ---- include/cru/platform/matrix.hpp | 83 ------------------- include/cru/platform/native/Base.hpp | 52 ++++++++++++ include/cru/platform/native/Cursor.hpp | 15 ++++ include/cru/platform/native/Keyboard.hpp | 120 ++++++++++++++++++++++++++++ include/cru/platform/native/Window.hpp | 67 ++++++++++++++++ include/cru/platform/native/base.hpp | 52 ------------ include/cru/platform/native/cursor.hpp | 15 ---- include/cru/platform/native/keyboard.hpp | 120 ---------------------------- include/cru/platform/native/window.hpp | 67 ---------------- include/cru/platform/resource.hpp | 10 --- 32 files changed, 559 insertions(+), 559 deletions(-) create mode 100644 include/cru/platform/Check.hpp create mode 100644 include/cru/platform/Exception.hpp create mode 100644 include/cru/platform/Matrix.hpp create mode 100644 include/cru/platform/Resource.hpp delete mode 100644 include/cru/platform/check.hpp delete mode 100644 include/cru/platform/exception.hpp create mode 100644 include/cru/platform/graph/Base.hpp create mode 100644 include/cru/platform/graph/Brush.hpp create mode 100644 include/cru/platform/graph/Factory.hpp create mode 100644 include/cru/platform/graph/Font.hpp create mode 100644 include/cru/platform/graph/Geometry.hpp create mode 100644 include/cru/platform/graph/Painter.hpp create mode 100644 include/cru/platform/graph/Resource.hpp delete mode 100644 include/cru/platform/graph/base.hpp delete mode 100644 include/cru/platform/graph/brush.hpp delete mode 100644 include/cru/platform/graph/factory.hpp delete mode 100644 include/cru/platform/graph/font.hpp delete mode 100644 include/cru/platform/graph/geometry.hpp delete mode 100644 include/cru/platform/graph/painter.hpp delete mode 100644 include/cru/platform/graph/resource.hpp create mode 100644 include/cru/platform/graph/util/Painter.hpp delete mode 100644 include/cru/platform/graph/util/painter.hpp delete mode 100644 include/cru/platform/matrix.hpp create mode 100644 include/cru/platform/native/Base.hpp create mode 100644 include/cru/platform/native/Cursor.hpp create mode 100644 include/cru/platform/native/Keyboard.hpp create mode 100644 include/cru/platform/native/Window.hpp delete mode 100644 include/cru/platform/native/base.hpp delete mode 100644 include/cru/platform/native/cursor.hpp delete mode 100644 include/cru/platform/native/keyboard.hpp delete mode 100644 include/cru/platform/native/window.hpp delete mode 100644 include/cru/platform/resource.hpp (limited to 'include/cru/platform') diff --git a/include/cru/platform/Check.hpp b/include/cru/platform/Check.hpp new file mode 100644 index 00000000..f4bbcfe8 --- /dev/null +++ b/include/cru/platform/Check.hpp @@ -0,0 +1,40 @@ +#pragma once +#include "Exception.hpp" +#include "Resource.hpp" + +#include +#include +#include + +namespace cru::platform { +template +TTarget* CheckPlatform(INativeResource* resource, + const std::string_view& target_platform) { + Expects(resource); + const auto result = dynamic_cast(resource); + if (result == nullptr) { + throw UnsupportPlatformException(fmt::format( + "Try to convert resource to target platform failed. Platform id of " + "resource to convert: {} . Target platform id: {} .", + resource->GetPlatformId(), target_platform)); + } + return result; +} + +template +std::shared_ptr CheckPlatform( + const std::shared_ptr& resource, + const std::string_view& target_platform) { + static_assert(std::is_base_of_v, + "TSource must be a subclass of INativeResource."); + Expects(resource); + const auto result = std::dynamic_pointer_cast(resource); + if (result == nullptr) { + throw UnsupportPlatformException(fmt::format( + "Try to convert resource to target platform failed. Platform id of " + "resource to convert: {} . Target platform id: {} .", + resource->GetPlatformId(), target_platform)); + } + return result; +} +} // namespace cru::platform diff --git a/include/cru/platform/Exception.hpp b/include/cru/platform/Exception.hpp new file mode 100644 index 00000000..8b958a1d --- /dev/null +++ b/include/cru/platform/Exception.hpp @@ -0,0 +1,28 @@ +#pragma once +#include "cru/common/Base.hpp" + +#include + +namespace cru::platform { +class PlatformException : public std::runtime_error { + public: + using runtime_error::runtime_error; // inherit constructors +}; + +// This exception is thrown when a resource is used on another platform. +// Of course, you can't mix resources of two different platform. +// For example, Win32 Brush (may add in the future) with Direct Painter. +class UnsupportPlatformException : public std::runtime_error { + public: + using runtime_error::runtime_error; // inherit constructors +}; + +// This exception is thrown when a resource has been disposed and not usable +// again. +// For example, calling Build twice on a GeometryBuilder::Build will lead to +// this exception. +class ReuseException : public std::runtime_error { + public: + using runtime_error::runtime_error; // inherit constructors +}; +} // namespace cru::platform diff --git a/include/cru/platform/Matrix.hpp b/include/cru/platform/Matrix.hpp new file mode 100644 index 00000000..cea5198b --- /dev/null +++ b/include/cru/platform/Matrix.hpp @@ -0,0 +1,83 @@ +#pragma once +#include "GraphBase.hpp" + +#include + +namespace cru::platform { +struct Matrix { + float m11; + float m12; + float m21; + float m22; + float m31; + float m32; + + Matrix() {} + + Matrix(float m11, float m12, float m21, float m22, float m31, float m32) { + this->m11 = m11; + this->m12 = m12; + this->m21 = m21; + this->m22 = m22; + this->m31 = m31; + this->m32 = m32; + } + + bool IsIdentity() const { + return m11 == 1.0f && m12 == 0.0f && m21 == 0.0f && m22 == 1.0f && + m31 == 0.0f && m32 == 0.0f; + } + + Matrix& operator*=(const Matrix& matrix) { + *this = Product(*this, matrix); + return *this; + } + + Matrix operator*(const Matrix& matrix) const { + return Product(*this, matrix); + } + + Point TransformPoint(const Point& point) const { + return Point{point.x * m11 + point.y * m21 + m31, + point.x * m12 + point.y * m22 + m32}; + } + + static Matrix Identity() { + return Matrix{1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}; + } + + static Matrix Translation(float x, float y) { + return Matrix{1.0f, 0.0f, 0.0f, 1.0f, x, y}; + } + + static Matrix Scale(float sx, float sy) { + return Matrix{sx, 0.0f, 0.0f, sy, 0.0f, 0.0f}; + } + + static Matrix Rotation(float angle) { + float r = AngleToRadian(angle); + float s = std::sinf(r); + float c = std::cosf(r); + + return Matrix{c, s, -s, c, 0.0f, 0.0f}; + } + + static Matrix Skew(float sx, float sy) { + return Matrix{1.0f, sx, sy, 1.0f, 0.0f, 0.0f}; + } + + static Matrix Product(const Matrix& a, const Matrix& b) { + return Matrix{a.m11 * b.m11 + a.m12 * b.m21, + a.m11 * b.m12 + a.m12 * b.m22, + a.m21 * b.m11 + a.m22 * b.m21, + a.m21 * b.m12 + a.m22 * b.m22, + a.m31 * b.m11 + a.m32 * b.m21 + b.m31, + a.m31 * b.m12 + a.m32 * b.m22 + b.m32}; + } + + private: + static constexpr float PI = 3.1415926535f; + + static float AngleToRadian(float angle) { return angle / 180.f * PI; } +}; +} // namespace cru::platform diff --git a/include/cru/platform/Resource.hpp b/include/cru/platform/Resource.hpp new file mode 100644 index 00000000..72cfaf52 --- /dev/null +++ b/include/cru/platform/Resource.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "cru/common/Base.hpp" + +#include + +namespace cru::platform { +struct INativeResource : virtual Interface { + virtual std::string_view GetPlatformId() const = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/check.hpp b/include/cru/platform/check.hpp deleted file mode 100644 index f4bbcfe8..00000000 --- a/include/cru/platform/check.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "Exception.hpp" -#include "Resource.hpp" - -#include -#include -#include - -namespace cru::platform { -template -TTarget* CheckPlatform(INativeResource* resource, - const std::string_view& target_platform) { - Expects(resource); - const auto result = dynamic_cast(resource); - if (result == nullptr) { - throw UnsupportPlatformException(fmt::format( - "Try to convert resource to target platform failed. Platform id of " - "resource to convert: {} . Target platform id: {} .", - resource->GetPlatformId(), target_platform)); - } - return result; -} - -template -std::shared_ptr CheckPlatform( - const std::shared_ptr& resource, - const std::string_view& target_platform) { - static_assert(std::is_base_of_v, - "TSource must be a subclass of INativeResource."); - Expects(resource); - const auto result = std::dynamic_pointer_cast(resource); - if (result == nullptr) { - throw UnsupportPlatformException(fmt::format( - "Try to convert resource to target platform failed. Platform id of " - "resource to convert: {} . Target platform id: {} .", - resource->GetPlatformId(), target_platform)); - } - return result; -} -} // namespace cru::platform diff --git a/include/cru/platform/exception.hpp b/include/cru/platform/exception.hpp deleted file mode 100644 index 8b958a1d..00000000 --- a/include/cru/platform/exception.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include "cru/common/Base.hpp" - -#include - -namespace cru::platform { -class PlatformException : public std::runtime_error { - public: - using runtime_error::runtime_error; // inherit constructors -}; - -// This exception is thrown when a resource is used on another platform. -// Of course, you can't mix resources of two different platform. -// For example, Win32 Brush (may add in the future) with Direct Painter. -class UnsupportPlatformException : public std::runtime_error { - public: - using runtime_error::runtime_error; // inherit constructors -}; - -// This exception is thrown when a resource has been disposed and not usable -// again. -// For example, calling Build twice on a GeometryBuilder::Build will lead to -// this exception. -class ReuseException : public std::runtime_error { - public: - using runtime_error::runtime_error; // inherit constructors -}; -} // namespace cru::platform diff --git a/include/cru/platform/graph/Base.hpp b/include/cru/platform/graph/Base.hpp new file mode 100644 index 00000000..61cfc5ef --- /dev/null +++ b/include/cru/platform/graph/Base.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "../GraphBase.hpp" +#include "../Matrix.hpp" +#include "../Resource.hpp" + +#include + +namespace cru::platform::graph { +// 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/graph/Brush.hpp b/include/cru/platform/graph/Brush.hpp new file mode 100644 index 00000000..e67384de --- /dev/null +++ b/include/cru/platform/graph/Brush.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graph { +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/graph/Factory.hpp b/include/cru/platform/graph/Factory.hpp new file mode 100644 index 00000000..0a425d15 --- /dev/null +++ b/include/cru/platform/graph/Factory.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "Resource.hpp" + +#include "Brush.hpp" +#include "Font.hpp" +#include "Geometry.hpp" +#include "TextLayout.hpp" + +#include +#include + +namespace cru::platform::graph { +// Entry point of the graph module. +struct IGraphFactory : virtual INativeResource { + virtual std::unique_ptr CreateSolidColorBrush() = 0; + + virtual std::unique_ptr CreateGeometryBuilder() = 0; + + virtual std::unique_ptr CreateFont(const std::string_view& font_family, + float font_size) = 0; + + virtual std::unique_ptr CreateTextLayout( + std::shared_ptr 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 new file mode 100644 index 00000000..182cc15b --- /dev/null +++ b/include/cru/platform/graph/Font.hpp @@ -0,0 +1,8 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graph { +struct IFont : virtual IGraphResource { + virtual float GetFontSize() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Geometry.hpp b/include/cru/platform/graph/Geometry.hpp new file mode 100644 index 00000000..354efd97 --- /dev/null +++ b/include/cru/platform/graph/Geometry.hpp @@ -0,0 +1,20 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graph { +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 Build() = 0; +}; +} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/Painter.hpp b/include/cru/platform/graph/Painter.hpp new file mode 100644 index 00000000..27ae420b --- /dev/null +++ b/include/cru/platform/graph/Painter.hpp @@ -0,0 +1,29 @@ +#pragma once +#include "Resource.hpp" + +namespace cru::platform::graph { + +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/graph/Resource.hpp b/include/cru/platform/graph/Resource.hpp new file mode 100644 index 00000000..8859360c --- /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/base.hpp b/include/cru/platform/graph/base.hpp deleted file mode 100644 index 61cfc5ef..00000000 --- a/include/cru/platform/graph/base.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "../GraphBase.hpp" -#include "../Matrix.hpp" -#include "../Resource.hpp" - -#include - -namespace cru::platform::graph { -// 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/graph/brush.hpp b/include/cru/platform/graph/brush.hpp deleted file mode 100644 index e67384de..00000000 --- a/include/cru/platform/graph/brush.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { -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/graph/factory.hpp b/include/cru/platform/graph/factory.hpp deleted file mode 100644 index 0a425d15..00000000 --- a/include/cru/platform/graph/factory.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "Resource.hpp" - -#include "Brush.hpp" -#include "Font.hpp" -#include "Geometry.hpp" -#include "TextLayout.hpp" - -#include -#include - -namespace cru::platform::graph { -// Entry point of the graph module. -struct IGraphFactory : virtual INativeResource { - virtual std::unique_ptr CreateSolidColorBrush() = 0; - - virtual std::unique_ptr CreateGeometryBuilder() = 0; - - virtual std::unique_ptr CreateFont(const std::string_view& font_family, - float font_size) = 0; - - virtual std::unique_ptr CreateTextLayout( - std::shared_ptr 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 deleted file mode 100644 index 182cc15b..00000000 --- a/include/cru/platform/graph/font.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { -struct IFont : virtual IGraphResource { - virtual float GetFontSize() = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/geometry.hpp b/include/cru/platform/graph/geometry.hpp deleted file mode 100644 index 354efd97..00000000 --- a/include/cru/platform/graph/geometry.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { -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 Build() = 0; -}; -} // namespace cru::platform::graph diff --git a/include/cru/platform/graph/painter.hpp b/include/cru/platform/graph/painter.hpp deleted file mode 100644 index 27ae420b..00000000 --- a/include/cru/platform/graph/painter.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include "Resource.hpp" - -namespace cru::platform::graph { - -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/graph/resource.hpp b/include/cru/platform/graph/resource.hpp deleted file mode 100644 index 8859360c..00000000 --- a/include/cru/platform/graph/resource.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#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/util/Painter.hpp b/include/cru/platform/graph/util/Painter.hpp new file mode 100644 index 00000000..f9aec027 --- /dev/null +++ b/include/cru/platform/graph/util/Painter.hpp @@ -0,0 +1,17 @@ +#pragma once +#include "../Painter.hpp" + +#include +#include + +namespace cru::platform::graph::util { +template +void WithTransform(IPainter* painter, const Matrix& matrix, const Fn& action) { + static_assert(std::is_invocable_v, + "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::graph::util diff --git a/include/cru/platform/graph/util/painter.hpp b/include/cru/platform/graph/util/painter.hpp deleted file mode 100644 index f9aec027..00000000 --- a/include/cru/platform/graph/util/painter.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "../Painter.hpp" - -#include -#include - -namespace cru::platform::graph::util { -template -void WithTransform(IPainter* painter, const Matrix& matrix, const Fn& action) { - static_assert(std::is_invocable_v, - "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::graph::util diff --git a/include/cru/platform/matrix.hpp b/include/cru/platform/matrix.hpp deleted file mode 100644 index cea5198b..00000000 --- a/include/cru/platform/matrix.hpp +++ /dev/null @@ -1,83 +0,0 @@ -#pragma once -#include "GraphBase.hpp" - -#include - -namespace cru::platform { -struct Matrix { - float m11; - float m12; - float m21; - float m22; - float m31; - float m32; - - Matrix() {} - - Matrix(float m11, float m12, float m21, float m22, float m31, float m32) { - this->m11 = m11; - this->m12 = m12; - this->m21 = m21; - this->m22 = m22; - this->m31 = m31; - this->m32 = m32; - } - - bool IsIdentity() const { - return m11 == 1.0f && m12 == 0.0f && m21 == 0.0f && m22 == 1.0f && - m31 == 0.0f && m32 == 0.0f; - } - - Matrix& operator*=(const Matrix& matrix) { - *this = Product(*this, matrix); - return *this; - } - - Matrix operator*(const Matrix& matrix) const { - return Product(*this, matrix); - } - - Point TransformPoint(const Point& point) const { - return Point{point.x * m11 + point.y * m21 + m31, - point.x * m12 + point.y * m22 + m32}; - } - - static Matrix Identity() { - return Matrix{1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}; - } - - static Matrix Translation(float x, float y) { - return Matrix{1.0f, 0.0f, 0.0f, 1.0f, x, y}; - } - - static Matrix Scale(float sx, float sy) { - return Matrix{sx, 0.0f, 0.0f, sy, 0.0f, 0.0f}; - } - - static Matrix Rotation(float angle) { - float r = AngleToRadian(angle); - float s = std::sinf(r); - float c = std::cosf(r); - - return Matrix{c, s, -s, c, 0.0f, 0.0f}; - } - - static Matrix Skew(float sx, float sy) { - return Matrix{1.0f, sx, sy, 1.0f, 0.0f, 0.0f}; - } - - static Matrix Product(const Matrix& a, const Matrix& b) { - return Matrix{a.m11 * b.m11 + a.m12 * b.m21, - a.m11 * b.m12 + a.m12 * b.m22, - a.m21 * b.m11 + a.m22 * b.m21, - a.m21 * b.m12 + a.m22 * b.m22, - a.m31 * b.m11 + a.m32 * b.m21 + b.m31, - a.m31 * b.m12 + a.m32 * b.m22 + b.m32}; - } - - private: - static constexpr float PI = 3.1415926535f; - - static float AngleToRadian(float angle) { return angle / 180.f * PI; } -}; -} // namespace cru::platform diff --git a/include/cru/platform/native/Base.hpp b/include/cru/platform/native/Base.hpp new file mode 100644 index 00000000..bba7b960 --- /dev/null +++ b/include/cru/platform/native/Base.hpp @@ -0,0 +1,52 @@ +#pragma once +#include "cru/common/Base.hpp" +#include "cru/common/Bitmask.hpp" +#include "cru/platform/graph/Base.hpp" +#include "Keyboard.hpp" + +namespace cru::platform::native { +struct ICursor; +struct ICursorManager; +struct IUiApplication; +struct INativeWindow; +struct INativeWindowResolver; +struct IInputMethodManager; +struct IInputMethodContext; + +struct Dpi { + float x; + float y; +}; + +namespace details { +struct TagMouseButton {}; +} // namespace details + +using MouseButton = Bitmask; + +namespace mouse_buttons { +constexpr MouseButton left{0b1}; +constexpr MouseButton middle{0b10}; +constexpr MouseButton right{0b100}; +} // namespace mouse_buttons + +enum class SystemCursorType { + Arrow, + Hand, +}; + +struct NativeMouseButtonEventArgs { + MouseButton button; + Point point; + KeyModifier modifier; +}; + +struct NativeKeyEventArgs { + KeyCode key; + KeyModifier modifier; +}; + +enum class FocusChangeType { Gain, Lost }; + +enum class MouseEnterLeaveType { Enter, Leave }; +} // namespace cru::platform::native diff --git a/include/cru/platform/native/Cursor.hpp b/include/cru/platform/native/Cursor.hpp new file mode 100644 index 00000000..6c8f8068 --- /dev/null +++ b/include/cru/platform/native/Cursor.hpp @@ -0,0 +1,15 @@ +#pragma once +#include "../Resource.hpp" +#include "Base.hpp" + +#include + +namespace cru::platform::native { +struct ICursor : virtual INativeResource {}; + +struct ICursorManager : virtual INativeResource { + virtual std::shared_ptr GetSystemCursor(SystemCursorType type) = 0; + + // TODO: Add method to create cursor. +}; +} // namespace cru::platform::native diff --git a/include/cru/platform/native/Keyboard.hpp b/include/cru/platform/native/Keyboard.hpp new file mode 100644 index 00000000..26a1409d --- /dev/null +++ b/include/cru/platform/native/Keyboard.hpp @@ -0,0 +1,120 @@ +#pragma once +#include "cru/common/Bitmask.hpp" + +namespace cru::platform::native { +// Because of the complexity of keyboard layout, I only add code in US keyboard +// layout, the most widely used layout in China. We should try to make it easy +// to add new keyboard layout. +enum class KeyCode { + Unknown, + LeftButton, + MiddleButton, + RightButton, + Escape, + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + N0, + N1, + N2, + N3, + N4, + N5, + N6, + N7, + N8, + N9, + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + GraveAccent, + Tab, + CapsLock, + LeftShift, + LeftCtrl, + LeftSuper, + LeftAlt, + Minus, + Equal, + Backspace, + LeftSquareBracket, + RightSquareBracket, + BackSlash, + Semicolon, + Quote, + Comma, + Period, + Slash, + RightShift, + RightCtrl, + RightSuper, + RightAlt, + Insert, + Delete, + Home, + End, + PageUp, + PageDown, + Up, + Left, + Down, + Right, + PrintScreen, + ScrollLock, + Pause, + NumPad0, + NumPad1, + NumPad2, + NumPad3, + NumPad4, + NumPad5, + NumPad6, + NumPad7, + NumPad8, + NumPad9 +}; + +namespace details { +struct TagKeyModifier {}; +} // namespace details + +using KeyModifier = Bitmask; + +namespace key_modifiers { +constexpr KeyModifier shift{0b1}; +constexpr KeyModifier ctrl{0b10}; +constexpr KeyModifier alt{0b100}; +} // namespace key_modifiers +} // namespace cru::platform::native diff --git a/include/cru/platform/native/Window.hpp b/include/cru/platform/native/Window.hpp new file mode 100644 index 00000000..1fcac1fc --- /dev/null +++ b/include/cru/platform/native/Window.hpp @@ -0,0 +1,67 @@ +#pragma once +#include "../Resource.hpp" +#include "Base.hpp" +#include "cru/common/Event.hpp" + +#include + +namespace cru::platform::native { +// Represents a native window, which exposes some low-level events and +// operations. +// +// Usually you save an INativeWindowResolver after creating a window. Because +// window may be destroyed when user do certain actions like click the close +// button. Then the INativeWindow instance is destroyed and +// INativeWindowResolver::Resolve return nullptr to indicate the fact. +struct INativeWindow : virtual INativeResource { + virtual std::shared_ptr GetResolver() = 0; + + virtual void Close() = 0; + + virtual INativeWindow* GetParent() = 0; + + virtual bool IsVisible() = 0; + virtual void SetVisible(bool is_visible) = 0; + + virtual Size GetClientSize() = 0; + virtual void SetClientSize(const Size& size) = 0; + + // Get the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + virtual Rect GetWindowRect() = 0; + + // Set the rect of the window containing frame. + // The lefttop of the rect is relative to screen lefttop. + virtual void SetWindowRect(const Rect& rect) = 0; + + // Relative to client lefttop. + virtual Point GetMousePosition() = 0; + + virtual bool CaptureMouse() = 0; + virtual bool ReleaseMouse() = 0; + + virtual void SetCursor(std::shared_ptr cursor) = 0; + + virtual void RequestRepaint() = 0; + + // Remember to call EndDraw on return value and destroy it. + virtual std::unique_ptr BeginPaint() = 0; + + virtual IEvent* DestroyEvent() = 0; + virtual IEvent* PaintEvent() = 0; + virtual IEvent* ResizeEvent() = 0; + virtual IEvent* FocusEvent() = 0; + virtual IEvent* MouseEnterLeaveEvent() = 0; + virtual IEvent* MouseMoveEvent() = 0; + virtual IEvent* MouseDownEvent() = 0; + virtual IEvent* MouseUpEvent() = 0; + virtual IEvent* KeyDownEvent() = 0; + virtual IEvent* KeyUpEvent() = 0; +}; + +// See INativeWindow for more info. +struct INativeWindowResolver : virtual INativeResource { + // Think twice before you save the return value. + virtual INativeWindow* Resolve() = 0; +}; +} // namespace cru::platform::native diff --git a/include/cru/platform/native/base.hpp b/include/cru/platform/native/base.hpp deleted file mode 100644 index bba7b960..00000000 --- a/include/cru/platform/native/base.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once -#include "cru/common/Base.hpp" -#include "cru/common/Bitmask.hpp" -#include "cru/platform/graph/Base.hpp" -#include "Keyboard.hpp" - -namespace cru::platform::native { -struct ICursor; -struct ICursorManager; -struct IUiApplication; -struct INativeWindow; -struct INativeWindowResolver; -struct IInputMethodManager; -struct IInputMethodContext; - -struct Dpi { - float x; - float y; -}; - -namespace details { -struct TagMouseButton {}; -} // namespace details - -using MouseButton = Bitmask; - -namespace mouse_buttons { -constexpr MouseButton left{0b1}; -constexpr MouseButton middle{0b10}; -constexpr MouseButton right{0b100}; -} // namespace mouse_buttons - -enum class SystemCursorType { - Arrow, - Hand, -}; - -struct NativeMouseButtonEventArgs { - MouseButton button; - Point point; - KeyModifier modifier; -}; - -struct NativeKeyEventArgs { - KeyCode key; - KeyModifier modifier; -}; - -enum class FocusChangeType { Gain, Lost }; - -enum class MouseEnterLeaveType { Enter, Leave }; -} // namespace cru::platform::native diff --git a/include/cru/platform/native/cursor.hpp b/include/cru/platform/native/cursor.hpp deleted file mode 100644 index 6c8f8068..00000000 --- a/include/cru/platform/native/cursor.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "../Resource.hpp" -#include "Base.hpp" - -#include - -namespace cru::platform::native { -struct ICursor : virtual INativeResource {}; - -struct ICursorManager : virtual INativeResource { - virtual std::shared_ptr GetSystemCursor(SystemCursorType type) = 0; - - // TODO: Add method to create cursor. -}; -} // namespace cru::platform::native diff --git a/include/cru/platform/native/keyboard.hpp b/include/cru/platform/native/keyboard.hpp deleted file mode 100644 index 26a1409d..00000000 --- a/include/cru/platform/native/keyboard.hpp +++ /dev/null @@ -1,120 +0,0 @@ -#pragma once -#include "cru/common/Bitmask.hpp" - -namespace cru::platform::native { -// Because of the complexity of keyboard layout, I only add code in US keyboard -// layout, the most widely used layout in China. We should try to make it easy -// to add new keyboard layout. -enum class KeyCode { - Unknown, - LeftButton, - MiddleButton, - RightButton, - Escape, - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - N0, - N1, - N2, - N3, - N4, - N5, - N6, - N7, - N8, - N9, - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, - GraveAccent, - Tab, - CapsLock, - LeftShift, - LeftCtrl, - LeftSuper, - LeftAlt, - Minus, - Equal, - Backspace, - LeftSquareBracket, - RightSquareBracket, - BackSlash, - Semicolon, - Quote, - Comma, - Period, - Slash, - RightShift, - RightCtrl, - RightSuper, - RightAlt, - Insert, - Delete, - Home, - End, - PageUp, - PageDown, - Up, - Left, - Down, - Right, - PrintScreen, - ScrollLock, - Pause, - NumPad0, - NumPad1, - NumPad2, - NumPad3, - NumPad4, - NumPad5, - NumPad6, - NumPad7, - NumPad8, - NumPad9 -}; - -namespace details { -struct TagKeyModifier {}; -} // namespace details - -using KeyModifier = Bitmask; - -namespace key_modifiers { -constexpr KeyModifier shift{0b1}; -constexpr KeyModifier ctrl{0b10}; -constexpr KeyModifier alt{0b100}; -} // namespace key_modifiers -} // namespace cru::platform::native diff --git a/include/cru/platform/native/window.hpp b/include/cru/platform/native/window.hpp deleted file mode 100644 index 1fcac1fc..00000000 --- a/include/cru/platform/native/window.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#pragma once -#include "../Resource.hpp" -#include "Base.hpp" -#include "cru/common/Event.hpp" - -#include - -namespace cru::platform::native { -// Represents a native window, which exposes some low-level events and -// operations. -// -// Usually you save an INativeWindowResolver after creating a window. Because -// window may be destroyed when user do certain actions like click the close -// button. Then the INativeWindow instance is destroyed and -// INativeWindowResolver::Resolve return nullptr to indicate the fact. -struct INativeWindow : virtual INativeResource { - virtual std::shared_ptr GetResolver() = 0; - - virtual void Close() = 0; - - virtual INativeWindow* GetParent() = 0; - - virtual bool IsVisible() = 0; - virtual void SetVisible(bool is_visible) = 0; - - virtual Size GetClientSize() = 0; - virtual void SetClientSize(const Size& size) = 0; - - // Get the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - virtual Rect GetWindowRect() = 0; - - // Set the rect of the window containing frame. - // The lefttop of the rect is relative to screen lefttop. - virtual void SetWindowRect(const Rect& rect) = 0; - - // Relative to client lefttop. - virtual Point GetMousePosition() = 0; - - virtual bool CaptureMouse() = 0; - virtual bool ReleaseMouse() = 0; - - virtual void SetCursor(std::shared_ptr cursor) = 0; - - virtual void RequestRepaint() = 0; - - // Remember to call EndDraw on return value and destroy it. - virtual std::unique_ptr BeginPaint() = 0; - - virtual IEvent* DestroyEvent() = 0; - virtual IEvent* PaintEvent() = 0; - virtual IEvent* ResizeEvent() = 0; - virtual IEvent* FocusEvent() = 0; - virtual IEvent* MouseEnterLeaveEvent() = 0; - virtual IEvent* MouseMoveEvent() = 0; - virtual IEvent* MouseDownEvent() = 0; - virtual IEvent* MouseUpEvent() = 0; - virtual IEvent* KeyDownEvent() = 0; - virtual IEvent* KeyUpEvent() = 0; -}; - -// See INativeWindow for more info. -struct INativeWindowResolver : virtual INativeResource { - // Think twice before you save the return value. - virtual INativeWindow* Resolve() = 0; -}; -} // namespace cru::platform::native diff --git a/include/cru/platform/resource.hpp b/include/cru/platform/resource.hpp deleted file mode 100644 index 72cfaf52..00000000 --- a/include/cru/platform/resource.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once -#include "cru/common/Base.hpp" - -#include - -namespace cru::platform { -struct INativeResource : virtual Interface { - virtual std::string_view GetPlatformId() const = 0; -}; -} // namespace cru::platform -- cgit v1.2.3