BsScriptEditorWindow.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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(bool assemblyRefresh) 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 mIsDestroyed;
  56. static MonoMethod* onResizedMethod;
  57. static MonoMethod* onFocusChangedMethod;
  58. static MonoField* guiPanelField;
  59. static MonoField* undoRedoField;
  60. // Global editor window management methods
  61. /**
  62. * Registers a newly created editor window interop object and adds it to a list of currently active editor windows.
  63. */
  64. static void registerScriptEditorWindow(ScriptEditorWindow* editorWindow);
  65. /**
  66. * Removes a window from the active editor window list.
  67. *
  68. * @param[in] windowTypeName Name of the window type. Provided by EditorWidget::getName.
  69. */
  70. static void unregisterScriptEditorWindow(const String& windowTypeName);
  71. /** Callback that is triggered when user requests a widget to be opened. */
  72. static EditorWidgetBase* openEditorWidgetCallback(String ns, String type, UINT32 width, UINT32 height,
  73. bool localUndoRedo, EditorWidgetContainer& parentContainer);
  74. static UnorderedMap<String, ScriptEditorWindow*> OpenScriptEditorWindows;
  75. static Vector<String> AvailableWindowTypes;
  76. /************************************************************************/
  77. /* CLR HOOKS */
  78. /************************************************************************/
  79. static MonoObject* internal_createOrGetInstance(MonoString* ns, MonoString* typeName);
  80. static MonoObject* internal_getInstance(MonoString* ns, MonoString* typeName);
  81. static MonoArray* internal_getAllWindows();
  82. static bool internal_hasFocus(ScriptEditorWindow* thisPtr);
  83. static void internal_setFocus(ScriptEditorWindow* thisPtr, bool focus);
  84. static void internal_setActive(ScriptEditorWindow* thisPtr, bool active);
  85. static bool internal_isActive(ScriptEditorWindow* thisPtr);
  86. static bool internal_isPointerHovering(ScriptEditorWindow* thisPtr);
  87. static void internal_screenToWindowPos(ScriptEditorWindow* thisPtr, Vector2I* screenPos, Vector2I* windowPos);
  88. static void internal_windowToScreenPos(ScriptEditorWindow* thisPtr, Vector2I* windowPos, Vector2I* screenPos);
  89. static UINT32 internal_getWidth(ScriptEditorWindow* thisPtr);
  90. static UINT32 internal_getHeight(ScriptEditorWindow* thisPtr);
  91. static void internal_getBounds(ScriptEditorWindow* thisPtr, Rect2I* bounds);
  92. };
  93. /** @} */
  94. /** @addtogroup EditorScript
  95. * @{
  96. */
  97. /**
  98. * Editor widget implementation that handles managed editor window implementations. Each implementation is wrapped in
  99. * this object and then managed by its parent interop object of ScriptEditorWindow type.
  100. */
  101. class BS_SCR_BED_EXPORT ScriptEditorWidget : public EditorWidgetBase
  102. {
  103. public:
  104. /**
  105. * Constructs a new managed widget.
  106. *
  107. * @param[in] ns Namespace of the widget type.
  108. * @param[in] type Name of the widget type.
  109. * @param[in] defaultWidth Default width of the widget when initially created.
  110. * @param[in] defaultHeight Default height of the widget when initially created.
  111. * @param[in] localUndoRedo Determines should the window use a local undo/redo stack instead of the global one.
  112. * @param[in] parentContainer Container to initially dock the widget in.
  113. */
  114. ScriptEditorWidget(const String& ns, const String& type, UINT32 defaultWidth,
  115. UINT32 defaultHeight, bool localUndoRedo, EditorWidgetContainer& parentContainer);
  116. ~ScriptEditorWidget();
  117. /**
  118. * Attempts to create a managed instance for the editor window described by the type provided upon construction.
  119. *
  120. * @return True if the managed instance was created.
  121. */
  122. bool createManagedInstance();
  123. /** Checks has the OnInitialize method been called yet. */
  124. bool isInitialized() const { return mIsInitialized; }
  125. /** @copydoc EditorWidgetBase::update */
  126. void update() override;
  127. /**
  128. * Loads all required mono methods, fields and types required for operation of this object. Must be called after
  129. * construction and after assembly refresh.
  130. *
  131. * @param[in] windowClass Mono class to load the types from.
  132. */
  133. void reloadMonoTypes(MonoClass* windowClass);
  134. /** Triggers OnInitialize callbacks on the managed instance. */
  135. void triggerOnInitialize();
  136. /** Triggers OnDestroy callbacks on the managed instance. */
  137. void triggerOnDestroy();
  138. /**
  139. * Sets the parent interop object that handles part of the communication between this object and the managed
  140. * instance.
  141. */
  142. void setScriptOwner(ScriptEditorWindow* owner) { mScriptOwner = owner; }
  143. /** Returns the managed instance for the editor window represented by this object. */
  144. MonoObject* getManagedInstance() const { return mManagedInstance; }
  145. /** Invalidates the managed instance pointer, and optionally frees the GC handle. */
  146. void clearManagedInstance(bool freeHandle);
  147. private:
  148. typedef void(BS_THUNKCALL *OnInitializeThunkDef) (MonoObject*, MonoException**);
  149. typedef void(BS_THUNKCALL *OnDestroyThunkDef) (MonoObject*, MonoException**);
  150. typedef void(BS_THUNKCALL *UpdateThunkDef) (MonoObject*, MonoException**);
  151. String mNamespace;
  152. String mTypename;
  153. OnInitializeThunkDef mOnInitializeThunk;
  154. OnDestroyThunkDef mOnDestroyThunk;
  155. UpdateThunkDef mUpdateThunk;
  156. MonoObject* mManagedInstance;
  157. UINT32 mGCHandle;
  158. MonoMethod* mGetDisplayName;
  159. ScriptEditorWindow* mScriptOwner;
  160. ScriptGUILayout* mContentsPanel;
  161. bool mIsInitialized;
  162. bool mHasLocalUndoRedo;
  163. };
  164. /** @} */
  165. }