Input bind complete.
This commit is contained in:
@@ -9,30 +9,23 @@
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/string.h"
|
||||
|
||||
char_t INPUT_BIND_NAMES[INPUT_BIND_COUNT][16] = {
|
||||
[INPUT_BIND_UP] = "UP",
|
||||
[INPUT_BIND_DOWN] = "DOWN",
|
||||
[INPUT_BIND_LEFT] = "LEFT",
|
||||
[INPUT_BIND_RIGHT] = "RIGHT",
|
||||
[INPUT_BIND_ACCEPT] = "ACCEPT",
|
||||
[INPUT_BIND_CANCEL] = "CANCEL",
|
||||
[INPUT_BIND_CONSOLE] = "CONSOLE"
|
||||
};
|
||||
#include "util/math.h"
|
||||
|
||||
input_t INPUT;
|
||||
|
||||
void inputInit(void) {
|
||||
memoryZero(&INPUT, sizeof(input_t));
|
||||
|
||||
INPUT.binds[INPUT_BIND_UP].keyboard[0] = SDL_SCANCODE_W;
|
||||
INPUT.binds[INPUT_BIND_UP].keyboard[1] = SDL_SCANCODE_UP;
|
||||
INPUT.binds[INPUT_BIND_CONSOLE].keyboard[0] = SDL_SCANCODE_GRAVE;
|
||||
for(uint8_t i = 0; i < INPUT_BIND_COUNT; i++) {
|
||||
INPUT.binds[i].bind = (inputbind_t)i;
|
||||
INPUT.binds[i].lastValue = 0.0f;
|
||||
INPUT.binds[i].currentValue = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void inputUpdate(void) {
|
||||
#if INPUT_SDL2 == 1
|
||||
const uint8_t *keyboardState = SDL_GetKeyboardState(NULL);
|
||||
INPUT.keyboardState = SDL_GetKeyboardState(NULL);
|
||||
#endif
|
||||
|
||||
// For each input bind...
|
||||
@@ -40,21 +33,14 @@ void inputUpdate(void) {
|
||||
do {
|
||||
data->lastValue = data->currentValue;
|
||||
data->currentValue = 0.0f;
|
||||
|
||||
// Handle keyboard
|
||||
#if INPUT_KEYBOARD == 1
|
||||
for(uint32_t i = 0; i < INPUT_BIND_KEYBOARD_BUTTONS_MAX; i++) {
|
||||
if(data->keyboard[i] == 0) break;
|
||||
|
||||
#if INPUT_SDL2 == 1
|
||||
if(keyboardState[data->keyboard[i]]) {
|
||||
data->currentValue = 1.0f;
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
for(uint8_t i = 0; i < INPUT_BIND_BUTTONS_MAX; i++) {
|
||||
inputbutton_t button = data->buttons[i];
|
||||
if(button.type == INPUT_BUTTON_TYPE_NONE) continue;// TODO: Break?
|
||||
data->currentValue = mathMax(
|
||||
inputButtonGetValue(button), data->currentValue
|
||||
);
|
||||
}
|
||||
data++;
|
||||
} while(data < INPUT.binds + INPUT_BIND_COUNT);
|
||||
}
|
||||
@@ -85,12 +71,68 @@ bool_t inputReleased(const inputbind_t bind) {
|
||||
return !inputIsDown(bind) && inputWasDown(bind);
|
||||
}
|
||||
|
||||
inputbind_t inputBindGetByName(const char_t *name) {
|
||||
assertNotNull(name, "name must not be NULL");
|
||||
for(inputbind_t i = 0; i < INPUT_BIND_COUNT; i++) {
|
||||
if(stringCompareInsensitive(INPUT_BIND_NAMES[i], name) == 0) {
|
||||
return i;
|
||||
void inputBind(const inputbind_t bind, const inputbutton_t button) {
|
||||
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
|
||||
|
||||
inputbinddata_t *data = &INPUT.binds[bind];
|
||||
|
||||
for(uint8_t i = 0; i < INPUT_BIND_BUTTONS_MAX; i++) {
|
||||
if(data->buttons[i].type != INPUT_BUTTON_TYPE_NONE) continue;
|
||||
data->buttons[i] = button;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void inputUnbind(const inputbind_t bind, const inputbutton_t button) {
|
||||
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
|
||||
|
||||
inputbinddata_t *data = &INPUT.binds[bind];
|
||||
|
||||
for(uint8_t i = 0; i < INPUT_BIND_BUTTONS_MAX; i++) {
|
||||
if(data->buttons[i].type == INPUT_BUTTON_TYPE_NONE) break;
|
||||
if(memoryCompare(&data->buttons[i], &button, sizeof(inputbutton_t)) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Shift remaining buttons down
|
||||
memoryMove(
|
||||
&data->buttons[i],
|
||||
&data->buttons[i + 1],
|
||||
sizeof(inputbutton_t) * (INPUT_BIND_BUTTONS_MAX - i - 1)
|
||||
);
|
||||
data->buttons[INPUT_BIND_BUTTONS_MAX - 1].type = INPUT_BUTTON_TYPE_NONE;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void inputUnbindAll(void) {
|
||||
for(uint8_t i = 0; i < INPUT_BIND_COUNT; i++) {
|
||||
memoryZero(
|
||||
&INPUT.binds[i].buttons,
|
||||
sizeof(inputbutton_t) * INPUT_BIND_BUTTONS_MAX
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void inputUnbindButton(const inputbutton_t button) {
|
||||
for(uint8_t i = 0; i < INPUT_BIND_COUNT; i++) {
|
||||
inputbinddata_t *data = &INPUT.binds[i];
|
||||
for(uint8_t j = 0; j < INPUT_BIND_BUTTONS_MAX; j++) {
|
||||
if(data->buttons[j].type == INPUT_BUTTON_TYPE_NONE) break;
|
||||
if(memoryCompare(&data->buttons[j], &button, sizeof(inputbutton_t)) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
inputUnbind((inputbind_t)i, button);
|
||||
}
|
||||
}
|
||||
return INPUT_BIND_COUNT;
|
||||
}
|
||||
|
||||
void inputUnbindBind(const inputbind_t bind) {
|
||||
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
|
||||
|
||||
memoryZero(
|
||||
&INPUT.binds[bind].buttons,
|
||||
sizeof(inputbutton_t) * INPUT_BIND_BUTTONS_MAX
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user