diff options
Diffstat (limited to 'CruUI')
-rw-r--r-- | CruUI/graph/graph.cpp | 48 | ||||
-rw-r--r-- | CruUI/graph/graph.h | 17 | ||||
-rw-r--r-- | CruUI/main.cpp | bin | 3038 -> 3460 bytes | |||
-rw-r--r-- | CruUI/ui/controls/text_block.cpp | 55 | ||||
-rw-r--r-- | CruUI/ui/controls/text_block.h | 6 |
5 files changed, 104 insertions, 22 deletions
diff --git a/CruUI/graph/graph.cpp b/CruUI/graph/graph.cpp index 4871c5f4..49616a6f 100644 --- a/CruUI/graph/graph.cpp +++ b/CruUI/graph/graph.cpp @@ -8,7 +8,7 @@ namespace cru { using Microsoft::WRL::ComPtr; WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) - { + { this->graph_manager_ = graph_manager; const auto d3d11_device = graph_manager->GetD3D11Device(); @@ -46,18 +46,18 @@ namespace cru { } 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_; + const auto target_this = old_target == this->target_bitmap_; if (target_this) d2d1_device_context->SetTarget(nullptr); @@ -75,19 +75,19 @@ namespace cru { } 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( @@ -114,8 +114,13 @@ namespace cru { ); } + GraphManager* GraphManager::GetInstance() + { + return Application::GetInstance()->GetGraphManager(); + } + GraphManager::GraphManager() - { + { UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; #ifdef _DEBUG @@ -174,59 +179,66 @@ namespace cru { ThrowIfFailed( dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_)) ); + + + ThrowIfFailed(DWriteCreateFactory( + DWRITE_FACTORY_TYPE_SHARED, + __uuidof(IDWriteFactory), + reinterpret_cast<IUnknown**>(dwrite_factory_.GetAddressOf()) + )); } GraphManager::~GraphManager() - { + { } std::shared_ptr<WindowRenderTarget> GraphManager::CreateWindowRenderTarget(HWND hwnd) - { + { return std::make_shared<WindowRenderTarget>(this, hwnd); } Dpi GraphManager::GetDpi() - { + { Dpi dpi; d2d1_factory_->GetDesktopDpi(&dpi.x, &dpi.y); return dpi; } void GraphManager::ReloadSystemMetrics() - { + { ThrowIfFailed( d2d1_factory_->ReloadSystemMetrics() ); } inline int DipToPixelInternal(float dip, float dpi) - { + { return static_cast<int>(dip * dpi / 96.0f); } int DipToPixelX(float dipX) - { + { return DipToPixelInternal(dipX, Application::GetInstance()->GetGraphManager()->GetDpi().x); } int DipToPixelY(float dipY) - { + { return DipToPixelInternal(dipY, Application::GetInstance()->GetGraphManager()->GetDpi().y); } inline float DipToPixelInternal(int pixel, float dpi) - { + { return static_cast<float>(pixel) * 96.0f / dpi; } float PixelToDipX(int pixelX) - { + { return DipToPixelInternal(pixelX, Application::GetInstance()->GetGraphManager()->GetDpi().x); } float PixelToDipY(int pixelY) - { + { return DipToPixelInternal(pixelY, Application::GetInstance()->GetGraphManager()->GetDpi().y); } } diff --git a/CruUI/graph/graph.h b/CruUI/graph/graph.h index 69d11b9c..b221115a 100644 --- a/CruUI/graph/graph.h +++ b/CruUI/graph/graph.h @@ -9,12 +9,12 @@ namespace cru { namespace graph - { + { class GraphManager; //Represents a window render target. class WindowRenderTarget : public Object - { + { public: WindowRenderTarget(GraphManager* graph_manager, HWND hwnd); WindowRenderTarget(const WindowRenderTarget& other) = delete; @@ -58,13 +58,16 @@ namespace cru }; struct Dpi - { + { float x; float y; }; class GraphManager : public Object - { + { + public: + static GraphManager* GetInstance(); + public: GraphManager(); GraphManager(const GraphManager& other) = delete; @@ -94,6 +97,12 @@ namespace cru 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); diff --git a/CruUI/main.cpp b/CruUI/main.cpp Binary files differindex 185dd90d..0c13fcd7 100644 --- a/CruUI/main.cpp +++ b/CruUI/main.cpp diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp index b8fe742d..cf2a7bda 100644 --- a/CruUI/ui/controls/text_block.cpp +++ b/CruUI/ui/controls/text_block.cpp @@ -1,11 +1,30 @@ #include "text_block.h" +#include "ui/window.h" +#include "graph/graph.h" + namespace cru { namespace ui { namespace controls { + TextBlock::TextBlock() + { + const auto device_context = graph::GraphManager::GetInstance()->GetD2D1DeviceContext(); + + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> solid_color_brush; + + device_context->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &solid_color_brush); + + brush_ = solid_color_brush; + } + + TextBlock::~TextBlock() + { + + } + void TextBlock::SetText(const String& text) { const auto old_text = text_; @@ -15,12 +34,48 @@ namespace cru void TextBlock::OnSizeChangedCore(events::SizeChangedEventArgs& args) { + CreateTextLayout(); + const auto window = GetWindow(); + if (window != nullptr) + window->Repaint(); + } + void TextBlock::OnDraw(ID2D1DeviceContext* device_context) + { + device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get()); } void TextBlock::OnTextChangedCore(const String& old_text, const String& new_text) { + CreateTextLayout(); + const auto window = GetWindow(); + if (window != nullptr) + window->Repaint(); + } + + void TextBlock::CreateTextLayout() + { + auto dwrite_factory = graph::GraphManager::GetInstance()->GetDWriteFactory(); + + Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format; + + dwrite_factory->CreateTextFormat( + L"΅ΘΟί", nullptr, + DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, + DWRITE_FONT_STRETCH_NORMAL, + 24.0, L"zh-cn", + &text_format + ); + + const auto&& size = GetSize(); + dwrite_factory->CreateTextLayout( + text_.c_str(), text_.size(), + text_format.Get(), + size.width, size.height, + &text_layout_ + ); } } } diff --git a/CruUI/ui/controls/text_block.h b/CruUI/ui/controls/text_block.h index aea4629f..ff1870c1 100644 --- a/CruUI/ui/controls/text_block.h +++ b/CruUI/ui/controls/text_block.h @@ -28,12 +28,18 @@ namespace cru protected: void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final; + void OnDraw(ID2D1DeviceContext* device_context) override; private: void OnTextChangedCore(const String& old_text, const String& new_text); + void CreateTextLayout(); + private: String text_; + + Microsoft::WRL::ComPtr<ID2D1Brush> brush_; + Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; }; } } |