InputEventGroup.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/RTTI/TypeInfoSimple.h>
  10. #include <AzCore/RTTI/RTTIMacros.h>
  11. #include <InputEventMap.h>
  12. namespace AZ
  13. {
  14. class ReflectContext;
  15. }
  16. namespace StartingPointInput
  17. {
  18. //////////////////////////////////////////////////////////////////////////
  19. /// This class holds all of the raw input handlers that generate events.
  20. //////////////////////////////////////////////////////////////////////////
  21. class InputEventGroup
  22. {
  23. public:
  24. AZ_RTTI(InputEventGroup, "{25143B7E-2FEC-4CC5-92FE-270B67E79734}");
  25. virtual ~InputEventGroup() = default;
  26. static void Reflect(AZ::ReflectContext* reflection);
  27. virtual void Activate(const AzFramework::LocalUserId& localUserId)
  28. {
  29. #if defined(RELEASE)
  30. if (m_excludeFromRelease)
  31. {
  32. return;
  33. }
  34. #endif // defined(RELEASE)
  35. InputEventNotificationId busId(localUserId, m_eventName.c_str());
  36. for (InputSubComponent* inputHandler : m_inputHandlers)
  37. {
  38. inputHandler->Activate(busId);
  39. }
  40. }
  41. virtual void Deactivate(const AzFramework::LocalUserId& localUserId)
  42. {
  43. #if defined(RELEASE)
  44. if (m_excludeFromRelease)
  45. {
  46. return;
  47. }
  48. #endif // defined(RELEASE)
  49. InputEventNotificationId busId(localUserId, m_eventName.c_str());
  50. for (InputSubComponent* inputHandler : m_inputHandlers)
  51. {
  52. inputHandler->Deactivate(busId);
  53. }
  54. }
  55. // Explicitly release our array of input handlers here. There is no system that is currently cleaning up the Input objects we have in this array
  56. // We cannot do this in the destructor because of the allocation patterns of this object in the serializer that causes us to end up releasing invalid data during serialization load
  57. // I did not have success changing the raw pointer array of input handlers to a shared pointer of InputSubComponents.
  58. // The most straight forward resolution for now is to be explicit about when we release this data in order to prevent large memory leaks
  59. void Cleanup()
  60. {
  61. // Release the InputSubComponents opposite of how they were allocated during serialization in InstanceFactory::Create
  62. for (InputSubComponent* inputHandler : m_inputHandlers)
  63. {
  64. inputHandler->~InputSubComponent();
  65. azfree(static_cast<void*>(inputHandler), AZ::SystemAllocator);
  66. }
  67. m_inputHandlers.clear();
  68. }
  69. protected:
  70. virtual AZStd::string GetEditorText() const
  71. {
  72. return m_eventName.empty() ? "<Unspecified Event>" : m_eventName;
  73. }
  74. AZStd::vector<InputSubComponent*> m_inputHandlers;
  75. AZStd::string m_eventName;
  76. bool m_excludeFromRelease = false;
  77. };
  78. } // namespace Input