BsScriptEditorWindow.h 7.0 KB

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