GuiControl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #if UNITY_EDITOR
  3. using UnityEngine;
  4. namespace Animancer.Editor
  5. {
  6. /// <summary>[Editor-Only] Utility for implementing IMGUI controls.</summary>
  7. /// <remarks>
  8. /// <strong>Example:</strong>
  9. /// <code>
  10. /// private static readonly int ControlHash = "ControlName".GetHashCode();
  11. ///
  12. /// void OnGUI(Rect area)
  13. /// {
  14. /// var control = new GUIControl(area, ControlHash);
  15. ///
  16. /// switch (control.EventType)
  17. /// {
  18. /// case EventType.MouseDown:
  19. /// if (control.TryUseMouseDown())
  20. /// {
  21. /// }
  22. /// break;
  23. ///
  24. /// case EventType.MouseUp:
  25. /// if (control.TryUseMouseUp())
  26. /// {
  27. /// }
  28. /// break;
  29. ///
  30. /// case EventType.MouseDrag:
  31. /// if (control.TryUseHotControl())
  32. /// {
  33. /// }
  34. /// break;
  35. /// }
  36. /// }
  37. /// </code></remarks>
  38. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/GUIControl
  39. public readonly struct GUIControl
  40. {
  41. /************************************************************************************************************************/
  42. /// <summary>The position and size of this control</summary>
  43. public readonly Rect Area;
  44. /// <summary>The Control ID of this control.</summary>
  45. public readonly int ID;
  46. /// <summary>The event being processed by this control.</summary>
  47. public readonly Event Event;
  48. /************************************************************************************************************************/
  49. /// <summary>The type of the <see cref="Event"/> in relation to this control.</summary>
  50. public EventType EventType
  51. => Event.GetTypeForControl(ID);
  52. /// <summary>Does the <see cref="Area"/> contain the <see cref="Event.mousePosition"/>?</summary>
  53. public bool ContainsMousePosition
  54. => Area.Contains(Event.mousePosition);
  55. /************************************************************************************************************************/
  56. /// <summary>Creaates a new <see cref="GUIControl"/>.</summary>
  57. public GUIControl(Rect area, Event currentEvent, int idHint, FocusType focusType = FocusType.Passive)
  58. {
  59. Area = area;
  60. Event = currentEvent;
  61. ID = GUIUtility.GetControlID(idHint, focusType, area);
  62. }
  63. /// <summary>Creaates a new <see cref="GUIControl"/> with the <see cref="Event.current"/>.</summary>
  64. public GUIControl(Rect area, int idHint, FocusType focusType = FocusType.Passive)
  65. : this(area, Event.current, idHint, focusType)
  66. {
  67. }
  68. /************************************************************************************************************************/
  69. /// <summary><see cref="AnimancerGUI.TryUseMouseDown"/></summary>
  70. public bool TryUseMouseDown()
  71. => AnimancerGUI.TryUseMouseDown(Area, Event, ID);
  72. /// <summary><see cref="AnimancerGUI.TryUseMouseUp"/></summary>
  73. public bool TryUseMouseUp(bool guiChanged = false)
  74. => AnimancerGUI.TryUseMouseUp(Event, ID, guiChanged);
  75. /// <summary><see cref="AnimancerGUI.TryUseHotControl"/></summary>
  76. public bool TryUseHotControl(bool guiChanged = true)
  77. => AnimancerGUI.TryUseHotControl(Event, ID, guiChanged);
  78. /// <summary><see cref="AnimancerGUI.TryUseKey"/></summary>
  79. public bool TryUseKey(KeyCode key = KeyCode.None)
  80. => AnimancerGUI.TryUseKey(Event, ID, key);
  81. /************************************************************************************************************************/
  82. }
  83. }
  84. #endif