Skip to content

Commit

Permalink
config: use strings for role inputs
Browse files Browse the repository at this point in the history
Resolves #1787.
  • Loading branch information
rr- committed Nov 2, 2024
1 parent 4a7f0ec commit cef24d9
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 213 deletions.
18 changes: 7 additions & 11 deletions src/libtrx/game/input/backends/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ static bool M_IsPressed(INPUT_LAYOUT layout, INPUT_ROLE role);
static bool M_IsRoleConflicted(INPUT_LAYOUT layout, INPUT_ROLE role);
static const char *M_GetName(INPUT_LAYOUT layout, INPUT_ROLE role);
static void M_UnassignRole(INPUT_LAYOUT layout, INPUT_ROLE role);
static bool M_AssignFromJSONObject(INPUT_LAYOUT layout, JSON_OBJECT *bind_obj);
static bool M_AssignFromJSONObject(
INPUT_LAYOUT layout, INPUT_ROLE role, JSON_OBJECT *bind_obj);
static bool M_AssignToJSONObject(
INPUT_LAYOUT layout, JSON_OBJECT *bind_obj, INPUT_ROLE role);
INPUT_LAYOUT layout, INPUT_ROLE role, JSON_OBJECT *bind_obj);
static void M_ResetLayout(INPUT_LAYOUT layout);
static bool M_ReadAndAssign(INPUT_LAYOUT layout, INPUT_ROLE role);

Expand Down Expand Up @@ -521,13 +522,9 @@ static void M_UnassignRole(const INPUT_LAYOUT layout, const INPUT_ROLE role)
}

static bool M_AssignFromJSONObject(
const INPUT_LAYOUT layout, JSON_OBJECT *const bind_obj)
const INPUT_LAYOUT layout, const INPUT_ROLE role,
JSON_OBJECT *const bind_obj)
{
const INPUT_ROLE role = JSON_ObjectGetInt(bind_obj, "role", -1);
if (role == (INPUT_ROLE)-1) {
return false;
}

int16_t button_type = M_GetAssignedButtonType(layout, role);
button_type = JSON_ObjectGetInt(bind_obj, "button_type", button_type);

Expand All @@ -546,8 +543,8 @@ static bool M_AssignFromJSONObject(
}

static bool M_AssignToJSONObject(
const INPUT_LAYOUT layout, JSON_OBJECT *const bind_obj,
const INPUT_ROLE role)
const INPUT_LAYOUT layout, const INPUT_ROLE role,
JSON_OBJECT *const bind_obj)
{
const int16_t default_button_type =
M_GetAssignedButtonType(INPUT_LAYOUT_DEFAULT, role);
Expand All @@ -564,7 +561,6 @@ static bool M_AssignToJSONObject(
return false;
}

JSON_ObjectAppendInt(bind_obj, "role", role);
JSON_ObjectAppendInt(bind_obj, "button_type", user_button_type);
JSON_ObjectAppendInt(bind_obj, "bind", user_bind);
JSON_ObjectAppendInt(bind_obj, "axis_dir", user_axis_dir);
Expand Down
18 changes: 7 additions & 11 deletions src/libtrx/game/input/backends/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ static bool M_IsPressed(INPUT_LAYOUT layout, INPUT_ROLE role);
static bool M_IsRoleConflicted(INPUT_LAYOUT layout, INPUT_ROLE role);
static const char *M_GetName(INPUT_LAYOUT layout, INPUT_ROLE role);
static void M_UnassignRole(INPUT_LAYOUT layout, INPUT_ROLE role);
static bool M_AssignFromJSONObject(INPUT_LAYOUT layout, JSON_OBJECT *bind_obj);
static bool M_AssignFromJSONObject(
INPUT_LAYOUT layout, INPUT_ROLE role, JSON_OBJECT *bind_obj);
static bool M_AssignToJSONObject(
INPUT_LAYOUT layout, JSON_OBJECT *bind_obj, INPUT_ROLE role);
INPUT_LAYOUT layout, INPUT_ROLE role, JSON_OBJECT *bind_obj);
static void M_ResetLayout(INPUT_LAYOUT layout);
static bool M_ReadAndAssign(INPUT_LAYOUT layout, INPUT_ROLE role);

