Work in progress

The content of this page was not yet updated for Godot 4.2 and may be outdated. If you know how to improve this page or you can confirm that it's up to date, feel free to open a pull request.

Création de plugins pour l'écran principal

Ce que couvre ce tutoriel

Main screen plugins allow you to create new UIs in the central part of the editor, which appear next to the "2D", "3D", "Script", and "AssetLib" buttons. Such editor plugins are referred as "Main screen plugins".

Ce tutoriel vous guide à travers la création d'un plugin de base pour l'écran principal. Par souci de simplicité, notre plugin pour l'écran principal contiendra un seul bouton qui imprimera du texte sur la console.

Initialisation du plugin

Créez d'abord un nouveau plugin à partir du menu Plugins. Pour ce tutoriel, nous le placerons dans un dossier appelé main_screen, mais vous pouvez utiliser le nom que vous souhaitez.

Le script du plugin sera fourni avec les méthodes _enter_tree() et _exit_tree(), mais pour un plugin pour l'écran principal, nous devons ajouter quelques méthodes supplémentaires. Ajoutez cinq méthodes supplémentaires de sorte que le script ressemble à ceci :

@tool
extends EditorPlugin


func _enter_tree():
    pass


func _exit_tree():
    pass


func _has_main_screen():
    return true


func _make_visible(visible):
    pass


func _get_plugin_name():
    return "Main Screen Plugin"


func _get_plugin_icon():
    return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")

The important part in this script is the _has_main_screen() function, which is overloaded so it returns true. This function is automatically called by the editor on plugin activation, to tell it that this plugin adds a new center view to the editor. For now, we'll leave this script as-is and we'll come back to it later.

Scène de l'écran principal

Créer une nouvelle scène avec un nœud racine dérivé de Control (pour cet exemple de plugin, nous allons faire du nœud racine un CenterContainer). Sélectionnez ce nœud racine, et dans la fenêtre d'affichage, cliquez sur le menu Layout et sélectionnez Full Rect. Vous devez également activer le drapeau de taille verticale Expand dans l'inspecteur. Le panneau utilise maintenant tout l'espace disponible dans la fenêtre d'affichage principale.

Ensuite, ajoutons un bouton à notre exemple de plugin pour l'écran principal. Ajoutez un nœud Button et définissez le texte comme "Print Hello" ou similaire. Ajoutez un script au bouton comme ceci :

@tool
extends Button


func _on_print_hello_pressed():
    print("Hello from the main screen plugin!")

Ensuite, connectez le signal "pressed" à lui-même. Si vous avez besoin d'aide pour les signaux, consultez l'article Utiliser les signaux.

Nous avons terminé pour le panneau de l'écran principal. Enregistrez la scène sous le nom main_panel.tscn.

Mettre à jour le script du plugin

Nous devons mettre à jour le script main_screen_plugin.gd afin que le plugin instance notre scène d'écran principal et la place là où elle doit être. Voici le script complet du plugin :

@tool
extends EditorPlugin


const MainPanel = preload("res://addons/main_screen/main_panel.tscn")

var main_panel_instance


func _enter_tree():
    main_panel_instance = MainPanel.instantiate()
    # Add the main panel to the editor's main viewport.
    EditorInterface.get_editor_main_screen().add_child(main_panel_instance)
    # Hide the main panel. Very much required.
    _make_visible(false)


func _exit_tree():
    if main_panel_instance:
        main_panel_instance.queue_free()


func _has_main_screen():
    return true


func _make_visible(visible):
    if main_panel_instance:
        main_panel_instance.visible = visible


func _get_plugin_name():
    return "Main Screen Plugin"


func _get_plugin_icon():
    # Must return some kind of Texture for the icon.
    return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")

Quelques lignes spécifiques ont été ajoutées. Le MainPanel est une constante qui fait référence à la scène, et nous l'utilisons dans l'instance main_panel_instance.

The _enter_tree() function is called before _ready(). This is where we instance the main panel scene, and add them as children of specific parts of the editor. We use EditorInterface.get_editor_main_screen() to obtain the main editor screen and add our main panel instance as a child to it. We call the _make_visible(false) function to hide the main panel so it doesn't compete for space when first activating the plugin.

La fonction _exit_tree() est appelée lorsque le plugin est désactivé. Si l'écran principal existe toujours, nous appelons la fonction queue_free() pour libérer l'instance et la supprimer de la mémoire.

The _make_visible() function is overridden to hide or show the main panel as needed. This function is automatically called by the editor when the user clicks on the main viewport buttons at the top of the editor.

The _get_plugin_name() and _get_plugin_icon() functions control the displayed name and icon for the plugin's main viewport button.

Une autre fonction que vous pouvez ajouter est la fonction handles(), qui vous permet de gérer un type de nœud, en focalisant automatiquement l'écran principal lorsque le type est sélectionné. Cette fonction est similaire à celle qui permet de cliquer sur un nœud 3D pour passer automatiquement à la fenêtre de visualisation 3D.

Essayez le plugin

Activez le plugin dans les Paramètres du projet. Vous observerez un nouveau bouton à côté de 2D, 3D, Script au-dessus de la fenêtre principale. En cliquant dessus, vous accéderez à l'écran principal de votre nouveau plugin, et le bouton du milieu imprimera du texte.

Si vous souhaitez essayer une version finalisée de ce plugin, consultez les démos du plugin ici : https://github.com/godotengine/godot-demo-projects/tree/master/plugins

Si vous souhaitez voir un exemple plus complet de ce dont sont capables les plugins d'écran principal, consultez les projets de démonstration 2.5D ici : https://github.com/godotengine/godot-demo-projects/tree/master/misc/2.5d