making_main_screen_plugins.rst 8.1 KB

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