Path: blob/master/editor/scene/gui/theme_editor_plugin.h
9912 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/plugins/editor_plugin.h"33#include "editor/scene/gui/theme_editor_preview.h"34#include "scene/gui/dialogs.h"35#include "scene/gui/margin_container.h"36#include "scene/gui/tree.h"37#include "scene/resources/theme.h"3839class Button;40class CheckButton;41class EditorFileDialog;42class ItemList;43class Label;44class LineEdit;45class OptionButton;46class PanelContainer;47class TabBar;48class TabContainer;49class ThemeEditorPlugin;50class TextureRect;5152class ThemeItemImportTree : public VBoxContainer {53GDCLASS(ThemeItemImportTree, VBoxContainer);5455Ref<Theme> edited_theme;56Ref<Theme> base_theme;5758struct ThemeItem {59String type_name;60Theme::DataType data_type;61String item_name;6263bool operator<(const ThemeItem &p_item) const {64if (type_name == p_item.type_name && data_type == p_item.data_type) {65return item_name < p_item.item_name;66}67if (type_name == p_item.type_name) {68return data_type < p_item.data_type;69}70return type_name < p_item.type_name;71}72};7374enum ItemCheckedState {75SELECT_IMPORT_DEFINITION,76SELECT_IMPORT_FULL,77};7879RBMap<ThemeItem, ItemCheckedState> selected_items;8081LineEdit *import_items_filter = nullptr;8283Tree *import_items_tree = nullptr;84List<TreeItem *> tree_color_items;85List<TreeItem *> tree_constant_items;86List<TreeItem *> tree_font_items;87List<TreeItem *> tree_font_size_items;88List<TreeItem *> tree_icon_items;89List<TreeItem *> tree_stylebox_items;9091bool updating_tree = false;9293enum ItemActionFlag {94IMPORT_ITEM = 1,95IMPORT_ITEM_DATA = 2,96};9798TextureRect *select_colors_icon = nullptr;99Label *select_colors_label = nullptr;100Button *select_all_colors_button = nullptr;101Button *select_full_colors_button = nullptr;102Button *deselect_all_colors_button = nullptr;103Label *total_selected_colors_label = nullptr;104105TextureRect *select_constants_icon = nullptr;106Label *select_constants_label = nullptr;107Button *select_all_constants_button = nullptr;108Button *select_full_constants_button = nullptr;109Button *deselect_all_constants_button = nullptr;110Label *total_selected_constants_label = nullptr;111112TextureRect *select_fonts_icon = nullptr;113Label *select_fonts_label = nullptr;114Button *select_all_fonts_button = nullptr;115Button *select_full_fonts_button = nullptr;116Button *deselect_all_fonts_button = nullptr;117Label *total_selected_fonts_label = nullptr;118119TextureRect *select_font_sizes_icon = nullptr;120Label *select_font_sizes_label = nullptr;121Button *select_all_font_sizes_button = nullptr;122Button *select_full_font_sizes_button = nullptr;123Button *deselect_all_font_sizes_button = nullptr;124Label *total_selected_font_sizes_label = nullptr;125126TextureRect *select_icons_icon = nullptr;127Label *select_icons_label = nullptr;128Button *select_all_icons_button = nullptr;129Button *select_full_icons_button = nullptr;130Button *deselect_all_icons_button = nullptr;131Label *total_selected_icons_label = nullptr;132133TextureRect *select_styleboxes_icon = nullptr;134Label *select_styleboxes_label = nullptr;135Button *select_all_styleboxes_button = nullptr;136Button *select_full_styleboxes_button = nullptr;137Button *deselect_all_styleboxes_button = nullptr;138Label *total_selected_styleboxes_label = nullptr;139140HBoxContainer *select_icons_warning_hb = nullptr;141TextureRect *select_icons_warning_icon = nullptr;142Label *select_icons_warning = nullptr;143144Button *import_collapse_types_button = nullptr;145Button *import_expand_types_button = nullptr;146Button *import_select_all_button = nullptr;147Button *import_select_full_button = nullptr;148Button *import_deselect_all_button = nullptr;149150void _update_items_tree();151void _toggle_type_items(bool p_collapse);152void _filter_text_changed(const String &p_value);153154void _store_selected_item(TreeItem *p_tree_item);155void _restore_selected_item(TreeItem *p_tree_item);156void _update_total_selected(Theme::DataType p_data_type);157158void _tree_item_edited();159void _check_propagated_to_tree_item(Object *p_obj, int p_column);160void _select_all_subitems(TreeItem *p_root_item, bool p_select_with_data);161void _deselect_all_subitems(TreeItem *p_root_item, bool p_deselect_completely);162163void _select_all_items_pressed();164void _select_full_items_pressed();165void _deselect_all_items_pressed();166167void _select_all_data_type_pressed(int p_data_type);168void _select_full_data_type_pressed(int p_data_type);169void _deselect_all_data_type_pressed(int p_data_type);170171void _import_selected();172173protected:174void _notification(int p_what);175static void _bind_methods();176177public:178void set_edited_theme(const Ref<Theme> &p_theme);179void set_base_theme(const Ref<Theme> &p_theme);180void reset_item_tree();181182bool has_selected_items() const;183184ThemeItemImportTree();185};186187class ThemeTypeEditor;188189class ThemeItemEditorDialog : public AcceptDialog {190GDCLASS(ThemeItemEditorDialog, AcceptDialog);191192ThemeTypeEditor *theme_type_editor = nullptr;193194Ref<Theme> edited_theme;195196TabContainer *tc = nullptr;197198enum TypesTreeAction {199TYPES_TREE_REMOVE_ITEM,200};201202Tree *edit_type_list = nullptr;203LineEdit *edit_add_type_value = nullptr;204Button *edit_add_type_button = nullptr;205String edited_item_type;206207Button *edit_items_add_color = nullptr;208Button *edit_items_add_constant = nullptr;209Button *edit_items_add_font = nullptr;210Button *edit_items_add_font_size = nullptr;211Button *edit_items_add_icon = nullptr;212Button *edit_items_add_stylebox = nullptr;213Button *edit_items_remove_class = nullptr;214Button *edit_items_remove_custom = nullptr;215Button *edit_items_remove_all = nullptr;216Tree *edit_items_tree = nullptr;217Label *edit_items_message = nullptr;218219enum ItemsTreeAction {220ITEMS_TREE_RENAME_ITEM,221ITEMS_TREE_REMOVE_ITEM,222ITEMS_TREE_REMOVE_DATA_TYPE,223};224225ConfirmationDialog *edit_theme_item_dialog = nullptr;226VBoxContainer *edit_theme_item_old_vb = nullptr;227Label *theme_item_old_name = nullptr;228LineEdit *theme_item_name = nullptr;229230enum ItemPopupMode {231CREATE_THEME_ITEM,232RENAME_THEME_ITEM,233ITEM_POPUP_MODE_MAX234};235236ItemPopupMode item_popup_mode = ITEM_POPUP_MODE_MAX;237String edit_item_old_name;238Theme::DataType edit_item_data_type = Theme::DATA_TYPE_MAX;239240ThemeItemImportTree *import_default_theme_items = nullptr;241ThemeItemImportTree *import_editor_theme_items = nullptr;242ThemeItemImportTree *import_other_theme_items = nullptr;243244LineEdit *import_another_theme_value = nullptr;245Button *import_another_theme_button = nullptr;246EditorFileDialog *import_another_theme_dialog = nullptr;247248ConfirmationDialog *confirm_closing_dialog = nullptr;249250void ok_pressed() override;251void _close_dialog();252253void _dialog_about_to_show();254void _update_edit_types();255void _edited_type_selected();256void _edited_type_edited();257void _edited_type_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);258259void _update_edit_item_tree(String p_item_type);260void _item_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);261262void _add_theme_type();263void _add_theme_item(Theme::DataType p_data_type, const String &p_item_name, const String &p_item_type);264void _remove_theme_type(const String &p_theme_type);265void _remove_data_type_items(Theme::DataType p_data_type, String p_item_type);266void _remove_class_items();267void _remove_custom_items();268void _remove_all_items();269270void _open_add_theme_item_dialog(int p_data_type);271void _open_rename_theme_item_dialog(Theme::DataType p_data_type, String p_item_name);272void _confirm_edit_theme_item();273void _edit_theme_item_gui_input(const Ref<InputEvent> &p_event);274275void _open_select_another_theme();276void _select_another_theme_cbk(const String &p_path);277278protected:279void _notification(int p_what);280static void _bind_methods();281282public:283void set_edited_theme(const Ref<Theme> &p_theme);284285ThemeItemEditorDialog(ThemeTypeEditor *p_theme_editor);286};287288class ThemeTypeDialog : public ConfirmationDialog {289GDCLASS(ThemeTypeDialog, ConfirmationDialog);290291Ref<Theme> edited_theme;292bool include_own_types = false;293294String pre_submitted_value;295296LineEdit *add_type_filter = nullptr;297ItemList *add_type_options = nullptr;298ConfirmationDialog *add_type_confirmation = nullptr;299300void _dialog_about_to_show();301void ok_pressed() override;302303void _update_add_type_options(const String &p_filter = "");304305void _add_type_filter_cbk(const String &p_value);306void _type_filter_input(const Ref<InputEvent> &p_event);307void _add_type_options_cbk(int p_index);308void _add_type_dialog_entered(const String &p_value);309void _add_type_dialog_activated(int p_index);310311void _add_type_selected(const String &p_type_name);312void _add_type_confirmed();313314protected:315void _notification(int p_what);316static void _bind_methods();317318public:319void set_edited_theme(const Ref<Theme> &p_theme);320void set_include_own_types(bool p_enable);321322ThemeTypeDialog();323};324325// Custom `Label` needed to use `EditorHelpBit` to display theme item documentation.326class ThemeItemLabel : public Label {327virtual Control *make_custom_tooltip(const String &p_text) const;328};329330class ThemeTypeEditor : public MarginContainer {331GDCLASS(ThemeTypeEditor, MarginContainer);332333Ref<Theme> edited_theme;334String edited_type;335bool updating = false;336337struct LeadingStylebox {338bool pinned = false;339StringName item_name;340Ref<StyleBox> stylebox;341Ref<StyleBox> ref_stylebox;342};343344LeadingStylebox leading_stylebox;345346OptionButton *theme_type_list = nullptr;347Button *add_type_button = nullptr;348Button *rename_type_button = nullptr;349ConfirmationDialog *theme_type_rename_dialog = nullptr;350LineEdit *theme_type_rename_line_edit = nullptr;351Button *remove_type_button = nullptr;352353CheckButton *show_default_items_button = nullptr;354355TabContainer *data_type_tabs = nullptr;356VBoxContainer *color_items_list = nullptr;357VBoxContainer *constant_items_list = nullptr;358VBoxContainer *font_items_list = nullptr;359VBoxContainer *font_size_items_list = nullptr;360VBoxContainer *icon_items_list = nullptr;361VBoxContainer *stylebox_items_list = nullptr;362363LineEdit *type_variation_edit = nullptr;364Button *type_variation_button = nullptr;365Label *type_variation_locked = nullptr;366367enum TypeDialogMode {368ADD_THEME_TYPE,369ADD_VARIATION_BASE,370};371372TypeDialogMode add_type_mode = ADD_THEME_TYPE;373ThemeTypeDialog *add_type_dialog = nullptr;374375Vector<Control *> focusables;376Timer *update_debounce_timer = nullptr;377378VBoxContainer *_create_item_list(Theme::DataType p_data_type);379void _update_type_list();380void _update_type_list_debounced();381HashMap<StringName, bool> _get_type_items(String p_type_name, Theme::DataType p_type, bool p_include_default);382HBoxContainer *_create_property_control(Theme::DataType p_data_type, String p_item_name, bool p_editable);383void _add_focusable(Control *p_control);384void _update_type_items();385386void _list_type_selected(int p_index);387void _add_type_button_cbk();388void _rename_type_button_cbk();389void _theme_type_rename_dialog_confirmed();390void _remove_type_button_cbk();391void _add_default_type_items();392393void _update_add_button(const String &p_text, LineEdit *p_for_edit);394void _item_add_cbk(int p_data_type, Control *p_control);395void _item_add_lineedit_cbk(String p_value, int p_data_type, Control *p_control);396void _item_override_cbk(int p_data_type, String p_item_name);397void _item_remove_cbk(int p_data_type, String p_item_name);398void _item_rename_cbk(int p_data_type, String p_item_name, Control *p_control);399void _item_rename_confirmed(int p_data_type, String p_item_name, Control *p_control);400void _item_rename_entered(String p_value, int p_data_type, String p_item_name, Control *p_control);401void _item_rename_canceled(int p_data_type, String p_item_name, Control *p_control);402403void _color_item_changed(Color p_value, String p_item_name);404void _constant_item_changed(float p_value, String p_item_name);405void _font_size_item_changed(float p_value, String p_item_name);406void _edit_resource_item(Ref<Resource> p_resource, bool p_edit);407void _font_item_changed(Ref<Font> p_value, String p_item_name);408void _icon_item_changed(Ref<Texture2D> p_value, String p_item_name);409void _stylebox_item_changed(Ref<StyleBox> p_value, String p_item_name);410void _change_pinned_stylebox();411void _on_pin_leader_button_pressed(Control *p_editor, String p_item_name);412void _pin_leading_stylebox(String p_item_name, Ref<StyleBox> p_stylebox);413void _on_unpin_leader_button_pressed();414void _unpin_leading_stylebox();415void _update_stylebox_from_leading();416417void _type_variation_changed(const String p_value);418void _add_type_variation_cbk();419420void _add_type_dialog_selected(const String p_type_name);421422protected:423void _notification(int p_what);424static void _bind_methods();425426public:427void set_edited_theme(const Ref<Theme> &p_theme);428void select_type(String p_type_name);429bool is_stylebox_pinned(Ref<StyleBox> p_stylebox);430431ThemeTypeEditor();432};433434class ThemeEditor : public VBoxContainer {435GDCLASS(ThemeEditor, VBoxContainer);436437friend class ThemeEditorPlugin;438ThemeEditorPlugin *plugin = nullptr;439440Ref<Theme> theme;441442TabBar *preview_tabs = nullptr;443PanelContainer *preview_tabs_content = nullptr;444Button *add_preview_button = nullptr;445EditorFileDialog *preview_scene_dialog = nullptr;446447ThemeTypeEditor *theme_type_editor = nullptr;448449Label *theme_name = nullptr;450ThemeItemEditorDialog *theme_edit_dialog = nullptr;451452void _theme_save_button_cbk(bool p_save_as);453void _theme_edit_button_cbk();454void _theme_close_button_cbk();455void _scene_closed(const String &p_path);456void _resource_saved(const Ref<Resource> &p_resource);457void _files_moved(const String &p_old_path, const String &p_new_path);458void _update_theme_name(const String &p_name);459460void _add_preview_button_cbk();461void _preview_scene_dialog_cbk(const String &p_path);462void _add_preview_tab(ThemeEditorPreview *p_preview_tab, const String &p_preview_name, const Ref<Texture2D> &p_icon);463void _change_preview_tab(int p_tab);464void _remove_preview_tab(int p_tab);465void _remove_preview_tab_invalid(Node *p_tab_control);466void _update_preview_tab(Node *p_tab_control);467void _preview_control_picked(String p_class_name);468469protected:470void _notification(int p_what);471472public:473void edit(const Ref<Theme> &p_theme);474Ref<Theme> get_edited_theme();475476bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);477void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);478479ThemeEditor();480};481482class ThemeEditorPlugin : public EditorPlugin {483GDCLASS(ThemeEditorPlugin, EditorPlugin);484485ThemeEditor *theme_editor = nullptr;486Button *button = nullptr;487488public:489virtual String get_plugin_name() const override { return "Theme"; }490bool has_main_screen() const override { return false; }491virtual void edit(Object *p_object) override;492virtual bool handles(Object *p_object) const override;493virtual void make_visible(bool p_visible) override;494virtual bool can_auto_hide() const override;495496ThemeEditorPlugin();497};498499500