making_main_screen_plugins.rst 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. This plugin mechanism also allows you to
  8. create new UIs in the central part of the editor, similarly to the basic 2D, 3D,
  9. Script and AssetLib views. Such editor plugins are referred as "Main screen
  10. plugins".
  11. This tutorial leads you through the creation of a basic main screen plugin. With
  12. this plugin example, we want to demonstrate:
  13. - Creating a main screen plugin
  14. - Linking the main screen to another plugin GUI element (such as a Tab panel,
  15. similar to the Inspector tab)
  16. For the sake of simplicity, the two GUI elements of our main screen plugin will
  17. both consist in a Label and a Button. Pressing one element's button will display
  18. some text on the other's label node.
  19. Initializing the plugin
  20. -----------------------
  21. The plugin itself is a Godot project. It is best to set its contents in an
  22. ``addons/my_plugin_name/`` structure. The only files that lie in the root folder
  23. are the project.godot file, and the project icon.
  24. In the ``addons/my_plugin_name/`` folder, we create the ``plugin.cfg`` file as
  25. described in the :ref:`doc_making_plugins` page.
  26. ::
  27. [plugin]
  28. name="Main screen plugin demo"
  29. description="A plugin that adds a main screen panel and a side-panel which communicate with each other."
  30. author="Your Name Here"
  31. version="1.0.0"
  32. script="main_screen_plugin.gd"
  33. We also initialize the file targeted by the ``script=`` property of the ``.cfg``
  34. file. In our example, ``main_screen_plugin.gd``.
  35. ::
  36. tool
  37. extends EditorPlugin
  38. func _enter_tree():
  39. pass
  40. func _exit_tree():
  41. pass
  42. func has_main_screen():
  43. return true
  44. func make_visible(visible):
  45. pass
  46. func get_plugin_name():
  47. return "Main Screen Plugin"
  48. The important part in this script is the ``has_main_screen()`` function, which is
  49. overloaded so it returns true. This function is automatically called by the
  50. editor on plugin activation, to tell it that this plugin adds a new center view to
  51. the editor. For now, we'll leave this script as-is and we'll come back to it
  52. later.
  53. Scenes
  54. ------
  55. The ``main_screen_plugin.gd`` file will be responsible for each of our plugin's
  56. UI element instantiation, and it will also manage the communication between them.
  57. As a matter of fact, we wish to design each UI element in their own scene.
  58. Different scenes are not aware of each other unless they are both children of a
  59. parent scene, yet they will then require ``get_node("../brother")`` accessors.
  60. Such practice is more likely to produce errors at runtime, especially if these
  61. scenes do not share the same parent node. This is why, they should only be
  62. allowed to access their children.
  63. So, in order to communicate information to another scene, the best design is to
  64. define signals. If a user action in a UI scene #1 has to trigger something in
  65. another UI scene #2, then this user action has to emit a signal from scene #1,
  66. and scene #2 will be connected to that signal. Since all of our UI scenes will
  67. be instanced by ``main_screen_plugin.gd`` script, this one script will also
  68. connect each of them to the required signals.
  69. .. note:: If the ``main_screen_plugin.gd`` instantiates the UI scenes, won't
  70. they be brothers nodes then?
  71. Not necessarily: this script may add all UI scenes as children of the same node
  72. of the editor's scene tree - but maybe it won't. And the ``main_screen_plugin.gd``
  73. script will *not* be the parent node of any instantiated scene because it is a
  74. script, not a node! This script will only hold references to instantiated
  75. scenes.
  76. Main screen scene
  77. -----------------
  78. Create a new scene with a ``Panel`` root node. Select this root node and, in the
  79. viewport, click the ``Layout`` menu and select ``Full Rect``. The panel now uses
  80. all the space available in the viewport. Now, let's add a new script on the root
  81. node. Name it ``main_panel.gd``.
  82. We then add 2 children to this Panel node: first a ``Button`` node. Place it
  83. anywhere on the Panel.
  84. Now we need to define a behaviour when this button is pressed. This is covered
  85. by the :ref:`Handling a signal <doc_scripting_handling_a_signal>` page, so this
  86. part will not be described in details in this tutorial.
  87. Select the Button node and click the ``Node`` side dock.
  88. Select the ``pressed()`` signal and click the ``Connect`` button (you can also
  89. double-click the ``pressed()`` signal instead). In the window that opened,
  90. select the Panel node (we will centralize all behaviors in its attached
  91. script). Keep the default function name, make sure that the ``Make function``
  92. toggle is ON and hit ``Connect``. This creates an ``on_Button_pressed()``
  93. function in the ``main_panel.gd`` script, that will be called every time the
  94. button is pressed.
  95. As the button gets pressed, we want the side-panel's ``Label`` node to show a
  96. specific text. As explained above, we cannot directly access the target scene,
  97. so we'll emit a signal instead. The ``main_screen_plugin.gd`` script will then
  98. connect this signal to the target scene. Let's continue in the ``main_panel.gd``
  99. script:
  100. ::
  101. tool
  102. extends Panel
  103. signal main_button_pressed(value)
  104. func on_Button_pressed():
  105. emit_signal("main_button_pressed", "Hello from main screen!")
  106. In the same way, this main scene's Label node has to show a value when it
  107. receives a specific signal. Let's create a new
  108. ``_on_side_button_pressed(text_to_show)`` function for this purpose:
  109. ::
  110. func _on_side_button_pressed(text_to_show):
  111. $Label.text = text_to_show
  112. We are done for the main screen panel. Save the scene as ``main_panel.tscn``.
  113. Tabbed panel scene
  114. ------------------
  115. The tabbed panel scene is almost identical to the main panel scene. You can
  116. either duplicate the ``main_panel.tscn`` file and name the new file
  117. ``side_panel.tscn``, or re-create it from a new scene by following the previous
  118. section again. However, you will have to create a new script and attach it to
  119. the Panel root node. Save it as ``side_panel.gd``. Its content is slightly
  120. different, as the signal emitted and the target function have different names.
  121. Here is the script's full content:
  122. ::
  123. tool
  124. extends Panel
  125. signal side_button_pressed(value)
  126. func on_Button_pressed():
  127. emit_signal("side_button_pressed", "Hello from side panel!")
  128. func _on_main_button_pressed(text_to_show):
  129. $Label.text = text_to_show
  130. Connecting the two scenes in the plugin script
  131. ----------------------------------------------
  132. We now need to update the ``main_screen_plugin.gd`` script so the plugin
  133. instances our 2 GUI scenes and places them at the right places in the editor.
  134. Here is the full ``main.gd``:
  135. ::
  136. tool
  137. extends EditorPlugin
  138. const MainPanel = preload("res://addons/my_plugin_name/main_panel.tscn")
  139. const SidePanel = preload("res://addons/my_plugin_name/side_panel.tscn")
  140. var main_panel_instance
  141. var side_panel_instance
  142. func _enter_tree():
  143. main_panel_instance = MainPanel.instance()
  144. side_panel_instance = SidePanel.instance()
  145. # Add the main panel to the editor's main viewport.
  146. get_editor_interface().get_editor_viewport().add_child(main_panel_instance)
  147. # Add the side panel to the Upper Left (UL) dock slot of the left part of the editor.
  148. # The editor has 4 dock slots (UL, UR, BL, BR) on each side (left/right) of the main screen.
  149. add_control_to_dock(DOCK_SLOT_LEFT_UL, side_panel_instance)
  150. # Hide the main panel
  151. make_visible(false)
  152. func _exit_tree():
  153. main_panel_instance.queue_free()
  154. side_panel_instance.queue_free()
  155. func _ready():
  156. main_panel_instance.connect("main_button_pressed", side_panel_instance, "_on_main_button_pressed")
  157. side_panel_instance.connect("side_button_pressed", main_panel_instance, "_on_side_button_pressed")
  158. func has_main_screen():
  159. return true
  160. func make_visible(visible):
  161. if visible:
  162. main_panel_instance.show()
  163. else:
  164. main_panel_instance.hide()
  165. func get_plugin_name():
  166. return "Main Screen Plugin"
  167. A couple of specific lines were added. First, we defined the constants that
  168. contain our 2 GUI packed scenes (``MainPanel`` and ``SidePanel``). We will use
  169. these resources to instance both scenes.
  170. The ``_enter_tree()`` function is called before ``_ready()``. This is where we
  171. actually instance the 2 GUI scenes, and add them as children of specific parts
  172. of the editor. The side panel case is similar to the example shown in
  173. :ref:`doc_making_plugins` page: we add the scene in an editor dock. We specified
  174. it will be placed in the left-side dock, upper-left tab.
  175. ``EditorPlugin`` class does not provide any function to add an element in the
  176. main viewport. We thus have to use the
  177. ``get_editor_interface().get_editor_viewport()`` to obtain this viewport and add
  178. our main panel instance as a child to it. We call the ``make_visible(false)``
  179. function to hide the main panel so it is not directly shown when first
  180. activating the plugin.
  181. The ``_exit_tree()`` is pretty straightforward. It is automatically called when
  182. the plugin is deactivated. It is then important to ``queue_free()`` the elements
  183. previously instanced to preserve memory. If you don't, the elements will
  184. effectively be invisible in the editor, but they will remain present in the
  185. memory. Multiple de-activations/re-activations will then increase memory usage
  186. without any way to free it, which is not good.
  187. Finally 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 another main viewport button such as 2D, 3D or Script.
  190. Try the plugin
  191. --------------
  192. Activate the plugin in the Project Settings. You'll observe a new button next to
  193. 2D, 3D, Script above the main viewport. You'll also notice a new tab in the left
  194. dock. Try to click the buttons in both side and main panels: events are emitted
  195. and caught by the corresponding target scene to change the Label caption inside it.