summaryrefslogtreecommitdiff
path: root/tests/pke-test-static-ui.cpp
blob: aa989cc01df0216ab75ebe5987386c4d79f3d2a4 (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
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

#include "./pke-test-static-ui.h"

#include "./pke-test-types.h"

#include "ecs.hpp"
#include "pke-test-stubs.h"
#include "player-input.hpp"
#include "window.hpp"
#include "vendor-glm-include.hpp"
#include <limits>

struct pke_ui_box_instance_buffer_item {
	glm::mat4 pos_scale;
	glm::vec2 px_scale;
	float depth;
	float padding[1];
};
struct pke_ui_flex_params {
	float px_per_unit;
	float unit_total;
};

#define PKE_TEST_EXPOSE
#include "static-ui.hpp"

static pk_membucket *bkt;

void pke_test_static_ui_setup() {
	pke_test_stub_init_vulkan();
	bkt = pk_mem_bucket_create("pke_test_static_ui", PK_MEM_DEFAULT_BUCKET_SIZE, PK_MEMBUCKET_FLAG_NONE);
	pk_mem_bucket_set_client_mem_bucket(bkt);
	Extent.width = 1920;
	Extent.height = 1080;
	pk_ev_init(nullptr);
	ECS_Init();
	pke_input_init();
	pke_ui_init();
}

void pke_test_static_ui_teardown() {
	pke_ui_teardown();
	ECS_Teardown();
	pk_ev_teardown();
	pke_input_teardown();
	pk_mem_bucket_destroy(bkt);
	pk_mem_bucket_set_client_mem_bucket(nullptr);
	bkt = nullptr;
	pke_test_stub_teardown_vulkan();
}

// test static
int pke_test_static_ui_000() {
	pk_arr_t<pke_ui_box_instance_buffer_item> arr{};
	pk_arr_t<std::pair<pke_ui_box*,pke_ui_box_instance_buffer_item>> txtr_arr{};
	float calculated_offset;
	uint8_t err_index = 0;

	pke_ui_box *ui_box = pke_ui_box_new_root();
	ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_STATIC;
	ui_box->pos_top_left.x = 10;
	ui_box->pos_top_left.y = 10;
	ui_box->max_size.x = 500;
	ui_box->max_size.y = 500;

	pke_ui_box *c_ui_box = pke_ui_box_new_child(ui_box);
	c_ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_STATIC;
	c_ui_box->pos_top_left.x = 10;
	c_ui_box->pos_top_left.y = 10;
	c_ui_box->max_size.x = 100;
	c_ui_box->max_size.y = 100;

	calculated_offset = ui_box->pos_top_left.x + c_ui_box->pos_top_left.x + built_in_offset;

	pke_ui_calc_px(arr, txtr_arr, nullptr, ui_box);
	pke_ui_recalc_sizes_recursive(arr, txtr_arr, ui_box);

	PKE_TEST_ASSERT(ui_box->internal.parent == nullptr, err_index);
	PKE_TEST_ASSERT(ui_box->internal.h_children == 1, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.parent != nullptr, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.h_children == 0, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.r_children == 0, err_index);

	PKE_TEST_ASSERT(ui_box->internal.px_corner.x == 10, err_index);
	PKE_TEST_ASSERT(ui_box->internal.px_corner.y == 10, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_corner.x == calculated_offset, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_corner.y == calculated_offset, err_index);

	PKE_TEST_ASSERT(ui_box->internal.px_size.x == 500, err_index);
	PKE_TEST_ASSERT(ui_box->internal.px_size.y == 500, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_size.x == 100, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_size.y == 100, err_index);

	return 0;
}

// test dynamic
int pke_test_static_ui_100() {
	pk_arr_t<pke_ui_box_instance_buffer_item> arr{};
	pk_arr_t<std::pair<pke_ui_box*,pke_ui_box_instance_buffer_item>> txtr_arr{};
	bool b;
	float unit;
	float calculated_offset_x, calculated_offset_y;
	uint8_t err_index = 0;

	pke_ui_box *ui_box = pke_ui_box_new_root();
	ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_DYNAMIC;
	ui_box->pos_top_left.x = 0.1;
	ui_box->pos_top_left.y = 0.1;
	ui_box->max_size.x = 0.8;
	ui_box->max_size.y = 0.8;

	pke_ui_box *c_ui_box = pke_ui_box_new_child(ui_box);
	c_ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_DYNAMIC;
	c_ui_box->pos_top_left.x = 0.1;
	c_ui_box->pos_top_left.y = 0.1;
	c_ui_box->max_size.x = 0.8;
	c_ui_box->max_size.y = 0.8;

	unit = Extent.width * 0.1;
	calculated_offset_x = (unit * 8) - (built_in_offset * 2); // padded parent
	calculated_offset_x = (calculated_offset_x * 0.1) + unit + built_in_offset;

	unit = Extent.height * 0.1;
	calculated_offset_y = (unit * 8) - (built_in_offset * 2); // padded parent
	calculated_offset_y = (calculated_offset_y * 0.1) + unit + built_in_offset;

	pke_ui_calc_px(arr, txtr_arr, nullptr, ui_box);
	pke_ui_recalc_sizes_recursive(arr, txtr_arr, ui_box);

	PKE_TEST_ASSERT(ui_box->internal.parent == nullptr, err_index);
	PKE_TEST_ASSERT(ui_box->internal.h_children == 1, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.parent != nullptr, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.h_children == 0, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.r_children == 0, err_index);

	PKE_TEST_ASSERT(ui_box->internal.px_corner.x == Extent.width * 0.1, err_index);
	PKE_TEST_ASSERT(ui_box->internal.px_corner.y == Extent.height * 0.1, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_corner.x == calculated_offset_x, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_corner.y == calculated_offset_y, err_index);

	PKE_TEST_ASSERT(ui_box->internal.px_size.x == (Extent.width * 0.1) * 8, err_index);
	PKE_TEST_ASSERT(ui_box->internal.px_size.y == (Extent.height * 0.1) * 8, err_index);
	b = flt_equal<float>(
			c_ui_box->internal.px_size.x,
			(ui_box->internal.px_size.x - (built_in_offset * 2)) * 0.8,
			std::numeric_limits<float>::epsilon());
	PKE_TEST_ASSERT(b, err_index);
	b = flt_equal<float>(
			c_ui_box->internal.px_size.y,
			(ui_box->internal.px_size.y - (built_in_offset * 2)) * 0.8,
			std::numeric_limits<float>::epsilon());
	PKE_TEST_ASSERT(b, err_index);

	return 0;
}


// test dynamic + static
int pke_test_static_ui_200() {
	pk_arr_t<pke_ui_box_instance_buffer_item> arr{};
	pk_arr_t<std::pair<pke_ui_box*,pke_ui_box_instance_buffer_item>> txtr_arr{};
	float unit;
	float calculated_offset_x, calculated_offset_y;
	uint8_t err_index = 0;

	pke_ui_box *ui_box = pke_ui_box_new_root();
	ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_DYNAMIC;
	ui_box->pos_top_left.x = 0.1;
	ui_box->pos_top_left.y = 0.1;
	ui_box->max_size.x = 0.8;
	ui_box->max_size.y = 0.8;

	pke_ui_box *c_ui_box = pke_ui_box_new_child(ui_box);
	c_ui_box->flags = PKE_UI_BOX_FLAG_POSITION_TYPE_STATIC;
	c_ui_box->pos_top_left.x = 1;
	c_ui_box->pos_top_left.y = 1;
	c_ui_box->max_size.x = 300;
	c_ui_box->max_size.y = 300;

	unit = Extent.width * 0.1;
	calculated_offset_x = unit + 1 + built_in_offset;

	unit = Extent.height * 0.1;
	calculated_offset_y = unit + 1 + built_in_offset;

	pke_ui_calc_px(arr, txtr_arr, nullptr, ui_box);
	pke_ui_recalc_sizes_recursive(arr, txtr_arr, ui_box);

	PKE_TEST_ASSERT(ui_box->internal.parent == nullptr, err_index);
	PKE_TEST_ASSERT(ui_box->internal.h_children == 1, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.parent != nullptr, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.h_children == 0, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.r_children == 0, err_index);

	PKE_TEST_ASSERT(ui_box->internal.px_corner.x == Extent.width * 0.1, err_index);
	PKE_TEST_ASSERT(ui_box->internal.px_corner.y == Extent.height * 0.1, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_corner.x == calculated_offset_x, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_corner.y == calculated_offset_y, err_index);

	PKE_TEST_ASSERT(ui_box->internal.px_size.x == (Extent.width * 0.1) * 8, err_index);
	PKE_TEST_ASSERT(ui_box->internal.px_size.y == (Extent.height * 0.1) * 8, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_size.x == 300, err_index);
	PKE_TEST_ASSERT(c_ui_box->internal.px_size.y == 300, err_index);

	return 0;
}

pk_test_group *pke_test_static_ui_get_group() {
	static const uint64_t test_count = 3;
	static struct pk_test tests[test_count] = {
		{
			.title = "test 000",
			.func = pke_test_static_ui_000,
			.expected_result = 0,
		},
		{
			.title = "test 100",
			.func = pke_test_static_ui_100,
			.expected_result = 0,
		},
		{
			.title = "test 200",
			.func = pke_test_static_ui_200,
			.expected_result = 0,
		},
	};
	static struct pk_test_group group = {};
	group.title = "static_ui";
	group.group_setup = NULL;
	group.group_teardown = NULL;
	group.test_setup = pke_test_static_ui_setup;
	group.test_teardown = pke_test_static_ui_teardown;
	group.tests = &tests[0];
	group.n_tests = test_count;

	return &group;
}