aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2018-09-04 20:24:07 +0800
committercrupest <crupest@outlook.com>2018-09-04 20:24:07 +0800
commiteebec8297a8538c40518ceee043edfc41e3b1343 (patch)
tree6050abc691cf99ae623231aeb4749f69610748f3
parentdc1d739f833d594c2bafb6e70e59077756b5d607 (diff)
downloadcru-eebec8297a8538c40518ceee043edfc41e3b1343.tar.gz
cru-eebec8297a8538c40518ceee043edfc41e3b1343.tar.bz2
cru-eebec8297a8538c40518ceee043edfc41e3b1343.zip
...
-rw-r--r--CruUI/graph/graph.cpp2
-rw-r--r--CruUI/graph/graph.h7
-rw-r--r--CruUI/ui/control.cpp6
-rw-r--r--CruUI/ui/control.h2
-rw-r--r--CruUI/ui/controls/text_block.cpp84
-rw-r--r--CruUI/ui/controls/text_block.h27
-rw-r--r--CruUI/ui/window.h2
7 files changed, 99 insertions, 31 deletions
diff --git a/CruUI/graph/graph.cpp b/CruUI/graph/graph.cpp
index 49616a6f..4f65a86c 100644
--- a/CruUI/graph/graph.cpp
+++ b/CruUI/graph/graph.cpp
@@ -186,6 +186,8 @@ namespace cru {
__uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(dwrite_factory_.GetAddressOf())
));
+
+ dwrite_factory_->GetSystemFontCollection(&dwrite_system_font_collection_);
}
GraphManager::~GraphManager()
diff --git a/CruUI/graph/graph.h b/CruUI/graph/graph.h
index b221115a..bb1de716 100644
--- a/CruUI/graph/graph.h
+++ b/CruUI/graph/graph.h
@@ -112,6 +112,11 @@ namespace cru
//Reload system metrics including desktop dpi.
void ReloadSystemMetrics();
+ Microsoft::WRL::ComPtr<IDWriteFontCollection> GetSystemFontCollection() const
+ {
+ return dwrite_system_font_collection_.Get();
+ }
+
private:
Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context_;
@@ -119,7 +124,9 @@ namespace cru
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_;
};
int DipToPixelX(float dip_x);
diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp
index e2e6f98b..ac338e95 100644
--- a/CruUI/ui/control.cpp
+++ b/CruUI/ui/control.cpp
@@ -199,6 +199,12 @@ namespace cru {
device_context->SetTransform(old_transform);
}
+ void Control::Repaint()
+ {
+ if (window_ != nullptr)
+ window_->Repaint();
+ }
+
bool Control::RequestFocus()
{
auto window = GetWindow();
diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h
index a87e6ce4..ee6abe12 100644
--- a/CruUI/ui/control.h
+++ b/CruUI/ui/control.h
@@ -120,6 +120,8 @@ namespace cru
//Draw this control and its child controls.
void Draw(ID2D1DeviceContext* device_context);
+ virtual void Repaint();
+
//*************** region: focus ***************
bool RequestFocus();
diff --git a/CruUI/ui/controls/text_block.cpp b/CruUI/ui/controls/text_block.cpp
index cf2a7bda..a3d7577f 100644
--- a/CruUI/ui/controls/text_block.cpp
+++ b/CruUI/ui/controls/text_block.cpp
@@ -2,6 +2,7 @@
#include "ui/window.h"
#include "graph/graph.h"
+#include "exception.h"
namespace cru
{
@@ -9,21 +10,15 @@ namespace cru
{
namespace controls
{
- TextBlock::TextBlock()
+ TextBlock::TextBlock(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format,
+ const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush)
{
- 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;
+ text_format_ = init_text_format;
+ if (init_brush == nullptr)
+ CreateDefaultBrush();
}
- TextBlock::~TextBlock()
- {
-
- }
+ TextBlock::~TextBlock() = default;
void TextBlock::SetText(const String& text)
{
@@ -32,12 +27,23 @@ namespace cru
OnTextChangedCore(old_text, text);
}
+ void TextBlock::SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush)
+ {
+ brush_ = brush;
+ Repaint();
+ }
+
+ void TextBlock::SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format)
+ {
+ text_format_ = text_format;
+ RecreateTextLayout();
+ Repaint();
+ }
+
void TextBlock::OnSizeChangedCore(events::SizeChangedEventArgs& args)
{
- CreateTextLayout();
- const auto window = GetWindow();
- if (window != nullptr)
- window->Repaint();
+ RecreateTextLayout();
+ Repaint();
}
void TextBlock::OnDraw(ID2D1DeviceContext* device_context)
@@ -45,34 +51,58 @@ namespace cru
device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get());
}
+ Size TextBlock::OnMeasure(const Size& available_size)
+ {
+
+ //TODO!
+ }
+
void TextBlock::OnTextChangedCore(const String& old_text, const String& new_text)
{
- CreateTextLayout();
- const auto window = GetWindow();
- if (window != nullptr)
- window->Repaint();
+ RecreateTextLayout();
+ Repaint();
}
- void TextBlock::CreateTextLayout()
+ void TextBlock::CreateDefaultBrush()
{
- auto dwrite_factory = graph::GraphManager::GetInstance()->GetDWriteFactory();
+ 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;
+ }
- Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format;
+ void TextBlock::CreateDefaultTextFormat()
+ {
+ const auto dwrite_factory = graph::GraphManager::GetInstance()->GetDWriteFactory();
- dwrite_factory->CreateTextFormat(
+ ThrowIfFailed(dwrite_factory->CreateTextFormat(
L"΅ΘΟί", nullptr,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
24.0, L"zh-cn",
- &text_format
- );
+ &text_format_
+ ));
+ }
+
+ void TextBlock::RecreateTextLayout()
+ {
+ if (text_.empty())
+ {
+ text_layout_ = nullptr;
+ return;
+ }
+
+ const auto dwrite_factory = graph::GraphManager::GetInstance()->GetDWriteFactory();
+
+ if (text_format_ == nullptr)
+ CreateDefaultTextFormat();
const auto&& size = GetSize();
dwrite_factory->CreateTextLayout(
text_.c_str(), text_.size(),
- text_format.Get(),
+ 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 ff1870c1..85bfe90c 100644
--- a/CruUI/ui/controls/text_block.h
+++ b/CruUI/ui/controls/text_block.h
@@ -1,6 +1,5 @@
#pragma once
-#include "ui/ui_base.h"
#include "ui/control.h"
namespace cru
@@ -12,7 +11,10 @@ namespace cru
class TextBlock : public Control
{
public:
- TextBlock();
+ explicit TextBlock(
+ const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format = nullptr,
+ const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush = nullptr
+ );
TextBlock(const TextBlock& other) = delete;
TextBlock(TextBlock&& other) = delete;
TextBlock& operator=(const TextBlock& other) = delete;
@@ -26,19 +28,38 @@ namespace cru
void SetText(const String& text);
+ Microsoft::WRL::ComPtr<ID2D1Brush> GetBrush() const
+ {
+ return brush_;
+ }
+
+ void SetBrush(const Microsoft::WRL::ComPtr<ID2D1Brush>& brush);
+
+ Microsoft::WRL::ComPtr<IDWriteTextFormat> GetTextFormat() const
+ {
+ return text_format_;
+ }
+
+ void SetTextFormat(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& text_format);
+
protected:
void OnSizeChangedCore(events::SizeChangedEventArgs& args) override final;
void OnDraw(ID2D1DeviceContext* device_context) override;
+ Size OnMeasure(const Size& available_size) override;
+
private:
void OnTextChangedCore(const String& old_text, const String& new_text);
- void CreateTextLayout();
+ void CreateDefaultBrush();
+ void CreateDefaultTextFormat();
+ void RecreateTextLayout();
private:
String text_;
Microsoft::WRL::ComPtr<ID2D1Brush> brush_;
+ Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_;
Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_;
};
}
diff --git a/CruUI/ui/window.h b/CruUI/ui/window.h
index 16427a2c..cdc67362 100644
--- a/CruUI/ui/window.h
+++ b/CruUI/ui/window.h
@@ -145,7 +145,7 @@ namespace cru {
void Close();
//Send a repaint message to the window's message queue which may make the window repaint.
- void Repaint();
+ void Repaint() override;
//Show the window.
void Show();