RectSelectionTool.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Animation
  4. {
  5. internal class RectSelectionTool<T>
  6. {
  7. private int m_HashCode = "RectSelectionTool".GetHashCode();
  8. private int m_ControlID = -1;
  9. private bool m_Moved = false;
  10. private RectSlider m_RectSlider = new RectSlider();
  11. public int controlID { get { return m_ControlID; } }
  12. public IRectSelector<T> rectSelector { get; set; }
  13. public ICacheUndo cacheUndo { get; set; }
  14. public Action onSelectionStart = () => {};
  15. public Action onSelectionUpdate = () => {};
  16. public Action onSelectionEnd = () => {};
  17. public void OnGUI()
  18. {
  19. Debug.Assert(rectSelector != null);
  20. Debug.Assert(cacheUndo != null);
  21. m_ControlID = GUIUtility.GetControlID(m_HashCode, FocusType.Passive);
  22. Event ev = Event.current;
  23. EventType eventType = ev.GetTypeForControl(m_ControlID);
  24. if (GUIUtility.hotControl == 0 && HandleUtility.nearestControl == m_ControlID &&
  25. rectSelector.selection.Count > 0 && eventType == EventType.MouseDown && ev.button == 0 && !ev.alt)
  26. {
  27. m_Moved = false;
  28. onSelectionStart();
  29. }
  30. if (m_Moved && GUIUtility.hotControl == m_ControlID && eventType == EventType.MouseUp && ev.button == 0)
  31. {
  32. cacheUndo.BeginUndoOperation(TextContent.selection);
  33. rectSelector.selection.EndSelection(true);
  34. onSelectionEnd();
  35. }
  36. EditorGUI.BeginChangeCheck();
  37. rectSelector.rect = m_RectSlider.Do(m_ControlID);
  38. if (EditorGUI.EndChangeCheck())
  39. {
  40. if(!m_Moved)
  41. {
  42. cacheUndo.BeginUndoOperation(TextContent.selection);
  43. if(!ev.shift)
  44. rectSelector.selection.Clear();
  45. m_Moved = true;
  46. }
  47. rectSelector.selection.BeginSelection();
  48. rectSelector.Select();
  49. onSelectionUpdate();
  50. }
  51. if (eventType == EventType.Repaint && GUIUtility.hotControl == m_ControlID)
  52. {
  53. DrawingUtility.DrawRect(rectSelector.rect, Vector3.zero, Quaternion.identity, new Color(0f, 1f, 1f, 1f), 0.05f, 0.8f);
  54. }
  55. }
  56. }
  57. }