BsScriptEditorWindow.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 void internal_screenToWindowPos(ScriptEditorWindow* thisPtr, Vector2I screenPos, Vector2I* windowPos);
  110. static void internal_windowToScreenPos(ScriptEditorWindow* thisPtr, Vector2I windowPos, Vector2I* screenPos);
  111. static UINT32 internal_getWidth(ScriptEditorWindow* thisPtr);
  112. static UINT32 internal_getHeight(ScriptEditorWindow* thisPtr);
  113. };
  114. /**
  115. * @brief Editor widget implementation that handles managed editor window implementations.
  116. * Each implementation is wrapped in this object and then managed by its parent interop
  117. * object of ScriptEditorWindow type.
  118. */
  119. class BS_SCR_BED_EXPORT ScriptEditorWidget : public EditorWidgetBase
  120. {
  121. public:
  122. /**
  123. * @brief Constructs a new managed widget.
  124. *
  125. * @param ns Namespace of the widget type.
  126. * @param type Name of the widget type.
  127. * @param defaultWidth Default width of the widget when initially created.
  128. * @param defaultHeight Default height of the widget when initially created.
  129. * @param parentContainer Container to initially dock the widget in.
  130. */
  131. ScriptEditorWidget(const String& ns, const String& type, UINT32 defaultWidth,
  132. UINT32 defaultHeight, EditorWidgetContainer& parentContainer);
  133. ~ScriptEditorWidget();
  134. /**
  135. * @brief Attempts to create a managed instance for the editor window described by the
  136. * type provided upon construction.
  137. *
  138. * @return True if the managed instance was created.
  139. */
  140. bool createManagedInstance();
  141. /**
  142. * @brief Checks has the OnInitialize method been called yet.
  143. */
  144. bool isInitialized() const { return mIsInitialized; }
  145. /**
  146. * @copydoc EditorWidgetBase::update
  147. */
  148. void update() override;
  149. /**
  150. * @brief Loads all required mono methods, fields and types required
  151. * for operation of this object. Must be called after construction
  152. * and after assembly refresh.
  153. *
  154. * @param windowClass Mono class to load the types from.
  155. */
  156. void reloadMonoTypes(MonoClass* windowClass);
  157. /**
  158. * @brief Triggers OnInitialize callbacks on the managed instance.
  159. */
  160. void triggerOnInitialize();
  161. /**
  162. * @brief Triggers OnDestroy callbacks on the managed instance.
  163. */
  164. void triggerOnDestroy();
  165. /**
  166. * @brief Sets the parent interop object that handles part of the communication
  167. * between this object and the managed instance.
  168. */
  169. void setScriptOwner(ScriptEditorWindow* owner) { mScriptOwner = owner; }
  170. /**
  171. * @brief Returns the managed instance for the editor window
  172. * represented by this object.
  173. */
  174. MonoObject* getManagedInstance() const { return mManagedInstance; }
  175. private:
  176. typedef void(__stdcall *OnInitializeThunkDef) (MonoObject*, MonoException**);
  177. typedef void(__stdcall *OnDestroyThunkDef) (MonoObject*, MonoException**);
  178. typedef void(__stdcall *UpdateThunkDef) (MonoObject*, MonoException**);
  179. String mNamespace;
  180. String mTypename;
  181. OnInitializeThunkDef mOnInitializeThunk;
  182. OnDestroyThunkDef mOnDestroyThunk;
  183. UpdateThunkDef mUpdateThunk;
  184. MonoObject* mManagedInstance;
  185. MonoMethod* mGetDisplayName;
  186. ScriptEditorWindow* mScriptOwner;
  187. ScriptGUILayout* mContentsPanel;
  188. bool mIsInitialized;
  189. };
  190. }