2
0

BsScriptGameObjectManager.h 4.2 KB

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