summaryrefslogtreecommitdiff
path: root/common/rawaccel.hpp
blob: 6710a0cdfcfd9c9f3c8a8f72921b28515d6c3699 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#pragma once

#include "accel-invoke.hpp"

#define _USE_MATH_DEFINES
#include <math.h>

namespace rawaccel {

    /// <summary> Struct to hold vector rotation details. </summary>
    struct rotator {

        /// <summary> Rotational vector, which points in the direction of the post-rotation positive x axis. </summary>
        vec2d rot_vec = { 1, 0 };

        /// <summary>
        /// Rotates given input vector according to struct's rotational vector.
        /// </summary>
        /// <param name="input">Input vector to be rotated</param>
        /// <returns>2d vector of rotated input.</returns>
        inline vec2d apply(const vec2d& input) const {
            return {
                input.x * rot_vec.x - input.y * rot_vec.y,
                input.x * rot_vec.y + input.y * rot_vec.x
            };
        }

        rotator(double degrees) {
            double rads = degrees * M_PI / 180;
            rot_vec = { cos(rads), sin(rads) };
        }

        rotator() = default;
    };

    struct snapper {
        double threshold = 0;

        inline vec2d apply(const vec2d& input) const {
            if (input.x != 0 && input.y != 0) {
                double angle = fabs(atan(input.y / input.x));
                auto mag = [&] { return sqrtsd(input.x * input.x + input.y * input.y); };

                if (angle > M_PI_2 - threshold) return { 0, _copysign(mag(), input.y) };
                if (angle < threshold) return { _copysign(mag(), input.x), 0 };
            }

            return input;
        }

        snapper(double degrees) : threshold(minsd(fabs(degrees), 45)* M_PI / 180) {}

        snapper() = default;
    };

    inline void clamp_speed(vec2d& v, double min, double max, double norm) {
        double speed = sqrtsd(v.x * v.x + v.y * v.y) * norm;
        double ratio = clampsd(speed, min, max) / speed;
        v.x *= ratio;
        v.y *= ratio;
    }

    struct weighted_distance {
        double p = 2.0;
        double p_inverse = 0.5;
        bool lp_norm_infinity = false;
        double sigma_x = 1.0;
        double sigma_y = 1.0;

        weighted_distance(const domain_args& args)
        {
            sigma_x = args.domain_weights.x;
            sigma_y = args.domain_weights.y;
            if (args.lp_norm <= 0)
            {
                lp_norm_infinity = true;
                p = 0.0;
                p_inverse = 0.0;
            }
            else
            {
                lp_norm_infinity = false;
                p = args.lp_norm;
                p_inverse = 1 / args.lp_norm;
            }
        }

        inline double calculate(double x, double y)
        {
            double abs_scaled_x = fabs(x) * sigma_x;
            double abs_scaled_y = fabs(y) * sigma_y;

            if (lp_norm_infinity) {
                return maxsd(abs_scaled_x, abs_scaled_y);
            }

            if (p == 2) {
                return sqrtsd(abs_scaled_x * abs_scaled_x +
                    abs_scaled_y * abs_scaled_y);
            }

            double dist_p = pow(abs_scaled_x, p) + pow(abs_scaled_y, p);
            return pow(dist_p, p_inverse);
        }

        weighted_distance() = default;
    };

    inline double directional_weight(const vec2d& in, const vec2d& weights)
    {
        double atan_scale = M_2_PI * atan2(fabs(in.y), fabs(in.x));
        return atan_scale * (weights.y - weights.x) + weights.x;
    }

    /// <summary> Struct to hold variables and methods for modifying mouse input </summary>
    struct mouse_modifier {
        bool apply_rotate = false;
        bool apply_snap = false;
        bool apply_speed_clamp = false;
        bool by_component = false;
        bool apply_directional_weight = false;
        rotator rotate;
        snapper snap;
        double dpi_factor = 1;
        double speed_min = 0;
        double speed_max = 0;
        weighted_distance distance;
        vec2d range_weights = { 1, 1 };
        vec2<accel_union> accels;
        vec2d sensitivity = { 1, 1 };
        vec2d directional_multipliers = {};

        mouse_modifier(const settings& args) :
            by_component(!args.combine_mags),
            dpi_factor(1000 / args.dpi),
            speed_min(args.speed_min),
            speed_max(args.speed_max),
            range_weights(args.range_weights)
        {
            if (args.degrees_rotation != 0) {
                rotate = rotator(args.degrees_rotation);
                apply_rotate = true;
            }

            if (args.degrees_snap != 0) {
                snap = snapper(args.degrees_snap);
                apply_snap = true;
            }

            if (args.sens.x != 0) sensitivity.x = args.sens.x;
            if (args.sens.y != 0) sensitivity.y = args.sens.y;

            directional_multipliers.x = fabs(args.dir_multipliers.x);
            directional_multipliers.y = fabs(args.dir_multipliers.y);

            apply_speed_clamp = speed_max > 0;

            if ((!by_component && args.argsv.x.mode == accel_mode::noaccel) ||
                (args.argsv.x.mode == accel_mode::noaccel &&
                    args.argsv.y.mode == accel_mode::noaccel)) {
                return;
            }

            accels.x = accel_union(args.argsv.x);
            accels.y = accel_union(args.argsv.y);

            distance = weighted_distance(args.dom_args);

            apply_directional_weight = range_weights.x != range_weights.y;
        }

        void modify(vec2d& movement, const vec2<accel_invoker>& inv = {}, milliseconds time = 1) {
            apply_rotation(movement);
            apply_angle_snap(movement);
            apply_acceleration(movement, inv, time);
            apply_sensitivity(movement);
        }

        inline void apply_rotation(vec2d& movement) {
            if (apply_rotate) movement = rotate.apply(movement);
        }

        inline void apply_angle_snap(vec2d& movement) {
            if (apply_snap) movement = snap.apply(movement);
        }

        inline void apply_acceleration(vec2d& movement, const vec2<accel_invoker>& inv, milliseconds time) {
            double norm = dpi_factor / time;

            if (apply_speed_clamp) {
                clamp_speed(movement, speed_min, speed_max, norm);
            }

            if (!by_component) {
                double mag = distance.calculate(movement.x, movement.y);
                double speed = mag * norm;

                double weight;

                if (apply_directional_weight) {
                    weight = directional_weight(movement, range_weights);
                }
                else {
                    weight = range_weights.x;
                }

                double scale = inv.x.invoke(accels.x, speed, weight);
                movement.x *= scale;
                movement.y *= scale;
            }
            else {
                if (movement.x != 0) {
                    double x = fabs(movement.x) * norm * distance.sigma_x;
                    movement.x *= inv.x.invoke(accels.x, x, range_weights.x);
                }
                if (movement.y != 0) {
                    double y = fabs(movement.y) * norm * distance.sigma_y;
                    movement.y *= inv.y.invoke(accels.y, y, range_weights.y);
                }
            }
        }
    

        inline void apply_sensitivity(vec2d& movement) {   
            movement.x *= sensitivity.x;
            movement.y *= sensitivity.y;

            if (directional_multipliers.x > 0 && movement.x < 0) {
                movement.x *= directional_multipliers.x;
            }
            if (directional_multipliers.y > 0 && movement.y < 0) {
                movement.y *= directional_multipliers.y;
            }
        }

        mouse_modifier() = default;
    };

    struct io_t {
        settings args;
        mouse_modifier mod;
    };

} // rawaccel