Expand Down Expand Up @@ -410,13 +411,9 @@ static void M_UnassignRole(const INPUT_LAYOUT layout, const INPUT_ROLE role)
}

static bool M_AssignFromJSONObject(
const INPUT_LAYOUT layout, JSON_OBJECT *const bind_obj)
const INPUT_LAYOUT layout, const INPUT_ROLE role,
JSON_OBJECT *const bind_obj)
{
const INPUT_ROLE role = JSON_ObjectGetInt(bind_obj, "role", -1);
if (role == (INPUT_ROLE)-1) {
return false;
}

const SDL_Scancode default_scancode = M_GetAssignedScancode(layout, role);
const SDL_Scancode user_scancode =
JSON_ObjectGetInt(bind_obj, "scancode", default_scancode);
Expand All @@ -425,8 +422,8 @@ static bool M_AssignFromJSONObject(
}

static bool M_AssignToJSONObject(
const INPUT_LAYOUT layout, JSON_OBJECT *const bind_obj,
const INPUT_ROLE role)
const INPUT_LAYOUT layout, const INPUT_ROLE role,
JSON_OBJECT *const bind_obj)
{
const SDL_Scancode default_scancode =
M_GetAssignedScancode(INPUT_LAYOUT_DEFAULT, role);
Expand All @@ -436,7 +433,6 @@ static bool M_AssignToJSONObject(
return false;
}

JSON_ObjectAppendInt(bind_obj, "role", role);
JSON_ObjectAppendInt(bind_obj, "scancode", user_scancode);
return true;
}
Expand Down
80 changes: 78 additions & 2 deletions src/libtrx/game/input/common.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "game/input/common.h"

