fix regression with gate flipping
[carveJwlIkooP6JGAAIwe30JlM.git] / menu.c
1 #pragma once
2 #include "skaterift.h"
3 #include "menu.h"
4 #include "model.h"
5 #include "entity.h"
6 #include "input.h"
7 #include "world_map.h"
8 #include "ent_miniworld.h"
9 #include "audio.h"
10 #include "workshop.h"
11 #include "gui.h"
12 #include "shaders/model_menu.h"
13
14 struct global_menu menu;
15
16 /*
17 * Attaches memory locations to the various items in the menu
18 */
19 void menu_link(void)
20 {
21 /* link data locations */
22 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
23 ent_menuitem *item = mdl_arritm( &menu.items, i );
24
25 if( item->type == k_ent_menuitem_type_toggle ||
26 item->type == k_ent_menuitem_type_slider ){
27
28 const char *name;
29
30 if( item->type == k_ent_menuitem_type_slider )
31 name = mdl_pstr( &menu.model, item->slider.pstr_data );
32 else
33 name = mdl_pstr( &menu.model, item->checkmark.pstr_data );
34 vg_var *var = vg_console_match_var( name );
35
36 if( var ){
37 if( ( item->type == k_ent_menuitem_type_slider &&
38 var->data_type != k_var_dtype_f32
39 ) ||
40 ( item->type == k_ent_menuitem_type_toggle &&!
41 ( var->data_type == k_var_dtype_i32 ||
42 var->data_type == k_var_dtype_u32
43 )
44 )
45 ){
46 vg_error( "Cannot hook to data %s(%p), because it is type %d.\n",
47 name, var, var->data_type );
48 item->pvoid = NULL;
49 }
50 else{
51 item->pvoid = var->data;
52 }
53 }
54 else{
55 vg_error( "No data named %s\n", name );
56 item->pvoid = NULL;
57 }
58 }
59 else{
60 item->pvoid = NULL;
61 }
62 }
63
64 /* link controllers */
65 menu.ctr_deck = NULL;
66 menu.ctr_kbm = NULL;
67 menu.ctr_ps = NULL;
68 menu.ctr_steam = NULL;
69 menu.ctr_xbox = NULL;
70
71 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
72 ent_menuitem *item = mdl_arritm( &menu.items, i );
73
74 if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "deck" ) )
75 menu.ctr_deck = item;
76 if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "kbm" ) )
77 menu.ctr_kbm = item;
78 if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "ps" ) )
79 menu.ctr_ps = item;
80 if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "steam" ) )
81 menu.ctr_steam = item;
82 if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "xbox" ) )
83 menu.ctr_xbox = item;
84 }
85 }
86
87 void menu_close(void)
88 {
89 skaterift.activity = k_skaterift_default;
90 menu.page_depth = 0;
91 menu.page = 0xffffffff;
92 srinput.state = k_input_state_resume;
93 }
94
95 void menu_init(void)
96 {
97 void *alloc = vg_mem.rtmemory;
98
99 mdl_open( &menu.model, "models/rs_menu.mdl", alloc );
100 mdl_load_metadata_block( &menu.model, alloc );
101
102 vg_linear_clear( vg_mem.scratch );
103
104 MDL_LOAD_ARRAY( &menu.model, &menu.items, ent_menuitem, alloc );
105 MDL_LOAD_ARRAY( &menu.model, &menu.markers, ent_marker, alloc );
106 MDL_LOAD_ARRAY( &menu.model, &menu.cameras, ent_camera, alloc );
107
108 u32 count = mdl_arrcount( &menu.model.textures );
109 menu.textures = vg_linear_alloc(alloc,vg_align8(sizeof(GLuint)*(count+1)));
110 menu.textures[0] = vg.tex_missing;
111
112 mdl_async_load_glmesh( &menu.model, &menu.mesh, NULL );
113
114 for( u32 i=0; i<count; i ++ ){
115 vg_linear_clear( vg_mem.scratch );
116 menu.textures[i+1] = vg.tex_missing;
117
118 mdl_texture *tex = mdl_arritm( &menu.model.textures, i );
119 void *data = vg_linear_alloc( vg_mem.scratch, tex->file.pack_size );
120 mdl_fread_pack_file( &menu.model, &tex->file, data );
121 vg_tex2d_load_qoi_async( data, tex->file.pack_size,
122 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
123 &menu.textures[i+1] );
124 }
125
126 mdl_close( &menu.model );
127 }
128
129 /*
130 * Drop back a page until we're at the bottom which then we jus quit
131 */
132 static void menu_back_page(void){
133 menu.page_depth --;
134 if( menu.page_depth == 0 ){
135 menu_close();
136 }
137 else{
138 menu.page = menu.page_stack[ menu.page_depth ].page;
139 menu.cam = menu.page_stack[ menu.page_depth ].cam;
140
141 if( menu.input_mode == k_menu_input_mode_keys )
142 menu.loc = menu.page_stack[ menu.page_depth ].loc;
143 else menu.loc = NULL;
144 }
145 }
146
147 /*
148 * Open page to the string identifier
149 */
150 void menu_open_page( const char *name,
151 enum ent_menuitem_stack_behaviour stackmode )
152 {
153 srinput.state = k_input_state_resume;
154 if( stackmode == k_ent_menuitem_stack_append ){
155 if( menu.page_depth >= MENU_STACK_SIZE )
156 vg_fatal_error( "Stack overflow\n" );
157 }
158 else{
159 if( menu.page_depth == 0 )
160 vg_fatal_error( "Stack underflow\n" );
161 }
162
163 u32 hash = vg_strdjb2( name );
164 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
165 ent_menuitem *item = mdl_arritm( &menu.items, i );
166
167 if( item->type == k_ent_menuitem_type_page ){
168 if( mdl_pstreq( &menu.model, item->page.pstr_name, name, hash ) ){
169 u32 new_page = __builtin_ctz( item->groups );
170
171 if( new_page == menu.page ){
172 if( stackmode != k_ent_menuitem_stack_replace )
173 menu_back_page();
174 }
175 else{
176 menu.page_stack[ menu.page_depth ].page = menu.page;
177 menu.page_stack[ menu.page_depth ].cam = menu.cam;
178 menu.page_stack[ menu.page_depth ].loc = menu.loc;
179
180 if( stackmode == k_ent_menuitem_stack_append )
181 menu.page_depth ++;
182
183 menu.page = __builtin_ctz( item->groups );
184
185 if( menu.input_mode == k_menu_input_mode_keys ){
186 if( item->page.id_entrypoint ){
187 u32 id = mdl_entity_id_id( item->page.id_entrypoint );
188 menu.loc = mdl_arritm( &menu.items, id );
189 }
190 }
191
192 if( item->page.id_viewpoint ){
193 u32 id = mdl_entity_id_id( item->page.id_viewpoint );
194 menu.cam = mdl_arritm( &menu.cameras, id );
195 }
196 }
197 return;
198 }
199 }
200 }
201 }
202
203 /*
204 * activate a pressable type
205 */
206 static void menu_trigger_item( ent_menuitem *item ){
207 if ( item->type == k_ent_menuitem_type_event_button ){
208 u32 q = item->button.pstr;
209
210 if( MDL_CONST_PSTREQ( &menu.model, q, "quit" ) ){
211 vg.window_should_close = 1;
212 }
213 else if( MDL_CONST_PSTREQ( &menu.model, q, "map" ) ){
214 menu_close();
215 world_map_enter();
216 }
217 else if( MDL_CONST_PSTREQ( &menu.model, q, "hub" ) ){
218 if( world_static.active_instance == k_world_purpose_client ){
219 menu_close();
220 ent_miniworld_goback();
221 }
222 }
223 else if( MDL_CONST_PSTREQ( &menu.model, q, "credits" ) ){
224 menu.credits_open = 1;
225 }
226 else if( MDL_CONST_PSTREQ( &menu.model, q, "workshop" ) ){
227 workshop_submit_command(0,NULL);
228 }
229 else if( MDL_CONST_PSTREQ( &menu.model, q, "engine" ) ){
230 vg_settings_open();
231 }
232 else if( MDL_CONST_PSTREQ( &menu.model, q, "prem_store" ) ){
233 if( steam_ready )
234 SteamAPI_ISteamFriends_ActivateGameOverlayToStore(
235 SteamAPI_SteamFriends(), 2103940, k_EOverlayToStoreFlag_None);
236 }
237 else if( MDL_CONST_PSTREQ( &menu.model, q, "prem_nevermind" ) ){
238 menu_close();
239 }
240 }
241 else if( item->type == k_ent_menuitem_type_page_button ){
242 menu_open_page( mdl_pstr( &menu.model, item->button.pstr ),
243 item->button.stack_behaviour );
244 }
245 else if( item->type == k_ent_menuitem_type_toggle ){
246 if( item->pi32 ){
247 *item->pi32 = *item->pi32 ^ 0x1;
248 }
249 }
250 }
251
252 static f32 menu_slider_snap( f32 value, f32 old, f32 notch ){
253 f32 const k_epsilon = 0.0125f;
254
255 if( fabsf(notch-value) < k_epsilon ){
256 if( fabsf(notch-old) > k_epsilon ){
257 audio_lock();
258 audio_oneshot( &audio_ui[0], 1.0f, 0.0f );
259 audio_unlock();
260 }
261
262 return notch;
263 }
264 else
265 return value;
266 }
267
268 static void menu_setitem_type( ent_menuitem *item,
269 enum ent_menuitem_type type ){
270 if( !item ) return;
271 item->type = type;
272 }
273
274 /*
275 * Run from vg_gui every frame
276 */
277 void menu_update(void)
278 {
279 if( workshop_form.page != k_workshop_form_hidden ){
280 return;
281 }
282
283 int escape = button_down( k_srbind_mback );
284 if( menu.credits_open || vg.settings_open ){
285 if( escape ){
286 menu.credits_open = 0;
287
288 if( vg.settings_open )
289 vg_settings_close();
290 }
291 return;
292 }
293
294 if( button_down( k_srbind_mopen ) ){
295 if( skaterift.activity == k_skaterift_default ){
296 skaterift.activity = k_skaterift_menu;
297 menu.page = 0xffffffff;
298 menu_open_page( "Main Menu", k_ent_menuitem_stack_append );
299 return;
300 }
301 }
302
303 if( skaterift.activity != k_skaterift_menu ) return;
304 enum menu_input_mode prev_mode = menu.input_mode;
305
306 /* get buttons inputs
307 * -------------------------------------------------------------------*/
308 int ml = button_down( k_srbind_mleft ),
309 mr = button_down( k_srbind_mright ),
310 mu = button_down( k_srbind_mup ),
311 md = button_down( k_srbind_mdown ),
312 mh = ml-mr,
313 mv = mu-md,
314 enter = button_down( k_srbind_maccept );
315
316 if( mh||mv||enter ){
317 menu.input_mode = k_menu_input_mode_keys;
318 }
319
320 /* get mouse inputs
321 * --------------------------------------------------------------------*/
322 menu.mouse_dist += v2_length( vg.mouse_delta ); /* TODO: Move to UI */
323 menu.mouse_track += vg.time_frame_delta;
324 if( menu.mouse_track > 0.1f ){
325 menu.mouse_track = fmodf( menu.mouse_track, 0.1f );
326 if( menu.mouse_dist > 10.0f ){
327 menu.input_mode = k_menu_input_mode_mouse;
328 menu.mouse_dist = 0.0f;
329 }
330 }
331
332 if( ui_clicking(UI_MOUSE_LEFT) || ui_clicking(UI_MOUSE_RIGHT) ){
333 menu.input_mode = k_menu_input_mode_mouse;
334 }
335
336 if( menu.input_mode == k_menu_input_mode_mouse ){
337 /*
338 * handle mouse input
339 * ------------------------------------------------------------*/
340 vg_ui.wants_mouse = 1;
341
342 /*
343 * this raycasting is super cumbersome because all the functions were
344 * designed for other purposes. we dont care though.
345 */
346 m4x4f inverse;
347 m4x4_inv( menu.view.mtx.p, inverse );
348 v4f coords;
349 coords[0] = vg_ui.mouse[0];
350 coords[1] = vg.window_y - vg_ui.mouse[1];
351 v2_div( coords, (v2f){ vg.window_x, vg.window_y }, coords );
352 v2_muls( coords, 2.0f, coords );
353 v2_add( coords, (v2f){-1.0f,-1.0f}, coords );
354 coords[2] = 1.0f;
355 coords[3] = 1.0f;
356 m4x4_mulv( inverse, coords, coords );
357 v3f ray;
358 m3x3_mulv( menu.view.transform, coords, ray );
359 v3_normalize( ray );
360
361 if( menu.loc && (menu.loc->type == k_ent_menuitem_type_slider) &&
362 ui_clicking(UI_MOUSE_LEFT) && menu.loc->pf32 ){
363
364 u32 il = mdl_entity_id_id( menu.loc->slider.id_min ),
365 ir = mdl_entity_id_id( menu.loc->slider.id_max );
366 ent_marker *ml = mdl_arritm( &menu.markers, il ),
367 *mr = mdl_arritm( &menu.markers, ir );
368
369 v3f q2;
370 v3_muladds( menu.view.pos, ray, 100.0f, q2 );
371
372 f32 s,t;
373 v3f c1, c2;
374 v3f p1, q1, v0;
375 v3_sub( mr->transform.co, ml->transform.co, v0 );
376 v3_muladds( ml->transform.co, v0, -1.0f, p1 );
377 v3_muladds( mr->transform.co, v0, 1.0f, q1 );
378 closest_segment_segment( p1, q1, menu.view.pos, q2, &s,&t, c1,c2 );
379
380 s-=(1.0f/3.0f);
381 s/=(1.0f/3.0f);
382
383 if( ui_click_down(UI_MOUSE_LEFT) ){
384 menu.slider_offset = *menu.loc->pf32 - s;
385 }
386
387 f32 newvalue = vg_clampf( s+menu.slider_offset, 0.0f, 1.0f );
388
389 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.00f );
390 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 1.00f );
391 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.25f );
392 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.50f );
393 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.75f );
394
395 *menu.loc->pf32 = newvalue;
396 return;
397 }
398
399 ent_menuitem *hit_item = NULL;
400
401 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
402 ent_menuitem *item = mdl_arritm( &menu.items, i );
403
404 if( item->type == k_ent_menuitem_type_page ) continue;
405 if( (item->type == k_ent_menuitem_type_visual) ||
406 (item->type == k_ent_menuitem_type_visual_nocol) ) continue;
407 if( item->type == k_ent_menuitem_type_binding ) continue;
408 if( !(item->groups & (0x1<<menu.page)) ) continue;
409
410 ent_menuitem *ray_item = item;
411
412 if( item->type == k_ent_menuitem_type_slider ){
413 u32 subtarget = mdl_entity_id_id( item->slider.id_handle );
414 ray_item = mdl_arritm( &menu.items, subtarget );
415 }
416
417 v3f local_ray,
418 local_co;
419
420 m4x3f inverse_mtx;
421 mdl_transform_m4x3( &ray_item->transform, inverse_mtx );
422 m4x3_invert_full( inverse_mtx, inverse_mtx );
423
424 m4x3_mulv( inverse_mtx, menu.view.transform[3], local_co );
425 m3x3_mulv( inverse_mtx, ray, local_ray );
426 v3_normalize( local_ray );
427
428 local_ray[0] = 1.0f/local_ray[0];
429 local_ray[1] = 1.0f/local_ray[1];
430 local_ray[2] = 1.0f/local_ray[2];
431
432 for( u32 j=0; j<ray_item->submesh_count; j++ ){
433 mdl_submesh *sm = mdl_arritm( &menu.model.submeshs,
434 ray_item->submesh_start + j );
435 if( ray_aabb1( sm->bbx, local_co, local_ray, 1000.0f ) ){
436 hit_item = item;
437 break;
438 }
439 }
440 }
441
442 if( hit_item != menu.loc ){
443 menu.loc = hit_item;
444 }
445
446 if( escape ){
447 menu_back_page();
448 }
449 else if( menu.loc ){
450 if( ui_click_down( UI_MOUSE_LEFT ) ){
451 menu_trigger_item( menu.loc );
452 }
453 }
454 }
455 else if( menu.input_mode == k_menu_input_mode_keys ){
456 /*
457 * handle button input
458 * ------------------------------------------------------------*/
459 if( (prev_mode != k_menu_input_mode_keys) && !menu.loc ){
460 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
461 ent_menuitem *item = mdl_arritm( &menu.items, i );
462
463 if( (item->type != k_ent_menuitem_type_page) &&
464 (item->type != k_ent_menuitem_type_visual) &&
465 (item->type != k_ent_menuitem_type_visual_nocol) &&
466 (item->groups & (0x1<<menu.page)) ){
467 menu.loc = item;
468 }
469 }
470 }
471
472 if( !menu.loc ) vg_fatal_error( "No location\n" );
473
474 if( menu.loc->type == k_ent_menuitem_type_slider && menu.loc->pf32 ){
475 f32 move = 0.0f;
476
477 if( vg_input.display_input_method == k_input_method_controller ){
478 move += button_press( k_srbind_mright );
479 move -= button_press( k_srbind_mleft );
480 }
481 else{
482 move += axis_state( k_sraxis_mbrowse_h );
483 }
484
485 move *= vg.time_frame_delta;
486 *menu.loc->pf32 = vg_clampf( *menu.loc->pf32 + move, 0.0f, 1.0f );
487
488 mh = 0;
489 }
490
491 if( escape ){
492 menu_back_page();
493 }
494 else if( enter ){
495 menu_trigger_item( menu.loc );
496 }
497 else if( mh||mv ){
498 v3f opt;
499 v3_zero( opt );
500 f32 best = 0.707f;
501 ent_menuitem *nextpos = NULL;
502
503 opt[0] += mh;
504 opt[2] += mv;
505 mdl_transform_vector( &menu.cam->transform, opt, opt );
506
507 for( u32 i=0; i<4; i++ ){
508 u32 id = menu.loc->id_links[i];
509 if( !id ) continue;
510 u32 index = mdl_entity_id_id( id );
511
512 ent_menuitem *other = mdl_arritm( &menu.items, index );
513 v3f delta;
514 v3_sub( menu.loc->transform.co, other->transform.co, delta );
515 v3_normalize( delta );
516
517 f32 score = v3_dot( delta, opt );
518 if( score > best ){
519 best = score;
520 nextpos = other;
521 }
522 }
523
524 if( nextpos ){
525 menu.loc = nextpos;
526 }
527 }
528 }
529
530 menu_setitem_type( menu.ctr_deck, k_ent_menuitem_type_disabled );
531 menu_setitem_type( menu.ctr_ps, k_ent_menuitem_type_disabled );
532 menu_setitem_type( menu.ctr_kbm, k_ent_menuitem_type_disabled );
533 menu_setitem_type( menu.ctr_xbox, k_ent_menuitem_type_disabled );
534 menu_setitem_type( menu.ctr_steam, k_ent_menuitem_type_disabled );
535
536 if( vg_input.display_input_method == k_input_method_kbm )
537 menu_setitem_type( menu.ctr_kbm, k_ent_menuitem_type_visual_nocol );
538 else{
539 if( vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS3 ||
540 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS4 ||
541 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS5 ){
542 menu_setitem_type( menu.ctr_ps, k_ent_menuitem_type_visual_nocol );
543 }
544 else {
545 menu_setitem_type( menu.ctr_xbox, k_ent_menuitem_type_visual_nocol );
546 }
547 /* FIXME: Steam/Deck controller detection? */
548 }
549 }
550
551 static void menu_binding_string( char buf[128], u32 pstr );
552
553 /*
554 * Run from vg_gui when active
555 */
556 void menu_render(void)
557 {
558 glEnable(GL_BLEND);
559 glDisable(GL_DEPTH_TEST);
560 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
561 glBlendEquation(GL_FUNC_ADD);
562
563 shader_blitcolour_use();
564 v4f colour;
565 ui_hex_to_norm( ui_colour( k_ui_bg+3 ), colour );
566 colour[3] = 0.5f;
567
568 shader_blitcolour_uColour( colour );
569 render_fsquad();
570
571 if( (workshop_form.page != k_workshop_form_hidden) ||
572 (vg_ui.focused_control_type != k_ui_control_none) ){
573 return;
574 }
575
576 if( vg.settings_open )
577 return;
578
579 if( menu.credits_open ){
580 ui_rect panel = { 0,0, 460, 400 },
581 screen = { 0,0, vg.window_x,vg.window_y };
582 ui_rect_center( screen, panel );
583 ui_fill( panel, ui_colour(k_ui_bg) );
584 ui_outline( panel, 1, ui_colour(k_ui_fg), 0 );
585 ui_rect_pad( panel, (ui_px[]){8,8} );
586
587 ui_rect title;
588 ui_split( panel, k_ui_axis_h, 28*2, 0, title, panel );
589 ui_text( title, "Skate Rift - Credits", 2, k_ui_align_middle_center, 0 );
590 ui_split( panel, k_ui_axis_h, 28, 0, title, panel );
591 ui_text( title, "Mt.Zero Software", 1, k_ui_align_middle_center, 0 );
592
593 ui_split( panel, k_ui_axis_h, 8, 0, title, panel );
594 ui_split( panel, k_ui_axis_h, 28*2, 0, title, panel );
595 ui_text( title, "Free Software", 2, k_ui_align_middle_center, 0 );
596
597 ui_split( panel, k_ui_axis_h, 8, 0, title, panel );
598 ui_text( panel,
599 "Sam Lantinga - SDL2 - libsdl.org\n"
600 "Hunter WB - Anyascii\n"
601 "David Herberth - GLAD\n"
602 "Dominic Szablewski - QOI - qoiformat.org\n"
603 "Sean Barrett - stb_image,stb_vorbis,stb_include\n"
604 "Khronos Group - OpenGL\n"
605 , 1, k_ui_align_left, 0 );
606 return;
607 }
608
609 glEnable( GL_DEPTH_TEST );
610 glDisable( GL_BLEND );
611
612 f32 rate = vg.time_frame_delta * 12.0f;
613
614 if( menu.cam ){
615 vg_camera target;
616
617 target.fov = menu.cam->fov;
618 v3_copy( menu.cam->transform.co, target.pos );
619
620 v3f v0;
621 mdl_transform_vector( &menu.cam->transform, (v3f){0.0f,-1.0f,0.0f}, v0 );
622 v3_angles( v0, target.angles );
623
624 vg_camera_lerp( &menu.view, &target, rate, &menu.view );
625
626 menu.view.farz = 150.0f;
627 menu.view.nearz = 0.01f;
628
629 vg_camera_update_transform( &menu.view );
630 vg_camera_update_view( &menu.view );
631 vg_camera_update_projection( &menu.view );
632 vg_camera_finalize( &menu.view );
633 }
634 else return;
635
636 shader_model_menu_use();
637 shader_model_menu_uTexMain( 1 );
638 shader_model_menu_uPv( menu.view.mtx.pv );
639 shader_model_menu_uPvmPrev( menu.view.mtx_prev.pv );
640
641 mesh_bind( &menu.mesh );
642
643 v4f white, blue;
644
645 ui_hex_to_norm( ui_colour( k_ui_fg ), white );
646 ui_hex_to_norm( ui_colour( k_ui_orange+k_ui_brighter ), blue );
647
648 ent_menuitem *text_list[ 8 ];
649 u32 text_count = 0;
650
651 u32 current_tex = 0xffffffff;
652
653 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
654 ent_menuitem *item = mdl_arritm( &menu.items, i );
655
656 if( item->type == k_ent_menuitem_type_disabled ) continue;
657 if( item->type == k_ent_menuitem_type_page ) continue;
658 if( !(item->groups & (0x1 << menu.page)) ) continue;
659
660 if( item->type == k_ent_menuitem_type_binding ){
661 if( text_count < vg_list_size(text_list) )
662 text_list[ text_count ++ ] = item;
663 else
664 vg_fatal_error( "Text list overflow" );
665
666 continue;
667 }
668
669 int selected = 0;
670
671 if( menu.loc ){
672 if( menu.loc->type == k_ent_menuitem_type_slider ){
673 u32 subid = menu.loc->slider.id_handle;
674 if( item == mdl_arritm( &menu.items, mdl_entity_id_id(subid) ))
675 selected = 1;
676 }
677 else{
678 if( item == menu.loc )
679 selected = 1;
680 }
681 }
682
683 if( item->type == k_ent_menuitem_type_visual_nocol ){
684 shader_model_menu_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
685 }
686 else{
687 v4f colour;
688 item->factive = vg_lerpf( item->factive, selected, rate );
689 v4_lerp( white, blue, item->factive, colour );
690 shader_model_menu_uColour( colour );
691 }
692
693 f32 scale = 1.0f+item->factive*0.1f;
694
695 m4x3f mmdl;
696 mdl_transform transform = item->transform;
697 v3_muls( transform.s, scale, transform.s );
698 mdl_transform_m4x3( &transform, mmdl );
699
700 if( item->type == k_ent_menuitem_type_toggle && item->pi32 ){
701 u32 subid = mdl_entity_id_id( item->checkmark.id_check );
702 ent_menuitem *subitem = mdl_arritm( &menu.items, subid );
703
704 v3_muladds( item->transform.co, item->checkmark.offset, scale,
705 subitem->transform.co );
706
707 subitem->fvisible = vg_lerpf( subitem->fvisible, *item->pi32, rate );
708 v3_fill( subitem->transform.s, subitem->fvisible );
709 }
710 else if( item->type == k_ent_menuitem_type_slider && item->pf32 ){
711 u32 il = mdl_entity_id_id( item->slider.id_min ),
712 ir = mdl_entity_id_id( item->slider.id_max ),
713 ih = mdl_entity_id_id( item->slider.id_handle );
714 ent_marker *ml = mdl_arritm( &menu.markers, il ),
715 *mr = mdl_arritm( &menu.markers, ir );
716 ent_menuitem *handle = mdl_arritm( &menu.items, ih );
717
718 v3_lerp( ml->transform.co, mr->transform.co, *item->pf32,
719 handle->transform.co );
720 }
721
722 shader_model_menu_uMdl( mmdl );
723
724 for( u32 j=0; j<item->submesh_count; j++ ){
725 u32 index = item->submesh_start + j;
726 mdl_submesh *sm = mdl_arritm( &menu.model.submeshs, index );
727
728 mdl_material *mat = mdl_arritm( &menu.model.materials,
729 sm->material_id-1 );
730
731 if( mat->tex_diffuse != current_tex ){
732 glActiveTexture( GL_TEXTURE1 );
733 glBindTexture( GL_TEXTURE_2D, menu.textures[ mat->tex_diffuse ] );
734 current_tex = mat->tex_diffuse;
735 }
736
737 mdl_draw_submesh( sm );
738 }
739 }
740
741 if( !text_count ) return;
742
743 char buf[ 128 ];
744
745 m4x3f local;
746 m4x3_identity( local );
747
748 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &menu.view );
749 for( u32 i=0; i<text_count; i++ ){
750 ent_menuitem *item = text_list[ i ];
751 m4x3f transform;
752 mdl_transform_m4x3( &item->transform, transform );
753
754 u32 variant = item->binding.font_variant;
755 menu_binding_string( buf, item->binding.pstr_bind );
756 f32 offset = font3d_string_width( variant, buf );
757
758 local[3][0] = -0.5f * offset;
759 m4x3_mul( transform, local, transform );
760
761 font3d_simple_draw( variant, buf, &menu.view, transform );
762 }
763 }
764
765 static void menu_binding_string( char buf[128], u32 pstr ){
766 vg_str str;
767 vg_strnull( &str, buf, 128 );
768
769 if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_jump" ) ){
770 vg_input_string( &str, input_button_list[k_srbind_jump], 1 );
771 }
772 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick0" ) ){
773 vg_strcat( &str, "SHUVIT " );
774 vg_input_string( &str, input_button_list[k_srbind_trick0], 1 );
775 }
776 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick1" ) ){
777 vg_strcat( &str, "KICKFLIP " );
778 vg_input_string( &str, input_button_list[k_srbind_trick1], 1 );
779 }
780 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick2" ) ){
781 vg_strcat( &str, "TREFLIP " );
782 vg_input_string( &str, input_button_list[k_srbind_trick2], 1 );
783 }
784 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_grab" ) ){
785 vg_input_string( &str, input_axis_list[k_sraxis_grab], 1 );
786 }
787 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_grab_mod" ) ){
788 vg_input_string( &str, input_joy_list[k_srjoystick_grab], 1 );
789 }
790 else
791 vg_strcat( &str, "error" );
792 }