-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathGeometryBackgroundBorder.cpp
419 lines (330 loc) · 14.8 KB
/
GeometryBackgroundBorder.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "GeometryBackgroundBorder.h"
#include "../../Include/RmlUi/Core/Box.h"
#include "../../Include/RmlUi/Core/Math.h"
#include <algorithm>
#include <float.h>
namespace Rml {
GeometryBackgroundBorder::GeometryBackgroundBorder(Vector<Vertex>& vertices, Vector<int>& indices) : vertices(vertices), indices(indices)
{}
void GeometryBackgroundBorder::Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, const Vector2f offset, const Colourb background_color, const Colourb* border_colors)
{
using Edge = Box::Edge;
EdgeSizes border_widths = {
Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::TOP)),
Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::RIGHT)),
Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::BOTTOM)),
Math::RoundFloat(box.GetEdge(Box::BORDER, Edge::LEFT)),
};
int num_borders = 0;
if (border_colors)
{
for (int i = 0; i < 4; i++)
if (border_colors[i].alpha > 0 && border_widths[i] > 0)
num_borders += 1;
}
const Vector2f padding_size = box.GetSize(Box::PADDING).Round();
const bool has_background = (background_color.alpha > 0 && padding_size.x > 0 && padding_size.y > 0);
const bool has_border = (num_borders > 0);
if (!has_background && !has_border)
return;
// -- Find the corner positions --
const Vector2f border_position = offset.Round();
const Vector2f padding_position = border_position + Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]);
const Vector2f border_size = padding_size + Vector2f(border_widths[Edge::LEFT] + border_widths[Edge::RIGHT], border_widths[Edge::TOP] + border_widths[Edge::BOTTOM]);
// Border edge positions
CornerPositions positions_outer = {
border_position,
border_position + Vector2f(border_size.x, 0),
border_position + border_size,
border_position + Vector2f(0, border_size.y)
};
// Padding edge positions
CornerPositions positions_inner = {
padding_position,
padding_position + Vector2f(padding_size.x, 0),
padding_position + padding_size,
padding_position + Vector2f(0, padding_size.y)
};
// -- For curved borders, find the positions to draw ellipses around, and the scaled outer and inner radii --
const float sum_radius = (radii[TOP_LEFT] + radii[TOP_RIGHT] + radii[BOTTOM_RIGHT] + radii[BOTTOM_LEFT]);
const bool has_radius = (sum_radius > 1.f);
// Curved borders are drawn as circles (outer border) and ellipses (inner border) around the centers.
CornerPositions positions_circle_center;
// Radii of the padding edges, 2-dimensional as these can be ellipses.
// The inner radii is effectively the (signed) distance from the circle center to the padding edge.
// They can also be zero or negative, in which case a sharp corner should be drawn instead of an arc.
CornerSizes2 inner_radii;
if (has_radius)
{
// Scale the radii such that we have no overlapping curves.
float scale_factor = FLT_MAX;
scale_factor = Math::Min(scale_factor, padding_size.x / (radii[TOP_LEFT] + radii[TOP_RIGHT])); // Top
scale_factor = Math::Min(scale_factor, padding_size.y / (radii[TOP_RIGHT] + radii[BOTTOM_RIGHT])); // Right
scale_factor = Math::Min(scale_factor, padding_size.x / (radii[BOTTOM_RIGHT] + radii[BOTTOM_LEFT])); // Bottom
scale_factor = Math::Min(scale_factor, padding_size.y / (radii[BOTTOM_LEFT] + radii[TOP_LEFT])); // Left
scale_factor = Math::Min(1.0f, scale_factor);
for (float& radius : radii)
radius = Math::RoundFloat(radius * scale_factor);
// Place the circle/ellipse centers
positions_circle_center = {
positions_outer[TOP_LEFT] + Vector2f(1, 1) * radii[TOP_LEFT],
positions_outer[TOP_RIGHT] + Vector2f(-1, 1) * radii[TOP_RIGHT],
positions_outer[BOTTOM_RIGHT] + Vector2f(-1, -1) * radii[BOTTOM_RIGHT],
positions_outer[BOTTOM_LEFT] + Vector2f(1, -1) * radii[BOTTOM_LEFT]
};
inner_radii = {
Vector2f(radii[TOP_LEFT]) - Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]),
Vector2f(radii[TOP_RIGHT]) - Vector2f(border_widths[Edge::RIGHT], border_widths[Edge::TOP]),
Vector2f(radii[BOTTOM_RIGHT]) - Vector2f(border_widths[Edge::RIGHT], border_widths[Edge::BOTTOM]),
Vector2f(radii[BOTTOM_LEFT]) - Vector2f(border_widths[Edge::LEFT], border_widths[Edge::BOTTOM])
};
}
// -- Generate the geometry --
GeometryBackgroundBorder geometry(vertices, indices);
{
// Reserve geometry. A conservative estimate, does not take border-radii into account and assumes same-colored borders.
const int estimated_num_vertices = 4 * int(has_background) + 2 * num_borders;
const int estimated_num_triangles = 2 * int(has_background) + 2 * num_borders;
vertices.reserve((int)vertices.size() + estimated_num_vertices);
indices.reserve((int)indices.size() + 3 * estimated_num_triangles);
}
// Draw the background
if (has_background)
{
const int offset_vertices = (int)vertices.size();
for (int corner = 0; corner < 4; corner++)
geometry.DrawBackgroundCorner(Corner(corner), positions_inner[corner], positions_circle_center[corner], radii[corner], inner_radii[corner], background_color);
geometry.FillBackground(offset_vertices);
}
// Draw the border
if (has_border)
{
using Edge = Box::Edge;
const int offset_vertices = (int)vertices.size();
const bool draw_edge[4] = {
border_widths[Edge::TOP] > 0 && border_colors[Edge::TOP].alpha > 0,
border_widths[Edge::RIGHT] > 0 && border_colors[Edge::RIGHT].alpha > 0,
border_widths[Edge::BOTTOM] > 0 && border_colors[Edge::BOTTOM].alpha > 0,
border_widths[Edge::LEFT] > 0 && border_colors[Edge::LEFT].alpha > 0
};
const bool draw_corner[4] = {
draw_edge[Edge::TOP] || draw_edge[Edge::LEFT],
draw_edge[Edge::TOP] || draw_edge[Edge::RIGHT],
draw_edge[Edge::BOTTOM] || draw_edge[Edge::RIGHT],
draw_edge[Edge::BOTTOM] || draw_edge[Edge::LEFT]
};
for (int corner = 0; corner < 4; corner++)
{
const Edge edge0 = Edge((corner + 3) % 4);
const Edge edge1 = Edge(corner);
if (draw_corner[corner])
geometry.DrawBorderCorner(Corner(corner), positions_outer[corner], positions_inner[corner], positions_circle_center[corner],
radii[corner], inner_radii[corner], border_colors[edge0], border_colors[edge1]);
if (draw_edge[edge1])
{
RMLUI_ASSERTMSG(draw_corner[corner] && draw_corner[(corner + 1) % 4], "Border edges can only be drawn if both of its connected corners are drawn.");
geometry.FillEdge(edge1 == Edge::LEFT ? offset_vertices : (int)vertices.size());
}
}
}
#if 0
// Debug draw vertices
if (has_radius)
{
const int num_vertices = vertices.size();
const int num_indices = indices.size();
vertices.resize(num_vertices + 4 * num_vertices);
indices.resize(num_indices + 6 * num_indices);
for (int i = 0; i < num_vertices; i++)
{
GeometryUtilities::GenerateQuad(vertices.data() + num_vertices + 4 * i, indices.data() + num_indices + 6 * i, vertices[i].position, Vector2f(3, 3), Colourb(255, 0, (i % 2) == 0 ? 0 : 255), num_vertices + 4 * i);
}
}
#endif
#ifdef RMLUI_DEBUG
const int num_vertices = (int)vertices.size();
for (int index : indices)
{
RMLUI_ASSERT(index < num_vertices);
}
#endif
}
void GeometryBackgroundBorder::DrawBackgroundCorner(Corner corner, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color)
{
if (R == 0 || r.x <= 0 || r.y <= 0)
{
DrawPoint(pos_inner, color);
}
else if (r.x > 0 && r.y > 0)
{
const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
const int num_points = GetNumPoints(R);
DrawArc(pos_circle_center, r, a0, a1, color, color, num_points);
}
}
void GeometryBackgroundBorder::DrawPoint(Vector2f pos, Colourb color)
{
const int offset_vertices = (int)vertices.size();
vertices.resize(offset_vertices + 1);
vertices[offset_vertices].position = pos;
vertices[offset_vertices].colour = color;
}
void GeometryBackgroundBorder::DrawArc(Vector2f pos_center, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points)
{
RMLUI_ASSERT(num_points >= 2 && r.x > 0 && r.y > 0);
const int offset_vertices = (int)vertices.size();
vertices.resize(offset_vertices + num_points);
for (int i = 0; i < num_points; i++)
{
const float t = float(i) / float(num_points - 1);
const float a = Math::Lerp(t, a0, a1);
const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
vertices[offset_vertices + i].position = unit_vector * r + pos_center;
vertices[offset_vertices + i].colour = Math::RoundedLerp(t, color0, color1);
}
}
void GeometryBackgroundBorder::FillBackground(int index_start)
{
const int num_added_vertices = (int)vertices.size() - index_start;
const int offset_indices = (int)indices.size();
const int num_triangles = (num_added_vertices - 2);
indices.resize(offset_indices + 3 * num_triangles);
for (int i = 0; i < num_triangles; i++)
{
indices[offset_indices + 3 * i] = index_start;
indices[offset_indices + 3 * i + 1] = index_start + i + 2;
indices[offset_indices + 3 * i + 2] = index_start + i + 1;
}
}
void GeometryBackgroundBorder::DrawBorderCorner(Corner corner, Vector2f pos_outer, Vector2f pos_inner, Vector2f pos_circle_center, float R, Vector2f r, Colourb color0, Colourb color1)
{
const float a0 = float((int)corner + 2) * 0.5f * Math::RMLUI_PI;
const float a1 = float((int)corner + 3) * 0.5f * Math::RMLUI_PI;
if (R == 0)
{
DrawPointPoint(pos_outer, pos_inner, color0, color1);
}
else if (r.x > 0 && r.y > 0)
{
DrawArcArc(pos_circle_center, R, r, a0, a1, color0, color1, GetNumPoints(R));
}
else
{
DrawArcPoint(pos_circle_center, pos_inner, R, a0, a1, color0, color1, GetNumPoints(R));
}
}
void GeometryBackgroundBorder::DrawPointPoint(Vector2f pos_outer, Vector2f pos_inner, Colourb color0, Colourb color1)
{
const bool different_color = (color0 != color1);
vertices.reserve((int)vertices.size() + (different_color ? 4 : 2));
DrawPoint(pos_inner, color0);
DrawPoint(pos_outer, color0);
if (different_color)
{
DrawPoint(pos_inner, color1);
DrawPoint(pos_outer, color1);
}
}
void GeometryBackgroundBorder::DrawArcArc(Vector2f pos_center, float R, Vector2f r, float a0, float a1, Colourb color0, Colourb color1, int num_points)
{
RMLUI_ASSERT(num_points >= 2 && R > 0 && r.x > 0 && r.y > 0);
const int num_triangles = 2 * (num_points - 1);
const int offset_vertices = (int)vertices.size();
const int offset_indices = (int)indices.size();
vertices.resize(offset_vertices + 2 * num_points);
indices.resize(offset_indices + 3 * num_triangles);
for (int i = 0; i < num_points; i++)
{
const float t = float(i) / float(num_points - 1);
const float a = Math::Lerp(t, a0, a1);
const Colourb color = Math::RoundedLerp(t, color0, color1);
const Vector2f unit_vector(Math::Cos(a), Math::Sin(a));
vertices[offset_vertices + 2 * i].position = unit_vector * r + pos_center;
vertices[offset_vertices + 2 * i].colour = color;
vertices[offset_vertices + 2 * i + 1].position = unit_vector * R + pos_center;
vertices[offset_vertices + 2 * i + 1].colour = color;
}
for (int i = 0; i < num_triangles; i += 2)
{
indices[offset_indices + 3 * i + 0] = offset_vertices + i + 0;
indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
indices[offset_indices + 3 * i + 3] = offset_vertices + i + 1;
indices[offset_indices + 3 * i + 4] = offset_vertices + i + 2;
indices[offset_indices + 3 * i + 5] = offset_vertices + i + 3;
}
}
void GeometryBackgroundBorder::DrawArcPoint(Vector2f pos_center, Vector2f pos_inner, float R, float a0, float a1, Colourb color0, Colourb color1, int num_points)
{
RMLUI_ASSERT(R > 0 && num_points >= 2);
const int offset_vertices = (int)vertices.size();
vertices.reserve(offset_vertices + num_points + 2);
// Generate the vertices. We could also split the arc mid-way to create a sharp color transition.
DrawPoint(pos_inner, color0);
DrawArc(pos_center, Vector2f(R), a0, a1, color0, color1, num_points);
DrawPoint(pos_inner, color1);
RMLUI_ASSERT((int)vertices.size() - offset_vertices == num_points + 2);
// Swap the last two vertices such that the outer edge vertex is last, see the comment for the border drawing functions. Their colors should already be the same.
const int last_vertex = (int)vertices.size() - 1;
std::swap(vertices[last_vertex - 1].position, vertices[last_vertex].position);
// Generate the indices
const int num_triangles = (num_points - 1);
const int i_vertex_inner0 = offset_vertices;
const int i_vertex_inner1 = last_vertex - 1;
const int offset_indices = (int)indices.size();
indices.resize(offset_indices + 3 * num_triangles);
for (int i = 0; i < num_triangles; i++)
{
indices[offset_indices + 3 * i + 0] = (i > num_triangles / 2 ? i_vertex_inner1 : i_vertex_inner0);
indices[offset_indices + 3 * i + 1] = offset_vertices + i + 2;
indices[offset_indices + 3 * i + 2] = offset_vertices + i + 1;
}
// Since we swapped the last two vertices we also need to change the last triangle.
indices[offset_indices + 3 * (num_triangles - 1) + 1] = last_vertex;
}
void GeometryBackgroundBorder::FillEdge(int index_next_corner)
{
const int offset_indices = (int)indices.size();
const int num_vertices = (int)vertices.size();
RMLUI_ASSERT(num_vertices >= 2);
indices.resize(offset_indices + 6);
indices[offset_indices + 0] = num_vertices - 2;
indices[offset_indices + 1] = index_next_corner;
indices[offset_indices + 2] = num_vertices - 1;
indices[offset_indices + 3] = num_vertices - 1;
indices[offset_indices + 4] = index_next_corner;
indices[offset_indices + 5] = index_next_corner + 1;
}
int GeometryBackgroundBorder::GetNumPoints(float R) const
{
return Math::Clamp(3 + Math::RoundToInteger(R / 6.f), 2, 100);
}
} // namespace Rml