BsScriptGameObjectManager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "Utility/BsModule.h"
  6. namespace bs
  7. {
  8. class ScriptRenderable;
  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 instance of a ManagedComponent instance with the native ManagedComponent class by creating
  43. * the interop object. Throws an exception if the interop object already exists.
  44. */
  45. ScriptManagedComponent* createManagedScriptComponent(MonoObject* existingInstance, const HManagedComponent& component);
  46. /**
  47. * Creates a new interop object that connects a built-in native component with a managed version of that component.
  48. */
  49. ScriptComponentBase* createBuiltinScriptComponent(const HComponent& component);
  50. /**
  51. * Attempts to find the interop object for the specified built-in component. If one cannot be found a new
  52. * script interop object is created if @p createNonExisting is enabled, or returns null otherwise.
  53. */
  54. ScriptComponentBase* getBuiltinScriptComponent(const HComponent& component, bool createNonExisting = true);
  55. /**
  56. * Attempts to find the interop object for the specified managed component. If one cannot be found null is returned.
  57. */
  58. ScriptManagedComponent* getManagedScriptComponent(const HManagedComponent& component) const;
  59. /**
  60. * Attempts to find the interop object for a component with the specified instance ID. If one cannot be
  61. * found null is returned.
  62. */
  63. ScriptComponentBase* getScriptComponent(UINT64 instanceId) const;
  64. /** Attempts to find the interop object for the specified SceneObject. If one cannot be found null is returned. */
  65. ScriptSceneObject* getScriptSceneObject(const HSceneObject& sceneObject) const;
  66. /**
  67. * Attempts to find the interop object for a managed scene object with the specified instance ID. If one cannot be
  68. * found null is returned.
  69. */
  70. ScriptSceneObject* getScriptSceneObject(UINT64 instanceId) const;
  71. /**
  72. * Attempts to find the interop object for a GameObject with the specified instance ID. If one cannot be found null
  73. * is returned.
  74. */
  75. ScriptGameObjectBase* getScriptGameObject(UINT64 instanceId) const;
  76. /** Destroys and unregisters the specified SceneObject interop object. */
  77. void destroyScriptSceneObject(ScriptSceneObject* sceneObject);
  78. /** Destroys and unregisters the specified ManagedComponent interop object. */
  79. void destroyScriptComponent(ScriptComponentBase* component);
  80. private:
  81. /**
  82. * Triggers OnReset methods on all registered managed components.
  83. *
  84. * @note Usually this happens after an assembly reload.
  85. */
  86. void sendComponentResetEvents();
  87. /** Triggered when the any game object is destroyed. */
  88. void onGameObjectDestroyed(const HGameObject& go);
  89. UnorderedMap<UINT64, ScriptComponentBase*> mScriptComponents;
  90. UnorderedMap<UINT64, ScriptSceneObject*> mScriptSceneObjects;
  91. HEvent mOnAssemblyReloadDoneConn;
  92. HEvent onGameObjectDestroyedConn;
  93. };
  94. /** @} */
  95. }