summaryrefslogtreecommitdiff
path: root/common/accel-base.hpp
blob: da2c96bce27d4dc2b4e1a5069a17411d598e8ecb (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
#pragma once

#include "vec2.h"

namespace rawaccel {

    // Error throwing calls std libraries which are unavailable in kernel mode.
    void error(const char* s);

    using milliseconds = double;

    /// <summary> Struct to hold arguments for an acceleration function. </summary>
    struct accel_args {
        double offset = 0;
        double accel = 0;
        double limit = 2;
        double exponent = 2;
        double midpoint = 0;
        double power_scale = 1;
        vec2d weight = { 1, 1 };
    };

    /// <summary>
    /// Struct to hold common acceleration curve implementation details.
    /// </summary>
    struct accel_base {

        /// <summary> Coefficients applied to acceleration per axis.</summary>
        vec2d weight = { 1, 1 };

        /// <summary> Generally, the acceleration ramp rate.
        double speed_coeff = 0;

        accel_base(const accel_args& args) {
            verify(args);

            speed_coeff = args.accel;
            weight = args.weight;
        }

        /// <summary> 
        /// Default transformation of speed -> acceleration.
        /// </summary>
        inline double accelerate(double speed) const { 
            return speed_coeff * speed; 
        }

        /// <summary> 
        /// Default transformation of acceleration -> mouse input multipliers.
        /// </summary>
        inline vec2d scale(double accel_val) const {
            return {
                weight.x * accel_val + 1,
                weight.y * accel_val + 1
            };
        }

        /// <summary>
        /// Verifies arguments as valid. Errors if not.
        /// </summary>
        /// <param name="args">Arguments to verified.</param>
        void verify(const accel_args& args) const {
            if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate");
        }

        accel_base() = default;
    };

}