2024-03-31 10:11:22 +00:00
|
|
|
#pragma once
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2023-04-29 14:22:25 +00:00
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2022-09-02 23:02:09 +00:00
|
|
|
|
2023-05-01 13:13:35 +00:00
|
|
|
#include "inputs/keyboard.h"
|
|
|
|
#include "inputs/mouse.h"
|
|
|
|
#include "window.h"
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2022-10-02 15:34:51 +00:00
|
|
|
namespace engine {
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2023-04-29 14:56:49 +00:00
|
|
|
enum class InputDevice : int { kMouse, kKeyboard, kController, kSize };
|
2023-04-29 14:22:25 +00:00
|
|
|
|
|
|
|
// This class should be used to get platform/input-device independent input
|
|
|
|
class InputManager {
|
2024-03-31 10:11:22 +00:00
|
|
|
public:
|
|
|
|
/* The Window object here is stored for the duration of the InputManager.
|
|
|
|
* 'win' must point to a valid Window object. */
|
|
|
|
InputManager(const Window* win) : win_(win)
|
|
|
|
{
|
|
|
|
assert(win != nullptr);
|
|
|
|
enabled_devices_.fill(true);
|
|
|
|
}
|
|
|
|
InputManager(const InputManager&) = delete;
|
|
|
|
|
|
|
|
InputManager& operator=(const InputManager&) = delete;
|
|
|
|
|
|
|
|
~InputManager() {}
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void AddInputButton(const std::string& name, inputs::MouseButton button) { AddInputDeviceButton(name, InputDevice::kMouse, static_cast<int>(button)); }
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void AddInputButton(const std::string& name, inputs::Key button) { AddInputDeviceButton(name, InputDevice::kKeyboard, static_cast<int>(button)); }
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void AddInputAxis(const std::string& name, inputs::MouseAxis axis) { AddInputDeviceAxis(name, InputDevice::kMouse, static_cast<int>(axis)); }
|
|
|
|
|
|
|
|
void AddInputButtonAsAxis(const std::string& name, inputs::MouseButton high, inputs::MouseButton low)
|
|
|
|
{
|
|
|
|
AddInputDeviceButtonAsAxis(name, InputDevice::kMouse, static_cast<int>(high), static_cast<int>(low));
|
|
|
|
}
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void AddInputButtonAsAxis(const std::string& name, inputs::Key high, inputs::Key low)
|
|
|
|
{
|
|
|
|
AddInputDeviceButtonAsAxis(name, InputDevice::kKeyboard, static_cast<int>(high), static_cast<int>(low));
|
|
|
|
}
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void DelInputButton(int index)
|
|
|
|
{
|
|
|
|
std::vector<struct ButtonEntry>::iterator it = button_entries_.begin();
|
|
|
|
std::advance(it, index);
|
|
|
|
button_entries_.erase(it);
|
|
|
|
}
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void DelInputAxis(int index)
|
|
|
|
{
|
|
|
|
std::vector<struct AxisEntry>::iterator it = axis_entries_.begin();
|
|
|
|
std::advance(it, index);
|
|
|
|
axis_entries_.erase(it);
|
|
|
|
}
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void SetDeviceActive(enum InputDevice device, bool active) { enabled_devices_[static_cast<int>(device)] = active; }
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
bool GetDeviceActive(enum InputDevice device) const { return enabled_devices_[static_cast<int>(device)]; }
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
float GetAxis(const std::string& axis_name) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
bool GetButton(const std::string& button_name) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
bool GetButtonPress(const std::string& button_name) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
bool GetButtonRelease(const std::string& button_name) const;
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
private:
|
|
|
|
struct ButtonEntry {
|
|
|
|
std::string name;
|
|
|
|
enum InputDevice device;
|
|
|
|
int button; // enumeration of device buttons or axes
|
|
|
|
};
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
struct AxisEntry {
|
|
|
|
std::string name;
|
|
|
|
enum InputDevice device;
|
|
|
|
int axis;
|
|
|
|
bool is_button_axis;
|
|
|
|
int high;
|
|
|
|
int low;
|
|
|
|
};
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
const Window* win_;
|
2023-04-29 14:22:25 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
std::vector<struct ButtonEntry> button_entries_;
|
|
|
|
std::vector<struct AxisEntry> axis_entries_;
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
std::array<bool, static_cast<int>(InputDevice::kSize)> enabled_devices_;
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
// private methods
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
float GetDeviceAxis(enum InputDevice device, int axis) const;
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
bool GetDeviceButton(enum InputDevice device, int button) const;
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
bool getDeviceButtonDown(enum InputDevice device, int button) const;
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
bool GetDeviceButtonUp(enum InputDevice device, int button) const;
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
float GetButtonAxis(enum InputDevice device, int high, int low) const
|
|
|
|
{
|
|
|
|
float value = 0.0f;
|
|
|
|
if (GetDeviceButton(device, high)) value += 1.0f;
|
|
|
|
if (low != 0) {
|
|
|
|
if (GetDeviceButton(device, low)) value += -1.0f;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void AddInputDeviceButton(const std::string& name, InputDevice device, int button) { button_entries_.push_back({name, device, button}); }
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void AddInputDeviceAxis(const std::string& name, InputDevice device, int axis) { axis_entries_.push_back({name, device, axis, false, 0, 0}); }
|
2022-09-02 11:06:59 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
void AddInputDeviceButtonAsAxis(const std::string& name, InputDevice device, int high, int low)
|
|
|
|
{
|
|
|
|
axis_entries_.push_back({name, device, 0, true, high, low});
|
2023-04-29 14:56:49 +00:00
|
|
|
}
|
2023-04-29 14:22:25 +00:00
|
|
|
};
|
2022-10-02 15:34:51 +00:00
|
|
|
|
2024-03-31 10:11:22 +00:00
|
|
|
} // namespace engine
|