BsScriptEditorWindow.h 7.4 KB

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