glider orphan / entity normals with scale
[carveJwlIkooP6JGAAIwe30JlM.git] / menu.h
diff --git a/menu.h b/menu.h
index fcf2da4c28a6abb47855a82f212f9962b9abccf9..73e048f9b17ca1c0f251726a002e42651f5154ef 100644 (file)
--- a/menu.h
+++ b/menu.h
@@ -9,16 +9,25 @@
 #include "audio.h"
 #include "input.h"
 #include "workshop.h"
+#include "world_map.h"
+#include "gui.h"
+#include "ent_miniworld.h"
 
 #define MENU_STACK_SIZE 8
 
 struct {
-   int active, credits_open;
-   f32 factive;
+   int credits_open;
    int disable_open;
 
    u32 page, /* current page index */
-       page_depth;
+       page_depth,
+       controls_page_id;
+
+   ent_menuitem *ctr_kbm,
+                *ctr_deck,
+                *ctr_ps,
+                *ctr_steam,
+                *ctr_xbox;
 
    enum menu_input_mode{
       k_menu_input_mode_keys,
@@ -40,7 +49,7 @@ struct {
    camera        view;
 
    mdl_context model;
-   GLuint texture;
+   GLuint *textures;
    glmesh mesh;
 
    mdl_array_ptr items, markers, cameras;
@@ -50,8 +59,7 @@ static menu;
 /*
  * Attaches memory locations to the various items in the menu
  */
-static void menu_link(void)
-{
+static void menu_link(void){
    /* link data locations */
    for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
       ent_menuitem *item = mdl_arritm( &menu.items, i );
@@ -94,10 +102,38 @@ static void menu_link(void)
          item->pvoid = NULL;
       }
    }
+
+   /* link controllers */
+   menu.ctr_deck = NULL;
+   menu.ctr_kbm = NULL;
+   menu.ctr_ps = NULL;
+   menu.ctr_steam = NULL;
+   menu.ctr_xbox = NULL;
+
+   for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
+      ent_menuitem *item = mdl_arritm( &menu.items, i );
+
+      if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "deck" ) )
+         menu.ctr_deck = item;
+      if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "kbm" ) )
+         menu.ctr_kbm = item;
+      if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "ps" ) )
+         menu.ctr_ps = item;
+      if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "steam" ) )
+         menu.ctr_steam = item;
+      if( MDL_CONST_PSTREQ( &menu.model, item->visual.pstr_name, "xbox" ) )
+         menu.ctr_xbox = item;
+   }
 }
 
