menu polish sound / interactions
[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 = { .skip_starter = 0 };
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 vg_console_reg_var( "skip_starter_menu", &menu.skip_starter,
129 k_var_dtype_i32, VG_VAR_PERSISTENT );
130 }
131
132 void menu_at_begin(void)
133 {
134 if( menu.skip_starter ) return;
135
136 skaterift.activity = k_skaterift_menu;
137 menu.page = 0xffffffff;
138 menu_open_page( "Starter", k_ent_menuitem_stack_append );
139 }
140
141 /*
142 * Drop back a page until we're at the bottom which then we jus quit
143 */
144 static void menu_back_page(void){
145 menu.page_depth --;
146 if( menu.page_depth == 0 ){
147 menu_close();
148 }
149 else{
150 menu.page = menu.page_stack[ menu.page_depth ].page;
151 menu.cam = menu.page_stack[ menu.page_depth ].cam;
152
153 if( menu.input_mode == k_menu_input_mode_keys )
154 menu.loc = menu.page_stack[ menu.page_depth ].loc;
155 else menu.loc = NULL;
156 }
157 }
158
159 /*
160 * Open page to the string identifier
161 */
162 void menu_open_page( const char *name,
163 enum ent_menuitem_stack_behaviour stackmode )
164 {
165 srinput.state = k_input_state_resume;
166 if( stackmode == k_ent_menuitem_stack_append ){
167 if( menu.page_depth >= MENU_STACK_SIZE )
168 vg_fatal_error( "Stack overflow\n" );
169 }
170 else{
171 if( menu.page_depth == 0 )
172 vg_fatal_error( "Stack underflow\n" );
173 }
174
175 u32 hash = vg_strdjb2( name );
176 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
177 ent_menuitem *item = mdl_arritm( &menu.items, i );
178
179 if( item->type == k_ent_menuitem_type_page ){
180 if( mdl_pstreq( &menu.model, item->page.pstr_name, name, hash ) ){
181 u32 new_page = __builtin_ctz( item->groups );
182
183 if( new_page == menu.page ){
184 if( stackmode != k_ent_menuitem_stack_replace )
185 menu_back_page();
186 }
187 else{
188 menu.page_stack[ menu.page_depth ].page = menu.page;
189 menu.page_stack[ menu.page_depth ].cam = menu.cam;
190 menu.page_stack[ menu.page_depth ].loc = menu.loc;
191
192 if( stackmode == k_ent_menuitem_stack_append )
193 menu.page_depth ++;
194
195 menu.page = __builtin_ctz( item->groups );
196
197 if( menu.input_mode == k_menu_input_mode_keys ){
198 if( item->page.id_entrypoint ){
199 u32 id = mdl_entity_id_id( item->page.id_entrypoint );
200 menu.loc = mdl_arritm( &menu.items, id );
201 }
202 }
203
204 if( item->page.id_viewpoint ){
205 u32 id = mdl_entity_id_id( item->page.id_viewpoint );
206 menu.cam = mdl_arritm( &menu.cameras, id );
207 }
208 }
209 return;
210 }
211 }
212 }
213 }
214
215 /*
216 * activate a pressable type
217 */
218 static void menu_trigger_item( ent_menuitem *item )
219 {
220 audio_lock();
221 audio_oneshot( &audio_ui[0], 1.0f, 0.0f );
222 audio_unlock();
223
224 if ( item->type == k_ent_menuitem_type_event_button )
225 {
226 u32 q = item->button.pstr;
227
228 if( MDL_CONST_PSTREQ( &menu.model, q, "quit" ) )
229 {
230 vg.window_should_close = 1;
231 }
232 else if( MDL_CONST_PSTREQ( &menu.model, q, "map" ) ){
233 menu_close();
234 world_map_enter();
235 }
236 else if( MDL_CONST_PSTREQ( &menu.model, q, "hub" ) ){
237 if( world_static.active_instance == k_world_purpose_client ){
238 menu_close();
239 ent_miniworld_goback();
240 }
241 }
242 else if( MDL_CONST_PSTREQ( &menu.model, q, "credits" ) ){
243 menu.credits_open = 1;
244 }
245 else if( MDL_CONST_PSTREQ( &menu.model, q, "workshop" ) ){
246 workshop_submit_command(0,NULL);
247 }
248 else if( MDL_CONST_PSTREQ( &menu.model, q, "engine" ) ){
249 vg_settings_open();
250 }
251 else if( MDL_CONST_PSTREQ( &menu.model, q, "prem_store" ) ){
252 if( steam_ready )
253 SteamAPI_ISteamFriends_ActivateGameOverlayToStore(
254 SteamAPI_SteamFriends(), 2103940, k_EOverlayToStoreFlag_None);
255 }
256 else if( MDL_CONST_PSTREQ( &menu.model, q, "prem_nevermind" ) ){
257 menu_close();
258 }
259 else if( MDL_CONST_PSTREQ( &menu.model, q, "starter_enter" ) )
260 {
261 if( network_client.auto_connect )
262 network_client.user_intent = k_server_intent_online;
263
264 menu_close();
265 }
266 }
267 else if( item->type == k_ent_menuitem_type_page_button )
268 {
269 menu_open_page( mdl_pstr( &menu.model, item->button.pstr ),
270 item->button.stack_behaviour );
271 }
272 else if( item->type == k_ent_menuitem_type_toggle )
273 {
274 if( item->pi32 ){
275 *item->pi32 = *item->pi32 ^ 0x1;
276 }
277 }
278 }
279
280 static f32 menu_slider_snap( f32 value, f32 old, f32 notch ){
281 f32 const k_epsilon = 0.0125f;
282
283 if( fabsf(notch-value) < k_epsilon ){
284 if( fabsf(notch-old) > k_epsilon ){
285 audio_lock();
286 audio_oneshot( &audio_ui[0], 1.0f, 0.0f );
287 audio_unlock();
288 }
289
290 return notch;
291 }
292 else
293 return value;
294 }
295
296 static void menu_setitem_type( ent_menuitem *item,
297 enum ent_menuitem_type type ){
298 if( !item ) return;
299 item->type = type;
300 }
301
302 /*
303 * Run from vg_gui every frame
304 */
305 void menu_update(void)
306 {
307 static f32 repeater = 0.0f;
308 if( repeater > 0.0f )
309 repeater -= vg.time_frame_delta;
310
311 if( workshop_form.page != k_workshop_form_hidden ){
312 return;
313 }
314
315 bool escape = 0;
316
317 if( menu.credits_open || vg.settings_open )
318 {
319 vg_exec_input_program( k_vg_input_type_button_u8,
320 input_button_list[k_srbind_mback], &escape );
321 if( escape )
322 {
323 menu.credits_open = 0;
324
325 if( vg.settings_open )
326 vg_settings_close();
327
328 srinput.state = k_input_state_resume;
329 }
330 return;
331 }
332 escape = button_down( k_srbind_mback );
333
334 if( button_down( k_srbind_mopen ) ){
335 if( skaterift.activity == k_skaterift_default ){
336 skaterift.activity = k_skaterift_menu;
337 menu.page = 0xffffffff;
338 menu_open_page( "Main Menu", k_ent_menuitem_stack_append );
339 return;
340 }
341 }
342
343 if( skaterift.activity != k_skaterift_menu ) return;
344 enum menu_input_mode prev_mode = menu.input_mode;
345
346 /* get buttons inputs
347 * -------------------------------------------------------------------*/
348 int ml = button_down( k_srbind_mleft ),
349 mr = button_down( k_srbind_mright ),
350 mu = button_down( k_srbind_mup ),
351 md = button_down( k_srbind_mdown ),
352 mh = ml-mr,
353 mv = mu-md,
354 enter = button_down( k_srbind_maccept );
355
356 if( mh||mv||enter ){
357 menu.input_mode = k_menu_input_mode_keys;
358 }
359
360 /* get mouse inputs
361 * --------------------------------------------------------------------*/
362 menu.mouse_dist += v2_length( vg.mouse_delta ); /* TODO: Move to UI */
363 menu.mouse_track += vg.time_frame_delta;
364 if( menu.mouse_track > 0.1f ){
365 menu.mouse_track = fmodf( menu.mouse_track, 0.1f );
366 if( menu.mouse_dist > 10.0f ){
367 menu.input_mode = k_menu_input_mode_mouse;
368 menu.mouse_dist = 0.0f;
369 }
370 }
371
372 if( ui_clicking(UI_MOUSE_LEFT) || ui_clicking(UI_MOUSE_RIGHT) ){
373 menu.input_mode = k_menu_input_mode_mouse;
374 }
375
376 if( menu.input_mode == k_menu_input_mode_mouse ){
377 /*
378 * handle mouse input
379 * ------------------------------------------------------------*/
380 vg_ui.wants_mouse = 1;
381
382 /*
383 * this raycasting is super cumbersome because all the functions were
384 * designed for other purposes. we dont care though.
385 */
386 m4x4f inverse;
387 m4x4_inv( menu.view.mtx.p, inverse );
388 v4f coords;
389 coords[0] = vg_ui.mouse[0];
390 coords[1] = vg.window_y - vg_ui.mouse[1];
391 v2_div( coords, (v2f){ vg.window_x, vg.window_y }, coords );
392 v2_muls( coords, 2.0f, coords );
393 v2_add( coords, (v2f){-1.0f,-1.0f}, coords );
394 coords[2] = 1.0f;
395 coords[3] = 1.0f;
396 m4x4_mulv( inverse, coords, coords );
397 v3f ray;
398 m3x3_mulv( menu.view.transform, coords, ray );
399 v3_normalize( ray );
400
401 if( menu.loc && (menu.loc->type == k_ent_menuitem_type_slider) &&
402 ui_clicking(UI_MOUSE_LEFT) && menu.loc->pf32 ){
403
404 u32 il = mdl_entity_id_id( menu.loc->slider.id_min ),
405 ir = mdl_entity_id_id( menu.loc->slider.id_max );
406 ent_marker *ml = mdl_arritm( &menu.markers, il ),
407 *mr = mdl_arritm( &menu.markers, ir );
408
409 v3f q2;
410 v3_muladds( menu.view.pos, ray, 100.0f, q2 );
411
412 f32 s,t;
413 v3f c1, c2;
414 v3f p1, q1, v0;
415 v3_sub( mr->transform.co, ml->transform.co, v0 );
416 v3_muladds( ml->transform.co, v0, -1.0f, p1 );
417 v3_muladds( mr->transform.co, v0, 1.0f, q1 );
418 closest_segment_segment( p1, q1, menu.view.pos, q2, &s,&t, c1,c2 );
419
420 s-=(1.0f/3.0f);
421 s/=(1.0f/3.0f);
422
423 if( ui_click_down(UI_MOUSE_LEFT) ){
424 menu.slider_offset = *menu.loc->pf32 - s;
425 }
426
427 f32 newvalue = vg_clampf( s+menu.slider_offset, 0.0f, 1.0f );
428
429 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.00f );
430 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 1.00f );
431 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.25f );
432 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.50f );
433 newvalue = menu_slider_snap( newvalue, *menu.loc->pf32, 0.75f );
434
435 *menu.loc->pf32 = newvalue;
436 return;
437 }
438
439 ent_menuitem *hit_item = NULL;
440
441 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
442 ent_menuitem *item = mdl_arritm( &menu.items, i );
443
444 if( item->type == k_ent_menuitem_type_page ) continue;
445 if( (item->type == k_ent_menuitem_type_visual) ||
446 (item->type == k_ent_menuitem_type_visual_nocol) ) continue;
447 if( item->type == k_ent_menuitem_type_binding ) continue;
448 if( !(item->groups & (0x1<<menu.page)) ) continue;
449
450 ent_menuitem *ray_item = item;
451
452 if( item->type == k_ent_menuitem_type_slider ){
453 u32 subtarget = mdl_entity_id_id( item->slider.id_handle );
454 ray_item = mdl_arritm( &menu.items, subtarget );
455 }
456
457 v3f local_ray,
458 local_co;
459
460 m4x3f inverse_mtx;
461 mdl_transform_m4x3( &ray_item->transform, inverse_mtx );
462 m4x3_invert_full( inverse_mtx, inverse_mtx );
463
464 m4x3_mulv( inverse_mtx, menu.view.transform[3], local_co );
465 m3x3_mulv( inverse_mtx, ray, local_ray );
466 v3_normalize( local_ray );
467
468 local_ray[0] = 1.0f/local_ray[0];
469 local_ray[1] = 1.0f/local_ray[1];
470 local_ray[2] = 1.0f/local_ray[2];
471
472 for( u32 j=0; j<ray_item->submesh_count; j++ ){
473 mdl_submesh *sm = mdl_arritm( &menu.model.submeshs,
474 ray_item->submesh_start + j );
475 if( ray_aabb1( sm->bbx, local_co, local_ray, 1000.0f ) ){
476 hit_item = item;
477 break;
478 }
479 }
480 }
481
482 if( hit_item != menu.loc ){
483 menu.loc = hit_item;
484 }
485
486 if( escape ){
487 menu_back_page();
488 }
489 else if( menu.loc ){
490 if( ui_click_down( UI_MOUSE_LEFT ) )
491 {
492 menu_trigger_item( menu.loc );
493 }
494 }
495 }
496 else if( menu.input_mode == k_menu_input_mode_keys ){
497 /*
498 * handle button input
499 * ------------------------------------------------------------*/
500 if( (prev_mode != k_menu_input_mode_keys) && !menu.loc ){
501 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
502 ent_menuitem *item = mdl_arritm( &menu.items, i );
503
504 if( (item->type != k_ent_menuitem_type_page) &&
505 (item->type != k_ent_menuitem_type_visual) &&
506 (item->type != k_ent_menuitem_type_visual_nocol) &&
507 (item->groups & (0x1<<menu.page)) ){
508 menu.loc = item;
509 }
510 }
511 }
512
513 if( !menu.loc ) vg_fatal_error( "No location\n" );
514
515 if( menu.loc->type == k_ent_menuitem_type_slider && menu.loc->pf32 ){
516 f32 move = 0.0f;
517
518 if( vg_input.display_input_method == k_input_method_controller ){
519 move += button_press( k_srbind_mright );
520 move -= button_press( k_srbind_mleft );
521 }
522 else{
523 move += axis_state( k_sraxis_mbrowse_h );
524 }
525
526 move *= vg.time_frame_delta;
527 *menu.loc->pf32 = vg_clampf( *menu.loc->pf32 + move, 0.0f, 1.0f );
528
529 mh = 0;
530 }
531
532 if( escape )
533 {
534 menu_back_page();
535 audio_lock();
536 audio_oneshot( &audio_ui[3], 1.0f, 0.0f );
537 audio_unlock();
538 }
539 else if( enter )
540 {
541 menu_trigger_item( menu.loc );
542 }
543 else if( mh||mv ){
544 v3f opt;
545 v3_zero( opt );
546 f32 best = 0.5f;
547 ent_menuitem *nextpos = NULL;
548
549 opt[0] += mh;
550 opt[2] += mv;
551 mdl_transform_vector( &menu.cam->transform, opt, opt );
552
553 for( u32 i=0; i<4; i++ ){
554 u32 id = menu.loc->id_links[i];
555 if( !id ) continue;
556 u32 index = mdl_entity_id_id( id );
557
558 ent_menuitem *other = mdl_arritm( &menu.items, index );
559 v3f delta;
560 v3_sub( menu.loc->transform.co, other->transform.co, delta );
561 v3_normalize( delta );
562
563 f32 score = v3_dot( delta, opt );
564 if( score > best ){
565 best = score;
566 nextpos = other;
567 }
568 }
569
570 if( nextpos && (repeater <= 0.0f) )
571 {
572 menu.loc = nextpos;
573 audio_lock();
574 audio_oneshot( &audio_ui[3], 1.0f, 0.0f );
575 audio_unlock();
576 repeater += 0.1f;
577 }
578 }
579 }
580
581 menu_setitem_type( menu.ctr_deck, k_ent_menuitem_type_disabled );
582 menu_setitem_type( menu.ctr_ps, k_ent_menuitem_type_disabled );
583 menu_setitem_type( menu.ctr_kbm, k_ent_menuitem_type_disabled );
584 menu_setitem_type( menu.ctr_xbox, k_ent_menuitem_type_disabled );
585 menu_setitem_type( menu.ctr_steam, k_ent_menuitem_type_disabled );
586
587 if( vg_input.display_input_method == k_input_method_kbm )
588 menu_setitem_type( menu.ctr_kbm, k_ent_menuitem_type_visual_nocol );
589 else{
590 if( vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS3 ||
591 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS4 ||
592 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS5 ){
593 menu_setitem_type( menu.ctr_ps, k_ent_menuitem_type_visual_nocol );
594 }
595 else {
596 menu_setitem_type( menu.ctr_xbox, k_ent_menuitem_type_visual_nocol );
597 }
598 /* FIXME: Steam/Deck controller detection? */
599 }
600 }
601
602 static void menu_binding_string( char buf[128], u32 pstr );
603
604 /*
605 * Run from vg_gui when active
606 */
607 void menu_render(void)
608 {
609 glEnable(GL_BLEND);
610 glDisable(GL_DEPTH_TEST);
611 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
612 glBlendEquation(GL_FUNC_ADD);
613
614 shader_blitcolour_use();
615 v4f colour;
616 ui_hex_to_norm( ui_colour( k_ui_bg+3 ), colour );
617 colour[3] = 0.5f;
618
619 shader_blitcolour_uColour( colour );
620 render_fsquad();
621
622 if( (workshop_form.page != k_workshop_form_hidden) ||
623 (vg_ui.focused_control_type != k_ui_control_none) ){
624 return;
625 }
626
627 if( vg.settings_open )
628 return;
629
630 if( menu.credits_open ){
631 ui_rect panel = { 0,0, 460, 400 },
632 screen = { 0,0, vg.window_x,vg.window_y };
633 ui_rect_center( screen, panel );
634 ui_fill( panel, ui_colour(k_ui_bg) );
635 ui_outline( panel, 1, ui_colour(k_ui_fg), 0 );
636 ui_rect_pad( panel, (ui_px[]){8,8} );
637
638 ui_rect title;
639 ui_split( panel, k_ui_axis_h, 28*2, 0, title, panel );
640 ui_text( title, "Skate Rift - Credits", 2, k_ui_align_middle_center, 0 );
641 ui_split( panel, k_ui_axis_h, 28, 0, title, panel );
642 ui_text( title, "Mt.Zero Software", 1, k_ui_align_middle_center, 0 );
643
644 ui_split( panel, k_ui_axis_h, 8, 0, title, panel );
645 ui_split( panel, k_ui_axis_h, 28*2, 0, title, panel );
646 ui_text( title, "Free Software", 2, k_ui_align_middle_center, 0 );
647
648 ui_split( panel, k_ui_axis_h, 8, 0, title, panel );
649 ui_text( panel,
650 "Sam Lantinga - SDL2 - libsdl.org\n"
651 "Hunter WB - Anyascii\n"
652 "David Herberth - GLAD\n"
653 "Dominic Szablewski - QOI - qoiformat.org\n"
654 "Sean Barrett - stb_image,stb_vorbis,stb_include\n"
655 "Khronos Group - OpenGL\n"
656 , 1, k_ui_align_left, 0 );
657 return;
658 }
659
660 glEnable( GL_DEPTH_TEST );
661 glDisable( GL_BLEND );
662
663 f32 rate = vg.time_frame_delta * 12.0f;
664
665 if( menu.cam ){
666 vg_camera target;
667
668 target.fov = menu.cam->fov;
669 v3_copy( menu.cam->transform.co, target.pos );
670
671 v3f v0;
672 mdl_transform_vector( &menu.cam->transform, (v3f){0.0f,-1.0f,0.0f}, v0 );
673 v3_angles( v0, target.angles );
674
675 vg_camera_lerp( &menu.view, &target, rate, &menu.view );
676
677 menu.view.farz = 150.0f;
678 menu.view.nearz = 0.01f;
679
680 vg_camera_update_transform( &menu.view );
681 vg_camera_update_view( &menu.view );
682 vg_camera_update_projection( &menu.view );
683 vg_camera_finalize( &menu.view );
684 }
685 else return;
686
687 shader_model_menu_use();
688 shader_model_menu_uTexMain( 1 );
689 shader_model_menu_uPv( menu.view.mtx.pv );
690 shader_model_menu_uPvmPrev( menu.view.mtx_prev.pv );
691
692 mesh_bind( &menu.mesh );
693
694 v4f white, blue;
695
696 ui_hex_to_norm( ui_colour( k_ui_fg ), white );
697 ui_hex_to_norm( ui_colour( k_ui_orange+k_ui_brighter ), blue );
698
699 ent_menuitem *text_list[ 8 ];
700 u32 text_count = 0;
701
702 u32 current_mat = 0xffffffff;
703
704 for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
705 ent_menuitem *item = mdl_arritm( &menu.items, i );
706
707 if( item->type == k_ent_menuitem_type_disabled ) continue;
708 if( item->type == k_ent_menuitem_type_page ) continue;
709 if( !(item->groups & (0x1 << menu.page)) ) continue;
710
711 if( item->type == k_ent_menuitem_type_binding ){
712 if( text_count < vg_list_size(text_list) )
713 text_list[ text_count ++ ] = item;
714 else
715 vg_fatal_error( "Text list overflow" );
716
717 continue;
718 }
719
720 int selected = 0;
721
722 if( menu.loc ){
723 if( menu.loc->type == k_ent_menuitem_type_slider ){
724 u32 subid = menu.loc->slider.id_handle;
725 if( item == mdl_arritm( &menu.items, mdl_entity_id_id(subid) ))
726 selected = 1;
727 }
728 else{
729 if( item == menu.loc )
730 selected = 1;
731 }
732 }
733
734 if( item->type == k_ent_menuitem_type_visual_nocol ){
735 shader_model_menu_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
736 }
737 else{
738 v4f colour;
739 item->factive = vg_lerpf( item->factive, selected, rate );
740 v4_lerp( white, blue, item->factive, colour );
741 shader_model_menu_uColour( colour );
742 }
743
744 f32 scale = 1.0f+item->factive*0.1f;
745
746 m4x3f mmdl;
747 mdl_transform transform = item->transform;
748 v3_muls( transform.s, scale, transform.s );
749 mdl_transform_m4x3( &transform, mmdl );
750
751 if( item->type == k_ent_menuitem_type_toggle && item->pi32 ){
752 u32 subid = mdl_entity_id_id( item->checkmark.id_check );
753 ent_menuitem *subitem = mdl_arritm( &menu.items, subid );
754
755 v3_muladds( item->transform.co, item->checkmark.offset, scale,
756 subitem->transform.co );
757
758 subitem->fvisible = vg_lerpf( subitem->fvisible, *item->pi32, rate );
759 v3_fill( subitem->transform.s, subitem->fvisible );
760 }
761 else if( item->type == k_ent_menuitem_type_slider && item->pf32 ){
762 u32 il = mdl_entity_id_id( item->slider.id_min ),
763 ir = mdl_entity_id_id( item->slider.id_max ),
764 ih = mdl_entity_id_id( item->slider.id_handle );
765 ent_marker *ml = mdl_arritm( &menu.markers, il ),
766 *mr = mdl_arritm( &menu.markers, ir );
767 ent_menuitem *handle = mdl_arritm( &menu.items, ih );
768
769 v3_lerp( ml->transform.co, mr->transform.co, *item->pf32,
770 handle->transform.co );
771 }
772
773 shader_model_menu_uMdl( mmdl );
774
775 for( u32 j=0; j<item->submesh_count; j++ )
776 {
777 u32 index = item->submesh_start + j;
778 mdl_submesh *sm = mdl_arritm( &menu.model.submeshs, index );
779
780 if( sm->material_id != current_mat )
781 {
782 mdl_material *mat = mdl_arritm( &menu.model.materials,
783 sm->material_id-1 );
784 glActiveTexture( GL_TEXTURE1 );
785
786 if( mat->shader == k_shader_standard )
787 {
788 struct shader_props_standard *props = mat->props.compiled;
789
790 /* FIXME: why does menu have its own texture array?? */
791 glBindTexture( GL_TEXTURE_2D,
792 menu.textures[ props->tex_diffuse ] );
793 }
794 else
795 {
796 glBindTexture( GL_TEXTURE_2D, vg.tex_missing );
797 }
798
799 current_mat = sm->material_id;
800 }
801
802 mdl_draw_submesh( sm );
803 }
804 }
805
806 if( !text_count ) return;
807
808 char buf[ 128 ];
809
810 m4x3f local;
811 m4x3_identity( local );
812
813 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &menu.view );
814 for( u32 i=0; i<text_count; i++ ){
815 ent_menuitem *item = text_list[ i ];
816 m4x3f transform;
817 mdl_transform_m4x3( &item->transform, transform );
818
819 u32 variant = item->binding.font_variant;
820 menu_binding_string( buf, item->binding.pstr_bind );
821 f32 offset = font3d_string_width( variant, buf );
822
823 local[3][0] = -0.5f * offset;
824 m4x3_mul( transform, local, transform );
825
826 font3d_simple_draw( variant, buf, &menu.view, transform );
827 }
828 }
829
830 static void menu_binding_string( char buf[128], u32 pstr ){
831 vg_str str;
832 vg_strnull( &str, buf, 128 );
833
834 if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_jump" ) ){
835 vg_input_string( &str, input_button_list[k_srbind_jump], 1 );
836 }
837 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick0" ) ){
838 vg_strcat( &str, "SHUVIT " );
839 vg_input_string( &str, input_button_list[k_srbind_trick0], 1 );
840 }
841 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick1" ) ){
842 vg_strcat( &str, "KICKFLIP " );
843 vg_input_string( &str, input_button_list[k_srbind_trick1], 1 );
844 }
845 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick2" ) ){
846 vg_strcat( &str, "TREFLIP " );
847 vg_input_string( &str, input_button_list[k_srbind_trick2], 1 );
848 }
849 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_grab" ) ){
850 vg_input_string( &str, input_axis_list[k_sraxis_grab], 1 );
851 }
852 else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_grab_mod" ) ){
853 vg_input_string( &str, input_joy_list[k_srjoystick_grab], 1 );
854 }
855 else
856 vg_strcat( &str, "error" );
857 }