+}
+
+/*
+ * activate a pressable type
+ */
+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" ) ){
+
+ /* FIXME: Should only be availible in CLIENT WORLD */
+
+ menu_close();
+ respawn_begin_chooser();
+
+ /* FIXME: Need a back to hub button */
+#if 0
+ srinput.state = k_input_state_resume;
+ menu_close();
+ ent_miniworld_goback();
+#endif
+ }
+
+ 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 ){
+ 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 ){
+ *item->pi32 = *item->pi32 ^ 0x1;
+ }
+ }
+}
+
+static f32 menu_slider_snap( f32 value, f32 old, f32 notch ){
+ f32 const k_epsilon = 0.0125f;
+
+ if( fabsf(notch-value) < k_epsilon ){
+ if( fabsf(notch-old) > k_epsilon ){
+ audio_lock();
+ audio_oneshot( &audio_ui[0], 1.0f, 0.0f );
+ audio_unlock();
+ }
+
+ return notch;
+ }
+ else
+ 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){
+ 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( skaterift.activity == k_skaterift_default ){
+ skaterift.activity = k_skaterift_menu;
+ menu.page = 0xffffffff;
+ menu_open_page( "Main Menu", k_ent_menuitem_stack_append );
+ return;
+ }
+ }
+
+ if( skaterift.activity != k_skaterift_menu ) return;
+ enum menu_input_mode prev_mode = menu.input_mode;
+
+ /* get buttons inputs
+ * -------------------------------------------------------------------*/
+ int ml = button_down( k_srbind_mleft ),
+ mr = button_down( k_srbind_mright ),
+ mu = button_down( k_srbind_mup ),
+ md = button_down( k_srbind_mdown ),
+ mh = ml-mr,
+ mv = mu-md,
+ enter = button_down( k_srbind_maccept );
+
+ if( mh||mv||enter ){
+ menu.input_mode = k_menu_input_mode_keys;
+ }
+
+ /* get mouse inputs
+ * --------------------------------------------------------------------*/
+ menu.mouse_dist += v2_length( vg.mouse_delta ); /* TODO: Move to UI */
+ menu.mouse_track += vg.time_frame_delta;
+ if( menu.mouse_track > 0.1f ){
+ menu.mouse_track = fmodf( menu.mouse_track, 0.1f );
+ if( menu.mouse_dist > 10.0f ){
+ menu.input_mode = k_menu_input_mode_mouse;
+ menu.mouse_dist = 0.0f;
+ }
+ }
+
+ if( ui_clicking(UI_MOUSE_LEFT) || ui_clicking(UI_MOUSE_RIGHT) ){
+ menu.input_mode = k_menu_input_mode_mouse;
+ }
+
+ if( menu.input_mode == k_menu_input_mode_mouse ){
+ /*
+ * handle mouse input
+ * ------------------------------------------------------------*/
+ vg_ui.wants_mouse = 1;
+
+ /*
+ * this raycasting is super cumbersome because all the functions were
+ * designed for other purposes. we dont care though.
+ */
+ m4x4f inverse;
+ m4x4_inv( menu.view.mtx.p, inverse );
+ v4f coords;
+ coords[0] = vg_ui.mouse[0];
+ coords[1] = vg.window_y - vg_ui.mouse[1];
+ v2_div( coords, (v2f){ vg.window_x, vg.window_y }, coords );
+ v2_muls( coords, 2.0f, coords );
+ v2_add( coords, (v2f){-1.0f,-1.0f}, coords );
+ coords[2] = 1.0f;
+ coords[3] = 1.0f;
+ m4x4_mulv( inverse, coords, coords );
+ v3f ray;
+ m3x3_mulv( menu.view.transform, coords, ray );
+ v3_normalize( ray );
+
+ if( menu.loc && (menu.loc->type == k_ent_menuitem_type_slider) &&
+ ui_clicking(UI_MOUSE_LEFT) && menu.loc->pf32 ){
+
+ u32 il = mdl_entity_id_id( menu.loc->slider.id_min ),
+ ir = mdl_entity_id_id( menu.loc->slider.id_max );
+ ent_marker *ml = mdl_arritm( &menu.markers, il ),
+ *mr = mdl_arritm( &menu.markers, ir );
+
+ v3f q2;
+ v3_muladds( menu.view.pos, ray, 100.0f, q2 );
+
+ f32 s,t;
+ v3f c1, c2;
+ v3f p1, q1, v0;
+ v3_sub( mr->transform.co, ml->transform.co, v0 );
+ v3_muladds( ml->transform.co, v0, -1.0f, p1 );
+ v3_muladds( mr->transform.co, v0, 1.0f, q1 );
+ closest_segment_segment( p1, q1, menu.view.pos, q2, &s,&t, c1,c2 );
+
+ s-=(1.0f/3.0f);
+ s/=(1.0f/3.0f);
+
+ if( ui_click_down(UI_MOUSE_LEFT) ){
+ menu.slider_offset = *menu.loc->pf32 - s;
+ }