Comparers.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections.Generic;
  2. namespace UnityEngine.InputSystem.Utilities
  3. {
  4. /// <summary>
  5. /// Compare two <see cref="Vector2"/> by magnitude.
  6. /// </summary>
  7. /// <example>
  8. /// <code>
  9. /// </code>
  10. /// public class CompositeWithVector2Part : InputBindingComposite&lt;Vector2&gt;
  11. /// {
  12. /// [InputControl(layout = "Vector2")]
  13. /// public int part;
  14. ///
  15. /// public override Vector2 ReadValue(ref InputBindingCompositeContext context)
  16. /// {
  17. /// // Return the Vector3 with the greatest magnitude.
  18. /// return context.ReadValue&lt;Vector2, Vector2MagnitudeComparer&gt;(part);
  19. /// }
  20. /// }
  21. /// </example>
  22. public struct Vector2MagnitudeComparer : IComparer<Vector2>
  23. {
  24. public int Compare(Vector2 x, Vector2 y)
  25. {
  26. var lenx = x.sqrMagnitude;
  27. var leny = y.sqrMagnitude;
  28. if (lenx < leny)
  29. return -1;
  30. if (lenx > leny)
  31. return 1;
  32. return 0;
  33. }
  34. }
  35. /// <summary>
  36. /// Compare two <see cref="Vector3"/> by magnitude.
  37. /// </summary>
  38. /// <example>
  39. /// <code>
  40. /// </code>
  41. /// public class CompositeWithVector3Part : InputBindingComposite&lt;Vector3&gt;
  42. /// {
  43. /// [InputControl(layout = "Vector3")]
  44. /// public int part;
  45. ///
  46. /// public override Vector3 ReadValue(ref InputBindingCompositeContext context)
  47. /// {
  48. /// // Return the Vector3 with the greatest magnitude.
  49. /// return context.ReadValue&lt;Vector3, Vector2MagnitudeComparer&gt;(part);
  50. /// }
  51. /// }
  52. /// </example>
  53. public struct Vector3MagnitudeComparer : IComparer<Vector3>
  54. {
  55. public int Compare(Vector3 x, Vector3 y)
  56. {
  57. var lenx = x.sqrMagnitude;
  58. var leny = y.sqrMagnitude;
  59. if (lenx < leny)
  60. return -1;
  61. if (lenx > leny)
  62. return 1;
  63. return 0;
  64. }
  65. }
  66. }