theme_editor_plugin.h 16 KB

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