File: RocketDecorators.cpp

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (248 lines) | stat: -rw-r--r-- 8,995 bytes parent folder | download
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
#include "RocketDecorators.h"

#include <Rocket/Core/RenderInterface.h>
#include <Rocket/Core/Vertex.h>

namespace scpui {
namespace decorators {

// ================================
// Element Underline Implementation
// ================================

DecoratorUnderline::DecoratorUnderline(float thickness, LineStyle style, float length, float space, Rocket::Core::Colourb color, bool element_color)
	: line_thickness(thickness), line_style(style), line_length(length), line_space(space), line_color(color), use_element_color(element_color)
{
}

DecoratorUnderline::~DecoratorUnderline() = default;

// Generate per-element data, if needed (return NULL if not needed)
Rocket::Core::DecoratorDataHandle DecoratorUnderline::GenerateElementData(Rocket::Core::Element* /*element*/)
{
	return 0; // No per-element data needed
}

// Release any element-specific data
void DecoratorUnderline::ReleaseElementData(Rocket::Core::DecoratorDataHandle /*element_data*/)
{
	// No element-specific data to release in this case
}

void DecoratorUnderline::RenderElement(Rocket::Core::Element* element,
	Rocket::Core::DecoratorDataHandle /*element_data*/)
{
	// Get the size of the element's content area
	Rocket::Core::Vector2f element_size = element->GetBox().GetSize(Rocket::Core::Box::CONTENT);

	// Determine line rendering properties based on style
	float dash_length = 5.0f;
	float space_length = 3.0f;

	switch (line_style) {
	case LineStyle::Dotted:
		dash_length = line_thickness; // Dotted lines are small circles
		space_length = line_space;
		break;
	case LineStyle::Dashed:
		space_length = line_space;
		dash_length = line_length;
		break;
	case LineStyle::Solid:
	default:
		space_length = 0; // No space for solid lines
		break;
	}

	// Calculate the starting position (bottom-left of the content area)
	Rocket::Core::Vector2f start = element->GetAbsoluteOffset(Rocket::Core::Box::CONTENT);
	float x_position = start.x;
	float y_position = start.y + element_size.y - (line_thickness * 2); // Adjusted y-position using the thickness

	Rocket::Core::RenderInterface* render_interface = element->GetRenderInterface();

	// Create space for vertices and indices for all dashes
	std::vector<Rocket::Core::Vertex> vertices;
	std::vector<int> indices;

	int vertex_count = 0;

	Rocket::Core::Colourb render_color = line_color;
	if (use_element_color) {
		render_color = element->GetProperty<Rocket::Core::Colourb>("color");
	}

	// Loop through to create each dash as a rectangle (or dot)
	while (x_position < start.x + element_size.x) {
		float dash_end_x = std::min(x_position + dash_length, start.x + element_size.x);

		// Create four vertices for the rectangle (two triangles)
		Rocket::Core::Vertex top_left, top_right, bottom_left, bottom_right;

		// Set vertex positions
		top_left.position = Rocket::Core::Vector2f(x_position, y_position);
		top_right.position = Rocket::Core::Vector2f(dash_end_x, y_position);
		bottom_left.position = Rocket::Core::Vector2f(x_position, y_position + line_thickness);
		bottom_right.position = Rocket::Core::Vector2f(dash_end_x, y_position + line_thickness);

		// Set the color
		top_left.colour = render_color;
		top_right.colour = render_color;
		bottom_left.colour = render_color;
		bottom_right.colour = render_color;

		// Set texture coordinates to 0 since we have no texture
		top_left.tex_coord = Rocket::Core::Vector2f(0, 0);
		top_right.tex_coord = Rocket::Core::Vector2f(0, 0);
		bottom_left.tex_coord = Rocket::Core::Vector2f(0, 0);
		bottom_right.tex_coord = Rocket::Core::Vector2f(0, 0);

		// Add the vertices to the list
		vertices.push_back(top_left);
		vertices.push_back(top_right);
		vertices.push_back(bottom_left);
		vertices.push_back(bottom_right);

		// Add indices for two triangles
		indices.push_back(vertex_count); // First triangle
		indices.push_back(vertex_count + 1);
		indices.push_back(vertex_count + 2);

		indices.push_back(vertex_count + 1); // Second triangle
		indices.push_back(vertex_count + 3);
		indices.push_back(vertex_count + 2);

		// Move to the next dash position
		x_position += dash_length + space_length;

		// Increment vertex count for the next set of triangles
		vertex_count += 4;
	}

	// Render the geometry
	render_interface->RenderGeometry(vertices.data(),
		(int)vertices.size(),
		indices.data(),
		(int)indices.size(),
		0,
		Rocket::Core::Vector2f(0, 0));
}

// =============================
// Corner Borders Implementation
// =============================

DecoratorCornerBorders::DecoratorCornerBorders(float thickness, float length_h, float length_v, Rocket::Core::Colourb color)
	: border_thickness(thickness), border_length_h(length_h), border_length_v(length_v), border_color(color)
{
}

DecoratorCornerBorders::~DecoratorCornerBorders() = default;

// Generate per-element data, if needed (return NULL if not needed)
Rocket::Core::DecoratorDataHandle DecoratorCornerBorders::GenerateElementData(Rocket::Core::Element* /*element*/)
{
	return 0; // No per-element data needed
}

// Release any element-specific data
void DecoratorCornerBorders::ReleaseElementData(Rocket::Core::DecoratorDataHandle /*element_data*/)
{
	// No element-specific data to release in this case
}

void DecoratorCornerBorders::RenderElement(Rocket::Core::Element* element,
	Rocket::Core::DecoratorDataHandle /*element_data*/)
{
	// Get the size of the element's content area
	Rocket::Core::Vector2f element_size = element->GetBox().GetSize(Rocket::Core::Box::CONTENT);
	Rocket::Core::Vector2f start = element->GetAbsoluteOffset(Rocket::Core::Box::CONTENT);

	Rocket::Core::RenderInterface* render_interface = element->GetRenderInterface();

	// Create space for vertices and indices for the corner borders
	std::vector<Rocket::Core::Vertex> vertices;
	std::vector<int> indices;

	int vertex_count = 0;

	// Define the four corner positions, moved inward by thickness
	Rocket::Core::Vector2f top_left = start + Rocket::Core::Vector2f(border_thickness, border_thickness);
	Rocket::Core::Vector2f top_right =
		start + Rocket::Core::Vector2f(element_size.x - border_thickness, border_thickness);
	Rocket::Core::Vector2f bottom_left =
		start + Rocket::Core::Vector2f(border_thickness, element_size.y - border_thickness);
	Rocket::Core::Vector2f bottom_right =
		start + Rocket::Core::Vector2f(element_size.x - border_thickness, element_size.y - border_thickness);

	// Function to add a line segment as two triangles
	auto add_line_segment = [&](const Rocket::Core::Vector2f& start_pos, const Rocket::Core::Vector2f& end_pos, float thickness) {
			// Vector direction of the line
			Rocket::Core::Vector2f direction = end_pos - start_pos;
			// Normalize direction and calculate the perpendicular vector for thickness
			Rocket::Core::Vector2f perpendicular =
				Rocket::Core::Vector2f(-direction.y, direction.x).Normalise() * thickness;

			// Define the four vertices of the line as a rectangle (two triangles)
			Rocket::Core::Vertex v1, v2, v3, v4;
			v1.position = start_pos - perpendicular;
			v2.position = start_pos + perpendicular;
			v3.position = end_pos - perpendicular;
			v4.position = end_pos + perpendicular;

			// Set color
			v1.colour = border_color;
			v2.colour = border_color;
			v3.colour = border_color;
			v4.colour = border_color;

			// Add vertices
			vertices.push_back(v1);
			vertices.push_back(v2);
			vertices.push_back(v3);
			vertices.push_back(v4);

			// Add indices for two triangles
			indices.push_back(vertex_count);
			indices.push_back(vertex_count + 1);
			indices.push_back(vertex_count + 2);

			indices.push_back(vertex_count + 1);
			indices.push_back(vertex_count + 3);
			indices.push_back(vertex_count + 2);

			vertex_count += 4;
		};

	// Define the lengths for horizontal and vertical borders
	Rocket::Core::Vector2f horizontal(border_length_h, 0);
	Rocket::Core::Vector2f vertical(0, border_length_v);

	// Add corner borders by adding two line segments for each corner, moved inward by thickness
	// Top-left corner
	add_line_segment(top_left, top_left + horizontal, border_thickness); // Horizontal line
	add_line_segment(top_left, top_left + vertical, border_thickness);   // Vertical line

	// Top-right corner
	add_line_segment(top_right, top_right - horizontal, border_thickness); // Horizontal line
	add_line_segment(top_right, top_right + vertical, border_thickness);   // Vertical line

	// Bottom-left corner
	add_line_segment(bottom_left, bottom_left + horizontal, border_thickness); // Horizontal line
	add_line_segment(bottom_left, bottom_left - vertical, border_thickness);   // Vertical line

	// Bottom-right corner
	add_line_segment(bottom_right, bottom_right - horizontal, border_thickness); // Horizontal line
	add_line_segment(bottom_right, bottom_right - vertical, border_thickness);   // Vertical line

	// Render the geometry
	render_interface->RenderGeometry(vertices.data(),
		(int)vertices.size(),
		indices.data(),
		(int)indices.size(),
		0,
		Rocket::Core::Vector2f(0, 0));
}

} // namespace decorators
} // namespace scpui