respawn UI
[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 struct{
9 struct gui_helper{
10 const char *bindstr, *text;
11 }
12 helpers[4];
13 u32 helper_count;
14
15 char location[64];
16 f64 location_time;
17
18 f32 factive;
19 font3d font;
20
21 v3f trick_co;
22 enum guitrick_type{
23 k_guitrick_type_none,
24 k_guitrick_type_ollie,
25 k_guitrick_type_trick,
26 k_guitrick_type_backflip,
27 k_guitrick_type_pump,
28 k_guitrick_type_isc
29 }
30 trick_type;
31 }
32 static gui;
33
34 VG_STATIC
35 void gui_helper_action( const char *bindstr, const char *text ){
36 if( gui.helper_count >= vg_list_size(gui.helpers) ){
37 vg_error( "Too many helpers\n" );
38 return;
39 }
40
41 struct gui_helper *helper = &gui.helpers[ gui.helper_count ++ ];
42 helper->bindstr = bindstr;
43 helper->text = text;
44 }
45
46 VG_STATIC
47 void gui_draw(void)
48 {
49 if( v3_dist2(localplayer.rb.co,gui.trick_co) > 2.0f ){
50 gui.trick_type = k_guitrick_type_none;
51 }
52
53 if( gui.trick_type == k_guitrick_type_pump ){
54 gui_helper_action(axis_display_string(k_sraxis_grab),"Crouch");
55 }
56 else if( gui.trick_type == k_guitrick_type_backflip ){
57 gui_helper_action(joystick_display_string(k_srjoystick_steer,1),"Flip");
58 }
59 else if( gui.trick_type == k_guitrick_type_ollie ){
60 gui_helper_action(button_display_string(k_srbind_jump),"Ollie");
61 }
62 else if( gui.trick_type == k_guitrick_type_trick ){
63 gui_helper_action(button_display_string(k_srbind_trick0),"Shuvit");
64 gui_helper_action(button_display_string(k_srbind_trick1),"Kickflip");
65 gui_helper_action(button_display_string(k_srbind_trick2),"Tre-Flip");
66 }
67 else if( gui.trick_type == k_guitrick_type_isc ){
68 gui_helper_action(button_display_string(k_srbind_camera),"Camera");
69 gui_helper_action(button_display_string(k_srbind_use), "Skate/Walk");
70 }
71
72 camera ortho;
73
74 float fl = 0.0f,
75 fr = vg.window_x,
76 fb = 0.0f,
77 ft = vg.window_y,
78 rl = 1.0f / (fr-fl),
79 tb = 1.0f / (ft-fb);
80
81 m4x4_zero( ortho.mtx.p );
82 ortho.mtx.p[0][0] = 2.0f * rl;
83 ortho.mtx.p[1][1] = 2.0f * tb;
84 ortho.mtx.p[3][0] = (fr + fl) * -rl;
85 ortho.mtx.p[3][1] = (ft + fb) * -tb;
86 ortho.mtx.p[3][3] = 1.0f;
87 m4x3_identity( ortho.transform );
88 camera_update_view( &ortho );
89 m4x4_mul( ortho.mtx.p, ortho.mtx.v, ortho.mtx.pv ); /* HACK */
90 camera_finalize( &ortho );
91
92 gui.factive = vg_lerpf( gui.factive, gui.helper_count?1.0f:0.0f,
93 vg.time_frame_delta*2.0f );
94
95 if( gui.factive > 0.01f ){
96 /* draw bottom bar */
97 glEnable(GL_BLEND);
98 glDisable(GL_DEPTH_TEST);
99 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
100 glBlendEquation(GL_FUNC_ADD);
101
102 shader_blitcolour_use();
103 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, gui.factive*0.8f } );
104 render_fsquad1();
105 }
106
107 f64 loc_t = (vg.time_real - gui.location_time) / 5.0;
108 if( (loc_t < 1.0) && (gui.location_time != 0.0) ){
109 /* yep this code is a mess, i dont care anymore */
110 glEnable(GL_BLEND);
111 glDisable(GL_DEPTH_TEST);
112 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
113 glBlendEquation(GL_FUNC_ADD);
114
115 f32 t = 1.0f-vg_minf(1.0f,vg_minf(loc_t*20.0f,2.0f-loc_t*2.0f)),
116 o = 1.0f-t*t*(2.0f-t);
117
118 shader_blitcolour_use();
119 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, o*0.5f } );
120 render_fsquad2();
121
122 f32 dy = ft/0.79f,
123 scale = dy*0x1p-4f*0.5f;
124
125 m4x3f mmdl;
126 m3x3_identity( mmdl );
127 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
128 v3_zero( mmdl[3] );
129
130 f32 pad = dy*0x1p-4f*0.125f;
131 f32 w = font3d_string_width( &gui.font,2,gui.location );
132
133 mmdl[3][0] = fr*0.5f - w*scale*0.5f;
134 mmdl[3][1] = 0.3f*ft+pad*2.0f;
135
136 font3d_bind( &gui.font, &ortho );
137 shader_model_font_uColour( (v4f){1.2f,1.2f,1.2f,o} );
138 font3d_simple_draw( &gui.font, 2, k_font_shader_default,
139 gui.location, &ortho, mmdl );
140 }
141
142 font3d_bind( &gui.font, &ortho );
143
144 float dy = ft/0.79f,
145 scale = dy*0x1p-4f*0.75f;
146
147 m4x3f mmdl;
148 m3x3_identity( mmdl );
149 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
150 v3_zero( mmdl[3] );
151
152 float pad = dy*0x1p-4f*0.125f;
153 mmdl[3][0] = pad*2.0f;
154 mmdl[3][1] = pad;
155
156 for( u32 i=0; i<gui.helper_count; i++ ){
157 struct gui_helper *helper = &gui.helpers[i];
158
159 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
160
161 struct font3d_render render = {
162 .font = &gui.font,
163 .variant_id = 2,
164 .shader = k_font_shader_default
165 };
166 font3d_begin( helper->bindstr, &ortho, mmdl, &render );
167 font3d_draw( &render );
168
169 const char *make_smaller = "\x02\xaf\x03 ";
170 render.u8pch = (const u8*)make_smaller;
171 font3d_draw( &render );
172
173 render.u8pch = (u8*)helper->text;
174 font3d_draw( &render );
175
176 float w = render.offset[0]+1.0f;
177 mmdl[3][0] += w*scale;
178 }
179
180 gui.helper_count = 0;
181 }
182
183 VG_STATIC
184 int gui_location_print_ccmd( int argc, const char *argv[] ){
185 if( argc > 0 ){
186 char new_loc[64];
187 vg_str str;
188 vg_strnull( &str, new_loc, 64 );
189 for( int i=0; i<argc; i++ ){
190 vg_strcat( &str, argv[i] );
191 vg_strcat( &str, " " );
192 }
193 if( !strcmp(gui.location,new_loc) ) return 0;
194 vg_strncpy( new_loc, gui.location, 64, k_strncpy_always_add_null );
195 gui.location_time = vg.time_real;
196 }
197 return 0;
198 }
199
200 VG_STATIC int gui_showtrick_ccmd( int argc, const char *argv[] ){
201 if( argc == 1 ){
202 enum guitrick_type type = k_guitrick_type_none;
203 if( !strcmp( argv[0], "pump" ) ) type = k_guitrick_type_pump;
204 else if( !strcmp( argv[0], "flip" ) ) type = k_guitrick_type_backflip;
205 else if( !strcmp( argv[0], "ollie" ) ) type = k_guitrick_type_ollie;
206 else if( !strcmp( argv[0], "trick" ) ) type = k_guitrick_type_trick;
207 else if( !strcmp( argv[0], "misc" ) ) type = k_guitrick_type_isc;
208 else return 1;
209 gui.trick_type = type;
210 v3_copy( localplayer.rb.co, gui.trick_co );
211 return 0;
212 }
213 return 1;
214 }
215
216 VG_STATIC void gui_init(void)
217 {
218 font3d_load( &gui.font, "models/rs_font.mdl", vg_mem.rtmemory );
219 vg_console_reg_cmd( "gui_location", gui_location_print_ccmd, NULL );
220 vg_console_reg_cmd( "showtrick", gui_showtrick_ccmd, NULL );
221 }
222
223 #endif /* GUI_H */