theme_editor_plugin.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. #pragma once
  31. #include "editor/plugins/editor_plugin.h"
  32. #include "editor/plugins/theme_editor_preview.h"
  33. #include "scene/gui/dialogs.h"
  34. #include "scene/gui/margin_container.h"
  35. #include "scene/gui/tree.h"
  36. #include "scene/resources/theme.h"
  37. class Button;
  38. class CheckButton;
  39. class EditorFileDialog;
  40. class ItemList;
  41. class Label;
  42. class OptionButton;
  43. class PanelContainer;
  44. class TabBar;
  45. class TabContainer;
  46. class ThemeEditorPlugin;
  47. class TextureRect;
  48. class ThemeItemImportTree : public VBoxContainer {
  49. GDCLASS(ThemeItemImportTree, VBoxContainer);
  50. Ref<Theme> edited_theme;
  51. Ref<Theme> base_theme;
  52. struct ThemeItem {
  53. String type_name;
  54. Theme::DataType data_type;
  55. String item_name;
  56. bool operator<(const ThemeItem &p_item) const {
  57. if (type_name == p_item.type_name && data_type == p_item.data_type) {
  58. return item_name < p_item.item_name;
  59. }
  60. if (type_name == p_item.type_name) {
  61. return data_type < p_item.data_type;
  62. }
  63. return type_name < p_item.type_name;
  64. }
  65. };
  66. enum ItemCheckedState {
  67. SELECT_IMPORT_DEFINITION,
  68. SELECT_IMPORT_FULL,
  69. };
  70. RBMap<ThemeItem, ItemCheckedState> selected_items;
  71. LineEdit *import_items_filter = nullptr;
  72. Tree *import_items_tree = nullptr;
  73. List<TreeItem *> tree_color_items;
  74. List<TreeItem *> tree_constant_items;
  75. List<TreeItem *> tree_font_items;
  76. List<TreeItem *> tree_font_size_items;
  77. List<TreeItem *> tree_icon_items;
  78. List<TreeItem *> tree_stylebox_items;
  79. bool updating_tree = false;
  80. enum ItemActionFlag {
  81. IMPORT_ITEM = 1,
  82. IMPORT_ITEM_DATA = 2,
  83. };
  84. TextureRect *select_colors_icon = nullptr;
  85. Label *select_colors_label = nullptr;
  86. Button *select_all_colors_button = nullptr;
  87. Button *select_full_colors_button = nullptr;
  88. Button *deselect_all_colors_button = nullptr;
  89. Label *total_selected_colors_label = nullptr;
  90. TextureRect *select_constants_icon = nullptr;
  91. Label *select_constants_label = nullptr;
  92. Button *select_all_constants_button = nullptr;
  93. Button *select_full_constants_button = nullptr;
  94. Button *deselect_all_constants_button = nullptr;
  95. Label *total_selected_constants_label = nullptr;
  96. TextureRect *select_fonts_icon = nullptr;
  97. Label *select_fonts_label = nullptr;
  98. Button *select_all_fonts_button = nullptr;
  99. Button *select_full_fonts_button = nullptr;
  100. Button *deselect_all_fonts_button = nullptr;
  101. Label *total_selected_fonts_label = nullptr;
  102. TextureRect *select_font_sizes_icon = nullptr;
  103. Label *select_font_sizes_label = nullptr;
  104. Button *select_all_font_sizes_button = nullptr;
  105. Button *select_full_font_sizes_button = nullptr;
  106. Button *deselect_all_font_sizes_button = nullptr;
  107. Label *total_selected_font_sizes_label = nullptr;
  108. TextureRect *select_icons_icon = nullptr;
  109. Label *select_icons_label = nullptr;
  110. Button *select_all_icons_button = nullptr;
  111. Button *select_full_icons_button = nullptr;
  112. Button *deselect_all_icons_button = nullptr;
  113. Label *total_selected_icons_label = nullptr;
  114. TextureRect *select_styleboxes_icon = nullptr;
  115. Label *select_styleboxes_label = nullptr;
  116. Button *select_all_styleboxes_button = nullptr;
  117. Button *select_full_styleboxes_button = nullptr;
  118. Button *deselect_all_styleboxes_button = nullptr;
  119. Label *total_selected_styleboxes_label = nullptr;
  120. HBoxContainer *select_icons_warning_hb = nullptr;
  121. TextureRect *select_icons_warning_icon = nullptr;
  122. Label *select_icons_warning = nullptr;
  123. Button *import_collapse_types_button = nullptr;
  124. Button *import_expand_types_button = nullptr;
  125. Button *import_select_all_button = nullptr;
  126. Button *import_select_full_button = nullptr;
  127. Button *import_deselect_all_button = nullptr;
  128. void _update_items_tree();
  129. void _toggle_type_items(bool p_collapse);
  130. void _filter_text_changed(const String &p_value);
  131. void _store_selected_item(TreeItem *p_tree_item);
  132. void _restore_selected_item(TreeItem *p_tree_item);
  133. void _update_total_selected(Theme::DataType p_data_type);
  134. void _tree_item_edited();
  135. void _check_propagated_to_tree_item(Object *p_obj, int p_column);
  136. void _select_all_subitems(TreeItem *p_root_item, bool p_select_with_data);
  137. void _deselect_all_subitems(TreeItem *p_root_item, bool p_deselect_completely);
  138. void _select_all_items_pressed();
  139. void _select_full_items_pressed();
  140. void _deselect_all_items_pressed();
  141. void _select_all_data_type_pressed(int p_data_type);
  142. void _select_full_data_type_pressed(int p_data_type);
  143. void _deselect_all_data_type_pressed(int p_data_type);
  144. void _import_selected();
  145. protected:
  146. void _notification(int p_what);
  147. static void _bind_methods();
  148. public:
  149. void set_edited_theme(const Ref<Theme> &p_theme);
  150. void set_base_theme(const Ref<Theme> &p_theme);
  151. void reset_item_tree();
  152. bool has_selected_items() const;
  153. ThemeItemImportTree();
  154. };
  155. class ThemeTypeEditor;
  156. class ThemeItemEditorDialog : public AcceptDialog {
  157. GDCLASS(ThemeItemEditorDialog, AcceptDialog);
  158. ThemeTypeEditor *theme_type_editor = nullptr;
  159. Ref<Theme> edited_theme;
  160. TabContainer *tc = nullptr;
  161. enum TypesTreeAction {
  162. TYPES_TREE_REMOVE_ITEM,
  163. };
  164. Tree *edit_type_list = nullptr;
  165. LineEdit *edit_add_type_value = nullptr;
  166. Button *edit_add_type_button = nullptr;
  167. String edited_item_type;
  168. Button *edit_items_add_color = nullptr;
  169. Button *edit_items_add_constant = nullptr;
  170. Button *edit_items_add_font = nullptr;
  171. Button *edit_items_add_font_size = nullptr;
  172. Button *edit_items_add_icon = nullptr;
  173. Button *edit_items_add_stylebox = nullptr;
  174. Button *edit_items_remove_class = nullptr;
  175. Button *edit_items_remove_custom = nullptr;
  176. Button *edit_items_remove_all = nullptr;
  177. Tree *edit_items_tree = nullptr;
  178. Label *edit_items_message = nullptr;
  179. enum ItemsTreeAction {
  180. ITEMS_TREE_RENAME_ITEM,
  181. ITEMS_TREE_REMOVE_ITEM,
  182. ITEMS_TREE_REMOVE_DATA_TYPE,
  183. };
  184. ConfirmationDialog *edit_theme_item_dialog = nullptr;
  185. VBoxContainer *edit_theme_item_old_vb = nullptr;
  186. Label *theme_item_old_name = nullptr;
  187. LineEdit *theme_item_name = nullptr;
  188. enum ItemPopupMode {
  189. CREATE_THEME_ITEM,
  190. RENAME_THEME_ITEM,
  191. ITEM_POPUP_MODE_MAX
  192. };
  193. ItemPopupMode item_popup_mode = ITEM_POPUP_MODE_MAX;
  194. String edit_item_old_name;
  195. Theme::DataType edit_item_data_type = Theme::DATA_TYPE_MAX;
  196. ThemeItemImportTree *import_default_theme_items = nullptr;
  197. ThemeItemImportTree *import_editor_theme_items = nullptr;
  198. ThemeItemImportTree *import_other_theme_items = nullptr;
  199. LineEdit *import_another_theme_value = nullptr;
  200. Button *import_another_theme_button = nullptr;
  201. EditorFileDialog *import_another_theme_dialog = nullptr;
  202. ConfirmationDialog *confirm_closing_dialog = nullptr;
  203. void ok_pressed() override;
  204. void _close_dialog();
  205. void _dialog_about_to_show();
  206. void _update_edit_types();
  207. void _edited_type_selected();
  208. void _edited_type_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);
  209. void _update_edit_item_tree(String p_item_type);
  210. void _item_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button);
  211. void _add_theme_type(const String &p_new_text);
  212. void _add_theme_item(Theme::DataType p_data_type, String p_item_name, String p_item_type);
  213. void _remove_theme_type(const String &p_theme_type);
  214. void _remove_data_type_items(Theme::DataType p_data_type, String p_item_type);
  215. void _remove_class_items();
  216. void _remove_custom_items();
  217. void _remove_all_items();
  218. void _open_add_theme_item_dialog(int p_data_type);
  219. void _open_rename_theme_item_dialog(Theme::DataType p_data_type, String p_item_name);
  220. void _confirm_edit_theme_item();
  221. void _edit_theme_item_gui_input(const Ref<InputEvent> &p_event);
  222. void _open_select_another_theme();
  223. void _select_another_theme_cbk(const String &p_path);
  224. protected:
  225. void _notification(int p_what);
  226. static void _bind_methods();
  227. public:
  228. void set_edited_theme(const Ref<Theme> &p_theme);
  229. ThemeItemEditorDialog(ThemeTypeEditor *p_theme_editor);
  230. };
  231. class ThemeTypeDialog : public ConfirmationDialog {
  232. GDCLASS(ThemeTypeDialog, ConfirmationDialog);
  233. Ref<Theme> edited_theme;
  234. bool include_own_types = false;
  235. String pre_submitted_value;
  236. LineEdit *add_type_filter = nullptr;
  237. ItemList *add_type_options = nullptr;
  238. ConfirmationDialog *add_type_confirmation = nullptr;
  239. void _dialog_about_to_show();
  240. void ok_pressed() override;
  241. void _update_add_type_options(const String &p_filter = "");
  242. void _add_type_filter_cbk(const String &p_value);
  243. void _type_filter_input(const Ref<InputEvent> &p_event);
  244. void _add_type_options_cbk(int p_index);
  245. void _add_type_dialog_entered(const String &p_value);
  246. void _add_type_dialog_activated(int p_index);
  247. void _add_type_selected(const String &p_type_name);
  248. void _add_type_confirmed();
  249. protected:
  250. void _notification(int p_what);
  251. static void _bind_methods();
  252. public:
  253. void set_edited_theme(const Ref<Theme> &p_theme);
  254. void set_include_own_types(bool p_enable);
  255. ThemeTypeDialog();
  256. };
  257. // Custom `Label` needed to use `EditorHelpBit` to display theme item documentation.
  258. class ThemeItemLabel : public Label {
  259. virtual Control *make_custom_tooltip(const String &p_text) const;
  260. };
  261. class ThemeTypeEditor : public MarginContainer {
  262. GDCLASS(ThemeTypeEditor, MarginContainer);
  263. Ref<Theme> edited_theme;
  264. String edited_type;
  265. bool updating = false;
  266. struct LeadingStylebox {
  267. bool pinned = false;
  268. StringName item_name;
  269. Ref<StyleBox> stylebox;
  270. Ref<StyleBox> ref_stylebox;
  271. };
  272. LeadingStylebox leading_stylebox;
  273. OptionButton *theme_type_list = nullptr;
  274. Button *add_type_button = nullptr;
  275. CheckButton *show_default_items_button = nullptr;
  276. TabContainer *data_type_tabs = nullptr;
  277. VBoxContainer *color_items_list = nullptr;
  278. VBoxContainer *constant_items_list = nullptr;
  279. VBoxContainer *font_items_list = nullptr;
  280. VBoxContainer *font_size_items_list = nullptr;
  281. VBoxContainer *icon_items_list = nullptr;
  282. VBoxContainer *stylebox_items_list = nullptr;
  283. LineEdit *type_variation_edit = nullptr;
  284. Button *type_variation_button = nullptr;
  285. Label *type_variation_locked = nullptr;
  286. enum TypeDialogMode {
  287. ADD_THEME_TYPE,
  288. ADD_VARIATION_BASE,
  289. };
  290. TypeDialogMode add_type_mode = ADD_THEME_TYPE;
  291. ThemeTypeDialog *add_type_dialog = nullptr;
  292. Vector<Control *> focusables;
  293. Timer *update_debounce_timer = nullptr;
  294. VBoxContainer *_create_item_list(Theme::DataType p_data_type);
  295. void _update_type_list();
  296. void _update_type_list_debounced();
  297. HashMap<StringName, bool> _get_type_items(String p_type_name, Theme::DataType p_type, bool p_include_default);
  298. HBoxContainer *_create_property_control(Theme::DataType p_data_type, String p_item_name, bool p_editable);
  299. void _add_focusable(Control *p_control);
  300. void _update_type_items();
  301. void _list_type_selected(int p_index);
  302. void _add_type_button_cbk();
  303. void _add_default_type_items();
  304. void _update_add_button(const String &p_text, LineEdit *p_for_edit);
  305. void _item_add_cbk(int p_data_type, Control *p_control);
  306. void _item_add_lineedit_cbk(String p_value, int p_data_type, Control *p_control);
  307. void _item_override_cbk(int p_data_type, String p_item_name);
  308. void _item_remove_cbk(int p_data_type, String p_item_name);
  309. void _item_rename_cbk(int p_data_type, String p_item_name, Control *p_control);
  310. void _item_rename_confirmed(int p_data_type, String p_item_name, Control *p_control);
  311. void _item_rename_entered(String p_value, int p_data_type, String p_item_name, Control *p_control);
  312. void _item_rename_canceled(int p_data_type, String p_item_name, Control *p_control);
  313. void _color_item_changed(Color p_value, String p_item_name);
  314. void _constant_item_changed(float p_value, String p_item_name);
  315. void _font_size_item_changed(float p_value, String p_item_name);
  316. void _edit_resource_item(Ref<Resource> p_resource, bool p_edit);
  317. void _font_item_changed(Ref<Font> p_value, String p_item_name);
  318. void _icon_item_changed(Ref<Texture2D> p_value, String p_item_name);
  319. void _stylebox_item_changed(Ref<StyleBox> p_value, String p_item_name);
  320. void _change_pinned_stylebox();
  321. void _on_pin_leader_button_pressed(Control *p_editor, String p_item_name);
  322. void _pin_leading_stylebox(String p_item_name, Ref<StyleBox> p_stylebox);
  323. void _on_unpin_leader_button_pressed();
  324. void _unpin_leading_stylebox();
  325. void _update_stylebox_from_leading();
  326. void _type_variation_changed(const String p_value);
  327. void _add_type_variation_cbk();
  328. void _add_type_dialog_selected(const String p_type_name);
  329. protected:
  330. void _notification(int p_what);
  331. static void _bind_methods();
  332. public:
  333. void set_edited_theme(const Ref<Theme> &p_theme);
  334. void select_type(String p_type_name);
  335. bool is_stylebox_pinned(Ref<StyleBox> p_stylebox);
  336. ThemeTypeEditor();
  337. };
  338. class ThemeEditor : public VBoxContainer {
  339. GDCLASS(ThemeEditor, VBoxContainer);
  340. friend class ThemeEditorPlugin;
  341. ThemeEditorPlugin *plugin = nullptr;
  342. Ref<Theme> theme;
  343. TabBar *preview_tabs = nullptr;
  344. PanelContainer *preview_tabs_content = nullptr;
  345. Button *add_preview_button = nullptr;
  346. EditorFileDialog *preview_scene_dialog = nullptr;
  347. ThemeTypeEditor *theme_type_editor = nullptr;
  348. Label *theme_name = nullptr;
  349. ThemeItemEditorDialog *theme_edit_dialog = nullptr;
  350. void _theme_save_button_cbk(bool p_save_as);
  351. void _theme_edit_button_cbk();
  352. void _theme_close_button_cbk();
  353. void _scene_closed(const String &p_path);
  354. void _add_preview_button_cbk();
  355. void _preview_scene_dialog_cbk(const String &p_path);
  356. void _add_preview_tab(ThemeEditorPreview *p_preview_tab, const String &p_preview_name, const Ref<Texture2D> &p_icon);
  357. void _change_preview_tab(int p_tab);
  358. void _remove_preview_tab(int p_tab);
  359. void _remove_preview_tab_invalid(Node *p_tab_control);
  360. void _update_preview_tab(Node *p_tab_control);
  361. void _preview_control_picked(String p_class_name);
  362. protected:
  363. void _notification(int p_what);
  364. public:
  365. void edit(const Ref<Theme> &p_theme);
  366. Ref<Theme> get_edited_theme();
  367. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  368. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  369. ThemeEditor();
  370. };
  371. class ThemeEditorPlugin : public EditorPlugin {
  372. GDCLASS(ThemeEditorPlugin, EditorPlugin);
  373. ThemeEditor *theme_editor = nullptr;
  374. Button *button = nullptr;
  375. public:
  376. virtual String get_plugin_name() const override { return "Theme"; }
  377. bool has_main_screen() const override { return false; }
  378. virtual void edit(Object *p_object) override;
  379. virtual bool handles(Object *p_object) const override;
  380. virtual void make_visible(bool p_visible) override;
  381. virtual bool can_auto_hide() const override;
  382. ThemeEditorPlugin();
  383. };