using System; using UnityEngine; namespace UnityEditor.U2D.Path.GUIFramework { /// /// Represents a UI control in a custom editor. /// public abstract class Control { private string m_Name; private int m_NameHashCode; private int m_ID; private LayoutData m_LayoutData; private int m_ActionID = -1; private LayoutData m_HotLayoutData; /// /// The name of the control. /// public string name { get { return m_Name; } } /// /// The control ID. The GUI uses this to identify the control. /// public int ID { get { return m_ID; } } /// /// The action ID. /// public int actionID { get { return m_ActionID; } } /// /// The control's layout data. This contains information about the control's position and orientation. /// public LayoutData layoutData { get { return m_LayoutData; } set { m_LayoutData = value; } } /// /// The control's hot layout data /// public LayoutData hotLayoutData { get { return m_HotLayoutData; } } /// /// Initializes and returns an instance of Control /// /// The name of the control public Control(string name) { m_Name = name; m_NameHashCode = name.GetHashCode(); } /// /// Gets the control from the guiState. /// /// The current state of the custom editor. public void GetControl(IGUIState guiState) { m_ID = guiState.GetControlID(m_NameHashCode, FocusType.Passive); } internal void SetActionID(int actionID) { m_ActionID = actionID; m_HotLayoutData = m_LayoutData; } /// /// Begins the layout for this control. A call to EndLayout must always follow a call to this function. /// /// The current state of the custom editor. public void BeginLayout(IGUIState guiState) { Debug.Assert(guiState.eventType == EventType.Layout); m_LayoutData = OnBeginLayout(LayoutData.zero, guiState); } /// /// Gets the control's layout data from the guiState. /// /// The current state of the custom editor. public void Layout(IGUIState guiState) { Debug.Assert(guiState.eventType == EventType.Layout); for (var i = 0; i < GetCount(); ++i) { if (guiState.hotControl == actionID && hotLayoutData.index == i) continue; var layoutData = new LayoutData() { index = i, position = GetPosition(guiState, i), distance = GetDistance(guiState, i), forward = GetForward(guiState, i), up = GetUp(guiState, i), right = GetRight(guiState, i), userData = GetUserData(guiState, i) }; m_LayoutData = LayoutData.Nearest(m_LayoutData, layoutData); } } /// /// Ends the layout for this control. This function must always follow a call to BeginLayout(). /// /// The current state of the custom editor. public void EndLayout(IGUIState guiState) { Debug.Assert(guiState.eventType == EventType.Layout); OnEndLayout(guiState); } /// /// Repaints the control. /// /// The current state of the custom editor. public void Repaint(IGUIState guiState) { for (var i = 0; i < GetCount(); ++i) OnRepaint(guiState, i); } /// /// Called when the control begins its layout. /// /// The layout data. /// The current state of the custom editor. /// Returns the layout data to use. protected virtual LayoutData OnBeginLayout(LayoutData data, IGUIState guiState) { return data; } /// /// Called when the control ends its layout. /// /// /// The current state of the custom editor. protected virtual void OnEndLayout(IGUIState guiState) { } /// /// Called when the control repaints its contents. /// /// The current state of the custom editor. /// The index. protected virtual void OnRepaint(IGUIState guiState, int index) { } /// /// Gets the number of sub-controllers. /// /// /// By default, this is `1`. If you implement your own controller and want to use multiple sub-controllers within it, you can override this function to declare how to count the sub-controllers. /// /// Returns the number of sub-controllers. If you do not override this function, this returns 1. protected virtual int GetCount() { return 1; } /// /// Gets the position of the control. /// /// The current state of the custom editor. /// The index. /// Returns Vector3.zero. protected virtual Vector3 GetPosition(IGUIState guiState, int index) { return Vector3.zero; } /// /// Gets the forward vector of the control. /// /// The current state of the custom editor. /// The index. /// Returns Vector3.forward. protected virtual Vector3 GetForward(IGUIState guiState, int index) { return Vector3.forward; } /// /// Gets the up vector of the control. /// /// The current state of the custom editor. /// The index. /// Returns Vector3.up, protected virtual Vector3 GetUp(IGUIState guiState, int index) { return Vector3.up; } /// /// Gets the right vector of the control. /// /// The current state of the custom editor. /// The index. /// Returns Vector3.right. protected virtual Vector3 GetRight(IGUIState guiState, int index) { return Vector3.right; } /// /// Gets the distance from the Scene view camera to the control. /// /// The current state of the custom editor. /// The index. /// Returns layoutData.distance. protected virtual float GetDistance(IGUIState guiState, int index) { return layoutData.distance; } /// /// Gets the control's user data. /// /// The current state of the custom editor. /// The index. /// Returns `null`. protected virtual object GetUserData(IGUIState guiState, int index) { return null; } } }