BsScriptEditorWindow.h 7.8 KB

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