BsScriptGameObjectManager.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /** @addtogroup SBansheeEngine
  10. * @{
  11. */
  12. /**
  13. * Manages all active GameObject interop objects. GameObjects can be created from native code and used in managed code
  14. * therefore we need to keep a dictionary or all the native objects we have mapped to managed objects.
  15. */
  16. class BS_SCR_BE_EXPORT ScriptGameObjectManager : public Module<ScriptGameObjectManager>
  17. {
  18. /** Contains information about a single interop object containing a game object. */
  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. * Attempts to find the interop object for the specified SceneObject. If one cannot be found new one is created and
  31. * returned.
  32. */
  33. ScriptSceneObject* getOrCreateScriptSceneObject(const HSceneObject& sceneObject);
  34. /** Creates a new interop object for the specified SceneObject. Throws an exception if one already exists. */
  35. ScriptSceneObject* createScriptSceneObject(const HSceneObject& sceneObject);
  36. /**
  37. * Connects an existing managed SceneObject instance with the native SceneObject by creating the interop object.
  38. * Throws an exception if the interop object already exists.
  39. */
  40. ScriptSceneObject* createScriptSceneObject(MonoObject* existingInstance, const HSceneObject& sceneObject);
  41. /**
  42. * Connects an existing managed ManagedComponent instance with the native ManagedComponent by creating the interop
  43. * object. Throws an exception if the interop object already exists.
  44. */
  45. ScriptComponent* createScriptComponent(MonoObject* existingInstance,
  46. const GameObjectHandle<ManagedComponent>& component);
  47. /**
  48. * Attempts to find the interop object for the specified managed component. If one cannot be found null is returned.
  49. */
  50. ScriptComponent* getScriptComponent(const GameObjectHandle<ManagedComponent>& component) const;
  51. /**
  52. * Attempts to find the interop object for a managed component with the specified instance ID. If one cannot be
  53. * found null is returned.
  54. */
  55. ScriptComponent* getScriptComponent(UINT64 instanceId) const;
  56. /** Attempts to find the interop object for the specified SceneObject. If one cannot be found null is returned. */
  57. ScriptSceneObject* getScriptSceneObject(const HSceneObject& sceneObject) const;
  58. /**
  59. * Attempts to find the interop object for a managed scene object with the specified instance ID. If one cannot be
  60. * found null is returned.
  61. */
  62. ScriptSceneObject* getScriptSceneObject(UINT64 instanceId) const;
  63. /**
  64. * Attempts to find the interop object for a GameObject with the specified instance ID. If one cannot be found null
  65. * is returned.
  66. */
  67. ScriptGameObjectBase* getScriptGameObject(UINT64 instanceId) const;
  68. /** Destroys and unregisters the specified SceneObject interop object. */
  69. void destroyScriptSceneObject(ScriptSceneObject* sceneObject);
  70. /** Destroys and unregisters the specified ManagedComponent interop object. */
  71. void destroyScriptComponent(ScriptComponent* component);
  72. /**
  73. * Sends OnInitialize/OnEnable events to all components that run only while the game is playing (i.e. without
  74. * RunInEditor attribute).
  75. */
  76. void wakeRuntimeComponents();
  77. private:
  78. /**
  79. * Triggers OnReset methods on all registered managed components.
  80. *
  81. * @note Usually this happens after an assembly reload.
  82. */
  83. void sendComponentResetEvents();
  84. /** Triggered when the any game object is destroyed. */
  85. void onGameObjectDestroyed(const HGameObject& go);
  86. UnorderedMap<UINT64, ScriptComponent*> mScriptComponents;
  87. UnorderedMap<UINT64, ScriptSceneObject*> mScriptSceneObjects;
  88. HEvent mOnAssemblyReloadDoneConn;
  89. HEvent onGameObjectDestroyedConn;
  90. };
  91. /** @} */
  92. }