BsScriptGUIElement.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsScriptObject.h"
  4. #include "BsGUIOptions.h"
  5. #include "BsGUIElementBase.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Interop class between C++ & CLR for all elements inheriting from
  10. * GUIElementBase.
  11. */
  12. class BS_SCR_BE_EXPORT ScriptGUIElementBaseTBase : public ScriptObjectBase
  13. {
  14. public:
  15. ScriptGUIElementBaseTBase(MonoObject* instance);
  16. virtual ~ScriptGUIElementBaseTBase() {}
  17. /**
  18. * @brief Returns the underlying GUIElementBase wrapped by this object.
  19. */
  20. GUIElementBase* getGUIElement() const { return (GUIElementBase*)mElement; }
  21. /**
  22. * @brief Destroys the underlying GUIElementBase.
  23. */
  24. virtual void destroy() = 0;
  25. /**
  26. * @brief Checks have we destroyed the underlying GUIElementBase.
  27. */
  28. bool isDestroyed() const { return mIsDestroyed; }
  29. /**
  30. * @brief Returns the parent interop object for a GUI layout or a GUI panel.
  31. */
  32. ScriptGUILayout* getParent() const { return mParent; }
  33. /**
  34. * @brief Sets an interop object for a GUI layout or a panel as this object's parent.
  35. */
  36. void setParent(ScriptGUILayout* parent) { mParent = parent; }
  37. protected:
  38. /**
  39. * @brief Initializes the interop object with a previously initialized GUI
  40. * element. You must call this before using this object.
  41. */
  42. void initialize(GUIElementBase* element);
  43. /**
  44. * @copydoc ScriptObjectBase::_onManagedInstanceDeleted
  45. */
  46. virtual void _onManagedInstanceDeleted() override;
  47. /**
  48. * @brief Triggered when the focus changes for the underlying GUIElementBase.
  49. */
  50. static void onFocusChanged(MonoObject* instance, bool focus);
  51. bool mIsDestroyed;
  52. GUIElementBase* mElement;
  53. ScriptGUILayout* mParent;
  54. };
  55. /**
  56. * @brief A more specialized implementation of ScriptGUIElementBaseTBase that
  57. * references a specific GUI element type instead of the generic GUIElementBase.
  58. */
  59. template <class Type>
  60. class TScriptGUIElementBase : public ScriptObject<Type, ScriptGUIElementBaseTBase>
  61. {
  62. public:
  63. virtual ~TScriptGUIElementBase() {}
  64. protected:
  65. TScriptGUIElementBase(MonoObject* instance, GUIElementBase* element)
  66. :ScriptObject(instance)
  67. {
  68. initialize(element);
  69. }
  70. /**
  71. * @copydoc ScriptObjectBase::_onManagedInstanceDeleted
  72. */
  73. void _onManagedInstanceDeleted()
  74. {
  75. // Elements with a GUIWidget parent are destroyed automatically when widget is destroyed, but those without one
  76. // we need to destroy manually.
  77. if (getGUIElement()->_getParentWidget() == nullptr)
  78. destroy();
  79. ScriptObject::_onManagedInstanceDeleted();
  80. }
  81. };
  82. /**
  83. * @brief Interop class between C++ & CLR for all elements inheriting from
  84. * GUIElement.
  85. */
  86. class BS_SCR_BE_EXPORT ScriptGUIElementTBase : public ScriptGUIElementBaseTBase
  87. {
  88. public:
  89. ScriptGUIElementTBase(MonoObject* instance);
  90. virtual ~ScriptGUIElementTBase() {}
  91. /**
  92. * @copydoc ScriptGUIElementBaseTBase::destroy
  93. */
  94. virtual void destroy() override;
  95. };
  96. /**
  97. * @brief A more specialized implementation of ScriptGUIElementTBase that
  98. * references a specific GUI element type instead of the generic GUIElement.
  99. */
  100. template <class Type>
  101. class TScriptGUIElement : public ScriptObject<Type, ScriptGUIElementTBase>
  102. {
  103. public:
  104. virtual ~TScriptGUIElement() {}
  105. protected:
  106. TScriptGUIElement(MonoObject* instance, GUIElementBase* element)
  107. :ScriptObject(instance)
  108. {
  109. initialize(element);
  110. }
  111. /**
  112. * @copydoc ScriptObjectBase::_onManagedInstanceDeleted
  113. */
  114. void _onManagedInstanceDeleted()
  115. {
  116. // Elements with a GUIWidget parent are destroyed automatically when widget is destroyed, but those without one
  117. // we need to destroy manually.
  118. if (!mIsDestroyed && getGUIElement()->_getParentWidget() == nullptr)
  119. destroy();
  120. ScriptObject::_onManagedInstanceDeleted();
  121. }
  122. };
  123. /**
  124. * @brief Interop class between C++ & CLR for GUIElement. This includes only base
  125. * methods belonging directly to GUIElement while specific GUI element
  126. * implementations have their own interop classes.
  127. */
  128. class BS_SCR_BE_EXPORT ScriptGUIElement : public ScriptObject<ScriptGUIElement>
  129. {
  130. public:
  131. SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "GUIElement")
  132. typedef void(__stdcall *OnFocusChangedThunkDef) (MonoObject*, bool, MonoException**);
  133. static OnFocusChangedThunkDef onFocusChangedThunk;
  134. private:
  135. ScriptGUIElement(MonoObject* instance);
  136. /************************************************************************/
  137. /* CLR HOOKS */
  138. /************************************************************************/
  139. static void internal_destroy(ScriptGUIElementBaseTBase* nativeInstance);
  140. static void internal_setVisible(ScriptGUIElementBaseTBase* nativeInstance, bool visible);
  141. static void internal_setFocus(ScriptGUIElementBaseTBase* nativeInstance, bool focus);
  142. static Rect2I internal_getBounds(ScriptGUIElementBaseTBase* nativeInstance);
  143. static void internal_setBounds(ScriptGUIElementBaseTBase* nativeInstance, Rect2I bounds);
  144. static Rect2I internal_getVisibleBounds(ScriptGUIElementBaseTBase* nativeInstance);
  145. static void internal_SetPosition(ScriptGUIElementBaseTBase* nativeInstance, INT32 x, INT32 y);
  146. static void internal_SetWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 width);
  147. static void internal_SetFlexibleWidth(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minWidth, UINT32 maxWidth);
  148. static void internal_SetHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 height);
  149. static void internal_SetFlexibleHeight(ScriptGUIElementBaseTBase* nativeInstance, UINT32 minHeight, UINT32 maxHeight);
  150. static void internal_SetContextMenu(ScriptGUIElementBaseTBase* nativeInstance, ScriptContextMenu* contextMenu);
  151. static void internal_ResetDimensions(ScriptGUIElementBaseTBase* nativeInstance);
  152. };
  153. }