a mess but stable
[carveJwlIkooP6JGAAIwe30JlM.git] / gui.h
1 #pragma once
2 #include "font.h"
3 #include "input.h"
4 #include "player.h"
5 #include "vg/vg_engine.h"
6 #include "vg/vg_ui/imgui.h"
7
8 #define GUI_COL_DARK ui_opacity( 0x00000000, 0.7f )
9 #define GUI_COL_NORM ui_opacity( 0x00101010, 0.7f )
10 #define GUI_COL_ACTIVE ui_opacity( 0x00444444, 0.7f )
11 #define GUI_COL_CLICK ui_opacity( 0x00858585, 0.7f )
12 #define GUI_COL_HI ui_opacity( 0x00ffffff, 0.8f )
13
14 enum gui_icon {
15 k_gui_icon_tick = 0,
16 k_gui_icon_tick_2d,
17 k_gui_icon_exclaim,
18 k_gui_icon_exclaim_2d,
19 k_gui_icon_board,
20 k_gui_icon_world,
21 k_gui_icon_rift,
22 k_gui_icon_rift_run,
23 k_gui_icon_rift_run_2d,
24 k_gui_icon_friend,
25 k_gui_icon_player,
26 k_gui_icon_rift_run_gold,
27 k_gui_icon_rift_run_silver,
28 k_gui_icon_glider,
29 k_gui_icon_spawn,
30 k_gui_icon_spawn_select,
31
32 k_gui_icon_count,
33 };
34
35 #define GUI_HELPER_TEXT_LENGTH 32
36
37 struct{
38 struct gui_helper{
39 vg_input_op *binding;
40 char text[GUI_HELPER_TEXT_LENGTH];
41 int greyed;
42 }
43 helpers[4];
44 u32 helper_count;
45
46 int active_positional_helper;
47
48 struct icon_call {
49 enum gui_icon icon;
50 v4f location;
51 v4f colour;
52 int colour_changed;
53 }
54 icon_draw_buffer[64];
55 u32 icon_draw_count;
56 v4f cur_icon_colour;
57 int colour_changed;
58
59 char location[64];
60 f64 location_time;
61
62 f32 factive;
63 font3d font;
64
65 v3f trick_co;
66
67 mdl_context model_icons;
68 GLuint icons_texture;
69 glmesh icons_mesh;
70
71 mdl_submesh *icons[ k_gui_icon_count ];
72 }
73 static gui = {.cur_icon_colour = {1.0f,1.0f,1.0f,1.0f},.colour_changed=1};
74
75 static void gui_helper_clear(void){
76 gui.helper_count = 0;
77 gui.active_positional_helper = 0;
78 }
79
80 static struct gui_helper *gui_new_helper( vg_input_op *bind, vg_str *out_text ){
81 if( gui.helper_count >= vg_list_size(gui.helpers) ){
82 vg_error( "Too many helpers\n" );
83 return NULL;
84 }
85
86 struct gui_helper *helper = &gui.helpers[ gui.helper_count ++ ];
87 helper->greyed = 0;
88 helper->binding = bind;
89 vg_strnull( out_text, helper->text, sizeof(helper->text) );
90 return helper;
91 }
92
93 static void gui_render_icons(void)
94 {
95 vg_camera ortho;
96
97 float fl = 0.0f,
98 fr = vg.window_x,
99 fb = 0.0f,
100 ft = vg.window_y,
101 rl = 1.0f / (fr-fl),
102 tb = 1.0f / (ft-fb);
103
104 m4x4_zero( ortho.mtx.p );
105 ortho.mtx.p[0][0] = 2.0f * rl;
106 ortho.mtx.p[1][1] = 2.0f * tb;
107 ortho.mtx.p[3][0] = (fr + fl) * -rl;
108 ortho.mtx.p[3][1] = (ft + fb) * -tb;
109 ortho.mtx.p[3][3] = 1.0f;
110 m4x3_identity( ortho.transform );
111 vg_camera_update_view( &ortho );
112 m4x4_mul( ortho.mtx.p, ortho.mtx.v, ortho.mtx.pv ); /* HACK */
113 vg_camera_finalize( &ortho );
114
115 /* icons */
116 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &ortho );
117 mesh_bind( &gui.icons_mesh );
118
119 m4x3f mmdl;
120 m4x3_identity( mmdl );
121 shader_model_font_uMdl( mmdl );
122
123 glActiveTexture( GL_TEXTURE0 );
124 glBindTexture( GL_TEXTURE_2D, gui.icons_texture );
125 shader_model_font_uTexMain( 0 );
126
127 for( u32 i=0; i<gui.icon_draw_count; i++ ){
128 struct icon_call *call = &gui.icon_draw_buffer[i];
129
130 if( call->colour_changed )
131 shader_model_font_uColour( call->colour );
132
133 shader_model_font_uOffset( call->location );
134
135 mdl_submesh *sm = gui.icons[ call->icon ];
136 if( sm )
137 mdl_draw_submesh( sm );
138 }
139
140 gui.icon_draw_count = 0;
141 }
142
143 static void gui_draw( ui_context *ctx )
144 {
145 if( gui.active_positional_helper &&
146 (v3_dist2(localplayer.rb.co,gui.trick_co) > 2.0f) )
147 gui_helper_clear();
148
149 /* helpers
150 * ----------------------------------------------------------------- */
151
152 gui.factive = vg_lerpf( gui.factive, gui.helper_count?1.0f:0.0f,
153 vg.time_frame_delta*2.0f );
154
155 ctx->font = &vgf_default_title;
156 ui_px height = ctx->font->ch + 16;
157 ui_rect lwr = { 0, vg.window_y - height, vg.window_x, height };
158
159 ui_px x = 0;
160 for( u32 i=0; i<gui.helper_count; i++ )
161 {
162 struct gui_helper *helper = &gui.helpers[i];
163
164 char buf[128];
165 vg_str str;
166 vg_strnull( &str, buf, sizeof(buf) );
167 vg_input_string( &str, helper->binding, 1 );
168
169 ui_rect box = { x, lwr[1], 1000, lwr[3] };
170
171 u32 fg = 0;
172 f32 opacity = 0.4f;
173 if( helper->greyed )
174 {
175 fg = ui_colour(ctx, k_ui_fg+2);
176 opacity = 0.1f;
177 }
178
179 struct ui_vert *bg = ui_fill( ctx, box,
180 ui_opacity( GUI_COL_DARK, opacity ) );
181
182 u32 w;
183 box[0] += 16;
184 w = ui_text( ctx, box, buf, 1, k_ui_align_middle_left, fg );
185 w *= ctx->font->sx;
186 bg[1].co[0] = x + w + 32;
187 bg[2].co[0] = x + w + 32;
188 x += w + 32;
189
190 box[0] = x;
191 bg = ui_fill( ctx, box, ui_opacity( GUI_COL_NORM, opacity*0.7f ) );
192 box[0] += 8;
193 w = ui_text( ctx, box, helper->text, 1, k_ui_align_middle_left, fg );
194 w *= ctx->font->sx;
195 bg[1].co[0] = box[0] + w + 16;
196 bg[2].co[0] = box[0] + w + 16;
197 x += w + 32;
198 }
199
200 vg_ui.frosting = gui.factive*0.015f;
201 ui_flush( ctx, k_ui_shader_colour, NULL );
202 vg_ui.frosting = 0.0f;
203
204
205 f64 loc_t = (vg.time_real - gui.location_time) / 5.0;
206 if( (loc_t < 1.0) && (gui.location_time != 0.0) )
207 {
208 f32 t = 1.0f-vg_minf(1.0f,vg_minf(loc_t*20.0f,2.0f-loc_t*2.0f)),
209 o = 1.0f-t*t*(2.0f-t);
210
211 ui_rect box = { 0, (vg.window_y*2)/3 - height/2, vg.window_x, height };
212 ui_fill( ctx, box, ui_opacity( GUI_COL_NORM, 0.5f ) );
213 ui_text( ctx, box, gui.location, 1, k_ui_align_middle_center, 0 );
214
215 vg_ui.colour[3] = o;
216 ui_flush( ctx, k_ui_shader_colour, NULL );
217 }
218
219 vg_ui.colour[3] = 1.0f;
220 ctx->font = &vgf_default_small;
221 }
222
223 static int gui_location_print_ccmd( int argc, const char *argv[] )
224 {
225 if( argc > 0 )
226 {
227 char new_loc[64];
228 vg_str str;
229 vg_strnull( &str, new_loc, 64 );
230 for( int i=0; i<argc; i++ )
231 {
232 vg_strcat( &str, argv[i] );
233 vg_strcat( &str, " " );
234 }
235 if( !strcmp(gui.location,new_loc) ) return 0;
236 vg_strncpy( new_loc, gui.location, 64, k_strncpy_always_add_null );
237 gui.location_time = vg.time_real;
238 }
239 return 0;
240 }
241
242 static int gui_showtrick_ccmd( int argc, const char *argv[] )
243 {
244 if( argc == 1 )
245 {
246 gui_helper_clear();
247 vg_str text;
248
249 if( !strcmp( argv[0], "pump" ) ){
250 if( gui_new_helper( input_axis_list[k_sraxis_grab], &text ) )
251 vg_strcat( &text, "Pump" );
252 }
253 else if( !strcmp( argv[0], "flip" ) ){
254 if( gui_new_helper( input_joy_list[k_srjoystick_steer], &text ) )
255 vg_strcat( &text, "Flip" );
256 }
257 else if( !strcmp( argv[0], "ollie" ) ){
258 if( gui_new_helper( input_button_list[k_srbind_jump], &text ) )
259 vg_strcat( &text, "Ollie" );
260 }
261 else if( !strcmp( argv[0], "trick" ) ){
262 if( gui_new_helper( input_button_list[k_srbind_trick0], &text ) )
263 vg_strcat( &text, "Shuvit" );
264 if( gui_new_helper( input_button_list[k_srbind_trick1], &text ) )
265 vg_strcat( &text, "Kickflip" );
266 if( gui_new_helper( input_button_list[k_srbind_trick2], &text ) )
267 vg_strcat( &text, "Tre-Flip" );
268 }
269 else if( !strcmp( argv[0], "misc" ) ){
270 if( gui_new_helper( input_button_list[k_srbind_camera], &text ) )
271 vg_strcat( &text, "Camera" );
272 if( gui_new_helper( input_button_list[k_srbind_use], &text ) )
273 vg_strcat( &text, "Skate/Walk" );
274 }
275 else return 1;
276
277 v3_copy( localplayer.rb.co, gui.trick_co );
278 gui.active_positional_helper = 1;
279 return 0;
280 }
281 return 1;
282 }
283
284 static void gui_draw_icon( enum gui_icon icon, v2f co, f32 size )
285 {
286 if( gui.icon_draw_count == vg_list_size(gui.icon_draw_buffer) )
287 return;
288
289 struct icon_call *call = &gui.icon_draw_buffer[ gui.icon_draw_count ++ ];
290
291 call->icon = icon;
292 call->location[0] = co[0] * (f32)vg.window_x;
293 call->location[1] = co[1] * (f32)vg.window_y;
294 call->location[2] = 0.0f;
295 call->location[3] = size * (f32)vg.window_x;
296
297 v4_copy( gui.cur_icon_colour, call->colour );
298 call->colour_changed = gui.colour_changed;
299 gui.colour_changed = 0;
300 }
301
302 static void gui_icon_setcolour( v4f colour ){
303 gui.colour_changed = 1;
304 v4_copy( colour, gui.cur_icon_colour );
305 }
306
307 static mdl_submesh *gui_find_icon( const char *name ){
308 mdl_mesh *mesh = mdl_find_mesh( &gui.model_icons, name );
309 if( mesh ){
310 if( mesh->submesh_count ){
311 return mdl_arritm( &gui.model_icons.submeshs, mesh->submesh_start );
312 }
313 }
314
315 return NULL;
316 }
317
318 static void gui_init(void)
319 {
320 font3d_load( &gui.font, "models/rs_font.mdl", vg_mem.rtmemory );
321 vg_console_reg_cmd( "gui_location", gui_location_print_ccmd, NULL );
322 vg_console_reg_cmd( "showtrick", gui_showtrick_ccmd, NULL );
323
324 /* load icons */
325 void *alloc = vg_mem.rtmemory;
326 mdl_open( &gui.model_icons, "models/rs_icons.mdl", alloc );
327 mdl_load_metadata_block( &gui.model_icons, alloc );
328
329 gui.icons[ k_gui_icon_tick ] = gui_find_icon( "icon_tick" );
330 gui.icons[ k_gui_icon_tick_2d ] = gui_find_icon( "icon_tick2d" );
331 gui.icons[ k_gui_icon_exclaim ] = gui_find_icon( "icon_exclaim" );
332 gui.icons[ k_gui_icon_exclaim_2d ] = gui_find_icon( "icon_exclaim2d" );
333 gui.icons[ k_gui_icon_board ] = gui_find_icon( "icon_board" );
334 gui.icons[ k_gui_icon_world ] = gui_find_icon( "icon_world" );
335 gui.icons[ k_gui_icon_rift ] = gui_find_icon( "icon_rift" );
336 gui.icons[ k_gui_icon_rift_run ] = gui_find_icon( "icon_rift_run" );
337 gui.icons[ k_gui_icon_rift_run_2d ] = gui_find_icon( "icon_rift_run2d" );
338 gui.icons[ k_gui_icon_friend ] = gui_find_icon( "icon_friend" );
339 gui.icons[ k_gui_icon_player ] = gui_find_icon( "icon_player" );
340 gui.icons[ k_gui_icon_glider ] = gui_find_icon( "icon_glider" );
341 gui.icons[ k_gui_icon_spawn ] = gui_find_icon( "icon_spawn" );
342 gui.icons[ k_gui_icon_spawn_select ] = gui_find_icon( "icon_spawn_select" );
343 gui.icons[ k_gui_icon_rift_run_gold ] =
344 gui_find_icon("icon_rift_run_medal_gold");
345 gui.icons[ k_gui_icon_rift_run_silver]=
346 gui_find_icon("icon_rift_run_medal_silver");
347
348 vg_linear_clear( vg_mem.scratch );
349 if( !mdl_arrcount( &gui.model_icons.textures ) )
350 vg_fatal_error( "No texture in menu file" );
351 mdl_texture *tex0 = mdl_arritm( &gui.model_icons.textures, 0 );
352 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
353 mdl_fread_pack_file( &gui.model_icons, &tex0->file, data );
354 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
355 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
356 &gui.icons_texture );
357
358 mdl_async_load_glmesh( &gui.model_icons, &gui.icons_mesh, NULL );
359 mdl_close( &gui.model_icons );
360 }