-
Notifications
You must be signed in to change notification settings - Fork 47
/
ui_bd_renderer.cpp
365 lines (339 loc) · 11.4 KB
/
ui_bd_renderer.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// ui
#include <style/ui_style.h>
#include <core/ui_manager.h>
#include <core/ui_basic_type.h>
#include <core/ui_color_list.h>
#include <util/ui_little_math.h>
#include <graphics/ui_bd_renderer.h>
#include <graphics/ui_graphics_impl.h>
// resource
#include <effect/ui_effect_borderimage.h>
#include <resource/ui_image_res.h>
// c++
#include <cassert>
#ifndef LUI_DISABLE_STYLE_SUPPORT
extern "C" const GUID GUID_LongUIEffect_BorderImage;
/// <summary>
/// Initializes a new instance of the <see cref="CUIRendererBorder"/> class.
/// </summary>
LongUI::CUIRendererBorder::CUIRendererBorder() noexcept {
}
/// <summary>
/// Finalizes an instance of the <see cref="CUIRendererBorder"/> class.
/// </summary>
/// <returns></returns>
LongUI::CUIRendererBorder::~CUIRendererBorder() noexcept {
// 需要释放设备资源
this->ReleaseDeviceData();
}
/// <summary>
/// Releases the device data.
/// </summary>
void LongUI::CUIRendererBorder::ReleaseDeviceData() noexcept {
// 先释放
this->release_effect();
}
/// <summary>
/// Releases the brush.
/// </summary>
/// <returns></returns>
void LongUI::CUIRendererBorder::release_effect() noexcept {
UIManager.TSReleaseCOM(LUI_OBJECT_TO_COM(m_pBorder));
UIManager.TSReleaseCOM(LUI_OBJECT_TO_COM(m_pOutput));
m_pBorder = nullptr;
m_pOutput = nullptr;
}
/// <summary>
/// Sets the image identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
void LongUI::CUIRendererBorder::SetImageId(uintptr_t id) noexcept {
#ifndef NDEBUG
if (m_idImage.GetId() == id) {
LUIDebug(Warning)
<< "set same image id: "
<< reinterpret_cast<void*>(id)
<< endl;
}
#endif
m_idImage.SetId(id);
// 将数据延迟到渲染时处理避免线程隐患
m_bIdChanged = true;
#if 0
// 没有就忽略
if (!m_pBorder) return;
// XXX: 错误处理
const auto code = this->refresh_image();
assert(code && "TODO: ERROR HANDLE");
#endif
}
/// <summary>
/// Sets the image slice.
/// </summary>
/// <param name="rect">The rect.</param>
/// <param name="fill">if set to <c>true</c> [fill].</param>
/// <returns></returns>
void LongUI::CUIRendererBorder::SetImageSlice(const RectF& rect, bool fill) noexcept {
m_rcSlice = rect;
m_bSliceFill = fill;
m_bLayoutChanged = true;
}
/// <summary>
/// Sets the image repeat.
/// </summary>
/// <param name="repeat">The repeat.</param>
/// <returns></returns>
void LongUI::CUIRendererBorder::SetImageRepeat(AttributeRepeat repeat) noexcept {
m_repeat = repeat;
m_bLayoutChanged = true;
}
/// <summary>
/// Refreshes the image.
/// </summary>
/// <returns></returns>
auto LongUI::CUIRendererBorder::refresh_image() noexcept -> Result {
m_szImage = {};
using effect_t = ID2D1Effect;
if (!m_idImage.GetId()) return { Result::RS_FALSE };
Result hr = { Result::RS_OK };
// 没有就创建
if (!m_pBorder && m_idImage.GetId()) {
auto& effect = reinterpret_cast<effect_t*&>(m_pBorder);
auto& guid = GUID_LongUIEffect_BorderImage;
hr.code = UIManager.Ref2DRenderer().CreateEffect(guid, &effect) ;
}
// 更新输入
if (m_pBorder) {
auto& res = m_idImage.RefResource();
assert(res.RefData().GetType() == ResourceType::Type_Image);
auto& img = static_cast<CUIImage&>(res);
// 检查大小
auto& frame = img.RefFrame(0);
const auto realsize = frame.bitmap->GetSize();
const auto fw = static_cast<float>(realsize.width);
const auto fh = static_cast<float>(realsize.height);
if (fw != m_szImage.width || fh != m_szImage.height)
m_bLayoutChanged = true;
//m_szImage = { realsize.width, realsize.height };
m_szImage = img.size;
m_szRatio.width = img.size.width / realsize.width;
m_szRatio.height = img.size.height / realsize.height;
m_rcSource.left = frame.source.left / realsize.width;
m_rcSource.top = frame.source.top / realsize.height;
m_rcSource.right = frame.source.right / realsize.width;
m_rcSource.bottom = frame.source.bottom / realsize.height;
// 设置输入
m_pBorder->SetInput(0, frame.bitmap);
// 获取输出
if (!m_pOutput) m_pBorder->GetOutput(AddrOf(m_pOutput));
}
return hr;
}
/// <summary>
/// Refreshes the real slice.
/// </summary>
/// <returns></returns>
void LongUI::CUIRendererBorder::refresh_real_slice() noexcept {
// LEFT
if (impl::is_percent_value(m_rcSlice.left))
m_rcRealSlice.left = impl::get_percent_value(m_rcSlice.left);
else
m_rcRealSlice.left = m_rcSlice.left / m_szImage.width;
// TOP
if (impl::is_percent_value(m_rcSlice.top))
m_rcRealSlice.top = impl::get_percent_value(m_rcSlice.top);
else
m_rcRealSlice.top = m_rcSlice.top / m_szImage.height;
// RIGHT
if (impl::is_percent_value(m_rcSlice.right))
m_rcRealSlice.right = impl::get_percent_value(m_rcSlice.right);
else
m_rcRealSlice.right = m_rcSlice.right / m_szImage.width;
// BOTTOM
if (impl::is_percent_value(m_rcSlice.bottom))
m_rcRealSlice.bottom = impl::get_percent_value(m_rcSlice.bottom);
else
m_rcRealSlice.bottom = m_rcSlice.bottom / m_szImage.height;
}
/// <summary>
/// Befores the render.
/// </summary>
/// <returns></returns>
void LongUI::CUIRendererBorder::BeforeRender(const Box& box) noexcept {
if (m_pBorder) {
// XXX: m_bIdChanged 应该用原子类型保证数据安全?
if (m_bIdChanged) {
m_bIdChanged = false;
this->refresh_image();
// XXX: 错误处理
const auto code = this->refresh_image();
assert(code && "TODO: ERROR HANDLE");
}
if (m_bLayoutChanged) {
this->refresh_real_slice();
this->refresh_draw_count();
this->refresh_image_matrix(box);
}
}
m_bLayoutChanged = false;
}
/// <summary>
/// Renders the default border.
/// </summary>
/// <param name="box">The box.</param>
/// <returns></returns>
void LongUI::CUIRendererBorder::render_default_border(const Box& box) const noexcept {
if (this->style == Style_None) return;
D2D1_ROUNDED_RECT rrect;
box.GetBorderEdge(auto_cast(rrect.rect));
rrect.radiusX = this->radius_x;
rrect.radiusY = this->radius_y;
auto& renderer = UIManager.Ref2DRenderer();
auto& brush = UIManager.RefCCBrush(this->color);
renderer.DrawRoundedRectangle(
&rrect,
&brush,
box.border.left
);
}
/// <summary>
/// Recreates this instance.
/// </summary>
/// <returns></returns>
auto LongUI::CUIRendererBorder::CreateDeviceData() noexcept -> Result {
assert(m_pBorder == nullptr && "must release first");
assert(m_pOutput == nullptr && "must release first");
// 创建笔刷
return this->refresh_image();
}
/// <summary>
/// Calculates the repeat.
/// </summary>
/// <param name="rect">The rect.</param>
/// <param name="box">The box.</param>
/// <param name="size">The size.</param>
/// <returns></returns>
void LongUI::CUIRendererBorder::calculate_repeat(
RectF& rect, const Box& box, Size2F size) const noexcept {
Size2F img_center_size = {
m_szImage.width * (1.f - m_rcRealSlice.left - m_rcRealSlice.right),
m_szImage.height * (1.f - m_rcRealSlice.top - m_rcRealSlice.bottom)
};
RectF img_scale = {
m_szImage.height * m_rcRealSlice.top / box.border.top,
m_szImage.width * m_rcRealSlice.left / box.border.left,
m_szImage.height * m_rcRealSlice.bottom / box.border.bottom,
m_szImage.width * m_rcRealSlice.right / box.border.right
};
Size2F bd_center_size = {
size.width - box.border.left - box.border.right,
size.height - box.border.top - box.border.bottom
};
rect.left = bd_center_size.width / img_center_size.width * img_scale.left;
rect.top = bd_center_size.height / img_center_size.height * img_scale.top;
rect.right = bd_center_size.width / img_center_size.width * img_scale.right;
rect.bottom = bd_center_size.height / img_center_size.height * img_scale.bottom;
// 计算X-REPEAT
switch (m_repeat & 0x0f)
{
case Repeat_Space:
case Repeat_Round:
rect.left = LongUI::RoundInGuiLevel(rect.left);
rect.right = LongUI::RoundInGuiLevel(rect.right);
break;
case Repeat_Stretch:
rect.left = 1.f;
rect.right = 1.f;
break;
}
// 计算Y-REPEAT
switch (m_repeat >> 4)
{
case Repeat_Repeat:
break;
case Repeat_Space:
case Repeat_Round:
rect.top = LongUI::RoundInGuiLevel(rect.top);
rect.bottom = LongUI::RoundInGuiLevel(rect.bottom);
break;
case Repeat_Stretch:
rect.top = 1.f;
rect.bottom = 1.f;
break;
}
// RATIO
//rect.left *= m_szRatio.width;
//rect.top *= m_szRatio.height;
//rect.right *= m_szRatio.width;
//rect.bottom *= m_szRatio.height;
}
/// <summary>
/// Renders the border.
/// </summary>
/// <param name="box">The box.</param>
/// <returns></returns>
void LongUI::CUIRendererBorder::RenderBorder(const Box& box) const noexcept {
assert(m_bLayoutChanged == false);
// 渲染默认风格
if (!m_idImage.GetId()) return this->render_default_border(box);
// 渲染边框图片
if (!m_pOutput) return;
// 记录
const auto border_rect = box.GetBorderEdge();
auto& renderer = UIManager.Ref2DRenderer();
// 正式渲染
Point2F offset = { border_rect.left, border_rect.top };
renderer.DrawImage(m_pOutput, &auto_cast(offset));
}
/// <summary>
/// Refreshes the image matrix.
/// </summary>
/// <param name="box">The box.</param>
/// <param name="size">The size.</param>
/// <returns></returns>
void LongUI::CUIRendererBorder::refresh_image_matrix(const Box& box) noexcept {
const auto border_rect = box.GetBorderEdge();
//LUIDebug(Hint) << this << border_rect << endl;
const Size2F size{
border_rect.right - border_rect.left,
border_rect.bottom - border_rect.top
};
// 更新cbuffer
Effect::BorderImageMatrix matrix;
// 目标:
matrix.zone0.left = box.border.left;
matrix.zone0.top = box.border.top;
matrix.zone1.left = size.width - box.border.left - box.border.right;
matrix.zone1.top = size.height - box.border.top - box.border.bottom;
matrix.zone2.left = box.border.right;
matrix.zone2.top = box.border.bottom;
matrix.zone3.left = size.width;
matrix.zone3.top = size.height;
// 支持图片中一小部分
matrix.zone0.right = m_rcSource.left;
matrix.zone0.bottom = m_rcSource.top;
matrix.zone1.right = m_rcSource.left + m_rcRealSlice.left * m_szRatio.width;
matrix.zone1.bottom = m_rcSource.top + m_rcRealSlice.top* m_szRatio.height;
matrix.zone2.right = m_rcSource.right - m_rcRealSlice.right* m_szRatio.width;
matrix.zone2.bottom = m_rcSource.bottom - m_rcRealSlice.bottom* m_szRatio.height;
matrix.zone3.right = m_rcSource.right;
matrix.zone3.bottom = m_rcSource.bottom;
// 重复
matrix.repeat = { 1.f, 1.f, 1.f, 1.f };
this->calculate_repeat(luiref matrix.repeat, box, size);
m_pBorder->SetValue(Effect::BImage_Matrix, matrix);
}
/// <summary>
/// Refreshes the drawcount.
/// </summary>
/// <returns></returns>
void LongUI::CUIRendererBorder::refresh_draw_count() noexcept {
// FILL
const uint32_t draw_count = m_bSliceFill
? Effect::VERTEX_FULLCOUNT
: Effect::VERTEX_NOFILLCOUNT
;
m_pBorder->SetValue(Effect::BImage_Draw, draw_count);
}
#endif