2017-01-21 14:04:00 +03:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-01-30 02:41:22 +03:00
|
|
|
#include <atomic>
|
2020-11-07 18:48:11 +03:00
|
|
|
#include <chrono>
|
|
|
|
#include <cmath>
|
|
|
|
#include <thread>
|
|
|
|
#include "common/math_util.h"
|
2021-04-15 02:07:40 +03:00
|
|
|
#include "common/settings.h"
|
2017-01-21 14:04:00 +03:00
|
|
|
#include "input_common/analog_from_button.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
|
|
|
class Analog final : public Input::AnalogDevice {
|
|
|
|
public:
|
|
|
|
using Button = std::unique_ptr<Input::ButtonDevice>;
|
|
|
|
|
|
|
|
Analog(Button up_, Button down_, Button left_, Button right_, Button modifier_,
|
2020-11-07 18:48:11 +03:00
|
|
|
float modifier_scale_, float modifier_angle_)
|
2017-01-21 14:04:00 +03:00
|
|
|
: up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
|
2020-11-07 18:48:11 +03:00
|
|
|
right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_),
|
|
|
|
modifier_angle(modifier_angle_) {
|
2021-05-30 07:35:46 +03:00
|
|
|
Input::InputCallback<bool> callbacks{
|
|
|
|
[this]([[maybe_unused]] bool status) { UpdateStatus(); }};
|
|
|
|
up->SetCallback(callbacks);
|
|
|
|
down->SetCallback(callbacks);
|
|
|
|
left->SetCallback(callbacks);
|
|
|
|
right->SetCallback(callbacks);
|
2021-07-05 19:39:42 +03:00
|
|
|
modifier->SetCallback(callbacks);
|
2020-11-07 18:48:11 +03:00
|
|
|
}
|
2017-01-21 14:04:00 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
bool IsAngleGreater(float old_angle, float new_angle) const {
|
|
|
|
constexpr float TAU = Common::PI * 2.0f;
|
|
|
|
// Use wider angle to ease the transition.
|
|
|
|
constexpr float aperture = TAU * 0.15f;
|
|
|
|
const float top_limit = new_angle + aperture;
|
|
|
|
return (old_angle > new_angle && old_angle <= top_limit) ||
|
|
|
|
(old_angle + TAU > new_angle && old_angle + TAU <= top_limit);
|
2020-11-07 18:48:11 +03:00
|
|
|
}
|
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
bool IsAngleSmaller(float old_angle, float new_angle) const {
|
2020-11-07 18:48:11 +03:00
|
|
|
constexpr float TAU = Common::PI * 2.0f;
|
|
|
|
// Use wider angle to ease the transition.
|
|
|
|
constexpr float aperture = TAU * 0.15f;
|
2021-05-30 07:35:46 +03:00
|
|
|
const float bottom_limit = new_angle - aperture;
|
|
|
|
return (old_angle >= bottom_limit && old_angle < new_angle) ||
|
|
|
|
(old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {
|
|
|
|
constexpr float TAU = Common::PI * 2.0f;
|
|
|
|
float new_angle = angle;
|
|
|
|
|
|
|
|
auto time_difference = static_cast<float>(
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count());
|
|
|
|
time_difference /= 1000.0f * 1000.0f;
|
|
|
|
if (time_difference > 0.5f) {
|
|
|
|
time_difference = 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsAngleGreater(new_angle, goal_angle)) {
|
|
|
|
new_angle -= modifier_angle * time_difference;
|
|
|
|
if (new_angle < 0) {
|
|
|
|
new_angle += TAU;
|
|
|
|
}
|
|
|
|
if (!IsAngleGreater(new_angle, goal_angle)) {
|
|
|
|
return goal_angle;
|
2020-11-07 18:48:11 +03:00
|
|
|
}
|
2021-05-30 07:35:46 +03:00
|
|
|
} else if (IsAngleSmaller(new_angle, goal_angle)) {
|
|
|
|
new_angle += modifier_angle * time_difference;
|
|
|
|
if (new_angle >= TAU) {
|
|
|
|
new_angle -= TAU;
|
|
|
|
}
|
|
|
|
if (!IsAngleSmaller(new_angle, goal_angle)) {
|
|
|
|
return goal_angle;
|
2020-11-07 18:48:11 +03:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-30 07:35:46 +03:00
|
|
|
return goal_angle;
|
2020-10-14 09:51:14 +03:00
|
|
|
}
|
2021-05-30 07:35:46 +03:00
|
|
|
return new_angle;
|
2020-11-07 18:48:11 +03:00
|
|
|
}
|
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
void SetGoalAngle(bool r, bool l, bool u, bool d) {
|
|
|
|
// Move to the right
|
|
|
|
if (r && !u && !d) {
|
|
|
|
goal_angle = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the upper right
|
|
|
|
if (r && u && !d) {
|
|
|
|
goal_angle = Common::PI * 0.25f;
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Move up
|
|
|
|
if (u && !l && !r) {
|
|
|
|
goal_angle = Common::PI * 0.5f;
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Move to the upper left
|
|
|
|
if (l && u && !d) {
|
|
|
|
goal_angle = Common::PI * 0.75f;
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Move to the left
|
|
|
|
if (l && !u && !d) {
|
|
|
|
goal_angle = Common::PI;
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Move to the bottom left
|
|
|
|
if (l && !u && d) {
|
|
|
|
goal_angle = Common::PI * 1.25f;
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Move down
|
|
|
|
if (d && !l && !r) {
|
|
|
|
goal_angle = Common::PI * 1.5f;
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Move to the bottom right
|
|
|
|
if (r && !u && d) {
|
|
|
|
goal_angle = Common::PI * 1.75f;
|
|
|
|
}
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
void UpdateStatus() {
|
|
|
|
const float coef = modifier->GetStatus() ? modifier_scale : 1.0f;
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
bool r = right->GetStatus();
|
|
|
|
bool l = left->GetStatus();
|
|
|
|
bool u = up->GetStatus();
|
|
|
|
bool d = down->GetStatus();
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Eliminate contradictory movements
|
|
|
|
if (r && l) {
|
|
|
|
r = false;
|
|
|
|
l = false;
|
|
|
|
}
|
|
|
|
if (u && d) {
|
|
|
|
u = false;
|
|
|
|
d = false;
|
|
|
|
}
|
2020-11-07 18:48:11 +03:00
|
|
|
|
2021-05-30 07:35:46 +03:00
|
|
|
// Move if a key is pressed
|
|
|
|
if (r || l || u || d) {
|
|
|
|
amplitude = coef;
|
|
|
|
} else {
|
|
|
|
amplitude = 0;
|
2020-10-14 09:51:14 +03:00
|
|
|
}
|
2021-05-30 07:35:46 +03:00
|
|
|
|
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
const auto time_difference = static_cast<u64>(
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count());
|
|
|
|
|
|
|
|
if (time_difference < 10) {
|
|
|
|
// Disable analog mode if inputs are too fast
|
|
|
|
SetGoalAngle(r, l, u, d);
|
|
|
|
angle = goal_angle;
|
|
|
|
} else {
|
|
|
|
angle = GetAngle(now);
|
|
|
|
SetGoalAngle(r, l, u, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
last_update = now;
|
2020-11-07 18:48:11 +03:00
|
|
|
}
|
2017-01-21 14:04:00 +03:00
|
|
|
|
2020-11-07 18:48:11 +03:00
|
|
|
std::tuple<float, float> GetStatus() const override {
|
2020-11-27 05:56:06 +03:00
|
|
|
if (Settings::values.emulate_analog_keyboard) {
|
2021-05-30 07:35:46 +03:00
|
|
|
const auto now = std::chrono::steady_clock::now();
|
|
|
|
float angle_ = GetAngle(now);
|
|
|
|
return std::make_tuple(std::cos(angle_) * amplitude, std::sin(angle_) * amplitude);
|
2020-11-27 05:56:06 +03:00
|
|
|
}
|
|
|
|
constexpr float SQRT_HALF = 0.707106781f;
|
|
|
|
int x = 0, y = 0;
|
|
|
|
if (right->GetStatus()) {
|
|
|
|
++x;
|
|
|
|
}
|
|
|
|
if (left->GetStatus()) {
|
|
|
|
--x;
|
|
|
|
}
|
|
|
|
if (up->GetStatus()) {
|
|
|
|
++y;
|
|
|
|
}
|
|
|
|
if (down->GetStatus()) {
|
|
|
|
--y;
|
|
|
|
}
|
|
|
|
const float coef = modifier->GetStatus() ? modifier_scale : 1.0f;
|
|
|
|
return std::make_tuple(static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF),
|
|
|
|
static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF));
|
2017-01-21 14:04:00 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 06:39:47 +03:00
|
|
|
Input::AnalogProperties GetAnalogProperties() const override {
|
|
|
|
return {modifier_scale, 1.0f, 0.5f};
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:45:37 +03:00
|
|
|
bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override {
|
|
|
|
switch (direction) {
|
|
|
|
case Input::AnalogDirection::RIGHT:
|
|
|
|
return right->GetStatus();
|
|
|
|
case Input::AnalogDirection::LEFT:
|
|
|
|
return left->GetStatus();
|
|
|
|
case Input::AnalogDirection::UP:
|
|
|
|
return up->GetStatus();
|
|
|
|
case Input::AnalogDirection::DOWN:
|
|
|
|
return down->GetStatus();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-21 14:04:00 +03:00
|
|
|
private:
|
|
|
|
Button up;
|
|
|
|
Button down;
|
|
|
|
Button left;
|
|
|
|
Button right;
|
|
|
|
Button modifier;
|
|
|
|
float modifier_scale;
|
2020-11-07 18:48:11 +03:00
|
|
|
float modifier_angle;
|
|
|
|
float angle{};
|
2021-05-30 07:35:46 +03:00
|
|
|
float goal_angle{};
|
2020-11-07 18:48:11 +03:00
|
|
|
float amplitude{};
|
2021-05-30 07:35:46 +03:00
|
|
|
std::chrono::time_point<std::chrono::steady_clock> last_update;
|
2017-01-21 14:04:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Input::AnalogDevice> AnalogFromButton::Create(const Common::ParamPackage& params) {
|
|
|
|
const std::string null_engine = Common::ParamPackage{{"engine", "null"}}.Serialize();
|
|
|
|
auto up = Input::CreateDevice<Input::ButtonDevice>(params.Get("up", null_engine));
|
|
|
|
auto down = Input::CreateDevice<Input::ButtonDevice>(params.Get("down", null_engine));
|
|
|
|
auto left = Input::CreateDevice<Input::ButtonDevice>(params.Get("left", null_engine));
|
|
|
|
auto right = Input::CreateDevice<Input::ButtonDevice>(params.Get("right", null_engine));
|
|
|
|
auto modifier = Input::CreateDevice<Input::ButtonDevice>(params.Get("modifier", null_engine));
|
|
|
|
auto modifier_scale = params.Get("modifier_scale", 0.5f);
|
2021-05-30 07:35:46 +03:00
|
|
|
auto modifier_angle = params.Get("modifier_angle", 5.5f);
|
2017-01-21 14:04:00 +03:00
|
|
|
return std::make_unique<Analog>(std::move(up), std::move(down), std::move(left),
|
2020-11-07 18:48:11 +03:00
|
|
|
std::move(right), std::move(modifier), modifier_scale,
|
|
|
|
modifier_angle);
|
2017-01-21 14:04:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace InputCommon
|