#include "enum_map.h"
#include "game/clock.h"
#include "game/game_string.h"
#include "game/input/backends/controller.h"
Expand Down Expand Up @@ -149,14 +150,89 @@ bool Input_AssignFromJSONObject(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
JSON_OBJECT *const bind_obj)
{
return M_GetBackend(backend)->assign_from_json_object(layout, bind_obj);
INPUT_ROLE role = (INPUT_ROLE)-1;

#if TR_VERSION == 1
// TR1X <=4.5, TR2X <=0.5
const int32_t role_idx = JSON_ObjectGetInt(bind_obj, "role", -1);
// clang-format off
switch (role_idx) {
case 0: role = INPUT_ROLE_UP; break;
case 1: role = INPUT_ROLE_DOWN; break;
case 2: role = INPUT_ROLE_LEFT; break;
case 3: role = INPUT_ROLE_RIGHT; break;
case 4: role = INPUT_ROLE_STEP_L; break;
case 5: role = INPUT_ROLE_STEP_R; break;
case 6: role = INPUT_ROLE_SLOW; break;
case 7: role = INPUT_ROLE_JUMP; break;
case 8: role = INPUT_ROLE_ACTION; break;
case 9: role = INPUT_ROLE_DRAW; break;
case 10: role = INPUT_ROLE_LOOK; break;
case 11: role = INPUT_ROLE_ROLL; break;
case 12: role = INPUT_ROLE_OPTION; break;
case 13: role = INPUT_ROLE_FLY_CHEAT; break;
case 14: role = INPUT_ROLE_ITEM_CHEAT; break;
case 15: role = INPUT_ROLE_LEVEL_SKIP_CHEAT; break;
case 16: role = INPUT_ROLE_TURBO_CHEAT; break;
case 17: role = INPUT_ROLE_PAUSE; break;
case 18: role = INPUT_ROLE_CAMERA_FORWARD; break;
case 19: role = INPUT_ROLE_CAMERA_BACK; break;
case 20: role = INPUT_ROLE_CAMERA_LEFT; break;
case 21: role = INPUT_ROLE_CAMERA_RIGHT; break;
case 22: role = INPUT_ROLE_CAMERA_RESET; break;
case 23: role = INPUT_ROLE_EQUIP_PISTOLS; break;
case 24: role = INPUT_ROLE_EQUIP_SHOTGUN; break;
case 25: role = INPUT_ROLE_EQUIP_MAGNUMS; break;
case 26: role = INPUT_ROLE_EQUIP_UZIS; break;
case 27: role = INPUT_ROLE_USE_SMALL_MEDI; break;
case 28: role = INPUT_ROLE_USE_BIG_MEDI; break;
case 29: role = INPUT_ROLE_SAVE; break;
case 30: role = INPUT_ROLE_LOAD; break;
case 31: role = INPUT_ROLE_FPS; break;
case 32: role = INPUT_ROLE_BILINEAR; break;
case 33: role = INPUT_ROLE_ENTER_CONSOLE; break;
case 34: role = INPUT_ROLE_CHANGE_TARGET; break;
case 35: role = INPUT_ROLE_TOGGLE_UI; break;
case 36: role = INPUT_ROLE_CAMERA_UP; break;
case 37: role = INPUT_ROLE_CAMERA_DOWN; break;
case 38: role = INPUT_ROLE_TOGGLE_PHOTO_MODE; break;
case 39: role = INPUT_ROLE_UNBIND_KEY; break;
case 40: role = INPUT_ROLE_RESET_BINDINGS; break;
case 42: role = INPUT_ROLE_PERSPECTIVE; break;
case 43: role = INPUT_ROLE_MENU_CONFIRM; break;
case 44: role = INPUT_ROLE_MENU_BACK; break;
case 45: role = INPUT_ROLE_MENU_LEFT; break;
case 46: role = INPUT_ROLE_MENU_UP; break;
case 47: role = INPUT_ROLE_MENU_DOWN; break;
case 48: role = INPUT_ROLE_MENU_RIGHT; break;
case 49: role = INPUT_ROLE_SCREENSHOT; break;
case 50: role = INPUT_ROLE_TOGGLE_FULLSCREEN; break;
}
// clang-format on
#endif

// TR1X >= 4.6, TR2X >= 0.6
if (role == (INPUT_ROLE)-1) {
role = ENUM_MAP_GET(
INPUT_ROLE, JSON_ObjectGetString(bind_obj, "role", ""),
(INPUT_ROLE)-1);
}

if (role == (INPUT_ROLE)-1) {
return false;
}

return M_GetBackend(backend)->assign_from_json_object(
layout, role, bind_obj);
}

bool Input_AssignToJSONObject(
const INPUT_BACKEND backend, const INPUT_LAYOUT layout,
JSON_OBJECT *const bind_obj, const INPUT_ROLE role)
{
return M_GetBackend(backend)->assign_to_json_object(layout, bind_obj, role);
JSON_ObjectAppendString(
bind_obj, "role", ENUM_MAP_TO_STRING(INPUT_ROLE, role));
return M_GetBackend(backend)->assign_to_json_object(layout, role, bind_obj);
}

