mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-08 14:56:12 -04:00
Webclient: Support controller input too
This commit is contained in:
parent
f9c65faa0f
commit
1fce20a30a
@ -324,14 +324,13 @@ void Pointer_SetPosition(int idx, int x, int y) {
|
||||
cc_uint8 KeyBinds_Gamepad[KEYBIND_COUNT];
|
||||
cc_uint8 KeyBinds_Normal[KEYBIND_COUNT];
|
||||
|
||||
/* TODO find a better way than this. maybe alternative keybinds? */
|
||||
const cc_uint8 KeyBind_GamepadDefaults[KEYBIND_COUNT] = {
|
||||
CCPAD_UP, CCPAD_DOWN, CCPAD_LEFT, CCPAD_RIGHT, /* Movement */
|
||||
CCPAD_A, 0, CCPAD_START, CCPAD_Y, /* Jump, SetSpawn, OpenChat */
|
||||
CCPAD_X, 0, CCPAD_START, 0, /* Inventory, EnterChat */
|
||||
CCKEY_LSHIFT, 'X', 'Z', 'Q', 'E', /* Hacks */
|
||||
0, 0, 0, 0,
|
||||
CCKEY_F5, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, /* Hacks */
|
||||
0, 0, 0, 0, /* LAlt - F11 */
|
||||
0, 0, 0, 0, /* F5 - C */
|
||||
0, CCPAD_L, 0, CCPAD_R,
|
||||
0, 0, 0,
|
||||
0,0,0, 0,0,0,0,
|
||||
|
12
src/Menus.c
12
src/Menus.c
@ -1842,6 +1842,8 @@ static void BindsSourceScreen_ModeGamepad(void* screen, void* b) {
|
||||
static void BindsSourceScreen_ContextRecreated(void* screen) {
|
||||
struct BindsSourceScreen* s = (struct BindsSourceScreen*)screen;
|
||||
struct FontDesc font;
|
||||
Gui_MakeTitleFont(&font);
|
||||
Screen_UpdateVb(screen);
|
||||
|
||||
ButtonWidget_SetConst(&s->btns[0], "Keyboard/Mouse", &font);
|
||||
ButtonWidget_SetConst(&s->btns[1], "Gamepad/Controller", &font);
|
||||
@ -1851,8 +1853,8 @@ static void BindsSourceScreen_ContextRecreated(void* screen) {
|
||||
|
||||
static void BindsSourceScreen_Layout(void* screen) {
|
||||
struct BindsSourceScreen* s = (struct BindsSourceScreen*)screen;
|
||||
Widget_SetLocation(&s->btns[0], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -50);
|
||||
Widget_SetLocation(&s->btns[1], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 50);
|
||||
Widget_SetLocation(&s->btns[0], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, -25);
|
||||
Widget_SetLocation(&s->btns[1], ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 25);
|
||||
Menu_LayoutBack(&s->cancel);
|
||||
}
|
||||
|
||||
@ -1866,7 +1868,7 @@ static void BindsSourceScreen_Init(void* screen) {
|
||||
|
||||
ButtonWidget_Init(&s->btns[0], 300, BindsSourceScreen_ModeNormal);
|
||||
ButtonWidget_Init(&s->btns[1], 300, BindsSourceScreen_ModeGamepad);
|
||||
ButtonWidget_Init(&s->cancel, 400, Menu_SwitchHotkeys);
|
||||
ButtonWidget_Init(&s->cancel, 400, Menu_SwitchPause);
|
||||
}
|
||||
|
||||
static const struct ScreenVTABLE BindsSourceScreen_VTABLE = {
|
||||
@ -1885,10 +1887,10 @@ void BindsSourceScreen_Show(void) {
|
||||
}
|
||||
|
||||
static void SwitchBindsMain(void* s, void* w) {
|
||||
if (Input.Sources & (INPUT_SOURCE_NORMAL | INPUT_SOURCE_GAMEPAD)) {
|
||||
if (Input.Sources == (INPUT_SOURCE_NORMAL | INPUT_SOURCE_GAMEPAD)) {
|
||||
/* User needs to decide whether to configure mouse/keyboard or gamepad */
|
||||
BindsSourceScreen_Show();
|
||||
} else if (Input.Sources & INPUT_SOURCE_GAMEPAD) {
|
||||
} else if (Input.Sources == INPUT_SOURCE_GAMEPAD) {
|
||||
binds_gamepad = true;
|
||||
NormalBindingsScreen_Show();
|
||||
} else {
|
||||
|
@ -77,8 +77,8 @@ static int RunProgram(int argc, char** argv) {
|
||||
#ifdef _MSC_VER
|
||||
/* NOTE: Make sure to comment this out before pushing a commit */
|
||||
//cc_string rawArgs = String_FromConst("UnknownShadow200 fffff 127.0.0.1 25565");
|
||||
//cc_string rawArgs = String_FromConst("UnknownShadow200");
|
||||
//argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4);
|
||||
cc_string rawArgs = String_FromConst("UnknownShadow200");
|
||||
argsCount = String_UNSAFE_Split(&rawArgs, ' ', args, 4);
|
||||
#endif
|
||||
|
||||
if (argsCount == 0) {
|
||||
|
@ -513,9 +513,7 @@ void Window_Close(void) {
|
||||
}
|
||||
|
||||
extern void interop_RequestCanvasResize(void);
|
||||
void Window_ProcessEvents(double delta) {
|
||||
if (!needResize) return;
|
||||
needResize = false;
|
||||
static void ProcessPendingResize(void) {
|
||||
if (!WindowInfo.Exists) return;
|
||||
|
||||
if (Window_GetWindowState() == WINDOW_STATE_FULLSCREEN) {
|
||||
@ -527,6 +525,49 @@ void Window_ProcessEvents(double delta) {
|
||||
UpdateWindowBounds();
|
||||
}
|
||||
|
||||
#define GetGamePadButton(i) i < numButtons ? ev->digitalButton[i] : 0
|
||||
static void ProcessGamePadInput(EmscriptenGamepadEvent* ev) {
|
||||
int numButtons = ev->numButtons;
|
||||
Input.Sources |= INPUT_SOURCE_GAMEPAD;
|
||||
/* https://www.w3.org/TR/gamepad/#dfn-standard-gamepad */
|
||||
|
||||
Input_SetNonRepeatable(CCPAD_A, GetGamePadButton(0));
|
||||
Input_SetNonRepeatable(CCPAD_B, GetGamePadButton(1));
|
||||
Input_SetNonRepeatable(CCPAD_X, GetGamePadButton(2));
|
||||
Input_SetNonRepeatable(CCPAD_Y, GetGamePadButton(3));
|
||||
|
||||
Input_SetNonRepeatable(CCPAD_L, GetGamePadButton(4));
|
||||
Input_SetNonRepeatable(CCPAD_R, GetGamePadButton(5));
|
||||
|
||||
Input_SetNonRepeatable(CCPAD_SELECT, GetGamePadButton(8));
|
||||
Input_SetNonRepeatable(CCPAD_START, GetGamePadButton(9));
|
||||
|
||||
Input_SetNonRepeatable(CCPAD_UP, GetGamePadButton(12));
|
||||
Input_SetNonRepeatable(CCPAD_DOWN, GetGamePadButton(13));
|
||||
Input_SetNonRepeatable(CCPAD_LEFT, GetGamePadButton(14));
|
||||
Input_SetNonRepeatable(CCPAD_RIGHT, GetGamePadButton(15));
|
||||
}
|
||||
|
||||
void Window_ProcessEvents(double delta) {
|
||||
int i, res, count;
|
||||
Input.Sources = INPUT_SOURCE_NORMAL;
|
||||
|
||||
if (emscripten_sample_gamepad_data() == 0) {
|
||||
count = emscripten_get_num_gamepads();
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
EmscriptenGamepadEvent ev;
|
||||
res = emscripten_get_gamepad_status(i, &ev);
|
||||
if (res == 0) ProcessGamePadInput(&ev);
|
||||
}
|
||||
}
|
||||
|
||||
if (!needResize) return;
|
||||
needResize = false;
|
||||
ProcessPendingResize();
|
||||
}
|
||||
|
||||
/* Not needed because browser provides relative mouse and touch events */
|
||||
static void Cursor_GetRawPos(int* x, int* y) { *x = 0; *y = 0; }
|
||||
/* Not allowed to move cursor from javascript */
|
||||
|
Loading…
x
Reference in New Issue
Block a user