@@ -14,6 +14,15 @@ engine_node_base_t *focused_gui_node_base = NULL;
14
14
bool gui_focused = false;
15
15
16
16
17
+ vector2_class_obj_t * resolve_gui_node_position (engine_node_base_t * gui_node_base ){
18
+ if (mp_obj_is_type (focused_gui_node_base , & engine_gui_bitmap_button_2d_node_class_type )){
19
+ return ((engine_gui_bitmap_button_2d_node_class_obj_t * )gui_node_base -> node )-> position ;
20
+ }else {
21
+ return ((engine_gui_button_2d_node_class_obj_t * )gui_node_base -> node )-> position ;
22
+ }
23
+ }
24
+
25
+
17
26
void engine_gui_reset (){
18
27
focused_gui_node_base = NULL ;
19
28
gui_focused = false;
@@ -140,12 +149,7 @@ void engine_gui_select_closest(bool (direction_check)(float)){
140
149
}
141
150
142
151
// Get the position of the currently focused GUI node
143
- vector2_class_obj_t * focused_gui_position = NULL ;
144
- if (mp_obj_is_type (focused_gui_node_base , & engine_gui_bitmap_button_2d_node_class_type )){
145
- focused_gui_position = ((engine_gui_bitmap_button_2d_node_class_obj_t * )focused_gui_node_base -> node )-> position ;
146
- }else {
147
- focused_gui_position = ((engine_gui_button_2d_node_class_obj_t * )focused_gui_node_base -> node )-> position ;
148
- }
152
+ vector2_class_obj_t * focused_gui_position = resolve_gui_node_position (focused_gui_node_base );
149
153
150
154
// Setup for looping through all GUI nodes and finding closest
151
155
linked_list * gui_list = engine_collections_get_gui_list ();
@@ -162,18 +166,11 @@ void engine_gui_select_closest(bool (direction_check)(float)){
162
166
continue ;
163
167
}
164
168
165
- // Get the node base of the currently looping
166
- // through node
169
+ // Get the node base of the currently looping through node
167
170
engine_node_base_t * searching_gui_node_base = current_gui_list_node -> object ;
168
171
169
- // Get the position of the currently focused
170
- // GUI node
171
- vector2_class_obj_t * searching_gui_position = NULL ;
172
- if (mp_obj_is_type (searching_gui_node_base , & engine_gui_bitmap_button_2d_node_class_type )){
173
- searching_gui_position = ((engine_gui_bitmap_button_2d_node_class_obj_t * )searching_gui_node_base -> node )-> position ;
174
- }else {
175
- searching_gui_position = ((engine_gui_button_2d_node_class_obj_t * )searching_gui_node_base -> node )-> position ;
176
- }
172
+ // Get the position of the current GUI node
173
+ vector2_class_obj_t * searching_gui_position = resolve_gui_node_position (searching_gui_node_base );
177
174
178
175
// Get the angle between the focused node and
179
176
// the node we looped to now
@@ -209,6 +206,48 @@ void engine_gui_select_closest(bool (direction_check)(float)){
209
206
}
210
207
211
208
209
+ void engine_gui_clear_focused (){
210
+ // If the GUI layer is focused, find a new node when
211
+ // the current focused node is to be cleared (likely gc'ed)
212
+ if (gui_focused ){
213
+ linked_list * gui_list = engine_collections_get_gui_list ();
214
+ linked_list_node * current_gui_list_node = gui_list -> start ;
215
+
216
+ // Get the position of the currently focused GUI node
217
+ vector2_class_obj_t * focused_gui_position = resolve_gui_node_position (focused_gui_node_base );
218
+
219
+ engine_node_base_t * closest_gui_node_base = NULL ;
220
+ float shortest_distance = FLT_MAX ;
221
+
222
+ while (current_gui_list_node != NULL ){
223
+ engine_node_base_t * searching_gui_node_base = current_gui_list_node -> object ;
224
+ vector2_class_obj_t * searching_gui_position = resolve_gui_node_position (searching_gui_node_base );
225
+
226
+ float distance = engine_math_distance_between (focused_gui_position -> x .value , focused_gui_position -> y .value , searching_gui_position -> x .value , searching_gui_position -> y .value );
227
+
228
+ // If the distance is closer than the last one
229
+ // we compared to, set it as the closest. Make
230
+ // sure not comparing focused vs. focused
231
+ if (distance < shortest_distance && focused_gui_node_base != searching_gui_node_base ){
232
+ shortest_distance = distance ;
233
+ closest_gui_node_base = searching_gui_node_base ;
234
+ }
235
+
236
+ current_gui_list_node = current_gui_list_node -> next ;
237
+ }
238
+
239
+ // Check if we found an alternative, node, focus it if we did
240
+ if (closest_gui_node_base != NULL ){
241
+ engine_gui_focus_node (closest_gui_node_base );
242
+ }else {
243
+ focused_gui_node_base = NULL ;
244
+ }
245
+ }else {
246
+ focused_gui_node_base = NULL ;
247
+ }
248
+ }
249
+
250
+
212
251
void engine_gui_tick (){
213
252
// Every tick, see if the button to toggle GUI focus was pressed.
214
253
// If the GUI toggle button is 0 that means None was set for the
0 commit comments