forgot to lock!
[carveJwlIkooP6JGAAIwe30JlM.git] / gui.h
1 #ifndef GUI_H
2 #define GUI_H
3
4 #include "font.h"
5 #include "input.h"
6 #include "player.h"
7
8 enum gui_icon {
9 k_gui_icon_tick = 0,
10 k_gui_icon_exclaim,
11 k_gui_icon_board,
12 k_gui_icon_world,
13 k_gui_icon_rift,
14 k_gui_icon_rift_run,
15 k_gui_icon_friend,
16
17 k_gui_icon_count,
18 };
19
20 #define GUI_HELPER_TEXT_LENGTH 32
21
22 struct{
23 struct gui_helper{
24 vg_input_op *binding;
25 char text[GUI_HELPER_TEXT_LENGTH];
26 int greyed;
27 }
28 helpers[4];
29 u32 helper_count;
30
31 int active_positional_helper;
32
33 struct icon_call {
34 enum gui_icon icon;
35 v4f location;
36 v4f colour;
37 int colour_changed;
38 }
39 icon_draw_buffer[32];
40 u32 icon_draw_count;
41 v4f cur_icon_colour;
42 int colour_changed;
43
44 char location[64];
45 f64 location_time;
46
47 f32 factive;
48 font3d font;
49
50 v3f trick_co;
51
52 mdl_context model_icons;
53 GLuint icons_texture;
54 glmesh icons_mesh;
55
56 mdl_submesh *icons[ k_gui_icon_count ];
57 }
58 static gui = {.cur_icon_colour = {1.0f,1.0f,1.0f,1.0f},.colour_changed=1};
59
60 static void gui_helper_clear(void){
61 gui.helper_count = 0;
62 gui.active_positional_helper = 0;
63 }
64
65 static struct gui_helper *gui_new_helper( vg_input_op *bind, vg_str *out_text ){
66 if( gui.helper_count >= vg_list_size(gui.helpers) ){
67 vg_error( "Too many helpers\n" );
68 return NULL;
69 }
70
71 struct gui_helper *helper = &gui.helpers[ gui.helper_count ++ ];
72 helper->greyed = 0;
73 helper->binding = bind;
74 vg_strnull( out_text, helper->text, sizeof(helper->text) );
75 return helper;
76 }
77
78 static void gui_draw(void){
79 if( gui.active_positional_helper &&
80 (v3_dist2(localplayer.rb.co,gui.trick_co) > 2.0f) )
81 gui_helper_clear();
82
83 camera ortho;
84
85 float fl = 0.0f,
86 fr = vg.window_x,
87 fb = 0.0f,
88 ft = vg.window_y,
89 rl = 1.0f / (fr-fl),
90 tb = 1.0f / (ft-fb);
91
92 m4x4_zero( ortho.mtx.p );
93 ortho.mtx.p[0][0] = 2.0f * rl;
94 ortho.mtx.p[1][1] = 2.0f * tb;
95 ortho.mtx.p[3][0] = (fr + fl) * -rl;
96 ortho.mtx.p[3][1] = (ft + fb) * -tb;
97 ortho.mtx.p[3][3] = 1.0f;
98 m4x3_identity( ortho.transform );
99 camera_update_view( &ortho );
100 m4x4_mul( ortho.mtx.p, ortho.mtx.v, ortho.mtx.pv ); /* HACK */
101 camera_finalize( &ortho );
102
103
104 /* icons */
105 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &ortho );
106 mesh_bind( &gui.icons_mesh );
107
108 m4x3f mmdl;
109 m4x3_identity( mmdl );
110 shader_model_font_uMdl( mmdl );
111
112 glActiveTexture( GL_TEXTURE0 );
113 glBindTexture( GL_TEXTURE_2D, gui.icons_texture );
114 shader_model_font_uTexMain( 0 );
115
116 for( u32 i=0; i<gui.icon_draw_count; i++ ){
117 struct icon_call *call = &gui.icon_draw_buffer[i];
118
119 if( call->colour_changed )
120 shader_model_font_uColour( call->colour );
121
122 shader_model_font_uOffset( call->location );
123
124 mdl_submesh *sm = gui.icons[ call->icon ];
125 if( sm )
126 mdl_draw_submesh( sm );
127 }
128
129 gui.icon_draw_count = 0;
130
131
132
133 gui.factive = vg_lerpf( gui.factive, gui.helper_count?1.0f:0.0f,
134 vg.time_frame_delta*2.0f );
135
136 if( gui.factive > 0.01f ){
137 /* draw bottom bar */
138 glEnable(GL_BLEND);
139 glDisable(GL_DEPTH_TEST);
140 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
141 glBlendEquation(GL_FUNC_ADD);
142
143 shader_blitcolour_use();
144 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, gui.factive*0.8f } );
145 render_fsquad1();
146 }
147
148 f64 loc_t = (vg.time_real - gui.location_time) / 5.0;
149 if( (loc_t < 1.0) && (gui.location_time != 0.0) ){
150 /* yep this code is a mess, i dont care anymore */
151 glEnable(GL_BLEND);
152 glDisable(GL_DEPTH_TEST);
153 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
154 glBlendEquation(GL_FUNC_ADD);
155
156 f32 t = 1.0f-vg_minf(1.0f,vg_minf(loc_t*20.0f,2.0f-loc_t*2.0f)),
157 o = 1.0f-t*t*(2.0f-t);
158
159 shader_blitcolour_use();
160 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, o*0.5f } );
161 render_fsquad2();
162
163 f32 dy = ft/0.79f,
164 scale = dy*0x1p-4f*0.5f;
165
166 m3x3_identity( mmdl );
167 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
168 v3_zero( mmdl[3] );
169
170 f32 pad = dy*0x1p-4f*0.125f;
171 f32 w = font3d_string_width( 2, gui.location );
172
173 mmdl[3][0] = fr*0.5f - w*scale*0.5f;
174 mmdl[3][1] = 0.3f*ft+pad*2.0f;
175
176 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &ortho );
177 shader_model_font_uColour( (v4f){1.2f,1.2f,1.2f,o} );
178 font3d_simple_draw( 2, gui.location, &ortho, mmdl );
179 }
180 else
181 font3d_bind( &gui.font, k_font_shader_default, 0, NULL, &ortho );
182
183 float dy = ft/0.79f,
184 scale = dy*0x1p-4f*0.75f;
185
186 m3x3_identity( mmdl );
187 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
188 v3_zero( mmdl[3] );
189
190 float pad = dy*0x1p-4f*0.125f;
191 mmdl[3][0] = pad*2.0f;
192 mmdl[3][1] = pad;
193
194 for( u32 i=0; i<gui.helper_count; i++ ){
195 struct gui_helper *helper = &gui.helpers[i];
196
197 char buf[32];
198 vg_str str;
199 vg_strnull( &str, buf, sizeof(buf) );
200 vg_input_string( &str, helper->binding, 1 );
201
202 font3d_setcolour( (v4f){1.0f,1.0f,1.0f, helper->greyed? 0.5f: 1.0f} );
203 font3d_simple_draw( 2, buf, &ortho, mmdl );
204
205 const char *make_smaller = "\x02\xaf\x03 ";
206 font3d_draw( make_smaller );
207 font3d_draw( helper->text );
208
209 float w = gui_font3d.offset[0]+1.0f;
210 mmdl[3][0] += w*scale;
211 }
212 }
213
214 static int gui_location_print_ccmd( int argc, const char *argv[] ){
215 if( argc > 0 ){
216 char new_loc[64];
217 vg_str str;
218 vg_strnull( &str, new_loc, 64 );
219 for( int i=0; i<argc; i++ ){
220 vg_strcat( &str, argv[i] );
221 vg_strcat( &str, " " );
222 }
223 if( !strcmp(gui.location,new_loc) ) return 0;
224 vg_strncpy( new_loc, gui.location, 64, k_strncpy_always_add_null );
225 gui.location_time = vg.time_real;
226 }
227 return 0;
228 }
229
230 static int gui_showtrick_ccmd( int argc, const char *argv[] ){
231 if( argc == 1 ){
232 gui_helper_clear();
233 vg_str text;
234
235 if( !strcmp( argv[0], "pump" ) ){
236 if( gui_new_helper( input_axis_list[k_sraxis_grab], &text ) )
237 vg_strcat( &text, "Pump" );
238 }
239 else if( !strcmp( argv[0], "flip" ) ){
240 if( gui_new_helper( input_joy_list[k_srjoystick_steer], &text ) )
241 vg_strcat( &text, "Flip" );
242 }
243 else if( !strcmp( argv[0], "ollie" ) ){
244 if( gui_new_helper( input_button_list[k_srbind_jump], &text ) )
245 vg_strcat( &text, "Ollie" );
246 }
247 else if( !strcmp( argv[0], "trick" ) ){
248 if( gui_new_helper( input_button_list[k_srbind_trick0], &text ) )
249 vg_strcat( &text, "Shuvit" );
250 if( gui_new_helper( input_button_list[k_srbind_trick1], &text ) )
251 vg_strcat( &text, "Kickflip" );
252 if( gui_new_helper( input_button_list[k_srbind_trick2], &text ) )
253 vg_strcat( &text, "Tre-Flip" );
254 }
255 else if( !strcmp( argv[0], "misc" ) ){
256 if( gui_new_helper( input_button_list[k_srbind_camera], &text ) )
257 vg_strcat( &text, "Camera" );
258 if( gui_new_helper( input_button_list[k_srbind_use], &text ) )
259 vg_strcat( &text, "Skate/Walk" );
260 }
261 else return 1;
262
263 v3_copy( localplayer.rb.co, gui.trick_co );
264 gui.active_positional_helper = 1;
265 return 0;
266 }
267 return 1;
268 }
269
270 static void gui_draw_icon( enum gui_icon icon, v2f co, f32 size ){
271 if( gui.icon_draw_count == vg_list_size(gui.icon_draw_buffer) )
272 return;
273
274 struct icon_call *call = &gui.icon_draw_buffer[ gui.icon_draw_count ++ ];
275
276 call->icon = icon;
277 call->location[0] = co[0] * (f32)vg.window_x;
278 call->location[1] = co[1] * (f32)vg.window_y;
279 call->location[2] = 0.0f;
280 call->location[3] = size * (f32)vg.window_x;
281
282 v4_copy( gui.cur_icon_colour, call->colour );
283 call->colour_changed = gui.colour_changed;
284 gui.colour_changed = 0;
285 }
286
287 static void gui_icon_setcolour( v4f colour ){
288 gui.colour_changed = 1;
289 v4_copy( colour, gui.cur_icon_colour );
290 }
291
292 static mdl_submesh *gui_find_icon( const char *name ){
293 mdl_mesh *mesh = mdl_find_mesh( &gui.model_icons, name );
294 if( mesh ){
295 if( mesh->submesh_count ){
296 return mdl_arritm( &gui.model_icons.submeshs, mesh->submesh_start );
297 }
298 }
299
300 return NULL;
301 }
302
303 static void gui_init(void){
304 font3d_load( &gui.font, "models/rs_font.mdl", vg_mem.rtmemory );
305 vg_console_reg_cmd( "gui_location", gui_location_print_ccmd, NULL );
306 vg_console_reg_cmd( "showtrick", gui_showtrick_ccmd, NULL );
307
308 /* load icons */
309 void *alloc = vg_mem.rtmemory;
310 mdl_open( &gui.model_icons, "models/rs_icons.mdl", alloc );
311 mdl_load_metadata_block( &gui.model_icons, alloc );
312
313 gui.icons[ k_gui_icon_tick ] = gui_find_icon( "icon_tick" );
314 gui.icons[ k_gui_icon_exclaim ] = gui_find_icon( "icon_exclaim" );
315 gui.icons[ k_gui_icon_board ] = gui_find_icon( "icon_board" );
316 gui.icons[ k_gui_icon_world ] = gui_find_icon( "icon_world" );
317 gui.icons[ k_gui_icon_rift ] = gui_find_icon( "icon_rift" );
318 gui.icons[ k_gui_icon_rift_run ] = gui_find_icon( "icon_rift_run" );
319 gui.icons[ k_gui_icon_friend ] = gui_find_icon( "icon_friend" );
320
321 vg_linear_clear( vg_mem.scratch );
322 if( !mdl_arrcount( &gui.model_icons.textures ) )
323 vg_fatal_error( "No texture in menu file" );
324 mdl_texture *tex0 = mdl_arritm( &gui.model_icons.textures, 0 );
325 void *data = vg_linear_alloc( vg_mem.scratch, tex0->file.pack_size );
326 mdl_fread_pack_file( &gui.model_icons, &tex0->file, data );
327 vg_tex2d_load_qoi_async( data, tex0->file.pack_size,
328 VG_TEX2D_LINEAR|VG_TEX2D_CLAMP,
329 &gui.icons_texture );
330
331 mdl_async_load_glmesh( &gui.model_icons, &gui.icons_mesh );
332 mdl_close( &gui.model_icons );
333 }
334
335 #endif /* GUI_H */