BsScriptGameObjectManager.h 4.0 KB

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