BsScriptEditorWindow.h 7.6 KB

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