making_main_screen_plugins.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. .. _doc_making_main_screen_plugins:
  2. Making main screen plugins
  3. ==========================
  4. What this tutorial covers
  5. -------------------------
  6. As seen in the :ref:`doc_making_plugins` page, making a basic plugin that
  7. extends the editor is fairly easy. 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. ::
  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_icon("Node", "EditorIcons")
  36. The important part in this script is the ``has_main_screen()`` function,
  37. which is overloaded so it returns ``true``. This function is automatically
  38. called by the editor on plugin activation, to tell it that this plugin
  39. adds a new center view to the editor. For now, we'll leave this script
  40. as-is and we'll come back to it later.
  41. Main screen scene
  42. -----------------
  43. Create a new scene with a root node derived from ``Control`` (for this
  44. example plugin, we'll make the root node a ``CenterContainer``).
  45. Select this root node, and in the viewport, click the ``Layout`` menu
  46. and select ``Full Rect``. You also need to enable the ``Expand``
  47. vertical size flag in the inspector.
  48. The panel now uses all the space available in the main viewport.
  49. Next, let's add a button to our example main screen plugin.
  50. Add a ``Button`` node, and set the text to "Print Hello" or similar.
  51. Add a script to the button like this:
  52. ::
  53. tool
  54. extends Button
  55. func _on_PrintHello_pressed():
  56. print("Hello from the main screen plugin!")
  57. Then connect the "pressed" signal to itself. If you need help with signals,
  58. see the :ref:`doc_signals` article.
  59. We are done with the main screen panel. Save the scene as ``main_panel.tscn``.
  60. Update the plugin script
  61. ------------------------
  62. We need to update the ``main_screen_plugin.gd`` script so the plugin
  63. instances our main panel scene and places it where it needs to be.
  64. Here is the full plugin script:
  65. ::
  66. tool
  67. extends EditorPlugin
  68. const MainPanel = preload("res://addons/main_screen/main_panel.tscn")
  69. var main_panel_instance
  70. func _enter_tree():
  71. main_panel_instance = MainPanel.instance()
  72. # Add the main panel to the editor's main viewport.
  73. get_editor_interface().get_editor_viewport().add_child(main_panel_instance)
  74. # Hide the main panel. Very much required.
  75. make_visible(false)
  76. func _exit_tree():
  77. if main_panel_instance:
  78. main_panel_instance.queue_free()
  79. func has_main_screen():
  80. return true
  81. func make_visible(visible):
  82. if main_panel_instance:
  83. main_panel_instance.visible = visible
  84. func get_plugin_name():
  85. return "Main Screen Plugin"
  86. func get_plugin_icon():
  87. # Must return some kind of Texture for the icon.
  88. return get_editor_interface().get_base_control().get_icon("Node", "EditorIcons")
  89. A couple of specific lines were added. ``MainPanel`` is a constant that holds
  90. a reference to the scene, and we instance it into `main_panel_instance`.
  91. The ``_enter_tree()`` function is called before ``_ready()``. This is where
  92. we instance the main panel scene, and add them as children of specific parts
  93. of the editor. We use ``get_editor_interface().get_editor_viewport()`` to
  94. obtain the viewport and add our main panel instance as a child to it.
  95. We call the ``make_visible(false)`` function to hide the main panel so
  96. it doesn't compete for space when first activating the plugin.
  97. The ``_exit_tree()`` function is called when the plugin is deactivated.
  98. If the main screen still exists, we call ``queue_free()`` to free the
  99. instance and remove it from memory.
  100. The ``make_visible()`` function is overridden to hide or show the main
  101. panel as needed. This function is automatically called by the editor when the
  102. user clicks on the main viewport buttons at the top of the editor.
  103. The ``get_plugin_name()`` and ``get_plugin_icon()`` functions control
  104. the displayed name and icon for the plugin's main viewport button.
  105. Another function you can add is the ``handles()`` function, which
  106. allows you to handle a node type, automatically focusing the main
  107. screen when the type is selected. This is similar to how clicking
  108. on a 3D node will automatically switch to the 3D viewport.
  109. Try the plugin
  110. --------------
  111. Activate the plugin in the Project Settings. You'll observe a new button next
  112. to 2D, 3D, Script above the main viewport. Clicking it will take you to your
  113. new main screen plugin, and the button in the middle will print text.
  114. If you would like to try a finished version of this plugin,
  115. check out the plugin demos here:
  116. https://github.com/godotengine/godot-demo-projects/tree/master/plugins
  117. If you would like to see a more complete example of what main screen plugins
  118. are capable of, check out the 2.5D demo projects here:
  119. https://github.com/godotengine/godot-demo-projects/tree/master/misc/2.5d