BsScriptEditorWindow.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. #include "BsScriptEditorPrerequisites.h"
  3. #include "BsScriptObject.h"
  4. #include "BsEditorWidget.h"
  5. #include "BsVector2I.h"
  6. namespace BansheeEngine
  7. {
  8. class ScriptEditorWidget;
  9. /**
  10. * @brief Interop class between C++ & CLR for ScriptEditorWidget.
  11. */
  12. class BS_SCR_BED_EXPORT ScriptEditorWindow : public ScriptObject<ScriptEditorWindow, PersistentScriptObjectBase>
  13. {
  14. /**
  15. * @brief Contains data about the managed handle to an editor window.
  16. */
  17. struct EditorWindowHandle
  18. {
  19. uint32_t gcHandle;
  20. ScriptEditorWindow* nativeObj;
  21. };
  22. public:
  23. SCRIPT_OBJ(EDITOR_ASSEMBLY, "BansheeEditor", "EditorWindow")
  24. ~ScriptEditorWindow();
  25. /**
  26. * @brief Returns the internal wrapped editor widget.
  27. */
  28. EditorWidgetBase* getEditorWidget() const;
  29. /**
  30. * @brief Checks has the native widget been destroyed.
  31. */
  32. bool isDestroyed() const { return mIsDestroyed; }
  33. /**
  34. * @brief Finds all editor window implementations in managed assemblies and registers
  35. * them with the editor widget system.
  36. */
  37. static void registerManagedEditorWindows();
  38. /**
  39. * @brief Removes all editor widgets registered previously with ::registerManagedEditorWindows.
  40. * Useful during assembly refresh when editor window implementations might be added/removed.
  41. */
  42. static void clearRegisteredEditorWindow();
  43. private:
  44. friend class ScriptEditorWidget;
  45. ScriptEditorWindow(ScriptEditorWidget* editorWidget);
  46. /**
  47. * @brief Triggered when the native editor widget is resized.
  48. */
  49. void onWidgetResized(UINT32 width, UINT32 height);
  50. /**
  51. * @brief Triggered when the native editor widget gains or loses focus.
  52. */
  53. void onFocusChanged(bool inFocus);
  54. /**
  55. * @brief Triggered when assembly refresh has started.
  56. */
  57. void onAssemblyRefreshStarted();
  58. /**
  59. * @copydoc ScriptObjectBase::_onManagedInstanceDeleted
  60. */
  61. void _onManagedInstanceDeleted() override;
  62. /**
  63. * @copydoc ScriptObjectBase::beginRefresh
  64. */
  65. ScriptObjectBackup beginRefresh() override;
  66. /**
  67. * @copydoc ScriptObjectBase::endRefresh
  68. */
  69. void endRefresh(const ScriptObjectBackup& backupData) override;
  70. /**
  71. * @copydoc ScriptObjectBase::_createManagedInstance
  72. */
  73. MonoObject* _createManagedInstance(bool construct) override;
  74. String mName;
  75. ScriptEditorWidget* mEditorWidget;
  76. HEvent mOnWidgetResizedConn;
  77. HEvent mOnFocusChangedConn;
  78. HEvent mOnAssemblyRefreshStartedConn;
  79. bool mRefreshInProgress;
  80. bool mIsDestroyed;
  81. static MonoMethod* onResizedMethod;
  82. static MonoMethod* onFocusChangedMethod;
  83. static MonoField* guiPanelField;
  84. // Global editor window management methods
  85. /**
  86. * @brief Registers a newly created editor window interop object and adds it to
  87. * a list of currently active editor windows.
  88. */
  89. static void registerScriptEditorWindow(ScriptEditorWindow* editorWindow);
  90. /**
  91. * @brief Removes a window from the active editor window list.
  92. *
  93. * @param windowTypeName Name of the window type. Provided by EditorWidget::getName.
  94. */
  95. static void unregisterScriptEditorWindow(const String& windowTypeName);
  96. /**
  97. * @brief Callback that is triggered when user requests a widget to be opened.
  98. */
  99. static EditorWidgetBase* openEditorWidgetCallback(String ns, String type, UINT32 width, UINT32 height,
  100. EditorWidgetContainer& parentContainer);
  101. static UnorderedMap<String, EditorWindowHandle> OpenScriptEditorWindows;
  102. static Vector<String> AvailableWindowTypes;
  103. /************************************************************************/
  104. /* CLR HOOKS */
  105. /************************************************************************/
  106. static MonoObject* internal_createOrGetInstance(MonoString* ns, MonoString* typeName);
  107. static MonoObject* internal_getInstance(MonoString* ns, MonoString* typeName);
  108. static bool internal_hasFocus(ScriptEditorWindow* thisPtr);
  109. static bool internal_isActive(ScriptEditorWindow* thisPtr);
  110. static bool internal_isPointerHovering(ScriptEditorWindow* thisPtr);
  111. static void internal_screenToWindowPos(ScriptEditorWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos);
  112. static void internal_windowToScreenPos(ScriptEditorWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos);
  113. static UINT32 internal_getWidth(ScriptEditorWindow* thisPtr);
  114. static UINT32 internal_getHeight(ScriptEditorWindow* thisPtr);
  115. static void internal_getBounds(ScriptEditorWindow* thisPtr, Rect2I* bounds);
  116. };
  117. /**
  118. * @brief Editor widget implementation that handles managed editor window implementations.
  119. * Each implementation is wrapped in this object and then managed by its parent interop
  120. * object of ScriptEditorWindow type.
  121. */
  122. class BS_SCR_BED_EXPORT ScriptEditorWidget : public EditorWidgetBase
  123. {
  124. public:
  125. /**
  126. * @brief Constructs a new managed widget.
  127. *
  128. * @param ns Namespace of the widget type.
  129. * @param type Name of the widget type.
  130. * @param defaultWidth Default width of the widget when initially created.
  131. * @param defaultHeight Default height of the widget when initially created.
  132. * @param parentContainer Container to initially dock the widget in.
  133. */
  134. ScriptEditorWidget(const String& ns, const String& type, UINT32 defaultWidth,
  135. UINT32 defaultHeight, EditorWidgetContainer& parentContainer);
  136. ~ScriptEditorWidget();
  137. /**
  138. * @brief Attempts to create a managed instance for the editor window described by the
  139. * type provided upon construction.
  140. *
  141. * @return True if the managed instance was created.
  142. */
  143. bool createManagedInstance();
  144. /**
  145. * @brief Checks has the OnInitialize method been called yet.
  146. */
  147. bool isInitialized() const { return mIsInitialized; }
  148. /**
  149. * @copydoc EditorWidgetBase::update
  150. */
  151. void update() override;
  152. /**
  153. * @brief Loads all required mono methods, fields and types required
  154. * for operation of this object. Must be called after construction
  155. * and after assembly refresh.
  156. *
  157. * @param windowClass Mono class to load the types from.
  158. */
  159. void reloadMonoTypes(MonoClass* windowClass);
  160. /**
  161. * @brief Triggers OnInitialize callbacks on the managed instance.
  162. */
  163. void triggerOnInitialize();
  164. /**
  165. * @brief Triggers OnDestroy callbacks on the managed instance.
  166. */
  167. void triggerOnDestroy();
  168. /**
  169. * @brief Sets the parent interop object that handles part of the communication
  170. * between this object and the managed instance.
  171. */
  172. void setScriptOwner(ScriptEditorWindow* owner) { mScriptOwner = owner; }
  173. /**
  174. * @brief Returns the managed instance for the editor window
  175. * represented by this object.
  176. */
  177. MonoObject* getManagedInstance() const { return mManagedInstance; }
  178. private:
  179. typedef void(__stdcall *OnInitializeThunkDef) (MonoObject*, MonoException**);
  180. typedef void(__stdcall *OnDestroyThunkDef) (MonoObject*, MonoException**);
  181. typedef void(__stdcall *UpdateThunkDef) (MonoObject*, MonoException**);
  182. String mNamespace;
  183. String mTypename;
  184. OnInitializeThunkDef mOnInitializeThunk;
  185. OnDestroyThunkDef mOnDestroyThunk;
  186. UpdateThunkDef mUpdateThunk;
  187. MonoObject* mManagedInstance;
  188. MonoMethod* mGetDisplayName;
  189. ScriptEditorWindow* mScriptOwner;
  190. ScriptGUILayout* mContentsPanel;
  191. bool mIsInitialized;
  192. };
  193. }