making_main_screen_plugins.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. .. _doc_making_main_screen_plugins:
  2. Making main screen plugins
  3. ==========================
  4. What this tutorial covers
  5. -------------------------
  6. Main screen plugins allow you to create
  7. new UIs in the central part of the editor, which appear next to the
  8. "2D", "3D", "Script", and "AssetLib" buttons. Such editor plugins are
  9. referred as "Main screen plugins".
  10. This tutorial leads you through the creation of a basic main screen plugin.
  11. For the sake of simplicity, our main screen plugin will contain a single
  12. button that prints text to the console.
  13. Initializing the plugin
  14. -----------------------
  15. First create a new plugin from the Plugins menu. For this tutorial, we'll put
  16. it in a folder called ``main_screen``, but you can use any name you'd like.
  17. The plugin script will come with ``_enter_tree()`` and ``_exit_tree()``
  18. methods, but for a main screen plugin we need to add a few extra methods.
  19. Add five extra methods such that the script looks like this:
  20. .. tabs::
  21. .. code-tab:: gdscript GDScript
  22. @tool
  23. extends EditorPlugin
  24. func _enter_tree():
  25. pass
  26. func _exit_tree():
  27. pass
  28. func _has_main_screen():
  29. return true
  30. func _make_visible(visible):
  31. pass
  32. func _get_plugin_name():
  33. return "Main Screen Plugin"
  34. func _get_plugin_icon():
  35. return get_editor_interface().get_base_control().get_theme_icon("Node", "EditorIcons")
  36. .. code-tab:: csharp
  37. #if TOOLS
  38. using Godot;
  39. [Tool]
  40. public partial class MainScreenPlugin : EditorPlugin
  41. {
  42. public override void _EnterTree()
  43. {
  44. }
  45. public override void _ExitTree()
  46. {
  47. }
  48. public override bool _HasMainScreen()
  49. {
  50. return true;
  51. }
  52. public override void _MakeVisible(bool visible)
  53. {
  54. }
  55. public override string _GetPluginName()
  56. {
  57. return "Main Screen Plugin";
  58. }
  59. public override Texture2D _GetPluginIcon()
  60. {
  61. return GetEditorInterface().GetBaseControl().GetThemeIcon("Node", "EditorIcons");
  62. }
  63. }
  64. #endif
  65. The important part in this script is the ``_has_main_screen()`` function,
  66. which is overloaded so it returns ``true``. This function is automatically
  67. called by the editor on plugin activation, to tell it that this plugin
  68. adds a new center view to the editor. For now, we'll leave this script
  69. as-is and we'll come back to it later.
  70. Main screen scene
  71. -----------------
  72. Create a new scene with a root node derived from ``Control`` (for this
  73. example plugin, we'll make the root node a ``CenterContainer``).
  74. Select this root node, and in the viewport, click the ``Layout`` menu
  75. and select ``Full Rect``. You also need to enable the ``Expand``
  76. vertical size flag in the inspector.
  77. The panel now uses all the space available in the main viewport.
  78. Next, let's add a button to our example main screen plugin.
  79. Add a ``Button`` node, and set the text to "Print Hello" or similar.
  80. Add a script to the button like this:
  81. .. tabs::
  82. .. code-tab:: gdscript GDScript
  83. @tool
  84. extends Button
  85. func _on_PrintHello_pressed():
  86. print("Hello from the main screen plugin!")
  87. .. code-tab:: csharp
  88. using Godot;
  89. [Tool]
  90. public partial class PrintHello : Button
  91. {
  92. public void OnPrintHelloPressed()
  93. {
  94. GD.Print("Hello from the main screen plugin!");
  95. }
  96. }
  97. Then connect the "pressed" signal to itself. If you need help with signals,
  98. see the :ref:`doc_signals` article.
  99. We are done with the main screen panel. Save the scene as ``main_panel.tscn``.
  100. Update the plugin script
  101. ------------------------
  102. We need to update the ``main_screen_plugin.gd`` script so the plugin
  103. instances our main panel scene and places it where it needs to be.
  104. Here is the full plugin script:
  105. .. tabs::
  106. .. code-tab:: gdscript GDScript
  107. @tool
  108. extends EditorPlugin
  109. const MainPanel = preload("res://addons/main_screen/main_panel.tscn")
  110. var main_panel_instance
  111. func _enter_tree():
  112. main_panel_instance = MainPanel.instantiate()
  113. # Add the main panel to the editor's main viewport.
  114. get_editor_interface().get_editor_main_screen().add_child(main_panel_instance)
  115. # Hide the main panel. Very much required.
  116. _make_visible(false)
  117. func _exit_tree():
  118. if main_panel_instance:
  119. main_panel_instance.queue_free()
  120. func _has_main_screen():
  121. return true
  122. func _make_visible(visible):
  123. if main_panel_instance:
  124. main_panel_instance.visible = visible
  125. func _get_plugin_name():
  126. return "Main Screen Plugin"
  127. func _get_plugin_icon():
  128. # Must return some kind of Texture for the icon.
  129. return get_editor_interface().get_base_control().get_theme_icon("Node", "EditorIcons")
  130. .. code-tab:: csharp
  131. #if TOOLS
  132. using Godot;
  133. [Tool]
  134. public partial class MainScreenPlugin : EditorPlugin
  135. {
  136. PackedScene MainPanel = ResourceLoader.Load<PackedScene>("res://addons/main_screen/main_panel.tscn");
  137. Control MainPanelInstance;
  138. public override void _EnterTree()
  139. {
  140. MainPanelInstance = (Control)MainPanel.Instantiate();
  141. // Add the main panel to the editor's main viewport.
  142. GetEditorInterface().GetEditorMainScreen().AddChild(MainPanelInstance);
  143. // Hide the main panel. Very much required.
  144. _MakeVisible(false);
  145. }
  146. public override void _ExitTree()
  147. {
  148. if (MainPanelInstance != null)
  149. {
  150. MainPanelInstance.QueueFree();
  151. }
  152. }
  153. public override bool _HasMainScreen()
  154. {
  155. return true;
  156. }
  157. public override void _MakeVisible(bool visible)
  158. {
  159. if (MainPanelInstance != null)
  160. {
  161. MainPanelInstance.Visible = visible;
  162. }
  163. }
  164. public override string _GetPluginName()
  165. {
  166. return "Main Screen Plugin";
  167. }
  168. public override Texture2D _GetPluginIcon()
  169. {
  170. // Must return some kind of Texture for the icon.
  171. return GetEditorInterface().GetBaseControl().GetThemeIcon("Node", "EditorIcons");
  172. }
  173. }
  174. #endif
  175. A couple of specific lines were added. ``MainPanel`` is a constant that holds
  176. a reference to the scene, and we instance it into `main_panel_instance`.
  177. The ``_enter_tree()`` function is called before ``_ready()``. This is where
  178. we instance the main panel scene, and add them as children of specific parts
  179. of the editor. We use ``get_editor_interface().get_editor_main_screen()`` to
  180. obtain the main editor screen and add our main panel instance as a child to it.
  181. We call the ``_make_visible(false)`` function to hide the main panel so
  182. it doesn't compete for space when first activating the plugin.
  183. The ``_exit_tree()`` function is called when the plugin is deactivated.
  184. If the main screen still exists, we call ``queue_free()`` to free the
  185. instance and remove it from memory.
  186. The ``_make_visible()`` function is overridden to hide or show the main
  187. panel as needed. This function is automatically called by the editor when the
  188. user clicks on the main viewport buttons at the top of the editor.
  189. The ``_get_plugin_name()`` and ``_get_plugin_icon()`` functions control
  190. the displayed name and icon for the plugin's main viewport button.
  191. Another function you can add is the ``handles()`` function, which
  192. allows you to handle a node type, automatically focusing the main
  193. screen when the type is selected. This is similar to how clicking
  194. on a 3D node will automatically switch to the 3D viewport.
  195. Try the plugin
  196. --------------
  197. Activate the plugin in the Project Settings. You'll observe a new button next
  198. to 2D, 3D, Script above the main viewport. Clicking it will take you to your
  199. new main screen plugin, and the button in the middle will print text.
  200. If you would like to try a finished version of this plugin,
  201. check out the plugin demos here:
  202. https://github.com/godotengine/godot-demo-projects/tree/master/plugins
  203. If you would like to see a more complete example of what main screen plugins
  204. are capable of, check out the 2.5D demo projects here:
  205. https://github.com/godotengine/godot-demo-projects/tree/master/misc/2.5d