Files
dusk/src/input/inputbutton.c
2025-09-08 13:30:27 -05:00

69 lines
1.7 KiB
C

/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "inputbutton.h"
#include "input.h"
#include "assert/assert.h"
#include "util/string.h"
const inputbuttondata_t INPUT_BUTTON_DATA[] = {
#if INPUT_SDL2 == 1
#if INPUT_GAMEPAD == 1
#if PSP
#else
#endif
#endif
#if INPUT_KEYBOARD == 1
{ .name = "a", { .type = INPUT_BUTTON_TYPE_KEYBOARD, .scancode = SDL_SCANCODE_A } },
{ .name = "`", { .type = INPUT_BUTTON_TYPE_KEYBOARD, .scancode = SDL_SCANCODE_GRAVE } },
#endif
#endif
};
inputbutton_t inputButtonGetByName(const char_t *name) {
assertNotNull(name, "name must not be NULL");
uint32_t len = sizeof(INPUT_BUTTON_DATA) / sizeof(inputbuttondata_t);
for(uint32_t i = 0; i < len; i++) {
if(stringCompareInsensitive(INPUT_BUTTON_DATA[i].name, name) != 0) continue;
return INPUT_BUTTON_DATA[i].button;
}
return (inputbutton_t){ .type = INPUT_BUTTON_TYPE_NONE };
}
float_t inputButtonGetValue(const inputbutton_t button) {
switch(button.type) {
#if INPUT_KEYBOARD == 1
case INPUT_BUTTON_TYPE_KEYBOARD: {
#if INPUT_SDL2 == 1
return INPUT.keyboardState[button.scancode] ? 1.0f : 0.0f;
#else
return 0.0f;
#endif
}
#endif
#if INPUT_GAMEPAD == 1
case INPUT_BUTTON_TYPE_GAMEPAD: {
#if INPUT_SDL2 == 1
if(SDL_GameControllerGetButton(
SDL_GameControllerFromInstanceID(0), button.gpButton
)) return 1.0f;
#endif
return 0.0f;
}
#endif
default: {
assertUnreachable("Unknown input button type");
return 0.0f;
}
}
}