GUIAction.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.U2D.Path.GUIFramework
  4. {
  5. public abstract class GUIAction
  6. {
  7. private int m_ID = -1;
  8. public Func<IGUIState, GUIAction, bool> enable;
  9. public Func<IGUIState, GUIAction, bool> enableRepaint;
  10. public Func<IGUIState, GUIAction, bool> repaintOnMouseMove;
  11. public Action<IGUIState, GUIAction> onPreRepaint;
  12. public Action<IGUIState, GUIAction> onRepaint;
  13. public int ID
  14. {
  15. get { return m_ID; }
  16. }
  17. public void OnGUI(IGUIState guiState)
  18. {
  19. m_ID = guiState.GetControlID(GetType().GetHashCode(), FocusType.Passive);
  20. if (guiState.hotControl == 0 && IsEnabled(guiState) && CanTrigger(guiState) && GetTriggerContidtion(guiState))
  21. {
  22. guiState.hotControl = ID;
  23. OnTrigger(guiState);
  24. }
  25. if (guiState.hotControl == ID)
  26. {
  27. if (GetFinishContidtion(guiState))
  28. {
  29. OnFinish(guiState);
  30. guiState.hotControl = 0;
  31. }
  32. else
  33. {
  34. OnPerform(guiState);
  35. }
  36. }
  37. if (guiState.eventType == EventType.Repaint && IsRepaintEnabled(guiState))
  38. Repaint(guiState);
  39. }
  40. public bool IsEnabled(IGUIState guiState)
  41. {
  42. if (enable != null)
  43. return enable(guiState, this);
  44. return true;
  45. }
  46. public bool IsRepaintEnabled(IGUIState guiState)
  47. {
  48. if (!IsEnabled(guiState))
  49. return false;
  50. if (enableRepaint != null)
  51. return enableRepaint(guiState, this);
  52. return true;
  53. }
  54. public void PreRepaint(IGUIState guiState)
  55. {
  56. Debug.Assert(guiState.eventType == EventType.Repaint);
  57. if (IsEnabled(guiState) && onPreRepaint != null)
  58. onPreRepaint(guiState, this);
  59. }
  60. private void Repaint(IGUIState guiState)
  61. {
  62. Debug.Assert(guiState.eventType == EventType.Repaint);
  63. if (onRepaint != null)
  64. onRepaint(guiState, this);
  65. }
  66. internal bool IsRepaintOnMouseMoveEnabled(IGUIState guiState)
  67. {
  68. if (!IsEnabled(guiState) || !IsRepaintEnabled(guiState))
  69. return false;
  70. if (repaintOnMouseMove != null)
  71. return repaintOnMouseMove(guiState, this);
  72. return false;
  73. }
  74. protected abstract bool GetFinishContidtion(IGUIState guiState);
  75. protected abstract bool GetTriggerContidtion(IGUIState guiState);
  76. protected virtual bool CanTrigger(IGUIState guiState) { return true; }
  77. protected virtual void OnTrigger(IGUIState guiState)
  78. {
  79. }
  80. protected virtual void OnPerform(IGUIState guiState)
  81. {
  82. }
  83. protected virtual void OnFinish(IGUIState guiState)
  84. {
  85. }
  86. }
  87. }