summaryrefslogtreecommitdiffstats
path: root/src/input_common/analog_from_button.cpp
diff options
context:
space:
mode:
authorwwylele <wwylele@gmail.com>2017-01-21 12:04:00 +0100
committerwwylele <wwylele@gmail.com>2017-03-01 22:30:57 +0100
commita6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a (patch)
tree2ed60e602d520f80f57f88f879c4f50363432c17 /src/input_common/analog_from_button.cpp
parentInputCommon: add Keyboard (diff)
downloadyuzu-a6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a.tar
yuzu-a6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a.tar.gz
yuzu-a6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a.tar.bz2
yuzu-a6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a.tar.lz
yuzu-a6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a.tar.xz
yuzu-a6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a.tar.zst
yuzu-a6bd7917cbc06f9b8f5a7ae24e75db776dc1cd6a.zip
Diffstat (limited to '')
-rwxr-xr-xsrc/input_common/analog_from_button.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/input_common/analog_from_button.cpp b/src/input_common/analog_from_button.cpp
new file mode 100755
index 000000000..e1a260762
--- /dev/null
+++ b/src/input_common/analog_from_button.cpp
@@ -0,0 +1,58 @@
+// Copyright 2017 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#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_,
+ float modifier_scale_)
+ : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
+ right(std::move(right_)), modifier(std::move(modifier_)),
+ modifier_scale(modifier_scale_) {}
+
+ std::tuple<float, float> GetStatus() const override {
+ 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;
+
+ float coef = modifier->GetStatus() ? modifier_scale : 1.0f;
+ return std::make_tuple(x * coef * (y == 0 ? 1.0f : SQRT_HALF),
+ y * coef * (x == 0 ? 1.0f : SQRT_HALF));
+ }
+
+private:
+ Button up;
+ Button down;
+ Button left;
+ Button right;
+ Button modifier;
+ float modifier_scale;
+};
+
+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);
+ return std::make_unique<Analog>(std::move(up), std::move(down), std::move(left),
+ std::move(right), std::move(modifier), modifier_scale);
+}
+
+} // namespace InputCommon