Brush.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal class Brush
  6. {
  7. private static readonly float kWheelSizeSpeed = 1f;
  8. private static readonly int kBrushHashCode = "Brush".GetHashCode();
  9. private IGUIWrapper m_GUIWrapper;
  10. private float m_DeltaAcc = 0f;
  11. private int m_ControlID = -1;
  12. private SliderData m_SliderData = SliderData.zero;
  13. public event Action<Brush> onMove = (b) => {};
  14. public event Action<Brush> onSize = (b) => {};
  15. public event Action<Brush> onRepaint = (b) => {};
  16. public event Action<Brush> onStrokeBegin = (b) => {};
  17. public event Action<Brush> onStrokeDelta = (b) => {};
  18. public event Action<Brush> onStrokeStep = (b) => {};
  19. public event Action<Brush> onStrokeEnd = (b) => {};
  20. public bool isHot
  21. {
  22. get { return m_GUIWrapper.IsControlHot(m_ControlID); }
  23. }
  24. public bool isActivable
  25. {
  26. get { return m_GUIWrapper.IsControlHot(0) && m_GUIWrapper.IsControlNearest(m_ControlID); }
  27. }
  28. public int controlID
  29. {
  30. get { return m_ControlID; }
  31. }
  32. public float hardness { get; set; }
  33. public float step { get; set; }
  34. public float size { get; set; }
  35. public Vector3 position
  36. {
  37. get { return m_SliderData.position; }
  38. }
  39. public Brush(IGUIWrapper guiWrapper)
  40. {
  41. m_GUIWrapper = guiWrapper;
  42. size = 25f;
  43. step = 20f;
  44. }
  45. public void OnGUI()
  46. {
  47. m_ControlID = m_GUIWrapper.GetControlID(kBrushHashCode, FocusType.Passive);
  48. var eventType = m_GUIWrapper.eventType;
  49. if (!m_GUIWrapper.isAltDown)
  50. m_GUIWrapper.LayoutControl(controlID, 0f);
  51. if (isActivable)
  52. {
  53. m_SliderData.position = m_GUIWrapper.GUIToWorld(m_GUIWrapper.mousePosition);
  54. if (m_GUIWrapper.IsMouseDown(0))
  55. {
  56. m_DeltaAcc = 0f;
  57. onStrokeBegin(this);
  58. onStrokeStep(this);
  59. m_GUIWrapper.SetGuiChanged(true);
  60. }
  61. if (eventType == EventType.MouseMove)
  62. {
  63. onMove(this);
  64. m_GUIWrapper.UseCurrentEvent();
  65. }
  66. if (m_GUIWrapper.isShiftDown && eventType == EventType.ScrollWheel)
  67. {
  68. var sizeDelta = HandleUtility.niceMouseDeltaZoom * kWheelSizeSpeed;
  69. size = Mathf.Max(1f, size + sizeDelta);
  70. onSize(this);
  71. m_GUIWrapper.UseCurrentEvent();
  72. }
  73. }
  74. if (isHot && m_GUIWrapper.IsMouseUp(0))
  75. onStrokeEnd(this);
  76. if (m_GUIWrapper.IsRepainting() && (isHot || isActivable))
  77. onRepaint(this);
  78. Vector3 position;
  79. if (m_GUIWrapper.DoSlider(m_ControlID, m_SliderData, out position))
  80. {
  81. step = Mathf.Max(step, 1f);
  82. var delta = position - m_SliderData.position;
  83. var direction = delta.normalized;
  84. var magnitude = delta.magnitude;
  85. m_SliderData.position -= direction * m_DeltaAcc;
  86. m_DeltaAcc += magnitude;
  87. if (m_DeltaAcc >= step)
  88. {
  89. var stepVector = direction * step;
  90. while (m_DeltaAcc >= step)
  91. {
  92. m_SliderData.position += stepVector;
  93. onMove(this);
  94. onStrokeStep(this);
  95. m_DeltaAcc -= step;
  96. }
  97. }
  98. m_SliderData.position = position;
  99. onStrokeDelta(this);
  100. }
  101. }
  102. }
  103. }