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
|
#include "cru/ui/render/border_render_object.hpp"
#include "cru/platform/debug.hpp"
#include "cru/platform/graph/geometry.hpp"
#include "cru/platform/graph/graph_factory.hpp"
#include "cru/platform/graph/painter_util.hpp"
#include <algorithm>
#include <cassert>
namespace cru::ui::render {
BorderRenderObject::BorderRenderObject(
std::shared_ptr<platform::graph::Brush> brush) {
assert(brush);
this->border_brush_ = std::move(brush);
RecreateGeometry();
}
void BorderRenderObject::Draw(platform::graph::Painter* painter) {
painter->FillGeometry(geometry_.get(), border_brush_.get());
if (const auto child = GetChild()) {
auto offset = child->GetOffset();
platform::graph::util::WithTransform(
painter, platform::Matrix::Translation(offset.x, offset.y),
[child](auto p) { child->Draw(p); });
}
}
RenderObject* BorderRenderObject::HitTest(const Point& point) {
if (const auto child = GetChild()) {
auto offset = child->GetOffset();
Point p{point.x - offset.x, point.y - offset.y};
const auto result = child->HitTest(point);
if (result != nullptr) {
return result;
}
}
if (is_enabled_) {
const auto contains =
border_outer_geometry_->FillContains(Point{point.x, point.y});
return contains ? this : nullptr;
} else {
const auto margin = GetMargin();
const auto size = GetSize();
return Rect{margin.left, margin.top,
std::max(size.width - margin.GetHorizontalTotal(), 0.0f),
std::max(size.height - margin.GetVerticalTotal(), 0.0f)}
.IsPointInside(point)
? this
: nullptr;
}
}
void BorderRenderObject::OnAddChild(RenderObject* new_child, int position) {
assert(GetChildren().size() == 1);
}
void BorderRenderObject::OnSizeChanged(const Size& old_size,
const Size& new_size) {
RecreateGeometry();
}
void BorderRenderObject::OnMeasureCore(const Size& available_size) {
const auto margin = GetMargin();
const auto padding = GetPadding();
Size margin_border_padding_size{
margin.GetHorizontalTotal() + padding.GetHorizontalTotal(),
margin.GetVerticalTotal() + padding.GetVerticalTotal()};
if (is_enabled_) {
margin_border_padding_size.width += border_thickness_.GetHorizontalTotal();
margin_border_padding_size.height += border_thickness_.GetVerticalTotal();
}
auto coerced_margin_border_padding_size = margin_border_padding_size;
if (coerced_margin_border_padding_size.width > available_size.width) {
platform::DebugMessage(
L"Measure: horizontal length of padding, border and margin is bigger "
L"than available length.");
coerced_margin_border_padding_size.width = available_size.width;
}
if (coerced_margin_border_padding_size.height > available_size.height) {
platform::DebugMessage(
L"Measure: vertical length of padding, border and margin is bigger "
L"than available length.");
coerced_margin_border_padding_size.height = available_size.height;
}
const auto coerced_content_available_size =
available_size - coerced_margin_border_padding_size;
const auto actual_content_size =
OnMeasureContent(coerced_content_available_size);
SetPreferredSize(coerced_margin_border_padding_size + actual_content_size);
}
void BorderRenderObject::OnLayoutCore(const Rect& rect) {
const auto margin = GetMargin();
const auto padding = GetPadding();
Size margin_border_padding_size{
margin.GetHorizontalTotal() + padding.GetHorizontalTotal(),
margin.GetVerticalTotal() + padding.GetVerticalTotal()};
if (is_enabled_) {
margin_border_padding_size.width += border_thickness_.GetHorizontalTotal();
margin_border_padding_size.height += border_thickness_.GetVerticalTotal();
}
const auto content_available_size =
rect.GetSize() - margin_border_padding_size;
auto coerced_content_available_size = content_available_size;
if (coerced_content_available_size.width < 0) {
platform::DebugMessage(
L"Layout: horizontal length of padding, border and margin is bigger "
L"than available length.");
coerced_content_available_size.width = 0;
}
if (coerced_content_available_size.height < 0) {
platform::DebugMessage(
L"Layout: vertical length of padding, border and margin is bigger "
L"than "
L"available length.");
coerced_content_available_size.height = 0;
}
OnLayoutContent(Rect{
margin.left + (is_enabled_ ? border_thickness_.left : 0) + padding.left,
margin.top + (is_enabled_ ? border_thickness_.top : 0) + padding.top,
coerced_content_available_size.width,
coerced_content_available_size.height});
}
Size BorderRenderObject::OnMeasureContent(const Size& available_size) {
const auto child = GetChild();
if (child) {
child->Measure(available_size);
return child->GetPreferredSize();
} else {
return Size{};
}
}
void BorderRenderObject::OnLayoutContent(const Rect& content_rect) {
const auto child = GetChild();
if (child) {
child->Layout(content_rect);
}
}
void BorderRenderObject::RecreateGeometry() {
geometry_.reset();
border_outer_geometry_.reset();
auto f = [](platform::graph::GeometryBuilder* builder, const Rect& rect,
const CornerRadius& corner) {
builder->BeginFigure(Point(rect.left + corner.left_top.x, rect.top));
builder->LineTo(Point(rect.GetRight() - corner.right_top.x, rect.top));
builder->QuadraticBezierTo(
Point(rect.GetRight(), rect.top),
Point(rect.GetRight(), rect.top + corner.right_top.y));
builder->LineTo(
Point(rect.GetRight(), rect.GetBottom() - corner.right_bottom.y));
builder->QuadraticBezierTo(
Point(rect.GetRight(), rect.GetBottom()),
Point(rect.GetRight() - corner.right_bottom.x, rect.GetBottom()));
builder->LineTo(Point(rect.left + corner.left_bottom.x, rect.GetBottom()));
builder->QuadraticBezierTo(
Point(rect.left, rect.GetBottom()),
Point(rect.left, rect.GetBottom() - corner.left_bottom.y));
builder->LineTo(Point(rect.left, rect.top + corner.left_top.y));
builder->QuadraticBezierTo(Point(rect.left, rect.top),
Point(rect.left + corner.left_top.x, rect.top));
builder->CloseFigure(true);
};
const auto size = GetSize();
const auto margin = GetMargin();
const Rect outer_rect{margin.left, margin.top,
size.width - margin.GetHorizontalTotal(),
size.height - margin.GetVerticalTotal()};
const auto graph_factory = platform::graph::GraphFactory::GetInstance();
std::unique_ptr<platform::graph::GeometryBuilder> builder{
graph_factory->CreateGeometryBuilder()};
f(builder.get(), outer_rect, corner_radius_);
border_outer_geometry_.reset(builder->Build());
builder.reset();
const Rect inner_rect = outer_rect.Shrink(border_thickness_);
builder.reset(graph_factory->CreateGeometryBuilder());
f(builder.get(), outer_rect, corner_radius_);
f(builder.get(), inner_rect, corner_radius_);
geometry_.reset(builder->Build());
builder.reset();
}
} // namespace cru::ui::render
|