GUISystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Path.GUIFramework
  6. {
  7. public class GUISystem
  8. {
  9. private readonly int kControlIDCheckHashCode = "ControlIDCheckHashCode".GetHashCode();
  10. private List<Control> m_Controls = new List<Control>();
  11. private List<GUIAction> m_Actions = new List<GUIAction>();
  12. private IGUIState m_GUIState;
  13. private int m_PrevNearestControl = -1;
  14. private LayoutData m_PrevNearestLayoutData = LayoutData.zero;
  15. private int m_ControlIDCheck = -1;
  16. public GUISystem(IGUIState guiState)
  17. {
  18. m_GUIState = guiState;
  19. }
  20. public void AddControl(Control control)
  21. {
  22. if (control == null)
  23. throw new NullReferenceException("Control is null");
  24. m_Controls.Add(control);
  25. }
  26. public void RemoveControl(Control control)
  27. {
  28. m_Controls.Remove(control);
  29. }
  30. public void AddAction(GUIAction action)
  31. {
  32. if (action == null)
  33. throw new NullReferenceException("Action is null");
  34. m_Actions.Add(action);
  35. }
  36. public void RemoveAction(GUIAction action)
  37. {
  38. m_Actions.Remove(action);
  39. }
  40. public void OnGUI()
  41. {
  42. var controlIDCheck = m_GUIState.GetControlID(kControlIDCheckHashCode, FocusType.Passive);
  43. if (m_GUIState.eventType == EventType.Layout)
  44. m_ControlIDCheck = controlIDCheck;
  45. else if (m_GUIState.eventType != EventType.Used && m_ControlIDCheck != controlIDCheck)
  46. Debug.LogWarning("GetControlID at event " + m_GUIState.eventType + " returns a controlID different from the one in Layout event");
  47. var nearestLayoutData = LayoutData.zero;
  48. foreach (var control in m_Controls)
  49. control.GetControl(m_GUIState);
  50. if (m_GUIState.eventType == EventType.Layout)
  51. {
  52. foreach (var control in m_Controls)
  53. control.BeginLayout(m_GUIState);
  54. foreach (var control in m_Controls)
  55. {
  56. control.Layout(m_GUIState);
  57. nearestLayoutData = LayoutData.Nearest(nearestLayoutData, control.layoutData);
  58. }
  59. foreach (var control in m_Controls)
  60. m_GUIState.AddControl(control.ID, control.layoutData.distance);
  61. foreach (var control in m_Controls)
  62. control.EndLayout(m_GUIState);
  63. if (m_PrevNearestControl == m_GUIState.nearestControl)
  64. {
  65. if (nearestLayoutData.index != m_PrevNearestLayoutData.index)
  66. m_GUIState.Repaint();
  67. }
  68. else
  69. {
  70. m_PrevNearestControl = m_GUIState.nearestControl;
  71. m_GUIState.Repaint();
  72. }
  73. m_PrevNearestLayoutData = nearestLayoutData;
  74. }
  75. if (m_GUIState.eventType == EventType.Repaint)
  76. {
  77. foreach (var action in m_Actions)
  78. if (action.IsRepaintEnabled(m_GUIState))
  79. action.PreRepaint(m_GUIState);
  80. foreach (var control in m_Controls)
  81. control.Repaint(m_GUIState);
  82. }
  83. var repaintOnMouseMove = false;
  84. foreach (var action in m_Actions)
  85. {
  86. if (IsMouseMoveEvent())
  87. repaintOnMouseMove |= action.IsRepaintOnMouseMoveEnabled(m_GUIState);
  88. action.OnGUI(m_GUIState);
  89. }
  90. if (repaintOnMouseMove)
  91. m_GUIState.Repaint();
  92. }
  93. private bool IsMouseMoveEvent()
  94. {
  95. return m_GUIState.eventType == EventType.MouseMove || m_GUIState.eventType == EventType.MouseDrag;
  96. }
  97. }
  98. }