Debian Codegraph @

editor/scene/gui/theme_editor_plugin.h

raw symbols SCIP
1 /**************************************************************************/
2 /* theme_editor_plugin.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "editor/docks/editor_dock.h"
34 #include "editor/plugins/editor_plugin.h"
35 #include "editor/scene/gui/theme_editor_preview.h"
36 #include "scene/gui/dialogs.h"
37 #include "scene/gui/margin_container.h"
38 #include "scene/gui/tree.h"
39 #include "scene/resources/theme.h"
40
41 class Button;
42 class CheckButton;
43 class EditorFileDialog;
44 class ItemList;
45 class Label;
46 class LineEdit;
47 class OptionButton;
48 class PanelContainer;
49 class TabBar;
50 class TabContainer;
51 class ThemeEditorPlugin;
52 class TextureRect;
53
54 class ThemeItemImportTree : public VBoxContainer {
55 GDCLASS(ThemeItemImportTree, VBoxContainer);
56
57 Ref<Theme> edited_theme;
58 Ref<Theme> base_theme;
59
60 struct ThemeItem {
61 String type_name;
62 Theme::DataType data_type;
63 String item_name;
64
65 bool operator<(const ThemeItem &p_item) const {
66 if (type_name == p_item.type_name && data_type == p_item.data_type) {
67 return item_name < p_item.item_name;
68 }
69 if (type_name == p_item.type_name) {
70 return data_type < p_item.data_type;
71 }
72 return type_name < p_item.type_name;
73 }
74 };
75
76 enum ItemCheckedState {
77 SELECT_IMPORT_DEFINITION,
78 SELECT_IMPORT_FULL,
79 };
80
81 RBMap<ThemeItem, ItemCheckedState> selected_items;
82
83 LineEdit *import_items_filter = nullptr;
84
85 Tree *import_items_tree = nullptr;
86 List<TreeItem *> tree_color_items;
87 List<TreeItem *> tree_constant_items;
88 List<TreeItem *> tree_font_items;
89 List<TreeItem *> tree_font_size_items;
90 List<TreeItem *> tree_icon_items;
91 List<TreeItem *> tree_stylebox_items;
92
93 bool updating_tree = false;
94
95 enum ItemActionFlag {
96 IMPORT_ITEM = 1,
97 IMPORT_ITEM_DATA = 2,
98 };
99
100 TextureRect *select_colors_icon = nullptr;
101 Label *select_colors_label = nullptr;
102 Button *select_all_colors_button = nullptr;
103 Button *select_full_colors_button = nullptr;
104 Button *deselect_all_colors_button = nullptr;
105 Label *total_selected_colors_label = nullptr;
106
107 TextureRect *select_constants_icon = nullptr;
108 Label *select_constants_label = nullptr;
109 Button *select_all_constants_button = nullptr;
110 Button *select_full_constants_button = nullptr;
111 Button *deselect_all_constants_button = nullptr;
112 Label *total_selected_constants_label = nullptr;
113
114 TextureRect *select_fonts_icon = nullptr;
115 Label *select_fonts_label = nullptr;
116 Button *select_all_fonts_button = nullptr;
117 Button *select_full_fonts_button = nullptr;
118 Button *deselect_all_fonts_button = nullptr;
119 Label *total_selected_fonts_label = nullptr;
120
121 TextureRect *select_font_sizes_icon = nullptr;
122 Label *select_font_sizes_label = nullptr;
123 Button *select_all_font_sizes_button = nullptr;
124 Button *select_full_font_sizes_button = nullptr;
125 Button *deselect_all_font_sizes_button = nullptr;
126 Label *total_selected_font_sizes_label = nullptr;
127
128 TextureRect *select_icons_icon = nullptr;
129 Label *select_icons_label = nullptr;
130 Button *select_all_icons_button = nullptr;
131 Button *select_full_icons_button = nullptr;
132 Button *deselect_all_icons_button = nullptr;
133 Label *total_selected_icons_label = nullptr;
134
135 TextureRect *select_styleboxes_icon = nullptr;
136 Label *select_styleboxes_label = nullptr;
137 Button *select_all_styleboxes_button = nullptr;
138 Button *select_full_styleboxes_button = nullptr;
139 Button *deselect_all_styleboxes_button = nullptr;
140 Label *total_selected_styleboxes_label = nullptr;
141
142 HBoxContainer *select_icons_warning_hb = nullptr;
143 TextureRect *select_icons_warning_icon = nullptr;
144 Label *select_icons_warning = nullptr;
145
146 Button *import_collapse_types_button = nullptr;
147 Button *import_expand_types_button = nullptr;
148 Button *import_select_all_button = nullptr;
149 Button *import_select_full_button = nullptr;
150 Button *import_deselect_all_button = nullptr;
151
152 void _update_items_tree();
153 void _toggle_type_items(bool p_collapse);
154 void _filter_text_changed(const String &p_value);
155
156 void _store_selected_item(TreeItem *p_tree_item);
157 void _restore_selected_item(TreeItem *p_tree_item);
158 void _update_total_selected(Theme::DataType p_data_type);
159
160 void _tree_item_edited();
161 void _check_propagated_to_tree_item(Object *p_obj, int p_column);
162 void _select_all_subitems(TreeItem *p_root_item, bool p_select_with_data);
163 void _deselect_all_subitems(TreeItem *p_root_item, bool p_deselect_completely);
164
165 void _select_all_items_pressed();
166 void _select_full_items_pressed();
167 void _deselect_all_items_pressed();
168
169 void _select_all_data_type_pressed(int p_data_type);
170 void _select_full_data_type_pressed(int p_data_type);
171 void _deselect_all_data_type_pressed(int p_data_type);
172
173 void _import_selected();
174
175 protected:
176 void _notification(int p_what);
177 static void _bind_methods();
178
179 public:
180 void set_edited_theme(const Ref<Theme> &p_theme);
181 void set_base_theme(const Ref<Theme> &p_theme);
182 void reset_item_tree();
183
184 bool has_selected_items() const;
185
186 ThemeItemImportTree();
187 };
188
189 class ThemeTypeEditor;
190
191 class ThemeItemEditorDialog : public AcceptDialog {
192 GDCLASS(ThemeItemEditorDialog, AcceptDialog);
193
194 ThemeTypeEditor *theme_type_editor = nullptr;
195
196 Ref<Theme> edited_theme;
197
198 TabContainer *tc = nullptr;
199
200 enum TypesTreeAction {
201 TYPES_TREE_REMOVE_ITEM,
202 };
203
204 Tree *edit_type_list = nullptr;
205 LineEdit *edit_add_type_value = nullptr;
206 Button *edit_add_type_button = nullptr;
207 String edited_item_type;
208
209 Button *edit_items_add_color = nullptr;
210 Button *edit_items_add_constant = nullptr;
211 Button *edit_items_add_font = nullptr;
212 Button *edit_items_add_font_size = nullptr;
213 Button *edit_items_add_icon = nullptr;
214 Button *edit_items_add_stylebox = nullptr;
215 Button *edit_items_remove_class = nullptr;
216 Button *edit_items_remove_custom = nullptr;
217 Button *edit_items_remove_all = nullptr;
218 Tree *edit_items_tree = nullptr;
219 Label *edit_items_message = nullptr;
220
221 enum ItemsTreeAction {
222 ITEMS_TREE_RENAME_ITEM,
223 ITEMS_TREE_REMOVE_ITEM,
224 ITEMS_TREE_REMOVE_DATA_TYPE,
225 };
226
227 ConfirmationDialog *edit_theme_item_dialog = nullptr;
228 VBoxContainer *edit_theme_item_old_vb = nullptr;
229 Label *theme_item_old_name = nullptr;
230 LineEdit *theme_item_name = nullptr;
231
232 enum ItemPopupMode {
233 CREATE_THEME_ITEM,
234 RENAME_THEME_ITEM,
235 ITEM_POPUP_MODE_MAX
236 };
237
238 ItemPopupMode item_popup_mode = ITEM_POPUP_MODE_MAX;
239 String edit_item_old_name;
240 Theme::DataType edit_item_data_type = Theme::DATA_TYPE_MAX;
241
242 ThemeItemImportTree *import_default_theme_items = nullptr;
243 ThemeItemImportTree *import_editor_theme_items = nullptr;
244 ThemeItemImportTree *import_other_theme_items = nullptr;
245
246 LineEdit *import_another_theme_value = nullptr;
247 Button *import_another_theme_button = nullptr;
248 EditorFileDialog *import_another_theme_dialog = nullptr;
249
250 ConfirmationDialog *confirm_closing_dialog = nullptr;
251
252 void ok_pressed() override;
253 void _close_dialog();
254
255 void _dialog_about_to_show();
256 void _update_edit_types();
257 void _edited_type_selected();
258 void _edited_type_edited();
259 void _edited_type_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);
260
261 void _update_edit_item_tree(String p_item_type);
262 void _item_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);
263
264 void _add_theme_type();
265 void _add_theme_item(Theme::DataType p_data_type, const String &p_item_name, const String &p_item_type);
266 void _remove_theme_type(const String &p_theme_type);
267 void _remove_data_type_items(Theme::DataType p_data_type, String p_item_type);
268 void _remove_class_items();
269 void _remove_custom_items();
270 void _remove_all_items();
271
272 void _open_add_theme_item_dialog(int p_data_type);
273 void _open_rename_theme_item_dialog(Theme::DataType p_data_type, String p_item_name);
274 void _confirm_edit_theme_item();
275 void _edit_theme_item_gui_input(const Ref<InputEvent> &p_event);
276
277 void _open_select_another_theme();
278 void _select_another_theme_cbk(const String &p_path);
279
280 protected:
281 void _notification(int p_what);
282 static void _bind_methods();
283
284 public:
285 void set_edited_theme(const Ref<Theme> &p_theme);
286
287 ThemeItemEditorDialog(ThemeTypeEditor *p_theme_editor);
288 };
289
290 class ThemeTypeDialog : public ConfirmationDialog {
291 GDCLASS(ThemeTypeDialog, ConfirmationDialog);
292
293 Ref<Theme> edited_theme;
294 bool include_own_types = false;
295
296 String pre_submitted_value;
297
298 LineEdit *add_type_filter = nullptr;
299 ItemList *add_type_options = nullptr;
300 ConfirmationDialog *add_type_confirmation = nullptr;
301
302 void _dialog_about_to_show();
303 void ok_pressed() override;
304
305 void _update_add_type_options(const String &p_filter = "");
306
307 void _add_type_filter_cbk(const String &p_value);
308 void _type_filter_input(const Ref<InputEvent> &p_event);
309 void _add_type_options_cbk(int p_index);
310 void _add_type_dialog_entered(const String &p_value);
311 void _add_type_dialog_activated(int p_index);
312
313 void _add_type_selected(const String &p_type_name);
314 void _add_type_confirmed();
315
316 protected:
317 void _notification(int p_what);
318 static void _bind_methods();
319
320 public:
321 void set_edited_theme(const Ref<Theme> &p_theme);
322 void set_include_own_types(bool p_enable);
323
324 ThemeTypeDialog();
325 };
326
327 // Custom `Label` needed to use `EditorHelpBit` to display theme item documentation.
328 class ThemeItemLabel : public Label {
329 virtual Control *make_custom_tooltip(const String &p_text) const;
330 };
331
332 class ThemeTypeEditor : public MarginContainer {
333 GDCLASS(ThemeTypeEditor, MarginContainer);
334
335 Ref<Theme> edited_theme;
336 String edited_type;
337 bool updating = false;
338
339 struct LeadingStylebox {
340 bool pinned = false;
341 StringName item_name;
342 Ref<StyleBox> stylebox;
343 Ref<StyleBox> ref_stylebox;
344 };
345
346 LeadingStylebox leading_stylebox;
347
348 OptionButton *theme_type_list = nullptr;
349 Button *add_type_button = nullptr;
350 Button *rename_type_button = nullptr;
351 ConfirmationDialog *theme_type_rename_dialog = nullptr;
352 LineEdit *theme_type_rename_line_edit = nullptr;
353 Button *remove_type_button = nullptr;
354
355 CheckButton *show_default_items_button = nullptr;
356
357 TabContainer *data_type_tabs = nullptr;
358 VBoxContainer *color_items_list = nullptr;
359 VBoxContainer *constant_items_list = nullptr;
360 VBoxContainer *font_items_list = nullptr;
361 VBoxContainer *font_size_items_list = nullptr;
362 VBoxContainer *icon_items_list = nullptr;
363 VBoxContainer *stylebox_items_list = nullptr;
364
365 LineEdit *type_variation_edit = nullptr;
366 Button *type_variation_button = nullptr;
367 Label *type_variation_locked = nullptr;
368
369 enum TypeDialogMode {
370 ADD_THEME_TYPE,
371 ADD_VARIATION_BASE,
372 };
373
374 TypeDialogMode add_type_mode = ADD_THEME_TYPE;
375 ThemeTypeDialog *add_type_dialog = nullptr;
376
377 Vector<Control *> focusables;
378 Timer *update_debounce_timer = nullptr;
379
380 VBoxContainer *_create_item_list(Theme::DataType p_data_type);
381 void _update_type_list();
382 void _update_type_list_debounced();
383 HashMap<StringName, bool> _get_type_items(String p_type_name, Theme::DataType p_type, bool p_include_default);
384 HBoxContainer *_create_property_control(Theme::DataType p_data_type, String p_item_name, bool p_editable);
385 void _add_focusable(Control *p_control);
386 void _update_type_items();
387
388 void _list_type_selected(int p_index);
389 void _add_type_button_cbk();
390 void _rename_type_button_cbk();
391 void _theme_type_rename_dialog_confirmed();
392 void _remove_type_button_cbk();
393 void _add_default_type_items();
394
395 void _update_add_button(const String &p_text, LineEdit *p_for_edit);
396 void _item_add_cbk(int p_data_type, Control *p_control);
397 void _item_add_lineedit_cbk(String p_value, int p_data_type, Control *p_control);
398 void _item_override_cbk(int p_data_type, String p_item_name);
399 void _item_remove_cbk(int p_data_type, String p_item_name);
400 void _item_rename_cbk(int p_data_type, String p_item_name, Control *p_control);
401 void _item_rename_confirmed(int p_data_type, String p_item_name, Control *p_control);
402 void _item_rename_entered(String p_value, int p_data_type, String p_item_name, Control *p_control);
403 void _item_rename_canceled(int p_data_type, String p_item_name, Control *p_control);
404
405 void _color_item_changed(Color p_value, String p_item_name);
406 void _constant_item_changed(float p_value, String p_item_name);
407 void _font_size_item_changed(float p_value, String p_item_name);
408 void _edit_resource_item(Ref<Resource> p_resource, bool p_edit);
409 void _font_item_changed(Ref<Font> p_value, String p_item_name);
410 void _icon_item_changed(Ref<Texture2D> p_value, String p_item_name);
411 void _stylebox_item_changed(Ref<StyleBox> p_value, String p_item_name);
412 void _change_pinned_stylebox();
413 void _on_pin_leader_button_pressed(Control *p_editor, String p_item_name);
414 void _pin_leading_stylebox(String p_item_name, Ref<StyleBox> p_stylebox);
415 void _on_unpin_leader_button_pressed();
416 void _unpin_leading_stylebox();
417 void _update_stylebox_from_leading();
418
419 void _type_variation_changed(const String p_value);
420 void _add_type_variation_cbk();
421
422 void _add_type_dialog_selected(const String p_type_name);
423
424 protected:
425 void _notification(int p_what);
426 static void _bind_methods();
427
428 public:
429 void set_edited_theme(const Ref<Theme> &p_theme);
430 void select_type(String p_type_name);
431 bool is_stylebox_pinned(Ref<StyleBox> p_stylebox);
432
433 ThemeTypeEditor();
434 };
435
436 class ThemeEditor : public EditorDock {
437 GDCLASS(ThemeEditor, EditorDock);
438
439 friend class ThemeEditorPlugin;
440 ThemeEditorPlugin *plugin = nullptr;
441
442 Ref<Theme> theme;
443
444 Button *theme_edit_button = nullptr;
445 Button *theme_close_button = nullptr;
446
447 TabBar *preview_tabs = nullptr;
448 PanelContainer *preview_tabs_content = nullptr;
449 Control *add_preview_button_ph = nullptr;
450 Button *add_preview_button = nullptr;
451 EditorFileDialog *preview_scene_dialog = nullptr;
452
453 ThemeTypeEditor *theme_type_editor = nullptr;
454
455 Label *theme_name = nullptr;
456 ThemeItemEditorDialog *theme_edit_dialog = nullptr;
457
458 void _theme_save_button_cbk(bool p_save_as);
459 void _theme_edit_button_cbk();
460 void _theme_close_button_cbk();
461 void _dock_closed_cbk();
462 void _scene_closed(const String &p_path);
463 void _resource_saved(const Ref<Resource> &p_resource);
464 void _files_moved(const String &p_old_path, const String &p_new_path);
465 void _update_theme_name(const String &p_name);
466
467 void _add_preview_button_cbk();
468 void _preview_scene_dialog_cbk(const String &p_path);
469 void _add_preview_tab(ThemeEditorPreview *p_preview_tab, const String &p_preview_name, const Ref<Texture2D> &p_icon);
470 void _change_preview_tab(int p_tab);
471 void _remove_preview_tab(int p_tab);
472 void _remove_preview_tab_invalid(Node *p_tab_control);
473 void _update_preview_tab(Node *p_tab_control);
474 void _preview_control_picked(String p_class_name);
475 void _preview_tabs_resized();
476
477 protected:
478 void _notification(int p_what);
479
480 public:
481 void edit(const Ref<Theme> &p_theme);
482 Ref<Theme> get_edited_theme();
483
484 bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
485 void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
486
487 ThemeEditor();
488 };
489
490 class ThemeEditorPlugin : public EditorPlugin {
491 GDCLASS(ThemeEditorPlugin, EditorPlugin);
492
493 ThemeEditor *theme_editor = nullptr;
494
495 public:
496 virtual String get_plugin_name() const override { return "Theme"; }
497 bool has_main_screen() const override { return false; }
498 virtual void edit(Object *p_object) override;
499 virtual bool handles(Object *p_object) const override;
500 virtual void make_visible(bool p_visible) override;
501 virtual bool can_auto_hide() const override;
502
503 ThemeEditorPlugin();
504 };