UILogic.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. ----------------------------------------------------------------------------------------------------
  2. --
  3. -- Copyright (c) Contributors to the Open 3D Engine Project.
  4. -- For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. --
  6. -- SPDX-License-Identifier: Apache-2.0 OR MIT
  7. --
  8. --
  9. --
  10. ----------------------------------------------------------------------------------------------------
  11. local UILogic = {
  12. Properties = {
  13. Debug = false,
  14. QuitInputEvent = "Quit",
  15. ResetInputEvent = "Reset",
  16. CtrlInputEvent = "Ctrl"
  17. },
  18. InputHandlers = {
  19. Quit = {},
  20. Reset = {},
  21. Ctrl = {}
  22. },
  23. GameplayEventHandlers = {
  24. OnCancel = {},
  25. OnQuit = {}
  26. }
  27. }
  28. function UILogic:OnActivate()
  29. self.ctrlPressed = false
  30. self.resetPressed = false
  31. self.quitCanvasVisible = false
  32. for event, handler in pairs(self.InputHandlers) do
  33. local id = InputEventNotificationId(event)
  34. handler.busHandler = InputEventNotificationBus.Connect(handler, id)
  35. handler.component = self
  36. end
  37. for event, handler in pairs(self.GameplayEventHandlers) do
  38. local id = GameplayNotificationId(EntityId(0), event, "float")
  39. handler.busHandler = GameplayNotificationBus.Connect(handler, id)
  40. handler.component = self
  41. end
  42. end
  43. function UILogic:SetCanvasVisible(visible)
  44. if visible then
  45. if self.quitCanvasVisible == false then
  46. self.quitCanvasVisible = true
  47. -- show cursor
  48. while UiCursorBus.Broadcast.IsUiCursorVisible() == false do
  49. UiCursorBus.Broadcast.IncrementVisibleCounter()
  50. end
  51. FlyCameraInputBus.Broadcast.SetIsEnabled(false)
  52. UiCanvasAssetRefBus.Event.LoadCanvas(self.entityId)
  53. end
  54. else
  55. self.quitCanvasVisible = false
  56. -- hide cursor
  57. UiCursorBus.Broadcast.DecrementVisibleCounter()
  58. FlyCameraInputBus.Broadcast.SetIsEnabled(true)
  59. UiCanvasAssetRefBus.Event.UnloadCanvas(self.entityId)
  60. end
  61. end
  62. function UILogic.GameplayEventHandlers.OnCancel:OnEventBegin(value)
  63. self.component:SetCanvasVisible(false)
  64. end
  65. function UILogic.GameplayEventHandlers.OnQuit:OnEventBegin(value)
  66. Debug.Log("Exiting application")
  67. ConsoleRequestBus.Broadcast.ExecuteConsoleCommand("quit")
  68. end
  69. function UILogic:Reset()
  70. if self.resetPressed and self.ctrlPressed then
  71. Debug.Log("Re-loading level")
  72. ROSConDemoRequestBus.Broadcast.ReloadLevel();
  73. -- LoadLevel command will reload the error but currently has graphic
  74. -- issues so a custom ReloadLevel command above is used
  75. -- ConsoleRequestBus.Broadcast.ExecuteConsoleCommand("LoadLevel main")
  76. end
  77. end
  78. function UILogic.InputHandlers.Quit:OnPressed(value)
  79. self.component:SetCanvasVisible(true)
  80. end
  81. function UILogic.InputHandlers.Reset:OnPressed(value)
  82. self.component.resetPressed = true
  83. self.component:Reset()
  84. end
  85. function UILogic.InputHandlers.Reset:OnReleased(value)
  86. self.component.resetPressed = false
  87. end
  88. function UILogic.InputHandlers.Ctrl:OnPressed(value)
  89. self.component.ctrlPressed = true
  90. end
  91. function UILogic.InputHandlers.Ctrl:OnReleased(value)
  92. self.component.ctrlPressed = false
  93. end
  94. function UILogic:OnDeactivate()
  95. for event, handler in pairs(self.InputHandlers) do
  96. handler.busHandler:Disconnect()
  97. end
  98. for event, handler in pairs(self.GameplayEventHandlers) do
  99. handler.busHandler:Disconnect()
  100. end
  101. end
  102. return UILogic