Control.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Path.GUIFramework
  4. {
  5. public abstract class Control
  6. {
  7. private string m_Name;
  8. private int m_NameHashCode;
  9. private int m_ID;
  10. private LayoutData m_LayoutData;
  11. private int m_ActionID = -1;
  12. private LayoutData m_HotLayoutData;
  13. public string name
  14. {
  15. get { return m_Name; }
  16. }
  17. public int ID
  18. {
  19. get { return m_ID; }
  20. }
  21. public int actionID
  22. {
  23. get { return m_ActionID; }
  24. }
  25. public LayoutData layoutData
  26. {
  27. get { return m_LayoutData; }
  28. set { m_LayoutData = value; }
  29. }
  30. public LayoutData hotLayoutData
  31. {
  32. get { return m_HotLayoutData; }
  33. }
  34. public Control(string name)
  35. {
  36. m_Name = name;
  37. m_NameHashCode = name.GetHashCode();
  38. }
  39. public void GetControl(IGUIState guiState)
  40. {
  41. m_ID = guiState.GetControlID(m_NameHashCode, FocusType.Passive);
  42. }
  43. internal void SetActionID(int actionID)
  44. {
  45. m_ActionID = actionID;
  46. m_HotLayoutData = m_LayoutData;
  47. }
  48. public void BeginLayout(IGUIState guiState)
  49. {
  50. Debug.Assert(guiState.eventType == EventType.Layout);
  51. m_LayoutData = OnBeginLayout(LayoutData.zero, guiState);
  52. }
  53. public void Layout(IGUIState guiState)
  54. {
  55. Debug.Assert(guiState.eventType == EventType.Layout);
  56. for (var i = 0; i < GetCount(); ++i)
  57. {
  58. if (guiState.hotControl == actionID && hotLayoutData.index == i)
  59. continue;
  60. var layoutData = new LayoutData()
  61. {
  62. index = i,
  63. position = GetPosition(guiState, i),
  64. distance = GetDistance(guiState, i),
  65. forward = GetForward(guiState, i),
  66. up = GetUp(guiState, i),
  67. right = GetRight(guiState, i),
  68. userData = GetUserData(guiState, i)
  69. };
  70. m_LayoutData = LayoutData.Nearest(m_LayoutData, layoutData);
  71. }
  72. }
  73. public void EndLayout(IGUIState guiState)
  74. {
  75. Debug.Assert(guiState.eventType == EventType.Layout);
  76. OnEndLayout(guiState);
  77. }
  78. public void Repaint(IGUIState guiState)
  79. {
  80. for (var i = 0; i < GetCount(); ++i)
  81. OnRepaint(guiState, i);
  82. }
  83. protected virtual LayoutData OnBeginLayout(LayoutData data, IGUIState guiState)
  84. {
  85. return data;
  86. }
  87. protected virtual void OnEndLayout(IGUIState guiState)
  88. {
  89. }
  90. protected virtual void OnRepaint(IGUIState guiState, int index)
  91. {
  92. }
  93. protected virtual int GetCount()
  94. {
  95. return 1;
  96. }
  97. protected virtual Vector3 GetPosition(IGUIState guiState, int index)
  98. {
  99. return Vector3.zero;
  100. }
  101. protected virtual Vector3 GetForward(IGUIState guiState, int index)
  102. {
  103. return Vector3.forward;
  104. }
  105. protected virtual Vector3 GetUp(IGUIState guiState, int index)
  106. {
  107. return Vector3.up;
  108. }
  109. protected virtual Vector3 GetRight(IGUIState guiState, int index)
  110. {
  111. return Vector3.right;
  112. }
  113. protected virtual float GetDistance(IGUIState guiState, int index)
  114. {
  115. return layoutData.distance;
  116. }
  117. protected virtual object GetUserData(IGUIState guiState, int index)
  118. {
  119. return null;
  120. }
  121. }
  122. }