diff --git a/config.json b/config.json index af279c6..2d07db0 100644 --- a/config.json +++ b/config.json @@ -4,10 +4,10 @@ "worlds": "saves" }, "input": { - "player_forward": [0, 200], - "player_backward": [1, 201], - "player_left": [2, 202], - "player_right": [3, 203], + "player_forward": [0, 200, 910], + "player_backward": [1, 201, 911], + "player_left": [2, 202, 912], + "player_right": [3, 203, 913], "player_jump": [5, 205], "player_sneak": [204], "item_action_left": [100, 210], @@ -16,10 +16,10 @@ "scroll_right": [8, 209, 212], "inventory": [6, 206], "open_menu": [10, 214], - "gui_up": [0, 200], - "gui_down": [1, 201], - "gui_left": [2, 202], - "gui_right": [3, 203], + "gui_up": [0, 200, 900, 910, 920], + "gui_down": [1, 201, 901, 911, 921], + "gui_left": [2, 202, 902, 912, 922], + "gui_right": [3, 203, 903, 913, 923], "gui_click": [4, 204], "gui_click_alt": [5, 205], "screenshot": [7], diff --git a/controls.md b/controls.md index c9a6131..f95be27 100644 --- a/controls.md +++ b/controls.md @@ -18,6 +18,10 @@ | --- | :-: | | Z | `100` | | C | `101` | +| joystick up | `900` | +| joystick down | `901` | +| joystick left | `902` | +| joystick right | `903` | | Classic controller | config id | | --- | :-: | @@ -36,6 +40,14 @@ | plus | `212` | | minus | `213` | | home | `214` | +| L joystick up | `910` | +| L joystick down | `911` | +| L joystick left | `912` | +| L joystick right | `913` | +| R joystick up | `920` | +| R joystick down | `921` | +| R joystick left | `922` | +| R joystick right | `923` | | Guitar Hero 3 | config id | | --- | :-: | diff --git a/source/platform/input.c b/source/platform/input.c index e8fb357..1131f8c 100644 --- a/source/platform/input.c +++ b/source/platform/input.c @@ -122,17 +122,81 @@ void input_native_joystick(float dt, float* dx, float* dy) { #include +static struct { + float dx, dy; + float magnitude; + bool available; +} joystick_input[3]; + +static bool js_emulated_btns_prev[3][4]; +static bool js_emulated_btns_held[3][4]; + void input_init() { WPAD_Init(); - WPAD_SetDataFormat(0, WPAD_FMT_BTNS_ACC_IR); - WPAD_SetVRes(WPAD_CHAN_ALL, gfx_width(), gfx_height()); + WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR); + WPAD_SetVRes(WPAD_CHAN_0, gfx_width(), gfx_height()); + + for(int k = 0; k < 4; k++) { + for(int j = 0; j < 3; j++) + js_emulated_btns_prev[j][k] = js_emulated_btns_held[j][k] = false; + } } void input_poll() { WPAD_ScanPads(); + + expansion_t e; + WPAD_Expansion(WPAD_CHAN_0, &e); + + if(e.type == WPAD_EXP_NUNCHUK) { + joystick_input[0].dx = sin(glm_rad(e.nunchuk.js.ang)); + joystick_input[0].dy = cos(glm_rad(e.nunchuk.js.ang)); + joystick_input[0].magnitude = e.nunchuk.js.mag; + joystick_input[0].available = true; + } else { + joystick_input[0].available = false; + } + + if(e.type == WPAD_EXP_CLASSIC) { + joystick_input[1].dx = sin(glm_rad(e.classic.ljs.ang)); + joystick_input[1].dy = cos(glm_rad(e.classic.ljs.ang)); + joystick_input[1].magnitude = e.classic.ljs.mag; + joystick_input[1].available = true; + + joystick_input[2].dx = sin(glm_rad(e.classic.rjs.ang)); + joystick_input[2].dy = cos(glm_rad(e.classic.rjs.ang)); + joystick_input[2].magnitude = e.classic.rjs.mag; + joystick_input[2].available = true; + } else { + joystick_input[1].available = joystick_input[2].available = false; + } + + for(int j = 0; j < 3; j++) { + for(int k = 0; k < 4; k++) { + js_emulated_btns_prev[j][k] = js_emulated_btns_held[j][k]; + js_emulated_btns_held[j][k] = false; + } + + if(joystick_input[j].available) { + float x = joystick_input[j].dx * joystick_input[j].magnitude; + float y = joystick_input[j].dy * joystick_input[j].magnitude; + + if(x > 0.2F) { + js_emulated_btns_held[j][3] = true; + } else if(x < -0.2F) { + js_emulated_btns_held[j][2] = true; + } + + if(y > 0.2F) { + js_emulated_btns_held[j][0] = true; + } else if(y < -0.2F) { + js_emulated_btns_held[j][1] = true; + } + } + } } -uint32_t input_wpad_translate(int key) { +static uint32_t input_wpad_translate(int key) { switch(key) { case 0: return WPAD_BUTTON_UP; case 1: return WPAD_BUTTON_DOWN; @@ -196,6 +260,20 @@ uint32_t input_wpad_translate(int key) { void input_native_key_status(int key, bool* pressed, bool* released, bool* held) { + if(key >= 900 && key < 924) { + int js = (key - 900) / 10; + int offset = (key - 900) % 10; + if(offset < 4) { + *held = js_emulated_btns_held[js][offset] + && js_emulated_btns_prev[js][offset]; + *pressed = js_emulated_btns_held[js][offset] + && !js_emulated_btns_prev[js][offset]; + *released = !js_emulated_btns_held[js][offset] + && js_emulated_btns_prev[js][offset]; + return; + } + } + *pressed = WPAD_ButtonsDown(WPAD_CHAN_0) & input_wpad_translate(key); *released = WPAD_ButtonsUp(WPAD_CHAN_0) & input_wpad_translate(key); *held = !(*pressed) && !(*released) @@ -204,6 +282,27 @@ void input_native_key_status(int key, bool* pressed, bool* released, bool input_native_key_symbol(int key, int* symbol, int* symbol_help, enum input_category* category, int* priority) { + if(key >= 900 && key < 904) { + *symbol = *symbol_help = 17; + *category = INPUT_CAT_NUNCHUK; + *priority = 1; + return true; + } + + if(key >= 910 && key < 914) { + *symbol = *symbol_help = 18; + *category = INPUT_CAT_CLASSIC_CONTROLLER; + *priority = 1; + return true; + } + + if(key >= 920 && key < 924) { + *symbol = *symbol_help = 19; + *category = INPUT_CAT_CLASSIC_CONTROLLER; + *priority = 1; + return true; + } + if(key < 0 || key > 308) return false; @@ -264,15 +363,13 @@ bool input_pointer(float* x, float* y, float* angle) { } void input_native_joystick(float dt, float* dx, float* dy) { - expansion_t e; - WPAD_Expansion(WPAD_CHAN_0, &e); - - if(e.type == WPAD_EXP_NUNCHUK && e.nunchuk.js.mag > 0.1F) { - *dx = sin(glm_rad(e.nunchuk.js.ang)) * e.nunchuk.js.mag * dt; - *dy = cos(glm_rad(e.nunchuk.js.ang)) * e.nunchuk.js.mag * dt; - } else if(e.type == WPAD_EXP_CLASSIC && e.classic.rjs.mag > 0.1F) { - *dx = sin(glm_rad(e.classic.rjs.ang)) * e.classic.rjs.mag * dt; - *dy = cos(glm_rad(e.classic.rjs.ang)) * e.classic.rjs.mag * dt; + if(joystick_input[0].available && joystick_input[0].magnitude > 0.1F) { + *dx = joystick_input[0].dx * joystick_input[0].magnitude * dt; + *dy = joystick_input[0].dy * joystick_input[0].magnitude * dt; + } else if(joystick_input[2].available + && joystick_input[2].magnitude > 0.1F) { + *dx = joystick_input[2].dx * joystick_input[2].magnitude * dt; + *dy = joystick_input[2].dy * joystick_input[2].magnitude * dt; } else { *dx = 0.0F; *dy = 0.0F;