using System; using UnityEngine; namespace UnityEditor.U2D.Path.GUIFramework { /// /// Represents an action that is tied to a GUI element. /// public abstract class GUIAction { private int m_ID = -1; /// /// Func for GetEnable /// public Func enable; /// /// Func for EnabledRepaint /// public Func enableRepaint; /// /// Func for repaintOnMouseMove /// public Func repaintOnMouseMove; /// /// Action for OnPreRepaint /// public Action onPreRepaint; /// /// Func for OnRepaint /// public Action onRepaint; /// /// The action ID. /// public int ID { get { return m_ID; } } /// /// Calls the methods in its invocation list when Unity draws this GUIAction's GUI. /// /// The current state of the custom editor. public void OnGUI(IGUIState guiState) { m_ID = guiState.GetControlID(GetType().GetHashCode(), FocusType.Passive); if (guiState.hotControl == 0 && IsEnabled(guiState) && CanTrigger(guiState) && GetTriggerContidtion(guiState)) { guiState.hotControl = ID; OnTrigger(guiState); } if (guiState.hotControl == ID) { if (GetFinishContidtion(guiState)) { OnFinish(guiState); guiState.hotControl = 0; } else { OnPerform(guiState); } } if (guiState.eventType == EventType.Repaint && IsRepaintEnabled(guiState)) Repaint(guiState); } /// /// Checks whether the GUIAction is enabled. /// /// The current state of the custom editor. /// Returns `true` if the GUIAction is enabled in the custom editor. Otherwise, returns `false`. public bool IsEnabled(IGUIState guiState) { if (enable != null) return enable(guiState, this); return true; } /// /// Checks whether the GUIAction should repaint. /// /// The current state of the custom editor. /// Returns `true` if the GUIAction should repaint. Otherwise, returns `false`. public bool IsRepaintEnabled(IGUIState guiState) { if (!IsEnabled(guiState)) return false; if (enableRepaint != null) return enableRepaint(guiState, this); return true; } /// /// Preprocessing that occurs before the GUI repaints. /// /// The current state of the custom editor. public void PreRepaint(IGUIState guiState) { Debug.Assert(guiState.eventType == EventType.Repaint); if (IsEnabled(guiState) && onPreRepaint != null) onPreRepaint(guiState, this); } /// /// Calls the methods in its invocation list when repainting the GUI. /// /// The current state of the custom editor. private void Repaint(IGUIState guiState) { Debug.Assert(guiState.eventType == EventType.Repaint); if (onRepaint != null) onRepaint(guiState, this); } /// /// Checks whether the GUI should repaint if the mouse moves over it. /// /// The current state of the custom editor. /// Returns `true` if the GUI should repaint if the moves moves over it. Otherwise, returns `false`. internal bool IsRepaintOnMouseMoveEnabled(IGUIState guiState) { if (!IsEnabled(guiState) || !IsRepaintEnabled(guiState)) return false; if (repaintOnMouseMove != null) return repaintOnMouseMove(guiState, this); return false; } /// /// Determines whether the finish condition has been met. /// /// The current state of the custom editor. /// Returns `true` if finish condition has been met. Otherwise, returns `false`. protected abstract bool GetFinishContidtion(IGUIState guiState); /// /// Determines whether the trigger condition has been met. /// /// The current state of the custom editor. /// Returns `true` if finish condition has been met. Otherwise, returns `false`. protected abstract bool GetTriggerContidtion(IGUIState guiState); /// /// Determines whether the GUIAction can trigger. /// /// The current state of the custom editor. /// Always returns `true`. protected virtual bool CanTrigger(IGUIState guiState) { return true; } /// /// Calls the methods in its invocation list when triggered. /// /// The current state of the custom editor. protected virtual void OnTrigger(IGUIState guiState) { } /// /// Calls the methods in its invocation list when performed. /// /// The current state of the custom editor. protected virtual void OnPerform(IGUIState guiState) { } /// /// Calls the methods in its invocation list when finished. /// /// The current state of the custom editor. protected virtual void OnFinish(IGUIState guiState) { } } }