Path: blob/master/editor/scene/gui/theme_editor_plugin.h
21327 views
/**************************************************************************/1/* theme_editor_plugin.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "editor/docks/editor_dock.h"33#include "editor/plugins/editor_plugin.h"34#include "editor/scene/gui/theme_editor_preview.h"35#include "scene/gui/dialogs.h"36#include "scene/gui/margin_container.h"37#include "scene/gui/tree.h"38#include "scene/resources/theme.h"3940class Button;41class CheckButton;42class EditorFileDialog;43class FilterLineEdit;44class HSplitContainer;45class ItemList;46class Label;47class LineEdit;48class OptionButton;49class PanelContainer;50class TabBar;51class TabContainer;52class ThemeEditorPlugin;53class TextureRect;5455class ThemeItemImportTree : public VBoxContainer {56GDCLASS(ThemeItemImportTree, VBoxContainer);5758Ref<Theme> edited_theme;59Ref<Theme> base_theme;6061struct ThemeItem {62String type_name;63Theme::DataType data_type;64String item_name;6566bool operator<(const ThemeItem &p_item) const {67if (type_name == p_item.type_name && data_type == p_item.data_type) {68return item_name < p_item.item_name;69}70if (type_name == p_item.type_name) {71return data_type < p_item.data_type;72}73return type_name < p_item.type_name;74}75};7677enum ItemCheckedState {78SELECT_IMPORT_DEFINITION,79SELECT_IMPORT_FULL,80};8182RBMap<ThemeItem, ItemCheckedState> selected_items;8384LineEdit *import_items_filter = nullptr;8586Tree *import_items_tree = nullptr;87List<TreeItem *> tree_color_items;88List<TreeItem *> tree_constant_items;89List<TreeItem *> tree_font_items;90List<TreeItem *> tree_font_size_items;91List<TreeItem *> tree_icon_items;92List<TreeItem *> tree_stylebox_items;9394bool updating_tree = false;9596enum ItemActionFlag {97IMPORT_ITEM = 1,98IMPORT_ITEM_DATA = 2,99};100101TextureRect *select_colors_icon = nullptr;102Label *select_colors_label = nullptr;103Button *select_all_colors_button = nullptr;104Button *select_full_colors_button = nullptr;105Button *deselect_all_colors_button = nullptr;106Label *total_selected_colors_label = nullptr;107108TextureRect *select_constants_icon = nullptr;109Label *select_constants_label = nullptr;110Button *select_all_constants_button = nullptr;111Button *select_full_constants_button = nullptr;112Button *deselect_all_constants_button = nullptr;113Label *total_selected_constants_label = nullptr;114115TextureRect *select_fonts_icon = nullptr;116Label *select_fonts_label = nullptr;117Button *select_all_fonts_button = nullptr;118Button *select_full_fonts_button = nullptr;119Button *deselect_all_fonts_button = nullptr;120Label *total_selected_fonts_label = nullptr;121122TextureRect *select_font_sizes_icon = nullptr;123Label *select_font_sizes_label = nullptr;124Button *select_all_font_sizes_button = nullptr;125Button *select_full_font_sizes_button = nullptr;126Button *deselect_all_font_sizes_button = nullptr;127Label *total_selected_font_sizes_label = nullptr;128129TextureRect *select_icons_icon = nullptr;130Label *select_icons_label = nullptr;131Button *select_all_icons_button = nullptr;132Button *select_full_icons_button = nullptr;133Button *deselect_all_icons_button = nullptr;134Label *total_selected_icons_label = nullptr;135136TextureRect *select_styleboxes_icon = nullptr;137Label *select_styleboxes_label = nullptr;138Button *select_all_styleboxes_button = nullptr;139Button *select_full_styleboxes_button = nullptr;140Button *deselect_all_styleboxes_button = nullptr;141Label *total_selected_styleboxes_label = nullptr;142143HBoxContainer *select_icons_warning_hb = nullptr;144TextureRect *select_icons_warning_icon = nullptr;145Label *select_icons_warning = nullptr;146147Button *import_collapse_types_button = nullptr;148Button *import_expand_types_button = nullptr;149Button *import_select_all_button = nullptr;150Button *import_select_full_button = nullptr;151Button *import_deselect_all_button = nullptr;152153void _update_items_tree();154void _toggle_type_items(bool p_collapse);155void _filter_text_changed(const String &p_value);156157void _store_selected_item(TreeItem *p_tree_item);158void _restore_selected_item(TreeItem *p_tree_item);159void _update_total_selected(Theme::DataType p_data_type);160161void _tree_item_edited();162void _check_propagated_to_tree_item(Object *p_obj, int p_column);163void _select_all_subitems(TreeItem *p_root_item, bool p_select_with_data);164void _deselect_all_subitems(TreeItem *p_root_item, bool p_deselect_completely);165166void _select_all_items_pressed();167void _select_full_items_pressed();168void _deselect_all_items_pressed();169170void _select_all_data_type_pressed(int p_data_type);171void _select_full_data_type_pressed(int p_data_type);172void _deselect_all_data_type_pressed(int p_data_type);173174void _import_selected();175176protected:177void _notification(int p_what);178static void _bind_methods();179180public:181void set_edited_theme(const Ref<Theme> &p_theme);182void set_base_theme(const Ref<Theme> &p_theme);183void reset_item_tree();184185bool has_selected_items() const;186187ThemeItemImportTree();188};189190class ThemeTypeEditor;191192class ThemeItemEditorDialog : public AcceptDialog {193GDCLASS(ThemeItemEditorDialog, AcceptDialog);194195ThemeTypeEditor *theme_type_editor = nullptr;196197Ref<Theme> edited_theme;198199TabContainer *tc = nullptr;200201enum TypesTreeAction {202TYPES_TREE_REMOVE_ITEM,203};204205Tree *edit_type_list = nullptr;206LineEdit *edit_add_type_value = nullptr;207Button *edit_add_type_button = nullptr;208String edited_item_type;209210Button *edit_items_add_color = nullptr;211Button *edit_items_add_constant = nullptr;212Button *edit_items_add_font = nullptr;213Button *edit_items_add_font_size = nullptr;214Button *edit_items_add_icon = nullptr;215Button *edit_items_add_stylebox = nullptr;216Button *edit_items_remove_class = nullptr;217Button *edit_items_remove_custom = nullptr;218Button *edit_items_remove_all = nullptr;219Tree *edit_items_tree = nullptr;220Label *edit_items_message = nullptr;221222enum ItemsTreeAction {223ITEMS_TREE_RENAME_ITEM,224ITEMS_TREE_REMOVE_ITEM,225ITEMS_TREE_REMOVE_DATA_TYPE,226};227228ConfirmationDialog *edit_theme_item_dialog = nullptr;229VBoxContainer *edit_theme_item_old_vb = nullptr;230Label *theme_item_old_name = nullptr;231LineEdit *theme_item_name = nullptr;232233enum ItemPopupMode {234CREATE_THEME_ITEM,235RENAME_THEME_ITEM,236ITEM_POPUP_MODE_MAX237};238239ItemPopupMode item_popup_mode = ITEM_POPUP_MODE_MAX;240String edit_item_old_name;241Theme::DataType edit_item_data_type = Theme::DATA_TYPE_MAX;242243ThemeItemImportTree *import_default_theme_items = nullptr;244ThemeItemImportTree *import_editor_theme_items = nullptr;245ThemeItemImportTree *import_other_theme_items = nullptr;246247LineEdit *import_another_theme_value = nullptr;248Button *import_another_theme_button = nullptr;249EditorFileDialog *import_another_theme_dialog = nullptr;250251ConfirmationDialog *confirm_closing_dialog = nullptr;252253void ok_pressed() override;254void _close_dialog();255256void _dialog_about_to_show();257void _update_edit_types();258void _edited_type_selected();259void _edited_type_edited();260void _edited_type_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);261262void _update_edit_item_tree(String p_item_type);263void _item_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);264265void _add_theme_type();266void _add_theme_item(Theme::DataType p_data_type, const String &p_item_name, const String &p_item_type);267void _remove_theme_type(const String &p_theme_type);268void _remove_data_type_items(Theme::DataType p_data_type, String p_item_type);269void _remove_class_items();270void _remove_custom_items();271void _remove_all_items();272273void _open_add_theme_item_dialog(int p_data_type);274void _open_rename_theme_item_dialog(Theme::DataType p_data_type, String p_item_name);275void _confirm_edit_theme_item();276void _edit_theme_item_gui_input(const Ref<InputEvent> &p_event);277278void _open_select_another_theme();279void _select_another_theme_cbk(const String &p_path);280281protected:282void _notification(int p_what);283static void _bind_methods();284285public:286void set_edited_theme(const Ref<Theme> &p_theme);287288ThemeItemEditorDialog(ThemeTypeEditor *p_theme_editor);289};290291class ThemeTypeDialog : public ConfirmationDialog {292GDCLASS(ThemeTypeDialog, ConfirmationDialog);293294Ref<Theme> edited_theme;295bool include_own_types = false;296297String pre_submitted_value;298299FilterLineEdit *add_type_filter = nullptr;300ItemList *add_type_options = nullptr;301ConfirmationDialog *add_type_confirmation = nullptr;302303void _dialog_about_to_show();304void ok_pressed() override;305306void _update_add_type_options(const String &p_filter = "");307308void _add_type_filter_cbk(const String &p_value);309void _add_type_options_cbk(int p_index);310void _add_type_dialog_entered(const String &p_value);311void _add_type_dialog_activated(int p_index);312313void _add_type_selected(const String &p_type_name);314void _add_type_confirmed();315316protected:317void _notification(int p_what);318static void _bind_methods();319320public:321void set_edited_theme(const Ref<Theme> &p_theme);322void set_include_own_types(bool p_enable);323324ThemeTypeDialog();325};326327// Custom `Label` needed to use `EditorHelpBit` to display theme item documentation.328class ThemeItemLabel : public Label {329virtual Control *make_custom_tooltip(const String &p_text) const;330};331332class ThemeTypeEditor : public MarginContainer {333GDCLASS(ThemeTypeEditor, MarginContainer);334335Ref<Theme> edited_theme;336String edited_type;337bool updating = false;338339struct LeadingStylebox {340bool pinned = false;341StringName item_name;342Ref<StyleBox> stylebox;343Ref<StyleBox> ref_stylebox;344};345346LeadingStylebox leading_stylebox;347348OptionButton *theme_type_list = nullptr;349Button *add_type_button = nullptr;350Button *rename_type_button = nullptr;351ConfirmationDialog *theme_type_rename_dialog = nullptr;352LineEdit *theme_type_rename_line_edit = nullptr;353Button *remove_type_button = nullptr;354355CheckButton *show_default_items_button = nullptr;356357TabContainer *data_type_tabs = nullptr;358VBoxContainer *color_items_list = nullptr;359VBoxContainer *constant_items_list = nullptr;360VBoxContainer *font_items_list = nullptr;361VBoxContainer *font_size_items_list = nullptr;362VBoxContainer *icon_items_list = nullptr;363VBoxContainer *stylebox_items_list = nullptr;364365LineEdit *type_variation_edit = nullptr;366Button *type_variation_button = nullptr;367Label *type_variation_locked = nullptr;368369enum TypeDialogMode {370ADD_THEME_TYPE,371ADD_VARIATION_BASE,372};373374TypeDialogMode add_type_mode = ADD_THEME_TYPE;375ThemeTypeDialog *add_type_dialog = nullptr;376377Vector<Control *> focusables;378Timer *update_debounce_timer = nullptr;379380VBoxContainer *_create_item_list(Theme::DataType p_data_type);381void _update_type_list();382void _update_type_list_debounced();383HashMap<StringName, bool> _get_type_items(String p_type_name, Theme::DataType p_type, bool p_include_default);384HBoxContainer *_create_property_control(Theme::DataType p_data_type, String p_item_name, bool p_editable);385void _add_focusable(Control *p_control);386void _update_type_items();387388void _list_type_selected(int p_index);389void _add_type_button_cbk();390void _rename_type_button_cbk();391void _theme_type_rename_dialog_confirmed();392void _remove_type_button_cbk();393void _add_default_type_items();394395void _update_add_button(const String &p_text, LineEdit *p_for_edit);396void _item_add_cbk(int p_data_type, Control *p_control);397void _item_add_lineedit_cbk(String p_value, int p_data_type, Control *p_control);398void _item_override_cbk(int p_data_type, String p_item_name);399void _item_remove_cbk(int p_data_type, String p_item_name);400void _item_rename_cbk(int p_data_type, String p_item_name, Control *p_control);401void _item_rename_confirmed(int p_data_type, String p_item_name, Control *p_control);402void _item_rename_entered(String p_value, int p_data_type, String p_item_name, Control *p_control);403void _item_rename_canceled(int p_data_type, String p_item_name, Control *p_control);404405void _color_item_changed(Color p_value, String p_item_name);406void _constant_item_changed(float p_value, String p_item_name);407void _font_size_item_changed(float p_value, String p_item_name);408void _edit_resource_item(Ref<Resource> p_resource, bool p_edit);409void _font_item_changed(Ref<Font> p_value, String p_item_name);410void _icon_item_changed(Ref<Texture2D> p_value, String p_item_name);411void _stylebox_item_changed(Ref<StyleBox> p_value, String p_item_name);412void _change_pinned_stylebox();413void _on_pin_leader_button_pressed(Control *p_editor, String p_item_name);414void _pin_leading_stylebox(String p_item_name, Ref<StyleBox> p_stylebox);415void _on_unpin_leader_button_pressed();416void _unpin_leading_stylebox();417void _update_stylebox_from_leading();418419void _type_variation_changed(const String p_value);420void _add_type_variation_cbk();421422void _add_type_dialog_selected(const String p_type_name);423424protected:425void _notification(int p_what);426static void _bind_methods();427428public:429void set_edited_theme(const Ref<Theme> &p_theme);430void select_type(String p_type_name);431bool is_stylebox_pinned(Ref<StyleBox> p_stylebox);432433ThemeTypeEditor();434};435436class ThemeEditor : public EditorDock {437GDCLASS(ThemeEditor, EditorDock);438439friend class ThemeEditorPlugin;440ThemeEditorPlugin *plugin = nullptr;441442Ref<Theme> theme;443444Button *theme_edit_button = nullptr;445Button *theme_close_button = nullptr;446447TabBar *preview_tabs = nullptr;448PanelContainer *preview_tabs_content = nullptr;449Control *add_preview_button_ph = nullptr;450Button *add_preview_button = nullptr;451EditorFileDialog *preview_scene_dialog = nullptr;452453ThemeTypeEditor *theme_type_editor = nullptr;454455Label *theme_name = nullptr;456ThemeItemEditorDialog *theme_edit_dialog = nullptr;457HSplitContainer *main_hs = nullptr;458459void _theme_save_button_cbk(bool p_save_as);460void _theme_edit_button_cbk();461void _theme_close_button_cbk();462void _dock_closed_cbk();463void _scene_closed(const String &p_path);464void _resource_saved(const Ref<Resource> &p_resource);465void _files_moved(const String &p_old_path, const String &p_new_path);466void _update_theme_name(const String &p_name);467468void _add_preview_button_cbk();469void _preview_scene_dialog_cbk(const String &p_path);470void _add_preview_tab(ThemeEditorPreview *p_preview_tab, const String &p_preview_name, const Ref<Texture2D> &p_icon);471void _change_preview_tab(int p_tab);472void _remove_preview_tab(int p_tab);473void _remove_preview_tab_invalid(Node *p_tab_control);474void _update_preview_tab(Node *p_tab_control);475void _preview_control_picked(String p_class_name);476void _preview_tabs_resized();477478protected:479void _notification(int p_what);480481virtual void save_layout_to_config(Ref<ConfigFile> &p_layout, const String &p_section) const override;482virtual void load_layout_from_config(const Ref<ConfigFile> &p_layout, const String &p_section) override;483484public:485void edit(const Ref<Theme> &p_theme);486Ref<Theme> get_edited_theme();487488bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);489void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);490491ThemeEditor();492};493494class ThemeEditorPlugin : public EditorPlugin {495GDCLASS(ThemeEditorPlugin, EditorPlugin);496497ThemeEditor *theme_editor = nullptr;498499public:500virtual String get_plugin_name() const override { return "Theme"; }501bool has_main_screen() const override { return false; }502virtual void edit(Object *p_object) override;503virtual bool handles(Object *p_object) const override;504virtual void make_visible(bool p_visible) override;505virtual bool can_auto_hide() const override;506507ThemeEditorPlugin();508};509510511