const char *Input_GetLayoutName(const INPUT_LAYOUT layout)
Expand Down
61 changes: 61 additions & 0 deletions src/libtrx/include/libtrx/game/enum_map.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_UP, "up")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_DOWN, "down")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_LEFT, "left")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_RIGHT, "right")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_STEP_L, "step_l")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_STEP_R, "step_r")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_SLOW, "slow")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_JUMP, "jump")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_ACTION, "action")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_DRAW, "draw")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_LOOK, "look")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_ROLL, "roll")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_OPTION, "option")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_ENTER_CONSOLE, "enter_console")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_MENU_CONFIRM, "menu_confirm")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_MENU_BACK, "menu_back")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_MENU_LEFT, "menu_left")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_MENU_UP, "menu_up")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_MENU_DOWN, "menu_down")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_MENU_RIGHT, "menu_right")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_FLY_CHEAT, "fly_cheat")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_ITEM_CHEAT, "item_cheat")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_LEVEL_SKIP_CHEAT, "level_skip_cheat")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_SAVE, "save")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_LOAD, "load")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_SCREENSHOT, "screenshot")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_FULLSCREEN, "toggle_fullscreen")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_EQUIP_PISTOLS, "equip_pistols")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_EQUIP_SHOTGUN, "equip_shotgun")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_EQUIP_MAGNUMS, "equip_magnums")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_EQUIP_UZIS, "equip_uzis")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_EQUIP_HARPOON, "equip_harpoon")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_EQUIP_M16, "equip_m16")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_EQUIP_GRENADE_LAUNCHER, "equip_grenade_launcher")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_USE_FLARE, "use_flare")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_USE_SMALL_MEDI, "use_small_medi")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_USE_BIG_MEDI, "use_big_medi")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TURBO_CHEAT, "turbo_cheat")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_PAUSE, "pause")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CAMERA_FORWARD, "camera_forward")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CAMERA_BACK, "camera_back")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CAMERA_LEFT, "camera_left")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CAMERA_RIGHT, "camera_right")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CAMERA_RESET, "camera_reset")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_FPS, "fps")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_BILINEAR, "bilinear")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CHANGE_TARGET, "change_target")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_UI, "toggle_ui")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CAMERA_UP, "camera_up")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_CAMERA_DOWN, "camera_down")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_PHOTO_MODE, "toggle_photo_mode")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_UNBIND_KEY, "unbind_key")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_RESET_BINDINGS, "reset_bindings")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_PERSPECTIVE, "perspective")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_SWITCH_RESOLUTION, "switch_resolution")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_SWITCH_INTERNAL_SCREEN_SIZE, "switch_internal_screen_size")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_BILINEAR_FILTER, "toggle_bilinear_filter")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_PERSPECTIVE_FILTER, "toggle_perspective_filter")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_Z_BUFFER, "toggle_z_buffer")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_DITHER, "toggle_dither")
ENUM_MAP_DEFINE(INPUT_ROLE, INPUT_ROLE_TOGGLE_RENDERING_MODE, "toggle_rendering_mode")
5 changes: 3 additions & 2 deletions src/libtrx/include/libtrx/game/input/backends/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ typedef struct {
bool (*is_role_conflicted)(INPUT_LAYOUT layout, INPUT_ROLE role);
const char *(*get_name)(INPUT_LAYOUT layout, INPUT_ROLE role);
void (*unassign_role)(INPUT_LAYOUT layout, INPUT_ROLE role);
bool (*assign_from_json_object)(INPUT_LAYOUT layout, JSON_OBJECT *bind_obj);
bool (*assign_from_json_object)(
INPUT_LAYOUT layout, INPUT_ROLE role, JSON_OBJECT *bind_obj);
bool (*assign_to_json_object)(
INPUT_LAYOUT layout, JSON_OBJECT *bind_obj, INPUT_ROLE role);
INPUT_LAYOUT layout, INPUT_ROLE role, JSON_OBJECT *bind_obj);
void (*reset_layout)(INPUT_LAYOUT layout);
bool (*read_and_assign)(INPUT_LAYOUT layout, INPUT_ROLE role);
} INPUT_BACKEND_IMPL;
18 changes: 16 additions & 2 deletions src/libtrx/include/libtrx/game/input/common.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
#pragma once

#include "../../json.h"
#include "role.h"
#include "state.h"

#include <stdbool.h>
#include <stdint.h>

typedef enum {
#undef INPUT_ROLE_DEFINE
#define INPUT_ROLE_DEFINE(role_name, state_name) INPUT_ROLE_##role_name,
#include "roles.def"
INPUT_ROLE_NUMBER_OF,
} INPUT_ROLE;

typedef union {
uint64_t any;
struct {
#undef INPUT_ROLE_DEFINE
#define INPUT_ROLE_DEFINE(role_name, state_name) uint64_t state_name : 1;
#include "roles.def"
};
} INPUT_STATE;

typedef enum {
INPUT_BACKEND_KEYBOARD,
INPUT_BACKEND_CONTROLLER,
Expand Down
Loading

0 comments on commit cef24d9

Please sign in to comment.