aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-03-24 19:06:17 +0800
committercrupest <crupest@outlook.com>2019-03-24 19:06:17 +0800
commit79d1d76509dbf6cf9c79f8eb55968535982975aa (patch)
tree322ed87621506f2e56f4887c73370db769843a29
parente8be3841457853daefc26d0ca00256ad8c44f593 (diff)
downloadcru-79d1d76509dbf6cf9c79f8eb55968535982975aa.tar.gz
cru-79d1d76509dbf6cf9c79f8eb55968535982975aa.tar.bz2
cru-79d1d76509dbf6cf9c79f8eb55968535982975aa.zip
...
-rw-r--r--CruUI.vcxproj8
-rw-r--r--CruUI.vcxproj.filters24
-rw-r--r--src/graph/graph.cpp161
-rw-r--r--src/graph/graph.hpp164
-rw-r--r--src/graph/graph_manager.cpp79
-rw-r--r--src/graph/graph_manager.hpp60
-rw-r--r--src/graph/graph_util.hpp63
-rw-r--r--src/graph/window_render_target.cpp99
-rw-r--r--src/graph/window_render_target.hpp49
-rw-r--r--src/ui/controls/button.cpp13
-rw-r--r--src/ui/controls/button.hpp31
-rw-r--r--src/ui/controls/text_block.cpp2
-rw-r--r--src/ui/render/border_render_object.cpp52
-rw-r--r--src/ui/render/border_render_object.hpp22
-rw-r--r--src/ui/render/flex_layout_render_object.cpp2
-rw-r--r--src/ui/render/render_object.cpp3
-rw-r--r--src/ui/render/render_object.hpp2
-rw-r--r--src/ui/render/text_render_object.cpp67
-rw-r--r--src/ui/render/text_render_object.hpp39
-rw-r--r--src/ui/render/window_render_object.cpp2
-rw-r--r--src/ui/ui_manager.cpp57
-rw-r--r--src/ui/ui_manager.hpp15
-rw-r--r--src/ui/window.cpp22
-rw-r--r--src/ui/window.hpp3
-rw-r--r--src/util/com_util.hpp22
25 files changed, 602 insertions, 459 deletions
diff --git a/CruUI.vcxproj b/CruUI.vcxproj
index 4143bb43..24b6957e 100644
--- a/CruUI.vcxproj
+++ b/CruUI.vcxproj
@@ -125,6 +125,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
+ <ClCompile Include="src\graph\window_render_target.cpp" />
<ClCompile Include="src\ui\content_control.cpp" />
<ClCompile Include="src\ui\controls\button.cpp" />
<ClCompile Include="src\ui\events\window_event.cpp" />
@@ -137,13 +138,15 @@
<ClCompile Include="src\cru_debug.cpp" />
<ClCompile Include="src\application.cpp" />
<ClCompile Include="src\exception.cpp" />
- <ClCompile Include="src\graph\graph.cpp" />
+ <ClCompile Include="src\graph\graph_manager.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\timer.cpp" />
<ClCompile Include="src\ui\animations\animation.cpp" />
<ClCompile Include="src\ui\control.cpp" />
<ClCompile Include="src\ui\controls\flex_layout.cpp" />
<ClCompile Include="src\util\string_util.cpp" />
+ <ClInclude Include="src\graph\graph_util.hpp" />
+ <ClInclude Include="src\graph\window_render_target.hpp" />
<ClInclude Include="src\ui\content_control.hpp" />
<ClInclude Include="src\ui\controls\button.hpp" />
<ClInclude Include="src\ui\events\window_event.hpp" />
@@ -153,6 +156,7 @@
<ClInclude Include="src\ui\render\window_render_object.hpp" />
<ClInclude Include="src\ui\render\text_render_object.hpp" />
<ClInclude Include="src\ui\window_class.hpp" />
+ <ClInclude Include="src\util\com_util.hpp" />
<ClInclude Include="src\util\format.hpp" />
<ClInclude Include="src\util\math_util.hpp" />
<ClInclude Include="src\ui\controls\text_block.hpp" />
@@ -171,7 +175,7 @@
<ClInclude Include="src\cru_debug.hpp" />
<ClInclude Include="src\exception.hpp" />
<ClInclude Include="src\pre.hpp" />
- <ClInclude Include="src\graph\graph.hpp" />
+ <ClInclude Include="src\graph\graph_manager.hpp" />
<ClInclude Include="src\timer.hpp" />
<ClInclude Include="src\ui\animations\animation.hpp" />
<ClInclude Include="src\ui\control.hpp" />
diff --git a/CruUI.vcxproj.filters b/CruUI.vcxproj.filters
index 01575ce2..68dc97ba 100644
--- a/CruUI.vcxproj.filters
+++ b/CruUI.vcxproj.filters
@@ -27,9 +27,6 @@
<ClCompile Include="src\timer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
- <ClCompile Include="src\graph\graph.cpp">
- <Filter>Source Files</Filter>
- </ClCompile>
<ClCompile Include="src\ui\control.cpp">
<Filter>Source Files</Filter>
</ClCompile>
@@ -93,11 +90,14 @@
<ClCompile Include="src\ui\events\window_event.cpp">
<Filter>Source Files</Filter>
</ClCompile>
+ <ClCompile Include="src\graph\window_render_target.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="src\graph\graph_manager.cpp">
+ <Filter>Source Files</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
- <ClInclude Include="src\graph\graph.hpp">
- <Filter>Header Files</Filter>
- </ClInclude>
<ClInclude Include="src\ui\animations\animation.hpp">
<Filter>Header Files</Filter>
</ClInclude>
@@ -191,6 +191,18 @@
<ClInclude Include="src\ui\events\window_event.hpp">
<Filter>Header Files</Filter>
</ClInclude>
+ <ClInclude Include="src\graph\graph_util.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="src\graph\window_render_target.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="src\graph\graph_manager.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="src\util\com_util.hpp">
+ <Filter>Header Files</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\application.cpp">
diff --git a/src/graph/graph.cpp b/src/graph/graph.cpp
deleted file mode 100644
index ed3fe5d5..00000000
--- a/src/graph/graph.cpp
+++ /dev/null
@@ -1,161 +0,0 @@
-#include "graph.hpp"
-
-#include "application.hpp"
-#include "exception.hpp"
-
-namespace cru::graph {
-using Microsoft::WRL::ComPtr;
-
-WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) {
- this->graph_manager_ = graph_manager;
-
- const auto d3d11_device = graph_manager->GetD3D11Device();
- const auto dxgi_factory = graph_manager->GetDxgiFactory();
-
- // Allocate a descriptor.
- DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0};
- swap_chain_desc.Width = 0; // use automatic sizing
- swap_chain_desc.Height = 0;
- swap_chain_desc.Format =
- DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format
- swap_chain_desc.Stereo = false;
- swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling
- swap_chain_desc.SampleDesc.Quality = 0;
- swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swap_chain_desc.BufferCount = 2; // use double buffering to enable flip
- swap_chain_desc.Scaling = DXGI_SCALING_NONE;
- swap_chain_desc.SwapEffect =
- DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect
- swap_chain_desc.Flags = 0;
-
- // Get the final swap chain for this window from the DXGI factory.
- ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd(
- d3d11_device.Get(), hwnd, &swap_chain_desc, nullptr, nullptr,
- &dxgi_swap_chain_));
-
- CreateTargetBitmap();
-}
-
-WindowRenderTarget::~WindowRenderTarget() {}
-
-void WindowRenderTarget::ResizeBuffer(const int width, const int height) {
- const auto graph_manager = graph_manager_;
- const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext();
-
- ComPtr<ID2D1Image> old_target;
- d2d1_device_context->GetTarget(&old_target);
- const auto target_this = old_target == this->target_bitmap_;
- if (target_this) d2d1_device_context->SetTarget(nullptr);
-
- old_target = nullptr;
- target_bitmap_ = nullptr;
-
- ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height,
- DXGI_FORMAT_UNKNOWN, 0));
-
- CreateTargetBitmap();
-
- if (target_this) d2d1_device_context->SetTarget(target_bitmap_.Get());
-}
-
-void WindowRenderTarget::SetAsTarget() {
- GetD2DDeviceContext()->SetTarget(target_bitmap_.Get());
-}
-
-void WindowRenderTarget::Present() {
- ThrowIfFailed(dxgi_swap_chain_->Present(1, 0));
-}
-
-void WindowRenderTarget::CreateTargetBitmap() {
- // Direct2D needs the dxgi version of the backbuffer surface pointer.
- ComPtr<IDXGISurface> dxgiBackBuffer;
- ThrowIfFailed(dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer)));
-
- const auto dpi = graph_manager_->GetDpi();
-
- auto bitmap_properties = D2D1::BitmapProperties1(
- D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
- D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
- dpi.x, dpi.y);
-
- // Get a D2D surface from the DXGI back buffer to use as the D2D render
- // target.
- ThrowIfFailed(
- graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface(
- dxgiBackBuffer.Get(), &bitmap_properties, &target_bitmap_));
-}
-
-GraphManager* GraphManager::GetInstance() {
- return Application::GetInstance()->ResolveSingleton<GraphManager>(
- [](auto) { return new GraphManager{}; });
-}
-
-GraphManager::GraphManager() {
- UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
-
-#ifdef CRU_DEBUG
- creation_flags |= D3D11_CREATE_DEVICE_DEBUG;
-#endif
-
- const D3D_FEATURE_LEVEL feature_levels[] = {
- D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1,
- D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2,
- D3D_FEATURE_LEVEL_9_1};
-
- ThrowIfFailed(D3D11CreateDevice(
- nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags,
- feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION,
- &d3d11_device_, nullptr, &d3d11_device_context_));
-
- Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device;
-
- ThrowIfFailed(d3d11_device_.As(&dxgi_device));
-
- ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
- __uuidof(ID2D1Factory1), &d2d1_factory_));
-
- ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device_));
-
- ThrowIfFailed(d2d1_device_->CreateDeviceContext(
- D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_));
-
- // Identify the physical adapter (GPU or card) this device is runs on.
- ComPtr<IDXGIAdapter> dxgi_adapter;
- ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter));
-
- // Get the factory object that created the DXGI device.
- ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_)));
-
- ThrowIfFailed(DWriteCreateFactory(
- DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
- reinterpret_cast<IUnknown**>(dwrite_factory_.GetAddressOf())));
-
- dwrite_factory_->GetSystemFontCollection(&dwrite_system_font_collection_);
-}
-
-GraphManager::~GraphManager() {}
-
-std::shared_ptr<WindowRenderTarget> GraphManager::CreateWindowRenderTarget(
- HWND hwnd) {
- return std::make_shared<WindowRenderTarget>(this, hwnd);
-}
-
-Dpi GraphManager::GetDpi() const {
- Dpi dpi;
- d2d1_factory_->GetDesktopDpi(&dpi.x, &dpi.y);
- return dpi;
-}
-
-void GraphManager::ReloadSystemMetrics() {
- ThrowIfFailed(d2d1_factory_->ReloadSystemMetrics());
-}
-
-Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(
- const D2D1_COLOR_F& color) {
- Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush;
- ThrowIfFailed(GraphManager::GetInstance()
- ->GetD2D1DeviceContext()
- ->CreateSolidColorBrush(color, &brush));
- return brush;
-}
-} // namespace cru::graph
diff --git a/src/graph/graph.hpp b/src/graph/graph.hpp
deleted file mode 100644
index af14cc50..00000000
--- a/src/graph/graph.hpp
+++ /dev/null
@@ -1,164 +0,0 @@
-#pragma once
-#include "pre.hpp"
-
-#include <d2d1_2.h>
-#include <dxgi1_2.h>
-#include <d3d11.h>
-#include <dwrite.h>
-#include <wrl/client.h>
-#include <functional>
-#include <memory>
-
-#include "base.hpp"
-
-namespace cru::graph {
-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;
-
- public:
- // Get the graph manager that created the render target.
- GraphManager* GetGraphManager() const { return graph_manager_; }
-
- // Get the d2d device context.
- inline Microsoft::WRL::ComPtr<ID2D1DeviceContext> GetD2DDeviceContext() const;
-
- // Get the target bitmap which can be set as the ID2D1DeviceContext's target.
- Microsoft::WRL::ComPtr<ID2D1Bitmap1> GetTargetBitmap() const {
- return target_bitmap_;
- }
-
- // 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_;
-};
-
-struct Dpi {
- float x;
- float y;
-};
-
-class GraphManager final : public Object {
- public:
- static GraphManager* GetInstance();
-
- private:
- GraphManager();
-
- public:
- GraphManager(const GraphManager& other) = delete;
- GraphManager(GraphManager&& other) = delete;
- GraphManager& operator=(const GraphManager& other) = delete;
- GraphManager& operator=(GraphManager&& other) = delete;
- ~GraphManager() override;
-
- public:
- Microsoft::WRL::ComPtr<ID2D1Factory1> GetD2D1Factory() const {
- return d2d1_factory_;
- }
-
- Microsoft::WRL::ComPtr<ID2D1DeviceContext> GetD2D1DeviceContext() const {
- return d2d1_device_context_;
- }
-
- Microsoft::WRL::ComPtr<ID3D11Device> GetD3D11Device() const {
- return d3d11_device_;
- }
-
- Microsoft::WRL::ComPtr<IDXGIFactory2> GetDxgiFactory() const {
- return dxgi_factory_;
- }
-
- Microsoft::WRL::ComPtr<IDWriteFactory> GetDWriteFactory() const {
- return dwrite_factory_;
- }
-
- // Create a window render target with the HWND.
- std::shared_ptr<WindowRenderTarget> CreateWindowRenderTarget(HWND hwnd);
-
- // Get the desktop dpi.
- Dpi GetDpi() const;
-
- // Reload system metrics including desktop dpi.
- void ReloadSystemMetrics();
-
- Microsoft::WRL::ComPtr<IDWriteFontCollection> GetSystemFontCollection()
- const {
- return dwrite_system_font_collection_;
- }
-
- private:
- Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
- Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context_;
- Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_;
- Microsoft::WRL::ComPtr<ID2D1Device> d2d1_device_;
- 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_;
-};
-
-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, GraphManager::GetInstance()->GetDpi().x);
-}
-
-inline int DipToPixelY(const float dip_y) {
- return DipToPixelInternal(dip_y, GraphManager::GetInstance()->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, GraphManager::GetInstance()->GetDpi().x);
-}
-
-inline float PixelToDipY(const int pixel_y) {
- return DipToPixelInternal(pixel_y, GraphManager::GetInstance()->GetDpi().y);
-}
-
-Microsoft::WRL::ComPtr<ID2D1DeviceContext>
-WindowRenderTarget::GetD2DDeviceContext() const {
- return graph_manager_->GetD2D1DeviceContext();
-}
-
-inline void WithTransform(
- ID2D1RenderTarget* device_context, const D2D1_MATRIX_3X2_F matrix,
- const std::function<void(ID2D1RenderTarget*)>& action) {
- D2D1_MATRIX_3X2_F old_transform;
- device_context->GetTransform(&old_transform);
- device_context->SetTransform(old_transform * matrix);
- action(device_context);
- device_context->SetTransform(old_transform);
-}
-
-Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> CreateSolidColorBrush(
- const D2D1_COLOR_F& color);
-} // namespace cru::graph
diff --git a/src/graph/graph_manager.cpp b/src/graph/graph_manager.cpp
new file mode 100644
index 00000000..ecc60915
--- /dev/null
+++ b/src/graph/graph_manager.cpp
@@ -0,0 +1,79 @@
+#include "graph_manager.hpp"
+
+#include <d2d1_2.h>
+#include <d3d11.h>
+#include <dwrite.h>
+#include <dxgi1_2.h>
+#include <wrl/client.h>
+
+#include "application.hpp"
+#include "exception.hpp"
+#include "util/com_util.hpp"
+#include "window_render_target.hpp"
+
+namespace cru::graph {
+
+GraphManager* GraphManager::GetInstance() {
+ return Application::GetInstance()->ResolveSingleton<GraphManager>(
+ [](auto) { return new GraphManager{}; });
+}
+
+GraphManager::GraphManager() {
+ UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
+
+#ifdef CRU_DEBUG
+ creation_flags |= D3D11_CREATE_DEVICE_DEBUG;
+#endif
+
+ const D3D_FEATURE_LEVEL feature_levels[] = {
+ D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1,
+ D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2,
+ D3D_FEATURE_LEVEL_9_1};
+
+ Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context;
+ ID3D11Device* d3d11_device;
+
+ ThrowIfFailed(D3D11CreateDevice(
+ nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags,
+ feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION,
+ &d3d11_device, nullptr, &d3d11_device_context));
+ this->d3d11_device_ = util::CreateComSharedPtr(d3d11_device);
+
+ Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device;
+ ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf()));
+
+ ID2D1Factory1* d2d1_factory;
+ ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
+ IID_PPV_ARGS(&d2d1_factory)));
+ this->d2d1_factory_ = util::CreateComSharedPtr(d2d1_factory);
+
+ Microsoft::WRL::ComPtr<ID2D1Device> d2d1_device;
+
+ ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device));
+
+ ID2D1DeviceContext* d2d1_device_context;
+ ThrowIfFailed(d2d1_device->CreateDeviceContext(
+ D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context));
+ this->d2d1_device_context_ = util::CreateComSharedPtr(d2d1_device_context);
+
+ // Identify the physical adapter (GPU or card) this device is runs on.
+ Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter;
+ ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter));
+
+ IDXGIFactory2* dxgi_factory;
+ // Get the factory object that created the DXGI device.
+ ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory)));
+ this->dxgi_factory_ = util::CreateComSharedPtr(dxgi_factory);
+
+ IDWriteFactory* dwrite_factory;
+ ThrowIfFailed(
+ DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
+ reinterpret_cast<IUnknown**>(&dwrite_factory)));
+ this->dwrite_factory_ = util::CreateComSharedPtr(dwrite_factory);
+
+ IDWriteFontCollection* font_collection;
+ ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(&font_collection));
+ this->dwrite_system_font_collection_ =
+ util::CreateComSharedPtr(font_collection);
+}
+} // namespace cru::graph
diff --git a/src/graph/graph_manager.hpp b/src/graph/graph_manager.hpp
new file mode 100644
index 00000000..4a1e7153
--- /dev/null
+++ b/src/graph/graph_manager.hpp
@@ -0,0 +1,60 @@
+#pragma once
+#include "pre.hpp"
+
+#include <memory>
+
+#include "base.hpp"
+
+struct ID3D11Device;
+struct ID3D11DeviceContext;
+struct ID2D1Factory1;
+struct ID2D1DeviceContext;
+struct IDXGIFactory2;
+struct IDWriteFontCollection;
+struct IDWriteFactory;
+
+struct ID2D1RenderTarget;
+
+namespace cru::graph {
+class WindowRenderTarget;
+
+class GraphManager final : public Object {
+ public:
+ static GraphManager* GetInstance();
+
+ private:
+ GraphManager();
+
+ public:
+ 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:
+ std::shared_ptr<ID3D11Device> d3d11_device_;
+ std::shared_ptr<ID2D1Factory1> d2d1_factory_;
+ std::shared_ptr<ID2D1DeviceContext> d2d1_device_context_;
+ std::shared_ptr<IDXGIFactory2> dxgi_factory_;
+ std::shared_ptr<IDWriteFactory> dwrite_factory_;
+ std::shared_ptr<IDWriteFontCollection> dwrite_system_font_collection_;
+};
+} // namespace cru::graph
diff --git a/src/graph/graph_util.hpp b/src/graph/graph_util.hpp
new file mode 100644
index 00000000..2d5be5f3
--- /dev/null
+++ b/src/graph/graph_util.hpp
@@ -0,0 +1,63 @@
+#pragma once
+#include "pre.hpp"
+
+#include <d2d1_1.h>
+#include <functional>
+
+#include "exception.hpp"
+#include "graph_manager.hpp"
+
+namespace cru::graph {
+struct Dpi {
+ float x;
+ float y;
+};
+
+inline Dpi GetDpi() {
+ Dpi dpi;
+ GraphManager::GetInstance()->GetD2D1Factory()->GetDesktopDpi(&dpi.x, &dpi.y);
+ return dpi;
+}
+
+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);
+}
+
+inline void WithTransform(
+ ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F matrix,
+ const std::function<void(ID2D1RenderTarget*)>& action) {
+ D2D1_MATRIX_3X2_F old_transform;
+ render_target->GetTransform(&old_transform);
+ render_target->SetTransform(old_transform * matrix);
+ action(render_target);
+ render_target->SetTransform(old_transform);
+}
+
+inline ID2D1SolidColorBrush* CreateSolidColorBrush(const D2D1_COLOR_F& color) {
+ ID2D1SolidColorBrush* brush;
+ ThrowIfFailed(GraphManager::GetInstance()
+ ->GetD2D1DeviceContext()
+ ->CreateSolidColorBrush(color, &brush));
+ return brush;
+}
+} // namespace cru::graph
diff --git a/src/graph/window_render_target.cpp b/src/graph/window_render_target.cpp
new file mode 100644
index 00000000..d110101a
--- /dev/null
+++ b/src/graph/window_render_target.cpp
@@ -0,0 +1,99 @@
+#include "window_render_target.hpp"
+
+#include <d2d1_1.h>
+#include <d3d11.h>
+#include <dxgi1_2.h>
+#include <wrl/client.h>
+
+#include "exception.hpp"
+#include "graph_manager.hpp"
+#include "graph_util.hpp"
+#include "util/com_util.hpp"
+
+namespace cru::graph {
+WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) {
+ this->graph_manager_ = graph_manager;
+
+ const auto d3d11_device = graph_manager->GetD3D11Device();
+ const auto dxgi_factory = graph_manager->GetDxgiFactory();
+
+ // Allocate a descriptor.
+ DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0};
+ swap_chain_desc.Width = 0; // use automatic sizing
+ swap_chain_desc.Height = 0;
+ swap_chain_desc.Format =
+ DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format
+ swap_chain_desc.Stereo = false;
+ swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling
+ swap_chain_desc.SampleDesc.Quality = 0;
+ swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
+ swap_chain_desc.BufferCount = 2; // use double buffering to enable flip
+ swap_chain_desc.Scaling = DXGI_SCALING_NONE;
+ swap_chain_desc.SwapEffect =
+ DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect
+ swap_chain_desc.Flags = 0;
+
+ IDXGISwapChain1* dxgi_swap_chain;
+ // Get the final swap chain for this window from the DXGI factory.
+ ThrowIfFailed(
+ dxgi_factory->CreateSwapChainForHwnd(d3d11_device, hwnd, &swap_chain_desc,
+ nullptr, nullptr, &dxgi_swap_chain));
+ this->dxgi_swap_chain_ = util::CreateComSharedPtr(dxgi_swap_chain);
+
+ CreateTargetBitmap();
+}
+
+void WindowRenderTarget::ResizeBuffer(const int width, const int height) {
+ const auto graph_manager = graph_manager_;
+ const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext();
+
+ ID2D1Image* old_target;
+ d2d1_device_context->GetTarget(&old_target);
+ const auto target_this = old_target == this->target_bitmap_.get();
+ if (target_this) d2d1_device_context->SetTarget(nullptr);
+
+ util::SafeRelease(old_target);
+ target_bitmap_.reset();
+
+ ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height,
+ DXGI_FORMAT_UNKNOWN, 0));
+
+ CreateTargetBitmap();
+
+ if (target_this) d2d1_device_context->SetTarget(target_bitmap_.get());
+}
+
+void WindowRenderTarget::SetAsTarget() {
+ graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.get());
+}
+
+void WindowRenderTarget::Present() {
+ ThrowIfFailed(dxgi_swap_chain_->Present(1, 0));
+}
+
+void WindowRenderTarget::CreateTargetBitmap() {
+ assert(target_bitmap_ == nullptr); // target bitmap must not exist.
+
+ // Direct2D needs the dxgi version of the backbuffer surface pointer.
+ Microsoft::WRL::ComPtr<IDXGISurface> dxgi_back_buffer;
+ ThrowIfFailed(
+ dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer)));
+
+ const auto dpi = GetDpi();
+
+ auto bitmap_properties = D2D1::BitmapProperties1(
+ D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
+ D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
+ dpi.x, dpi.y);
+
+ ID2D1Bitmap1* bitmap;
+ // Get a D2D surface from the DXGI back buffer to use as the D2D render
+ // target.
+ ThrowIfFailed(
+ graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface(
+ dxgi_back_buffer.Get(), &bitmap_properties, &bitmap));
+ this->target_bitmap_ = util::CreateComSharedPtr(bitmap);
+
+ dxgi_back_buffer->Release();
+}
+} // namespace cru::graph
diff --git a/src/graph/window_render_target.hpp b/src/graph/window_render_target.hpp
new file mode 100644
index 00000000..9b93df19
--- /dev/null
+++ b/src/graph/window_render_target.hpp
@@ -0,0 +1,49 @@
+#pragma once
+#include "pre.hpp"
+
+#include <Windows.h>
+#include <memory>
+
+#include "base.hpp"
+
+struct IDXGISwapChain1;
+struct ID2D1Bitmap1;
+
+namespace cru::graph {
+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_;
+ std::shared_ptr<IDXGISwapChain1> dxgi_swap_chain_;
+ std::shared_ptr<ID2D1Bitmap1> target_bitmap_;
+};
+} // namespace cru::graph
diff --git a/src/ui/controls/button.cpp b/src/ui/controls/button.cpp
index e69de29b..5d4e15cc 100644
--- a/src/ui/controls/button.cpp
+++ b/src/ui/controls/button.cpp
@@ -0,0 +1,13 @@
+#include "button.hpp"
+
+#include "ui/ui_manager.hpp"
+#include "ui/render/border_render_object.hpp"
+
+namespace cru::ui::controls {
+Button::Button() {
+ const auto predefined_resource =
+ UiManager::GetInstance()->GetPredefineResources();
+ render_object_ = new render::BorderRenderObject(); }
+
+void Button::OnChildChanged(Control* old_child, Control* new_child) {}
+} // namespace cru::ui::controls
diff --git a/src/ui/controls/button.hpp b/src/ui/controls/button.hpp
index 010c3f5b..27e7fc7b 100644
--- a/src/ui/controls/button.hpp
+++ b/src/ui/controls/button.hpp
@@ -1,8 +1,35 @@
#pragma once
#include "pre.hpp"
-#include "ui/control.hpp"
+#include "ui/content_control.hpp"
+
+namespace cru::ui::render {
+class BorderRenderObject;
+}
namespace cru::ui::controls {
+class Button : public ContentControl {
+ public:
+ static constexpr auto control_type = L"Button";
-}
+ static Button* Create();
+
+ protected:
+ Button();
+
+ public:
+ Button(const Button& other) = delete;
+ Button(Button&& other) = delete;
+ Button& operator=(const Button& other) = delete;
+ Button& operator=(Button&& other) = delete;
+ ~Button();
+
+ render::RenderObject* GetRenderObject() const override;
+
+ protected:
+ void OnChildChanged(Control* old_child, Control* new_child) override;
+
+ private:
+ render::BorderRenderObject* render_object_;
+};
+} // namespace cru::ui::controls
diff --git a/src/ui/controls/text_block.cpp b/src/ui/controls/text_block.cpp
index 85116910..c891b832 100644
--- a/src/ui/controls/text_block.cpp
+++ b/src/ui/controls/text_block.cpp
@@ -1,7 +1,5 @@
#include "text_block.hpp"
-#include <dwrite.h>
-
#include "ui/render/text_render_object.hpp"
#include "ui/ui_manager.hpp"
diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp
index 033f59d8..e1550665 100644
--- a/src/ui/render/border_render_object.cpp
+++ b/src/ui/render/border_render_object.cpp
@@ -1,16 +1,45 @@
#include "border_render_object.hpp"
+#include <d2d1_1.h>
+#include <wrl/client.h>
#include <algorithm>
#include "cru_debug.hpp"
#include "exception.hpp"
-#include "graph/graph.hpp"
+#include "graph/graph_manager.hpp"
+#include "graph/graph_util.hpp"
+#include "util/com_util.hpp"
namespace cru::ui::render {
-BorderRenderObject::BorderRenderObject(Microsoft::WRL::ComPtr<ID2D1Brush> brush)
- : border_brush_(std::move(brush)) {}
+BorderRenderObject::BorderRenderObject(ID2D1Brush* brush) {
+ assert(brush);
+ brush->AddRef();
+ this->border_brush_ = brush;
+ try {
+ RecreateGeometry();
+ } catch (...) {
+ brush->Release();
+ throw;
+ }
+}
+
+BorderRenderObject::~BorderRenderObject() {
+ util::SafeRelease(border_brush_);
+ util::SafeRelease(geometry_);
+ util::SafeRelease(border_outer_geometry_);
+}
+
+void BorderRenderObject::SetBrush(ID2D1Brush* new_brush) {
+ assert(new_brush);
+ util::SafeRelease(border_brush_);
+ new_brush->AddRef();
+ border_brush_ = new_brush;
+}
void BorderRenderObject::RecreateGeometry() {
+ util::SafeRelease(geometry_);
+ util::SafeRelease(border_outer_geometry_);
+
const auto d2d_factory = graph::GraphManager::GetInstance()->GetD2D1Factory();
Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry;
@@ -19,8 +48,8 @@ void BorderRenderObject::RecreateGeometry() {
Microsoft::WRL::ComPtr<ID2D1PathGeometry> border_outer_geometry;
ThrowIfFailed(d2d_factory->CreatePathGeometry(&border_outer_geometry));
- ID2D1GeometrySink* sink;
- auto f = [sink](const Rect& rect, const CornerRadius& corner) {
+ Microsoft::WRL::ComPtr<ID2D1GeometrySink> sink;
+ auto f = [&sink](const Rect& rect, const CornerRadius& corner) {
sink->BeginFigure(D2D1::Point2F(rect.left + corner.left_top.x, rect.top),
D2D1_FIGURE_BEGIN_FILLED);
sink->AddLine(
@@ -53,21 +82,21 @@ void BorderRenderObject::RecreateGeometry() {
ThrowIfFailed(border_outer_geometry->Open(&sink));
f(outer_rect, corner_radius_);
ThrowIfFailed(sink->Close());
- sink->Release();
+ sink = nullptr;
const Rect inner_rect = outer_rect.Shrink(border_thickness_);
ThrowIfFailed(geometry->Open(&sink));
f(outer_rect, corner_radius_);
f(inner_rect, corner_radius_);
ThrowIfFailed(sink->Close());
- sink->Release();
+ sink = nullptr;
- geometry_ = std::move(geometry);
- border_outer_geometry_ = std::move(border_outer_geometry);
+ geometry_ = geometry.Detach();
+ border_outer_geometry_ = border_outer_geometry.Detach();
}
void BorderRenderObject::Draw(ID2D1RenderTarget* render_target) {
- render_target->FillGeometry(geometry_.Get(), border_brush_.Get());
+ render_target->FillGeometry(geometry_, border_brush_);
if (const auto child = GetChild()) {
auto offset = child->GetOffset();
graph::WithTransform(render_target,
@@ -167,7 +196,8 @@ void BorderRenderObject::OnLayoutCore(const Rect& rect) {
}
if (coerced_content_available_size.height < 0) {
debug::DebugMessage(
- L"Layout: vertical length of padding, border and margin is bigger than "
+ L"Layout: vertical length of padding, border and margin is bigger "
+ L"than "
L"available length.");
coerced_content_available_size.height = 0;
}
diff --git a/src/ui/render/border_render_object.hpp b/src/ui/render/border_render_object.hpp
index d6effc21..eccb1219 100644
--- a/src/ui/render/border_render_object.hpp
+++ b/src/ui/render/border_render_object.hpp
@@ -28,20 +28,18 @@ struct CornerRadius {
class BorderRenderObject : public RenderObject {
public:
- explicit BorderRenderObject(Microsoft::WRL::ComPtr<ID2D1Brush> brush);
+ explicit BorderRenderObject(ID2D1Brush* brush);
BorderRenderObject(const BorderRenderObject& other) = delete;
BorderRenderObject(BorderRenderObject&& other) = delete;
BorderRenderObject& operator=(const BorderRenderObject& other) = delete;
BorderRenderObject& operator=(BorderRenderObject&& other) = delete;
- ~BorderRenderObject() override = default;
+ ~BorderRenderObject() override;
bool IsEnabled() const { return is_enabled_; }
void SetEnabled(bool enabled) { is_enabled_ = enabled; }
- Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const { return border_brush_; }
- void SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush> new_brush) {
- border_brush_ = std::move(new_brush);
- }
+ ID2D1Brush* GetBrush() const { return border_brush_; }
+ void SetBrush(ID2D1Brush* new_brush);
Thickness GetBorderWidth() const { return border_thickness_; }
void SetBorderWidth(const Thickness& thickness) { border_thickness_ = thickness; }
@@ -51,11 +49,11 @@ class BorderRenderObject : public RenderObject {
corner_radius_ = new_corner_radius;
}
- void RecreateGeometry(); // TODO
+ void RecreateGeometry();
- void Draw(ID2D1RenderTarget* render_target) override; // TODO
+ void Draw(ID2D1RenderTarget* render_target) override;
- RenderObject* HitTest(const Point& point) override; // TODO
+ RenderObject* HitTest(const Point& point) override;
protected:
void OnAddChild(RenderObject* new_child, int position) override;
@@ -73,11 +71,11 @@ class BorderRenderObject : public RenderObject {
private:
bool is_enabled_ = false;
- Microsoft::WRL::ComPtr<ID2D1Brush> border_brush_;
+ ID2D1Brush* border_brush_ = nullptr;
Thickness border_thickness_ = Thickness::Zero();
CornerRadius corner_radius_{};
- Microsoft::WRL::ComPtr<ID2D1Geometry> geometry_{nullptr};
- Microsoft::WRL::ComPtr<ID2D1Geometry> border_outer_geometry_{nullptr};
+ ID2D1Geometry* geometry_ = nullptr;
+ ID2D1Geometry* border_outer_geometry_ = nullptr;
};
} // namespace cru::ui::render
diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp
index 4708d187..e4d327f1 100644
--- a/src/ui/render/flex_layout_render_object.cpp
+++ b/src/ui/render/flex_layout_render_object.cpp
@@ -4,7 +4,7 @@
#include <functional>
#include "cru_debug.hpp"
-#include "graph/graph.hpp"
+#include "graph/graph_util.hpp"
namespace cru::ui::render {
FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) {
diff --git a/src/ui/render/render_object.cpp b/src/ui/render/render_object.cpp
index f56baa8f..232eda41 100644
--- a/src/ui/render/render_object.cpp
+++ b/src/ui/render/render_object.cpp
@@ -1,8 +1,5 @@
#include "render_object.hpp"
-#include <d2d1.h>
-#include <dwrite.h>
-
#include "cru_debug.hpp"
namespace cru::ui::render {
diff --git a/src/ui/render/render_object.hpp b/src/ui/render/render_object.hpp
index 51b0c3ae..abdda605 100644
--- a/src/ui/render/render_object.hpp
+++ b/src/ui/render/render_object.hpp
@@ -1,7 +1,6 @@
#pragma once
#include "pre.hpp"
-#include <optional>
#include <vector>
#include "base.hpp"
@@ -9,7 +8,6 @@
// forward declarations
struct ID2D1RenderTarget;
-
namespace cru::ui {
class Control;
}
diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp
index e8967d48..b66fffa3 100644
--- a/src/ui/render/text_render_object.cpp
+++ b/src/ui/render/text_render_object.cpp
@@ -5,19 +5,61 @@
#include <algorithm>
#include "exception.hpp"
-#include "graph/graph.hpp"
+#include "graph/graph_manager.hpp"
+#include "graph/graph_util.hpp"
+#include "util/com_util.hpp"
namespace cru::ui::render {
-TextRenderObject::TextRenderObject(
- Microsoft::WRL::ComPtr<ID2D1Brush> brush,
- Microsoft::WRL::ComPtr<IDWriteTextFormat> format,
- Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush)
- : brush_(std::move(brush)),
- text_format_(std::move(format)),
- selection_brush_(std::move(selection_brush)) {
+TextRenderObject::TextRenderObject(ID2D1Brush* brush, IDWriteTextFormat* format,
+ ID2D1Brush* selection_brush) {
+ assert(brush);
+ assert(format);
+ assert(selection_brush);
+ brush->AddRef();
+ format->AddRef();
+ selection_brush->AddRef();
+ this->brush_ = brush;
+ this->text_format_ = format;
+ this->selection_brush_ = selection_brush;
+ try {
+ RecreateTextLayout();
+ } catch (...) {
+ brush->Release();
+ format->Release();
+ selection_brush->Release();
+ throw;
+ }
+}
+
+TextRenderObject::~TextRenderObject() {
+ util::SafeRelease(brush_);
+ util::SafeRelease(text_format_);
+ util::SafeRelease(text_layout_);
+ util::SafeRelease(selection_brush_);
+}
+
+void TextRenderObject::SetBrush(ID2D1Brush* new_brush) {
+ assert(new_brush);
+ util::SafeRelease(brush_);
+ new_brush->AddRef();
+ brush_ = new_brush;
+}
+
+void TextRenderObject::SetTextFormat(IDWriteTextFormat* new_text_format) {
+ assert(new_text_format);
+ util::SafeRelease(text_format_);
+ new_text_format->AddRef();
+ text_format_ = new_text_format;
RecreateTextLayout();
}
+void TextRenderObject::SetSelectionBrush(ID2D1Brush* new_brush) {
+ assert(new_brush);
+ util::SafeRelease(selection_brush_);
+ new_brush->AddRef();
+ selection_brush_ = new_brush;
+}
+
namespace {
void DrawSelectionRect(ID2D1RenderTarget* render_target,
IDWriteTextLayout* layout, ID2D1Brush* brush,
@@ -54,9 +96,8 @@ void TextRenderObject::Draw(ID2D1RenderTarget* render_target) {
D2D1::Matrix3x2F::Translation(GetMargin().left + GetPadding().left,
GetMargin().top + GetPadding().top),
[this](auto rt) {
- DrawSelectionRect(rt, text_layout_.Get(), selection_brush_.Get(),
- selection_range_);
- rt->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get());
+ DrawSelectionRect(rt, text_layout_, selection_brush_, selection_range_);
+ rt->DrawTextLayout(D2D1::Point2F(), text_layout_, brush_);
});
}
@@ -99,7 +140,7 @@ void TextRenderObject::OnLayoutContent(const Rect& content_rect) {}
void TextRenderObject::RecreateTextLayout() {
assert(text_format_ != nullptr);
- text_layout_ = nullptr; // release last one
+ util::SafeRelease(text_layout_);
const auto dwrite_factory =
graph::GraphManager::GetInstance()->GetDWriteFactory();
@@ -107,7 +148,7 @@ void TextRenderObject::RecreateTextLayout() {
const auto&& size = GetSize();
ThrowIfFailed(dwrite_factory->CreateTextLayout(
- text_.c_str(), static_cast<UINT32>(text_.size()), text_format_.Get(),
+ text_.c_str(), static_cast<UINT32>(text_.size()), text_format_,
size.width, size.height, &text_layout_));
}
} // namespace cru::ui::render
diff --git a/src/ui/render/text_render_object.hpp b/src/ui/render/text_render_object.hpp
index ac874b75..30d78736 100644
--- a/src/ui/render/text_render_object.hpp
+++ b/src/ui/render/text_render_object.hpp
@@ -1,8 +1,6 @@
#pragma once
#include "pre.hpp"
-#include <wrl/client.h> // for ComPtr
-
#include "render_object.hpp"
// forward declarations
@@ -13,14 +11,13 @@ struct IDWriteTextLayout;
namespace cru::ui::render {
class TextRenderObject : public RenderObject {
public:
- TextRenderObject(Microsoft::WRL::ComPtr<ID2D1Brush> brush,
- Microsoft::WRL::ComPtr<IDWriteTextFormat> format,
- Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush);
+ TextRenderObject(ID2D1Brush* brush, IDWriteTextFormat* format,
+ ID2D1Brush* selection_brush);
TextRenderObject(const TextRenderObject& other) = delete;
TextRenderObject(TextRenderObject&& other) = delete;
TextRenderObject& operator=(const TextRenderObject& other) = delete;
TextRenderObject& operator=(TextRenderObject&& other) = delete;
- ~TextRenderObject() override = default;
+ ~TextRenderObject() override;
String GetText() const { return text_; }
void SetText(String new_text) {
@@ -28,19 +25,11 @@ class TextRenderObject : public RenderObject {
RecreateTextLayout();
}
- Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const { return brush_; }
- void SetBrush(Microsoft::WRL::ComPtr<ID2D1Brush> new_brush) {
- brush_ = std::move(new_brush);
- }
+ ID2D1Brush* GetBrush() const { return brush_; }
+ void SetBrush(ID2D1Brush* new_brush);
- Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const {
- return text_format_;
- }
- void SetTextFormat(
- Microsoft::WRL::ComPtr<IDWriteTextFormat> new_text_format) {
- text_format_ = std::move(new_text_format);
- RecreateTextLayout();
- }
+ IDWriteTextFormat* GetTextFormat() const { return text_format_; }
+ void SetTextFormat(IDWriteTextFormat* new_text_format);
std::optional<TextRange> GetSelectionRange() const {
return selection_range_;
@@ -49,12 +38,10 @@ class TextRenderObject : public RenderObject {
selection_range_ = std::move(new_range);
}
- Microsoft::WRL::ComPtr<ID2D1Brush> GetSelectionBrush() const {
+ ID2D1Brush* GetSelectionBrush() const {
return selection_brush_;
}
- void SetSelectionBrush(Microsoft::WRL::ComPtr<ID2D1Brush> new_brush) {
- selection_brush_ = std::move(new_brush);
- }
+ void SetSelectionBrush(ID2D1Brush* new_brush);
void Draw(ID2D1RenderTarget* render_target) override;
@@ -72,11 +59,11 @@ class TextRenderObject : public RenderObject {
private:
String text_;
- Microsoft::WRL::ComPtr<ID2D1Brush> brush_;
- Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_;
- Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_;
+ ID2D1Brush* brush_;
+ IDWriteTextFormat* text_format_;
+ IDWriteTextLayout* text_layout_;
std::optional<TextRange> selection_range_ = std::nullopt;
- Microsoft::WRL::ComPtr<ID2D1Brush> selection_brush_;
+ ID2D1Brush* selection_brush_;
};
} // namespace cru::ui::render
diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp
index f198c2fa..44c3c426 100644
--- a/src/ui/render/window_render_object.cpp
+++ b/src/ui/render/window_render_object.cpp
@@ -1,6 +1,6 @@
#include "window_render_object.hpp"
-#include "graph/graph.hpp"
+#include "graph/graph_util.hpp"
#include "ui/window.hpp"
namespace cru::ui::render {
diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp
index 26b1fe62..add77516 100644
--- a/src/ui/ui_manager.cpp
+++ b/src/ui/ui_manager.cpp
@@ -1,10 +1,14 @@
#include "ui_manager.hpp"
#include <Windows.h>
+#include <d2d1.h>
+#include <dwrite.h>
#include "application.hpp"
#include "exception.hpp"
-#include "graph/graph.hpp"
+#include "graph/graph_manager.hpp"
+#include "graph/graph_util.hpp"
+#include "util/com_util.hpp"
namespace cru::ui {
namespace {
@@ -17,19 +21,10 @@ void GetSystemCaretInfo(CaretInfo* caret_info) {
caret_info->half_caret_width = caret_width / 2.0f;
}
-Microsoft::WRL::ComPtr<ID2D1Brush> CreateSolidBrush(
- graph::GraphManager* graph_manager, const D2D1_COLOR_F& color) {
- const auto device_context = graph_manager->GetD2D1DeviceContext();
- Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> solid_color_brush;
- device_context->CreateSolidColorBrush(color, &solid_color_brush);
- return solid_color_brush;
-}
-
-Microsoft::WRL::ComPtr<IDWriteTextFormat> CreateDefaultTextFormat(
- graph::GraphManager* graph_manager) {
- const auto dwrite_factory = graph_manager->GetDWriteFactory();
-
- Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format;
+IDWriteTextFormat* CreateDefaultTextFormat() {
+ const auto dwrite_factory =
+ graph::GraphManager::GetInstance()->GetDWriteFactory();
+ IDWriteTextFormat* text_format;
ThrowIfFailed(dwrite_factory->CreateTextFormat(
L"等线", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
@@ -43,26 +38,32 @@ Microsoft::WRL::ComPtr<IDWriteTextFormat> CreateDefaultTextFormat(
}
} // namespace
-PredefineResources::PredefineResources(graph::GraphManager* graph_manager)
- : text_block_selection_brush{CreateSolidBrush(
- graph_manager, D2D1::ColorF(D2D1::ColorF::LightSkyBlue))},
- text_block_text_brush{
- CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::Black))},
- text_block_text_format{CreateDefaultTextFormat(graph_manager)},
- debug_layout_out_border_brush{
- CreateSolidBrush(graph_manager, D2D1::ColorF(D2D1::ColorF::Crimson))},
- debug_layout_margin_brush{CreateSolidBrush(
- graph_manager, D2D1::ColorF(D2D1::ColorF::LightCoral, 0.25f))},
- debug_layout_padding_brush{CreateSolidBrush(
- graph_manager, D2D1::ColorF(D2D1::ColorF::SkyBlue, 0.25f))} {}
+PredefineResources::PredefineResources() {
+ try {
+ text_block_selection_brush =
+ graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::LightSkyBlue));
+ text_block_text_brush =
+ graph::CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black));
+ text_block_text_format = CreateDefaultTextFormat();
+ } catch (...) {
+ util::SafeRelease(text_block_selection_brush);
+ util::SafeRelease(text_block_text_brush);
+ util::SafeRelease(text_block_text_format);
+ }
+}
+
+PredefineResources::~PredefineResources() {
+ util::SafeRelease(text_block_selection_brush);
+ util::SafeRelease(text_block_text_brush);
+ util::SafeRelease(text_block_text_format);
+}
UiManager* UiManager::GetInstance() {
return Application::GetInstance()->ResolveSingleton<UiManager>(
[](auto) { return new UiManager{}; });
}
-UiManager::UiManager()
- : predefine_resources_(graph::GraphManager::GetInstance()) {
+UiManager::UiManager() : predefine_resources_() {
GetSystemCaretInfo(&caret_info_);
}
} // namespace cru::ui
diff --git a/src/ui/ui_manager.hpp b/src/ui/ui_manager.hpp
index c2331dd4..b736381d 100644
--- a/src/ui/ui_manager.hpp
+++ b/src/ui/ui_manager.hpp
@@ -18,22 +18,17 @@ struct CaretInfo {
class PredefineResources : public Object {
public:
- explicit PredefineResources(graph::GraphManager* graph_manager);
+ PredefineResources();
PredefineResources(const PredefineResources& other) = delete;
PredefineResources(PredefineResources&& other) = delete;
PredefineResources& operator=(const PredefineResources& other) = delete;
PredefineResources& operator=(PredefineResources&& other) = delete;
- ~PredefineResources() override = default;
+ ~PredefineResources() override;
// region TextBlock
- Microsoft::WRL::ComPtr<ID2D1Brush> text_block_selection_brush;
- Microsoft::WRL::ComPtr<ID2D1Brush> text_block_text_brush;
- Microsoft::WRL::ComPtr<IDWriteTextFormat> text_block_text_format;
-
- // region debug
- Microsoft::WRL::ComPtr<ID2D1Brush> debug_layout_out_border_brush;
- Microsoft::WRL::ComPtr<ID2D1Brush> debug_layout_margin_brush;
- Microsoft::WRL::ComPtr<ID2D1Brush> debug_layout_padding_brush;
+ ID2D1Brush* text_block_selection_brush = nullptr;
+ ID2D1Brush* text_block_text_brush = nullptr;
+ IDWriteTextFormat* text_block_text_format = nullptr;
};
class UiManager : public Object {
diff --git a/src/ui/window.cpp b/src/ui/window.cpp
index 7b00ca05..90add552 100644
--- a/src/ui/window.cpp
+++ b/src/ui/window.cpp
@@ -1,10 +1,13 @@
#include "window.hpp"
#include <windowsx.h>
+#include <d2d1_1.h>
#include "application.hpp"
#include "exception.hpp"
-#include "graph/graph.hpp"
+#include "graph/graph_manager.hpp"
+#include "graph/graph_util.hpp"
+#include "graph/window_render_target.hpp"
#include "render/window_render_object.hpp"
namespace cru::ui {
@@ -207,10 +210,9 @@ void Window::BeforeCreateHwnd() { window_ = this; }
void Window::AfterCreateHwnd(WindowManager* window_manager) {
window_manager->RegisterWindow(hwnd_, this);
- render_target_ =
- graph::GraphManager::GetInstance()->CreateWindowRenderTarget(hwnd_);
+ render_target_.reset(new graph::WindowRenderTarget(graph::GraphManager::GetInstance(), hwnd_));
- render_object_ = new render::WindowRenderObject(this);
+ render_object_.reset(new render::WindowRenderObject(this));
}
Window::~Window() {
@@ -220,12 +222,11 @@ Window::~Window() {
}
TraverseDescendants(
[this](Control* control) { control->OnDetachToWindow(this); });
- delete render_object_;
}
StringView Window::GetControlType() const { return control_type; }
-render::RenderObject* Window::GetRenderObject() const { return render_object_; }
+render::RenderObject* Window::GetRenderObject() const { return render_object_.get(); }
void Window::SetDeleteThisOnDestroy(bool value) {
delete_this_on_destroy_ = value;
@@ -556,17 +557,12 @@ void Window::OnDestroyInternal() {
void Window::OnPaintInternal() {
render_target_->SetAsTarget();
- auto device_context = render_target_->GetD2DDeviceContext();
-
+ auto device_context = render_target_->GetGraphManager()->GetD2D1DeviceContext();
device_context->BeginDraw();
-
// Clear the background.
device_context->Clear(D2D1::ColorF(D2D1::ColorF::White));
-
- render_object_->Draw(device_context.Get());
-
+ render_object_->Draw(device_context);
ThrowIfFailed(device_context->EndDraw(), "Failed to draw window.");
-
render_target_->Present();
ValidateRect(hwnd_, nullptr);
diff --git a/src/ui/window.hpp b/src/ui/window.hpp
index 1c48bf43..dd7631d0 100644
--- a/src/ui/window.hpp
+++ b/src/ui/window.hpp
@@ -221,8 +221,7 @@ class Window final : public ContentControl {
HWND hwnd_ = nullptr;
Window* parent_window_ = nullptr;
std::shared_ptr<graph::WindowRenderTarget> render_target_{};
-
- render::WindowRenderObject* render_object_;
+ std::shared_ptr<render::WindowRenderObject> render_object_{};
Control* mouse_hover_control_ = nullptr;
diff --git a/src/util/com_util.hpp b/src/util/com_util.hpp
new file mode 100644
index 00000000..bbaf1c27
--- /dev/null
+++ b/src/util/com_util.hpp
@@ -0,0 +1,22 @@
+#pragma once
+#include "pre.hpp"
+
+#include <memory>
+
+namespace cru::util {
+
+template <typename TInterface>
+std::shared_ptr<TInterface> CreateComSharedPtr(TInterface* p) {
+ return std::shared_ptr<TInterface>(p, [](TInterface* ptr) {
+ if (ptr != nullptr) ptr->Release();
+ });
+}
+
+template <class TInterface>
+void SafeRelease(TInterface*& p) {
+ if (p != nullptr) {
+ p->Release();
+ p = nullptr;
+ }
+}
+} // namespace cru::util