clean up blurring code. remove dead code
[carveJwlIkooP6JGAAIwe30JlM.git] / workshop.c
1 #include "vg/vg_engine.h"
2 #include "vg/vg_tex.h"
3 #include "vg/vg_image.h"
4 #include "vg/vg_msg.h"
5 #include "vg/vg_binstr.h"
6 #include "vg/vg_loader.h"
7 #include "vg/vg_io.h"
8 #include "ent_skateshop.h"
9
10 #include "vg/vg_steam_auth.h"
11 #include "vg/vg_steam_ugc.h"
12 #include "vg/vg_steam_friends.h"
13 #include "steam.h"
14 #include "workshop.h"
15
16 struct workshop_form workshop_form;
17
18 static struct ui_enum_opt workshop_form_visibility_opts[] = {
19 { k_ERemoteStoragePublishedFileVisibilityPublic, "Public" },
20 { k_ERemoteStoragePublishedFileVisibilityUnlisted, "Unlisted" },
21 { k_ERemoteStoragePublishedFileVisibilityFriendsOnly, "Friends Only" },
22 { k_ERemoteStoragePublishedFileVisibilityPrivate, "Private" },
23 };
24
25 static struct ui_enum_opt workshop_form_type_opts[] = {
26 { k_addon_type_none, "None" },
27 { k_addon_type_board, "Board" },
28 { k_addon_type_world, "World" },
29 { k_addon_type_player, "Player" },
30 };
31
32 /*
33 * Close the form and discard UGC query result
34 */
35 static void workshop_quit_form(void){
36 player_board_unload( &workshop_form.board_model );
37 workshop_form.file_intent = k_workshop_form_file_intent_none;
38
39 if( workshop_form.ugc_query.result == k_EResultOK ){
40 workshop_form.ugc_query.result = k_EResultNone;
41
42 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
43 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest(
44 hSteamUGC, workshop_form.ugc_query.handle );
45 }
46
47 workshop_form.page = k_workshop_form_hidden;
48 workshop_form.op = k_workshop_op_none;
49 }
50
51 /*
52 * Delete all information about the submission
53 */
54 static void workshop_reset_submission_data(void)
55 {
56 workshop_form.submission.file_id = 0; /* assuming id of 0 is none/invalid */
57 workshop_form.submission.description[0] = '\0';
58 workshop_form.submission.title[0] = '\0';
59 workshop_form.submission.author[0] = '\0';
60 workshop_form.submission.submission_type_selection =
61 k_addon_type_none;
62 workshop_form.submission.type = k_addon_type_none;
63
64 workshop_form.submission.visibility =
65 k_ERemoteStoragePublishedFileVisibilityPublic;
66
67 workshop_form.addon_folder[0] = '\0';
68 player_board_unload( &workshop_form.board_model );
69 workshop_form.file_intent = k_workshop_form_file_intent_none;
70 }
71
72
73 /*
74 * Mostly copies of what it sais on the Steam API documentation
75 */
76 static const char *workshop_EResult_user_string( EResult result )
77 {
78 switch( result ){
79 case k_EResultInsufficientPrivilege:
80 return "Your account is currently restricted from uploading content "
81 "due to a hub ban, account lock, or community ban. You need to "
82 "contact Steam Support to resolve the issue.";
83 case k_EResultBanned:
84 return "You do not have permission to upload content to this hub "
85 "because you have an active VAC or Game ban.";
86 case k_EResultTimeout:
87 return "The operation took longer than expected, so it was discarded. "
88 "Please try again.";
89 case k_EResultNotLoggedOn:
90 return "You are currently not logged into Steam.";
91 case k_EResultServiceUnavailable:
92 return "The workshop server is having issues or is unavailable, "
93 "please try again.";
94 case k_EResultInvalidParam:
95 return "One of the submission fields contains something not being "
96 "accepted by that field.";
97 case k_EResultAccessDenied:
98 return "There was a problem trying to save the title and description. "
99 "Access was denied.";
100 case k_EResultLimitExceeded:
101 return "You have exceeded your Steam Cloud quota. If you wish to "
102 "upload this file, you must remove some published items.";
103 case k_EResultFileNotFound:
104 return "The uploaded file could not be found.";
105 case k_EResultDuplicateRequest:
106 return "The file was already successfully uploaded.";
107 case k_EResultDuplicateName:
108 return "You already have a Steam Workshop item with that name.";
109 case k_EResultServiceReadOnly:
110 return "Due to a recent password or email change, you are not allowed "
111 "to upload new content. Usually this restriction will expire in"
112 " 5 days, but can last up to 30 days if the account has been "
113 "inactive recently.";
114 default:
115 return "Operation failed for an error which has not been accounted for "
116 "by the programmer. Try again, sorry :)";
117 }
118 }
119
120 /*
121 * op: k_workshop_form_op_publishing_update
122 * ----------------------------------------------------------------------------
123 */
124
125 /*
126 * The endpoint of this operation
127 */
128 static void on_workshop_update_result( void *data, void *user )
129 {
130 vg_info( "Recieved workshop update result\n" );
131 SubmitItemUpdateResult_t *result = data;
132
133 /* this seems to be set here, but my account definitely has accepted it */
134 if( result->m_bUserNeedsToAcceptWorkshopLegalAgreement ){
135 vg_warn( "Workshop agreement currently not accepted\n" );
136 }
137
138 if( result->m_eResult == k_EResultOK ){
139 workshop_form.page = k_workshop_form_closing_good;
140 workshop_form.failure_or_success_string = "Uploaded workshop file!";
141 vg_success( "file uploaded\n" );
142 }
143 else{
144 workshop_form.page = k_workshop_form_closing_bad;
145 workshop_form.failure_or_success_string =
146 workshop_EResult_user_string( result->m_eResult );
147
148 vg_error( "Error with the submitted file (%d)\n", result->m_eResult );
149 }
150 workshop_form.op = k_workshop_op_none;
151 }
152
153 static const char *workshop_filetype_folder(void){
154 enum addon_type type = workshop_form.submission.type;
155 if ( type == k_addon_type_board ) return "boards/";
156 else if( type == k_addon_type_player ) return "playermodels/";
157 else if( type == k_addon_type_world ) return "maps/";
158
159 return "unknown_addon_type/";
160 }
161
162 /*
163 * reciever on completion of packaging the files, it will then start the item
164 * update with Steam API
165 */
166 static void workshop_form_upload_submission( PublishedFileId_t file_id,
167 char *metadata )
168 {
169 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
170 UGCUpdateHandle_t handle
171 = SteamAPI_ISteamUGC_StartItemUpdate( hSteamUGC, SKATERIFT_APPID,
172 file_id );
173
174 /* TODO: Handle failure cases for these */
175
176 SteamAPI_ISteamUGC_SetItemMetadata( hSteamUGC, handle, metadata );
177
178 if( workshop_form.submission.submit_title ){
179 vg_info( "Setting title\n" );
180 SteamAPI_ISteamUGC_SetItemTitle( hSteamUGC, handle,
181 workshop_form.submission.title );
182 }
183
184 if( workshop_form.submission.submit_description ){
185 vg_info( "Setting description\n" );
186 SteamAPI_ISteamUGC_SetItemDescription( hSteamUGC, handle,
187 workshop_form.submission.description);
188 }
189
190 if( workshop_form.submission.submit_file_and_image ){
191 char path_buf[4096];
192 vg_str folder;
193 vg_strnull( &folder, path_buf, 4096 );
194 vg_strcat( &folder, vg.base_path );
195
196 vg_strcat( &folder, workshop_filetype_folder() );
197 vg_strcat( &folder, workshop_form.addon_folder );
198
199 vg_info( "Setting item content\n" );
200 SteamAPI_ISteamUGC_SetItemContent( hSteamUGC, handle, folder.buffer );
201
202 vg_str preview = folder;
203 vg_strcat( &preview, "/preview.jpg" );
204
205 vg_info( "Setting preview image\n" );
206 SteamAPI_ISteamUGC_SetItemPreview( hSteamUGC, handle, preview.buffer );
207 }
208
209 vg_info( "Setting visibility\n" );
210 SteamAPI_ISteamUGC_SetItemVisibility( hSteamUGC, handle,
211 workshop_form.submission.visibility );
212
213 vg_info( "Submitting updates\n" );
214 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
215 call->userdata = NULL;
216 call->p_handler = on_workshop_update_result;
217 call->id = SteamAPI_ISteamUGC_SubmitItemUpdate( hSteamUGC, handle, "" );
218 }
219
220 /*
221 * Steam API call result for when we've created a new item on their network, or
222 * not, if it has failed
223 */
224 static void on_workshop_createitem( void *data, void *user )
225 {
226 CreateItemResult_t *result = data;
227
228 if( result->m_eResult == k_EResultOK ){
229 vg_info( "Created workshop file with id: %lu\n",
230 result->m_nPublishedFileId );
231
232 if( result->m_bUserNeedsToAcceptWorkshopLegalAgreement ){
233 vg_warn( "Workshop agreement currently not accepted\n" );
234 }
235
236 workshop_form_upload_submission( result->m_nPublishedFileId, user );
237 }
238 else{
239 const char *errstr = workshop_EResult_user_string( result->m_eResult );
240
241 if( errstr ){
242 vg_error( "ISteamUGC_CreateItem() failed(%d): '%s' \n",
243 result->m_eResult, errstr );
244 }
245
246 workshop_form.page = k_workshop_form_closing_bad;
247 workshop_form.failure_or_success_string = errstr;
248 }
249 }
250
251 /*
252 * Starts the workshop upload process through Steam API
253 */
254 static void workshop_form_async_submit_begin( void *payload, u32 size )
255 {
256
257 /* use existing file */
258 if( workshop_form.submission.file_id ){
259 workshop_form_upload_submission( workshop_form.submission.file_id,
260 payload );
261 }
262 else{
263 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
264 call->userdata = payload;
265 call->p_handler = on_workshop_createitem;
266 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
267 call->id = SteamAPI_ISteamUGC_CreateItem( hSteamUGC, SKATERIFT_APPID,
268 k_EWorkshopFileTypeCommunity );
269 }
270 }
271
272 /*
273 * Downloads the framebuffer into scratch memory
274 */
275 static void workshop_form_async_download_image( void *payload, u32 size )
276 {
277 int w, h;
278 vg_framebuffer_get_res( gpipeline.fb_workshop_preview, &w, &h );
279 vg_linear_clear( vg_mem.scratch );
280 workshop_form.img_buffer = vg_linear_alloc( vg_mem.scratch, w*h*3 );
281
282 vg_info( "read framebuffer: glReadPixels( %dx%d )\n", w,h );
283
284 glBindFramebuffer( GL_READ_FRAMEBUFFER, gpipeline.fb_workshop_preview->id );
285 glReadBuffer( GL_COLOR_ATTACHMENT0 );
286 glReadPixels( 0,0, w,h, GL_RGB, GL_UNSIGNED_BYTE, workshop_form.img_buffer );
287
288 workshop_form.img_w = w;
289 workshop_form.img_h = h;
290 }
291
292 /*
293 * Thread which kicks off the upload process
294 */
295 static void _workshop_form_submit_thread( void *data )
296 {
297 vg_async_call( workshop_form_async_download_image, NULL, 0 );
298 vg_async_stall();
299
300 char path_buf[4096];
301 vg_str folder;
302 vg_strnull( &folder, path_buf, 4096 );
303
304 vg_strcat( &folder, workshop_filetype_folder() );
305 vg_strcat( &folder, workshop_form.addon_folder );
306
307 if( !vg_strgood(&folder) ){
308 vg_error( "addon folder path too long\n" );
309 workshop_form.op = k_workshop_op_none;
310 return;
311 }
312
313 /*
314 * Create the metadata file
315 * -----------------------------------------------------------------------*/
316 u8 descriptor_buf[ 512 ];
317 vg_msg descriptor;
318 vg_msg_init( &descriptor, descriptor_buf, sizeof(descriptor_buf) );
319 vg_linear_clear( vg_mem.scratch );
320
321 /* short description */
322 vg_msg_frame( &descriptor, "workshop" );
323 vg_msg_wkvstr( &descriptor, "title", workshop_form.submission.title );
324 //vg_msg_wkvstr( &descriptor, "author", "unknown" );
325 vg_msg_wkvnum( &descriptor, "type", k_vg_msg_u32, 1,
326 &workshop_form.submission.type );
327 vg_msg_wkvstr( &descriptor, "folder", workshop_form.addon_folder );
328 vg_msg_end_frame( &descriptor );
329 //vg_msg_wkvstr( &descriptor, "location", "USA" );
330
331 char *short_descriptor_str =
332 vg_linear_alloc( vg_mem.scratch, vg_align8(descriptor.cur.co*2+1));
333 vg_bin_str( descriptor_buf, short_descriptor_str, descriptor.cur.co );
334 short_descriptor_str[descriptor.cur.co*2] = '\0';
335 vg_info( "binstr: %s\n", short_descriptor_str );
336
337 vg_dir dir;
338 if( !vg_dir_open( &dir, folder.buffer ) )
339 {
340 vg_error( "could not open addon folder '%s'\n", folder.buffer );
341 workshop_form.op = k_workshop_op_none;
342 return;
343 }
344
345 while( vg_dir_next_entry(&dir) )
346 {
347 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file )
348 {
349 const char *d_name = vg_dir_entry_name(&dir);
350 if( d_name[0] == '.' ) continue;
351
352 vg_str file = folder;
353 vg_strcat( &file, "/" );
354 vg_strcat( &file, d_name );
355 if( !vg_strgood( &file ) ) continue;
356
357 char *ext = vg_strch( &file, '.' );
358 if( !ext ) continue;
359 if( strcmp(ext,".mdl") ) continue;
360
361 vg_msg_wkvstr( &descriptor, "content", d_name );
362 break;
363 }
364 }
365 vg_dir_close(&dir);
366
367 vg_str descriptor_file = folder;
368 vg_strcat( &descriptor_file, "/addon.inf" );
369 if( !vg_strgood(&descriptor_file) ){
370 vg_error( "Addon info path too long\n" );
371 workshop_form.op = k_workshop_op_none;
372 return;
373 }
374
375 FILE *fp = fopen( descriptor_file.buffer, "wb" );
376 if( !fp ){
377 vg_error( "Could not open addon info file '%s'\n",
378 descriptor_file.buffer );
379 workshop_form.op = k_workshop_op_none;
380 return;
381 }
382 fwrite( descriptor_buf, descriptor.cur.co, 1, fp );
383 fclose( fp );
384
385 /* Save the preview
386 * -----------------------------------------------------------------------*/
387 vg_str preview = folder;
388 vg_strcat( &preview, "/preview.jpg" );
389
390 if( !vg_strgood(&preview) ){
391 vg_error( "preview image path too long\n" );
392 workshop_form.op = k_workshop_op_none;
393 return;
394 }
395
396 int w = workshop_form.img_w,
397 h = workshop_form.img_h;
398
399 vg_info( "writing: %s (%dx%d @90%%)\n", preview.buffer, w,h );
400 stbi_flip_vertically_on_write(1);
401 stbi_write_jpg( preview.buffer, w,h, 3, workshop_form.img_buffer, 90 );
402
403 vg_async_call( workshop_form_async_submit_begin, short_descriptor_str, 0 );
404 }
405
406 /*
407 * Entry point for the publishing submission operation
408 */
409 static void workshop_op_submit( ui_context *ctx )
410 {
411 /* TODO: Show these errors to the user */
412 if( workshop_form.submission.submit_title )
413 {
414 if( !workshop_form.submission.title[0] )
415 {
416 ui_start_modal( ctx, "Cannot submit because a title is required\n",
417 UI_MODAL_WARN );
418 workshop_form.op = k_workshop_op_none;
419 return;
420 }
421 }
422
423 if( workshop_form.submission.submit_description )
424 {
425 if( !workshop_form.submission.description[0] )
426 {
427 ui_start_modal( ctx,
428 "Cannot submit because a description is required\n",
429 UI_MODAL_WARN );
430 workshop_form.op = k_workshop_op_none;
431 return;
432 }
433 }
434
435 if( workshop_form.submission.submit_file_and_image )
436 {
437 if( workshop_form.file_intent == k_workshop_form_file_intent_none )
438 {
439 ui_start_modal( ctx, "Cannot submit because the file is "
440 "empty or unspecified\n", UI_MODAL_WARN );
441 workshop_form.op = k_workshop_op_none;
442 return;
443 }
444 }
445
446 player_board_unload( &workshop_form.board_model );
447 workshop_form.file_intent = k_workshop_form_file_intent_none;
448 workshop_form.op = k_workshop_op_publishing_update;
449
450 vg_loader_start( _workshop_form_submit_thread, NULL );
451 }
452
453 /*
454 * op: k_workshop_form_op_loading_model
455 * -----------------------------------------------------------------------------
456 */
457
458 /*
459 * Reciever for completion of the model file load
460 */
461 static void workshop_form_loadmodel_async_complete( void *payload, u32 size )
462 {
463 v2_zero( workshop_form.view_angles );
464 v3_zero( workshop_form.view_offset );
465 workshop_form.view_dist = 1.0f;
466 workshop_form.view_changed = 1;
467 workshop_form.file_intent = k_workshop_form_file_intent_new;
468
469 vg_success( "workshop async load complete\n" );
470 workshop_form.op = k_workshop_op_none;
471 }
472
473 /*
474 * Reciever for failure to load
475 */
476 static void workshop_form_loadmodel_async_error( void *payload, u32 size ){
477 }
478
479 /*
480 * Thread which loads the model from the disk
481 */
482 static void _workshop_form_load_thread( void *data )
483 {
484 char path_buf[4096];
485 vg_str folder;
486 vg_strnull( &folder, path_buf, 4096 );
487
488 vg_strcat( &folder, workshop_filetype_folder() );
489 vg_strcat( &folder, workshop_form.addon_folder );
490
491 if( !vg_strgood(&folder) ){
492 vg_error( "workshop async load failed: path too long\n" );
493 vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
494 workshop_form.op = k_workshop_op_none;
495 return;
496 }
497
498 vg_dir dir;
499 if( !vg_dir_open( &dir, folder.buffer ) ){
500 vg_error( "workshop async load failed: could not open folder\n" );
501 vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
502 workshop_form.op = k_workshop_op_none;
503 return;
504 }
505
506 vg_info( "Searching %s for model files\n", folder.buffer );
507
508 int found_mdl = 0;
509 while( vg_dir_next_entry(&dir) ){
510 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
511 const char *d_name = vg_dir_entry_name(&dir);
512 if( d_name[0] == '.' ) continue;
513
514 vg_str file = folder;
515 vg_strcat( &file, "/" );
516 vg_strcat( &file, d_name );
517 if( !vg_strgood( &file ) ) continue;
518
519 char *ext = vg_strch( &file, '.' );
520 if( !ext ) continue;
521 if( strcmp(ext,".mdl") ) continue;
522 found_mdl = 1;
523 break;
524 }
525 }
526 vg_dir_close(&dir);
527
528 if( !found_mdl ){
529 vg_error( "workshop async load failed: no model files found\n" );
530 vg_async_call( workshop_form_loadmodel_async_error, NULL, 0 );
531 workshop_form.op = k_workshop_op_none;
532 return;
533 }
534
535 if( workshop_form.submission.type == k_addon_type_board )
536 player_board_load( &workshop_form.board_model, path_buf );
537 else if( workshop_form.submission.type == k_addon_type_player )
538 player_model_load( &workshop_form.player_model, path_buf );
539
540 vg_async_call( workshop_form_loadmodel_async_complete, NULL, 0 );
541 }
542
543 /*
544 * Entry point for load model operation
545 */
546 static void workshop_op_load_model( ui_context *ctx )
547 {
548 world_instance *world = world_current_instance();
549 workshop_form.view_world = world;
550
551 if( workshop_form.submission.type == k_addon_type_board )
552 {
553 if( mdl_arrcount( &world->ent_swspreview ) )
554 {
555 workshop_form.ptr_ent = mdl_arritm( &world->ent_swspreview, 0 );
556 }
557 else
558 {
559 ui_start_modal( ctx, "There is no ent_swspreview in the level. \n"
560 "Cannot publish here\n", UI_MODAL_BAD );
561 workshop_form.op = k_workshop_op_none;
562 return;
563 }
564 }
565 else if( workshop_form.submission.type == k_addon_type_player ){}
566 else
567 {
568 ui_start_modal( ctx, "Don't know how to prepare for this item type. \n"
569 "Please contact the developers.\n", UI_MODAL_BAD );
570 workshop_form.op = k_workshop_op_none;
571 return;
572 }
573
574 workshop_form.op = k_workshop_op_loading_model;
575 vg_loader_start( _workshop_form_load_thread, NULL );
576 }
577
578 /*
579 * op: k_workshop_form_op_downloading_submission
580 * -----------------------------------------------------------------------------
581 */
582
583 /*
584 * The image has been decoded and is ready to slap into the framebuffer
585 */
586 static void workshop_form_async_imageload( void *data, u32 len )
587 {
588 if( data )
589 {
590 vg_framebuffer_attachment *a =
591 &gpipeline.fb_workshop_preview->attachments[0];
592
593 glBindTexture( GL_TEXTURE_2D, a->id );
594 glTexSubImage2D( GL_TEXTURE_2D, 0,0,0,
595 WORKSHOP_PREVIEW_WIDTH, WORKSHOP_PREVIEW_HEIGHT,
596 a->format, a->type, data );
597 stbi_image_free( data );
598 vg_success( "Loaded workshop preview image\n" );
599 }
600 else
601 {
602 snprintf( workshop_form.error_msg, sizeof(workshop_form.error_msg),
603 "Preview image could not be loaded. Reason: %s\n",
604 stbi_failure_reason() );
605 ui_start_modal( &vg_ui.ctx, workshop_form.error_msg, UI_MODAL_BAD );
606 }
607 workshop_form.op = k_workshop_op_none;
608 }
609
610 /*
611 * Load the image located at ./workshop_preview.jpg into our framebuffer
612 */
613 static void _workshop_load_preview_thread( void *data ){
614 char path_buf[ 4096 ];
615 vg_str path;
616 vg_strnull( &path, path_buf, 4096 );
617 vg_strcat( &path, workshop_filetype_folder() );
618 vg_strcat( &path, workshop_form.addon_folder );
619 vg_strcat( &path, "/preview.jpg" );
620
621 if( vg_strgood( &path ) )
622 {
623 stbi_set_flip_vertically_on_load(1);
624 int x, y, nc;
625 u8 *rgb = stbi_load( path.buffer, &x, &y, &nc, 3 );
626
627 if( rgb )
628 {
629 if( (x == WORKSHOP_PREVIEW_WIDTH) && (y == WORKSHOP_PREVIEW_HEIGHT) )
630 {
631 vg_async_call( workshop_form_async_imageload, rgb, x*y*3 );
632 }
633 else
634 {
635 vg_error( "Resolution does not match framebuffer, so we can't"
636 " show it\n" );
637 stbi_image_free( rgb );
638 vg_async_call( workshop_form_async_imageload, NULL, 0 );
639 }
640 }
641 else
642 {
643 vg_async_call( workshop_form_async_imageload, NULL, 0 );
644 }
645 }
646 else
647 {
648 vg_async_call( workshop_form_async_imageload, NULL, 0 );
649 }
650 }
651
652 /*
653 * Entry point to view operation
654 */
655 static void workshop_op_download_and_view_submission( int result_index )
656 {
657 workshop_form.op = k_workshop_op_downloading_submission;
658 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
659 ISteamRemoteStorage *hSteamRemoteStorage = SteamAPI_SteamRemoteStorage();
660 SteamUGCDetails_t details;
661 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC,
662 workshop_form.ugc_query.handle,
663 result_index,
664 &details ) )
665 {
666 workshop_reset_submission_data();
667 workshop_form.submission.submit_description = 0;
668 workshop_form.submission.submit_file_and_image = 0;
669 workshop_form.submission.submit_title = 0;
670
671 u8 metadata_buf[512];
672 char metadata_str[1024+1];
673 int have_meta = SteamAPI_ISteamUGC_GetQueryUGCMetadata( hSteamUGC,
674 workshop_form.ugc_query.handle,
675 result_index, metadata_str,
676 1024+1 );
677
678 vg_strncpy( details.m_rgchDescription,
679 workshop_form.submission.description,
680 vg_list_size( workshop_form.submission.description ),
681 k_strncpy_always_add_null );
682
683 vg_strncpy( details.m_rgchTitle,
684 workshop_form.submission.title,
685 vg_list_size( workshop_form.submission.title ),
686 k_strncpy_always_add_null );
687
688 snprintf( workshop_form.addon_folder,
689 vg_list_size( workshop_form.addon_folder ),
690 "Steam Cloud ("PRINTF_U64")", details.m_nPublishedFileId );
691
692 workshop_form.submission.file_id = details.m_nPublishedFileId;
693 workshop_form.file_intent = k_workshop_form_file_intent_keep_old;
694 workshop_form.page = k_workshop_form_edit;
695 workshop_form.submission.visibility = details.m_eVisibility;
696 workshop_form.submission.type = k_addon_type_none;
697 workshop_form.submission.submission_type_selection = k_addon_type_none;
698
699 if( have_meta )
700 {
701 u32 len = strlen(metadata_str);
702 vg_info( "Metadata: %s\n", metadata_str );
703 vg_str_bin( metadata_str, metadata_buf, len );
704 vg_msg msg;
705 vg_msg_init( &msg, metadata_buf, len/2 );
706
707 if( vg_msg_seekframe( &msg, "workshop" ))
708 {
709 u32 type;
710 vg_msg_getkvintg( &msg, "type", k_vg_msg_u32, &type, NULL );
711 workshop_form.submission.type = type;
712 workshop_form.submission.submission_type_selection = type;
713
714 const char *kv_folder = vg_msg_getkvstr( &msg, "folder" );
715 if( kv_folder )
716 {
717 vg_strncpy( kv_folder, workshop_form.addon_folder,
718 sizeof(workshop_form.addon_folder),
719 k_strncpy_always_add_null );
720 }
721 }
722 }
723 else
724 {
725 vg_error( "No metadata was returned with this item.\n" );
726 }
727
728 vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f );
729 glClearColor( 0.2f, 0.0f, 0.0f, 1.0f );
730 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
731 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
732 glViewport( 0,0, vg.window_x, vg.window_y );
733
734 vg_loader_start( _workshop_load_preview_thread, NULL );
735 }
736 else
737 {
738 vg_error( "GetQueryUGCResult: Index out of range\n" );
739 workshop_form.op = k_workshop_op_none;
740 }
741 }
742
743 /*
744 * Regular stuff
745 * -----------------------------------------------------------------------------
746 */
747
748 /*
749 * View a page of results on the sidebar
750 */
751 static void workshop_view_page( int req )
752 {
753 if( workshop_form.ugc_query.result != k_EResultOK )
754 {
755 vg_error( "Tried to change page without complete data\n" );
756 workshop_form.op = k_workshop_op_none;
757 return;
758 }
759
760 int page = VG_MAX(VG_MIN(req, workshop_form.view_published_page_count-1),0),
761 start = page * WORKSHOP_VIEW_PER_PAGE,
762 end = VG_MIN( (page+1) * WORKSHOP_VIEW_PER_PAGE,
763 workshop_form.ugc_query.returned_item_count ),
764 count = end-start;
765
766 vg_info( "View page %d\n", page );
767
768 workshop_form.view_published_page_id = page;
769 workshop_form.published_files_list_length = count;
770 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
771
772 for( int i=0; i<count; i ++ )
773 {
774 struct published_file *pfile = &workshop_form.published_files_list[i];
775
776 SteamUGCDetails_t details;
777 if( SteamAPI_ISteamUGC_GetQueryUGCResult( hSteamUGC,
778 workshop_form.ugc_query.handle,
779 start+i,
780 &details ) )
781 {
782 if( details.m_eResult != k_EResultOK )
783 {
784 snprintf( pfile->title, 80, "Error (%d)", details.m_eResult );
785 }
786 else
787 {
788 vg_strncpy( details.m_rgchTitle, pfile->title, 80,
789 k_strncpy_always_add_null );
790 }
791
792 pfile->result = details.m_eResult;
793 pfile->result_index = start+i;
794 }
795 else
796 {
797 pfile->result = k_EResultValueOutOfRange;
798 pfile->result_index = -1;
799 snprintf( pfile->title, 80, "Error (invalid index)" );
800 }
801 }
802 }
803
804 /*
805 * Steam API result for when we recieve submitted UGC information about the user
806 */
807 static void on_workshop_UGCQueryComplete( void *data, void *userdata )
808 {
809 SteamUGCQueryCompleted_t *query = data;
810 workshop_form.ugc_query.result = query->m_eResult;
811
812 if( query->m_eResult == k_EResultOK )
813 {
814 if( query->m_unTotalMatchingResults > 50 )
815 {
816 vg_warn( "You have %d items submitted, "
817 "we can only view the last 50\n" );
818 }
819
820 workshop_form.ugc_query.all_item_count = query->m_unTotalMatchingResults;
821 workshop_form.ugc_query.returned_item_count =
822 query->m_unNumResultsReturned;
823
824 workshop_form.ugc_query.handle = query->m_handle;
825 workshop_form.view_published_page_count =
826 (query->m_unNumResultsReturned+WORKSHOP_VIEW_PER_PAGE-1)/
827 WORKSHOP_VIEW_PER_PAGE;
828 workshop_form.view_published_page_id = 0;
829 workshop_form.published_files_list_length = 0;
830
831 workshop_view_page( 0 );
832 }
833 else
834 {
835 vg_error( "Steam UGCQuery failed (%d)\n", query->m_eResult );
836 workshop_form.view_published_page_count = 0;
837 workshop_form.view_published_page_id = 0;
838 workshop_form.published_files_list_length = 0;
839
840 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
841 SteamAPI_ISteamUGC_ReleaseQueryUGCRequest( hSteamUGC, query->m_handle );
842 }
843 }
844
845 /*
846 * Console command to open the workshop publisher
847 */
848 int workshop_submit_command( int argc, const char *argv[] )
849 {
850 if( !steam_ready )
851 {
852 ui_start_modal( &vg_ui.ctx,
853 "Steam API is not initialized\n", UI_MODAL_BAD );
854 return 0;
855 }
856
857 workshop_form.page = k_workshop_form_open;
858 workshop_form.view_published_page_count = 0;
859 workshop_form.view_published_page_id = 0;
860 workshop_form.published_files_list_length = 0;
861 workshop_form.ugc_query.result = k_EResultNone;
862
863 vg_steam_async_call *call = vg_alloc_async_steam_api_call();
864
865 ISteamUser *hSteamUser = SteamAPI_SteamUser();
866 CSteamID steamid;
867 steamid.m_unAll64Bits = SteamAPI_ISteamUser_GetSteamID( hSteamUser );
868
869 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
870 call->p_handler = on_workshop_UGCQueryComplete;
871 call->userdata = NULL;
872 UGCQueryHandle_t handle = SteamAPI_ISteamUGC_CreateQueryUserUGCRequest
873 (
874 hSteamUGC,
875 steamid.m_comp.m_unAccountID,
876 k_EUserUGCList_Published,
877 k_EUGCMatchingUGCType_Items,
878 k_EUserUGCListSortOrder_CreationOrderDesc,
879 SKATERIFT_APPID, SKATERIFT_APPID,
880 1 );
881 SteamAPI_ISteamUGC_SetReturnMetadata( hSteamUGC, handle, 1 );
882 call->id = SteamAPI_ISteamUGC_SendQueryUGCRequest( hSteamUGC, handle );
883 return 0;
884 }
885
886 void workshop_init(void)
887 {
888 vg_console_reg_cmd( "workshop_submit", workshop_submit_command, NULL );
889 }
890
891 static void workshop_render_world_preview(void)
892 {
893 vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f );
894
895 glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
896 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
897 glEnable( GL_DEPTH_TEST );
898 glDisable( GL_BLEND );
899
900 render_world( world_current_instance(), &skaterift.cam, 0, 0, 1, 1 );
901
902 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
903 glViewport( 0,0, vg.window_x, vg.window_y );
904 }
905
906 /*
907 * Redraw the playermodel into the workshop framebuffer
908 */
909 static void workshop_render_player_preview(void)
910 {
911 vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f );
912 glClearColor( 0.16f, 0.15f, 0.15f, 1.0f );
913 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
914 glEnable( GL_DEPTH_TEST );
915 glDisable( GL_BLEND );
916
917 struct skeleton *sk = &localplayer.skeleton;
918
919 player_pose res;
920 res.type = k_player_pose_type_ik;
921
922 struct skeleton_anim *anim = skeleton_get_anim( sk, "idle_cycle+y" );
923 skeleton_sample_anim( sk, anim, vg.time*0.1f, res.keyframes );
924 q_axis_angle( res.root_q, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
925 v3_zero( res.root_co );
926 res.root_co[1] = 200.0f;
927
928 m4x3f transform;
929 q_m3x3( res.root_q, transform );
930 v3_copy( res.root_co, transform[3] );
931
932 /* TODO: Function. */
933 skeleton_apply_pose( sk, res.keyframes, k_anim_apply_defer_ik,
934 localplayer.final_mtx );
935 skeleton_apply_ik_pass( sk, localplayer.final_mtx );
936 skeleton_apply_pose( sk, res.keyframes, k_anim_apply_deffered_only,
937 localplayer.final_mtx );
938 skeleton_apply_inverses( sk, localplayer.final_mtx );
939 skeleton_apply_transform( sk, transform, localplayer.final_mtx );
940
941 vg_camera cam;
942 v3_copy( (v3f){ 0.0f, 201.7f, 1.2f }, cam.pos );
943
944 cam.nearz = 0.01f;
945 cam.farz = 100.0f;
946 cam.fov = 57.0f;
947 v3_zero( cam.angles );
948
949 vg_camera_update_transform( &cam );
950 vg_camera_update_view( &cam );
951 vg_camera_update_projection( &cam );
952 vg_camera_finalize( &cam );
953
954 render_playermodel( &cam, world_current_instance(), 0,
955 &workshop_form.player_model, sk, localplayer.final_mtx );
956
957 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
958 glViewport( 0,0, vg.window_x, vg.window_y );
959 }
960
961 /*
962 * Redraw the model file into the workshop framebuffer
963 */
964 static void workshop_render_board_preview(void)
965 {
966 if( !workshop_form.ptr_ent )
967 {
968 return;
969 }
970
971 vg_framebuffer_bind( gpipeline.fb_workshop_preview, 1.0f );
972
973 glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );
974 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
975 glEnable( GL_DEPTH_TEST );
976 glDisable( GL_BLEND );
977
978 ent_swspreview *swsprev = workshop_form.ptr_ent;
979 world_instance *world = workshop_form.view_world;
980
981 ent_camera *ref = mdl_arritm( &world->ent_camera,
982 mdl_entity_id_id(swsprev->id_camera) );
983 ent_marker *display = mdl_arritm( &world->ent_marker,
984 mdl_entity_id_id(swsprev->id_display) ),
985 *display1= mdl_arritm( &world->ent_marker,
986 mdl_entity_id_id(swsprev->id_display1) );
987
988 v3f baseco;
989 v3_add( display->transform.co, display1->transform.co, baseco );
990 v3_muls( baseco, 0.5f, baseco );
991
992 vg_camera cam;
993 v3f basevector;
994 v3_sub( display->transform.co, ref->transform.co, basevector );
995 float dist = v3_length( basevector );
996
997 v3f baseangles;
998 v3_angles( basevector, baseangles );
999
1000 v2_add( workshop_form.view_angles, baseangles, cam.angles );
1001 cam.angles[2] = 0.0f;
1002
1003 float sX = sinf( cam.angles[0] ),
1004 cX = cosf( cam.angles[0] ),
1005 sY = sinf( cam.angles[1] ),
1006 cY = cosf( cam.angles[1] );
1007
1008 v3f offset = { -sX * cY, sY, cX * cY };
1009
1010 v3_muladds( display->transform.co, offset,
1011 dist*workshop_form.view_dist, cam.pos );
1012
1013 cam.pos[0] += -sX*workshop_form.view_offset[2];
1014 cam.pos[2] += cX*workshop_form.view_offset[2];
1015 cam.pos[0] += cX*workshop_form.view_offset[0];
1016 cam.pos[2] += sX*workshop_form.view_offset[0];
1017 cam.pos[1] += workshop_form.view_offset[1];
1018
1019 cam.nearz = 0.01f;
1020 cam.farz = 100.0f;
1021 cam.fov = ref->fov;
1022
1023 vg_camera_update_transform( &cam );
1024 vg_camera_update_view( &cam );
1025 vg_camera_update_projection( &cam );
1026 vg_camera_finalize( &cam );
1027
1028 m4x3f mmdl, mmdl1;
1029 mdl_transform_m4x3( &display->transform, mmdl );
1030 mdl_transform_m4x3( &display1->transform, mmdl1 );
1031
1032 /* force update this for nice shadows. its usually set in the world
1033 * pre-render step, but that includes timer stuff
1034 */
1035 struct player_board *board = &workshop_form.board_model;
1036 struct ub_world_lighting *ubo = &world->ub_lighting;
1037 v3f vp0, vp1;
1038 v3_copy((v3f){0.0f,0.1f, board->truck_positions[0][2]}, vp0 );
1039 v3_copy((v3f){0.0f,0.1f, board->truck_positions[1][2]}, vp1 );
1040 m4x3_mulv( mmdl1, vp0, ubo->g_board_0 );
1041 m4x3_mulv( mmdl1, vp1, ubo->g_board_1 );
1042 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
1043 glBufferSubData( GL_UNIFORM_BUFFER, 0,
1044 sizeof(struct ub_world_lighting), &world->ub_lighting );
1045
1046 render_world( world, &cam, 0, 0, 0, 0 );
1047 struct player_board_pose pose = {0};
1048 render_board( &cam, world, board, mmdl, &pose, k_board_shader_entity );
1049 render_board( &cam, world, board, mmdl1, &pose, k_board_shader_entity );
1050
1051 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
1052 glViewport( 0,0, vg.window_x, vg.window_y );
1053 }
1054
1055 /*
1056 * ImGUI section for workshop form
1057 * -----------------------------------------------------------------------------
1058 */
1059
1060 static void workshop_changed_model_path( ui_context *ctx, char *buf, u32 len )
1061 {
1062 workshop_form.submission.submit_file_and_image = 1;
1063 }
1064
1065 static void workshop_changed_title( ui_context *ctx, char *buf, u32 len )
1066 {
1067 workshop_form.submission.submit_title = 1;
1068 }
1069
1070 static void workshop_changed_description( ui_context *ctx, char *buf, u32 len )
1071 {
1072 workshop_form.submission.submit_description = 1;
1073 }
1074
1075 static void workshop_form_gui_page_undecided( ui_context *ctx, ui_rect content )
1076 {
1077 ui_rect box;
1078 rect_copy( content, box );
1079 box[3] = 128;
1080 box[2] = (box[2]*2)/3;
1081 ui_rect_center( content, box );
1082
1083 ui_rect row;
1084 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1085 ui_text( ctx, row,
1086 "Select the type of item\n", 1, k_ui_align_middle_center,0);
1087 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1088 ui_enum( ctx, row, "Type:", workshop_form_type_opts,
1089 4, &workshop_form.submission.submission_type_selection );
1090 ui_split( box, k_ui_axis_h, 8, 0, row, box );
1091 ui_split( box, k_ui_axis_h, 28, 0, row, box );
1092
1093 ui_rect button_l, button_r;
1094 rect_copy( row, button_l );
1095 button_l[2] = 128*2;
1096 ui_rect_center( row, button_l );
1097 ui_split_ratio( button_l, k_ui_axis_v, 0.5f, 2, button_l, button_r );
1098
1099 enum addon_type type = workshop_form.submission.submission_type_selection;
1100 if( type != k_addon_type_none)
1101 {
1102 if( ui_button_text( ctx, button_l, "OK", 1 ) == 1 )
1103 {
1104 workshop_form.submission.type = type;
1105
1106 if( type == k_addon_type_world ){
1107 workshop_form.view_changed = 1;
1108 workshop_form.file_intent = k_workshop_form_file_intent_new;
1109 }
1110 }
1111 }
1112 else
1113 {
1114 ui_fill( ctx, button_l, ui_colour(ctx,k_ui_bg) );
1115 ui_text( ctx, button_l, "OK", 1, k_ui_align_middle_center,
1116 ui_colour(ctx, k_ui_bg+4) );
1117 }
1118
1119 if( ui_button_text( ctx, button_r, "Cancel", 1 ) == 1 )
1120 {
1121 workshop_form.page = k_workshop_form_open;
1122 workshop_form.file_intent = k_workshop_form_file_intent_none;
1123 }
1124 }
1125
1126 static void workshop_form_gui_draw_preview( ui_context *ctx, ui_rect img_box )
1127 {
1128 enum addon_type type = workshop_form.submission.type;
1129 if( workshop_form.file_intent == k_workshop_form_file_intent_keep_old )
1130 {
1131 ui_image( ctx,
1132 img_box, &gpipeline.fb_workshop_preview->attachments[0].id );
1133 }
1134 else if( workshop_form.file_intent == k_workshop_form_file_intent_new )
1135 {
1136 ui_image( ctx,
1137 img_box, &gpipeline.fb_workshop_preview->attachments[0].id );
1138
1139 if( type == k_addon_type_world )
1140 {
1141 return;
1142 }
1143
1144 int hover = ui_inside_rect( img_box, ctx->mouse ),
1145 target = ui_inside_rect( img_box, ctx->mouse_click );
1146
1147 if( ui_click_down(ctx,UI_MOUSE_MIDDLE) && target )
1148 {
1149 v3_copy( workshop_form.view_offset,
1150 workshop_form.view_offset_begin );
1151 }
1152 else if( ui_click_down(ctx,UI_MOUSE_LEFT) && target )
1153 {
1154 v2_copy( workshop_form.view_angles,
1155 workshop_form.view_angles_begin );
1156 }
1157
1158 if( ui_clicking(ctx,UI_MOUSE_MIDDLE) && target )
1159 {
1160 v2f delta = { ctx->mouse[0]-ctx->mouse_click[0],
1161 ctx->mouse[1]-ctx->mouse_click[1] };
1162
1163 float *begin = workshop_form.view_offset_begin,
1164 *offset = workshop_form.view_offset;
1165 offset[0] = vg_clampf( begin[0]-delta[0]*0.002f, -1.0f, 1.0f );
1166 offset[2] = vg_clampf( begin[2]-delta[1]*0.002f, -1.0f, 1.0f );
1167 workshop_form.view_changed = 1;
1168 }
1169 else if( ui_clicking(ctx,UI_MOUSE_LEFT) && target )
1170 {
1171 v2f delta = { ctx->mouse[0]-ctx->mouse_click[0],
1172 ctx->mouse[1]-ctx->mouse_click[1] };
1173
1174 v2f angles;
1175 v2_muladds( workshop_form.view_angles_begin, delta, 0.002f, angles);
1176
1177 float limit = VG_PIf*0.2f;
1178
1179 angles[0] = vg_clampf( angles[0], -limit, limit );
1180 angles[1] = vg_clampf( angles[1], -limit, limit );
1181
1182 v2_copy( angles, workshop_form.view_angles );
1183 workshop_form.view_changed = 1;
1184 }
1185
1186 if( !ui_clicking(ctx,UI_MOUSE_LEFT) && hover )
1187 {
1188 float zoom = workshop_form.view_dist;
1189 zoom += vg.mouse_wheel[1] * -0.07f;
1190 zoom = vg_clampf( zoom, 0.4f, 2.0f );
1191
1192 if( zoom != workshop_form.view_dist )
1193 {
1194 workshop_form.view_changed = 1;
1195 workshop_form.view_dist = zoom;
1196 }
1197 }
1198 }
1199 else
1200 {
1201 ui_text( ctx, img_box, "No image", 1, k_ui_align_middle_center,
1202 ui_colour( ctx, k_ui_orange ) );
1203 }
1204 }
1205
1206 static void workshop_form_gui_edit_page( ui_context *ctx, ui_rect content )
1207 {
1208 enum addon_type type = workshop_form.submission.type;
1209
1210 if( type == k_addon_type_none )
1211 {
1212 workshop_form_gui_page_undecided( ctx, content );
1213 return;
1214 }
1215
1216 ui_rect image_plane;
1217 ui_split( content, k_ui_axis_h, 300, 0, image_plane, content );
1218 ui_fill( ctx, image_plane, ui_colour( ctx, k_ui_bg+0 ) );
1219
1220 ui_rect img_box;
1221 ui_fit_item( image_plane, (ui_px[2]){ 3, 2 }, img_box );
1222 workshop_form_gui_draw_preview( ctx, img_box );
1223
1224 /* file path */
1225 ui_rect file_button, file_label;
1226
1227 char buf[128];
1228 snprintf( buf, 128,
1229 "Addon folder: skaterift/%s", workshop_filetype_folder() );
1230
1231 if( type == k_addon_type_world )
1232 {
1233 struct ui_textbox_callbacks callbacks =
1234 {
1235 .change = workshop_changed_model_path
1236 };
1237 ui_textbox( ctx, content, buf, workshop_form.addon_folder,
1238 vg_list_size(workshop_form.addon_folder), 1, 0, &callbacks );
1239 }
1240 else
1241 {
1242 ui_rect file_entry;
1243 ui_standard_widget( ctx, content, file_entry, 1 );
1244 ui_split( file_entry, k_ui_axis_v, -128, 0, file_entry, file_button );
1245
1246 if( workshop_form.file_intent != k_workshop_form_file_intent_none )
1247 {
1248 ui_text( ctx, file_entry, workshop_form.addon_folder, 1,
1249 k_ui_align_middle_left, ui_colour( ctx, k_ui_fg+4 ) );
1250
1251 if( ui_button_text( ctx, file_button, "Remove", 1 ) == 1 )
1252 {
1253 if( type == k_addon_type_board )
1254 player_board_unload( &workshop_form.board_model );
1255 else if( type == k_addon_type_player )
1256 player_model_unload( &workshop_form.player_model );
1257
1258 workshop_form.file_intent = k_workshop_form_file_intent_none;
1259 workshop_form.addon_folder[0] = '\0';
1260 }
1261 }
1262 else
1263 {
1264 struct ui_textbox_callbacks callbacks =
1265 {
1266 .change = workshop_changed_model_path
1267 };
1268
1269 ui_textbox( ctx, file_entry, buf, workshop_form.addon_folder,
1270 vg_list_size(workshop_form.addon_folder), 1,
1271 0, &callbacks );
1272
1273 if( ui_button_text( ctx, file_button, "Load", 1 ) == 1 )
1274 {
1275 workshop_op_load_model( ctx );
1276 }
1277 }
1278 }
1279
1280 const char *str_title = "Title:", *str_desc = "Description:";
1281
1282 /* title box */
1283 {
1284 struct ui_textbox_callbacks callbacks = {
1285 .change = workshop_changed_title
1286 };
1287 ui_textbox( ctx, content, str_title, workshop_form.submission.title,
1288 vg_list_size(workshop_form.submission.title), 1,
1289 0, &callbacks );
1290 }
1291
1292 /* visibility option */
1293 {
1294 ui_enum( ctx, content, "Visibility:", workshop_form_visibility_opts,
1295 4, &workshop_form.submission.visibility );
1296 }
1297
1298 /* description box */
1299 {
1300 struct ui_textbox_callbacks callbacks =
1301 {
1302 .change = workshop_changed_description
1303 };
1304 ui_textbox( ctx, content, str_desc, workshop_form.submission.description,
1305 vg_list_size(workshop_form.submission.description), 4,
1306 UI_TEXTBOX_MULTILINE|UI_TEXTBOX_WRAP, &callbacks );
1307 }
1308
1309 /* submissionable */
1310 ui_rect final_row;
1311 ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content, final_row );
1312
1313 ui_rect submission_center;
1314 rect_copy( final_row, submission_center );
1315 submission_center[2] = 256;
1316 ui_rect_center( final_row, submission_center );
1317
1318 ui_rect btn_left, btn_right;
1319 ui_split_ratio( submission_center, k_ui_axis_v, 0.5f, 8,
1320 btn_left, btn_right );
1321
1322 if( ui_button_text( ctx, btn_left, "Publish", 1 ) == 1 )
1323 {
1324 workshop_op_submit( ctx );
1325 }
1326 if( ui_button_text( ctx, btn_right, "Cancel", 1 ) == 1 )
1327 {
1328 workshop_form.page = k_workshop_form_open;
1329 player_board_unload( &workshop_form.board_model );
1330 workshop_form.file_intent = k_workshop_form_file_intent_none;
1331 }
1332
1333 /* disclaimer */
1334 const char *disclaimer_text =
1335 "By submitting this item, you agree to the workshop terms of service";
1336
1337 ui_rect disclaimer_row, inner, link;
1338 ui_split( content, k_ui_axis_h, content[3]-32, 0, content, disclaimer_row );
1339
1340 ui_px btn_width = 32;
1341
1342 rect_copy( disclaimer_row, inner );
1343 inner[2] = ui_text_line_width( ctx, disclaimer_text ) + btn_width+8;
1344
1345 ui_rect label;
1346 ui_rect_center( disclaimer_row, inner );
1347 ui_split( inner, k_ui_axis_v, inner[2]-btn_width, 0, label, btn_right);
1348 ui_rect_pad( btn_right, (ui_px[2]){2,2} );
1349
1350 if( ui_button_text( ctx, btn_right, "\xb2", 2 ) == 1 )
1351 {
1352 ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
1353 SteamAPI_ISteamFriends_ActivateGameOverlayToWebPage( hSteamFriends,
1354 "https://steamcommunity.com/sharedfiles/workshoplegalagreement",
1355 k_EActivateGameOverlayToWebPageMode_Default );
1356 }
1357
1358 ui_text( ctx, label, disclaimer_text, 1, k_ui_align_middle_left,
1359 ui_colour( ctx, k_ui_fg+4 ) );
1360 }
1361
1362 static void workshop_form_gui_sidebar( ui_context *ctx, ui_rect sidebar )
1363 {
1364 ui_fill( ctx, sidebar, ui_colour( ctx, k_ui_bg+2 ) );
1365
1366 ui_rect title;
1367 ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
1368
1369 if( workshop_form.page == k_workshop_form_edit )
1370 {
1371 ui_text( ctx, title, "Editing", 1, k_ui_align_middle_center, 0 );
1372 ui_split( sidebar, k_ui_axis_h, 28, 0, title, sidebar );
1373
1374 if( workshop_form.submission.type != k_addon_type_none )
1375 {
1376 char buf[512];
1377 vg_str str;
1378 vg_strnull( &str, buf, 512 );
1379
1380 if( workshop_form.submission.file_id )
1381 vg_strcat( &str, "Editing an existing " );
1382 else
1383 vg_strcat( &str, "Creating a new " );
1384
1385 if( workshop_form.submission.type == k_addon_type_board )
1386 vg_strcat( &str, "skateboard." );
1387 else if( workshop_form.submission.type == k_addon_type_world )
1388 vg_strcat( &str, "world." );
1389 else if( workshop_form.submission.type == k_addon_type_player )
1390 vg_strcat( &str, "playermodel." );
1391 else
1392 vg_strcat( &str, "???." );
1393
1394 ui_text( ctx, title, buf, 1, k_ui_align_middle_center,
1395 ui_colour(ctx, k_ui_fg+4) );
1396 }
1397 return;
1398 }
1399
1400 /*
1401 * sidebar existing entries panel
1402 */
1403 ui_text( ctx, title, "Your submissions", 1, k_ui_align_middle_center, 0 );
1404
1405 ui_rect controls, btn_create_new;
1406 ui_split( sidebar, k_ui_axis_h, 32, 0, controls, sidebar );
1407 ui_split( sidebar, k_ui_axis_h, -32, 0, sidebar, btn_create_new );
1408 ui_fill( ctx, controls, ui_colour( ctx, k_ui_bg+1 ) );
1409
1410 char buf[32];
1411 vg_str str;
1412 vg_strnull( &str, buf, sizeof(buf) );
1413 vg_strcat( &str, "page " );
1414 vg_strcati32( &str, workshop_form.view_published_page_id+1 );
1415 vg_strcatch( &str, '/' );
1416 vg_strcati32( &str, workshop_form.view_published_page_count );
1417
1418 ui_rect_pad( controls, (ui_px[2]){0,4} );
1419 ui_rect info;
1420 ui_split_ratio( controls, k_ui_axis_v, 0.25f, 0, info, controls );
1421 ui_text( ctx, info, buf, 1, k_ui_align_middle_center, 0 );
1422
1423 ui_rect btn_left, btn_right;
1424 ui_split_ratio( controls, k_ui_axis_v, 0.5f, 2, btn_left, btn_right );
1425
1426 if( ui_button_text( ctx, btn_left, "newer", 1 ) == 1 )
1427 {
1428 workshop_view_page( workshop_form.view_published_page_id-1 );
1429 }
1430
1431 if( ui_button_text( ctx, btn_right, "older", 1 ) == 1 )
1432 {
1433 workshop_view_page( workshop_form.view_published_page_count+1 );
1434 }
1435
1436 if( ui_button_text( ctx, btn_create_new, "Create New Item", 1 ) == 1 )
1437 {
1438 workshop_reset_submission_data();
1439 workshop_form.submission.submit_title = 1;
1440 workshop_form.submission.submit_description = 1;
1441 workshop_form.submission.submit_file_and_image = 1;
1442 workshop_form.page = k_workshop_form_edit;
1443 }
1444
1445 for( int i=0; i<workshop_form.published_files_list_length; i++ )
1446 {
1447 ui_rect item;
1448 ui_split( sidebar, k_ui_axis_h, 28, 0, item, sidebar );
1449 ui_rect_pad( item, (ui_px[2]){4,4} );
1450
1451 struct published_file *pfile = &workshop_form.published_files_list[i];
1452 if( ui_button_text( ctx, item, pfile->title, 1 ) == 1 )
1453 {
1454 if( pfile->result == k_EResultOK )
1455 {
1456 vg_info( "Select index: %d\n", pfile->result_index );
1457 workshop_op_download_and_view_submission( pfile->result_index );
1458 }
1459 else
1460 {
1461 vg_warn( "Cannot select that item, result not OK\n" );
1462 }
1463 }
1464 }
1465 }
1466
1467 void workshop_form_gui( ui_context *ctx )
1468 {
1469 enum workshop_form_page stable_page = workshop_form.page;
1470 if( stable_page == k_workshop_form_hidden ) return;
1471
1472 ui_rect null;
1473 ui_rect screen = { 0, 0, vg.window_x, vg.window_y };
1474 ui_rect window = { 0, 0, 1000, 700 };
1475 ui_rect_center( screen, window );
1476 ctx->wants_mouse = 1;
1477
1478 ui_fill( ctx, window, ui_colour( ctx, k_ui_bg+1 ) );
1479 ui_outline( ctx, window, 1, ui_colour( ctx, k_ui_bg+7 ), 0 );
1480
1481 ui_rect title, panel;
1482 ui_split( window, k_ui_axis_h, 28, 0, title, panel );
1483 ui_fill( ctx, title, ui_colour( ctx, k_ui_bg+7 ) );
1484 ui_text( ctx, title, "Workshop tool", 1, k_ui_align_middle_center,
1485 ui_colourcont( ctx, k_ui_bg+7 ) );
1486
1487 ui_rect quit_button;
1488 ui_split( title, k_ui_axis_v, title[2]-title[3], 2, title, quit_button );
1489
1490 if( vg_loader_availible() )
1491 {
1492 if( ui_button_text( ctx, quit_button, "X", 1 ) == 1 )
1493 {
1494 workshop_quit_form();
1495 return;
1496 }
1497 }
1498
1499 /*
1500 * temporary operation blinders, we don't yet have a nice way to show the
1501 * user that we're doing something uninterruptable, so the code just
1502 * escapes here and we show them a basic string
1503 */
1504
1505 if( workshop_form.op != k_workshop_op_none )
1506 {
1507 const char *op_string = "The programmer has not bothered to describe "
1508 "the current operation that is running.";
1509
1510 switch( workshop_form.op )
1511 {
1512 case k_workshop_op_loading_model:
1513 op_string = "Operation in progress: Loading model file.";
1514 break;
1515 case k_workshop_op_publishing_update:
1516 op_string = "Operation in progress: publishing submission update "
1517 "to steam.";
1518 break;
1519 case k_workshop_op_downloading_submission:
1520 op_string = "Operation in progress: downloading existing submission"
1521 " from Steam services.";
1522 break;
1523 default: break;
1524 }
1525
1526 ui_text( ctx, panel, op_string, 1, k_ui_align_middle_center, 0 );
1527 return;
1528 }
1529
1530 /* re draw board preview if need to */
1531 if( (stable_page == k_workshop_form_edit) &&
1532 workshop_form.view_changed &&
1533 workshop_form.file_intent == k_workshop_form_file_intent_new )
1534 {
1535 enum addon_type type = workshop_form.submission.type;
1536 if( type == k_addon_type_board ){
1537 workshop_render_board_preview();
1538 }
1539 else if( type == k_addon_type_world ){
1540 vg_success( "Renders world preview\n" );
1541 workshop_render_world_preview();
1542 }
1543 else if( type == k_addon_type_player ){
1544 workshop_render_player_preview();
1545 }
1546
1547 workshop_form.view_changed = 0;
1548 }
1549
1550 struct workshop_form *form = &workshop_form;
1551
1552 ui_rect sidebar, content;
1553 ui_split_ratio( panel, k_ui_axis_v, 0.3f, 1, sidebar, content );
1554
1555 /* content page */
1556 ui_rect_pad( content, (ui_px[2]){8,8} );
1557
1558 if( stable_page == k_workshop_form_edit )
1559 {
1560 workshop_form_gui_edit_page( ctx, content );
1561 }
1562 else if( stable_page == k_workshop_form_open )
1563 {
1564 ui_text( ctx, content, "Nothing selected.", 1, k_ui_align_middle_center,
1565 ui_colour( ctx, k_ui_fg+4 ) );
1566 }
1567 else if( stable_page >= k_workshop_form_cclosing )
1568 {
1569 ui_rect submission_row;
1570 ui_split( content, k_ui_axis_h, content[3]-32-8, 0, content,
1571 submission_row );
1572
1573 u32 colour;
1574
1575 if( stable_page == k_workshop_form_closing_bad )
1576 colour = ui_colour( ctx, k_ui_red+k_ui_brighter );
1577 else
1578 colour = ui_colour( ctx, k_ui_green+k_ui_brighter );
1579
1580 ui_text( ctx, content, workshop_form.failure_or_success_string, 1,
1581 k_ui_align_middle_center, colour );
1582
1583 ui_rect submission_center;
1584 rect_copy( submission_row, submission_center );
1585 submission_center[2] = 128;
1586 ui_rect_center( submission_row, submission_center );
1587 ui_rect_pad( submission_center, (ui_px[2]){8,8} );
1588
1589 if( ui_button_text( ctx, submission_center, "OK", 1 ) == 1 )
1590 {
1591 workshop_form.page = k_workshop_form_open;
1592 }
1593 }
1594
1595 workshop_form_gui_sidebar( ctx, sidebar );
1596 }
1597
1598 /*
1599 * Some async api stuff
1600 * -----------------------------------------------------------------------------
1601 */
1602
1603 void async_workshop_get_filepath( void *data, u32 len )
1604 {
1605 struct async_workshop_filepath_info *info = data;
1606
1607 u64 _size;
1608 u32 _ts;
1609
1610 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
1611 if( !SteamAPI_ISteamUGC_GetItemInstallInfo( hSteamUGC, info->id, &_size,
1612 info->buf, info->len, &_ts ))
1613 {
1614 vg_error( "GetItemInstallInfo failed\n" );
1615 info->buf[0] = '\0';
1616 }
1617 }
1618
1619 void async_workshop_get_installed_files( void *data, u32 len )
1620 {
1621 struct async_workshop_installed_files_info *info = data;
1622
1623 ISteamUGC *hSteamUGC = SteamAPI_SteamUGC();
1624 u32 count = SteamAPI_ISteamUGC_GetSubscribedItems( hSteamUGC, info->buffer,
1625 *info->len );
1626
1627 vg_info( "Found %u subscribed items\n", count );
1628
1629 u32 j=0;
1630 for( u32 i=0; i<count; i++ ){
1631 u32 state = SteamAPI_ISteamUGC_GetItemState( hSteamUGC, info->buffer[i] );
1632 if( state & k_EItemStateInstalled ){
1633 info->buffer[j ++] = info->buffer[i];
1634 }
1635 }
1636
1637 *info->len = j;
1638 }