@@ -42,6 +42,26 @@ namespace rawaccel {
42
42
rotator () = default ;
43
43
};
44
44
45
+ struct snapper {
46
+ double threshold = 0 ;
47
+
48
+ inline vec2d apply (const vec2d& input) const {
49
+ if (input.x != 0 && input.y != 0 ) {
50
+ double angle = fabs (atan (input.y / input.x ));
51
+ auto mag = [&] { return sqrtsd (input.x * input.x + input.y * input.y ); };
52
+
53
+ if (angle > M_PI_2 - threshold) return { 0 , _copysign (mag (), input.y ) };
54
+ if (angle < threshold) return { _copysign (mag (), input.x ), 0 };
55
+ }
56
+
57
+ return input;
58
+ }
59
+
60
+ snapper (double degrees) : threshold(minsd(fabs(degrees), 45 ) * M_PI / 180 ) {}
61
+
62
+ snapper () = default ;
63
+ };
64
+
45
65
// / <summary> Struct to hold clamp (min and max) details for acceleration application </summary>
46
66
struct accel_scale_clamp {
47
67
double lo = 0 ;
@@ -232,7 +252,7 @@ namespace rawaccel {
232
252
double sigma_x = 1.0 ;
233
253
double sigma_y = 1.0 ;
234
254
235
- weighted_distance (domain_args args)
255
+ weighted_distance (const domain_args& args)
236
256
{
237
257
sigma_x = args.domain_weights .x ;
238
258
sigma_y = args.domain_weights .y ;
@@ -250,19 +270,18 @@ namespace rawaccel {
250
270
}
251
271
}
252
272
253
- double calculate (double x, double y)
273
+ inline double calculate (double x, double y)
254
274
{
255
275
double abs_x = fabs (x);
256
276
double abs_y = fabs (y);
257
277
258
- if (lp_norm_infinity)
259
- {
260
- return abs_x > abs_y ? abs_x : abs_y;
261
- }
278
+ if (lp_norm_infinity) return maxsd (abs_x, abs_y);
262
279
263
280
double x_scaled = abs_x * sigma_x;
264
281
double y_scaled = abs_y * sigma_y;
265
- return pow (pow (x_scaled, p) + pow (y_scaled, p), p_inverse);
282
+
283
+ if (p == 2 ) return sqrtsd (x_scaled * x_scaled + y_scaled * y_scaled);
284
+ else return pow (pow (x_scaled, p) + pow (y_scaled, p), p_inverse);
266
285
}
267
286
268
287
weighted_distance () = default ;
@@ -273,27 +292,20 @@ namespace rawaccel {
273
292
double start = 1.0 ;
274
293
bool should_apply = false ;
275
294
276
- direction_weight (vec2d thetas)
295
+ direction_weight (const vec2d& thetas)
277
296
{
278
297
diff = thetas.y - thetas.x ;
279
298
start = thetas.x ;
280
299
281
- if (diff != 0 )
282
- {
283
- should_apply = true ;
284
- }
285
- else
286
- {
287
- should_apply = false ;
288
- }
300
+ should_apply = diff != 0 ;
289
301
}
290
302
291
303
inline double atan_scale (double x, double y)
292
304
{
293
305
return M_2_PI * atan2 (fabs (y), fabs (x));
294
306
}
295
307
296
- double apply (double x, double y)
308
+ inline double apply (double x, double y)
297
309
{
298
310
return atan_scale (x, y) * diff + start;
299
311
}
@@ -304,9 +316,11 @@ namespace rawaccel {
304
316
// / <summary> Struct to hold variables and methods for modifying mouse input </summary>
305
317
struct mouse_modifier {
306
318
bool apply_rotate = false ;
319
+ bool apply_snap = false ;
307
320
bool apply_accel = false ;
308
321
bool combine_magnitudes = true ;
309
322
rotator rotate;
323
+ snapper snap;
310
324
weighted_distance distance;
311
325
direction_weight directional;
312
326
vec2<accelerator> accels;
@@ -321,6 +335,11 @@ namespace rawaccel {
321
335
apply_rotate = true ;
322
336
}
323
337
338
+ if (args.degrees_snap != 0 ) {
339
+ snap = snapper (args.degrees_snap );
340
+ apply_snap = true ;
341
+ }
342
+
324
343
if (args.sens .x != 0 ) sensitivity.x = args.sens .x ;
325
344
if (args.sens .y != 0 ) sensitivity.y = args.sens .y ;
326
345
@@ -344,6 +363,7 @@ namespace rawaccel {
344
363
345
364
void modify (vec2d& movement, milliseconds time) {
346
365
apply_rotation (movement);
366
+ apply_angle_snap (movement);
347
367
apply_acceleration (movement, [=] { return time; });
348
368
apply_sensitivity (movement);
349
369
}
@@ -352,6 +372,10 @@ namespace rawaccel {
352
372
if (apply_rotate) movement = rotate.apply (movement);
353
373
}
354
374
375
+ inline void apply_angle_snap (vec2d& movement) {
376
+ if (apply_snap) movement = snap.apply (movement);
377
+ }
378
+
355
379
template <typename TimeSupplier>
356
380
inline void apply_acceleration (vec2d& movement, TimeSupplier time_supp) {
357
381
if (apply_accel) {
0 commit comments