Play-/Source/input/InputBindingManager.h

158 lines
4.2 KiB
C
Raw Normal View History

2017-05-28 14:17:03 +01:00
#pragma once
#include "Config.h"
2020-02-25 18:47:50 +00:00
#include "InputConfig.h"
2017-05-28 14:17:03 +01:00
#include "ControllerInfo.h"
#include "InputProvider.h"
2018-05-23 03:11:24 +03:00
#include <array>
2017-05-28 14:17:03 +01:00
#include <memory>
#include <functional>
2020-02-25 18:47:50 +00:00
#include <string>
2017-05-28 14:17:03 +01:00
class CInputBindingManager
{
public:
typedef std::shared_ptr<CInputProvider> ProviderPtr;
2018-11-28 22:32:27 -05:00
enum
{
MAX_PADS = 2,
};
2018-11-28 22:32:27 -05:00
2017-05-28 14:17:03 +01:00
enum BINDINGTYPE
{
BINDING_UNBOUND = 0,
BINDING_SIMPLE = 1,
BINDING_SIMULATEDAXIS = 2,
BINDING_POVHAT = 3,
};
class CBinding
{
public:
virtual ~CBinding() = default;
2017-05-28 14:17:03 +01:00
virtual void ProcessEvent(const BINDINGTARGET&, uint32) = 0;
2017-05-28 14:17:03 +01:00
2018-04-30 21:01:23 +01:00
virtual BINDINGTYPE GetBindingType() const = 0;
virtual const char* GetBindingTypeName() const = 0;
virtual uint32 GetValue() const = 0;
virtual void SetValue(uint32) = 0;
virtual std::string GetDescription(CInputBindingManager*) const = 0;
2017-05-28 14:17:03 +01:00
2018-04-30 21:01:23 +01:00
virtual void Save(Framework::CConfig&, const char*) const = 0;
virtual void Load(Framework::CConfig&, const char*) = 0;
2017-05-28 14:17:03 +01:00
};
2018-11-28 22:32:27 -05:00
2018-11-20 08:33:23 -05:00
CInputBindingManager();
virtual ~CInputBindingManager() = default;
2017-05-28 14:17:03 +01:00
2018-11-20 08:33:23 -05:00
bool HasBindings() const;
2018-11-28 22:32:27 -05:00
2018-11-20 08:33:23 -05:00
void RegisterInputProvider(const ProviderPtr&);
void OverrideInputEventHandler(const InputEventFunction&);
2018-11-28 22:32:27 -05:00
2018-11-20 08:33:23 -05:00
std::string GetTargetDescription(const BINDINGTARGET&) const;
2018-11-28 22:32:27 -05:00
uint32 GetBindingValue(uint32, PS2::CControllerInfo::BUTTON) const;
2018-11-20 08:33:23 -05:00
void ResetBindingValues();
const CBinding* GetBinding(uint32, PS2::CControllerInfo::BUTTON) const;
void SetSimpleBinding(uint32, PS2::CControllerInfo::BUTTON, const BINDINGTARGET&);
void SetPovHatBinding(uint32, PS2::CControllerInfo::BUTTON, const BINDINGTARGET&, uint32);
void SetSimulatedAxisBinding(uint32, PS2::CControllerInfo::BUTTON, const BINDINGTARGET&, const BINDINGTARGET&);
2018-11-20 08:33:23 -05:00
2020-02-25 18:47:50 +00:00
void Reload();
void Load(std::string);
2018-11-20 08:33:23 -05:00
void Save();
private:
2017-05-28 14:17:03 +01:00
class CSimpleBinding : public CBinding
{
public:
CSimpleBinding() = default;
CSimpleBinding(const BINDINGTARGET&);
2018-11-28 22:32:27 -05:00
void ProcessEvent(const BINDINGTARGET&, uint32) override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
BINDINGTYPE GetBindingType() const override;
const char* GetBindingTypeName() const override;
std::string GetDescription(CInputBindingManager*) const override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
uint32 GetValue() const override;
void SetValue(uint32) override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
void Save(Framework::CConfig&, const char*) const override;
void Load(Framework::CConfig&, const char*) override;
2018-11-28 22:32:27 -05:00
2017-05-28 14:17:03 +01:00
private:
BINDINGTARGET m_binding;
uint32 m_value = 0;
2017-05-28 14:17:03 +01:00
};
2018-11-28 22:32:27 -05:00
2017-05-28 14:17:03 +01:00
class CPovHatBinding : public CBinding
{
public:
CPovHatBinding() = default;
CPovHatBinding(const BINDINGTARGET&, uint32 = -1);
2018-11-28 22:32:27 -05:00
void ProcessEvent(const BINDINGTARGET&, uint32) override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
BINDINGTYPE GetBindingType() const override;
const char* GetBindingTypeName() const override;
std::string GetDescription(CInputBindingManager*) const override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
uint32 GetValue() const override;
void SetValue(uint32) override;
2018-11-28 22:32:27 -05:00
static void RegisterPreferences(Framework::CConfig&, const char*);
2018-04-30 21:01:23 +01:00
void Save(Framework::CConfig&, const char*) const override;
void Load(Framework::CConfig&, const char*) override;
2018-11-28 22:32:27 -05:00
2017-05-28 14:17:03 +01:00
private:
2018-04-30 21:01:23 +01:00
static int32 GetShortestDistanceBetweenAngles(int32, int32);
2018-11-28 22:32:27 -05:00
BINDINGTARGET m_binding;
2018-04-30 21:01:23 +01:00
uint32 m_refValue = 0;
uint32 m_value = 0;
2017-05-28 14:17:03 +01:00
};
2018-11-28 22:32:27 -05:00
2017-05-28 14:17:03 +01:00
class CSimulatedAxisBinding : public CBinding
{
public:
CSimulatedAxisBinding() = default;
CSimulatedAxisBinding(const BINDINGTARGET&, const BINDINGTARGET&);
2018-11-28 22:32:27 -05:00
void ProcessEvent(const BINDINGTARGET&, uint32) override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
BINDINGTYPE GetBindingType() const override;
const char* GetBindingTypeName() const override;
std::string GetDescription(CInputBindingManager*) const override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
uint32 GetValue() const override;
2018-07-31 13:00:55 -04:00
void SetValue(uint32) override;
2018-11-28 22:32:27 -05:00
2018-04-30 21:01:23 +01:00
void Save(Framework::CConfig&, const char*) const override;
void Load(Framework::CConfig&, const char*) override;
2018-11-28 22:32:27 -05:00
2017-05-28 14:17:03 +01:00
private:
BINDINGTARGET m_key1Binding;
BINDINGTARGET m_key2Binding;
2018-11-28 22:32:27 -05:00
uint32 m_key1State = 0;
uint32 m_key2State = 0;
2017-05-28 14:17:03 +01:00
};
2018-04-30 21:01:23 +01:00
typedef std::shared_ptr<CBinding> BindingPtr;
typedef std::map<uint32, ProviderPtr> ProviderMap;
void OnInputEventReceived(const BINDINGTARGET&, uint32);
2017-05-28 14:17:03 +01:00
BindingPtr m_bindings[MAX_PADS][PS2::CControllerInfo::MAX_BUTTONS];
2018-04-30 21:01:23 +01:00
static uint32 m_buttonDefaultValue[PS2::CControllerInfo::MAX_BUTTONS];
static const char* m_padPreferenceName[MAX_PADS];
2018-11-28 22:32:27 -05:00
2020-02-25 18:47:50 +00:00
std::unique_ptr<CInputConfig> m_config;
ProviderMap m_providers;
2017-05-28 14:17:03 +01:00
};