Add scaling factors for input actions

This commit is contained in:
2023-10-16 13:44:55 +02:00
parent 68fb6d0910
commit 5b404efa74
3 changed files with 45 additions and 15 deletions

View File

@@ -55,12 +55,16 @@ namespace OpenVulkano
{
m_actionForward->BindKey(Input::InputKey::Controller::AXIS_LEFT_Y);
m_actionForward->BindAxisButtons(Input::InputKey::Keyboard::KEY_W, Input::InputKey::Keyboard::KEY_S);
m_actionForward->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y, -0.01f);
m_actionSide->BindKey(Input::InputKey::Controller::AXIS_LEFT_X);
m_actionSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_D, Input::InputKey::Keyboard::KEY_A);
m_actionSide->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_X, 0.01f);
m_actionLookUp->BindKey(Input::InputKey::Controller::AXIS_RIGHT_Y);
m_actionLookSide->BindKey(Input::InputKey::Controller::AXIS_RIGHT_X);
m_actionLookUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_DOWN, Input::InputKey::Keyboard::KEY_UP);
m_actionLookUp->BindKey(Input::InputKey::Touch::AXIS_PAN_Y, 0.10f);
m_actionLookSide->BindKey(Input::InputKey::Controller::AXIS_RIGHT_X);
m_actionLookSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_RIGHT, Input::InputKey::Keyboard::KEY_LEFT);
m_actionLookSide->BindKey(Input::InputKey::Touch::AXIS_PAN_X, 0.10f);
m_actionLookUp->BindKey(Input::InputKey::Mouse::AXIS_Y);
m_actionLookSide->BindKey(Input::InputKey::Mouse::AXIS_X);
m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL);

View File

@@ -11,33 +11,54 @@
namespace OpenVulkano::Input
{
struct KeyBinding
{
InputKey key;
float scale;
KeyBinding(InputKey keyBinding, float scaleFactor)
: key(keyBinding), scale(scaleFactor)
{}
};
struct AxisButtonBinding
{
InputKey positive;
InputKey negative;
float scale;
AxisButtonBinding(InputKey pos, InputKey neg, float scaleFactor)
: positive(pos), negative(neg), scale(scaleFactor)
{}
};
class InputAction final : public BaseInputAction
{
std::vector<InputKey> keys;
std::vector<std::pair<InputKey, InputKey>> axisButtons;
std::vector<KeyBinding> keys;
std::vector<AxisButtonBinding> axisButtons;
public:
InputAction(const std::string& name) : BaseInputAction(name)
{}
[[nodiscard]] const std::vector<InputKey>& GetKeys() const
[[nodiscard]] const std::vector<KeyBinding>& GetKeys() const
{
return keys;
}
[[nodiscard]] const std::vector<std::pair<InputKey, InputKey>>& GetAxisButtons() const
[[nodiscard]] const std::vector<AxisButtonBinding>& GetAxisButtons() const
{
return axisButtons;
}
void BindKey(InputKey key)
void BindKey(InputKey key, float scale = 1)
{
keys.push_back(key);
keys.emplace_back(key, scale);
}
void BindAxisButtons(InputKey keyPositive, InputKey keyNegative)
void BindAxisButtons(InputKey keyPositive, InputKey keyNegative, float scale = 1)
{
axisButtons.emplace_back(keyPositive, keyNegative);
axisButtons.emplace_back(keyPositive, keyNegative, scale);
}
};
}

View File

@@ -36,13 +36,15 @@ namespace OpenVulkano::Input
const std::vector<InputDevice*>& testDevices = action->GetDevices().empty() ? devices : action->GetDevices();
for (const InputDevice* device : testDevices)
{
for(InputKey key : action->GetKeys())
for(KeyBinding binding : action->GetKeys())
{
value += device->GetAxis(key);
if (binding.key.GetInputDeviceType() != device->GetType()) continue;
value += device->GetAxis(binding.key) * binding.scale;
}
for(const auto& keys : action->GetAxisButtons())
for(const auto& binding : action->GetAxisButtons())
{
value += GetAxis(keys.first) - GetAxis(keys.second);
if (binding.positive.GetInputDeviceType() != device->GetType()) continue;
value += (GetAxis(binding.positive) - GetAxis(binding.negative)) * binding.scale;
}
}
return value;
@@ -53,6 +55,7 @@ namespace OpenVulkano::Input
float value = 0;
for (const InputDevice* device : devices)
{
if (key.GetInputDeviceType() != device->GetType()) continue;
value += device->GetAxis(key);
}
return value;
@@ -63,9 +66,10 @@ namespace OpenVulkano::Input
const std::vector<InputDevice*>& testDevices = action->GetDevices().empty() ? devices : action->GetDevices();
for (const InputDevice* device : testDevices)
{
for (const InputKey key : action->GetKeys())
for (const KeyBinding binding : action->GetKeys())
{
if (device->GetButton(key)) return true;
if (binding.key.GetInputDeviceType() != device->GetType()) continue;
if (device->GetButton(binding.key)) return true;
}
}
return false;
@@ -75,6 +79,7 @@ namespace OpenVulkano::Input
{
for(const InputDevice* device : devices)
{
if (key.GetInputDeviceType() != device->GetType()) continue;
if (device->GetButton(key)) return true;
}
return false;