Files
simian/include/InputManager.h
T
2026-03-08 12:14:50 +13:00

82 lines
2.8 KiB
C++

#pragma once
#include "raylib.h"
#include <string>
#include <vector>
#include <unordered_map>
// Input types supported by the action system
enum class InputType {
Keyboard,
MouseButton,
GamepadButton,
GamepadAxis
};
// Represents a single input binding (e.g., KEY_SPACE, MOUSE_BUTTON_LEFT)
struct InputBinding {
InputType type;
int code; // Key code, mouse button, or gamepad button
int gamepadId; // For gamepad inputs (0-3)
float axisThreshold; // For axis inputs (e.g., trigger > 0.5)
bool negative; // For axis inputs (e.g., left stick left = negative)
InputBinding()
: type(InputType::Keyboard)
, code(0)
, gamepadId(0)
, axisThreshold(0.5f)
, negative(false) {}
InputBinding(InputType t, int c, int gamepad = 0)
: type(t)
, code(c)
, gamepadId(gamepad)
, axisThreshold(0.5f)
, negative(false) {}
};
// Manages input actions with remappable bindings
class InputManager {
public:
InputManager();
~InputManager();
// Action management
void RegisterAction(const std::string& actionName);
void UnregisterAction(const std::string& actionName);
bool HasAction(const std::string& actionName) const;
// Binding management
void BindKeyboard(const std::string& actionName, int keyCode);
void BindMouseButton(const std::string& actionName, int mouseButton);
void BindGamepadButton(const std::string& actionName, int gamepadId, int button);
void BindGamepadAxis(const std::string& actionName, int gamepadId, int axis, float threshold, bool negative = false);
void ClearBindings(const std::string& actionName);
// Input queries (called per frame)
bool IsActionPressed(const std::string& actionName) const;
bool IsActionDown(const std::string& actionName) const;
bool IsActionReleased(const std::string& actionName) const;
float GetActionStrength(const std::string& actionName) const; // For analog inputs
// Configuration
void LoadFromFile(const std::string& filepath);
void SaveToFile(const std::string& filepath) const;
void SetupDefaultBindings(); // Define default controls
// Get list of all actions
std::vector<std::string> GetAllActions() const;
// Get bindings for an action
const std::vector<InputBinding>& GetBindings(const std::string& actionName) const;
private:
std::unordered_map<std::string, std::vector<InputBinding>> actions;
// Helper to check if a single binding is active
bool IsBindingPressed(const InputBinding& binding) const;
bool IsBindingDown(const InputBinding& binding) const;
bool IsBindingReleased(const InputBinding& binding) const;
float GetBindingStrength(const InputBinding& binding) const;
};