summaryrefslogtreecommitdiffstats
path: root/src/input_common/analog_from_button.cpp
blob: 07a0fa4a1480162c1ecfd01542a9dcdb89ee59d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <atomic>
#include <chrono>
#include <cmath>
#include <thread>
#include "common/math_util.h"
#include "core/settings.h"
#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_, float modifier_angle_)
        : up(std::move(up_)), down(std::move(down_)), left(std::move(left_)),
          right(std::move(right_)), modifier(std::move(modifier_)), modifier_scale(modifier_scale_),
          modifier_angle(modifier_angle_) {
        update_thread_running.store(true);
        update_thread = std::thread(&Analog::UpdateStatus, this);
    }

    ~Analog() override {
        if (update_thread_running.load()) {
            update_thread_running.store(false);
            if (update_thread.joinable()) {
                update_thread.join();
            }
        }
    }

    void MoveToDirection(bool enable, float to_angle) {
        if (!enable) {
            return;
        }
        constexpr float TAU = Common::PI * 2.0f;
        // Use wider angle to ease the transition.
        constexpr float aperture = TAU * 0.15f;
        const float top_limit = to_angle + aperture;
        const float bottom_limit = to_angle - aperture;

        if ((angle > to_angle && angle <= top_limit) ||
            (angle + TAU > to_angle && angle + TAU <= top_limit)) {
            angle -= modifier_angle;
            if (angle < 0) {
                angle += TAU;
            }
        } else if ((angle >= bottom_limit && angle < to_angle) ||
                   (angle - TAU >= bottom_limit && angle - TAU < to_angle)) {
            angle += modifier_angle;
            if (angle >= TAU) {
                angle -= TAU;
            }
        } else {
            angle = to_angle;
        }
    }

    void UpdateStatus() {
        while (update_thread_running.load()) {
            const float coef = modifier->GetStatus() ? modifier_scale : 1.0f;

            bool r = right->GetStatus();
            bool l = left->GetStatus();
            bool u = up->GetStatus();
            bool d = down->GetStatus();

            // Eliminate contradictory movements
            if (r && l) {
                r = false;
                l = false;
            }
            if (u && d) {
                u = false;
                d = false;
            }

            // Move to the right
            MoveToDirection(r && !u && !d, 0.0f);

            // Move to the upper right
            MoveToDirection(r && u && !d, Common::PI * 0.25f);

            // Move up
            MoveToDirection(u && !l && !r, Common::PI * 0.5f);

            // Move to the upper left
            MoveToDirection(l && u && !d, Common::PI * 0.75f);

            // Move to the left
            MoveToDirection(l && !u && !d, Common::PI);

            // Move to the bottom left
            MoveToDirection(l && !u && d, Common::PI * 1.25f);

            // Move down
            MoveToDirection(d && !l && !r, Common::PI * 1.5f);

            // Move to the bottom right
            MoveToDirection(r && !u && d, Common::PI * 1.75f);

            // Move if a key is pressed
            if (r || l || u || d) {
                amplitude = coef;
            } else {
                amplitude = 0;
            }

            // Delay the update rate to 100hz
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }

    std::tuple<float, float> GetStatus() const override {
        if (Settings::values.emulate_analog_keyboard) {
            return std::make_tuple(std::cos(angle) * amplitude, std::sin(angle) * amplitude);
        }
        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));
    }

    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;
    }

private:
    Button up;
    Button down;
    Button left;
    Button right;
    Button modifier;
    float modifier_scale;
    float modifier_angle;
    float angle{};
    float amplitude{};
    std::thread update_thread;
    std::atomic<bool> update_thread_running{};
};

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);
    auto modifier_angle = params.Get("modifier_angle", 0.035f);
    return std::make_unique<Analog>(std::move(up), std::move(down), std::move(left),
                                    std::move(right), std::move(modifier), modifier_scale,
                                    modifier_angle);
}

} // namespace InputCommon