diff options
Diffstat (limited to 'include/cru/platform')
-rw-r--r-- | include/cru/platform/basic_types.hpp | 1 | ||||
-rw-r--r-- | include/cru/platform/brush.hpp | 13 | ||||
-rw-r--r-- | include/cru/platform/debug.hpp | 2 | ||||
-rw-r--r-- | include/cru/platform/dpi_util.hpp | 32 | ||||
-rw-r--r-- | include/cru/platform/geometry.hpp | 26 | ||||
-rw-r--r-- | include/cru/platform/graph_factory.hpp | 14 | ||||
-rw-r--r-- | include/cru/platform/native_window.hpp | 2 | ||||
-rw-r--r-- | include/cru/platform/painter.hpp | 15 | ||||
-rw-r--r-- | include/cru/platform/ui_applicaition.hpp | 2 | ||||
-rw-r--r-- | include/cru/platform/win/d2d_util.hpp | 91 | ||||
-rw-r--r-- | include/cru/platform/win/graph_manager.hpp | 44 | ||||
-rw-r--r-- | include/cru/platform/win/win_application.hpp | 11 | ||||
-rw-r--r-- | include/cru/platform/win/win_brush.hpp | 23 | ||||
-rw-r--r-- | include/cru/platform/win/win_geometry.hpp | 45 | ||||
-rw-r--r-- | include/cru/platform/win/win_graph_factory.hpp | 24 | ||||
-rw-r--r-- | include/cru/platform/win/win_native_window.hpp | 7 | ||||
-rw-r--r-- | include/cru/platform/win/win_pre_config.hpp | 6 | ||||
-rw-r--r-- | include/cru/platform/win/window_render_target.hpp | 44 |
18 files changed, 363 insertions, 39 deletions
diff --git a/include/cru/platform/basic_types.hpp b/include/cru/platform/basic_types.hpp index 67637b83..81ee3e34 100644 --- a/include/cru/platform/basic_types.hpp +++ b/include/cru/platform/basic_types.hpp @@ -7,5 +7,4 @@ struct Dpi { }; enum class MouseButton { Left, Right, Middle }; - } // namespace cru::platform diff --git a/include/cru/platform/brush.hpp b/include/cru/platform/brush.hpp new file mode 100644 index 00000000..713d302d --- /dev/null +++ b/include/cru/platform/brush.hpp @@ -0,0 +1,13 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct Brush : public virtual Interface {}; + +struct SolidColorBrush : public virtual Brush { + virtual ui::Color GetColor() = 0; + virtual void SetColor(const ui::Color& color) = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/debug.hpp b/include/cru/platform/debug.hpp index 24759ee1..21c2b646 100644 --- a/include/cru/platform/debug.hpp +++ b/include/cru/platform/debug.hpp @@ -4,5 +4,5 @@ #include <string_view> namespace cru::platform::debug { -void DebugMessage(const std::string_view& message); +void DebugMessage(const std::wstring_view& message); } diff --git a/include/cru/platform/dpi_util.hpp b/include/cru/platform/dpi_util.hpp deleted file mode 100644 index 3c0ae6ca..00000000 --- a/include/cru/platform/dpi_util.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "ui_applicaition.hpp" - -namespace cru::platform { -inline Dpi GetDpi() { - return UiApplication::GetInstance()->GetDpi(); -} - -inline int DipToPixelInternal(const float dip, const float dpi) { - return static_cast<int>(dip * dpi / 96.0f); -} - -inline int DipToPixelX(const float dip_x) { - return DipToPixelInternal(dip_x, GetDpi().x); -} - -inline int DipToPixelY(const float dip_y) { - return DipToPixelInternal(dip_y, GetDpi().y); -} - -inline float DipToPixelInternal(const int pixel, const float dpi) { - return static_cast<float>(pixel) * 96.0f / dpi; -} - -inline float PixelToDipX(const int pixel_x) { - return DipToPixelInternal(pixel_x, GetDpi().x); -} - -inline float PixelToDipY(const int pixel_y) { - return DipToPixelInternal(pixel_y, GetDpi().y); -} -} // namespace cru::platform diff --git a/include/cru/platform/geometry.hpp b/include/cru/platform/geometry.hpp new file mode 100644 index 00000000..aa93fe37 --- /dev/null +++ b/include/cru/platform/geometry.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "basic_types.hpp" +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct Geometry : virtual Interface { + virtual bool FillContains(const ui::Point& point) = 0; +}; + +struct GeometryBuilder : virtual Interface { + virtual bool IsValid() = 0; + 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; + virtual void CloseFigure(bool close) = 0; + virtual Geometry* Build() = 0; + + Geometry* BuildAndDeleteThis() { + Build(); + delete this; + } +}; +} // namespace cru::platform diff --git a/include/cru/platform/graph_factory.hpp b/include/cru/platform/graph_factory.hpp new file mode 100644 index 00000000..f2e5f286 --- /dev/null +++ b/include/cru/platform/graph_factory.hpp @@ -0,0 +1,14 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct SolidColorBrush; +struct GeometryBuilder; + +struct GraphFactory : virtual Interface { + virtual SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; + virtual GeometryBuilder* CreateGeometryBuilder() = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/native_window.hpp b/include/cru/platform/native_window.hpp index 5e8897ab..3a8e27ad 100644 --- a/include/cru/platform/native_window.hpp +++ b/include/cru/platform/native_window.hpp @@ -32,7 +32,7 @@ struct NativeWindow : public virtual Interface { // The lefttop of the rect is relative to screen lefttop. virtual void SetWindowRect(const ui::Rect& rect) = 0; - virtual Painter* GetPainter() = 0; + virtual Painter* BeginDraw() = 0; virtual Event<>* DestroyEvent() = 0; virtual Event<ui::Size>* ResizeEvent() = 0; diff --git a/include/cru/platform/painter.hpp b/include/cru/platform/painter.hpp new file mode 100644 index 00000000..4ad09247 --- /dev/null +++ b/include/cru/platform/painter.hpp @@ -0,0 +1,15 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct Brush; +struct Geometry; + +struct Painter : virtual Interface { + virtual void StrokeGeometry(Geometry* geometry, Brush* brush, float width) = 0; + virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; + virtual void EndDraw() = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/ui_applicaition.hpp b/include/cru/platform/ui_applicaition.hpp index e149166b..1f30ce17 100644 --- a/include/cru/platform/ui_applicaition.hpp +++ b/include/cru/platform/ui_applicaition.hpp @@ -27,8 +27,6 @@ struct UiApplication : public virtual Interface { virtual std::vector<NativeWindow*> GetAllWindow() = 0; virtual NativeWindow* CreateWindow() = 0; - virtual Dpi GetDpi() = 0; - virtual GraphFactory* GetGraphFactory() = 0; }; } // namespace cru::platform diff --git a/include/cru/platform/win/d2d_util.hpp b/include/cru/platform/win/d2d_util.hpp new file mode 100644 index 00000000..9ff6556a --- /dev/null +++ b/include/cru/platform/win/d2d_util.hpp @@ -0,0 +1,91 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform::win::util { +inline D2D1_COLOR_F Convert(const ui::Color& color) { + return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, + color.blue / 255.0f, color.alpha / 255.0f); +} + +inline D2D1_POINT_2F Convert(const ui::Point& point) { + return D2D1::Point2F(point.x, point.y); +} + +inline D2D1_RECT_F Convert(const ui::Rect& rect) { + return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, + rect.top + rect.height); +} + +inline D2D1_ROUNDED_RECT Convert(const ui::RoundedRect& rounded_rect) { + return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, + rounded_rect.radius_y); +} + +inline D2D1_ELLIPSE Convert(const ui::Ellipse& ellipse) { + return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, + ellipse.radius_y); +} + +inline ui::Color Convert(const D2D1_COLOR_F& color) { + auto floor = [](float n) { return static_cast<std::uint8_t>(n + 0.5f); }; + return ui::Color{floor(color.r * 255.0f), floor(color.g * 255.0f), + floor(color.b * 255.0f), floor(color.a * 255.0f)}; +} + +inline ui::Point Convert(const D2D1_POINT_2F& point) { + return ui::Point(point.x, point.y); +} + +inline ui::Rect Convert(const D2D1_RECT_F& rect) { + return ui::Rect(rect.left, rect.top, rect.right - rect.left, + rect.bottom - rect.top); +} + +inline ui::RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { + return ui::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, + rounded_rect.radiusY); +} + +inline ui::Ellipse Convert(const D2D1_ELLIPSE& ellipse) { + return ui::Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); +} + +inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return left.x == right.x && left.y == right.y; +} + +inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return left.rect == right.rect && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return left.point == right.point && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return !(left == right); +} +} // namespace cru::platform::win::util diff --git a/include/cru/platform/win/graph_manager.hpp b/include/cru/platform/win/graph_manager.hpp new file mode 100644 index 00000000..f2e29c22 --- /dev/null +++ b/include/cru/platform/win/graph_manager.hpp @@ -0,0 +1,44 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../basic_types.hpp" +#include "cru/common/base.hpp" + +namespace cru::platform::win { +class WindowRenderTarget; + +class GraphManager final : public Object { + public: + GraphManager(); + GraphManager(const GraphManager& other) = delete; + GraphManager(GraphManager&& other) = delete; + GraphManager& operator=(const GraphManager& other) = delete; + GraphManager& operator=(GraphManager&& other) = delete; + ~GraphManager() override = default; + + public: + ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); } + + ID2D1DeviceContext* GetD2D1DeviceContext() const { + return d2d1_device_context_.Get(); + } + + ID3D11Device* GetD3D11Device() const { return d3d11_device_.Get(); } + + IDXGIFactory2* GetDxgiFactory() const { return dxgi_factory_.Get(); } + + IDWriteFactory* GetDWriteFactory() const { return dwrite_factory_.Get(); } + + IDWriteFontCollection* GetSystemFontCollection() const { + return dwrite_system_font_collection_.Get(); + } + + private: + Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_; + Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_; + Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_; + Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory_; + Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_; + Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_application.hpp b/include/cru/platform/win/win_application.hpp index fcc0a7c9..7defa703 100644 --- a/include/cru/platform/win/win_application.hpp +++ b/include/cru/platform/win/win_application.hpp @@ -10,6 +10,8 @@ namespace cru::platform::win { class GodWindow; class TimerManager; class WindowManager; +class GraphManager; +class WinGraphFactory; class WinApplication : public Object, public virtual UiApplication { public: @@ -39,13 +41,16 @@ class WinApplication : public Object, public virtual UiApplication { const std::function<void()>& action) override; void CancelTimer(unsigned long id) override; + GraphFactory* GetGraphFactory() override; + + HINSTANCE GetInstanceHandle() const { return h_instance_; } GodWindow* GetGodWindow() const { return god_window_.get(); } - TimerManager* GetTimerManager() const { return timer_manager_.get(); } - WindowManager* GetWindowManager() const { return window_manager_.get(); } + GraphManager* GetGraphManager() const { return graph_manager_.get(); } + WinGraphFactory* GetWinGraphFactory() const { return graph_factory_.get(); } private: HINSTANCE h_instance_; @@ -53,5 +58,7 @@ class WinApplication : public Object, public virtual UiApplication { std::shared_ptr<GodWindow> god_window_; std::shared_ptr<TimerManager> timer_manager_; std::shared_ptr<WindowManager> window_manager_; + std::shared_ptr<GraphManager> graph_manager_; + std::shared_ptr<WinGraphFactory> graph_factory_; }; } // namespace cru::platform::win diff --git a/include/cru/platform/win/win_brush.hpp b/include/cru/platform/win/win_brush.hpp new file mode 100644 index 00000000..2668215d --- /dev/null +++ b/include/cru/platform/win/win_brush.hpp @@ -0,0 +1,23 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../brush.hpp" + +namespace cru::platform::win { +class WinSolidColorBrush : public Object, public virtual SolidColorBrush { + public: + explicit WinSolidColorBrush( + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush); + WinSolidColorBrush(const WinSolidColorBrush& other) = delete; + WinSolidColorBrush(WinSolidColorBrush&& other) = delete; + WinSolidColorBrush& operator=(const WinSolidColorBrush& other) = delete; + WinSolidColorBrush& operator=(WinSolidColorBrush&& other) = delete; + ~WinSolidColorBrush() override = default; + + ui::Color GetColor() override; + void SetColor(const ui::Color& color) override; + + private: + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_geometry.hpp b/include/cru/platform/win/win_geometry.hpp new file mode 100644 index 00000000..e8ab7796 --- /dev/null +++ b/include/cru/platform/win/win_geometry.hpp @@ -0,0 +1,45 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../geometry.hpp" + +namespace cru::platform::win { +class WinGeometryBuilder : public Object, public virtual GeometryBuilder { + public: + explicit WinGeometryBuilder(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); + WinGeometryBuilder(const WinGeometryBuilder& other) = delete; + WinGeometryBuilder(WinGeometryBuilder&& other) = delete; + WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete; + WinGeometryBuilder& operator=(WinGeometryBuilder&& other) = delete; + ~WinGeometryBuilder() override; + + bool IsValid() override { return geometry_ != nullptr; } + void BeginFigure(const ui::Point& point) override; + void LineTo(const ui::Point& point) override; + void QuadraticBezierTo(const ui::Point& control_point, + const ui::Point& end_point) override; + void CloseFigure(bool close) override; + Geometry* Build() override; + + private: + Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; + Microsoft::WRL::ComPtr<ID2D1GeometrySink> geometry_sink_; +}; + +class WinGeometry : public Object, public virtual Geometry { + public: + explicit WinGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); + WinGeometry(const WinGeometry& other) = delete; + WinGeometry(WinGeometry&& other) = delete; + WinGeometry& operator=(const WinGeometry& other) = delete; + WinGeometry& operator=(WinGeometry&& other) = delete; + ~WinGeometry() override = default; + + bool FillContains(const ui::Point& point) override; + + ID2D1PathGeometry* GetNative() const { return geometry_.Get(); } + + private: + Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_graph_factory.hpp b/include/cru/platform/win/win_graph_factory.hpp new file mode 100644 index 00000000..b49413e4 --- /dev/null +++ b/include/cru/platform/win/win_graph_factory.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../graph_factory.hpp" + +namespace cru::platform::win { +class GraphManager; + +class WinGraphFactory : public Object, public virtual GraphFactory { + public: + explicit WinGraphFactory(GraphManager* graph_manager); + WinGraphFactory(const WinGraphFactory& other) = delete; + WinGraphFactory(WinGraphFactory&& other) = delete; + WinGraphFactory& operator=(const WinGraphFactory& other) = delete; + WinGraphFactory& operator=(WinGraphFactory&& other) = delete; + ~WinGraphFactory() override = default; + + SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) override; + GeometryBuilder* CreateGeometryBuilder() override; + + private: + GraphManager* graph_manager_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_native_window.hpp b/include/cru/platform/win/win_native_window.hpp index 9deac767..8b787485 100644 --- a/include/cru/platform/win/win_native_window.hpp +++ b/include/cru/platform/win/win_native_window.hpp @@ -10,6 +10,7 @@ namespace cru::platform::win { class WinApplication; class WindowClass; class WindowManager; +class WindowRenderTarget; class WinNativeWindow : public Object, public virtual NativeWindow { public: @@ -70,6 +71,10 @@ class WinNativeWindow : public Object, public virtual NativeWindow { bool HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param, LRESULT* result); + WindowRenderTarget* GetWindowRenderTarget() const { + return window_render_target_.get(); + } + private: // Get the client rect in pixel. RECT GetClientRectPixel(); @@ -107,6 +112,8 @@ class WinNativeWindow : public Object, public virtual NativeWindow { bool has_focus_ = false; bool is_mouse_in_ = false; + std::shared_ptr<WindowRenderTarget> window_render_target_; + Event<> destroy_event_; Event<ui::Size> resize_event_; Event<> paint_event_; diff --git a/include/cru/platform/win/win_pre_config.hpp b/include/cru/platform/win/win_pre_config.hpp index 2e8bb80e..d6ba4ec7 100644 --- a/include/cru/platform/win/win_pre_config.hpp +++ b/include/cru/platform/win/win_pre_config.hpp @@ -4,3 +4,9 @@ #define WIN32_LEAN_AND_MEAN #include <Windows.h> #undef CreateWindow + +#include <d2d1_2.h> +#include <d3d11.h> +#include <dwrite.h> +#include <dxgi1_2.h> +#include <wrl/client.h> diff --git a/include/cru/platform/win/window_render_target.hpp b/include/cru/platform/win/window_render_target.hpp new file mode 100644 index 00000000..f328a84a --- /dev/null +++ b/include/cru/platform/win/window_render_target.hpp @@ -0,0 +1,44 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "cru/common/base.hpp" + + +namespace cru::platform::win { +class GraphManager; + +// Represents a window render target. +class WindowRenderTarget : public Object { + public: + WindowRenderTarget(GraphManager* graph_manager, HWND hwnd); + WindowRenderTarget(const WindowRenderTarget& other) = delete; + WindowRenderTarget(WindowRenderTarget&& other) = delete; + WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; + WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete; + ~WindowRenderTarget() override = default; + + public: + // Get the graph manager that created the render target. + GraphManager* GetGraphManager() const { return graph_manager_; } + + // Get the target bitmap which can be set as the ID2D1DeviceContext's target. + ID2D1Bitmap1* GetTargetBitmap() const { return target_bitmap_.Get(); } + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Set this render target as the d2d device context's target. + void SetAsTarget(); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + GraphManager* graph_manager_; + Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_; + Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_; +}; +} // namespace cru::graph |