BsScriptGameObjectManager.h 4.2 KB

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