-static void menu_init(void)
-{
+static void menu_close(void){
+   skaterift.activity = k_skaterift_default;
+   menu.page_depth = 0;
+   menu.page = 0xffffffff;
+   srinput.state = k_input_state_resume;
+}
+
+static void menu_init(void){
    void *alloc = vg_mem.rtmemory;
 
    mdl_open( &menu.model, "models/rs_menu.mdl", alloc );
@@ -105,23 +141,27 @@ static void menu_init(void)
 
    vg_linear_clear( vg_mem.scratch );
 
-   mdl_load_array( &menu.model, &menu.items,   "ent_menuitem", alloc );
-   mdl_load_array( &menu.model, &menu.markers, "ent_marker", alloc );
-   mdl_load_array( &menu.model, &menu.cameras, "ent_camera", alloc );
+   MDL_LOAD_ARRAY( &menu.model, &menu.items,   ent_menuitem, alloc );
+   MDL_LOAD_ARRAY( &menu.model, &menu.markers, ent_marker, alloc );
+   MDL_LOAD_ARRAY( &menu.model, &menu.cameras, ent_camera, alloc );
 
-   vg_linear_clear( vg_mem.scratch );
+   u32 count = mdl_arrcount( &menu.model.textures );
+   menu.textures = vg_linear_alloc(alloc,vg_align8(sizeof(GLuint)*(count+1)));
+   menu.textures[0] = vg.tex_missing;
 
-   if( !mdl_arrcount( &menu.model.textures ) )
-      vg_fatal_error( "No texture in menu file" );
+   mdl_async_load_glmesh( &menu.model, &menu.mesh, NULL );
 
-   mdl_texture *tex0 = mdl_arritm( &menu.model.textures, 0 );
-   void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
-   mdl_fread_pack_file( &menu.model, &tex0->file, data );
+   for( u32 i=0; i<count; i ++ ){
+      vg_linear_clear( vg_mem.scratch );
+      menu.textures[i+1] = vg.tex_missing;
 
-   mdl_async_load_glmesh( &menu.model, &menu.mesh );
-   vg_tex2d_load_qoi_async( data, tex0->file.pack_size, 
-                            VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
-                            &menu.texture );
+      mdl_texture *tex = mdl_arritm( &menu.model.textures, i );
+      void *data = vg_linear_alloc( vg_mem.scratch, tex->file.pack_size );
+      mdl_fread_pack_file( &menu.model, &tex->file, data );
+      vg_tex2d_load_qoi_async( data, tex->file.pack_size, 
+                               VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
+                               &menu.textures[i+1] );
+   }
 
    mdl_close( &menu.model );
    shader_model_menu_register();
@@ -130,13 +170,10 @@ static void menu_init(void)
 /*
  * Drop back a page until we're at the bottom which then we jus quit
  */
-static void menu_back_page(void)
-{
-   vg_info( "menu_back_page()\n" );
+static void menu_back_page(void){
    menu.page_depth --;
    if( menu.page_depth == 0 ){
-      menu.active = 0;
-      menu.page = 0xffffffff;
+      menu_close();
    }
    else{
       menu.page = menu.page_stack[ menu.page_depth ].page;
@@ -151,12 +188,17 @@ static void menu_back_page(void)
 /*
  * Open page to the string identifier
  */
-static void menu_open_page( const char *name )
-{
-   if( menu.page_depth >= MENU_STACK_SIZE )
-      vg_fatal_error( "Stack overflow\n" );
-
-   vg_info( "Try to open %s\n", name );
+static void menu_open_page( const char *name, 
+                            enum ent_menuitem_stack_behaviour stackmode ){
+   srinput.state = k_input_state_resume;
+   if( stackmode == k_ent_menuitem_stack_append ){
+      if( menu.page_depth >= MENU_STACK_SIZE )
+         vg_fatal_error( "Stack overflow\n" );
+   }
+   else{
+      if( menu.page_depth == 0 )
+         vg_fatal_error( "Stack underflow\n" );
+   }
 
    u32 hash = vg_strdjb2( name );
    for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
@@ -167,12 +209,17 @@ static void menu_open_page( const char *name )
             u32 new_page = __builtin_ctz( item->groups );
 
             if( new_page == menu.page ){
-               menu_back_page();
+               if( stackmode != k_ent_menuitem_stack_replace )
+                  menu_back_page();
             }
             else{
-               menu.page_stack[ menu.page_depth    ].page = menu.page;
-               menu.page_stack[ menu.page_depth    ].cam = menu.cam;
-               menu.page_stack[ menu.page_depth ++ ].loc = menu.loc;
+               menu.page_stack[ menu.page_depth ].page = menu.page;
+               menu.page_stack[ menu.page_depth ].cam = menu.cam;
+               menu.page_stack[ menu.page_depth ].loc = menu.loc;
+               
+               if( stackmode == k_ent_menuitem_stack_append )
+                  menu.page_depth ++;
+
                menu.page = __builtin_ctz( item->groups );
 
                if( menu.input_mode == k_menu_input_mode_keys ){
@@ -186,8 +233,6 @@ static void menu_open_page( const char *name )
                   u32 id = mdl_entity_id_id( item->page.id_viewpoint );
                   menu.cam = mdl_arritm( &menu.cameras, id );
                }
-               vg_info( "menu page: %u (%p,%p)\n", 
-                        menu.page, menu.loc, menu.cam );
             }
             return;
          }
@@ -198,30 +243,22 @@ static void menu_open_page( const char *name )
 /*
  * activate a pressable type
  */
-static void menu_trigger_item( ent_menuitem *item )
-{
+static void menu_trigger_item( ent_menuitem *item ){
    if     ( item->type == k_ent_menuitem_type_event_button ){
       u32 q = item->button.pstr;
 
       if( MDL_CONST_PSTREQ( &menu.model, q, "quit" ) ){
          vg.window_should_close = 1;
       }
-      else if( MDL_CONST_PSTREQ( &menu.model, q, "reset_nearest" ) ){
-         localplayer_cmd_respawn( 0, NULL );
-         
-         menu.page_depth = 0;
-         menu.active = 0;
-         menu.page = 0xffffffff;
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "map" ) ){
+         menu_close();
+         world_map_enter();
       }
-      else if( MDL_CONST_PSTREQ( &menu.model, q, "reset_home" ) ){
-         world_static.active_world = 0;
-         world_static.active_trigger_volume_count = 0;
-         localplayer.viewable_world = world_current_instance();
-         localplayer_cmd_respawn( 1, (const char *[]){"start"} );
-
-         menu.page_depth = 0;
-         menu.active = 0;
-         menu.page = 0xffffffff;
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "hub" ) ){
+         if( world_static.active_instance == k_world_purpose_client ){
+            menu_close();
+            ent_miniworld_goback();
+         }
       }
       else if( MDL_CONST_PSTREQ( &menu.model, q, "credits" ) ){
          menu.credits_open = 1;
@@ -229,9 +266,21 @@ static void menu_trigger_item( ent_menuitem *item )
       else if( MDL_CONST_PSTREQ( &menu.model, q, "workshop" ) ){
          workshop_submit_command(0,NULL);
       }
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "engine" ) ){
+         vg_settings_open();
+      }
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "prem_store" ) ){
+         if( steam_ready )
+            SteamAPI_ISteamFriends_ActivateGameOverlayToStore( 
+                  SteamAPI_SteamFriends(), 2103940, k_EOverlayToStoreFlag_None);
+      }
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "prem_nevermind" ) ){
+         menu_close();
+      }
    }
    else if( item->type == k_ent_menuitem_type_page_button ){
-      menu_open_page( mdl_pstr( &menu.model, item->button.pstr ) );
+      menu_open_page( mdl_pstr( &menu.model, item->button.pstr ),
+                      item->button.stack_behaviour );
    }
    else if( item->type == k_ent_menuitem_type_toggle ){
       if( item->pi32 ){
@@ -240,8 +289,7 @@ static void menu_trigger_item( ent_menuitem *item )
    }
 }
 
-static f32 menu_slider_snap( f32 value, f32 old, f32 notch )
-{
+static f32 menu_slider_snap( f32 value, f32 old, f32 notch ){
    f32 const k_epsilon = 0.0125f;
 
    if( fabsf(notch-value) < k_epsilon ){
@@ -257,37 +305,41 @@ static f32 menu_slider_snap( f32 value, f32 old, f32 notch )
       return value;
 }
 
+static void menu_setitem_type( ent_menuitem *item, 
+                               enum ent_menuitem_type type ){
+   if( !item ) return;
+   item->type = type;
+}
+
 /* 
  * Run from vg_gui every frame
  */
-static void menu_update(void)
-{
+static void menu_update(void){
    if( workshop_form.page != k_workshop_form_hidden ){
       return;
    }
 
    int escape = button_down( k_srbind_mback );
-   if( menu.credits_open ){
+   if( menu.credits_open || vg.settings_open ){
       if( escape ){
          menu.credits_open = 0;
+
+         if( vg.settings_open )
+            vg_settings_close();
       }
       return;
    }
 
    if( button_down( k_srbind_mopen ) ){
-      if( !menu.active && !menu.disable_open ){
-         menu.active = 1;
+      if( skaterift.activity == k_skaterift_default ){
+         skaterift.activity = k_skaterift_menu;
          menu.page = 0xffffffff;
-         menu_open_page( "Main Menu" );
+         menu_open_page( "Main Menu", k_ent_menuitem_stack_append );
          return;
       }
    }
 
-   menu.factive = vg_lerpf( menu.factive, menu.active, 
-                            vg.time_frame_delta * 6.0f );
-
-   if( !menu.active ) return;
-
+   if( skaterift.activity != k_skaterift_menu ) return;
    enum menu_input_mode prev_mode = menu.input_mode;
 
    /* get buttons inputs
@@ -389,7 +441,9 @@ static void menu_update(void)
          ent_menuitem *item = mdl_arritm( &menu.items, i );
 
          if( item->type == k_ent_menuitem_type_page ) continue;
-         if( item->type == k_ent_menuitem_type_visual ) continue;
+         if( (item->type == k_ent_menuitem_type_visual) ||
+             (item->type == k_ent_menuitem_type_visual_nocol) ) continue;
+         if( item->type == k_ent_menuitem_type_binding ) continue;
          if( !(item->groups & (0x1<<menu.page)) ) continue;
 
          ent_menuitem *ray_item = item;
@@ -447,6 +501,7 @@ static void menu_update(void)
             
             if( (item->type != k_ent_menuitem_type_page) &&
                 (item->type != k_ent_menuitem_type_visual) &&
+                (item->type != k_ent_menuitem_type_visual_nocol) &&
                 (item->groups & (0x1<<menu.page)) ){
                menu.loc = item;
             }
@@ -510,13 +565,34 @@ static void menu_update(void)
          }
       }
    }
+
+   menu_setitem_type( menu.ctr_deck, k_ent_menuitem_type_disabled );
+   menu_setitem_type( menu.ctr_ps, k_ent_menuitem_type_disabled );
+   menu_setitem_type( menu.ctr_kbm, k_ent_menuitem_type_disabled );
+   menu_setitem_type( menu.ctr_xbox, k_ent_menuitem_type_disabled );
+   menu_setitem_type( menu.ctr_steam, k_ent_menuitem_type_disabled );
+
+   if( vg_input.display_input_method == k_input_method_kbm )
+      menu_setitem_type( menu.ctr_kbm, k_ent_menuitem_type_visual_nocol );
+   else{
+      if( vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS3 ||
+          vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS4 ||
+          vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS5 ){
+         menu_setitem_type( menu.ctr_ps, k_ent_menuitem_type_visual_nocol );
+      }
+      else {
+         menu_setitem_type( menu.ctr_xbox, k_ent_menuitem_type_visual_nocol );
+      }
+      /* FIXME: Steam/Deck controller detection? */
+   }
 }
 
+static void menu_binding_string( char buf[128], u32 pstr );
+
 /*
  * Run from vg_gui when active
  */
-VG_STATIC void menu_render(void)
-{
+static void menu_render(void){
    glEnable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
@@ -535,12 +611,15 @@ VG_STATIC void menu_render(void)
       return;
    }
 
+   if( vg.settings_open )
+      return;
+
    if( menu.credits_open ){
       ui_rect panel = { 0,0, 460, 400 },
               screen = { 0,0, vg.window_x,vg.window_y };
       ui_rect_center( screen, panel );
       ui_fill( panel, ui_colour(k_ui_bg) );
-      ui_outline( panel, 1, ui_colour(k_ui_fg) );
+      ui_outline( panel, 1, ui_colour(k_ui_fg), 0 );
       ui_rect_pad( panel, (ui_px[]){8,8} );
 
       ui_rect title;
@@ -549,11 +628,6 @@ VG_STATIC void menu_render(void)
       ui_split( panel, k_ui_axis_h, 28, 0, title, panel );
       ui_text( title, "Mt.Zero Software", 1, k_ui_align_middle_center, 0 );
 
-      ui_split( panel, k_ui_axis_h, 8, 0, title, panel );
-      ui_split( panel, k_ui_axis_h, 28, 0, title, panel );
-      ui_text( title, "A game by Harry Godden", 1, 
-               k_ui_align_middle_center, 0 );
-
       ui_split( panel, k_ui_axis_h, 8, 0, title, panel );
       ui_split( panel, k_ui_axis_h, 28*2, 0, title, panel );
       ui_text( title, "Free Software", 2, k_ui_align_middle_center, 0 );
@@ -583,7 +657,7 @@ VG_STATIC void menu_render(void)
 
       v3f v0;
       mdl_transform_vector( &menu.cam->transform, (v3f){0.0f,-1.0f,0.0f}, v0 );
-      player_vector_angles( target.angles, v0, 1.0f, 0.0f );
+      v3_angles( v0, target.angles );
 
       camera_lerp( &menu.view, &target, rate, &menu.view );
 
@@ -599,8 +673,6 @@ VG_STATIC void menu_render(void)
 
    shader_model_menu_use();
    shader_model_menu_uTexMain( 1 );
-   glActiveTexture( GL_TEXTURE1 );
-   glBindTexture( GL_TEXTURE_2D, menu.texture );
    shader_model_menu_uPv( menu.view.mtx.pv );
    shader_model_menu_uPvmPrev( menu.view.mtx_prev.pv );
 
@@ -611,12 +683,27 @@ VG_STATIC void menu_render(void)
    ui_hex_to_norm( ui_colour( k_ui_fg ), white );
    ui_hex_to_norm( ui_colour( k_ui_orange+k_ui_brighter ), blue );
 
+   ent_menuitem *text_list[ 8 ];
+   u32 text_count = 0;
+
+   u32 current_tex = 0xffffffff;
+
    for( u32 i=0; i<mdl_arrcount(&menu.items); i++ ){
       ent_menuitem *item = mdl_arritm( &menu.items, i );
 
+      if(   item->type == k_ent_menuitem_type_disabled ) continue;
       if(   item->type == k_ent_menuitem_type_page ) continue;
       if( !(item->groups & (0x1 << menu.page)) ) continue;
 
+      if(   item->type == k_ent_menuitem_type_binding ){
+         if( text_count < vg_list_size(text_list) )
+            text_list[ text_count ++ ] = item;
+         else
+            vg_fatal_error( "Text list overflow" );
+
+         continue;  
+      }
+
       int selected = 0;
       
       if( menu.loc ){
@@ -631,10 +718,15 @@ VG_STATIC void menu_render(void)
          }
       }
 
-      item->factive = vg_lerpf( item->factive, selected, rate );
-      v4f colour;
-      v4_lerp( white, blue, item->factive, colour );
-      shader_model_menu_uColour( colour );
+      if( item->type == k_ent_menuitem_type_visual_nocol ){
+         shader_model_menu_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
+      }
+      else{
+         v4f colour;
+         item->factive = vg_lerpf( item->factive, selected, rate );
+         v4_lerp( white, blue, item->factive, colour );
+         shader_model_menu_uColour( colour );
+      }
 
       f32 scale = 1.0f+item->factive*0.1f;
 
@@ -669,9 +761,72 @@ VG_STATIC void menu_render(void)
 
       for( u32 j=0; j<item->submesh_count; j++ ){
          u32 index = item->submesh_start + j;
-         mdl_draw_submesh( mdl_arritm( &menu.model.submeshs, index ));
+         mdl_submesh *sm = mdl_arritm( &menu.model.submeshs, index );
+         
+         mdl_material *mat = mdl_arritm( &menu.model.materials, 
+                                         sm->material_id-1 );
+
+         if( mat->tex_diffuse != current_tex ){
+            glActiveTexture( GL_TEXTURE1 );
+            glBindTexture( GL_TEXTURE_2D, menu.textures[ mat->tex_diffuse ] );
+            current_tex = mat->tex_diffuse;
+         }
+
+         mdl_draw_submesh( sm );
       }
    }
+
+   if( !text_count ) return;
+
+   char buf[ 128 ];
+
+   m4x3f local;
+   m4x3_identity( local );
+
+   font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &menu.view );
+   for( u32 i=0; i<text_count; i++ ){
+      ent_menuitem *item = text_list[ i ];
+      m4x3f transform;
+      mdl_transform_m4x3( &item->transform, transform );
+
+      u32 variant = item->binding.font_variant;
+      menu_binding_string( buf, item->binding.pstr_bind );
+      f32 offset = font3d_string_width( variant, buf );
+
+      local[3][0] = -0.5f * offset;
+      m4x3_mul( transform, local, transform );
+
+      font3d_simple_draw( variant, buf, &menu.view, transform );
+   }
+}
+
+static void menu_binding_string( char buf[128], u32 pstr ){
+   vg_str str;
+   vg_strnull( &str, buf, 128 );
+
+   if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_jump" ) ){
+      vg_input_string( &str, input_button_list[k_srbind_jump], 1 );
+   }
+   else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick0" ) ){
+      vg_strcat( &str, "SHUVIT " );
+      vg_input_string( &str, input_button_list[k_srbind_trick0], 1 );
+   }
+   else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick1" ) ){
+      vg_strcat( &str, "KICKFLIP " );
+      vg_input_string( &str, input_button_list[k_srbind_trick1], 1 );
+   }
+   else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_trick2" ) ){
+      vg_strcat( &str, "TREFLIP " );
+      vg_input_string( &str, input_button_list[k_srbind_trick2], 1 );
+   }
+   else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_grab" ) ){
+      vg_input_string( &str, input_axis_list[k_sraxis_grab], 1 );
+   }
+   else if( MDL_CONST_PSTREQ( &menu.model, pstr, "bind_grab_mod" ) ){
+      vg_input_string( &str, input_joy_list[k_srjoystick_grab], 1 );
+   }
+   else
+      vg_strcat( &str, "error" );
 }
 
 #endif /* MENU_H */