BsScriptGameObjectManager.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsScriptEnginePrerequisites.h"
  5. #include "BsModule.h"
  6. #include <mono/jit/jit.h>
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Manages all active GameObject interop objects. GameObjects can be created from native
  11. * code and used in managed code therefore we need to keep a dictionary or all the native
  12. * objects we have mapped to managed objects.
  13. */
  14. class BS_SCR_BE_EXPORT ScriptGameObjectManager : public Module<ScriptGameObjectManager>
  15. {
  16. /**
  17. * @brief Contains information about a single interop object containing a game object.
  18. */
  19. struct ScriptGameObjectEntry
  20. {
  21. ScriptGameObjectEntry();
  22. ScriptGameObjectEntry(ScriptGameObjectBase* instance, bool isComponent);
  23. ScriptGameObjectBase* instance;
  24. bool isComponent;
  25. };
  26. public:
  27. ScriptGameObjectManager();
  28. ~ScriptGameObjectManager();
  29. /**
  30. * @brief Attempts to find the interop object for the specified SceneObject. If one cannot be found
  31. * new one is created and returned.
  32. */
  33. ScriptSceneObject* getOrCreateScriptSceneObject(const HSceneObject& sceneObject);
  34. /**
  35. * @brief Creates a new interop object for the specified SceneObject. Throws an exception if one
  36. * already exists.
  37. */
  38. ScriptSceneObject* createScriptSceneObject(const HSceneObject& sceneObject);
  39. /**
  40. * @brief Connects an existing managed SceneObject instance with the native SceneObject by creating
  41. * the interop object. Throws an exception if the interop object already exists.
  42. */
  43. ScriptSceneObject* createScriptSceneObject(MonoObject* existingInstance, const HSceneObject& sceneObject);
  44. /**
  45. * @brief Connects an existing managed ManagedComponent instance with the native ManagedComponent by creating
  46. * the interop object. Throws an exception if the interop object already exists.
  47. */
  48. ScriptComponent* createScriptComponent(MonoObject* existingInstance, const GameObjectHandle<ManagedComponent>& component);
  49. /**
  50. * @brief Attempts to find the interop object for the specified managed component.
  51. * If one cannot be found null is returned.
  52. */
  53. ScriptComponent* getScriptComponent(const GameObjectHandle<ManagedComponent>& component) const;
  54. /**
  55. * @brief Attempts to find the interop object for a managed component with the specified instance ID.
  56. * If one cannot be found null is returned.
  57. */
  58. ScriptComponent* getScriptComponent(UINT64 instanceId) const;
  59. /**
  60. * @brief Attempts to find the interop object for the specified SceneObject. If one cannot be found
  61. * null is returned.
  62. */
  63. ScriptSceneObject* getScriptSceneObject(const HSceneObject& sceneObject) const;
  64. /**
  65. * @brief Attempts to find the interop object for a managed scene object with the specified instance ID.
  66. * If one cannot be found null is returned.
  67. */
  68. ScriptSceneObject* getScriptSceneObject(UINT64 instanceId) const;
  69. /**
  70. * @brief Attempts to find the interop object for a GameObject with the specified instance ID.
  71. * If one cannot be found null is returned.
  72. */
  73. ScriptGameObjectBase* getScriptGameObject(UINT64 instanceId) const;
  74. /**
  75. * @brief Notifies the managed that the OnInitialize method on a managed component has been called.
  76. */
  77. void notifyComponentInitialized(UINT64 instanceId);
  78. /**
  79. * @brief Destroys and unregisters the specified SceneObject interop object.
  80. */
  81. void destroyScriptSceneObject(ScriptSceneObject* sceneObject);
  82. /**
  83. * @brief Destroys and unregisters the specified ManagedComponent interop object.
  84. */
  85. void destroyScriptComponent(ScriptComponent* component);
  86. /**
  87. * @brief Triggers OnInitialize methods on all uninitialized managed components.
  88. */
  89. void sendComponentInitializeEvents();
  90. private:
  91. /**
  92. * @brief Triggers OnReset methods on all registered managed components.
  93. *
  94. * @note Usually this happens after an assembly reload.
  95. */
  96. void sendComponentResetEvents();
  97. /**
  98. * @brief Triggered when the any game object is destroyed.
  99. */
  100. void onGameObjectDestroyed(const HGameObject& go);
  101. UnorderedMap<UINT64, ScriptComponent*> mScriptComponents;
  102. UnorderedMap<UINT64, ScriptSceneObject*> mScriptSceneObjects;
  103. UnorderedMap<UINT64, ScriptComponent*> mUninitializedScriptComponents;
  104. HEvent mOnAssemblyReloadDoneConn;
  105. HEvent onGameObjectDestroyedConn;
  106. };
  107. }