aboutsummaryrefslogtreecommitdiff
path: root/CruUI/ui/controls/text_block.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'CruUI/ui/controls/text_block.cpp')
-rw-r--r--CruUI/ui/controls/text_block.cpp84
1 files changed, 57 insertions, 27 deletions
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_
);