leafwing_input_manager/input_processing/dual_axis/mod.rs
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
//! Processors for dual-axis input values
use std::hash::{Hash, Hasher};
use bevy::{
math::FloatOrd,
prelude::{BVec2, Reflect, Vec2},
};
use serde::{Deserialize, Serialize};
use crate::input_processing::AxisProcessor;
pub use self::circle::*;
pub use self::custom::*;
pub use self::range::*;
mod circle;
mod custom;
mod range;
/// A processor for dual-axis input values,
/// accepting a [`Vec2`] input and producing a [`Vec2`] output.
#[must_use]
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
pub enum DualAxisProcessor {
/// Converts input values into three discrete values along each axis,
/// similar to [`Vec2::signum()`] but returning `0.0` for zero values.
///
/// ```rust
/// use bevy::prelude::*;
/// use leafwing_input_manager::prelude::*;
///
/// // 1.0 for positive values
/// assert_eq!(DualAxisProcessor::Digital.process(Vec2::splat(2.5)), Vec2::ONE);
/// assert_eq!(DualAxisProcessor::Digital.process(Vec2::splat(0.5)), Vec2::ONE);
///
/// // 0.0 for zero values
/// assert_eq!(DualAxisProcessor::Digital.process(Vec2::ZERO), Vec2::ZERO);
/// assert_eq!(DualAxisProcessor::Digital.process(-Vec2::ZERO), Vec2::ZERO);
///
/// // -1.0 for negative values
/// assert_eq!(DualAxisProcessor::Digital.process(Vec2::splat(-0.5)), Vec2::NEG_ONE);
/// assert_eq!(DualAxisProcessor::Digital.process(Vec2::splat(-2.5)), Vec2::NEG_ONE);
///
/// // Mixed digital values
/// assert_eq!(DualAxisProcessor::Digital.process(Vec2::new(0.5, -0.5)), Vec2::new(1.0, -1.0));
/// assert_eq!(DualAxisProcessor::Digital.process(Vec2::new(-0.5, 0.5)), Vec2::new(-1.0, 1.0));
/// ```
Digital,
/// A wrapper around [`DualAxisInverted`] to represent inversion.
Inverted(DualAxisInverted),
/// A wrapper around [`DualAxisSensitivity`] to represent sensitivity.
Sensitivity(DualAxisSensitivity),
/// A wrapper around [`DualAxisBounds`] to represent value bounds.
ValueBounds(DualAxisBounds),
/// A wrapper around [`DualAxisExclusion`] to represent unscaled deadzone.
Exclusion(DualAxisExclusion),
/// A wrapper around [`DualAxisDeadZone`] to represent scaled deadzone.
DeadZone(DualAxisDeadZone),
/// A wrapper around [`CircleBounds`] to represent circular value bounds.
CircleBounds(CircleBounds),
/// A wrapper around [`CircleExclusion`] to represent unscaled deadzone.
CircleExclusion(CircleExclusion),
/// A wrapper around [`CircleDeadZone`] to represent scaled deadzone.
CircleDeadZone(CircleDeadZone),
/// A user-defined processor that implements [`CustomDualAxisProcessor`].
Custom(Box<dyn CustomDualAxisProcessor>),
}
impl DualAxisProcessor {
/// Computes the result by processing the `input_value`.
#[must_use]
#[inline]
pub fn process(&self, input_value: Vec2) -> Vec2 {
match self {
Self::Digital => Vec2::new(
AxisProcessor::Digital.process(input_value.x),
AxisProcessor::Digital.process(input_value.y),
),
Self::Inverted(inversion) => inversion.invert(input_value),
Self::Sensitivity(sensitivity) => sensitivity.scale(input_value),
Self::ValueBounds(bounds) => bounds.clamp(input_value),
Self::Exclusion(exclusion) => exclusion.exclude(input_value),
Self::DeadZone(deadzone) => deadzone.normalize(input_value),
Self::CircleBounds(bounds) => bounds.clamp(input_value),
Self::CircleExclusion(exclusion) => exclusion.exclude(input_value),
Self::CircleDeadZone(deadzone) => deadzone.normalize(input_value),
Self::Custom(processor) => processor.process(input_value),
}
}
}
/// Provides methods for configuring and manipulating the processing pipeline for dual-axis input.
pub trait WithDualAxisProcessingPipelineExt: Sized {
/// Resets the processing pipeline, removing any currently applied processors.
fn reset_processing_pipeline(self) -> Self;
/// Replaces the current processing pipeline with the given [`DualAxisProcessor`]s.
fn replace_processing_pipeline(
self,
processors: impl IntoIterator<Item = DualAxisProcessor>,
) -> Self;
/// Appends the given [`DualAxisProcessor`] as the next processing step.
fn with_processor(self, processor: impl Into<DualAxisProcessor>) -> Self;
/// Appends an [`DualAxisProcessor::Digital`] processor as the next processing step,
/// similar to [`Vec2::signum`] but returning `0.0` for zero values.
#[inline]
fn digital(self) -> Self {
self.with_processor(DualAxisProcessor::Digital)
}
/// Appends a [`DualAxisInverted::ALL`] processor as the next processing step,
/// flipping the sign of values on both axes.
#[inline]
fn inverted(self) -> Self {
self.with_processor(DualAxisInverted::ALL)
}
/// Appends a [`DualAxisInverted::ONLY_X`] processor as the next processing step,
/// only flipping the sign of the X-axis values.
#[inline]
fn inverted_x(self) -> Self {
self.with_processor(DualAxisInverted::ONLY_X)
}
/// Appends a [`DualAxisInverted::ONLY_Y`] processor as the next processing step,
/// only flipping the sign of the Y-axis values.
#[inline]
fn inverted_y(self) -> Self {
self.with_processor(DualAxisInverted::ONLY_Y)
}
/// Appends a [`DualAxisSensitivity`] processor as the next processing step,
/// multiplying values on both axes with the given sensitivity factor.
#[inline]
fn sensitivity(self, sensitivity: f32) -> Self {
self.with_processor(DualAxisSensitivity::all(sensitivity))
}
/// Appends a [`DualAxisSensitivity`] processor as the next processing step,
/// only multiplying the X-axis values with the given sensitivity factor.
#[inline]
fn sensitivity_x(self, sensitivity: f32) -> Self {
self.with_processor(DualAxisSensitivity::only_x(sensitivity))
}
/// Appends a [`DualAxisSensitivity`] processor as the next processing step,
/// only multiplying the Y-axis values with the given sensitivity factor.
#[inline]
fn sensitivity_y(self, sensitivity: f32) -> Self {
self.with_processor(DualAxisSensitivity::only_y(sensitivity))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting values within the same range `[min, max]` on both axes.
#[inline]
fn with_bounds(self, min: f32, max: f32) -> Self {
self.with_processor(DualAxisBounds::all(min, max))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting values within the same range `[-threshold, threshold]` on both axes.
#[inline]
fn with_bounds_symmetric(self, threshold: f32) -> Self {
self.with_processor(DualAxisBounds::symmetric_all(threshold))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// only restricting values within the range `[min, max]` on the X-axis.
#[inline]
fn with_bounds_x(self, min: f32, max: f32) -> Self {
self.with_processor(DualAxisBounds::only_x(min, max))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting values within the range `[-threshold, threshold]` on the X-axis.
#[inline]
fn with_bounds_x_symmetric(self, threshold: f32) -> Self {
self.with_processor(DualAxisBounds::symmetric_all(threshold))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// only restricting values within the range `[min, max]` on the Y-axis.
#[inline]
fn with_bounds_y(self, min: f32, max: f32) -> Self {
self.with_processor(DualAxisBounds::only_y(min, max))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting values within the range `[-threshold, threshold]` on the Y-axis.
#[inline]
fn with_bounds_y_symmetric(self, threshold: f32) -> Self {
self.with_processor(DualAxisBounds::symmetric_all(threshold))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting values to a minimum value on both axes.
#[inline]
fn at_least(self, min: f32) -> Self {
self.with_processor(DualAxisBounds::at_least_all(min))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting X values to a minimum value.
#[inline]
fn at_least_only_x(self, min: f32) -> Self {
self.with_processor(DualAxisBounds::at_least_only_x(min))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting Y values to a minimum value.
#[inline]
fn at_least_only_y(self, min: f32) -> Self {
self.with_processor(DualAxisBounds::at_least_only_y(min))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting values to a maximum value on both axes.
#[inline]
fn at_most(self, min: f32) -> Self {
self.with_processor(DualAxisBounds::at_most_all(min))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting X values to a maximum value.
#[inline]
fn at_most_only_x(self, min: f32) -> Self {
self.with_processor(DualAxisBounds::at_most_only_x(min))
}
/// Appends a [`DualAxisBounds`] processor as the next processing step,
/// restricting Y values to a maximum value.
#[inline]
fn at_most_only_y(self, min: f32) -> Self {
self.with_processor(DualAxisBounds::at_most_only_y(min))
}
/// Appends a [`CircleBounds`] processor as the next processing step,
/// restricting values to a `max` magnitude.
///
/// # Requirements
///
/// - `max` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_circle_bounds(self, max: f32) -> Self {
self.with_processor(CircleBounds::new(max))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// excluding values within the dead zone range `[negative_max, positive_min]` on both axes,
/// treating them as zeros, then normalizing non-excluded input values into the "live zone",
/// the remaining range within the [`DualAxisBounds::symmetric_all(1.0)`](DualAxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(DualAxisDeadZone::all(negative_max, positive_min))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// excluding values within the dead zone range `[-threshold, threshold]` on both axes,
/// treating them as zeros, then normalizing non-excluded input values into the "live zone",
/// the remaining range within the [`DualAxisBounds::symmetric_all(1.0)`](DualAxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_symmetric(self, threshold: f32) -> Self {
self.with_processor(DualAxisDeadZone::symmetric_all(threshold))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// only passing positive values that greater than `positive_min` on both axes
/// and then normalizing them into the "live zone" range `[positive_min, 1.0]`.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive(self, positive_min: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_positive_all(positive_min))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// only passing negative values that less than `negative_max` on both axes
/// and then normalizing them into the "live zone" range `[-1.0, negative_max]`.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative(self, negative_max: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_negative_all(negative_max))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// excluding values within the range `[negative_max, positive_min]` on the X-axis,
/// treating them as zeros, then normalizing non-excluded X values into the "live zone",
/// the remaining range within the [`AxisBounds::symmetric(1.0)`](super::AxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_x(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_x(negative_max, positive_min))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// excluding values within the range `[-threshold, threshold]` on the X-axis,
/// treating them as zeros, then normalizing non-excluded X values into the "live zone",
/// the remaining range within the [`AxisBounds::symmetric(1.0)`](super::AxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_x_symmetric(self, threshold: f32) -> Self {
self.with_processor(DualAxisDeadZone::symmetric_only_x(threshold))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// only excluding X values that less than or equal to `positive_min`, treating them as zeros
/// and then normalizing non-excluded X values into the "live zone" range `[positive_min, 1.0]`.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive_x(self, positive_min: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_positive_x(positive_min))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// only excluding X values that greater than or equal to `negative_max`, treating them as zeros
/// and then normalizing non-excluded X values into the "live zone" range `[-1.0, negative_max]`.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative_x(self, negative_max: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_negative_x(negative_max))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// excluding values within the range `[negative_max, positive_min]` on the Y-axis,
/// treating them as zeros, then normalizing non-excluded Y values into the "live zone",
/// the remaining range within the [`AxisBounds::symmetric(1.0)`](super::AxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_y(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_y(negative_max, positive_min))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// excluding values within the range `[-threshold, threshold]` on the Y-axis,
/// treating them as zeros, then normalizing non-excluded Y values into the "live zone",
/// the remaining range within the [`AxisBounds::symmetric(1.0)`](super::AxisBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_y_symmetric(self, threshold: f32) -> Self {
self.with_processor(DualAxisDeadZone::symmetric_only_y(threshold))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// only excluding Y values that less than or equal to `positive_min`, treating them as zeros
/// and then normalizing non-excluded Y values into the range `[positive_min, 1.0]`.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive_y(self, positive_min: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_positive_y(positive_min))
}
/// Appends a [`DualAxisDeadZone`] processor as the next processing step,
/// only excluding Y values that greater than or equal to `negative_max`, treating them as zeros
/// and then normalizing non-excluded Y values into the range `[-1.0, negative_max]`.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative_y(self, negative_max: f32) -> Self {
self.with_processor(DualAxisDeadZone::only_negative_y(negative_max))
}
/// Appends a [`CircleDeadZone`] processor as the next processing step,
/// ignoring values below a `min` magnitude, treating them as zeros,
/// then normalizing non-excluded input values into the "live zone",
/// the remaining range within the [`CircleBounds::new(1.0)`](CircleBounds::default)
/// after dead zone exclusion.
///
/// # Requirements
///
/// - `min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_circle_deadzone(self, min: f32) -> Self {
self.with_processor(CircleDeadZone::new(min))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// ignoring values within the range `[negative_max, positive_min]` on both axes,
/// treating them as zeros.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_unscaled(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(DualAxisExclusion::all(negative_max, positive_min))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// ignoring values within the range `[-threshold, threshold]` on both axes,
/// treating them as zeros.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_symmetric_unscaled(self, threshold: f32) -> Self {
self.with_processor(DualAxisExclusion::symmetric_all(threshold))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only passing positive values that greater than `positive_min` on both axes,
/// treating them as zeros.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive_unscaled(self, positive_min: f32) -> Self {
self.with_processor(DualAxisExclusion::only_positive_all(positive_min))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only passing negative values that less than `negative_max` on both axes,
/// treating them as zeros.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative_unscaled(self, negative_max: f32) -> Self {
self.with_processor(DualAxisExclusion::only_negative_all(negative_max))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only ignoring values within the range `[negative_max, positive_min]` on the X-axis,
/// treating them as zeros.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_x_unscaled(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(DualAxisExclusion::only_x(negative_max, positive_min))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only ignoring values within the range `[-threshold, threshold]` on the X-axis,
/// treating them as zeros.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_x_symmetric_unscaled(self, threshold: f32) -> Self {
self.with_processor(DualAxisExclusion::symmetric_only_x(threshold))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only excluding X values that less than or equal to `positive_min`,
/// treating them as zeros.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive_x_unscaled(self, positive_min: f32) -> Self {
self.with_processor(DualAxisExclusion::only_positive_x(positive_min))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only excluding X values that greater than or equal to `negative_max`,
/// treating them as zeros.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative_x_unscaled(self, negative_max: f32) -> Self {
self.with_processor(DualAxisExclusion::only_negative_x(negative_max))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only ignoring values within the range `[negative_max, positive_min]` on the Y-axis,
/// treating them as zeros.
///
/// # Requirements
///
/// - `negative_max` <= `0.0` <= `positive_min`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_y_unscaled(self, negative_max: f32, positive_min: f32) -> Self {
self.with_processor(DualAxisExclusion::only_y(negative_max, positive_min))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only ignoring values within the range `[-threshold, threshold]` on the Y-axis,
/// treating them as zeros.
///
/// # Requirements
///
/// - `threshold` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_deadzone_y_symmetric_unscaled(self, threshold: f32) -> Self {
self.with_processor(DualAxisExclusion::symmetric_only_y(threshold))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only excluding Y values that less than or equal to `positive_min`,
/// treating them as zeros.
///
/// # Requirements
///
/// - `positive_min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_positive_y_unscaled(self, positive_min: f32) -> Self {
self.with_processor(DualAxisExclusion::only_positive_y(positive_min))
}
/// Appends a [`DualAxisExclusion`] processor as the next processing step,
/// only excluding Y values that greater than or equal to `negative_max`,
/// treating them as zeros.
///
/// # Requirements
///
/// - `negative_max` <= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn only_negative_y_unscaled(self, negative_max: f32) -> Self {
self.with_processor(DualAxisExclusion::only_negative_y(negative_max))
}
/// Appends a [`CircleExclusion`] processor as the next processing step,
/// ignoring values below a `min` magnitude, treating them as zeros.
///
/// # Requirements
///
/// - `min` >= `0.0`.
///
/// # Panics
///
/// Panics if the requirements aren't met.
#[inline]
fn with_circle_deadzone_unscaled(self, min: f32) -> Self {
self.with_processor(CircleExclusion::new(min))
}
}
/// Flips the sign of dual-axis input values, resulting in a directional reversal of control.
///
/// ```rust
/// use bevy::prelude::*;
/// use leafwing_input_manager::prelude::*;
///
/// let value = Vec2::new(1.5, 2.0);
/// let Vec2 { x, y } = value;
///
/// assert_eq!(DualAxisInverted::ALL.invert(value), -value);
/// assert_eq!(DualAxisInverted::ALL.invert(-value), value);
///
/// assert_eq!(DualAxisInverted::ONLY_X.invert(value), Vec2::new(-x, y));
/// assert_eq!(DualAxisInverted::ONLY_X.invert(-value), Vec2::new(x, -y));
///
/// assert_eq!(DualAxisInverted::ONLY_Y.invert(value), Vec2::new(x, -y));
/// assert_eq!(DualAxisInverted::ONLY_Y.invert(-value), Vec2::new(-x, y));
#[derive(Debug, Clone, Copy, PartialEq, Reflect, Serialize, Deserialize)]
#[must_use]
pub struct DualAxisInverted(Vec2);
impl DualAxisInverted {
/// The [`DualAxisInverted`] that inverts both axes.
pub const ALL: Self = Self(Vec2::NEG_ONE);
/// The [`DualAxisInverted`] that only inverts the X-axis inputs.
pub const ONLY_X: Self = Self(Vec2::new(-1.0, 1.0));
/// The [`DualAxisInverted`] that only inverts the Y-axis inputs.
pub const ONLY_Y: Self = Self(Vec2::new(1.0, -1.0));
/// Are inputs inverted on both axes?
#[must_use]
#[inline]
pub fn inverted(&self) -> BVec2 {
self.0.cmpeq(Vec2::NEG_ONE)
}
/// Multiples the `input_value` by the specified inversion vector.
#[must_use]
#[inline]
pub fn invert(&self, input_value: Vec2) -> Vec2 {
self.0 * input_value
}
}
impl From<DualAxisInverted> for DualAxisProcessor {
fn from(value: DualAxisInverted) -> Self {
Self::Inverted(value)
}
}
impl Eq for DualAxisInverted {}
impl Hash for DualAxisInverted {
fn hash<H: Hasher>(&self, state: &mut H) {
FloatOrd(self.0.x).hash(state);
FloatOrd(self.0.y).hash(state);
}
}
/// Scales dual-axis input values using a specified multiplier to fine-tune the responsiveness of control.
///
/// ```rust
/// use bevy::prelude::*;
/// use leafwing_input_manager::prelude::*;
///
/// let value = Vec2::new(1.5, 2.5);
/// let Vec2 { x, y } = value;
///
/// // Negated X and halved Y
/// let neg_x_half_y = DualAxisSensitivity::new(-1.0, 0.5);
/// assert_eq!(neg_x_half_y.scale(value).x, -x);
/// assert_eq!(neg_x_half_y.scale(value).y, 0.5 * y);
///
/// // Doubled X and doubled Y
/// let double = DualAxisSensitivity::all(2.0);
/// assert_eq!(double.scale(value), 2.0 * value);
///
/// // Halved X
/// let half_x = DualAxisSensitivity::only_x(0.5);
/// assert_eq!(half_x.scale(value).x, 0.5 * x);
/// assert_eq!(half_x.scale(value).y, y);
///
/// // Negated and doubled Y
/// let neg_double_y = DualAxisSensitivity::only_y(-2.0);
/// assert_eq!(neg_double_y.scale(value).x, x);
/// assert_eq!(neg_double_y.scale(value).y, -2.0 * y);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Reflect, Serialize, Deserialize)]
#[must_use]
pub struct DualAxisSensitivity(pub(crate) Vec2);
impl DualAxisSensitivity {
/// Creates a [`DualAxisSensitivity`] with the given values for each axis separately.
#[inline]
pub const fn new(sensitivity_x: f32, sensitivity_y: f32) -> Self {
Self(Vec2::new(sensitivity_x, sensitivity_y))
}
/// Creates a [`DualAxisSensitivity`] with the same value for both axes.
#[inline]
pub const fn all(sensitivity: f32) -> Self {
Self::new(sensitivity, sensitivity)
}
/// Creates a [`DualAxisSensitivity`] that only affects the X-axis using the given value.
#[inline]
pub const fn only_x(sensitivity: f32) -> Self {
Self::new(sensitivity, 1.0)
}
/// Creates a [`DualAxisSensitivity`] that only affects the Y-axis using the given value.
#[inline]
pub const fn only_y(sensitivity: f32) -> Self {
Self::new(1.0, sensitivity)
}
/// Returns the sensitivity values.
#[must_use]
#[inline]
pub fn sensitivities(&self) -> Vec2 {
self.0
}
/// Multiples the `input_value` by the specified sensitivity vector.
#[must_use]
#[inline]
pub fn scale(&self, input_value: Vec2) -> Vec2 {
self.0 * input_value
}
}
impl From<DualAxisSensitivity> for DualAxisProcessor {
fn from(value: DualAxisSensitivity) -> Self {
Self::Sensitivity(value)
}
}
impl Eq for DualAxisSensitivity {}
impl Hash for DualAxisSensitivity {
fn hash<H: Hasher>(&self, state: &mut H) {
FloatOrd(self.0.x).hash(state);
FloatOrd(self.0.y).hash(state);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dual_axis_inverted() {
let all = DualAxisInverted::ALL;
assert_eq!(all.inverted(), BVec2::TRUE);
let only_x = DualAxisInverted::ONLY_X;
assert_eq!(only_x.inverted(), BVec2::new(true, false));
let only_y = DualAxisInverted::ONLY_Y;
assert_eq!(only_y.inverted(), BVec2::new(false, true));
for x in -300..300 {
let x = x as f32 * 0.01;
for y in -300..300 {
let y = y as f32 * 0.01;
let value = Vec2::new(x, y);
let processor = DualAxisProcessor::Inverted(all);
assert_eq!(DualAxisProcessor::from(all), processor);
assert_eq!(processor.process(value), all.invert(value));
assert_eq!(all.invert(value), -value);
assert_eq!(all.invert(-value), value);
let processor = DualAxisProcessor::Inverted(only_x);
assert_eq!(DualAxisProcessor::from(only_x), processor);
assert_eq!(processor.process(value), only_x.invert(value));
assert_eq!(only_x.invert(value), Vec2::new(-x, y));
assert_eq!(only_x.invert(-value), Vec2::new(x, -y));
let processor = DualAxisProcessor::Inverted(only_y);
assert_eq!(DualAxisProcessor::from(only_y), processor);
assert_eq!(processor.process(value), only_y.invert(value));
assert_eq!(only_y.invert(value), Vec2::new(x, -y));
assert_eq!(only_y.invert(-value), Vec2::new(-x, y));
}
}
}
#[test]
fn test_dual_axis_sensitivity() {
for x in -300..300 {
let x = x as f32 * 0.01;
for y in -300..300 {
let y = y as f32 * 0.01;
let value = Vec2::new(x, y);
let sensitivity = x;
let all = DualAxisSensitivity::all(sensitivity);
let processor = DualAxisProcessor::Sensitivity(all);
assert_eq!(DualAxisProcessor::from(all), processor);
assert_eq!(processor.process(value), all.scale(value));
assert_eq!(all.sensitivities(), Vec2::splat(sensitivity));
assert_eq!(all.scale(value), sensitivity * value);
let only_x = DualAxisSensitivity::only_x(sensitivity);
let processor = DualAxisProcessor::Sensitivity(only_x);
assert_eq!(DualAxisProcessor::from(only_x), processor);
assert_eq!(processor.process(value), only_x.scale(value));
assert_eq!(only_x.sensitivities(), Vec2::new(sensitivity, 1.0));
assert_eq!(only_x.scale(value).x, x * sensitivity);
assert_eq!(only_x.scale(value).y, y);
let only_y = DualAxisSensitivity::only_y(sensitivity);
let processor = DualAxisProcessor::Sensitivity(only_y);
assert_eq!(DualAxisProcessor::from(only_y), processor);
assert_eq!(processor.process(value), only_y.scale(value));
assert_eq!(only_y.sensitivities(), Vec2::new(1.0, sensitivity));
assert_eq!(only_y.scale(value).x, x);
assert_eq!(only_y.scale(value).y, y * sensitivity);
let sensitivity2 = y;
let separate = DualAxisSensitivity::new(sensitivity, sensitivity2);
let processor = DualAxisProcessor::Sensitivity(separate);
assert_eq!(DualAxisProcessor::from(separate), processor);
assert_eq!(processor.process(value), separate.scale(value));
assert_eq!(
separate.sensitivities(),
Vec2::new(sensitivity, sensitivity2)
);
assert_eq!(separate.scale(value).x, x * sensitivity);
assert_eq!(separate.scale(value).y, y * sensitivity2);
}
}
}
}