WIP on redoing input binds to support multiple bindings to same button

This commit is contained in:
UnknownShadow200 2024-06-08 13:52:34 +10:00
parent d3bb45337a
commit d36f8dab79
4 changed files with 138 additions and 89 deletions

View File

@ -871,7 +871,8 @@ static void LocalPlayer_DoRespawn(struct LocalPlayer* p) {
p->Base.OnGround = Entity_TouchesAny(&bb, LocalPlayer_IsSolidCollide); p->Base.OnGround = Entity_TouchesAny(&bb, LocalPlayer_IsSolidCollide);
} }
cc_bool LocalPlayer_HandleRespawn(struct LocalPlayer* p) { static cc_bool LocalPlayer_HandleRespawn(int key) {
struct LocalPlayer* p = Entities.CurPlayer;
if (p->Hacks.CanRespawn) { if (p->Hacks.CanRespawn) {
LocalPlayer_DoRespawn(p); LocalPlayer_DoRespawn(p);
return true; return true;
@ -882,7 +883,8 @@ cc_bool LocalPlayer_HandleRespawn(struct LocalPlayer* p) {
return false; return false;
} }
cc_bool LocalPlayer_HandleSetSpawn(struct LocalPlayer* p) { static cc_bool LocalPlayer_HandleSetSpawn(int key) {
struct LocalPlayer* p = Entities.CurPlayer;
if (p->Hacks.CanRespawn) { if (p->Hacks.CanRespawn) {
if (!p->Hacks.CanNoclip && !p->Base.OnGround) { if (!p->Hacks.CanNoclip && !p->Base.OnGround) {
@ -904,10 +906,12 @@ cc_bool LocalPlayer_HandleSetSpawn(struct LocalPlayer* p) {
p->SpawnYaw = p->Base.Yaw; p->SpawnYaw = p->Base.Yaw;
p->SpawnPitch = p->Base.Pitch; p->SpawnPitch = p->Base.Pitch;
} }
return LocalPlayer_HandleRespawn(p); return LocalPlayer_HandleRespawn(key);
} }
cc_bool LocalPlayer_HandleFly(struct LocalPlayer* p) { static cc_bool LocalPlayer_HandleFly(int key) {
struct LocalPlayer* p = Entities.CurPlayer;
if (p->Hacks.CanFly && p->Hacks.Enabled) { if (p->Hacks.CanFly && p->Hacks.Enabled) {
HacksComp_SetFlying(&p->Hacks, !p->Hacks.Flying); HacksComp_SetFlying(&p->Hacks, !p->Hacks.Flying);
return true; return true;
@ -918,7 +922,9 @@ cc_bool LocalPlayer_HandleFly(struct LocalPlayer* p) {
return false; return false;
} }
cc_bool LocalPlayer_HandleNoclip(struct LocalPlayer* p) { static cc_bool LocalPlayer_HandleNoclip(int key) {
struct LocalPlayer* p = Entities.CurPlayer;
if (p->Hacks.CanNoclip && p->Hacks.Enabled) { if (p->Hacks.CanNoclip && p->Hacks.Enabled) {
if (p->Hacks.WOMStyleHacks) return true; /* don't handle this here */ if (p->Hacks.WOMStyleHacks) return true; /* don't handle this here */
if (p->Hacks.Noclip) p->Base.Velocity.y = 0; if (p->Hacks.Noclip) p->Base.Velocity.y = 0;
@ -932,7 +938,8 @@ cc_bool LocalPlayer_HandleNoclip(struct LocalPlayer* p) {
return false; return false;
} }
cc_bool LocalPlayer_HandleJump(struct LocalPlayer* p) { static cc_bool LocalPlayer_HandleJump(int key) {
struct LocalPlayer* p = Entities.CurPlayer;
struct HacksComp* hacks = &p->Hacks; struct HacksComp* hacks = &p->Hacks;
struct PhysicsComp* physics = &p->Physics; struct PhysicsComp* physics = &p->Physics;
int maxJumps; int maxJumps;
@ -950,6 +957,14 @@ cc_bool LocalPlayer_HandleJump(struct LocalPlayer* p) {
return false; return false;
} }
static void LocalPlayer_HookBinds(void) {
Bind_OnTriggered[BIND_RESPAWN] = LocalPlayer_HandleRespawn;
Bind_OnTriggered[BIND_SET_SPAWN] = LocalPlayer_HandleSetSpawn;
Bind_OnTriggered[BIND_FLY] = LocalPlayer_HandleFly;
Bind_OnTriggered[BIND_NOCLIP] = LocalPlayer_HandleNoclip;
Bind_OnTriggered[BIND_JUMP] = LocalPlayer_HandleJump;
}
cc_bool LocalPlayer_CheckCanZoom(struct LocalPlayer* p) { cc_bool LocalPlayer_CheckCanZoom(struct LocalPlayer* p) {
if (p->Hacks.CanFly) return true; if (p->Hacks.CanFly) return true;
@ -1061,6 +1076,7 @@ static void Entities_Init(void) {
Entities.List[MAX_NET_PLAYERS + i] = &LocalPlayer_Instances[i].Base; Entities.List[MAX_NET_PLAYERS + i] = &LocalPlayer_Instances[i].Base;
} }
Entities.CurPlayer = &LocalPlayer_Instances[0]; Entities.CurPlayer = &LocalPlayer_Instances[0];
LocalPlayer_HookBinds();
} }
static void Entities_Free(void) { static void Entities_Free(void) {

View File

@ -257,10 +257,4 @@ cc_bool LocalPlayer_CheckCanZoom(struct LocalPlayer* p);
/* Moves local player back to spawn point. */ /* Moves local player back to spawn point. */
void LocalPlayers_MoveToSpawn(struct LocationUpdate* update); void LocalPlayers_MoveToSpawn(struct LocationUpdate* update);
void LocalPlayer_CalcDefaultSpawn(struct LocalPlayer* p, struct LocationUpdate* update); void LocalPlayer_CalcDefaultSpawn(struct LocalPlayer* p, struct LocationUpdate* update);
cc_bool LocalPlayer_HandleRespawn(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleSetSpawn(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleFly(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleNoclip(struct LocalPlayer* p);
cc_bool LocalPlayer_HandleJump(struct LocalPlayer* p);
#endif #endif

View File

@ -381,6 +381,7 @@ void Pointer_SetPosition(int idx, int x, int y) {
*#########################################################################################################################*/ *#########################################################################################################################*/
BindMapping PadBind_Mappings[BIND_COUNT]; BindMapping PadBind_Mappings[BIND_COUNT];
BindMapping KeyBind_Mappings[BIND_COUNT]; BindMapping KeyBind_Mappings[BIND_COUNT];
BindTriggered Bind_OnTriggered[BIND_COUNT];
const BindMapping PadBind_Defaults[BIND_COUNT] = { const BindMapping PadBind_Defaults[BIND_COUNT] = {
{ CCPAD_UP, 0 }, { CCPAD_DOWN, 0 }, /* BIND_FORWARD, BIND_BACK */ { CCPAD_UP, 0 }, { CCPAD_DOWN, 0 }, /* BIND_FORWARD, BIND_BACK */
@ -1152,72 +1153,91 @@ static void InputHandler_CheckZoomFov(void* obj) {
if (!h->Enabled || !h->CanUseThirdPerson) Camera_SetFov(Camera.DefaultFov); if (!h->Enabled || !h->CanUseThirdPerson) Camera_SetFov(Camera.DefaultFov);
} }
static cc_bool HandleBlockKey(int key) { static cc_bool BindTriggered_DeleteBlock(int key) {
if (InputBind_Claims(BIND_DELETE_BLOCK, key)) {
MouseStatePress(MOUSE_LEFT); MouseStatePress(MOUSE_LEFT);
InputHandler_DeleteBlock(); InputHandler_DeleteBlock();
} else if (InputBind_Claims(BIND_PLACE_BLOCK, key)) {
MouseStatePress(MOUSE_RIGHT);
InputHandler_PlaceBlock();
} else if (InputBind_Claims(BIND_PICK_BLOCK, key)) {
MouseStatePress(MOUSE_MIDDLE);
InputHandler_PickBlock();
} else {
return false;
}
return true; return true;
} }
static cc_bool HandleNonClassicKey(int key) { static cc_bool BindTriggered_PlaceBlock(int key) {
if (InputBind_Claims(BIND_HIDE_GUI, key)) { MouseStatePress(MOUSE_RIGHT);
InputHandler_PlaceBlock();
return true;
}
static cc_bool BindTriggered_PickBlock(int key) {
MouseStatePress(MOUSE_MIDDLE);
InputHandler_PickBlock();
return true;
}
static cc_bool BindTriggered_HideFPS(int key) {
Gui.ShowFPS = !Gui.ShowFPS;
return true;
}
static cc_bool BindTriggered_Fullscreen(int key) {
Game_ToggleFullscreen();
return true;
}
static cc_bool BindTriggered_Fog(int key) {
Game_CycleViewDistance();
return true;
}
static cc_bool BindTriggered_HideGUI(int key) {
Game_HideGui = !Game_HideGui; Game_HideGui = !Game_HideGui;
} else if (InputBind_Claims(BIND_SMOOTH_CAMERA, key)) { return true;
}
static cc_bool BindTriggered_SmoothCamera(int key) {
InputHandler_Toggle(key, &Camera.Smooth, InputHandler_Toggle(key, &Camera.Smooth,
" &eSmooth camera is &aenabled", " &eSmooth camera is &aenabled",
" &eSmooth camera is &cdisabled"); " &eSmooth camera is &cdisabled");
} else if (InputBind_Claims(BIND_AXIS_LINES, key)) { return true;
}
static cc_bool BindTriggered_AxisLines(int key) {
InputHandler_Toggle(key, &AxisLinesRenderer_Enabled, InputHandler_Toggle(key, &AxisLinesRenderer_Enabled,
" &eAxis lines (&4X&e, &2Y&e, &1Z&e) now show", " &eAxis lines (&4X&e, &2Y&e, &1Z&e) now show",
" &eAxis lines no longer show"); " &eAxis lines no longer show");
} else if (InputBind_Claims(BIND_AUTOROTATE, key)) { return true;
}
static cc_bool BindTriggered_AutoRotate(int key) {
InputHandler_Toggle(key, &AutoRotate_Enabled, InputHandler_Toggle(key, &AutoRotate_Enabled,
" &eAuto rotate is &aenabled", " &eAuto rotate is &aenabled",
" &eAuto rotate is &cdisabled"); " &eAuto rotate is &cdisabled");
} else if (InputBind_Claims(BIND_THIRD_PERSON, key)) { return true;
}
static cc_bool BindTriggered_ThirdPerson(int key) {
Camera_CycleActive(); Camera_CycleActive();
} else if (InputBind_Claims(BIND_DROP_BLOCK, key)) { return true;
}
static cc_bool BindTriggered_DropBlock(int key) {
if (Inventory_CheckChangeSelected() && Inventory_SelectedBlock != BLOCK_AIR) { if (Inventory_CheckChangeSelected() && Inventory_SelectedBlock != BLOCK_AIR) {
/* Don't assign SelectedIndex directly, because we don't want held block /* Don't assign SelectedIndex directly, because we don't want held block
switching positions if they already have air in their inventory hotbar. */ switching positions if they already have air in their inventory hotbar. */
Inventory_Set(Inventory.SelectedIndex, BLOCK_AIR); Inventory_Set(Inventory.SelectedIndex, BLOCK_AIR);
Event_RaiseVoid(&UserEvents.HeldBlockChanged); Event_RaiseVoid(&UserEvents.HeldBlockChanged);
} }
} else if (InputBind_Claims(BIND_IDOVERLAY, key)) {
TexIdsOverlay_Show();
} else if (InputBind_Claims(BIND_BREAK_LIQUIDS, key)) {
InputHandler_Toggle(key, &Game_BreakableLiquids,
" &eBreakable liquids is &aenabled",
" &eBreakable liquids is &cdisabled");
} else {
return false;
}
return true; return true;
} }
static cc_bool HandleCoreKey(int key) { static cc_bool BindTriggered_IDOverlay(int key) {
if (InputBind_Claims(BIND_HIDE_FPS, key)) { TexIdsOverlay_Show();
Gui.ShowFPS = !Gui.ShowFPS; return true;
} else if (InputBind_Claims(BIND_FULLSCREEN, key)) { }
Game_ToggleFullscreen();
} else if (InputBind_Claims(BIND_FOG, key)) { static cc_bool BindTriggered_BreakLiquids(int key) {
Game_CycleViewDistance(); InputHandler_Toggle(key, &Game_BreakableLiquids,
} else if (key == CCKEY_F5 && Game_ClassicMode) { " &eBreakable liquids is &aenabled",
int weather = Env.Weather == WEATHER_SUNNY ? WEATHER_RAINY : WEATHER_SUNNY; " &eBreakable liquids is &cdisabled");
Env_SetWeather(weather);
} else {
if (Game_ClassicMode) return false;
return HandleNonClassicKey(key);
}
return true; return true;
} }
@ -1237,21 +1257,24 @@ static void HandleHotkeyDown(int key) {
} }
} }
static cc_bool HandleLocalPlayerKey(int key) { static void HookInputBinds(void) {
struct LocalPlayer* p = Entities.CurPlayer; Bind_OnTriggered[BIND_HIDE_FPS] = BindTriggered_HideFPS;
Bind_OnTriggered[BIND_FULLSCREEN] = BindTriggered_Fullscreen;
Bind_OnTriggered[BIND_FOG] = BindTriggered_Fog;
if (InputBind_Claims(BIND_RESPAWN, key)) { Bind_OnTriggered[BIND_DELETE_BLOCK] = BindTriggered_DeleteBlock;
return LocalPlayer_HandleRespawn(p); Bind_OnTriggered[BIND_PLACE_BLOCK] = BindTriggered_PlaceBlock;
} else if (InputBind_Claims(BIND_SET_SPAWN, key)) { Bind_OnTriggered[BIND_PICK_BLOCK] = BindTriggered_PickBlock;
return LocalPlayer_HandleSetSpawn(p);
} else if (InputBind_Claims(BIND_FLY, key)) { if (Game_ClassicMode) return;
return LocalPlayer_HandleFly(p); Bind_OnTriggered[BIND_HIDE_GUI] = BindTriggered_HideGUI;
} else if (InputBind_Claims(BIND_NOCLIP, key)) { Bind_OnTriggered[BIND_SMOOTH_CAMERA] = BindTriggered_SmoothCamera;
return LocalPlayer_HandleNoclip(p); Bind_OnTriggered[BIND_AXIS_LINES] = BindTriggered_AxisLines;
} else if (InputBind_Claims(BIND_JUMP, key)) { Bind_OnTriggered[BIND_AUTOROTATE] = BindTriggered_AutoRotate;
return LocalPlayer_HandleJump(p); Bind_OnTriggered[BIND_THIRD_PERSON] = BindTriggered_ThirdPerson;
} Bind_OnTriggered[BIND_DROP_BLOCK] = BindTriggered_DropBlock;
return false; Bind_OnTriggered[BIND_IDOVERLAY] = BindTriggered_IDOverlay;
Bind_OnTriggered[BIND_BREAK_LIQUIDS] = BindTriggered_BreakLiquids;
} }
@ -1307,6 +1330,7 @@ static void OnPointerUp(void* obj, int idx) {
static void OnInputDown(void* obj, int key, cc_bool was) { static void OnInputDown(void* obj, int key, cc_bool was) {
struct Screen* s; struct Screen* s;
cc_bool triggered;
int i; int i;
if (Window_Main.SoftKeyboardFocus) return; if (Window_Main.SoftKeyboardFocus) return;
@ -1346,9 +1370,20 @@ static void OnInputDown(void* obj, int key, cc_bool was) {
/* These should not be triggered multiple times when holding down */ /* These should not be triggered multiple times when holding down */
if (was) return; if (was) return;
if (HandleBlockKey(key)) { triggered = false;
} else if (HandleCoreKey(key)) {
} else if (HandleLocalPlayerKey(key)) { for (i = 0; i < BIND_COUNT; i++)
{
if (!Bind_OnTriggered[i]) continue;
if (!InputBind_Claims(i, key)) continue;
triggered |= Bind_OnTriggered[i](key);
}
if (triggered) {
} else if (key == CCKEY_F5 && Game_ClassicMode) {
int weather = Env.Weather == WEATHER_SUNNY ? WEATHER_RAINY : WEATHER_SUNNY;
Env_SetWeather(weather);
} else { HandleHotkeyDown(key); } } else { HandleHotkeyDown(key); }
} }
@ -1392,6 +1427,7 @@ static struct LocalPlayerInput normalInput = { PlayerInputNormal };
static void OnInit(void) { static void OnInit(void) {
LocalPlayerInput_Add(&normalInput); LocalPlayerInput_Add(&normalInput);
LocalPlayerInput_Add(&gamepadInput); LocalPlayerInput_Add(&gamepadInput);
HookInputBinds();
Event_Register_(&PointerEvents.Down, NULL, OnPointerDown); Event_Register_(&PointerEvents.Down, NULL, OnPointerDown);
Event_Register_(&PointerEvents.Up, NULL, OnPointerUp); Event_Register_(&PointerEvents.Up, NULL, OnPointerUp);

View File

@ -175,6 +175,7 @@ enum InputBind_ {
}; };
typedef int InputBind; typedef int InputBind;
typedef struct BindMapping_ { cc_uint8 button1, button2; } BindMapping; typedef struct BindMapping_ { cc_uint8 button1, button2; } BindMapping;
typedef cc_bool (*BindTriggered)(int key);
#define BindMapping_Set(mapping, btn1, btn2) (mapping)->button1 = btn1; (mapping)->button2 = btn2; #define BindMapping_Set(mapping, btn1, btn2) (mapping)->button1 = btn1; (mapping)->button2 = btn2;
/* The keyboard/mouse buttons that are bound to each input binding */ /* The keyboard/mouse buttons that are bound to each input binding */
@ -185,6 +186,8 @@ extern BindMapping PadBind_Mappings[BIND_COUNT];
extern const BindMapping KeyBind_Defaults[BIND_COUNT]; extern const BindMapping KeyBind_Defaults[BIND_COUNT];
/* Default gamepad button that each input binding is bound to */ /* Default gamepad button that each input binding is bound to */
extern const BindMapping PadBind_Defaults[BIND_COUNT]; extern const BindMapping PadBind_Defaults[BIND_COUNT];
/* Callback behaviour for when the given input binding is triggered */
extern BindTriggered Bind_OnTriggered[BIND_COUNT];
/* InputBind_IsPressed is what should be used, but export KeyBind_IsPressed for backwards compatibility */ /* InputBind_IsPressed is what should be used, but export KeyBind_IsPressed for backwards compatibility */
#define InputBind_IsPressed KeyBind_IsPressed #define InputBind_IsPressed KeyBind_IsPressed