aboutsummaryrefslogtreecommitdiff
path: root/CruUI/ui/controls/text_block.cpp
blob: 739cd793b9b0054923e6e6ac12860128cd26fcc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "text_block.h"

#include <chrono>

#include "ui/window.h"
#include "graph/graph.h"
#include "exception.h"
#include "debug_base.h"

namespace cru
{
    namespace ui
    {
        namespace controls
        {
            inline Microsoft::WRL::ComPtr<IDWriteFactory> GetDWriteFactory()
            {
                return graph::GraphManager::GetInstance()->GetDWriteFactory();
            }

            TextBlock::TextBlock(const Microsoft::WRL::ComPtr<IDWriteTextFormat>& init_text_format,
                const Microsoft::WRL::ComPtr<ID2D1Brush>& init_brush) : Control(false)
            {
                text_format_ = init_text_format;
                if (init_brush == nullptr)
                    CreateDefaultBrush();
            }

            TextBlock::~TextBlock() = default;

            void TextBlock::SetText(const String& text)
            {
                if (text_ != text)
                {
                    const auto old_text = text_;
                    text_ = text;
                    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)
            {
                text_layout_->SetMaxWidth(args.GetNewSize().width);
                text_layout_->SetMaxHeight(args.GetNewSize().height);
                Repaint();
                Control::OnSizeChangedCore(args);
            }

            void TextBlock::OnDraw(ID2D1DeviceContext* device_context)
            {
                if (text_layout_ != nullptr)
                    device_context->DrawTextLayout(D2D1::Point2F(), text_layout_.Get(), brush_.Get());
            }

            void TextBlock::OnMouseDownCore(events::MouseButtonEventArgs& args)
            {
                if (args.GetMouseButton() == MouseButton::Left)
                {
                    BOOL is_trailing, is_inside;
                    DWRITE_HIT_TEST_METRICS metrics;
                    text_layout_->HitTestPoint(args.GetPoint(this).x, args.GetPoint(this).y, &is_trailing, &is_inside, &metrics);
                    if (is_inside)
                    {
                        mouse_down_position_ = is_trailing == 0 ? metrics.textPosition : metrics.textPosition + 1;
                        is_mouse_down_ = true;
                    }
                }
                Control::OnMouseDownCore(args);
            }

            Size TextBlock::OnMeasure(const Size& available_size)
            {
                if (text_.empty())
                    return Size::zero;

                const auto layout_params = GetLayoutParams();

                if (layout_params->width.mode == MeasureMode::Stretch && layout_params->height.mode == MeasureMode::Stretch)
                    return available_size;

                auto&& get_measure_length = [](const MeasureLength& layout_length, const float available_length) -> float
                {
                    switch (layout_length.mode)
                    {
                    case MeasureMode::Exactly:
                    {
                        return std::min(layout_length.length, available_length);
                    }
                    case MeasureMode::Stretch:
                    case MeasureMode::Content:
                    {
                        return available_length;
                    }
                    default:
                        UnreachableCode();
                    }
                };

                const Size measure_size(get_measure_length(layout_params->width, available_size.width),
                    get_measure_length(layout_params->height, available_size.height));

                ThrowIfFailed(text_layout_->SetMaxWidth(measure_size.width));
                ThrowIfFailed(text_layout_->SetMaxHeight(measure_size.height));

                DWRITE_TEXT_METRICS metrics{};

                ThrowIfFailed(text_layout_->GetMetrics(&metrics));

                const Size measure_result(metrics.width, metrics.height);

                auto&& calculate_final_length = [](const MeasureLength& layout_length, const float measure_length, const float measure_result_length) -> float
                {
                    if ((layout_length.mode == MeasureMode::Stretch ||
                        layout_length.mode == MeasureMode::Exactly)
                        && measure_result_length < measure_length)
                        return measure_length;
                    else
                        return measure_result_length;
                };

                const Size result_size(
                    calculate_final_length(layout_params->width, measure_size.width, measure_result.width),
                    calculate_final_length(layout_params->height, measure_size.height, measure_result.height)
                );

                return result_size;
            }

            void TextBlock::OnTextChangedCore(const String& old_text, const String& new_text)
            {
                RecreateTextLayout();
                Repaint();
            }

            void TextBlock::CreateDefaultBrush()
            {
                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;
            }

            void TextBlock::CreateDefaultTextFormat()
            {
                const auto dwrite_factory = GetDWriteFactory();

                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_
                ));
            }

            void TextBlock::RecreateTextLayout()
            {
                if (text_.empty())
                {
                    text_layout_ = nullptr;
                    return;
                }

                const auto dwrite_factory = GetDWriteFactory();

                if (text_format_ == nullptr)
                    CreateDefaultTextFormat();

                const auto&& size = GetSize();

                ThrowIfFailed(dwrite_factory->CreateTextLayout(
                    text_.c_str(), static_cast<UINT32>(text_.size()),
                    text_format_.Get(),
                    size.width, size.height,
                    &text_layout_
                ));

                std::for_each(text_layout_handlers_.cbegin(), text_layout_handlers_.cend(), [this](const std::shared_ptr<TextLayoutHandler>& handler)
                {
                    (*handler)(text_layout_);
                });
            }
        }
    }
}