#pragma once #include "InputKey.hpp" #include "InputDevice.hpp" #include "Base/Utils.hpp" #include #include #include namespace openVulkanoCpp { namespace Input { class BaseInputAction { std::string name; std::vector devices; bool enabled; protected: BaseInputAction(const std::string& name) : name(name), enabled(true) {} public: virtual ~BaseInputAction() = default; const std::string& GetName() const { return name; } const std::vector& GetDevices() const { return devices; } bool IsEnabled() const { return enabled; } void SetEnabled(bool enabled) { this->enabled = enabled; } }; class InputAction : public BaseInputAction { std::vector keys; std::vector> axisButtons; public: InputAction(const std::string& name) : BaseInputAction(name) {} const std::vector& GetKeys() const { return keys; } const std::vector>& GetAxisButtons() const { return axisButtons; } void BindKey(InputKey key) { keys.push_back(key); } void BindAxisButtons(InputKey keyPositive, InputKey keyNegative) { axisButtons.emplace_back(keyPositive, keyNegative); } }; class InputShortcut : public BaseInputAction { std::string name; std::vector devices; std::vector> buttonBindings; public: InputShortcut(const std::string& name) : BaseInputAction(name) {} }; class InputManager { InputManager() = default; public: static InputManager* GetInstance() { static InputManager* instance = new InputManager(); return instance; } void RegisterInputDevice(InputDevice* device) { devices.push_back(device); if (!lastActiveDevice) lastActiveDevice = device; } void UnregisterInputDevice(InputDevice* device) { Utils::Remove(devices, device); } InputAction* GetAction(const std::string& actionName) { InputAction*& action = actionNameMapping[actionName]; if(!action) { action = new InputAction(actionName); } return action; } float GetAxis(const InputAction* action) const { float value = 0; const std::vector& testDevices = action->GetDevices().empty() ? devices : action->GetDevices(); for (const InputDevice* device : testDevices) { for(InputKey key : action->GetKeys()) { value += device->GetAxis(key); } for(const auto& keys : action->GetAxisButtons()) { value += GetAxis(keys.first) - GetAxis(keys.second); } } return value; } float GetAxis(InputKey key) const { float value = 0; for (const InputDevice* device : devices) { value += device->GetAxis(key); } return value; } bool GetButton(InputAction* action) const { const std::vector& testDevices = action->GetDevices().empty() ? devices : action->GetDevices(); for (const InputDevice* device : testDevices) { for (const InputKey key : action->GetKeys()) { if (device->GetButton(key)) return true; } } return false; } bool GetButton(InputKey key) const { for(const InputDevice* device : devices) { if (device->GetButton(key)) return true; } return false; } InputDevice* GetLastActiveDevice() const { return lastActiveDevice; } private: //std::unordered_map> inputActionMapping; std::unordered_map actionNameMapping; std::vector devices; InputDevice* lastActiveDevice = nullptr; }; } }