Split up Input into Input and InputHandler

This commit is contained in:
UnknownShadow200 2024-07-09 20:39:58 +10:00
parent cf63911261
commit 1f12b596f6
12 changed files with 1269 additions and 1182 deletions

View File

@ -7,6 +7,7 @@
#include "Gui.h"
#include "Entity.h"
#include "Input.h"
#include "InputHandler.h"
#include "Event.h"
#include "Options.h"
#include "Picking.h"

View File

@ -13,6 +13,7 @@
#include "Chat.h"
#include "Model.h"
#include "Input.h"
#include "InputHandler.h"
#include "Gui.h"
#include "Stream.h"
#include "Bitmap.h"

View File

@ -22,6 +22,7 @@
#include "Http.h"
#include "Inventory.h"
#include "Input.h"
#include "InputHandler.h"
#include "Server.h"
#include "TexturePack.h"
#include "Screens.h"
@ -423,6 +424,7 @@ static void Game_Load(void) {
Game_AddComponent(&World_Component);
Game_AddComponent(&Textures_Component);
Game_AddComponent(&Input_Component);
Game_AddComponent(&InputHandler_Component);
Game_AddComponent(&Camera_Component);
Game_AddComponent(&Gfx_Component);
Game_AddComponent(&Blocks_Component);

View File

@ -17,6 +17,7 @@
#include "Funcs.h"
#include "Server.h"
#include "TexturePack.h"
#include "InputHandler.h"
struct _GuiData Gui;
struct Screen* Gui_Screens[GUI_MAX_SCREENS];

File diff suppressed because it is too large Load Diff

View File

@ -2,11 +2,10 @@
#define CC_INPUT_H
#include "Core.h"
/*
Manages input state, raising input related events, and base input handling
Manages input state and raising input related events
Copyright 2014-2023 ClassiCube | Licensed under BSD-3
*/
struct IGameComponent;
struct StringsBuffer;
extern struct IGameComponent Input_Component;
enum InputButtons {
@ -67,6 +66,7 @@ enum InputButtons {
};
#define Input_IsPadButton(btn) ((btn) >= CCPAD_1 && (btn) < INPUT_COUNT)
extern const char* const Input_StorageNames[INPUT_COUNT];
extern const char* Input_DisplayNames[INPUT_COUNT];
extern struct _InputState {
@ -78,9 +78,6 @@ extern struct _InputState {
cc_uint8 Sources;
} Input;
#define INPUT_SOURCE_NORMAL (1 << 0)
#define INPUT_SOURCE_GAMEPAD (1 << 1)
/* Sets Input_Pressed[key] to true and raises InputEvents.Down */
void Input_SetPressed(int key);
/* Sets Input_Pressed[key] to false and raises InputEvents.Up */
@ -92,15 +89,6 @@ void Input_SetNonRepeatable(int key, int pressed);
void Input_Clear(void);
#define INPUT_DEVICE_NORMAL 0x01
#define INPUT_DEVICE_TOUCH 0x02
#define INPUT_DEVICE_GAMEPAD 0x04
#define INPUT_DEVICE_GAMEPAD1 0x10
#define INPUT_DEVICE_GAMEPAD2 0x20
#define INPUT_DEVICE_GAMEPAD3 0x30
#define INPUT_DEVICE_GAMEPAD4 0x40
struct InputDevice {
int type, index; /* Device type and index (e.g. controller port) */
int upButton, downButton, leftButton, rightButton;
@ -112,6 +100,12 @@ struct InputDevice {
int tabLauncher;
};
#define INPUT_SOURCE_NORMAL (1 << 0)
#define INPUT_SOURCE_GAMEPAD (1 << 1)
#define INPUT_DEVICE_NORMAL 0x01
#define INPUT_DEVICE_TOUCH 0x02
#define INPUT_DEVICE_GAMEPAD 0x04
extern struct InputDevice NormDevice;
extern struct InputDevice PadDevice;
extern struct InputDevice TouchDevice;
@ -174,61 +168,6 @@ void Mouse_ScrollHWheel(float delta);
void Pointer_SetPosition(int idx, int x, int y);
/* Enumeration of all input bindings. */
enum InputBind_ {
BIND_FORWARD, BIND_BACK, BIND_LEFT, BIND_RIGHT,
BIND_JUMP, BIND_RESPAWN, BIND_SET_SPAWN, BIND_CHAT,
BIND_INVENTORY, BIND_FOG, BIND_SEND_CHAT, BIND_TABLIST,
BIND_SPEED, BIND_NOCLIP, BIND_FLY, BIND_FLY_UP, BIND_FLY_DOWN,
BIND_EXT_INPUT, BIND_HIDE_FPS, BIND_SCREENSHOT, BIND_FULLSCREEN,
BIND_THIRD_PERSON, BIND_HIDE_GUI, BIND_AXIS_LINES, BIND_ZOOM_SCROLL,
BIND_HALF_SPEED, BIND_DELETE_BLOCK, BIND_PICK_BLOCK, BIND_PLACE_BLOCK,
BIND_AUTOROTATE, BIND_HOTBAR_SWITCH, BIND_SMOOTH_CAMERA,
BIND_DROP_BLOCK, BIND_IDOVERLAY, BIND_BREAK_LIQUIDS,
BIND_LOOK_UP, BIND_LOOK_DOWN, BIND_LOOK_RIGHT, BIND_LOOK_LEFT,
BIND_HOTBAR_1, BIND_HOTBAR_2, BIND_HOTBAR_3,
BIND_HOTBAR_4, BIND_HOTBAR_5, BIND_HOTBAR_6,
BIND_HOTBAR_7, BIND_HOTBAR_8, BIND_HOTBAR_9,
BIND_HOTBAR_LEFT, BIND_HOTBAR_RIGHT,
BIND_COUNT
};
typedef int InputBind;
typedef struct BindMapping_ { cc_uint8 button1, button2; } BindMapping;
typedef cc_bool (*BindTriggered)(int key, struct InputDevice* device);
typedef void (*BindReleased)(int key, struct InputDevice* device);
#define BindMapping_Set(mapping, btn1, btn2) (mapping)->button1 = btn1; (mapping)->button2 = btn2;
/* The keyboard/mouse buttons that are bound to each input binding */
extern BindMapping KeyBind_Mappings[BIND_COUNT];
/* The gamepad buttons that are bound to each input binding */
extern BindMapping PadBind_Mappings[BIND_COUNT];
/* Default keyboard/mouse button that each input binding is bound to */
extern const BindMapping KeyBind_Defaults[BIND_COUNT];
/* Default gamepad button that each input binding is bound to */
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];
/* Whether the given input binding is activated by one or more devices */
extern cc_uint8 Bind_IsTriggered[BIND_COUNT];
/* Whether the given binding should be triggered in response to given input button being pressed */
cc_bool InputBind_Claims(InputBind binding, int btn, struct InputDevice* device);
/* Gets whether the given input binding is currently being triggered */
/* DEPRECATED, and should not be used anymore */
CC_API cc_bool KeyBind_IsPressed(InputBind binding);
/* Sets the key/mouse button that the given input binding is bound to */
void KeyBind_Set(InputBind binding, int btn);
/* Sets the gamepad button that the given input binding is bound to */
void PadBind_Set(InputBind binding, int btn);
/* Resets the key/mouse button that the given input binding is bound to */
void KeyBind_Reset(InputBind binding);
/* Resets the gamepad button that the given input binding is bound to */
void PadBind_Reset(InputBind binding);
/* Gamepad axes. Default behaviour is: */
/* - left axis: player movement */
/* - right axis: camera movement */
@ -243,46 +182,16 @@ void Gamepad_SetButton(int port, int btn, int pressed);
/* Sets value of the given axis */
void Gamepad_SetAxis(int port, int axis, float x, float y, float delta);
void Gamepad_Tick(float delta);
#define INPUT_MAX_GAMEPADS 4
#define GAMEPAD_BEG_BTN CCPAD_1
#define GAMEPAD_BTN_COUNT (INPUT_COUNT - GAMEPAD_BEG_BTN)
/* whether to leave text input open for user to enter further input */
#define HOTKEY_FLAG_STAYS_OPEN 0x01
/* Whether the hotkey was auto defined (e.g. by server) */
#define HOTKEY_FLAG_AUTO_DEFINED 0x02
extern const cc_uint8 Hotkeys_LWJGL[256];
struct HotkeyData {
int textIndex; /* contents to copy directly into the input bar */
cc_uint8 trigger; /* Member of Key enumeration */
cc_uint8 mods; /* HotkeyModifiers bitflags */
cc_uint8 flags; /* HOTKEY_FLAG flags */
struct GamepadState {
float axisX[2], axisY[2];
cc_bool pressed[GAMEPAD_BTN_COUNT];
float holdtime[GAMEPAD_BTN_COUNT];
};
extern struct GamepadState Gamepad_States[INPUT_MAX_GAMEPADS];
#define HOTKEYS_MAX_COUNT 256
extern struct HotkeyData HotkeysList[HOTKEYS_MAX_COUNT];
extern struct StringsBuffer HotkeysText;
enum HotkeyModifiers {
HOTKEY_MOD_CTRL = 1, HOTKEY_MOD_SHIFT = 2, HOTKEY_MOD_ALT = 4
};
/* Adds or updates a new hotkey. */
void Hotkeys_Add(int trigger, cc_uint8 modifiers, const cc_string* text, cc_uint8 flags);
/* Removes the given hotkey. */
cc_bool Hotkeys_Remove(int trigger, cc_uint8 modifiers);
/* Returns the first hotkey which is bound to the given key and has its modifiers pressed. */
/* NOTE: The hotkeys list is sorted, so hotkeys with most modifiers are checked first. */
int Hotkeys_FindPartial(int key);
/* Loads the given hotkey from options. (if it exists) */
void StoredHotkeys_Load(int trigger, cc_uint8 modifiers);
/* Removes the given hotkey from options. */
void StoredHotkeys_Remove(int trigger, cc_uint8 modifiers);
/* Adds the given hotkey from options. */
void StoredHotkeys_Add(int trigger, cc_uint8 modifiers, cc_bool moreInput, const cc_string* text);
cc_bool InputHandler_SetFOV(int fov);
cc_bool Input_HandleMouseWheel(float delta);
void InputHandler_Tick(void);
void InputHandler_OnScreensChanged(void);
#endif

1128
src/InputHandler.c Normal file

File diff suppressed because it is too large Load Diff

108
src/InputHandler.h Normal file
View File

@ -0,0 +1,108 @@
#ifndef CC_INPUTHANDLER_H
#define CC_INPUTHANDLER_H
#include "Core.h"
/*
Manages base game input handling
Copyright 2014-2023 ClassiCube | Licensed under BSD-3
*/
struct IGameComponent;
struct StringsBuffer;
struct InputDevice;
extern struct IGameComponent InputHandler_Component;
/* Enumeration of all input bindings. */
enum InputBind_ {
BIND_FORWARD, BIND_BACK, BIND_LEFT, BIND_RIGHT,
BIND_JUMP, BIND_RESPAWN, BIND_SET_SPAWN, BIND_CHAT,
BIND_INVENTORY, BIND_FOG, BIND_SEND_CHAT, BIND_TABLIST,
BIND_SPEED, BIND_NOCLIP, BIND_FLY, BIND_FLY_UP, BIND_FLY_DOWN,
BIND_EXT_INPUT, BIND_HIDE_FPS, BIND_SCREENSHOT, BIND_FULLSCREEN,
BIND_THIRD_PERSON, BIND_HIDE_GUI, BIND_AXIS_LINES, BIND_ZOOM_SCROLL,
BIND_HALF_SPEED, BIND_DELETE_BLOCK, BIND_PICK_BLOCK, BIND_PLACE_BLOCK,
BIND_AUTOROTATE, BIND_HOTBAR_SWITCH, BIND_SMOOTH_CAMERA,
BIND_DROP_BLOCK, BIND_IDOVERLAY, BIND_BREAK_LIQUIDS,
BIND_LOOK_UP, BIND_LOOK_DOWN, BIND_LOOK_RIGHT, BIND_LOOK_LEFT,
BIND_HOTBAR_1, BIND_HOTBAR_2, BIND_HOTBAR_3,
BIND_HOTBAR_4, BIND_HOTBAR_5, BIND_HOTBAR_6,
BIND_HOTBAR_7, BIND_HOTBAR_8, BIND_HOTBAR_9,
BIND_HOTBAR_LEFT, BIND_HOTBAR_RIGHT,
BIND_COUNT
};
typedef int InputBind;
typedef struct BindMapping_ { cc_uint8 button1, button2; } BindMapping;
typedef cc_bool (*BindTriggered)(int key, struct InputDevice* device);
typedef void (*BindReleased)(int key, struct InputDevice* device);
#define BindMapping_Set(mapping, btn1, btn2) (mapping)->button1 = btn1; (mapping)->button2 = btn2;
/* The keyboard/mouse buttons that are bound to each input binding */
extern BindMapping KeyBind_Mappings[BIND_COUNT];
/* The gamepad buttons that are bound to each input binding */
extern BindMapping PadBind_Mappings[BIND_COUNT];
/* Default keyboard/mouse button that each input binding is bound to */
extern const BindMapping KeyBind_Defaults[BIND_COUNT];
/* Default gamepad button that each input binding is bound to */
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];
/* Whether the given input binding is activated by one or more devices */
extern cc_uint8 Bind_IsTriggered[BIND_COUNT];
/* Whether the given binding should be triggered in response to given input button being pressed */
cc_bool InputBind_Claims(InputBind binding, int btn, struct InputDevice* device);
/* Gets whether the given input binding is currently being triggered */
/* DEPRECATED, and should not be used anymore */
CC_API cc_bool KeyBind_IsPressed(InputBind binding);
/* Sets the key/mouse button that the given input binding is bound to */
void KeyBind_Set(InputBind binding, int btn);
/* Sets the gamepad button that the given input binding is bound to */
void PadBind_Set(InputBind binding, int btn);
/* Resets the key/mouse button that the given input binding is bound to */
void KeyBind_Reset(InputBind binding);
/* Resets the gamepad button that the given input binding is bound to */
void PadBind_Reset(InputBind binding);
/* whether to leave text input open for user to enter further input */
#define HOTKEY_FLAG_STAYS_OPEN 0x01
/* Whether the hotkey was auto defined (e.g. by server) */
#define HOTKEY_FLAG_AUTO_DEFINED 0x02
extern const cc_uint8 Hotkeys_LWJGL[256];
struct HotkeyData {
int textIndex; /* contents to copy directly into the input bar */
cc_uint8 trigger; /* Member of Key enumeration */
cc_uint8 mods; /* HotkeyModifiers bitflags */
cc_uint8 flags; /* HOTKEY_FLAG flags */
};
#define HOTKEYS_MAX_COUNT 256
extern struct HotkeyData HotkeysList[HOTKEYS_MAX_COUNT];
extern struct StringsBuffer HotkeysText;
enum HotkeyModifiers {
HOTKEY_MOD_CTRL = 1, HOTKEY_MOD_SHIFT = 2, HOTKEY_MOD_ALT = 4
};
/* Adds or updates a new hotkey. */
void Hotkeys_Add(int trigger, cc_uint8 modifiers, const cc_string* text, cc_uint8 flags);
/* Removes the given hotkey. */
cc_bool Hotkeys_Remove(int trigger, cc_uint8 modifiers);
/* Returns the first hotkey which is bound to the given key and has its modifiers pressed. */
/* NOTE: The hotkeys list is sorted, so hotkeys with most modifiers are checked first. */
int Hotkeys_FindPartial(int key);
/* Loads the given hotkey from options. (if it exists) */
void StoredHotkeys_Load(int trigger, cc_uint8 modifiers);
/* Removes the given hotkey from options. */
void StoredHotkeys_Remove(int trigger, cc_uint8 modifiers);
/* Adds the given hotkey from options. */
void StoredHotkeys_Add(int trigger, cc_uint8 modifiers, cc_bool moreInput, const cc_string* text);
cc_bool InputHandler_SetFOV(int fov);
cc_bool Input_HandleMouseWheel(float delta);
void InputHandler_Tick(void);
void InputHandler_OnScreensChanged(void);
#endif

View File

@ -35,6 +35,7 @@
#include "Errors.h"
#include "SystemFonts.h"
#include "Lighting.h"
#include "InputHandler.h"
/* Describes a menu option button */
struct MenuOptionDesc {

View File

@ -30,6 +30,7 @@
#include "Picking.h"
#include "Input.h"
#include "Utils.h"
#include "InputHandler.h"
struct _ProtocolData Protocol;

View File

@ -22,6 +22,7 @@
#include "Input.h"
#include "Utils.h"
#include "Options.h"
#include "InputHandler.h"
#define CHAT_MAX_STATUS Array_Elems(Chat_Status)
#define CHAT_MAX_BOTTOMRIGHT Array_Elems(Chat_BottomRight)

View File

@ -16,6 +16,7 @@
#include "Bitmap.h"
#include "Block.h"
#include "Input.h"
#include "InputHandler.h"
static void Widget_NullFunc(void* widget) { }
static int Widget_Pointer(void* elem, int id, int x, int y) { return false; }