BsScriptEditorWindow.h 7.6 KB

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