BsScriptEditorWindow.h 6.7 KB

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