ContextMenu.gd 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. Launcher.Action.Enable(not disableAction)
  54. func Hide():
  55. visible = false
  56. canFadeOut = false
  57. currentStep = 0.0
  58. Launcher.Action.Enable(true)
  59. Clear()
  60. func Clear():
  61. for child in contextList.get_children():
  62. contextList.remove_child(child)
  63. child.queue_free()
  64. #
  65. func _process(delta):
  66. if not visible:
  67. return
  68. if canFadeOut or currentStep <= displayStep:
  69. currentStep += delta
  70. if currentStep <= fadeInStep:
  71. if fadeInStep > 0.0:
  72. modulate.a = currentStep / fadeInStep
  73. elif currentStep <= displayStep:
  74. if displayStep > 0.0:
  75. modulate.a = 1.0
  76. elif canFadeOut:
  77. if currentStep <= fadeOutStep:
  78. if fadeOutDelay > 0.0:
  79. modulate.a = 1.0 - (currentStep - displayStep) / fadeOutDelay
  80. else:
  81. Hide()
  82. func _ready():
  83. Hide()