暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

GUISystem.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UnityEditor.U2D.Path.GUIFramework
  6. {
  7. /// <summary>
  8. /// Represents a system of GUI elements and controls.
  9. /// </summary>
  10. public class GUISystem
  11. {
  12. private readonly int kControlIDCheckHashCode = "ControlIDCheckHashCode".GetHashCode();
  13. private List<Control> m_Controls = new List<Control>();
  14. private List<GUIAction> m_Actions = new List<GUIAction>();
  15. private IGUIState m_GUIState;
  16. private int m_PrevNearestControl = -1;
  17. private LayoutData m_PrevNearestLayoutData = LayoutData.zero;
  18. private int m_ControlIDCheck = -1;
  19. /// <summary>
  20. /// Initializes and returns an instance of GUISystem
  21. /// </summary>
  22. /// <param name="guiState">The current state of the custom editor.</param>
  23. public GUISystem(IGUIState guiState)
  24. {
  25. m_GUIState = guiState;
  26. }
  27. /// <summary>
  28. /// Adds a control to the internal list of controls.
  29. /// </summary>
  30. /// <param name="control">The control to add.</param>
  31. public void AddControl(Control control)
  32. {
  33. if (control == null)
  34. throw new NullReferenceException("Control is null");
  35. m_Controls.Add(control);
  36. }
  37. /// <summary>
  38. /// Removes a control from the internal list of controls.
  39. /// </summary>
  40. /// <param name="control">The control to remove.</param>
  41. public void RemoveControl(Control control)
  42. {
  43. m_Controls.Remove(control);
  44. }
  45. /// <summary>
  46. /// Adds an action to the internal list of actions.
  47. /// </summary>
  48. /// <param name="action">The action to add.</param>
  49. public void AddAction(GUIAction action)
  50. {
  51. if (action == null)
  52. throw new NullReferenceException("Action is null");
  53. m_Actions.Add(action);
  54. }
  55. /// <summary>
  56. /// Removes an action from the internal list of actions.
  57. /// </summary>
  58. /// <param name="action">The action to remove.</param>
  59. public void RemoveAction(GUIAction action)
  60. {
  61. m_Actions.Remove(action);
  62. }
  63. /// <summary>
  64. /// Calls the methods in its invocation list when Unity draws this GUISystems's GUI.
  65. /// </summary>
  66. public void OnGUI()
  67. {
  68. var controlIDCheck = m_GUIState.GetControlID(kControlIDCheckHashCode, FocusType.Passive);
  69. if (m_GUIState.eventType == EventType.Layout)
  70. m_ControlIDCheck = controlIDCheck;
  71. else if (m_GUIState.eventType != EventType.Used && m_ControlIDCheck != controlIDCheck)
  72. Debug.LogWarning("GetControlID at event " + m_GUIState.eventType + " returns a controlID different from the one in Layout event");
  73. var nearestLayoutData = LayoutData.zero;
  74. foreach (var control in m_Controls)
  75. control.GetControl(m_GUIState);
  76. if (m_GUIState.eventType == EventType.Layout)
  77. {
  78. foreach (var control in m_Controls)
  79. control.BeginLayout(m_GUIState);
  80. foreach (var control in m_Controls)
  81. {
  82. control.Layout(m_GUIState);
  83. nearestLayoutData = LayoutData.Nearest(nearestLayoutData, control.layoutData);
  84. }
  85. foreach (var control in m_Controls)
  86. m_GUIState.AddControl(control.ID, control.layoutData.distance);
  87. foreach (var control in m_Controls)
  88. control.EndLayout(m_GUIState);
  89. if (m_PrevNearestControl == m_GUIState.nearestControl)
  90. {
  91. if (nearestLayoutData.index != m_PrevNearestLayoutData.index)
  92. m_GUIState.Repaint();
  93. }
  94. else
  95. {
  96. m_PrevNearestControl = m_GUIState.nearestControl;
  97. m_GUIState.Repaint();
  98. }
  99. m_PrevNearestLayoutData = nearestLayoutData;
  100. }
  101. if (m_GUIState.eventType == EventType.Repaint)
  102. {
  103. foreach (var action in m_Actions)
  104. if (action.IsRepaintEnabled(m_GUIState))
  105. action.PreRepaint(m_GUIState);
  106. foreach (var control in m_Controls)
  107. control.Repaint(m_GUIState);
  108. }
  109. var repaintOnMouseMove = false;
  110. foreach (var action in m_Actions)
  111. {
  112. if (IsMouseMoveEvent())
  113. repaintOnMouseMove |= action.IsRepaintOnMouseMoveEnabled(m_GUIState);
  114. action.OnGUI(m_GUIState);
  115. }
  116. if (repaintOnMouseMove)
  117. m_GUIState.Repaint();
  118. }
  119. /// <summary>
  120. /// Calls the methods in its invocation list when the mouse moves.
  121. /// </summary>
  122. /// <returns>Returns `true` if the mouse moved. Otherwise, returns `false`.</returns>
  123. private bool IsMouseMoveEvent()
  124. {
  125. return m_GUIState.eventType == EventType.MouseMove || m_GUIState.eventType == EventType.MouseDrag;
  126. }
  127. }
  128. }