small updates and api changes
[carveJwlIkooP6JGAAIwe30JlM.git] / menu.h
diff --git a/menu.h b/menu.h
index dd2cf686d8b3cf286f29afeca822998fd2212240..2071440c365c2bd24a89ab102deb59a94e819e57 100644 (file)
--- a/menu.h
+++ b/menu.h
@@ -5,24 +5,31 @@
 #include "model.h"
 #include "world_render.h"
 #include "player.h"
-#include "conf.h"
 #include "shaders/model_menu.h"
 #include "audio.h"
 #include "input.h"
+#include "workshop.h"
+#include "respawn.h"
 
 #define MENU_STACK_SIZE 8
 
 struct {
-   int active;
-   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_mouse,
-      k_menu_input_mode_keys
+      k_menu_input_mode_keys,
+      k_menu_input_mode_mouse
    }
    input_mode;
    f32 mouse_track, mouse_dist;  /* used for waking up mouse */
@@ -50,8 +57,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 +100,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.enabled = 0;
+}
+
+static void menu_init(void){
    void *alloc = vg_mem.rtmemory;
 
    mdl_open( &menu.model, "models/rs_menu.mdl", alloc );
@@ -130,13 +164,11 @@ 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)
-{
+static void menu_back_page(void){
    vg_info( "menu_back_page()\n" );
    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,8 +183,7 @@ static void menu_back_page(void)
 /*
  * Open page to the string identifier
  */
-static void menu_open_page( const char *name )
-{
+static void menu_open_page( const char *name ){
    if( menu.page_depth >= MENU_STACK_SIZE )
       vg_fatal_error( "Stack overflow\n" );
 
@@ -198,20 +229,37 @@ 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" ) ){
+
+      /* DEPRECATED? */
+      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;
+         menu_close();
+      }
+      /* DEPRECATED? */
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "reset_home" ) ){
+         world_set_active_instance( 0 );
+         localplayer.viewable_world = world_current_instance();
+         localplayer_cmd_respawn( 1, (const char *[]){"start"} );
+         menu_close();
+      }
+
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "reset" ) ){
+         menu_close();
+         respawn_begin_chooser();
+      }
+
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "credits" ) ){
+         menu.credits_open = 1;
+      }
+      else if( MDL_CONST_PSTREQ( &menu.model, q, "workshop" ) ){
+         workshop_submit_command(0,NULL);
       }
    }
    else if( item->type == k_ent_menuitem_type_page_button ){
@@ -224,8 +272,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 ){
@@ -241,25 +288,38 @@ 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( escape ){
+         menu.credits_open = 0;
+      }
+      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" );
          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
@@ -270,8 +330,7 @@ static void menu_update(void)
        md = button_down( k_srbind_mdown ),
        mh = ml-mr,
        mv = mu-md,
-       enter = button_down( k_srbind_maccept ),
-       escape = button_down( k_srbind_mback );
+       enter = button_down( k_srbind_maccept );
 
    if( mh||mv||enter ){
       menu.input_mode = k_menu_input_mode_keys;
@@ -483,13 +542,32 @@ 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 );
+   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 );
+      }
+      else {
+         menu_setitem_type( menu.ctr_xbox, k_ent_menuitem_type_visual );
+      }
+      /* FIXME: Steam/Deck controller detection? */
+   }
 }
 
 /*
  * Run from vg_gui when active
  */
-VG_STATIC void menu_render(void)
-{
+VG_STATIC void menu_render(void){
    glEnable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
@@ -503,6 +581,46 @@ VG_STATIC void menu_render(void)
    shader_blitcolour_uColour( colour );
    render_fsquad();
 
+   if( (workshop_form.page != k_workshop_form_hidden) ||
+       (vg_ui.focused_control_type != k_ui_control_none) ){
+      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), 0 );
+      ui_rect_pad( panel, (ui_px[]){8,8} );
+
+      ui_rect title;
+      ui_split( panel, k_ui_axis_h, 28*2, 0, title, panel );
+      ui_text( title, "Skate Rift - Credits", 2, k_ui_align_middle_center, 0 );
+      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 );
+
+      ui_split( panel, k_ui_axis_h, 8, 0, title, panel );
+      ui_text( panel, 
+            "Sam Lantinga       - SDL2 - libsdl.org\n"
+            "Hunter WB          - Anyascii\n"
+            "David Herberth     - GLAD\n"
+            "Dominic Szablewski - QOI - qoiformat.org\n"
+            "Sean Barrett       - stb_image,stb_vorbis,stb_include\n"
+            "Khronos Group      - OpenGL\n"
+            , 1, k_ui_align_left, 0 );
+      return;
+   }
+
    glEnable( GL_DEPTH_TEST );
    glDisable( GL_BLEND );
 
@@ -547,6 +665,7 @@ VG_STATIC void menu_render(void)
    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;