diff options
| author | a1xd <[email protected]> | 2021-04-01 23:28:41 -0400 |
|---|---|---|
| committer | a1xd <[email protected]> | 2021-04-01 23:28:41 -0400 |
| commit | d8140fb31ba622f48756986d4d66db6b6ab8b511 (patch) | |
| tree | 8faa873d4468882c63f1f8fa02b94f4b6b3a65f6 /common | |
| parent | check for safe mode before hooking into dev stack (diff) | |
| download | rawaccel-d8140fb31ba622f48756986d4d66db6b6ab8b511.tar.xz rawaccel-d8140fb31ba622f48756986d4d66db6b6ab8b511.zip | |
use callbacks for applying accel
Diffstat (limited to 'common')
| -rw-r--r-- | common/accel-invoke.hpp | 43 | ||||
| -rw-r--r-- | common/accel-union.hpp | 80 | ||||
| -rw-r--r-- | common/common.vcxitems | 1 | ||||
| -rw-r--r-- | common/rawaccel.hpp | 21 | ||||
| -rw-r--r-- | common/utility.hpp | 7 |
5 files changed, 95 insertions, 57 deletions
diff --git a/common/accel-invoke.hpp b/common/accel-invoke.hpp new file mode 100644 index 0000000..0e264c1 --- /dev/null +++ b/common/accel-invoke.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include "accel-union.hpp" + +namespace rawaccel { + + class accel_invoker { + using callback_t = double (*)(const accel_union&, double, double); + + callback_t cb = &invoke_impl<accel_noaccel>; + + template <typename T> + static double invoke_impl(const accel_union& u, double x, double w) + { + return apply_weighted(reinterpret_cast<const T&>(u), x, w); + } + + public: + + accel_invoker(const accel_args& args) + { + cb = visit_accel([](auto&& arg) { + return &invoke_impl<remove_ref_t<decltype(arg)>>; + }, make_mode(args), accel_union{}); + } + + accel_invoker() = default; + + double invoke(const accel_union& u, double x, double weight = 1) const + { + return (*cb)(u, x, weight); + } + }; + + inline vec2<accel_invoker> invokers(const settings& args) + { + return { + accel_invoker(args.argsv.x), + accel_invoker(args.argsv.y) + }; + } + +} diff --git a/common/accel-union.hpp b/common/accel-union.hpp index 7a6b173..3d41a18 100644 --- a/common/accel-union.hpp +++ b/common/accel-union.hpp @@ -34,7 +34,7 @@ namespace rawaccel { default: return internal_mode::noaccel; } } - else if (mode < accel_mode{} || mode >= accel_mode::noaccel) { + else if (mode == accel_mode::noaccel) { return internal_mode::noaccel; } else { @@ -48,61 +48,49 @@ namespace rawaccel { return make_mode(args.mode, args.lut_args.mode, args.legacy); } - template <typename Visitor, typename Variant> - inline auto visit_accel(Visitor vis, Variant&& var) + template <typename Visitor, typename AccelUnion> + constexpr auto visit_accel(Visitor vis, internal_mode mode, AccelUnion&& u) { - switch (var.tag) { - case internal_mode::classic_lgcy: return vis(var.u.classic_l); - case internal_mode::classic_gain: return vis(var.u.classic_g); - case internal_mode::jump_lgcy: return vis(var.u.jump_l); - case internal_mode::jump_gain: return vis(var.u.jump_g); - case internal_mode::natural_lgcy: return vis(var.u.natural_l); - case internal_mode::natural_gain: return vis(var.u.natural_g); - case internal_mode::power_lgcy: return vis(var.u.power_l); - case internal_mode::power_gain: return vis(var.u.power_g); - case internal_mode::motivity_lgcy: return vis(var.u.motivity_l); - case internal_mode::motivity_gain: return vis(var.u.motivity_g); - case internal_mode::lut_log: return vis(var.u.log_lut); - case internal_mode::lut_lin: return vis(var.u.lin_lut); - default: return vis(var.u.noaccel); + switch (mode) { + case internal_mode::classic_lgcy: return vis(u.classic_l); + case internal_mode::classic_gain: return vis(u.classic_g); + case internal_mode::jump_lgcy: return vis(u.jump_l); + case internal_mode::jump_gain: return vis(u.jump_g); + case internal_mode::natural_lgcy: return vis(u.natural_l); + case internal_mode::natural_gain: return vis(u.natural_g); + case internal_mode::power_lgcy: return vis(u.power_l); + case internal_mode::power_gain: return vis(u.power_g); + case internal_mode::motivity_lgcy: return vis(u.motivity_l); + case internal_mode::motivity_gain: return vis(u.motivity_g); + case internal_mode::lut_log: return vis(u.log_lut); + case internal_mode::lut_lin: return vis(u.lin_lut); + default: return vis(u.noaccel); } } - struct accel_variant { - internal_mode tag = internal_mode::noaccel; + union accel_union { + classic classic_g; + classic_legacy classic_l; + jump jump_g; + jump_legacy jump_l; + natural natural_g; + natural_legacy natural_l; + power power_g; + power_legacy power_l; + sigmoid motivity_l; + motivity motivity_g; + linear_lut lin_lut; + binlog_lut log_lut; + accel_noaccel noaccel = {}; - union union_t { - classic classic_g; - classic_legacy classic_l; - jump jump_g; - jump_legacy jump_l; - natural natural_g; - natural_legacy natural_l; - power power_g; - power_legacy power_l; - sigmoid motivity_l; - motivity motivity_g; - linear_lut lin_lut; - binlog_lut log_lut; - accel_noaccel noaccel = {}; - } u = {}; - - accel_variant(const accel_args& args) : - tag(make_mode(args)) + accel_union(const accel_args& args) { visit_accel([&](auto& impl) { impl = { args }; - }, *this); - } - - double apply(double speed, double weight = 1) const - { - return visit_accel([=](auto&& impl) { - return apply_weighted(impl, speed, weight); - }, *this); + }, make_mode(args), *this); } - accel_variant() = default; + accel_union() = default; }; } diff --git a/common/common.vcxitems b/common/common.vcxitems index 6d1b861..2cf2df2 100644 --- a/common/common.vcxitems +++ b/common/common.vcxitems @@ -15,6 +15,7 @@ </ItemGroup> <ItemGroup> <ClInclude Include="$(MSBuildThisFileDirectory)accel-classic.hpp" /> + <ClInclude Include="$(MSBuildThisFileDirectory)accel-invoke.hpp" /> <ClInclude Include="$(MSBuildThisFileDirectory)accel-jump.hpp" /> <ClInclude Include="$(MSBuildThisFileDirectory)accel-lookup.hpp" /> <ClInclude Include="$(MSBuildThisFileDirectory)accel-motivity.hpp" /> diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index 8e10644..6710a0c 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -1,7 +1,6 @@ #pragma once -#include "accel-union.hpp" -#include "utility.hpp" +#include "accel-invoke.hpp" #define _USE_MATH_DEFINES #include <math.h> @@ -127,7 +126,7 @@ namespace rawaccel { double speed_max = 0; weighted_distance distance; vec2d range_weights = { 1, 1 }; - vec2<accel_variant> accels; + vec2<accel_union> accels; vec2d sensitivity = { 1, 1 }; vec2d directional_multipliers = {}; @@ -162,18 +161,18 @@ namespace rawaccel { return; } - accels.x = accel_variant(args.argsv.x); - accels.y = accel_variant(args.argsv.y); + 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, milliseconds time) { + void modify(vec2d& movement, const vec2<accel_invoker>& inv = {}, milliseconds time = 1) { apply_rotation(movement); apply_angle_snap(movement); - apply_acceleration(movement, time); + apply_acceleration(movement, inv, time); apply_sensitivity(movement); } @@ -185,7 +184,7 @@ namespace rawaccel { if (apply_snap) movement = snap.apply(movement); } - inline void apply_acceleration(vec2d& movement, milliseconds time) { + inline void apply_acceleration(vec2d& movement, const vec2<accel_invoker>& inv, milliseconds time) { double norm = dpi_factor / time; if (apply_speed_clamp) { @@ -205,18 +204,18 @@ namespace rawaccel { weight = range_weights.x; } - double scale = accels.x.apply(speed, weight); + 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 *= accels.x.apply(x, range_weights.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 *= accels.y.apply(y, range_weights.y); + movement.y *= inv.y.invoke(accels.y, y, range_weights.y); } } } diff --git a/common/utility.hpp b/common/utility.hpp index 5f5c186..a8e5f83 100644 --- a/common/utility.hpp +++ b/common/utility.hpp @@ -85,4 +85,11 @@ namespace rawaccel { template <typename... Ts> constexpr void operator()(Ts&&...) const noexcept {} }; + + template <typename T> struct remove_ref { using type = T; }; + template <typename T> struct remove_ref<T&> { using type = T; }; + template <typename T> struct remove_ref<T&&> { using type = T; }; + + template <typename T> + using remove_ref_t = typename remove_ref<T>::type; } |