From e7a0619e478ec4de0f487a225588432fdfab6ad5 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 13 Jun 2024 21:55:40 +1000 Subject: [PATCH] Move more things to use input binds directly --- src/Entity.c | 46 ++++++++++++++++++++--------------- src/Input.c | 69 +++++++++++++++++++++++++++++++--------------------- src/Input.h | 3 +++ 3 files changed, 71 insertions(+), 47 deletions(-) diff --git a/src/Entity.c b/src/Entity.c index 569a24182..6892dffa8 100644 --- a/src/Entity.c +++ b/src/Entity.c @@ -668,23 +668,6 @@ static void LocalPlayer_HandleInput(struct LocalPlayer* p, float* xMoving, float } } -static void LocalPlayer_InputSet(int key, cc_bool pressed) { - struct HacksComp* hacks = &LocalPlayer_Instances[0].Hacks; - - if (pressed && !hacks->Enabled) return; - if (InputBind_Claims(BIND_SPEED, key)) hacks->Speeding = pressed; - if (InputBind_Claims(BIND_HALF_SPEED, key)) hacks->HalfSpeeding = pressed; -} - -static void LocalPlayer_InputDown(void* obj, int key, cc_bool was) { - /* e.g. pressing Shift in chat input shouldn't turn on speeding */ - if (!Gui.InputGrab) LocalPlayer_InputSet(key, true); -} - -static void LocalPlayer_InputUp(void* obj, int key) { - LocalPlayer_InputSet(key, false); -} - static void LocalPlayer_SetLocation(struct Entity* e, struct LocationUpdate* update) { struct LocalPlayer* p = (struct LocalPlayer*)e; LocalInterpComp_SetLocation(&p->Interp, update, e); @@ -957,12 +940,39 @@ static cc_bool LocalPlayer_HandleJump(int key) { return false; } +static cc_bool LocalPlayer_TriggerHalfSpeed(int key) { + struct HacksComp* hacks = &Entities.CurPlayer->Hacks; + hacks->HalfSpeeding = hacks->Enabled; + return true; +} + +static cc_bool LocalPlayer_TriggerSpeed(int key) { + struct HacksComp* hacks = &Entities.CurPlayer->Hacks; + hacks->Speeding = hacks->Enabled; + return true; +} + +static void LocalPlayer_ReleaseHalfSpeed(int key) { + struct HacksComp* hacks = &Entities.CurPlayer->Hacks; + hacks->HalfSpeeding = false; +} + +static void LocalPlayer_ReleaseSpeed(int key) { + struct HacksComp* hacks = &Entities.CurPlayer->Hacks; + hacks->Speeding = 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; + + Bind_OnTriggered[BIND_HALF_SPEED] = LocalPlayer_TriggerHalfSpeed; + Bind_OnTriggered[BIND_SPEED] = LocalPlayer_TriggerSpeed; + Bind_OnReleased[BIND_HALF_SPEED] = LocalPlayer_ReleaseHalfSpeed; + Bind_OnReleased[BIND_SPEED] = LocalPlayer_ReleaseSpeed; } cc_bool LocalPlayer_CheckCanZoom(struct LocalPlayer* p) { @@ -1062,8 +1072,6 @@ void NetPlayer_Init(struct NetPlayer* p) { static void Entities_Init(void) { int i; Event_Register_(&GfxEvents.ContextLost, NULL, Entities_ContextLost); - Event_Register_(&InputEvents.Down, NULL, LocalPlayer_InputDown); - Event_Register_(&InputEvents.Up, NULL, LocalPlayer_InputUp); Entities.NamesMode = Options_GetEnum(OPT_NAMES_MODE, NAME_MODE_HOVERED, NameMode_Names, Array_Elems(NameMode_Names)); diff --git a/src/Input.c b/src/Input.c index f4d3bb6a8..0f3f8719d 100644 --- a/src/Input.c +++ b/src/Input.c @@ -382,6 +382,7 @@ void Pointer_SetPosition(int idx, int x, int y) { BindMapping PadBind_Mappings[BIND_COUNT]; BindMapping KeyBind_Mappings[BIND_COUNT]; BindTriggered Bind_OnTriggered[BIND_COUNT]; +BindReleased Bind_OnReleased[BIND_COUNT]; const BindMapping PadBind_Defaults[BIND_COUNT] = { { CCPAD_UP, 0 }, { CCPAD_DOWN, 0 }, /* BIND_FORWARD, BIND_BACK */ @@ -850,6 +851,9 @@ void StoredHotkeys_Add(int trigger, cc_uint8 modifiers, cc_bool moreInput, const *#########################################################################################################################*/ static void MouseStateUpdate(int button, cc_bool pressed) { struct Entity* p; + input_buttonsDown[button] = pressed; + if (!Server.SupportsPlayerClick) return; + /* defer getting the targeted entity, as it's a costly operation */ if (input_pickingId == -1) { p = &Entities.CurPlayer->Base; @@ -859,31 +863,20 @@ static void MouseStateUpdate(int button, cc_bool pressed) { input_pickingId = ENTITIES_SELF_ID; } - input_buttonsDown[button] = pressed; + CPE_SendPlayerClick(button, pressed, (EntityID)input_pickingId, &Game_SelectedPos); } -static void MouseStateChanged(int button, cc_bool pressed) { - if (!Server.SupportsPlayerClick) return; - - if (pressed) { - /* Can send multiple Pressed events */ - MouseStateUpdate(button, true); - } else { - if (!input_buttonsDown[button]) return; - MouseStateUpdate(button, false); - } -} - static void MouseStatePress(int button) { input_lastClick = Game.Time; input_pickingId = -1; - MouseStateChanged(button, true); + MouseStateUpdate(button, true); } static void MouseStateRelease(int button) { input_pickingId = -1; - MouseStateChanged(button, false); + if (!input_buttonsDown[button]) return; + MouseStateUpdate(button, false); } void InputHandler_OnScreensChanged(void) { @@ -893,9 +886,9 @@ void InputHandler_OnScreensChanged(void) { /* If input is grabbed, then the mouse isn't used for picking blocks in world anymore. */ /* So release all mouse buttons, since game stops sending PlayerClick during grabbed input */ - MouseStateChanged(MOUSE_LEFT, false); - MouseStateChanged(MOUSE_RIGHT, false); - MouseStateChanged(MOUSE_MIDDLE, false); + MouseStateRelease(MOUSE_LEFT); + MouseStateRelease(MOUSE_RIGHT); + MouseStateRelease(MOUSE_MIDDLE); } static cc_bool TouchesSolid(BlockID b) { return Blocks.Collide[b] == COLLIDE_SOLID; } @@ -1069,9 +1062,9 @@ void InputHandler_Tick(void) { /* elapsed time using DateTime_CurrentUTC_MS() instead */ input_lastClick = now; - left = InputBind_IsPressed(BIND_DELETE_BLOCK); - middle = InputBind_IsPressed(BIND_PICK_BLOCK); - right = InputBind_IsPressed(BIND_PLACE_BLOCK); + left = input_buttonsDown[MOUSE_LEFT]; + middle = input_buttonsDown[MOUSE_MIDDLE]; + right = input_buttonsDown[MOUSE_RIGHT]; #ifdef CC_BUILD_TOUCH if (Input_TouchMode) { @@ -1083,9 +1076,9 @@ void InputHandler_Tick(void) { if (Server.SupportsPlayerClick) { input_pickingId = -1; - MouseStateChanged(MOUSE_LEFT, left); - MouseStateChanged(MOUSE_RIGHT, right); - MouseStateChanged(MOUSE_MIDDLE, middle); + if (left) MouseStateUpdate(MOUSE_LEFT, true); + if (right) MouseStateUpdate(MOUSE_RIGHT, true); + if (middle) MouseStateUpdate(MOUSE_MIDDLE, true); } if (left) { @@ -1153,6 +1146,7 @@ static void InputHandler_CheckZoomFov(void* obj) { if (!h->Enabled || !h->CanUseThirdPerson) Camera_SetFov(Camera.DefaultFov); } + static cc_bool BindTriggered_DeleteBlock(int key) { MouseStatePress(MOUSE_LEFT); InputHandler_DeleteBlock(); @@ -1171,6 +1165,18 @@ static cc_bool BindTriggered_PickBlock(int key) { return true; } +static void BindReleased_DeleteBlock(int key) { + MouseStateRelease(MOUSE_LEFT); +} + +static void BindReleased_PlaceBlock(int key) { + MouseStateRelease(MOUSE_RIGHT); +} + +static void BindReleased_PickBlock(int key) { + MouseStateRelease(MOUSE_MIDDLE); +} + static cc_bool BindTriggered_HideFPS(int key) { Gui.ShowFPS = !Gui.ShowFPS; @@ -1266,6 +1272,10 @@ static void HookInputBinds(void) { Bind_OnTriggered[BIND_PLACE_BLOCK] = BindTriggered_PlaceBlock; Bind_OnTriggered[BIND_PICK_BLOCK] = BindTriggered_PickBlock; + Bind_OnReleased[BIND_DELETE_BLOCK] = BindReleased_DeleteBlock; + Bind_OnReleased[BIND_PLACE_BLOCK] = BindReleased_PlaceBlock; + Bind_OnReleased[BIND_PICK_BLOCK] = BindReleased_PickBlock; + if (Game_ClassicMode) return; Bind_OnTriggered[BIND_HIDE_GUI] = BindTriggered_HideGUI; Bind_OnTriggered[BIND_SMOOTH_CAMERA] = BindTriggered_SmoothCamera; @@ -1408,10 +1418,13 @@ static void OnInputUp(void* obj, int key) { s->VTABLE->OnInputUp(s, key); } - if (Gui.InputGrab) return; - if (InputBind_Claims(BIND_DELETE_BLOCK, key)) MouseStateRelease(MOUSE_LEFT); - if (InputBind_Claims(BIND_PLACE_BLOCK, key)) MouseStateRelease(MOUSE_RIGHT); - if (InputBind_Claims(BIND_PICK_BLOCK, key)) MouseStateRelease(MOUSE_MIDDLE); + for (i = 0; i < BIND_COUNT; i++) + { + if (!Bind_OnReleased[i]) continue; + if (!InputBind_Claims(i, key)) continue; + + Bind_OnReleased[i](key); + } } static void OnFocusChanged(void* obj) { if (!Window_Main.Focused) Input_Clear(); } diff --git a/src/Input.h b/src/Input.h index df6d8f7af..84e43b971 100644 --- a/src/Input.h +++ b/src/Input.h @@ -176,6 +176,7 @@ enum InputBind_ { typedef int InputBind; typedef struct BindMapping_ { cc_uint8 button1, button2; } BindMapping; typedef cc_bool (*BindTriggered)(int key); +typedef void (*BindReleased)(int key); #define BindMapping_Set(mapping, btn1, btn2) (mapping)->button1 = btn1; (mapping)->button2 = btn2; /* The keyboard/mouse buttons that are bound to each input binding */ @@ -188,6 +189,8 @@ extern const BindMapping KeyBind_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]; +/* Callback behaviour for when the given input binding is released */ +extern BindReleased Bind_OnReleased[BIND_COUNT]; /* InputBind_IsPressed is what should be used, but export KeyBind_IsPressed for backwards compatibility */ #define InputBind_IsPressed KeyBind_IsPressed