TODO about preupdate call style
[carveJwlIkooP6JGAAIwe30JlM.git] / ent_skateshop.c
index ed222168ab59a94e2aab0df426645fa78b06e453..05999f9322c5793df9cccb00c7e547f14c03b460 100644 (file)
@@ -74,7 +74,8 @@ static void skateshop_async_preview_imageload( void *data, u32 len ){
    SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
 }
 
-static void skateshop_update_preview_image_thread(void *_args){
+static void skateshop_update_preview_image_thread(void *_args)
+{
    char path_buf[4096];
    vg_str folder;
    vg_strnull( &folder, path_buf, sizeof(path_buf) );
@@ -83,7 +84,8 @@ static void skateshop_update_preview_image_thread(void *_args){
    addon_reg *reg_preview = global_skateshop.reg_preview;
    SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
 
-   if( !addon_get_content_folder( reg_preview, &folder, 1 ) ){
+   if( !addon_get_content_folder( reg_preview, &folder, 1 ) )
+   {
       SDL_AtomicLock( &addon_system.sl_cache_using_resources );
       global_skateshop.reg_loaded_preview = reg_preview;
       SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
@@ -97,13 +99,16 @@ static void skateshop_update_preview_image_thread(void *_args){
 
    inf->reg = reg_preview;
 
-   if( vg_strgood( &folder ) ){
+   if( vg_strgood( &folder ) )
+   {
       stbi_set_flip_vertically_on_load(1);
       int x, y, nc;
       inf->data = stbi_load( folder.buffer, &x, &y, &nc, 3 );
 
-      if( inf->data ){
-         if( (x != WORKSHOP_PREVIEW_WIDTH) || (y != WORKSHOP_PREVIEW_HEIGHT) ){
+      if( inf->data )
+      {
+         if( (x != WORKSHOP_PREVIEW_WIDTH) || (y != WORKSHOP_PREVIEW_HEIGHT) )
+         {
             vg_error( "Resolution does not match framebuffer, so we can't"
                       " show it\n" );
             stbi_image_free( inf->data );
@@ -113,70 +118,36 @@ static void skateshop_update_preview_image_thread(void *_args){
 
       vg_async_dispatch( call, skateshop_async_preview_imageload );
    }
-}
-
-/*
- * op/subroutine: k_workshop_op_item_load
- * -----------------------------------------------------------------------------
- */
-
-static void world_scan_thread( void *_args ){
-   addon_mount_content_folder( k_addon_type_world, "maps", ".mdl" );
-   addon_mount_workshop_items();
-   vg_async_call( async_addon_reg_update, NULL, 0 );
-}
-
-/*
- * Asynchronous scan of local disk for worlds
- */
-static void skateshop_op_world_scan(void){
-   vg_loader_start( world_scan_thread, NULL );
-}
-
-static void board_processview_thread( void *_args ){
-   addon_cache_load_loop();
-}
-
-static void board_scan_thread( void *_args ){
-   addon_mount_content_folder( k_addon_type_board, "boards", ".mdl" );
-   addon_mount_workshop_items();
-   vg_async_call( async_addon_reg_update, NULL, 0 );
-   vg_async_stall();
-   board_processview_thread(NULL);
-}
-
-/* TODO: migrate to addon.c */
-static void skateshop_op_board_scan(void){
-   vg_loader_start( board_scan_thread, NULL );
-}
-
-/* TODO: migrate to addon.c */
-void skateshop_autostart_loading(void)
-{
-   if( !vg_loader_availible() ) return;
+   else
+   {
+      vg_error( "Path too long to workshop preview image.\n" );
 
-   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
-   if( global_skateshop.reg_preview != global_skateshop.reg_loaded_preview ){
+      SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+      global_skateshop.reg_loaded_preview = reg_preview;
       SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
-      vg_loader_start( skateshop_update_preview_image_thread, NULL );
-      return;
    }
+}
 
-   for( u32 type=0; type<k_addon_type_max; type++ ){
-      struct addon_cache *cache = &addon_system.cache[type];
-
-      for( u32 id=1; id<=cache->pool.count; id++ ){
-         addon_cache_entry *entry = vg_pool_item( &cache->pool, id );
-         if( entry->state == k_addon_cache_state_load_request ){
-            SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
-            vg_loader_start( board_processview_thread, NULL );
-            return;
-         }
+void skateshop_world_preview_preupdate(void)
+{
+   /* try to load preview image if we availible to do. */
+   if( vg_loader_availible() )
+   {
+      SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+      if( global_skateshop.reg_preview != global_skateshop.reg_loaded_preview )
+      {
+         SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+         vg_loader_start( skateshop_update_preview_image_thread, NULL );
       }
+      else SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
    }
-   SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
 }
 
+/*
+ * op/subroutine: k_workshop_op_item_load
+ * -----------------------------------------------------------------------------
+ */
+
 /*
  * Regular stuff
  * -----------------------------------------------------------------------------
@@ -238,12 +209,13 @@ static void skateshop_server_helper_update(void){
  * VG event preupdate 
  */
 void temp_update_playermodel(void);
-void ent_skateshop_preupdate( ent_skateshop *shop, int active )
+void ent_skateshop_preupdate( ent_focus_context *ctx )
 {
-   if( !active ) return;
+   if( !ctx->active ) 
+      return;
 
-   /* input filter */
-   world_instance *world = world_current_instance();
+   world_instance *world = ctx->world;
+   ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, ctx->index );
 
    /* camera positioning */
    ent_camera *ref = mdl_arritm( &world->ent_camera, 
@@ -317,7 +289,8 @@ void ent_skateshop_preupdate( ent_skateshop *shop, int active )
          localplayer.board_view_slot = cache_id;
          network_send_item( k_netmsg_playeritem_board );
 
-         world_entity_unfocus();
+         world_entity_exit_modal();
+         world_entity_clear_focus();
          gui_helper_clear();
          skaterift_autosave(1);
          return;
@@ -364,7 +337,8 @@ void ent_skateshop_preupdate( ent_skateshop *shop, int active )
 
       if( button_down( k_srbind_maccept ) ){
          network_send_item( k_netmsg_playeritem_player );
-         world_entity_unfocus();
+         world_entity_exit_modal();
+         world_entity_clear_focus();
          gui_helper_clear();
       }
    }
@@ -437,11 +411,13 @@ void ent_skateshop_preupdate( ent_skateshop *shop, int active )
       vg_fatal_error( "Unknown store (%u)\n", shop->type );
    }
 
-   if( button_down( k_srbind_mback ) ){
+   if( button_down( k_srbind_mback ) )
+   {
       if( shop->type == k_skateshop_type_charshop )
          network_send_item( k_netmsg_playeritem_player );
 
-      world_entity_unfocus();
+      world_entity_exit_modal();
+      world_entity_clear_focus();
       gui_helper_clear();
       return;
    }
@@ -639,14 +615,16 @@ static void skateshop_render_worldshop( ent_skateshop *shop ){
       if( !reg ) 
          goto none;
 
-      if( reg->alias.workshop_id ){
+      if( reg->alias.workshop_id )
+      {
          vg_msg msg;
          vg_msg_init( &msg, reg->metadata, reg->metadata_len );
 
          global_skateshop.render.world_loc = vg_msg_getkvstr(&msg,"location");
          global_skateshop.render.world_reg = global_skateshop.selected_world_id;
 
-         if( vg_msg_seekframe( &msg, "workshop" ) ){
+         if( vg_msg_seekframe( &msg, "workshop" ) )
+         {
             global_skateshop.render.world_title = vg_msg_getkvstr(&msg,"title");
             vg_msg_skip_frame( &msg );
          }
@@ -668,17 +646,20 @@ none:;
    vg_strnull( &subtext, bufsubtext, 128 );
    
    u32 valid_count = addon_count(k_addon_type_world,ADDON_REG_HIDDEN);
-   if( valid_count ){
+   if( valid_count )
+   {
       vg_strcati32( &info, global_skateshop.selected_world_id+1 );
       vg_strcatch( &info, '/' );
       vg_strcati32( &info, valid_count );
       vg_strcatch( &info, ' ' );
       vg_strcat( &info, global_skateshop.render.world_title );
 
-      if( !vg_loader_availible() ){
+      if( !vg_loader_availible() )
+      {
          vg_strcat( &subtext, "Loading..." );
       }
-      else{
+      else
+      {
          addon_reg *reg = get_addon_from_index( k_addon_type_world,
                      global_skateshop.selected_world_id, ADDON_REG_HIDDEN );
 
@@ -688,7 +669,8 @@ none:;
          vg_strcat( &subtext, global_skateshop.render.world_loc );
       }
    }
-   else{
+   else
+   {
       vg_strcat( &info, "No workshop worlds installed" );
    }
 
@@ -735,7 +717,8 @@ void skateshop_render( ent_skateshop *shop )
 
 void skateshop_render_nonfocused( world_instance *world, vg_camera *cam )
 {
-   for( u32 j=0; j<mdl_arrcount( &world->ent_skateshop ); j ++ ){
+   for( u32 j=0; j<mdl_arrcount( &world->ent_skateshop ); j ++ )
+   {
       ent_skateshop *shop = mdl_arritm(&world->ent_skateshop, j );
 
       if( shop->type != k_skateshop_type_boardshop ) continue;
@@ -751,7 +734,8 @@ void skateshop_render_nonfocused( world_instance *world, vg_camera *cam )
          continue;
 
       u32 slot_count = vg_list_size(global_skateshop.shop_view_slots);
-      for( u32 i=0; i<slot_count; i++ ){
+      for( u32 i=0; i<slot_count; i++ )
+      {
          struct player_board *board = &localplayer.fallback_board;
 
          mdl_transform xform;
@@ -768,7 +752,8 @@ void skateshop_render_nonfocused( world_instance *world, vg_camera *cam )
    }
 }
 
-static void ent_skateshop_helpers_pickable( const char *acceptance ){
+static void ent_skateshop_helpers_pickable( const char *acceptance )
+{
    vg_str text;
 
    if( gui_new_helper( input_button_list[k_srbind_mback], &text ))
@@ -785,39 +770,72 @@ static void ent_skateshop_helpers_pickable( const char *acceptance ){
    }
 }
 
+static void board_scan_thread( void *_args )
+{
+   addon_mount_content_folder( k_addon_type_board, "boards", ".mdl" );
+   addon_mount_workshop_items();
+   vg_async_call( async_addon_reg_update, NULL, 0 );
+   vg_async_stall();
+
+   /* 04.03.24
+    * REVIEW: This is removed as it *should* be done on the preupdate of the 
+    *         addon system.
+    *
+    *         Verify that it works the same.
+    */
+#if 0
+   board_processview_thread(NULL);
+#endif
+}
+
+static void world_scan_thread( void *_args )
+{
+   addon_mount_content_folder( k_addon_type_world, "maps", ".mdl" );
+   addon_mount_workshop_items();
+   vg_async_call( async_addon_reg_update, NULL, 0 );
+}
+
 /*
  * Entity logic: entrance event
  */
-void ent_skateshop_call( world_instance *world, ent_call *call )
+entity_call_result ent_skateshop_call( world_instance *world, ent_call *call )
 {
    u32 index = mdl_entity_id_id( call->id );
    ent_skateshop *shop = mdl_arritm( &world->ent_skateshop, index );
    vg_info( "skateshop_call\n" );
 
-   if( skaterift.activity != k_skaterift_default ) return;
-   if( !vg_loader_availible() ) return;
+   if( (skaterift.activity != k_skaterift_default) || 
+       !vg_loader_availible() ) 
+      return k_entity_call_result_invalid;
 
-   if( call->function == k_ent_function_trigger ){
-      if( localplayer.subsystem != k_player_subsystem_walk ) return;
+   if( call->function == k_ent_function_trigger )
+   {
+      if( localplayer.subsystem != k_player_subsystem_walk )
+         return k_entity_call_result_OK;
       
       vg_info( "Entering skateshop\n" );
 
-      world_entity_focus( call->id );
+      world_entity_set_focus( call->id );
+      world_entity_focus_modal();
       gui_helper_clear();
       
-      if( shop->type == k_skateshop_type_boardshop ){
+      if( shop->type == k_skateshop_type_boardshop )
+      {
          skateshop_update_viewpage();
-         skateshop_op_board_scan();
+         vg_loader_start( board_scan_thread, NULL );
          ent_skateshop_helpers_pickable( "pick" );
       }
-      else if( shop->type == k_skateshop_type_charshop ){
+      else if( shop->type == k_skateshop_type_charshop )
+      {
          ent_skateshop_helpers_pickable( "pick" );
       }
-      else if( shop->type == k_skateshop_type_worldshop ){
+      else if( shop->type == k_skateshop_type_worldshop )
+      {
          ent_skateshop_helpers_pickable( "open rift" );
-         skateshop_op_world_scan();
+         vg_loader_start( world_scan_thread, NULL );
       }
-      else if( shop->type == k_skateshop_type_server ){
+      else if( shop->type == k_skateshop_type_server )
+      {
          vg_str text;
          global_skateshop.helper_pick = gui_new_helper(
                      input_button_list[k_srbind_maccept], &text);
@@ -825,5 +843,8 @@ void ent_skateshop_call( world_instance *world, ent_call *call )
             vg_strcat( &text, "exit" );
          skateshop_server_helper_update();
       }
+      return k_entity_call_result_OK;
    }
+   else
+      return k_entity_call_result_unhandled;
 }