BsScriptComponent.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "Wrappers/BsScriptGameObject.h"
  6. #include "BsScriptObject.h"
  7. #include "BsMonoUtil.h"
  8. namespace bs
  9. {
  10. /** @addtogroup ScriptInteropEngine
  11. * @{
  12. */
  13. /** Base class for all Component interop classes. */
  14. class BS_SCR_BE_EXPORT ScriptComponentBase : public ScriptGameObjectBase
  15. {
  16. public:
  17. ScriptComponentBase(MonoObject* instance);
  18. virtual ~ScriptComponentBase() { }
  19. /** Returns the component wrapped by this object. */
  20. HComponent getComponent() const { return static_object_cast<Component>(getNativeHandle()); }
  21. protected:
  22. friend class ScriptGameObjectManager;
  23. /** Destroys the interop object, unless refresh is in progress in which case it is just prepared for re-creation. */
  24. void destroy(bool assemblyRefresh);
  25. /** Triggered by the script game object manager when the handle this object is referencing is destroyed. */
  26. virtual void _notifyDestroyed() { }
  27. /** Checks if the provided game object is destroyed and logs a warning if it is. */
  28. static bool checkIfDestroyed(const GameObjectHandleBase& handle);
  29. };
  30. /** Base class for a specific builtin component's interop object. */
  31. template<class ScriptClass, class CompType, class BaseType = ScriptComponentBase>
  32. class BS_SCR_BE_EXPORT TScriptComponent : public ScriptObject <ScriptClass, BaseType>
  33. {
  34. public:
  35. /** Returns a generic handle to the internal wrapped component. */
  36. HGameObject getNativeHandle() const override { return static_object_cast<GameObject>(mComponent); }
  37. /** Sets the internal component this object wraps. */
  38. void setNativeHandle(const HGameObject& gameObject) override { mComponent = static_object_cast<CompType>(gameObject); }
  39. /** Returns a handle to the internal wrapped component. */
  40. const GameObjectHandle<CompType>& getHandle() const { return mComponent; }
  41. protected:
  42. friend class ScriptGameObjectManager;
  43. TScriptComponent(MonoObject* instance, const GameObjectHandle<CompType>& component)
  44. :ScriptObject<ScriptClass, BaseType>(instance), mComponent(component)
  45. {
  46. this->setManagedInstance(instance);
  47. }
  48. virtual ~TScriptComponent() {}
  49. /** @copydoc ScriptObject::_createManagedInstance */
  50. MonoObject* _createManagedInstance(bool construct) override
  51. {
  52. MonoObject* managedInstance = ScriptClass::metaData.scriptClass->createInstance(construct);
  53. this->setManagedInstance(managedInstance);
  54. return managedInstance;
  55. }
  56. /** @copydoc ScriptObjectBase::_clearManagedInstance */
  57. void _clearManagedInstance() override
  58. {
  59. this->freeManagedInstance();
  60. }
  61. /**
  62. * Triggered by the script game object manager when the native component handle this object point to has been
  63. * destroyed.
  64. */
  65. void _notifyDestroyed() override
  66. {
  67. this->freeManagedInstance();
  68. }
  69. /** Called when the managed instance gets finalized by the CLR. */
  70. void _onManagedInstanceDeleted(bool assemblyRefresh) override
  71. {
  72. this->freeManagedInstance();
  73. this->destroy(assemblyRefresh);
  74. }
  75. GameObjectHandle<CompType> mComponent;
  76. };
  77. /** Interop class between C++ & CLR for Component. */
  78. class BS_SCR_BE_EXPORT ScriptComponent : public ScriptObject<ScriptComponent, ScriptComponentBase>
  79. {
  80. public:
  81. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Component")
  82. private:
  83. friend class ScriptGameObjectManager;
  84. ScriptComponent(MonoObject* instance);
  85. /************************************************************************/
  86. /* CLR HOOKS */
  87. /************************************************************************/
  88. static MonoObject* internal_addComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  89. static MonoObject* internal_getComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  90. static MonoArray* internal_getComponents(MonoObject* parentSceneObject);
  91. static MonoArray* internal_getComponentsPerType(MonoObject* parentSceneObject, MonoReflectionType* type);
  92. static void internal_removeComponent(MonoObject* parentSceneObject, MonoReflectionType* type);
  93. static MonoObject* internal_getSceneObject(ScriptComponentBase* nativeInstance);
  94. static TransformChangedFlags internal_getNotifyFlags(ScriptComponentBase* nativeInstance);
  95. static void internal_setNotifyFlags(ScriptComponentBase* nativeInstance, TransformChangedFlags flags);
  96. static void internal_destroy(ScriptComponentBase* nativeInstance, bool immediate);
  97. };
  98. /** @} */
  99. }