location gui
[carveJwlIkooP6JGAAIwe30JlM.git] / gui.h
1 #ifndef GUI_H
2 #define GUI_H
3
4 #include "font.h"
5
6 struct{
7 struct gui_helper{
8 const char *bindstr, *text;
9 }
10 helpers[4];
11 u32 helper_count;
12
13 char location[64];
14 f64 location_time;
15
16 f32 factive;
17 font3d font;
18 }
19 static gui;
20
21 VG_STATIC
22 void gui_draw(void)
23 {
24 camera ortho;
25
26 float fl = 0.0f,
27 fr = vg.window_x,
28 fb = 0.0f,
29 ft = vg.window_y,
30 rl = 1.0f / (fr-fl),
31 tb = 1.0f / (ft-fb);
32
33 m4x4_zero( ortho.mtx.p );
34 ortho.mtx.p[0][0] = 2.0f * rl;
35 ortho.mtx.p[1][1] = 2.0f * tb;
36 ortho.mtx.p[3][0] = (fr + fl) * -rl;
37 ortho.mtx.p[3][1] = (ft + fb) * -tb;
38 ortho.mtx.p[3][3] = 1.0f;
39 m4x3_identity( ortho.transform );
40 camera_update_view( &ortho );
41 m4x4_mul( ortho.mtx.p, ortho.mtx.v, ortho.mtx.pv ); /* HACK */
42 camera_finalize( &ortho );
43
44 gui.factive = vg_lerpf( gui.factive, gui.helper_count?1.0f:0.0f,
45 vg.time_delta*2.0f );
46
47 if( gui.factive > 0.01f ){
48 /* draw bottom bar */
49 glEnable(GL_BLEND);
50 glDisable(GL_DEPTH_TEST);
51 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
52 glBlendEquation(GL_FUNC_ADD);
53
54 shader_blitcolour_use();
55 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, gui.factive*0.8f } );
56 render_fsquad1();
57 }
58
59 f64 loc_t = (vg.time - gui.location_time) / 5.0;
60 if( (loc_t < 1.0) && (gui.location_time != 0.0) ){
61 /* yep this code is a mess, i dont care anymore */
62 glEnable(GL_BLEND);
63 glDisable(GL_DEPTH_TEST);
64 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
65 glBlendEquation(GL_FUNC_ADD);
66
67 f32 t = 1.0f-vg_minf(1.0f,vg_minf(loc_t*20.0f,2.0f-loc_t*2.0f)),
68 o = 1.0f-t*t*(2.0f-t);
69
70 shader_blitcolour_use();
71 shader_blitcolour_uColour( (v4f){ 0.0f, 0.0f, 0.0f, o*0.5f } );
72 render_fsquad2();
73
74 f32 dy = ft/0.79f,
75 scale = dy*0x1p-4f*0.5f;
76
77 m4x3f mmdl;
78 m3x3_identity( mmdl );
79 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
80 v3_zero( mmdl[3] );
81
82 f32 pad = dy*0x1p-4f*0.125f;
83 f32 w = font3d_string_width( &gui.font,2,gui.location );
84
85 mmdl[3][0] = fr*0.5f - w*scale*0.5f;
86 mmdl[3][1] = 0.3f*ft+pad*2.0f;
87
88 font3d_bind( &gui.font, &ortho );
89 shader_model_font_uColour( (v4f){1.2f,1.2f,1.2f,o} );
90 font3d_simple_draw( &gui.font, 2, gui.location, &ortho, mmdl );
91 }
92
93 font3d_bind( &gui.font, &ortho );
94
95 float dy = ft/0.79f,
96 scale = dy*0x1p-4f*0.75f;
97
98 m4x3f mmdl;
99 m3x3_identity( mmdl );
100 m3x3_scale( mmdl, (v3f){scale,scale,scale} );
101 v3_zero( mmdl[3] );
102
103 float pad = dy*0x1p-4f*0.125f;
104 mmdl[3][0] = pad*2.0f;
105 mmdl[3][1] = pad;
106
107 for( u32 i=0; i<gui.helper_count; i++ ){
108 struct gui_helper *helper = &gui.helpers[i];
109
110 shader_model_font_uColour( (v4f){1.0f,1.0f,1.0f,1.0f} );
111
112 struct font3d_render render;
113 font3d_begin( &gui.font, 2, &ortho, mmdl, &render );
114
115 render.u8pch = (u8*)helper->bindstr;
116 font3d_draw( &render );
117
118 const char *make_smaller = "\x02\xaf\x03 ";
119 render.u8pch = (const u8*)make_smaller;
120 font3d_draw( &render );
121
122 render.u8pch = (u8*)helper->text;
123 font3d_draw( &render );
124
125 float w = render.offset[0]+1.0f;
126 mmdl[3][0] += w*scale;
127 }
128
129 gui.helper_count = 0;
130 }
131
132 VG_STATIC
133 void gui_helper_action( const char *bindstr, const char *text )
134 {
135 if( gui.helper_count >= vg_list_size(gui.helpers) )
136 vg_fatal_error( "Too many helpers\n" );
137
138 struct gui_helper *helper = &gui.helpers[ gui.helper_count ++ ];
139 helper->bindstr = bindstr;
140 helper->text = text;
141 }
142
143 VG_STATIC
144 int gui_location_print_ccmd( int argc, const char *argv[] ){
145 if( argc > 0 ){
146 char new_loc[64];
147 vg_str str;
148 vg_strnull( &str, new_loc, 64 );
149 for( int i=0; i<argc; i++ ){
150 vg_strcat( &str, argv[i] );
151 vg_strcat( &str, " " );
152 }
153 if( !strcmp(gui.location,new_loc) ) return 0;
154 vg_strncpy( new_loc, gui.location, 64, k_strncpy_always_add_null );
155 gui.location_time = vg.time;
156 }
157 return 0;
158 }
159
160 VG_STATIC void gui_init(void)
161 {
162 font3d_load( &gui.font, "models/rs_font.mdl", vg_mem.rtmemory );
163 vg_console_reg_cmd( "gui_location", gui_location_print_ccmd, NULL );
164 }
165
166 #endif /* GUI_H */