ContextMenu.gd 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. extends PanelContainer
  2. class_name ContextMenu
  3. @export var fadeInDelay : float = 1.0
  4. @export var displayDelay : float = 5.0
  5. @export var fadeOutDelay : float = 2.0
  6. @export var persistant : bool = true
  7. @onready var contextList : Control = $Margin/List
  8. var buffer : Array[ContextData] = []
  9. var currentStep : float = 0.0
  10. var fadeInStep : float = 0.0
  11. var displayStep : float = 0.0
  12. var fadeOutStep : float = 0.0
  13. var canFadeOut : bool = false
  14. #
  15. func FlushDataBuffer():
  16. while buffer.size() > 0:
  17. var data : ContextData = buffer.pop_front()
  18. var action : Control = UICommons.ContextAction.instantiate()
  19. action.Init(data, self)
  20. contextList.add_child.call_deferred(action)
  21. func ResetSteps():
  22. fadeInStep = fadeInDelay
  23. displayStep = fadeInStep + displayDelay
  24. fadeOutStep = displayStep + displayDelay
  25. if currentStep >= displayStep:
  26. if fadeOutDelay > 0:
  27. currentStep = (1 - (currentStep - displayStep) / fadeOutDelay) * fadeInStep
  28. else:
  29. currentStep = 0.0
  30. _process(0) # Force refresh
  31. func Push(data : ContextData):
  32. buffer.push_back(data)
  33. func FadeIn(disableAction : bool = false):
  34. ResetSteps()
  35. Show(disableAction)
  36. FlushDataBuffer()
  37. canFadeOut = not persistant
  38. _process(0)
  39. func FadeOut():
  40. if currentStep < fadeInStep:
  41. if fadeInStep > 0:
  42. currentStep = (1 - currentStep / fadeInStep) * fadeOutDelay + displayStep
  43. else:
  44. currentStep = displayStep
  45. elif currentStep < displayStep:
  46. currentStep = displayStep
  47. canFadeOut = true
  48. _process(0)
  49. func Show(disableAction):
  50. visible = true
  51. for child in contextList.get_children():
  52. child._on_visibility_changed()
  53. if Launcher.Action:
  54. Launcher.Action.Enable(not disableAction)
  55. func Hide():
  56. visible = false
  57. canFadeOut = false
  58. currentStep = 0.0
  59. if Launcher.Action:
  60. Launcher.Action.Enable(true)
  61. Clear()
  62. func Clear():
  63. for child in contextList.get_children():
  64. contextList.remove_child(child)
  65. child.queue_free()
  66. #
  67. func _process(delta):
  68. if not visible:
  69. return
  70. if canFadeOut or currentStep <= displayStep:
  71. currentStep += delta
  72. if currentStep <= fadeInStep:
  73. if fadeInStep > 0.0:
  74. modulate.a = currentStep / fadeInStep
  75. elif currentStep <= displayStep:
  76. if displayStep > 0.0:
  77. modulate.a = 1.0
  78. elif canFadeOut:
  79. if currentStep <= fadeOutStep:
  80. if fadeOutDelay > 0.0:
  81. modulate.a = 1.0 - (currentStep - displayStep) / fadeOutDelay
  82. else:
  83. Hide()
  84. func _ready():
  85. Hide()