BsScriptEditorWindow.h 7.2 KB

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