Allowed binds to execute commands.

This commit is contained in:
2025-09-11 12:58:04 -05:00
parent fe9af039fc
commit 8b20f0bf31
13 changed files with 244 additions and 251 deletions

View File

@@ -16,11 +16,17 @@ input_t INPUT;
void inputInit(void) {
memoryZero(&INPUT, sizeof(input_t));
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;
for(uint8_t i = 0; i < INPUT_ACTION_COUNT; i++) {
INPUT.actions[i].action = (inputaction_t)i;
INPUT.actions[i].lastValue = 0.0f;
INPUT.actions[i].currentValue = 0.0f;
}
stringCopy(&INPUT_BUTTON_DATA[0].command[0], "echo \"test\";", CONSOLE_LINE_MAX);
INPUT_BUTTON_DATA[0].actionType = INPUT_BUTTON_ACTION_TYPE_COMMAND;
INPUT_BUTTON_DATA[1].actionType = INPUT_BUTTON_ACTION_TYPE_ACTION;
INPUT_BUTTON_DATA[1].action = INPUT_ACTION_UP;
}
void inputUpdate(void) {
@@ -28,111 +34,108 @@ void inputUpdate(void) {
INPUT.keyboardState = SDL_GetKeyboardState(NULL);
#endif
// For each input bind...
inputbinddata_t *data = INPUT.binds;
// Reset all actions
inputactiondata_t *action = &INPUT.actions[0];
do {
data->lastValue = data->currentValue;
data->currentValue = 0.0f;
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);
}
action->lastValue = action->currentValue;
action->currentValue = 0.0f;
action++;
} while(action < &INPUT.actions[INPUT_ACTION_COUNT]);
float_t inputGetCurrentValue(const inputbind_t bind) {
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
return INPUT.binds[bind].currentValue;
}
// For each button...
inputbuttondata_t *cur = &INPUT_BUTTON_DATA[0];
do {
cur->lastVal = cur->curVal;
cur->curVal = inputButtonGetValue(cur->button);
float_t inputGetLast(const inputbind_t bind) {
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
return INPUT.binds[bind].lastValue;
}
bool_t inputIsDown(const inputbind_t bind) {
return inputGetCurrentValue(bind) > 0.0f;
}
bool_t inputWasDown(const inputbind_t bind) {
return inputGetLast(bind) > 0.0f;
}
bool_t inputPressed(const inputbind_t bind) {
return inputIsDown(bind) && !inputWasDown(bind);
}
bool_t inputReleased(const inputbind_t bind) {
return !inputIsDown(bind) && inputWasDown(bind);
}
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) {
if(cur->curVal == 0.0f) {
cur++;
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;
switch(cur->actionType) {
case INPUT_BUTTON_ACTION_TYPE_NULL: {
break;
}
case INPUT_BUTTON_ACTION_TYPE_COMMAND: {
if(cur->lastVal == 0.0f && cur->command[0] != '\0') {
consoleExec(cur->command);
}
break;
}
inputUnbind((inputbind_t)i, button);
case INPUT_BUTTON_ACTION_TYPE_ACTION: {
INPUT.actions[cur->action].currentValue = mathMax(
cur->curVal, INPUT.actions[cur->action].currentValue
);
break;
}
default: {
assertUnreachable("Unknown input button action type");
break;
}
}
}
cur++;
} while(cur->name);
}
void inputUnbindBind(const inputbind_t bind) {
assertTrue(bind < INPUT_BIND_COUNT, "Input bind out of bounds");
float_t inputGetCurrentValue(const inputaction_t action) {
assertTrue(action < INPUT_ACTION_COUNT, "Input action out of bounds");
return INPUT.actions[action].currentValue;
}
memoryZero(
&INPUT.binds[bind].buttons,
sizeof(inputbutton_t) * INPUT_BIND_BUTTONS_MAX
);
float_t inputGetLast(const inputaction_t action) {
assertTrue(action < INPUT_ACTION_COUNT, "Input action out of bounds");
return INPUT.actions[action].lastValue;
}
bool_t inputIsDown(const inputaction_t action) {
return inputGetCurrentValue(action) > 0.0f;
}
bool_t inputWasDown(const inputaction_t action) {
return inputGetLast(action) > 0.0f;
}
bool_t inputPressed(const inputaction_t action) {
return inputIsDown(action) && !inputWasDown(action);
}
bool_t inputReleased(const inputaction_t action) {
return !inputIsDown(action) && inputWasDown(action);
}
void inputBind(const inputbutton_t button, const char_t *action) {
assertNotNull(action, "Input action is null");
assertStrLenMin(action, 1, "Input action is empty");
assertStrLenMax(action, CONSOLE_LINE_MAX - 1, "Input action is too long");
// Get the button data for this button.
inputbuttondata_t *data = INPUT_BUTTON_DATA;
do {
if(memoryCompare(&data->button, &button, sizeof(inputbutton_t)) == 0) {
break;
}
data++;
} while(data->name != NULL);
assertNotNull(data->name, "Input button not found");
// Try find input action first.
for(inputaction_t act = 0; act < INPUT_ACTION_COUNT; act++) {
if(stringCompareInsensitive(INPUT_ACTION_NAMES[act], action) != 0) {
continue;
}
// Action found.
data->actionType = INPUT_BUTTON_ACTION_TYPE_ACTION;
data->action = act;
return;
}
// No action found, treat as command.
data->actionType = INPUT_BUTTON_ACTION_TYPE_COMMAND;
stringCopy(data->command, action, CONSOLE_LINE_MAX);
}