BsScriptEditorWindow.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 bool internal_isActive(ScriptEditorWindow* thisPtr);
  92. static bool internal_isPointerHovering(ScriptEditorWindow* thisPtr);
  93. static void internal_screenToWindowPos(ScriptEditorWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos);
  94. static void internal_windowToScreenPos(ScriptEditorWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos);
  95. static UINT32 internal_getWidth(ScriptEditorWindow* thisPtr);
  96. static UINT32 internal_getHeight(ScriptEditorWindow* thisPtr);
  97. static void internal_getBounds(ScriptEditorWindow* thisPtr, Rect2I* bounds);
  98. };
  99. /** @} */
  100. /** @addtogroup SBansheeEditor
  101. * @{
  102. */
  103. /**
  104. * Editor widget implementation that handles managed editor window implementations. Each implementation is wrapped in
  105. * this object and then managed by its parent interop object of ScriptEditorWindow type.
  106. */
  107. class BS_SCR_BED_EXPORT ScriptEditorWidget : public EditorWidgetBase
  108. {
  109. public:
  110. /**
  111. * Constructs a new managed widget.
  112. *
  113. * @param[in] ns Namespace of the widget type.
  114. * @param[in] type Name of the widget type.
  115. * @param[in] defaultWidth Default width of the widget when initially created.
  116. * @param[in] defaultHeight Default height of the widget when initially created.
  117. * @param[in] localUndoRedo Determines should the window use a local undo/redo stack instead of the global one.
  118. * @param[in] parentContainer Container to initially dock the widget in.
  119. */
  120. ScriptEditorWidget(const String& ns, const String& type, UINT32 defaultWidth,
  121. UINT32 defaultHeight, bool localUndoRedo, EditorWidgetContainer& parentContainer);
  122. ~ScriptEditorWidget();
  123. /**
  124. * Attempts to create a managed instance for the editor window described by the type provided upon construction.
  125. *
  126. * @return True if the managed instance was created.
  127. */
  128. bool createManagedInstance();
  129. /** Checks has the OnInitialize method been called yet. */
  130. bool isInitialized() const { return mIsInitialized; }
  131. /** @copydoc EditorWidgetBase::update */
  132. void update() override;
  133. /**
  134. * Loads all required mono methods, fields and types required for operation of this object. Must be called after
  135. * construction and after assembly refresh.
  136. *
  137. * @param[in] windowClass Mono class to load the types from.
  138. */
  139. void reloadMonoTypes(MonoClass* windowClass);
  140. /** Triggers OnInitialize callbacks on the managed instance. */
  141. void triggerOnInitialize();
  142. /** Triggers OnDestroy callbacks on the managed instance. */
  143. void triggerOnDestroy();
  144. /**
  145. * Sets the parent interop object that handles part of the communication between this object and the managed
  146. * instance.
  147. */
  148. void setScriptOwner(ScriptEditorWindow* owner) { mScriptOwner = owner; }
  149. /** Returns the managed instance for the editor window represented by this object. */
  150. MonoObject* getManagedInstance() const { return mManagedInstance; }
  151. private:
  152. typedef void(__stdcall *OnInitializeThunkDef) (MonoObject*, MonoException**);
  153. typedef void(__stdcall *OnDestroyThunkDef) (MonoObject*, MonoException**);
  154. typedef void(__stdcall *UpdateThunkDef) (MonoObject*, MonoException**);
  155. String mNamespace;
  156. String mTypename;
  157. OnInitializeThunkDef mOnInitializeThunk;
  158. OnDestroyThunkDef mOnDestroyThunk;
  159. UpdateThunkDef mUpdateThunk;
  160. MonoObject* mManagedInstance;
  161. MonoMethod* mGetDisplayName;
  162. ScriptEditorWindow* mScriptOwner;
  163. ScriptGUILayout* mContentsPanel;
  164. bool mIsInitialized;
  165. bool mHasLocalUndoRedo;
  166. };
  167. /** @} */
  168. }