better nametag
[carveJwlIkooP6JGAAIwe30JlM.git] / input.h
1 #ifndef INPUT_H
2 #define INPUT_H
3
4 #define VG_GAME
5 #include "vg/vg.h"
6 #include "vg/vg_platform.h"
7 #include "vg/vg_console.h"
8 #include "vg/vg_input.h"
9 #include "vg/vg_m.h"
10 #include "font.h"
11
12 enum sr_bind{
13 k_srbind_jump = 0,
14 k_srbind_push,
15 k_srbind_trick0,
16 k_srbind_trick1,
17 k_srbind_trick2,
18 k_srbind_sit,
19 k_srbind_use,
20 k_srbind_reset,
21 k_srbind_camera,
22 k_srbind_mleft,
23 k_srbind_mright,
24 k_srbind_mup,
25 k_srbind_mdown,
26 k_srbind_mback,
27 k_srbind_maccept,
28 k_srbind_mopen,
29 k_srbind_replay_play,
30 k_srbind_replay_freecam,
31 k_srbind_replay_resume,
32 k_srbind_world_left,
33 k_srbind_world_right,
34 k_srbind_home,
35 k_srbind_lobby,
36 k_srbind_max,
37 };
38
39 enum sr_joystick{
40 k_srjoystick_steer = 0,
41 k_srjoystick_grab,
42 k_srjoystick_look,
43 k_srjoystick_max
44 };
45
46 enum sr_axis{
47 k_sraxis_grab = 0,
48 k_sraxis_mbrowse_h,
49 k_sraxis_mbrowse_v,
50 k_sraxis_replay_h,
51 k_sraxis_mworld_h,
52
53 k_sraxis_max
54 };
55
56 struct {
57 float axis_states[ k_sraxis_max ][2];
58 v2f joystick_states[ k_srjoystick_max ][2];
59 u8 button_states[ k_srbind_max ][2];
60 u32 enabled;
61 }
62 static srinput;
63
64 static const char *controller_glyphs[ SDL_CONTROLLER_BUTTON_MAX ][2] =
65 { /* xbox/generic playstation */
66 [ SDL_CONTROLLER_BUTTON_A ] = { "\x1e\x85","\x1e\x82" },
67 [ SDL_CONTROLLER_BUTTON_B ] = { "\x1e\x86","\x1e\x81" },
68 [ SDL_CONTROLLER_BUTTON_X ] = { "\x1e\x83","\x1e\x7f" },
69 [ SDL_CONTROLLER_BUTTON_Y ] = { "\x1e\x84","\x1e\x80" },
70 [ SDL_CONTROLLER_BUTTON_LEFTSTICK ] = { "\x87", "\x87" },
71 [ SDL_CONTROLLER_BUTTON_RIGHTSTICK ] = { "\x8b", "\x8b" },
72 [ SDL_CONTROLLER_BUTTON_LEFTSHOULDER ] = { "\x91", "\x91" },
73 [ SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ]= { "\x92", "\x92" },
74 [ SDL_CONTROLLER_BUTTON_DPAD_LEFT ] = { "\x1e\x93","\x1e\x93" },
75 [ SDL_CONTROLLER_BUTTON_DPAD_UP ] = { "\x1e\x94","\x1e\x94" },
76 [ SDL_CONTROLLER_BUTTON_DPAD_RIGHT ] = { "\x1e\x95","\x1e\x95" },
77 [ SDL_CONTROLLER_BUTTON_DPAD_DOWN ] = { "\x1e\x96","\x1e\x96" },
78 [ SDL_CONTROLLER_BUTTON_GUIDE ] = { "\x91", "\x91" },
79 };
80
81 static const char *controller_glyph( u32 sdl_index ){
82 if( vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS3 ||
83 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS4 ||
84 vg_input.display_input_type == SDL_CONTROLLER_TYPE_PS5 )
85 {
86 return controller_glyphs[ sdl_index ][ 1 ];
87 }
88 else if( vg_input.display_input_type ==
89 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO ||
90 vg_input.display_input_type ==
91 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_LEFT ||
92 vg_input.display_input_type ==
93 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_PAIR ||
94 vg_input.display_input_type ==
95 SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_JOYCON_RIGHT )
96 {
97 return NULL;
98 }
99 else
100 return controller_glyphs[ sdl_index ][ 0 ];
101 }
102
103 #define KEYBOARD_GLYPH( X ) "\x1f" X " "
104
105 static const char *button_display_string( enum sr_bind button )
106 {
107 const char *controller_table[ k_srbind_max ] = {
108 [k_srbind_jump] = controller_glyph( SDL_CONTROLLER_BUTTON_A ),
109 [k_srbind_push] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
110 [k_srbind_trick0] = controller_glyph( SDL_CONTROLLER_BUTTON_A ),
111 [k_srbind_trick1] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
112 [k_srbind_trick2] = controller_glyph( SDL_CONTROLLER_BUTTON_X ),
113 [k_srbind_use] = controller_glyph( SDL_CONTROLLER_BUTTON_Y ),
114 [k_srbind_reset] = controller_glyph( SDL_CONTROLLER_BUTTON_LEFTSHOULDER ),
115 [k_srbind_camera] = controller_glyph( SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ),
116 [k_srbind_mleft] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
117 [k_srbind_mright] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
118 [k_srbind_world_left] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_LEFT ),
119 [k_srbind_world_right] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_RIGHT ),
120 [k_srbind_home] = controller_glyph( SDL_CONTROLLER_BUTTON_Y ),
121 [k_srbind_mup] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_UP ),
122 [k_srbind_mdown] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_DOWN ),
123 [k_srbind_mback] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
124 [k_srbind_maccept]= controller_glyph( SDL_CONTROLLER_BUTTON_A ),
125 [k_srbind_mopen] = controller_glyph( SDL_CONTROLLER_BUTTON_GUIDE ),
126 [k_srbind_replay_play]= controller_glyph( SDL_CONTROLLER_BUTTON_X ),
127 [k_srbind_replay_freecam]=controller_glyph(SDL_CONTROLLER_BUTTON_Y ),
128 [k_srbind_replay_resume]=controller_glyph( SDL_CONTROLLER_BUTTON_A ),
129 [k_srbind_sit] = controller_glyph( SDL_CONTROLLER_BUTTON_B ),
130 [k_srbind_lobby] = controller_glyph( SDL_CONTROLLER_BUTTON_DPAD_LEFT )
131 };
132
133 const char *keyboard_table[ k_srbind_max ] = {
134 [k_srbind_jump] = "\xa1",
135 [k_srbind_push] = KEYBOARD_GLYPH( "W" ),
136 [k_srbind_trick0] = "\x99",
137 [k_srbind_trick1] = "\x9a",
138 [k_srbind_trick2] = "\x9b",
139 [k_srbind_use] = KEYBOARD_GLYPH( "E" ),
140 [k_srbind_reset] = KEYBOARD_GLYPH( "R" ),
141 [k_srbind_camera] = KEYBOARD_GLYPH( "C" ),
142 [k_srbind_mleft] = KEYBOARD_GLYPH( "\x93" ),
143 [k_srbind_mright] = KEYBOARD_GLYPH( "\x95" ),
144 [k_srbind_world_left] = KEYBOARD_GLYPH( "\x93" ),
145 [k_srbind_world_right] = KEYBOARD_GLYPH( "\x95" ),
146 [k_srbind_home] = KEYBOARD_GLYPH( "H" ),
147 [k_srbind_mup] = KEYBOARD_GLYPH( "\x94" ),
148 [k_srbind_mdown] = KEYBOARD_GLYPH( "\x96" ),
149 [k_srbind_mback] = "\xa3",
150 [k_srbind_maccept]= "\xa2",
151 [k_srbind_mopen] = "\xa3" ,
152 [k_srbind_replay_play]= KEYBOARD_GLYPH( "G" ),
153 [k_srbind_replay_freecam] = KEYBOARD_GLYPH( "F" ),
154 [k_srbind_replay_resume]= "\xa1",
155 [k_srbind_sit] = KEYBOARD_GLYPH( "Z" ),
156 [k_srbind_lobby] = KEYBOARD_GLYPH( "" ) // FIXME: what is tab?
157 };
158
159 if( vg_input.display_input_method == k_input_method_controller )
160 return controller_table[ button ];
161 else
162 return keyboard_table[ button ];
163 }
164
165 static const char *axis_display_string( enum sr_axis axis )
166 {
167 const char *controller_table[ k_sraxis_max ] = {
168 [k_sraxis_grab] = "\x90",
169 [k_sraxis_mbrowse_h] = "\x88",
170 [k_sraxis_mbrowse_v] = "\x89",
171 [k_sraxis_replay_h] = "\x8f\x90",
172 [k_sraxis_mworld_h] = "\x93\x95"
173 };
174
175 const char *keyboard_table[ k_sraxis_max ] = {
176 [k_sraxis_grab] = "\x9e",
177 [k_sraxis_mbrowse_h] = KEYBOARD_GLYPH("\x93")KEYBOARD_GLYPH("\x95"),
178 [k_sraxis_mbrowse_v] = KEYBOARD_GLYPH("\x96")KEYBOARD_GLYPH("\x94"),
179 [k_sraxis_replay_h] = KEYBOARD_GLYPH("Q")KEYBOARD_GLYPH("E"),
180 [k_sraxis_mworld_h] = KEYBOARD_GLYPH("\x93")KEYBOARD_GLYPH("\x95"),
181 };
182
183 if( vg_input.display_input_method == k_input_method_controller )
184 return controller_table[ axis ];
185 else
186 return keyboard_table[ axis ];
187 }
188
189 static const char *joystick_display_string( enum sr_joystick joystick,
190 u32 axis )
191 {
192 const char *controller_table[ k_srjoystick_max ][3] = {
193 [k_srjoystick_steer] = {"\x88","\x89","\x8a"},
194 [k_srjoystick_grab] = {"\x8c","\x8d","\x8e"},
195 [k_srjoystick_look] = {"\x8c","\x8d","\x8e"}
196 };
197
198 const char *keyboard_table[ k_srjoystick_max ][3] = {
199 [k_srjoystick_steer] = {KEYBOARD_GLYPH("A")KEYBOARD_GLYPH("D"), /*h*/
200 KEYBOARD_GLYPH("W")KEYBOARD_GLYPH("S"), /*v*/
201
202 KEYBOARD_GLYPH("A")KEYBOARD_GLYPH("D") /*hv*/
203 KEYBOARD_GLYPH("W")KEYBOARD_GLYPH("S"), },
204 [k_srjoystick_grab] = {"\xa4","\xa4","\xa4"},
205 [k_srjoystick_look] = {"\xa4","\xa4","\xa4"}
206 };
207
208 if( vg_input.display_input_method == k_input_method_controller )
209 return controller_table[ joystick ][axis];
210 else return keyboard_table[ joystick ][axis];
211 }
212
213 static int buttons_filter_fixed(void)
214 {
215 if( !srinput.enabled ) return 1;
216 if( vg_ui.wants_mouse ) return 1;
217
218 if( vg.engine_stage == k_engine_stage_update_fixed )
219 if( vg.fixed_iterations > 0 )
220 return 1;
221
222 return 0;
223 }
224
225 /* Rising edge of button */
226 static int button_down( enum sr_bind button )
227 {
228 if( buttons_filter_fixed() ) return 0;
229
230 if( srinput.button_states[ button ][0] &&
231 !srinput.button_states[ button ][1] )
232 return 1;
233 else
234 return 0;
235 }
236
237 /* Falling edge of button */
238 static int button_up( enum sr_bind button )
239 {
240 if( buttons_filter_fixed() ) return 0;
241
242 if( !srinput.button_states[ button ][0] &&
243 srinput.button_states[ button ][1] )
244 return 1;
245 else
246 return 0;
247 }
248
249 /* State of button */
250 static int button_press( enum sr_bind button )
251 {
252 if( vg_ui.wants_mouse ) return 0;
253 return srinput.button_states[ button ][0];
254 }
255
256 static void joystick_state( enum sr_joystick joystick, v2f state )
257 {
258 if( vg_ui.wants_mouse )
259 v2_zero( state );
260 else
261 v2_copy( srinput.joystick_states[ joystick ][0], state );
262 }
263
264 static float axis_state( enum sr_axis axis ){
265 if( vg_ui.wants_mouse ) return 0.0f;
266 else return srinput.axis_states[axis][0];
267 }
268
269 static void setbtn( enum sr_bind button, u8 value )
270 {
271 srinput.button_states[button][0] |= value;
272 }
273
274 static void skaterift_preupdate_inputs(void)
275 {
276 for( u32 i=0; i<k_srbind_max; i++ ){
277 srinput.button_states[i][1] = srinput.button_states[i][0];
278 srinput.button_states[i][0] = 0;
279 }
280
281 for( u32 i=0; i<k_srjoystick_max; i++ ){
282 v2_copy( srinput.joystick_states[i][0], srinput.joystick_states[i][1] );
283 v2_zero( srinput.joystick_states[i][0] );
284 }
285
286 for( u32 i=0; i<k_sraxis_max; i++ ){
287 srinput.axis_states[i][1] = srinput.axis_states[i][0];
288 srinput.axis_states[i][0] = 0.0f;
289 }
290
291 u32 mouse = SDL_GetMouseState(NULL,NULL),
292 mouse1= (mouse & SDL_BUTTON(SDL_BUTTON_LEFT))? 1: 0,
293 mouse2= (mouse & SDL_BUTTON(SDL_BUTTON_RIGHT))? 1: 0;
294
295 /* button inputs
296 * ------------------------------------- */
297 setbtn( k_srbind_jump, vg_getkey(SDLK_SPACE) );
298 setbtn( k_srbind_push, vg_getkey(SDLK_w) );
299 setbtn( k_srbind_trick0, mouse1 );
300 setbtn( k_srbind_trick1, mouse2 );
301 setbtn( k_srbind_trick2, mouse1 & mouse2 );
302 setbtn( k_srbind_use, vg_getkey(SDLK_e) );
303 setbtn( k_srbind_reset, vg_getkey(SDLK_r) );
304 setbtn( k_srbind_camera, vg_getkey(SDLK_c) );
305 setbtn( k_srbind_mleft, vg_getkey(SDLK_LEFT) );
306 setbtn( k_srbind_mright, vg_getkey(SDLK_RIGHT) );
307 setbtn( k_srbind_world_left, vg_getkey(SDLK_LEFT) );
308 setbtn( k_srbind_world_right, vg_getkey(SDLK_RIGHT) );
309 setbtn( k_srbind_home, vg_getkey(SDLK_h) );
310 setbtn( k_srbind_mup, vg_getkey(SDLK_UP) );
311 setbtn( k_srbind_mdown, vg_getkey(SDLK_DOWN) );
312 setbtn( k_srbind_mback, vg_getkey(SDLK_ESCAPE) );
313 setbtn( k_srbind_mopen, vg_getkey(SDLK_ESCAPE) );
314 setbtn( k_srbind_maccept, vg_getkey(SDLK_e) );
315 setbtn( k_srbind_maccept, vg_getkey(SDLK_RETURN));
316 setbtn( k_srbind_maccept, vg_getkey(SDLK_RETURN2));
317 setbtn( k_srbind_replay_play, vg_getkey(SDLK_g) );
318 setbtn( k_srbind_replay_freecam, vg_getkey(SDLK_f) );
319 setbtn( k_srbind_replay_resume, vg_getkey(SDLK_SPACE) );
320 setbtn( k_srbind_sit, vg_getkey(SDLK_z) );
321 setbtn( k_srbind_lobby, vg_getkey(SDLK_TAB) );
322
323 /* axis
324 * --------------------------------------------*/
325 srinput.axis_states[ k_sraxis_grab ][0] = vg_getkey( SDLK_LSHIFT );
326
327 if( vg_getkey( SDLK_e ) )
328 srinput.axis_states[ k_sraxis_replay_h ][0] += 1.0f;
329 if( vg_getkey( SDLK_q ) )
330 srinput.axis_states[ k_sraxis_replay_h ][0] -= 1.0f;
331
332 if( vg_getkey( SDLK_d ) )
333 srinput.joystick_states[k_srjoystick_steer][0][0] += 1.0f;
334 if( vg_getkey( SDLK_a ) )
335 srinput.joystick_states[k_srjoystick_steer][0][0] -= 1.0f;
336 if( vg_getkey( SDLK_w ) )
337 srinput.joystick_states[k_srjoystick_steer][0][1] -= 1.0f;
338 if( vg_getkey( SDLK_s ) )
339 srinput.joystick_states[k_srjoystick_steer][0][1] += 1.0f;
340
341 if( vg_getkey( SDLK_DOWN ) )
342 srinput.axis_states[ k_sraxis_mbrowse_v ][0] -= 1.0f;
343 if( vg_getkey( SDLK_UP ) )
344 srinput.axis_states[ k_sraxis_mbrowse_v ][0] += 1.0f;
345
346 if( vg_getkey( SDLK_LEFT ) ){
347 srinput.axis_states[ k_sraxis_mbrowse_h ][0] -= 1.0f;
348 srinput.axis_states[ k_sraxis_mworld_h ][0] -= 1.0f;
349 }
350 if( vg_getkey( SDLK_RIGHT ) ){
351 srinput.axis_states[ k_sraxis_mbrowse_h ][0] += 1.0f;
352 srinput.axis_states[ k_sraxis_mworld_h ][0] += 1.0f;
353 }
354
355 if( vg_input.active_controller_index >= 0 ){
356 struct vg_controller *controller =
357 &vg_input.controllers[vg_input.active_controller_index];
358
359 u32 *buttons = controller->buttons;
360 setbtn( k_srbind_jump, buttons[ SDL_CONTROLLER_BUTTON_A ] );
361 setbtn( k_srbind_push, buttons[ SDL_CONTROLLER_BUTTON_B ] );
362 setbtn( k_srbind_trick0, buttons[ SDL_CONTROLLER_BUTTON_A ] );
363 setbtn( k_srbind_trick1, buttons[ SDL_CONTROLLER_BUTTON_B ] );
364 setbtn( k_srbind_trick2, buttons[ SDL_CONTROLLER_BUTTON_X ] );
365 setbtn( k_srbind_use, buttons[ SDL_CONTROLLER_BUTTON_Y ] );
366 setbtn( k_srbind_reset, buttons[ SDL_CONTROLLER_BUTTON_LEFTSHOULDER ] );
367 setbtn( k_srbind_camera, buttons[ SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ] );
368 setbtn( k_srbind_mleft, buttons[ SDL_CONTROLLER_BUTTON_DPAD_LEFT ] );
369 setbtn( k_srbind_mright, buttons[ SDL_CONTROLLER_BUTTON_DPAD_RIGHT ] );
370 setbtn( k_srbind_world_left, buttons[ SDL_CONTROLLER_BUTTON_DPAD_LEFT ] );
371 setbtn( k_srbind_world_right,buttons[ SDL_CONTROLLER_BUTTON_DPAD_RIGHT ]);
372 setbtn( k_srbind_home, buttons[ SDL_CONTROLLER_BUTTON_Y ] );
373 setbtn( k_srbind_mup, buttons[ SDL_CONTROLLER_BUTTON_DPAD_UP ] );
374 setbtn( k_srbind_mdown, buttons[ SDL_CONTROLLER_BUTTON_DPAD_DOWN ] );
375 setbtn( k_srbind_mback, buttons[ SDL_CONTROLLER_BUTTON_B ] );
376 setbtn( k_srbind_maccept,buttons[ SDL_CONTROLLER_BUTTON_A ] );
377 setbtn( k_srbind_mopen, buttons[ SDL_CONTROLLER_BUTTON_START ] );
378 setbtn( k_srbind_mback, buttons[ SDL_CONTROLLER_BUTTON_START ] );
379 setbtn( k_srbind_replay_resume, buttons[ SDL_CONTROLLER_BUTTON_A ] );
380 setbtn( k_srbind_replay_play, buttons[ SDL_CONTROLLER_BUTTON_X ] );
381 setbtn( k_srbind_replay_freecam, buttons[ SDL_CONTROLLER_BUTTON_Y ] );
382 setbtn( k_srbind_sit, buttons[ SDL_CONTROLLER_BUTTON_B ] );
383 setbtn( k_srbind_lobby, buttons[ SDL_CONTROLLER_BUTTON_DPAD_LEFT ] );
384
385 float *axis = controller->axises;
386 float *steer = srinput.joystick_states[ k_srjoystick_steer ][0],
387 *look = srinput.joystick_states[ k_srjoystick_look ][0],
388 *grab = srinput.joystick_states[ k_srjoystick_grab ][0],
389 *axisgrab = &srinput.axis_states[ k_sraxis_grab ][0],
390 *axisbrowseh = &srinput.axis_states[ k_sraxis_mbrowse_h ][0],
391 *axisbrowsev = &srinput.axis_states[ k_sraxis_mbrowse_v ][0],
392 *axisreplayh = &srinput.axis_states[ k_sraxis_replay_h ][0];
393
394 steer[0] += axis[ SDL_CONTROLLER_AXIS_LEFTX ];
395 steer[1] += axis[ SDL_CONTROLLER_AXIS_LEFTY ];
396 look[0] += axis[ SDL_CONTROLLER_AXIS_RIGHTX ];
397 look[1] += axis[ SDL_CONTROLLER_AXIS_RIGHTY ];
398 grab[0] += axis[ SDL_CONTROLLER_AXIS_RIGHTX ];
399 grab[1] += axis[ SDL_CONTROLLER_AXIS_RIGHTY ];
400 axisgrab[0] += vg_maxf( 0.0f, axis[ SDL_CONTROLLER_AXIS_TRIGGERRIGHT ] );
401 axisbrowseh[0] += axis[ SDL_CONTROLLER_AXIS_LEFTX ];
402 axisbrowsev[0] += axis[ SDL_CONTROLLER_AXIS_LEFTY ];
403 axisreplayh[0] += vg_maxf(0.0f,axis[SDL_CONTROLLER_AXIS_TRIGGERRIGHT]);
404 axisreplayh[0] -= vg_maxf(0.0f,axis[SDL_CONTROLLER_AXIS_TRIGGERLEFT]);
405
406 float lh = axis[ SDL_CONTROLLER_AXIS_LEFTX ],
407 lv = axis[ SDL_CONTROLLER_AXIS_LEFTY ],
408 sensitivity = 0.35f;
409
410 if( lh > sensitivity ) setbtn( k_srbind_mright, 1 );
411 if( lh < -sensitivity ) setbtn( k_srbind_mleft, 1 );
412 if( lv > sensitivity ) setbtn( k_srbind_mdown, 1 );
413 if( lv < -sensitivity ) setbtn( k_srbind_mup, 1 );
414 }
415
416 srinput.axis_states[ k_sraxis_grab ][0] =
417 vg_minf( 1.0f, srinput.axis_states[ k_sraxis_grab ][0] );
418 }
419
420 #endif /* INPUT_H */