| 1 | /**************************************************************************/ |
| 2 | /* theme_editor_plugin.cpp */ |
| 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 | #include "theme_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/doc/editor_help.h" |
| 34 | #include "editor/docks/editor_dock_manager.h" |
| 35 | #include "editor/docks/filesystem_dock.h" |
| 36 | #include "editor/docks/inspector_dock.h" |
| 37 | #include "editor/editor_node.h" |
| 38 | #include "editor/editor_string_names.h" |
| 39 | #include "editor/editor_undo_redo_manager.h" |
| 40 | #include "editor/file_system/editor_file_system.h" |
| 41 | #include "editor/gui/editor_bottom_panel.h" |
| 42 | #include "editor/gui/editor_file_dialog.h" |
| 43 | #include "editor/gui/editor_spin_slider.h" |
| 44 | #include "editor/gui/progress_dialog.h" |
| 45 | #include "editor/inspector/editor_resource_picker.h" |
| 46 | #include "editor/settings/editor_command_palette.h" |
| 47 | #include "editor/settings/editor_settings.h" |
| 48 | #include "editor/themes/editor_scale.h" |
| 49 | #include "scene/gui/check_button.h" |
| 50 | #include "scene/gui/color_picker.h" |
| 51 | #include "scene/gui/item_list.h" |
| 52 | #include "scene/gui/option_button.h" |
| 53 | #include "scene/gui/panel_container.h" |
| 54 | #include "scene/gui/scroll_container.h" |
| 55 | #include "scene/gui/separator.h" |
| 56 | #include "scene/gui/split_container.h" |
| 57 | #include "scene/gui/tab_bar.h" |
| 58 | #include "scene/gui/tab_container.h" |
| 59 | #include "scene/gui/texture_rect.h" |
| 60 | #include "scene/theme/theme_db.h" |
| 61 | |
| 62 | static void _rename_theme_type(EditorUndoRedoManager *p_ur, Theme *p_theme, const String &p_old_theme_type, const String &p_new_theme_type) { |
| 63 | p_ur->add_do_method(p_theme, "rename_type", p_old_theme_type, p_new_theme_type); |
| 64 | p_ur->add_undo_method(p_theme, "rename_type", p_new_theme_type, p_old_theme_type); |
| 65 | |
| 66 | // Renaming a theme type to an empty name or a variation to a type associated with a built-in class |
| 67 | // removes type variation connections in a way that cannot be undone by reversing the rename alone. |
| 68 | const StringName old_base_type = p_theme->get_type_variation_base(p_old_theme_type); |
| 69 | if ((!p_old_theme_type.is_empty() && p_new_theme_type.is_empty()) || (old_base_type != StringName() && ClassDB::class_exists(p_new_theme_type))) { |
| 70 | if (old_base_type != StringName()) { |
| 71 | p_ur->add_undo_method(p_theme, "set_type_variation", p_old_theme_type, old_base_type); |
| 72 | } |
| 73 | |
| 74 | List<StringName> names; |
| 75 | p_theme->get_type_variation_list(p_old_theme_type, &names); |
| 76 | for (const StringName &E : names) { |
| 77 | p_ur->add_undo_method(p_theme, "set_type_variation", E, p_old_theme_type); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /////////////////////// |
| 83 | |
| 84 | void ThemeItemImportTree::_update_items_tree() { |
| 85 | import_items_tree->clear(); |
| 86 | TreeItem *root = import_items_tree->create_item(); |
| 87 | |
| 88 | if (base_theme.is_null()) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | String filter_text = import_items_filter->get_text(); |
| 93 | |
| 94 | List<StringName> types; |
| 95 | List<StringName> names; |
| 96 | List<StringName> filtered_names; |
| 97 | base_theme->get_type_list(&types); |
| 98 | types.sort_custom<StringName::AlphCompare>(); |
| 99 | |
| 100 | int color_amount = 0; |
| 101 | int constant_amount = 0; |
| 102 | int font_amount = 0; |
| 103 | int font_size_amount = 0; |
| 104 | int icon_amount = 0; |
| 105 | int stylebox_amount = 0; |
| 106 | |
| 107 | tree_color_items.clear(); |
| 108 | tree_constant_items.clear(); |
| 109 | tree_font_items.clear(); |
| 110 | tree_font_size_items.clear(); |
| 111 | tree_icon_items.clear(); |
| 112 | tree_stylebox_items.clear(); |
| 113 | |
| 114 | for (const StringName &E : types) { |
| 115 | String type_name = (String)E; |
| 116 | |
| 117 | Ref<Texture2D> type_icon; |
| 118 | if (E == "") { |
| 119 | type_icon = get_editor_theme_icon(SNAME("NodeDisabled")); |
| 120 | } else { |
| 121 | type_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled"); |
| 122 | } |
| 123 | |
| 124 | TreeItem *type_node = import_items_tree->create_item(root); |
| 125 | type_node->set_meta("_can_be_imported", false); |
| 126 | type_node->set_collapsed(true); |
| 127 | type_node->set_icon(0, type_icon); |
| 128 | type_node->set_text(0, type_name); |
| 129 | type_node->set_cell_mode(IMPORT_ITEM, TreeItem::CELL_MODE_CHECK); |
| 130 | type_node->set_checked(IMPORT_ITEM, false); |
| 131 | type_node->set_editable(IMPORT_ITEM, true); |
| 132 | type_node->set_cell_mode(IMPORT_ITEM_DATA, TreeItem::CELL_MODE_CHECK); |
| 133 | type_node->set_checked(IMPORT_ITEM_DATA, false); |
| 134 | type_node->set_editable(IMPORT_ITEM_DATA, true); |
| 135 | |
| 136 | bool is_matching_filter = (filter_text.is_empty() || type_name.containsn(filter_text)); |
| 137 | bool has_filtered_items = false; |
| 138 | |
| 139 | for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) { |
| 140 | Theme::DataType dt = (Theme::DataType)i; |
| 141 | |
| 142 | names.clear(); |
| 143 | filtered_names.clear(); |
| 144 | base_theme->get_theme_item_list(dt, E, &names); |
| 145 | |
| 146 | bool data_type_has_filtered_items = false; |
| 147 | |
| 148 | for (const StringName &F : names) { |
| 149 | String item_name = (String)F; |
| 150 | bool is_item_matching_filter = item_name.containsn(filter_text); |
| 151 | if (!filter_text.is_empty() && !is_matching_filter && !is_item_matching_filter) { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | // Only mark this if actual items match the filter and not just the type group. |
| 156 | if (!filter_text.is_empty() && is_item_matching_filter) { |
| 157 | has_filtered_items = true; |
| 158 | data_type_has_filtered_items = true; |
| 159 | } |
| 160 | filtered_names.push_back(F); |
| 161 | } |
| 162 | |
| 163 | if (filtered_names.is_empty()) { |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | TreeItem *data_type_node = import_items_tree->create_item(type_node); |
| 168 | data_type_node->set_meta("_can_be_imported", false); |
| 169 | data_type_node->set_metadata(0, i); |
| 170 | data_type_node->set_collapsed(!data_type_has_filtered_items); |
| 171 | data_type_node->set_cell_mode(IMPORT_ITEM, TreeItem::CELL_MODE_CHECK); |
| 172 | data_type_node->set_checked(IMPORT_ITEM, false); |
| 173 | data_type_node->set_editable(IMPORT_ITEM, true); |
| 174 | data_type_node->set_cell_mode(IMPORT_ITEM_DATA, TreeItem::CELL_MODE_CHECK); |
| 175 | data_type_node->set_checked(IMPORT_ITEM_DATA, false); |
| 176 | data_type_node->set_editable(IMPORT_ITEM_DATA, true); |
| 177 | |
| 178 | List<TreeItem *> *item_list = nullptr; |
| 179 | |
| 180 | switch (dt) { |
| 181 | case Theme::DATA_TYPE_COLOR: |
| 182 | data_type_node->set_icon(0, get_editor_theme_icon(SNAME("Color"))); |
| 183 | data_type_node->set_text(0, TTR("Colors")); |
| 184 | |
| 185 | item_list = &tree_color_items; |
| 186 | color_amount += filtered_names.size(); |
| 187 | break; |
| 188 | |
| 189 | case Theme::DATA_TYPE_CONSTANT: |
| 190 | data_type_node->set_icon(0, get_editor_theme_icon(SNAME("MemberConstant"))); |
| 191 | data_type_node->set_text(0, TTR("Constants")); |
| 192 | |
| 193 | item_list = &tree_constant_items; |
| 194 | constant_amount += filtered_names.size(); |
| 195 | break; |
| 196 | |
| 197 | case Theme::DATA_TYPE_FONT: |
| 198 | data_type_node->set_icon(0, get_editor_theme_icon(SNAME("FontItem"))); |
| 199 | data_type_node->set_text(0, TTR("Fonts")); |
| 200 | |
| 201 | item_list = &tree_font_items; |
| 202 | font_amount += filtered_names.size(); |
| 203 | break; |
| 204 | |
| 205 | case Theme::DATA_TYPE_FONT_SIZE: |
| 206 | data_type_node->set_icon(0, get_editor_theme_icon(SNAME("FontSize"))); |
| 207 | data_type_node->set_text(0, TTR("Font Sizes")); |
| 208 | |
| 209 | item_list = &tree_font_size_items; |
| 210 | font_size_amount += filtered_names.size(); |
| 211 | break; |
| 212 | |
| 213 | case Theme::DATA_TYPE_ICON: |
| 214 | data_type_node->set_icon(0, get_editor_theme_icon(SNAME("ImageTexture"))); |
| 215 | data_type_node->set_text(0, TTR("Icons")); |
| 216 | |
| 217 | item_list = &tree_icon_items; |
| 218 | icon_amount += filtered_names.size(); |
| 219 | break; |
| 220 | |
| 221 | case Theme::DATA_TYPE_STYLEBOX: |
| 222 | data_type_node->set_icon(0, get_editor_theme_icon(SNAME("StyleBoxFlat"))); |
| 223 | data_type_node->set_text(0, TTR("Styleboxes")); |
| 224 | |
| 225 | item_list = &tree_stylebox_items; |
| 226 | stylebox_amount += filtered_names.size(); |
| 227 | break; |
| 228 | |
| 229 | case Theme::DATA_TYPE_MAX: |
| 230 | break; // Can't happen, but silences warning. |
| 231 | } |
| 232 | |
| 233 | filtered_names.sort_custom<StringName::AlphCompare>(); |
| 234 | for (const StringName &F : filtered_names) { |
| 235 | TreeItem *item_node = import_items_tree->create_item(data_type_node); |
| 236 | item_node->set_meta("_can_be_imported", true); |
| 237 | item_node->set_text(0, F); |
| 238 | item_node->set_cell_mode(IMPORT_ITEM, TreeItem::CELL_MODE_CHECK); |
| 239 | item_node->set_checked(IMPORT_ITEM, false); |
| 240 | item_node->set_editable(IMPORT_ITEM, true); |
| 241 | item_node->set_cell_mode(IMPORT_ITEM_DATA, TreeItem::CELL_MODE_CHECK); |
| 242 | item_node->set_checked(IMPORT_ITEM_DATA, false); |
| 243 | item_node->set_editable(IMPORT_ITEM_DATA, true); |
| 244 | |
| 245 | _restore_selected_item(item_node); |
| 246 | item_node->propagate_check(IMPORT_ITEM, false); |
| 247 | item_node->propagate_check(IMPORT_ITEM_DATA, false); |
| 248 | |
| 249 | item_list->push_back(item_node); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Remove the item if it doesn't match the filter in any way. |
| 254 | if (!is_matching_filter && !has_filtered_items) { |
| 255 | root->remove_child(type_node); |
| 256 | memdelete(type_node); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | // Show one level inside of a type group if there are matches in items. |
| 261 | if (!filter_text.is_empty() && has_filtered_items) { |
| 262 | type_node->set_collapsed(false); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (color_amount > 0) { |
| 267 | Array arr = { color_amount }; |
| 268 | select_colors_label->set_text(TTRN("1 color", "{num} colors", color_amount).format(arr, "{num}")); |
| 269 | select_all_colors_button->set_visible(true); |
| 270 | select_full_colors_button->set_visible(true); |
| 271 | deselect_all_colors_button->set_visible(true); |
| 272 | } else { |
| 273 | select_colors_label->set_text(TTR("No colors found.")); |
| 274 | select_all_colors_button->set_visible(false); |
| 275 | select_full_colors_button->set_visible(false); |
| 276 | deselect_all_colors_button->set_visible(false); |
| 277 | } |
| 278 | |
| 279 | if (constant_amount > 0) { |
| 280 | Array arr = { constant_amount }; |
| 281 | select_constants_label->set_text(TTRN("1 constant", "{num} constants", constant_amount).format(arr, "{num}")); |
| 282 | select_all_constants_button->set_visible(true); |
| 283 | select_full_constants_button->set_visible(true); |
| 284 | deselect_all_constants_button->set_visible(true); |
| 285 | } else { |
| 286 | select_constants_label->set_text(TTR("No constants found.")); |
| 287 | select_all_constants_button->set_visible(false); |
| 288 | select_full_constants_button->set_visible(false); |
| 289 | deselect_all_constants_button->set_visible(false); |
| 290 | } |
| 291 | |
| 292 | if (font_amount > 0) { |
| 293 | Array arr = { font_amount }; |
| 294 | select_fonts_label->set_text(TTRN("1 font", "{num} fonts", font_amount).format(arr, "{num}")); |
| 295 | select_all_fonts_button->set_visible(true); |
| 296 | select_full_fonts_button->set_visible(true); |
| 297 | deselect_all_fonts_button->set_visible(true); |
| 298 | } else { |
| 299 | select_fonts_label->set_text(TTR("No fonts found.")); |
| 300 | select_all_fonts_button->set_visible(false); |
| 301 | select_full_fonts_button->set_visible(false); |
| 302 | deselect_all_fonts_button->set_visible(false); |
| 303 | } |
| 304 | |
| 305 | if (font_size_amount > 0) { |
| 306 | Array arr = { font_size_amount }; |
| 307 | select_font_sizes_label->set_text(TTRN("1 font size", "{num} font sizes", font_size_amount).format(arr, "{num}")); |
| 308 | select_all_font_sizes_button->set_visible(true); |
| 309 | select_full_font_sizes_button->set_visible(true); |
| 310 | deselect_all_font_sizes_button->set_visible(true); |
| 311 | } else { |
| 312 | select_font_sizes_label->set_text(TTR("No font sizes found.")); |
| 313 | select_all_font_sizes_button->set_visible(false); |
| 314 | select_full_font_sizes_button->set_visible(false); |
| 315 | deselect_all_font_sizes_button->set_visible(false); |
| 316 | } |
| 317 | |
| 318 | if (icon_amount > 0) { |
| 319 | Array arr = { icon_amount }; |
| 320 | select_icons_label->set_text(TTRN("1 icon", "{num} icons", icon_amount).format(arr, "{num}")); |
| 321 | select_all_icons_button->set_visible(true); |
| 322 | select_full_icons_button->set_visible(true); |
| 323 | deselect_all_icons_button->set_visible(true); |
| 324 | select_icons_warning_hb->set_visible(true); |
| 325 | } else { |
| 326 | select_icons_label->set_text(TTR("No icons found.")); |
| 327 | select_all_icons_button->set_visible(false); |
| 328 | select_full_icons_button->set_visible(false); |
| 329 | deselect_all_icons_button->set_visible(false); |
| 330 | select_icons_warning_hb->set_visible(false); |
| 331 | } |
| 332 | |
| 333 | if (stylebox_amount > 0) { |
| 334 | Array arr = { stylebox_amount }; |
| 335 | select_styleboxes_label->set_text(TTRN("1 stylebox", "{num} styleboxes", stylebox_amount).format(arr, "{num}")); |
| 336 | select_all_styleboxes_button->set_visible(true); |
| 337 | select_full_styleboxes_button->set_visible(true); |
| 338 | deselect_all_styleboxes_button->set_visible(true); |
| 339 | } else { |
| 340 | select_styleboxes_label->set_text(TTR("No styleboxes found.")); |
| 341 | select_all_styleboxes_button->set_visible(false); |
| 342 | select_full_styleboxes_button->set_visible(false); |
| 343 | deselect_all_styleboxes_button->set_visible(false); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void ThemeItemImportTree::_toggle_type_items(bool p_collapse) { |
| 348 | TreeItem *root = import_items_tree->get_root(); |
| 349 | if (!root) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | TreeItem *type_node = root->get_first_child(); |
| 354 | while (type_node) { |
| 355 | type_node->set_collapsed(p_collapse); |
| 356 | type_node = type_node->get_next(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void ThemeItemImportTree::_filter_text_changed(const String &p_value) { |
| 361 | _update_items_tree(); |
| 362 | } |
| 363 | |
| 364 | void ThemeItemImportTree::_store_selected_item(TreeItem *p_tree_item) { |
| 365 | if (!p_tree_item->get_meta("_can_be_imported")) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | TreeItem *data_type_node = p_tree_item->get_parent(); |
| 370 | if (!data_type_node || data_type_node == import_items_tree->get_root()) { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | TreeItem *type_node = data_type_node->get_parent(); |
| 375 | if (!type_node || type_node == import_items_tree->get_root()) { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | ThemeItem ti; |
| 380 | ti.item_name = p_tree_item->get_text(0); |
| 381 | ti.data_type = (Theme::DataType)(int)data_type_node->get_metadata(0); |
| 382 | ti.type_name = type_node->get_text(0); |
| 383 | |
| 384 | bool import = p_tree_item->is_checked(IMPORT_ITEM); |
| 385 | bool with_data = p_tree_item->is_checked(IMPORT_ITEM_DATA); |
| 386 | |
| 387 | if (import && with_data) { |
| 388 | selected_items[ti] = SELECT_IMPORT_FULL; |
| 389 | } else if (import) { |
| 390 | selected_items[ti] = SELECT_IMPORT_DEFINITION; |
| 391 | } else { |
| 392 | selected_items.erase(ti); |
| 393 | } |
| 394 | |
| 395 | _update_total_selected(ti.data_type); |
| 396 | } |
| 397 | |
| 398 | void ThemeItemImportTree::_restore_selected_item(TreeItem *p_tree_item) { |
| 399 | if (!p_tree_item->get_meta("_can_be_imported")) { |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | TreeItem *data_type_node = p_tree_item->get_parent(); |
| 404 | if (!data_type_node || data_type_node == import_items_tree->get_root()) { |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | TreeItem *type_node = data_type_node->get_parent(); |
| 409 | if (!type_node || type_node == import_items_tree->get_root()) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | ThemeItem ti; |
| 414 | ti.item_name = p_tree_item->get_text(0); |
| 415 | ti.data_type = (Theme::DataType)(int)data_type_node->get_metadata(0); |
| 416 | ti.type_name = type_node->get_text(0); |
| 417 | |
| 418 | if (!selected_items.has(ti)) { |
| 419 | p_tree_item->set_checked(IMPORT_ITEM, false); |
| 420 | p_tree_item->set_checked(IMPORT_ITEM_DATA, false); |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | if (selected_items[ti] == SELECT_IMPORT_FULL) { |
| 425 | p_tree_item->set_checked(IMPORT_ITEM, true); |
| 426 | p_tree_item->set_checked(IMPORT_ITEM_DATA, true); |
| 427 | } else if (selected_items[ti] == SELECT_IMPORT_DEFINITION) { |
| 428 | p_tree_item->set_checked(IMPORT_ITEM, true); |
| 429 | p_tree_item->set_checked(IMPORT_ITEM_DATA, false); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void ThemeItemImportTree::_update_total_selected(Theme::DataType p_data_type) { |
| 434 | ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds."); |
| 435 | |
| 436 | Label *total_selected_items_label = nullptr; |
| 437 | switch (p_data_type) { |
| 438 | case Theme::DATA_TYPE_COLOR: |
| 439 | total_selected_items_label = total_selected_colors_label; |
| 440 | break; |
| 441 | |
| 442 | case Theme::DATA_TYPE_CONSTANT: |
| 443 | total_selected_items_label = total_selected_constants_label; |
| 444 | break; |
| 445 | |
| 446 | case Theme::DATA_TYPE_FONT: |
| 447 | total_selected_items_label = total_selected_fonts_label; |
| 448 | break; |
| 449 | |
| 450 | case Theme::DATA_TYPE_FONT_SIZE: |
| 451 | total_selected_items_label = total_selected_font_sizes_label; |
| 452 | break; |
| 453 | |
| 454 | case Theme::DATA_TYPE_ICON: |
| 455 | total_selected_items_label = total_selected_icons_label; |
| 456 | break; |
| 457 | |
| 458 | case Theme::DATA_TYPE_STYLEBOX: |
| 459 | total_selected_items_label = total_selected_styleboxes_label; |
| 460 | break; |
| 461 | |
| 462 | case Theme::DATA_TYPE_MAX: |
| 463 | return; // Can't happen, but silences warning. |
| 464 | } |
| 465 | |
| 466 | if (!total_selected_items_label) { |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | int count = 0; |
| 471 | for (const KeyValue<ThemeItem, ItemCheckedState> &E : selected_items) { |
| 472 | ThemeItem ti = E.key; |
| 473 | if (ti.data_type == p_data_type) { |
| 474 | count++; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (count == 0) { |
| 479 | total_selected_items_label->hide(); |
| 480 | } else { |
| 481 | Array arr = { count }; |
| 482 | total_selected_items_label->set_text(TTRN("{num} currently selected", "{num} currently selected", count).format(arr, "{num}")); |
| 483 | total_selected_items_label->show(); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void ThemeItemImportTree::_tree_item_edited() { |
| 488 | if (updating_tree) { |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | TreeItem *edited_item = import_items_tree->get_edited(); |
| 493 | if (!edited_item) { |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | updating_tree = true; |
| 498 | |
| 499 | int edited_column = import_items_tree->get_edited_column(); |
| 500 | bool is_checked = edited_item->is_checked(edited_column); |
| 501 | if (is_checked) { |
| 502 | if (edited_column == IMPORT_ITEM_DATA) { |
| 503 | edited_item->set_checked(IMPORT_ITEM, true); |
| 504 | edited_item->propagate_check(IMPORT_ITEM); |
| 505 | } |
| 506 | } else { |
| 507 | if (edited_column == IMPORT_ITEM) { |
| 508 | edited_item->set_checked(IMPORT_ITEM_DATA, false); |
| 509 | edited_item->propagate_check(IMPORT_ITEM_DATA); |
| 510 | } |
| 511 | } |
| 512 | edited_item->propagate_check(edited_column); |
| 513 | updating_tree = false; |
| 514 | } |
| 515 | |
| 516 | void ThemeItemImportTree::_check_propagated_to_tree_item(Object *p_obj, int p_column) { |
| 517 | TreeItem *item = Object::cast_to<TreeItem>(p_obj); |
| 518 | // Skip "category" tree items by checking for children. |
| 519 | if (item && !item->get_first_child()) { |
| 520 | _store_selected_item(item); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void ThemeItemImportTree::_select_all_subitems(TreeItem *p_root_item, bool p_select_with_data) { |
| 525 | TreeItem *child_item = p_root_item->get_first_child(); |
| 526 | while (child_item) { |
| 527 | child_item->set_checked(IMPORT_ITEM, true); |
| 528 | if (p_select_with_data) { |
| 529 | child_item->set_checked(IMPORT_ITEM_DATA, true); |
| 530 | } |
| 531 | _store_selected_item(child_item); |
| 532 | |
| 533 | _select_all_subitems(child_item, p_select_with_data); |
| 534 | child_item = child_item->get_next(); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | void ThemeItemImportTree::_deselect_all_subitems(TreeItem *p_root_item, bool p_deselect_completely) { |
| 539 | TreeItem *child_item = p_root_item->get_first_child(); |
| 540 | while (child_item) { |
| 541 | child_item->set_checked(IMPORT_ITEM_DATA, false); |
| 542 | if (p_deselect_completely) { |
| 543 | child_item->set_checked(IMPORT_ITEM, false); |
| 544 | } |
| 545 | _store_selected_item(child_item); |
| 546 | |
| 547 | _deselect_all_subitems(child_item, p_deselect_completely); |
| 548 | child_item = child_item->get_next(); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void ThemeItemImportTree::_select_all_items_pressed() { |
| 553 | if (updating_tree) { |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | updating_tree = true; |
| 558 | |
| 559 | TreeItem *root = import_items_tree->get_root(); |
| 560 | _select_all_subitems(root, false); |
| 561 | |
| 562 | updating_tree = false; |
| 563 | } |
| 564 | |
| 565 | void ThemeItemImportTree::_select_full_items_pressed() { |
| 566 | if (updating_tree) { |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | updating_tree = true; |
| 571 | |
| 572 | TreeItem *root = import_items_tree->get_root(); |
| 573 | _select_all_subitems(root, true); |
| 574 | |
| 575 | updating_tree = false; |
| 576 | } |
| 577 | |
| 578 | void ThemeItemImportTree::_deselect_all_items_pressed() { |
| 579 | if (updating_tree) { |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | updating_tree = true; |
| 584 | |
| 585 | TreeItem *root = import_items_tree->get_root(); |
| 586 | _deselect_all_subitems(root, true); |
| 587 | |
| 588 | updating_tree = false; |
| 589 | } |
| 590 | |
| 591 | void ThemeItemImportTree::_select_all_data_type_pressed(int p_data_type) { |
| 592 | ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds."); |
| 593 | |
| 594 | if (updating_tree) { |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | Theme::DataType data_type = (Theme::DataType)p_data_type; |
| 599 | List<TreeItem *> *item_list = nullptr; |
| 600 | |
| 601 | switch (data_type) { |
| 602 | case Theme::DATA_TYPE_COLOR: |
| 603 | item_list = &tree_color_items; |
| 604 | break; |
| 605 | |
| 606 | case Theme::DATA_TYPE_CONSTANT: |
| 607 | item_list = &tree_constant_items; |
| 608 | break; |
| 609 | |
| 610 | case Theme::DATA_TYPE_FONT: |
| 611 | item_list = &tree_font_items; |
| 612 | break; |
| 613 | |
| 614 | case Theme::DATA_TYPE_FONT_SIZE: |
| 615 | item_list = &tree_font_size_items; |
| 616 | break; |
| 617 | |
| 618 | case Theme::DATA_TYPE_ICON: |
| 619 | item_list = &tree_icon_items; |
| 620 | break; |
| 621 | |
| 622 | case Theme::DATA_TYPE_STYLEBOX: |
| 623 | item_list = &tree_stylebox_items; |
| 624 | break; |
| 625 | |
| 626 | case Theme::DATA_TYPE_MAX: |
| 627 | return; // Can't happen, but silences warning. |
| 628 | } |
| 629 | |
| 630 | updating_tree = true; |
| 631 | |
| 632 | for (List<TreeItem *>::Element *E = item_list->front(); E; E = E->next()) { |
| 633 | TreeItem *child_item = E->get(); |
| 634 | if (!child_item) { |
| 635 | continue; |
| 636 | } |
| 637 | |
| 638 | child_item->set_checked(IMPORT_ITEM, true); |
| 639 | child_item->propagate_check(IMPORT_ITEM, false); |
| 640 | _store_selected_item(child_item); |
| 641 | } |
| 642 | |
| 643 | updating_tree = false; |
| 644 | } |
| 645 | |
| 646 | void ThemeItemImportTree::_select_full_data_type_pressed(int p_data_type) { |
| 647 | ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds."); |
| 648 | |
| 649 | if (updating_tree) { |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | Theme::DataType data_type = (Theme::DataType)p_data_type; |
| 654 | List<TreeItem *> *item_list = nullptr; |
| 655 | |
| 656 | switch (data_type) { |
| 657 | case Theme::DATA_TYPE_COLOR: |
| 658 | item_list = &tree_color_items; |
| 659 | break; |
| 660 | |
| 661 | case Theme::DATA_TYPE_CONSTANT: |
| 662 | item_list = &tree_constant_items; |
| 663 | break; |
| 664 | |
| 665 | case Theme::DATA_TYPE_FONT: |
| 666 | item_list = &tree_font_items; |
| 667 | break; |
| 668 | |
| 669 | case Theme::DATA_TYPE_FONT_SIZE: |
| 670 | item_list = &tree_font_size_items; |
| 671 | break; |
| 672 | |
| 673 | case Theme::DATA_TYPE_ICON: |
| 674 | item_list = &tree_icon_items; |
| 675 | break; |
| 676 | |
| 677 | case Theme::DATA_TYPE_STYLEBOX: |
| 678 | item_list = &tree_stylebox_items; |
| 679 | break; |
| 680 | |
| 681 | case Theme::DATA_TYPE_MAX: |
| 682 | return; // Can't happen, but silences warning. |
| 683 | } |
| 684 | |
| 685 | updating_tree = true; |
| 686 | |
| 687 | for (List<TreeItem *>::Element *E = item_list->front(); E; E = E->next()) { |
| 688 | TreeItem *child_item = E->get(); |
| 689 | if (!child_item) { |
| 690 | continue; |
| 691 | } |
| 692 | |
| 693 | child_item->set_checked(IMPORT_ITEM, true); |
| 694 | child_item->set_checked(IMPORT_ITEM_DATA, true); |
| 695 | child_item->propagate_check(IMPORT_ITEM, false); |
| 696 | child_item->propagate_check(IMPORT_ITEM_DATA, false); |
| 697 | _store_selected_item(child_item); |
| 698 | } |
| 699 | |
| 700 | updating_tree = false; |
| 701 | } |
| 702 | |
| 703 | void ThemeItemImportTree::_deselect_all_data_type_pressed(int p_data_type) { |
| 704 | ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds."); |
| 705 | |
| 706 | if (updating_tree) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | Theme::DataType data_type = (Theme::DataType)p_data_type; |
| 711 | List<TreeItem *> *item_list = nullptr; |
| 712 | |
| 713 | switch (data_type) { |
| 714 | case Theme::DATA_TYPE_COLOR: |
| 715 | item_list = &tree_color_items; |
| 716 | break; |
| 717 | |
| 718 | case Theme::DATA_TYPE_CONSTANT: |
| 719 | item_list = &tree_constant_items; |
| 720 | break; |
| 721 | |
| 722 | case Theme::DATA_TYPE_FONT: |
| 723 | item_list = &tree_font_items; |
| 724 | break; |
| 725 | |
| 726 | case Theme::DATA_TYPE_FONT_SIZE: |
| 727 | item_list = &tree_font_size_items; |
| 728 | break; |
| 729 | |
| 730 | case Theme::DATA_TYPE_ICON: |
| 731 | item_list = &tree_icon_items; |
| 732 | break; |
| 733 | |
| 734 | case Theme::DATA_TYPE_STYLEBOX: |
| 735 | item_list = &tree_stylebox_items; |
| 736 | break; |
| 737 | |
| 738 | case Theme::DATA_TYPE_MAX: |
| 739 | return; // Can't happen, but silences warning. |
| 740 | } |
| 741 | |
| 742 | updating_tree = true; |
| 743 | |
| 744 | for (List<TreeItem *>::Element *E = item_list->front(); E; E = E->next()) { |
| 745 | TreeItem *child_item = E->get(); |
| 746 | if (!child_item) { |
| 747 | continue; |
| 748 | } |
| 749 | |
| 750 | child_item->set_checked(IMPORT_ITEM, false); |
| 751 | child_item->set_checked(IMPORT_ITEM_DATA, false); |
| 752 | child_item->propagate_check(IMPORT_ITEM, false); |
| 753 | child_item->propagate_check(IMPORT_ITEM_DATA, false); |
| 754 | _store_selected_item(child_item); |
| 755 | } |
| 756 | |
| 757 | updating_tree = false; |
| 758 | } |
| 759 | |
| 760 | void ThemeItemImportTree::_import_selected() { |
| 761 | if (selected_items.is_empty()) { |
| 762 | EditorNode::get_singleton()->show_accept(TTR("Nothing was selected for the import."), TTR("OK")); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 767 | Ref<Theme> new_snapshot = edited_theme->duplicate(); |
| 768 | |
| 769 | ProgressDialog::get_singleton()->add_task("import_theme_items", TTR("Importing Theme Items"), selected_items.size() + 2); |
| 770 | |
| 771 | int idx = 0; |
| 772 | for (KeyValue<ThemeItem, ItemCheckedState> &E : selected_items) { |
| 773 | // Arbitrary number of items to skip from reporting. |
| 774 | // Reduces the number of UI updates that this causes when copying large themes. |
| 775 | if (idx % 10 == 0) { |
| 776 | Array arr = { idx + 1, selected_items.size() }; |
| 777 | ProgressDialog::get_singleton()->task_step("import_theme_items", TTR("Importing items {n}/{n}").format(arr, "{n}"), idx); |
| 778 | } |
| 779 | |
| 780 | ItemCheckedState cs = E.value; |
| 781 | ThemeItem ti = E.key; |
| 782 | |
| 783 | if (cs == SELECT_IMPORT_DEFINITION || cs == SELECT_IMPORT_FULL) { |
| 784 | Variant item_value = Variant(); |
| 785 | |
| 786 | if (cs == SELECT_IMPORT_FULL) { |
| 787 | item_value = base_theme->get_theme_item(ti.data_type, ti.item_name, ti.type_name); |
| 788 | } else { |
| 789 | switch (ti.data_type) { |
| 790 | case Theme::DATA_TYPE_COLOR: |
| 791 | item_value = Color(); |
| 792 | break; |
| 793 | |
| 794 | case Theme::DATA_TYPE_CONSTANT: |
| 795 | item_value = 0; |
| 796 | break; |
| 797 | |
| 798 | case Theme::DATA_TYPE_FONT: |
| 799 | item_value = Ref<Font>(); |
| 800 | break; |
| 801 | |
| 802 | case Theme::DATA_TYPE_FONT_SIZE: |
| 803 | item_value = -1; |
| 804 | break; |
| 805 | |
| 806 | case Theme::DATA_TYPE_ICON: |
| 807 | item_value = Ref<Texture2D>(); |
| 808 | break; |
| 809 | |
| 810 | case Theme::DATA_TYPE_STYLEBOX: |
| 811 | item_value = Ref<StyleBox>(); |
| 812 | break; |
| 813 | |
| 814 | case Theme::DATA_TYPE_MAX: |
| 815 | break; // Can't happen, but silences warning. |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | new_snapshot->set_theme_item(ti.data_type, ti.item_name, ti.type_name, item_value); |
| 820 | } |
| 821 | |
| 822 | idx++; |
| 823 | } |
| 824 | |
| 825 | // Allow changes to be reported now that the operation is finished. |
| 826 | ProgressDialog::get_singleton()->task_step("import_theme_items", TTR("Updating the editor"), idx++); |
| 827 | |
| 828 | // Make sure the task is not ended before the editor freezes to update the Inspector. |
| 829 | ProgressDialog::get_singleton()->task_step("import_theme_items", TTR("Finalizing"), idx++); |
| 830 | |
| 831 | ProgressDialog::get_singleton()->end_task("import_theme_items"); |
| 832 | |
| 833 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 834 | ur->create_action(TTR("Import Theme Items")); |
| 835 | |
| 836 | ur->add_do_method(*edited_theme, "clear"); |
| 837 | ur->add_do_method(*edited_theme, "merge_with", new_snapshot); |
| 838 | ur->add_undo_method(*edited_theme, "clear"); |
| 839 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 840 | |
| 841 | ur->add_do_method(this, "emit_signal", SNAME("items_imported")); |
| 842 | ur->add_undo_method(this, "emit_signal", SNAME("items_imported")); |
| 843 | |
| 844 | ur->commit_action(); |
| 845 | } |
| 846 | |
| 847 | void ThemeItemImportTree::set_edited_theme(const Ref<Theme> &p_theme) { |
| 848 | edited_theme = p_theme; |
| 849 | } |
| 850 | |
| 851 | void ThemeItemImportTree::set_base_theme(const Ref<Theme> &p_theme) { |
| 852 | base_theme = p_theme; |
| 853 | } |
| 854 | |
| 855 | void ThemeItemImportTree::reset_item_tree() { |
| 856 | import_items_filter->clear(); |
| 857 | selected_items.clear(); |
| 858 | |
| 859 | total_selected_colors_label->hide(); |
| 860 | total_selected_constants_label->hide(); |
| 861 | total_selected_fonts_label->hide(); |
| 862 | total_selected_font_sizes_label->hide(); |
| 863 | total_selected_icons_label->hide(); |
| 864 | total_selected_styleboxes_label->hide(); |
| 865 | |
| 866 | _update_items_tree(); |
| 867 | } |
| 868 | |
| 869 | bool ThemeItemImportTree::has_selected_items() const { |
| 870 | return (selected_items.size() > 0); |
| 871 | } |
| 872 | |
| 873 | void ThemeItemImportTree::_notification(int p_what) { |
| 874 | switch (p_what) { |
| 875 | case NOTIFICATION_THEME_CHANGED: { |
| 876 | select_icons_warning_icon->set_texture(get_editor_theme_icon(SNAME("StatusWarning"))); |
| 877 | select_icons_warning->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor))); |
| 878 | |
| 879 | import_items_filter->set_right_icon(get_editor_theme_icon(SNAME("Search"))); |
| 880 | |
| 881 | // Bottom panel buttons. |
| 882 | import_collapse_types_button->set_button_icon(get_editor_theme_icon(SNAME("CollapseTree"))); |
| 883 | import_expand_types_button->set_button_icon(get_editor_theme_icon(SNAME("ExpandTree"))); |
| 884 | |
| 885 | import_select_all_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll"))); |
| 886 | import_select_full_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull"))); |
| 887 | import_deselect_all_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll"))); |
| 888 | |
| 889 | // Side panel buttons. |
| 890 | select_colors_icon->set_texture(get_editor_theme_icon(SNAME("Color"))); |
| 891 | deselect_all_colors_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll"))); |
| 892 | select_all_colors_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll"))); |
| 893 | select_full_colors_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull"))); |
| 894 | |
| 895 | select_constants_icon->set_texture(get_editor_theme_icon(SNAME("MemberConstant"))); |
| 896 | deselect_all_constants_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll"))); |
| 897 | select_all_constants_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll"))); |
| 898 | select_full_constants_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull"))); |
| 899 | |
| 900 | select_fonts_icon->set_texture(get_editor_theme_icon(SNAME("FontItem"))); |
| 901 | deselect_all_fonts_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll"))); |
| 902 | select_all_fonts_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll"))); |
| 903 | select_full_fonts_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull"))); |
| 904 | |
| 905 | select_font_sizes_icon->set_texture(get_editor_theme_icon(SNAME("FontSize"))); |
| 906 | deselect_all_font_sizes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll"))); |
| 907 | select_all_font_sizes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll"))); |
| 908 | select_full_font_sizes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull"))); |
| 909 | |
| 910 | select_icons_icon->set_texture(get_editor_theme_icon(SNAME("ImageTexture"))); |
| 911 | deselect_all_icons_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll"))); |
| 912 | select_all_icons_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll"))); |
| 913 | select_full_icons_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull"))); |
| 914 | |
| 915 | select_styleboxes_icon->set_texture(get_editor_theme_icon(SNAME("StyleBoxFlat"))); |
| 916 | deselect_all_styleboxes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeDeselectAll"))); |
| 917 | select_all_styleboxes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectAll"))); |
| 918 | select_full_styleboxes_button->set_button_icon(get_editor_theme_icon(SNAME("ThemeSelectFull"))); |
| 919 | } break; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | void ThemeItemImportTree::_bind_methods() { |
| 924 | ADD_SIGNAL(MethodInfo("items_imported")); |
| 925 | } |
| 926 | |
| 927 | ThemeItemImportTree::ThemeItemImportTree() { |
| 928 | import_items_filter = memnew(LineEdit); |
| 929 | import_items_filter->set_placeholder(TTR("Filter Items")); |
| 930 | import_items_filter->set_clear_button_enabled(true); |
| 931 | add_child(import_items_filter); |
| 932 | import_items_filter->connect(SceneStringName(text_changed), callable_mp(this, &ThemeItemImportTree::_filter_text_changed)); |
| 933 | |
| 934 | HBoxContainer *import_main_hb = memnew(HBoxContainer); |
| 935 | import_main_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 936 | add_child(import_main_hb); |
| 937 | |
| 938 | import_items_tree = memnew(Tree); |
| 939 | import_items_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 940 | import_items_tree->set_hide_root(true); |
| 941 | import_items_tree->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 942 | import_main_hb->add_child(import_items_tree); |
| 943 | import_items_tree->connect("item_edited", callable_mp(this, &ThemeItemImportTree::_tree_item_edited)); |
| 944 | import_items_tree->connect("check_propagated_to_item", callable_mp(this, &ThemeItemImportTree::_check_propagated_to_tree_item)); |
| 945 | |
| 946 | import_items_tree->set_columns(3); |
| 947 | import_items_tree->set_column_titles_visible(true); |
| 948 | import_items_tree->set_column_title(IMPORT_ITEM, TTR("Import")); |
| 949 | import_items_tree->set_column_title(IMPORT_ITEM_DATA, TTR("With Data")); |
| 950 | import_items_tree->set_column_expand(0, true); |
| 951 | import_items_tree->set_column_clip_content(0, true); |
| 952 | import_items_tree->set_column_expand(IMPORT_ITEM, false); |
| 953 | import_items_tree->set_column_expand(IMPORT_ITEM_DATA, false); |
| 954 | import_items_tree->set_column_custom_minimum_width(0, 160 * EDSCALE); |
| 955 | import_items_tree->set_column_custom_minimum_width(IMPORT_ITEM, 80 * EDSCALE); |
| 956 | import_items_tree->set_column_custom_minimum_width(IMPORT_ITEM_DATA, 80 * EDSCALE); |
| 957 | import_items_tree->set_column_clip_content(1, true); |
| 958 | import_items_tree->set_column_clip_content(2, true); |
| 959 | import_items_tree->set_theme_type_variation("TreeSecondary"); |
| 960 | |
| 961 | ScrollContainer *import_bulk_sc = memnew(ScrollContainer); |
| 962 | import_bulk_sc->set_custom_minimum_size(Size2(260.0, 0.0) * EDSCALE); |
| 963 | import_bulk_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 964 | import_bulk_sc->set_theme_type_variation("ScrollContainerSecondary"); |
| 965 | import_main_hb->add_child(import_bulk_sc); |
| 966 | VBoxContainer *import_bulk_vb = memnew(VBoxContainer); |
| 967 | import_bulk_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 968 | import_bulk_sc->add_child(import_bulk_vb); |
| 969 | |
| 970 | Label *import_bulk_label = memnew(Label); |
| 971 | import_bulk_label->set_text(TTR("Select by data type:")); |
| 972 | import_bulk_vb->add_child(import_bulk_label); |
| 973 | |
| 974 | select_colors_icon = memnew(TextureRect); |
| 975 | select_colors_label = memnew(Label); |
| 976 | deselect_all_colors_button = memnew(Button); |
| 977 | select_all_colors_button = memnew(Button); |
| 978 | select_full_colors_button = memnew(Button); |
| 979 | total_selected_colors_label = memnew(Label); |
| 980 | |
| 981 | select_constants_icon = memnew(TextureRect); |
| 982 | select_constants_label = memnew(Label); |
| 983 | deselect_all_constants_button = memnew(Button); |
| 984 | select_all_constants_button = memnew(Button); |
| 985 | select_full_constants_button = memnew(Button); |
| 986 | total_selected_constants_label = memnew(Label); |
| 987 | |
| 988 | select_fonts_icon = memnew(TextureRect); |
| 989 | select_fonts_label = memnew(Label); |
| 990 | deselect_all_fonts_button = memnew(Button); |
| 991 | select_all_fonts_button = memnew(Button); |
| 992 | select_full_fonts_button = memnew(Button); |
| 993 | total_selected_fonts_label = memnew(Label); |
| 994 | |
| 995 | select_font_sizes_icon = memnew(TextureRect); |
| 996 | select_font_sizes_label = memnew(Label); |
| 997 | deselect_all_font_sizes_button = memnew(Button); |
| 998 | select_all_font_sizes_button = memnew(Button); |
| 999 | select_full_font_sizes_button = memnew(Button); |
| 1000 | total_selected_font_sizes_label = memnew(Label); |
| 1001 | |
| 1002 | select_icons_icon = memnew(TextureRect); |
| 1003 | select_icons_label = memnew(Label); |
| 1004 | deselect_all_icons_button = memnew(Button); |
| 1005 | select_all_icons_button = memnew(Button); |
| 1006 | select_full_icons_button = memnew(Button); |
| 1007 | total_selected_icons_label = memnew(Label); |
| 1008 | |
| 1009 | select_styleboxes_icon = memnew(TextureRect); |
| 1010 | select_styleboxes_label = memnew(Label); |
| 1011 | deselect_all_styleboxes_button = memnew(Button); |
| 1012 | select_all_styleboxes_button = memnew(Button); |
| 1013 | select_full_styleboxes_button = memnew(Button); |
| 1014 | total_selected_styleboxes_label = memnew(Label); |
| 1015 | |
| 1016 | for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) { |
| 1017 | Theme::DataType dt = (Theme::DataType)i; |
| 1018 | |
| 1019 | TextureRect *select_items_icon = nullptr; |
| 1020 | Label *select_items_label = nullptr; |
| 1021 | Button *deselect_all_items_button = nullptr; |
| 1022 | Button *select_all_items_button = nullptr; |
| 1023 | Button *select_full_items_button = nullptr; |
| 1024 | Label *total_selected_items_label = nullptr; |
| 1025 | |
| 1026 | String items_title; |
| 1027 | String select_all_items_tooltip; |
| 1028 | String select_full_items_tooltip; |
| 1029 | String deselect_all_items_tooltip; |
| 1030 | |
| 1031 | switch (dt) { |
| 1032 | case Theme::DATA_TYPE_COLOR: |
| 1033 | select_items_icon = select_colors_icon; |
| 1034 | select_items_label = select_colors_label; |
| 1035 | deselect_all_items_button = deselect_all_colors_button; |
| 1036 | select_all_items_button = select_all_colors_button; |
| 1037 | select_full_items_button = select_full_colors_button; |
| 1038 | total_selected_items_label = total_selected_colors_label; |
| 1039 | |
| 1040 | items_title = TTR("Colors"); |
| 1041 | select_all_items_tooltip = TTR("Select all visible color items."); |
| 1042 | select_full_items_tooltip = TTR("Select all visible color items and their data."); |
| 1043 | deselect_all_items_tooltip = TTR("Deselect all visible color items."); |
| 1044 | break; |
| 1045 | |
| 1046 | case Theme::DATA_TYPE_CONSTANT: |
| 1047 | select_items_icon = select_constants_icon; |
| 1048 | select_items_label = select_constants_label; |
| 1049 | deselect_all_items_button = deselect_all_constants_button; |
| 1050 | select_all_items_button = select_all_constants_button; |
| 1051 | select_full_items_button = select_full_constants_button; |
| 1052 | total_selected_items_label = total_selected_constants_label; |
| 1053 | |
| 1054 | items_title = TTR("Constants"); |
| 1055 | select_all_items_tooltip = TTR("Select all visible constant items."); |
| 1056 | select_full_items_tooltip = TTR("Select all visible constant items and their data."); |
| 1057 | deselect_all_items_tooltip = TTR("Deselect all visible constant items."); |
| 1058 | break; |
| 1059 | |
| 1060 | case Theme::DATA_TYPE_FONT: |
| 1061 | select_items_icon = select_fonts_icon; |
| 1062 | select_items_label = select_fonts_label; |
| 1063 | deselect_all_items_button = deselect_all_fonts_button; |
| 1064 | select_all_items_button = select_all_fonts_button; |
| 1065 | select_full_items_button = select_full_fonts_button; |
| 1066 | total_selected_items_label = total_selected_fonts_label; |
| 1067 | |
| 1068 | items_title = TTR("Fonts"); |
| 1069 | select_all_items_tooltip = TTR("Select all visible font items."); |
| 1070 | select_full_items_tooltip = TTR("Select all visible font items and their data."); |
| 1071 | deselect_all_items_tooltip = TTR("Deselect all visible font items."); |
| 1072 | break; |
| 1073 | |
| 1074 | case Theme::DATA_TYPE_FONT_SIZE: |
| 1075 | select_items_icon = select_font_sizes_icon; |
| 1076 | select_items_label = select_font_sizes_label; |
| 1077 | deselect_all_items_button = deselect_all_font_sizes_button; |
| 1078 | select_all_items_button = select_all_font_sizes_button; |
| 1079 | select_full_items_button = select_full_font_sizes_button; |
| 1080 | total_selected_items_label = total_selected_font_sizes_label; |
| 1081 | |
| 1082 | items_title = TTR("Font sizes"); |
| 1083 | select_all_items_tooltip = TTR("Select all visible font size items."); |
| 1084 | select_full_items_tooltip = TTR("Select all visible font size items and their data."); |
| 1085 | deselect_all_items_tooltip = TTR("Deselect all visible font size items."); |
| 1086 | break; |
| 1087 | |
| 1088 | case Theme::DATA_TYPE_ICON: |
| 1089 | select_items_icon = select_icons_icon; |
| 1090 | select_items_label = select_icons_label; |
| 1091 | deselect_all_items_button = deselect_all_icons_button; |
| 1092 | select_all_items_button = select_all_icons_button; |
| 1093 | select_full_items_button = select_full_icons_button; |
| 1094 | total_selected_items_label = total_selected_icons_label; |
| 1095 | |
| 1096 | items_title = TTR("Icons"); |
| 1097 | select_all_items_tooltip = TTR("Select all visible icon items."); |
| 1098 | select_full_items_tooltip = TTR("Select all visible icon items and their data."); |
| 1099 | deselect_all_items_tooltip = TTR("Deselect all visible icon items."); |
| 1100 | break; |
| 1101 | |
| 1102 | case Theme::DATA_TYPE_STYLEBOX: |
| 1103 | select_items_icon = select_styleboxes_icon; |
| 1104 | select_items_label = select_styleboxes_label; |
| 1105 | deselect_all_items_button = deselect_all_styleboxes_button; |
| 1106 | select_all_items_button = select_all_styleboxes_button; |
| 1107 | select_full_items_button = select_full_styleboxes_button; |
| 1108 | total_selected_items_label = total_selected_styleboxes_label; |
| 1109 | |
| 1110 | items_title = TTR("Styleboxes"); |
| 1111 | select_all_items_tooltip = TTR("Select all visible stylebox items."); |
| 1112 | select_full_items_tooltip = TTR("Select all visible stylebox items and their data."); |
| 1113 | deselect_all_items_tooltip = TTR("Deselect all visible stylebox items."); |
| 1114 | break; |
| 1115 | |
| 1116 | case Theme::DATA_TYPE_MAX: |
| 1117 | continue; // Can't happen, but silences warning. |
| 1118 | } |
| 1119 | |
| 1120 | if (i > 0) { |
| 1121 | import_bulk_vb->add_child(memnew(HSeparator)); |
| 1122 | } |
| 1123 | |
| 1124 | HBoxContainer *all_set = memnew(HBoxContainer); |
| 1125 | import_bulk_vb->add_child(all_set); |
| 1126 | |
| 1127 | HBoxContainer *label_set = memnew(HBoxContainer); |
| 1128 | label_set->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1129 | all_set->add_child(label_set); |
| 1130 | select_items_icon->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 1131 | label_set->add_child(select_items_icon); |
| 1132 | select_items_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1133 | select_items_label->set_clip_text(true); |
| 1134 | select_items_label->set_text(items_title); |
| 1135 | label_set->add_child(select_items_label); |
| 1136 | |
| 1137 | HBoxContainer *button_set = memnew(HBoxContainer); |
| 1138 | button_set->set_alignment(BoxContainer::ALIGNMENT_END); |
| 1139 | all_set->add_child(button_set); |
| 1140 | select_all_items_button->set_flat(true); |
| 1141 | select_all_items_button->set_tooltip_text(select_all_items_tooltip); |
| 1142 | button_set->add_child(select_all_items_button); |
| 1143 | select_all_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_all_data_type_pressed).bind(i)); |
| 1144 | select_full_items_button->set_flat(true); |
| 1145 | select_full_items_button->set_tooltip_text(select_full_items_tooltip); |
| 1146 | button_set->add_child(select_full_items_button); |
| 1147 | select_full_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_full_data_type_pressed).bind(i)); |
| 1148 | deselect_all_items_button->set_flat(true); |
| 1149 | deselect_all_items_button->set_tooltip_text(deselect_all_items_tooltip); |
| 1150 | button_set->add_child(deselect_all_items_button); |
| 1151 | deselect_all_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_deselect_all_data_type_pressed).bind(i)); |
| 1152 | |
| 1153 | total_selected_items_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
| 1154 | total_selected_items_label->hide(); |
| 1155 | import_bulk_vb->add_child(total_selected_items_label); |
| 1156 | |
| 1157 | if (dt == Theme::DATA_TYPE_ICON) { |
| 1158 | select_icons_warning_hb = memnew(HBoxContainer); |
| 1159 | import_bulk_vb->add_child(select_icons_warning_hb); |
| 1160 | |
| 1161 | select_icons_warning_icon = memnew(TextureRect); |
| 1162 | select_icons_warning_icon->set_v_size_flags(Control::SIZE_SHRINK_CENTER); |
| 1163 | select_icons_warning_hb->add_child(select_icons_warning_icon); |
| 1164 | |
| 1165 | select_icons_warning = memnew(Label); |
| 1166 | select_icons_warning->set_text(TTR("Caution: Adding icon data may considerably increase the size of your Theme resource.")); |
| 1167 | select_icons_warning->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART); |
| 1168 | select_icons_warning->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 1169 | select_icons_warning_hb->add_child(select_icons_warning); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | HBoxContainer *import_buttons = memnew(HBoxContainer); |
| 1174 | add_child(import_buttons); |
| 1175 | |
| 1176 | import_collapse_types_button = memnew(Button); |
| 1177 | import_collapse_types_button->set_flat(true); |
| 1178 | import_collapse_types_button->set_tooltip_text(TTR("Collapse types.")); |
| 1179 | import_buttons->add_child(import_collapse_types_button); |
| 1180 | import_collapse_types_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_toggle_type_items).bind(true)); |
| 1181 | import_expand_types_button = memnew(Button); |
| 1182 | import_expand_types_button->set_flat(true); |
| 1183 | import_expand_types_button->set_tooltip_text(TTR("Expand types.")); |
| 1184 | import_buttons->add_child(import_expand_types_button); |
| 1185 | import_expand_types_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_toggle_type_items).bind(false)); |
| 1186 | |
| 1187 | import_buttons->add_child(memnew(VSeparator)); |
| 1188 | |
| 1189 | import_select_all_button = memnew(Button); |
| 1190 | import_select_all_button->set_flat(true); |
| 1191 | import_select_all_button->set_text(TTR("Select All")); |
| 1192 | import_select_all_button->set_tooltip_text(TTR("Select all Theme items.")); |
| 1193 | import_buttons->add_child(import_select_all_button); |
| 1194 | import_select_all_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_all_items_pressed)); |
| 1195 | import_select_full_button = memnew(Button); |
| 1196 | import_select_full_button->set_flat(true); |
| 1197 | import_select_full_button->set_text(TTR("Select With Data")); |
| 1198 | import_select_full_button->set_tooltip_text(TTR("Select all Theme items with item data.")); |
| 1199 | import_buttons->add_child(import_select_full_button); |
| 1200 | import_select_full_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_select_full_items_pressed)); |
| 1201 | import_deselect_all_button = memnew(Button); |
| 1202 | import_deselect_all_button->set_flat(true); |
| 1203 | import_deselect_all_button->set_text(TTR("Deselect All")); |
| 1204 | import_deselect_all_button->set_tooltip_text(TTR("Deselect all Theme items.")); |
| 1205 | import_buttons->add_child(import_deselect_all_button); |
| 1206 | import_deselect_all_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_deselect_all_items_pressed)); |
| 1207 | |
| 1208 | import_buttons->add_spacer(); |
| 1209 | |
| 1210 | Button *import_add_selected_button = memnew(Button); |
| 1211 | import_add_selected_button->set_text(TTR("Import Selected")); |
| 1212 | import_buttons->add_child(import_add_selected_button); |
| 1213 | import_add_selected_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemImportTree::_import_selected)); |
| 1214 | } |
| 1215 | |
| 1216 | /////////////////////// |
| 1217 | |
| 1218 | void ThemeItemEditorDialog::ok_pressed() { |
| 1219 | if (import_default_theme_items->has_selected_items() || import_editor_theme_items->has_selected_items() || import_other_theme_items->has_selected_items()) { |
| 1220 | confirm_closing_dialog->set_text(TTR("Import Items tab has some items selected. Selection will be lost upon closing this window.\nClose anyway?")); |
| 1221 | confirm_closing_dialog->popup_centered(Size2(380, 120) * EDSCALE); |
| 1222 | return; |
| 1223 | } |
| 1224 | |
| 1225 | hide(); |
| 1226 | } |
| 1227 | |
| 1228 | void ThemeItemEditorDialog::_close_dialog() { |
| 1229 | hide(); |
| 1230 | } |
| 1231 | |
| 1232 | void ThemeItemEditorDialog::_dialog_about_to_show() { |
| 1233 | ERR_FAIL_COND_MSG(edited_theme.is_null(), "Invalid state of the Theme Editor; the Theme resource is missing."); |
| 1234 | |
| 1235 | _update_edit_types(); |
| 1236 | |
| 1237 | import_default_theme_items->set_edited_theme(edited_theme); |
| 1238 | import_default_theme_items->set_base_theme(ThemeDB::get_singleton()->get_default_theme()); |
| 1239 | import_default_theme_items->reset_item_tree(); |
| 1240 | |
| 1241 | import_editor_theme_items->set_edited_theme(edited_theme); |
| 1242 | import_editor_theme_items->set_base_theme(EditorNode::get_singleton()->get_editor_theme()); |
| 1243 | import_editor_theme_items->reset_item_tree(); |
| 1244 | |
| 1245 | import_other_theme_items->set_edited_theme(edited_theme); |
| 1246 | import_other_theme_items->reset_item_tree(); |
| 1247 | } |
| 1248 | |
| 1249 | void ThemeItemEditorDialog::_update_edit_types() { |
| 1250 | Ref<Theme> base_theme = ThemeDB::get_singleton()->get_default_theme(); |
| 1251 | |
| 1252 | List<StringName> theme_types; |
| 1253 | edited_theme->get_type_list(&theme_types); |
| 1254 | theme_types.sort_custom<StringName::AlphCompare>(); |
| 1255 | |
| 1256 | bool item_reselected = false; |
| 1257 | edit_type_list->clear(); |
| 1258 | TreeItem *list_root = edit_type_list->create_item(); |
| 1259 | |
| 1260 | for (const StringName &E : theme_types) { |
| 1261 | Ref<Texture2D> item_icon; |
| 1262 | if (E == "") { |
| 1263 | item_icon = get_editor_theme_icon(SNAME("NodeDisabled")); |
| 1264 | } else { |
| 1265 | item_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled"); |
| 1266 | } |
| 1267 | TreeItem *list_item = edit_type_list->create_item(list_root); |
| 1268 | list_item->set_text(0, E); |
| 1269 | list_item->set_metadata(0, E); |
| 1270 | list_item->set_editable(0, true); |
| 1271 | list_item->set_icon(0, item_icon); |
| 1272 | list_item->add_button(0, get_editor_theme_icon(SNAME("Remove")), TYPES_TREE_REMOVE_ITEM, false, TTRC("Remove Type")); |
| 1273 | |
| 1274 | if (E == edited_item_type) { |
| 1275 | list_item->select(0); |
| 1276 | item_reselected = true; |
| 1277 | } |
| 1278 | } |
| 1279 | if (!item_reselected) { |
| 1280 | edited_item_type = ""; |
| 1281 | |
| 1282 | if (list_root->get_child_count() > 0) { |
| 1283 | list_root->get_child(0)->select(0); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | List<StringName> default_types; |
| 1288 | base_theme->get_type_list(&default_types); |
| 1289 | default_types.sort_custom<StringName::AlphCompare>(); |
| 1290 | |
| 1291 | String selected_type = ""; |
| 1292 | TreeItem *selected_item = edit_type_list->get_selected(); |
| 1293 | if (selected_item) { |
| 1294 | selected_type = selected_item->get_text(0); |
| 1295 | |
| 1296 | edit_items_add_color->set_disabled(false); |
| 1297 | edit_items_add_constant->set_disabled(false); |
| 1298 | edit_items_add_font->set_disabled(false); |
| 1299 | edit_items_add_font_size->set_disabled(false); |
| 1300 | edit_items_add_icon->set_disabled(false); |
| 1301 | edit_items_add_stylebox->set_disabled(false); |
| 1302 | |
| 1303 | edit_items_remove_class->set_disabled(false); |
| 1304 | edit_items_remove_custom->set_disabled(false); |
| 1305 | edit_items_remove_all->set_disabled(false); |
| 1306 | |
| 1307 | edit_items_message->set_text(""); |
| 1308 | edit_items_message->hide(); |
| 1309 | } else { |
| 1310 | edit_items_add_color->set_disabled(true); |
| 1311 | edit_items_add_constant->set_disabled(true); |
| 1312 | edit_items_add_font->set_disabled(true); |
| 1313 | edit_items_add_font_size->set_disabled(true); |
| 1314 | edit_items_add_icon->set_disabled(true); |
| 1315 | edit_items_add_stylebox->set_disabled(true); |
| 1316 | |
| 1317 | edit_items_remove_class->set_disabled(true); |
| 1318 | edit_items_remove_custom->set_disabled(true); |
| 1319 | edit_items_remove_all->set_disabled(true); |
| 1320 | |
| 1321 | edit_items_message->set_text(TTR("Select a theme type from the list to edit its items.\nYou can add a custom type or import a type with its items from another theme.")); |
| 1322 | edit_items_message->show(); |
| 1323 | } |
| 1324 | |
| 1325 | _update_edit_item_tree(selected_type); |
| 1326 | } |
| 1327 | |
| 1328 | void ThemeItemEditorDialog::_edited_type_selected() { |
| 1329 | TreeItem *selected_item = edit_type_list->get_selected(); |
| 1330 | String selected_type = selected_item->get_text(0); |
| 1331 | _update_edit_item_tree(selected_type); |
| 1332 | } |
| 1333 | |
| 1334 | void ThemeItemEditorDialog::_edited_type_edited() { |
| 1335 | TreeItem *edited_item = edit_type_list->get_selected(); |
| 1336 | const String old_type_name = edited_item->get_metadata(0); |
| 1337 | |
| 1338 | const String &new_type_name = Theme::validate_type_name(edited_item->get_text(0)); |
| 1339 | if (old_type_name == new_type_name) { |
| 1340 | edited_item->set_text(0, old_type_name); |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | List<StringName> theme_types; |
| 1345 | edited_theme->get_type_list(&theme_types); |
| 1346 | if (theme_types.find(new_type_name) != nullptr) { |
| 1347 | edited_item->set_text(0, old_type_name); |
| 1348 | return; |
| 1349 | } |
| 1350 | |
| 1351 | // The list will be recreated, but let's update the item just in case. |
| 1352 | edited_item->set_metadata(0, new_type_name); |
| 1353 | edited_item->set_text(0, new_type_name); |
| 1354 | |
| 1355 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1356 | ur->create_action(TTR("Rename Theme Type")); |
| 1357 | |
| 1358 | _rename_theme_type(ur, *edited_theme, old_type_name, new_type_name); |
| 1359 | |
| 1360 | // Set `edited_item_type`. |
| 1361 | ur->add_do_method(this, "_update_edit_item_tree", new_type_name); |
| 1362 | ur->add_undo_method(this, "_update_edit_item_tree", old_type_name); |
| 1363 | |
| 1364 | ur->add_do_method(this, "_update_edit_types"); |
| 1365 | ur->add_undo_method(this, "_update_edit_types"); |
| 1366 | |
| 1367 | ur->commit_action(); |
| 1368 | } |
| 1369 | |
| 1370 | void ThemeItemEditorDialog::_edited_type_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) { |
| 1371 | if (p_button != MouseButton::LEFT) { |
| 1372 | return; |
| 1373 | } |
| 1374 | |
| 1375 | TreeItem *item = Object::cast_to<TreeItem>(p_item); |
| 1376 | if (!item) { |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | switch (p_id) { |
| 1381 | case TYPES_TREE_REMOVE_ITEM: { |
| 1382 | String type_name = item->get_text(0); |
| 1383 | _remove_theme_type(type_name); |
| 1384 | } break; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | void ThemeItemEditorDialog::_update_edit_item_tree(String p_item_type) { |
| 1389 | edited_item_type = p_item_type; |
| 1390 | |
| 1391 | edit_items_tree->clear(); |
| 1392 | TreeItem *root = edit_items_tree->create_item(); |
| 1393 | |
| 1394 | List<StringName> names; |
| 1395 | bool has_any_items = false; |
| 1396 | |
| 1397 | { // Colors. |
| 1398 | names.clear(); |
| 1399 | edited_theme->get_color_list(p_item_type, &names); |
| 1400 | |
| 1401 | if (names.size() > 0) { |
| 1402 | TreeItem *color_root = edit_items_tree->create_item(root); |
| 1403 | color_root->set_metadata(0, Theme::DATA_TYPE_COLOR); |
| 1404 | color_root->set_icon(0, get_editor_theme_icon(SNAME("Color"))); |
| 1405 | color_root->set_text(0, TTR("Colors")); |
| 1406 | color_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Color Items")); |
| 1407 | |
| 1408 | names.sort_custom<StringName::AlphCompare>(); |
| 1409 | for (const StringName &E : names) { |
| 1410 | TreeItem *item = edit_items_tree->create_item(color_root); |
| 1411 | item->set_text(0, E); |
| 1412 | item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item")); |
| 1413 | item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item")); |
| 1414 | } |
| 1415 | |
| 1416 | has_any_items = true; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | { // Constants. |
| 1421 | names.clear(); |
| 1422 | edited_theme->get_constant_list(p_item_type, &names); |
| 1423 | |
| 1424 | if (names.size() > 0) { |
| 1425 | TreeItem *constant_root = edit_items_tree->create_item(root); |
| 1426 | constant_root->set_metadata(0, Theme::DATA_TYPE_CONSTANT); |
| 1427 | constant_root->set_icon(0, get_editor_theme_icon(SNAME("MemberConstant"))); |
| 1428 | constant_root->set_text(0, TTR("Constants")); |
| 1429 | constant_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Constant Items")); |
| 1430 | |
| 1431 | names.sort_custom<StringName::AlphCompare>(); |
| 1432 | for (const StringName &E : names) { |
| 1433 | TreeItem *item = edit_items_tree->create_item(constant_root); |
| 1434 | item->set_text(0, E); |
| 1435 | item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item")); |
| 1436 | item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item")); |
| 1437 | } |
| 1438 | |
| 1439 | has_any_items = true; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | { // Fonts. |
| 1444 | names.clear(); |
| 1445 | edited_theme->get_font_list(p_item_type, &names); |
| 1446 | |
| 1447 | if (names.size() > 0) { |
| 1448 | TreeItem *font_root = edit_items_tree->create_item(root); |
| 1449 | font_root->set_metadata(0, Theme::DATA_TYPE_FONT); |
| 1450 | font_root->set_icon(0, get_editor_theme_icon(SNAME("FontItem"))); |
| 1451 | font_root->set_text(0, TTR("Fonts")); |
| 1452 | font_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Font Items")); |
| 1453 | |
| 1454 | names.sort_custom<StringName::AlphCompare>(); |
| 1455 | for (const StringName &E : names) { |
| 1456 | TreeItem *item = edit_items_tree->create_item(font_root); |
| 1457 | item->set_text(0, E); |
| 1458 | item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item")); |
| 1459 | item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item")); |
| 1460 | } |
| 1461 | |
| 1462 | has_any_items = true; |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | { // Font sizes. |
| 1467 | names.clear(); |
| 1468 | edited_theme->get_font_size_list(p_item_type, &names); |
| 1469 | |
| 1470 | if (names.size() > 0) { |
| 1471 | TreeItem *font_size_root = edit_items_tree->create_item(root); |
| 1472 | font_size_root->set_metadata(0, Theme::DATA_TYPE_FONT_SIZE); |
| 1473 | font_size_root->set_icon(0, get_editor_theme_icon(SNAME("FontSize"))); |
| 1474 | font_size_root->set_text(0, TTR("Font Sizes")); |
| 1475 | font_size_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Font Size Items")); |
| 1476 | |
| 1477 | names.sort_custom<StringName::AlphCompare>(); |
| 1478 | for (const StringName &E : names) { |
| 1479 | TreeItem *item = edit_items_tree->create_item(font_size_root); |
| 1480 | item->set_text(0, E); |
| 1481 | item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item")); |
| 1482 | item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item")); |
| 1483 | } |
| 1484 | |
| 1485 | has_any_items = true; |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | { // Icons. |
| 1490 | names.clear(); |
| 1491 | edited_theme->get_icon_list(p_item_type, &names); |
| 1492 | |
| 1493 | if (names.size() > 0) { |
| 1494 | TreeItem *icon_root = edit_items_tree->create_item(root); |
| 1495 | icon_root->set_metadata(0, Theme::DATA_TYPE_ICON); |
| 1496 | icon_root->set_icon(0, get_editor_theme_icon(SNAME("ImageTexture"))); |
| 1497 | icon_root->set_text(0, TTR("Icons")); |
| 1498 | icon_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All Icon Items")); |
| 1499 | |
| 1500 | names.sort_custom<StringName::AlphCompare>(); |
| 1501 | for (const StringName &E : names) { |
| 1502 | TreeItem *item = edit_items_tree->create_item(icon_root); |
| 1503 | item->set_text(0, E); |
| 1504 | item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item")); |
| 1505 | item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item")); |
| 1506 | } |
| 1507 | |
| 1508 | has_any_items = true; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | { // Styleboxes. |
| 1513 | names.clear(); |
| 1514 | edited_theme->get_stylebox_list(p_item_type, &names); |
| 1515 | |
| 1516 | if (names.size() > 0) { |
| 1517 | TreeItem *stylebox_root = edit_items_tree->create_item(root); |
| 1518 | stylebox_root->set_metadata(0, Theme::DATA_TYPE_STYLEBOX); |
| 1519 | stylebox_root->set_icon(0, get_editor_theme_icon(SNAME("StyleBoxFlat"))); |
| 1520 | stylebox_root->set_text(0, TTR("Styleboxes")); |
| 1521 | stylebox_root->add_button(0, get_editor_theme_icon(SNAME("Clear")), ITEMS_TREE_REMOVE_DATA_TYPE, false, TTR("Remove All StyleBox Items")); |
| 1522 | |
| 1523 | names.sort_custom<StringName::AlphCompare>(); |
| 1524 | for (const StringName &E : names) { |
| 1525 | TreeItem *item = edit_items_tree->create_item(stylebox_root); |
| 1526 | item->set_text(0, E); |
| 1527 | item->add_button(0, get_editor_theme_icon(SNAME("Edit")), ITEMS_TREE_RENAME_ITEM, false, TTR("Rename Item")); |
| 1528 | item->add_button(0, get_editor_theme_icon(SNAME("Remove")), ITEMS_TREE_REMOVE_ITEM, false, TTR("Remove Item")); |
| 1529 | } |
| 1530 | |
| 1531 | has_any_items = true; |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | // If some type is selected, but it doesn't seem to have any items, show a guiding message. |
| 1536 | TreeItem *selected_item = edit_type_list->get_selected(); |
| 1537 | if (selected_item) { |
| 1538 | if (!has_any_items) { |
| 1539 | edit_items_message->set_text(TTR("This theme type is empty.\nAdd more items to it manually or by importing from another theme.")); |
| 1540 | edit_items_message->show(); |
| 1541 | } else { |
| 1542 | edit_items_message->set_text(""); |
| 1543 | edit_items_message->hide(); |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | void ThemeItemEditorDialog::_item_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) { |
| 1549 | if (p_button != MouseButton::LEFT) { |
| 1550 | return; |
| 1551 | } |
| 1552 | |
| 1553 | TreeItem *item = Object::cast_to<TreeItem>(p_item); |
| 1554 | if (!item) { |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | switch (p_id) { |
| 1559 | case ITEMS_TREE_RENAME_ITEM: { |
| 1560 | String item_name = item->get_text(0); |
| 1561 | int data_type = item->get_parent()->get_metadata(0); |
| 1562 | _open_rename_theme_item_dialog((Theme::DataType)data_type, item_name); |
| 1563 | _update_edit_item_tree(edited_item_type); |
| 1564 | } break; |
| 1565 | case ITEMS_TREE_REMOVE_ITEM: { |
| 1566 | String item_name = item->get_text(0); |
| 1567 | int data_type = item->get_parent()->get_metadata(0); |
| 1568 | |
| 1569 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1570 | ur->create_action(TTR("Remove Theme Item")); |
| 1571 | ur->add_do_method(*edited_theme, "clear_theme_item", (Theme::DataType)data_type, item_name, edited_item_type); |
| 1572 | ur->add_undo_method(*edited_theme, "set_theme_item", (Theme::DataType)data_type, item_name, edited_item_type, edited_theme->get_theme_item((Theme::DataType)data_type, item_name, edited_item_type)); |
| 1573 | ur->add_do_method(this, "_update_edit_item_tree", edited_item_type); |
| 1574 | ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type); |
| 1575 | ur->commit_action(); |
| 1576 | } break; |
| 1577 | case ITEMS_TREE_REMOVE_DATA_TYPE: { |
| 1578 | int data_type = item->get_metadata(0); |
| 1579 | _remove_data_type_items((Theme::DataType)data_type, edited_item_type); |
| 1580 | } break; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | void ThemeItemEditorDialog::_add_theme_type() { |
| 1585 | const String &new_type_name = Theme::validate_type_name(edit_add_type_value->get_text()); |
| 1586 | edit_add_type_value->clear(); |
| 1587 | |
| 1588 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1589 | ur->create_action(TTR("Add Theme Type")); |
| 1590 | |
| 1591 | ur->add_do_method(*edited_theme, "add_type", new_type_name); |
| 1592 | ur->add_undo_method(*edited_theme, "remove_type", new_type_name); |
| 1593 | |
| 1594 | ur->add_do_method(this, "_update_edit_types"); |
| 1595 | ur->add_undo_method(this, "_update_edit_types"); |
| 1596 | |
| 1597 | ur->commit_action(); |
| 1598 | } |
| 1599 | |
| 1600 | void ThemeItemEditorDialog::_add_theme_item(Theme::DataType p_data_type, const String &p_item_name, const String &p_item_type) { |
| 1601 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1602 | ur->create_action(TTR("Create Theme Item")); |
| 1603 | |
| 1604 | switch (p_data_type) { |
| 1605 | case Theme::DATA_TYPE_ICON: |
| 1606 | ur->add_do_method(*edited_theme, "set_icon", p_item_name, p_item_type, Ref<Texture2D>()); |
| 1607 | ur->add_undo_method(*edited_theme, "clear_icon", p_item_name, p_item_type); |
| 1608 | break; |
| 1609 | case Theme::DATA_TYPE_STYLEBOX: |
| 1610 | ur->add_do_method(*edited_theme, "set_stylebox", p_item_name, p_item_type, Ref<StyleBox>()); |
| 1611 | ur->add_undo_method(*edited_theme, "clear_stylebox", p_item_name, p_item_type); |
| 1612 | |
| 1613 | if (theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(p_item_name, p_item_type))) { |
| 1614 | ur->add_undo_method(theme_type_editor, "_unpin_leading_stylebox"); |
| 1615 | } |
| 1616 | break; |
| 1617 | case Theme::DATA_TYPE_FONT: |
| 1618 | ur->add_do_method(*edited_theme, "set_font", p_item_name, p_item_type, Ref<Font>()); |
| 1619 | ur->add_undo_method(*edited_theme, "clear_font", p_item_name, p_item_type); |
| 1620 | break; |
| 1621 | case Theme::DATA_TYPE_FONT_SIZE: |
| 1622 | ur->add_do_method(*edited_theme, "set_font_size", p_item_name, p_item_type, -1); |
| 1623 | ur->add_undo_method(*edited_theme, "clear_font_size", p_item_name, p_item_type); |
| 1624 | break; |
| 1625 | case Theme::DATA_TYPE_COLOR: |
| 1626 | ur->add_do_method(*edited_theme, "set_color", p_item_name, p_item_type, Color()); |
| 1627 | ur->add_undo_method(*edited_theme, "clear_color", p_item_name, p_item_type); |
| 1628 | break; |
| 1629 | case Theme::DATA_TYPE_CONSTANT: |
| 1630 | ur->add_do_method(*edited_theme, "set_constant", p_item_name, p_item_type, 0); |
| 1631 | ur->add_undo_method(*edited_theme, "clear_constant", p_item_name, p_item_type); |
| 1632 | break; |
| 1633 | case Theme::DATA_TYPE_MAX: |
| 1634 | break; // Can't happen, but silences warning. |
| 1635 | } |
| 1636 | |
| 1637 | ur->add_do_method(this, "_update_edit_item_tree", edited_item_type); |
| 1638 | ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type); |
| 1639 | ur->commit_action(); |
| 1640 | } |
| 1641 | |
| 1642 | void ThemeItemEditorDialog::_remove_theme_type(const String &p_theme_type) { |
| 1643 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 1644 | |
| 1645 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1646 | ur->create_action(TTR("Remove Theme Type")); |
| 1647 | |
| 1648 | ur->add_do_method(*edited_theme, "remove_type", p_theme_type); |
| 1649 | // If the type was empty, it cannot be restored with merge, but thankfully we can fake it. |
| 1650 | ur->add_undo_method(*edited_theme, "add_type", p_theme_type); |
| 1651 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 1652 | |
| 1653 | ur->add_do_method(this, "_update_edit_types"); |
| 1654 | ur->add_undo_method(this, "_update_edit_types"); |
| 1655 | |
| 1656 | ur->commit_action(); |
| 1657 | } |
| 1658 | |
| 1659 | void ThemeItemEditorDialog::_remove_data_type_items(Theme::DataType p_data_type, String p_item_type) { |
| 1660 | List<StringName> names; |
| 1661 | |
| 1662 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 1663 | Ref<Theme> new_snapshot = edited_theme->duplicate(); |
| 1664 | |
| 1665 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1666 | ur->create_action(TTR("Remove Data Type Items From Theme")); |
| 1667 | |
| 1668 | new_snapshot->get_theme_item_list(p_data_type, p_item_type, &names); |
| 1669 | for (const StringName &E : names) { |
| 1670 | new_snapshot->clear_theme_item(p_data_type, E, edited_item_type); |
| 1671 | |
| 1672 | if (p_data_type == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, p_item_type))) { |
| 1673 | ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox"); |
| 1674 | ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, p_item_type)); |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | ur->add_do_method(*edited_theme, "clear"); |
| 1679 | ur->add_do_method(*edited_theme, "merge_with", new_snapshot); |
| 1680 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 1681 | |
| 1682 | ur->add_do_method(this, "_update_edit_item_tree", edited_item_type); |
| 1683 | ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type); |
| 1684 | |
| 1685 | ur->commit_action(); |
| 1686 | } |
| 1687 | |
| 1688 | void ThemeItemEditorDialog::_remove_class_items() { |
| 1689 | List<StringName> names; |
| 1690 | |
| 1691 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 1692 | Ref<Theme> new_snapshot = edited_theme->duplicate(); |
| 1693 | |
| 1694 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1695 | ur->create_action(TTR("Remove Class Items From Theme")); |
| 1696 | |
| 1697 | for (int dt = 0; dt < Theme::DATA_TYPE_MAX; dt++) { |
| 1698 | Theme::DataType data_type = (Theme::DataType)dt; |
| 1699 | |
| 1700 | names.clear(); |
| 1701 | ThemeDB::get_singleton()->get_default_theme()->get_theme_item_list(data_type, edited_item_type, &names); |
| 1702 | for (const StringName &E : names) { |
| 1703 | if (new_snapshot->has_theme_item_nocheck(data_type, E, edited_item_type)) { |
| 1704 | new_snapshot->clear_theme_item(data_type, E, edited_item_type); |
| 1705 | |
| 1706 | if (dt == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, edited_item_type))) { |
| 1707 | ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox"); |
| 1708 | ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, edited_item_type)); |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | ur->add_do_method(*edited_theme, "clear"); |
| 1715 | ur->add_do_method(*edited_theme, "merge_with", new_snapshot); |
| 1716 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 1717 | |
| 1718 | ur->add_do_method(this, "_update_edit_item_tree", edited_item_type); |
| 1719 | ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type); |
| 1720 | |
| 1721 | ur->commit_action(); |
| 1722 | } |
| 1723 | |
| 1724 | void ThemeItemEditorDialog::_remove_custom_items() { |
| 1725 | List<StringName> names; |
| 1726 | |
| 1727 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 1728 | Ref<Theme> new_snapshot = edited_theme->duplicate(); |
| 1729 | |
| 1730 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1731 | ur->create_action(TTR("Remove Custom Items From Theme")); |
| 1732 | |
| 1733 | for (int dt = 0; dt < Theme::DATA_TYPE_MAX; dt++) { |
| 1734 | Theme::DataType data_type = (Theme::DataType)dt; |
| 1735 | |
| 1736 | names.clear(); |
| 1737 | new_snapshot->get_theme_item_list(data_type, edited_item_type, &names); |
| 1738 | for (const StringName &E : names) { |
| 1739 | if (!ThemeDB::get_singleton()->get_default_theme()->has_theme_item_nocheck(data_type, E, edited_item_type)) { |
| 1740 | new_snapshot->clear_theme_item(data_type, E, edited_item_type); |
| 1741 | |
| 1742 | if (dt == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, edited_item_type))) { |
| 1743 | ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox"); |
| 1744 | ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, edited_item_type)); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | ur->add_do_method(*edited_theme, "clear"); |
| 1751 | ur->add_do_method(*edited_theme, "merge_with", new_snapshot); |
| 1752 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 1753 | |
| 1754 | ur->add_do_method(this, "_update_edit_item_tree", edited_item_type); |
| 1755 | ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type); |
| 1756 | |
| 1757 | ur->commit_action(); |
| 1758 | } |
| 1759 | |
| 1760 | void ThemeItemEditorDialog::_remove_all_items() { |
| 1761 | List<StringName> names; |
| 1762 | |
| 1763 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 1764 | Ref<Theme> new_snapshot = edited_theme->duplicate(); |
| 1765 | |
| 1766 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1767 | ur->create_action(TTR("Remove All Items From Theme")); |
| 1768 | |
| 1769 | for (int dt = 0; dt < Theme::DATA_TYPE_MAX; dt++) { |
| 1770 | Theme::DataType data_type = (Theme::DataType)dt; |
| 1771 | |
| 1772 | names.clear(); |
| 1773 | new_snapshot->get_theme_item_list(data_type, edited_item_type, &names); |
| 1774 | for (const StringName &E : names) { |
| 1775 | new_snapshot->clear_theme_item(data_type, E, edited_item_type); |
| 1776 | |
| 1777 | if (dt == Theme::DATA_TYPE_STYLEBOX && theme_type_editor->is_stylebox_pinned(edited_theme->get_stylebox(E, edited_item_type))) { |
| 1778 | ur->add_do_method(theme_type_editor, "_unpin_leading_stylebox"); |
| 1779 | ur->add_undo_method(theme_type_editor, "_pin_leading_stylebox", E, edited_theme->get_stylebox(E, edited_item_type)); |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | ur->add_do_method(*edited_theme, "clear"); |
| 1785 | ur->add_do_method(*edited_theme, "merge_with", new_snapshot); |
| 1786 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 1787 | |
| 1788 | ur->add_do_method(this, "_update_edit_item_tree", edited_item_type); |
| 1789 | ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type); |
| 1790 | |
| 1791 | ur->commit_action(); |
| 1792 | } |
| 1793 | |
| 1794 | void ThemeItemEditorDialog::_open_add_theme_item_dialog(int p_data_type) { |
| 1795 | ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds."); |
| 1796 | |
| 1797 | item_popup_mode = CREATE_THEME_ITEM; |
| 1798 | edit_item_data_type = (Theme::DataType)p_data_type; |
| 1799 | |
| 1800 | switch (edit_item_data_type) { |
| 1801 | case Theme::DATA_TYPE_COLOR: |
| 1802 | edit_theme_item_dialog->set_title(TTR("Add Color Item")); |
| 1803 | break; |
| 1804 | case Theme::DATA_TYPE_CONSTANT: |
| 1805 | edit_theme_item_dialog->set_title(TTR("Add Constant Item")); |
| 1806 | break; |
| 1807 | case Theme::DATA_TYPE_FONT: |
| 1808 | edit_theme_item_dialog->set_title(TTR("Add Font Item")); |
| 1809 | break; |
| 1810 | case Theme::DATA_TYPE_FONT_SIZE: |
| 1811 | edit_theme_item_dialog->set_title(TTR("Add Font Size Item")); |
| 1812 | break; |
| 1813 | case Theme::DATA_TYPE_ICON: |
| 1814 | edit_theme_item_dialog->set_title(TTR("Add Icon Item")); |
| 1815 | break; |
| 1816 | case Theme::DATA_TYPE_STYLEBOX: |
| 1817 | edit_theme_item_dialog->set_title(TTR("Add Stylebox Item")); |
| 1818 | break; |
| 1819 | case Theme::DATA_TYPE_MAX: |
| 1820 | break; // Can't happen, but silences warning. |
| 1821 | } |
| 1822 | |
| 1823 | edit_theme_item_old_vb->hide(); |
| 1824 | theme_item_name->clear(); |
| 1825 | edit_theme_item_dialog->popup_centered(Size2(380, 110) * EDSCALE); |
| 1826 | theme_item_name->grab_focus(); |
| 1827 | } |
| 1828 | |
| 1829 | void ThemeItemEditorDialog::_open_rename_theme_item_dialog(Theme::DataType p_data_type, String p_item_name) { |
| 1830 | ERR_FAIL_INDEX_MSG(p_data_type, Theme::DATA_TYPE_MAX, "Theme item data type is out of bounds."); |
| 1831 | |
| 1832 | item_popup_mode = RENAME_THEME_ITEM; |
| 1833 | edit_item_data_type = p_data_type; |
| 1834 | edit_item_old_name = p_item_name; |
| 1835 | |
| 1836 | switch (edit_item_data_type) { |
| 1837 | case Theme::DATA_TYPE_COLOR: |
| 1838 | edit_theme_item_dialog->set_title(TTR("Rename Color Item")); |
| 1839 | break; |
| 1840 | case Theme::DATA_TYPE_CONSTANT: |
| 1841 | edit_theme_item_dialog->set_title(TTR("Rename Constant Item")); |
| 1842 | break; |
| 1843 | case Theme::DATA_TYPE_FONT: |
| 1844 | edit_theme_item_dialog->set_title(TTR("Rename Font Item")); |
| 1845 | break; |
| 1846 | case Theme::DATA_TYPE_FONT_SIZE: |
| 1847 | edit_theme_item_dialog->set_title(TTR("Rename Font Size Item")); |
| 1848 | break; |
| 1849 | case Theme::DATA_TYPE_ICON: |
| 1850 | edit_theme_item_dialog->set_title(TTR("Rename Icon Item")); |
| 1851 | break; |
| 1852 | case Theme::DATA_TYPE_STYLEBOX: |
| 1853 | edit_theme_item_dialog->set_title(TTR("Rename Stylebox Item")); |
| 1854 | break; |
| 1855 | case Theme::DATA_TYPE_MAX: |
| 1856 | break; // Can't happen, but silences warning. |
| 1857 | } |
| 1858 | |
| 1859 | edit_theme_item_old_vb->show(); |
| 1860 | theme_item_old_name->set_text(p_item_name); |
| 1861 | theme_item_name->set_text(p_item_name); |
| 1862 | edit_theme_item_dialog->popup_centered(Size2(380, 140) * EDSCALE); |
| 1863 | theme_item_name->grab_focus(); |
| 1864 | } |
| 1865 | |
| 1866 | void ThemeItemEditorDialog::_confirm_edit_theme_item() { |
| 1867 | const String new_item_name = theme_item_name->get_text().strip_edges().validate_ascii_identifier(); |
| 1868 | |
| 1869 | if (item_popup_mode == CREATE_THEME_ITEM) { |
| 1870 | _add_theme_item(edit_item_data_type, new_item_name, edited_item_type); |
| 1871 | } else if (item_popup_mode == RENAME_THEME_ITEM) { |
| 1872 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 1873 | ur->create_action(TTR("Rename Theme Item")); |
| 1874 | |
| 1875 | ur->add_do_method(*edited_theme, "rename_theme_item", edit_item_data_type, edit_item_old_name, new_item_name, edited_item_type); |
| 1876 | ur->add_undo_method(*edited_theme, "rename_theme_item", edit_item_data_type, new_item_name, edit_item_old_name, edited_item_type); |
| 1877 | |
| 1878 | ur->add_do_method(this, "_update_edit_item_tree", edited_item_type); |
| 1879 | ur->add_undo_method(this, "_update_edit_item_tree", edited_item_type); |
| 1880 | |
| 1881 | ur->commit_action(); |
| 1882 | } |
| 1883 | |
| 1884 | item_popup_mode = ITEM_POPUP_MODE_MAX; |
| 1885 | edit_item_data_type = Theme::DATA_TYPE_MAX; |
| 1886 | edit_item_old_name = ""; |
| 1887 | } |
| 1888 | |
| 1889 | void ThemeItemEditorDialog::_edit_theme_item_gui_input(const Ref<InputEvent> &p_event) { |
| 1890 | Ref<InputEventKey> k = p_event; |
| 1891 | |
| 1892 | if (k.is_valid()) { |
| 1893 | if (!k->is_pressed()) { |
| 1894 | return; |
| 1895 | } |
| 1896 | |
| 1897 | if (k->is_action_pressed(SNAME("ui_text_submit"), false, true)) { |
| 1898 | _confirm_edit_theme_item(); |
| 1899 | edit_theme_item_dialog->hide(); |
| 1900 | edit_theme_item_dialog->set_input_as_handled(); |
| 1901 | } else if (k->is_action_pressed(SNAME("ui_cancel"), false, true)) { |
| 1902 | edit_theme_item_dialog->hide(); |
| 1903 | edit_theme_item_dialog->set_input_as_handled(); |
| 1904 | } |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | void ThemeItemEditorDialog::_open_select_another_theme() { |
| 1909 | import_another_theme_dialog->popup_file_dialog(); |
| 1910 | } |
| 1911 | |
| 1912 | void ThemeItemEditorDialog::_select_another_theme_cbk(const String &p_path) { |
| 1913 | Ref<Theme> loaded_theme = ResourceLoader::load(p_path); |
| 1914 | if (loaded_theme.is_null()) { |
| 1915 | EditorNode::get_singleton()->show_warning(TTR("Invalid file, not a Theme resource.")); |
| 1916 | return; |
| 1917 | } |
| 1918 | if (loaded_theme == edited_theme) { |
| 1919 | EditorNode::get_singleton()->show_warning(TTR("Invalid file, same as the edited Theme resource.")); |
| 1920 | return; |
| 1921 | } |
| 1922 | |
| 1923 | import_another_theme_value->set_text(p_path); |
| 1924 | import_other_theme_items->set_base_theme(loaded_theme); |
| 1925 | import_other_theme_items->reset_item_tree(); |
| 1926 | } |
| 1927 | |
| 1928 | void ThemeItemEditorDialog::_notification(int p_what) { |
| 1929 | switch (p_what) { |
| 1930 | case NOTIFICATION_POSTINITIALIZE: { |
| 1931 | connect("about_to_popup", callable_mp(this, &ThemeItemEditorDialog::_dialog_about_to_show)); |
| 1932 | } break; |
| 1933 | |
| 1934 | case NOTIFICATION_THEME_CHANGED: { |
| 1935 | edit_items_add_color->set_button_icon(get_editor_theme_icon(SNAME("Color"))); |
| 1936 | edit_items_add_constant->set_button_icon(get_editor_theme_icon(SNAME("MemberConstant"))); |
| 1937 | edit_items_add_font->set_button_icon(get_editor_theme_icon(SNAME("FontItem"))); |
| 1938 | edit_items_add_font_size->set_button_icon(get_editor_theme_icon(SNAME("FontSize"))); |
| 1939 | edit_items_add_icon->set_button_icon(get_editor_theme_icon(SNAME("ImageTexture"))); |
| 1940 | edit_items_add_stylebox->set_button_icon(get_editor_theme_icon(SNAME("StyleBoxFlat"))); |
| 1941 | |
| 1942 | edit_items_remove_class->set_button_icon(get_editor_theme_icon(SNAME("Control"))); |
| 1943 | edit_items_remove_custom->set_button_icon(get_editor_theme_icon(SNAME("ThemeRemoveCustomItems"))); |
| 1944 | edit_items_remove_all->set_button_icon(get_editor_theme_icon(SNAME("ThemeRemoveAllItems"))); |
| 1945 | |
| 1946 | edit_add_type_button->set_button_icon(get_editor_theme_icon(SNAME("Add"))); |
| 1947 | |
| 1948 | import_another_theme_button->set_button_icon(get_editor_theme_icon(SNAME("Folder"))); |
| 1949 | } break; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | void ThemeItemEditorDialog::_bind_methods() { |
| 1954 | ClassDB::bind_method(D_METHOD("_update_edit_types"), &ThemeItemEditorDialog::_update_edit_types); |
| 1955 | ClassDB::bind_method(D_METHOD("_update_edit_item_tree"), &ThemeItemEditorDialog::_update_edit_item_tree); |
| 1956 | } |
| 1957 | |
| 1958 | void ThemeItemEditorDialog::set_edited_theme(const Ref<Theme> &p_theme) { |
| 1959 | edited_theme = p_theme; |
| 1960 | } |
| 1961 | |
| 1962 | ThemeItemEditorDialog::ThemeItemEditorDialog(ThemeTypeEditor *p_theme_type_editor) { |
| 1963 | set_title(TTR("Manage Theme Items")); |
| 1964 | set_ok_button_text(TTR("Close")); |
| 1965 | set_hide_on_ok(false); // Closing may require a confirmation in some cases. |
| 1966 | |
| 1967 | theme_type_editor = p_theme_type_editor; |
| 1968 | |
| 1969 | tc = memnew(TabContainer); |
| 1970 | add_child(tc); |
| 1971 | tc->set_theme_type_variation("TabContainerOdd"); |
| 1972 | |
| 1973 | // Edit Items tab. |
| 1974 | HSplitContainer *edit_dialog_hs = memnew(HSplitContainer); |
| 1975 | tc->add_child(edit_dialog_hs); |
| 1976 | tc->set_tab_title(0, TTR("Edit Items")); |
| 1977 | |
| 1978 | VBoxContainer *edit_dialog_side_vb = memnew(VBoxContainer); |
| 1979 | edit_dialog_side_vb->set_custom_minimum_size(Size2(200.0, 0.0) * EDSCALE); |
| 1980 | edit_dialog_hs->add_child(edit_dialog_side_vb); |
| 1981 | |
| 1982 | Label *edit_type_label = memnew(Label); |
| 1983 | edit_type_label->set_text(TTR("Types:")); |
| 1984 | edit_dialog_side_vb->add_child(edit_type_label); |
| 1985 | |
| 1986 | edit_type_list = memnew(Tree); |
| 1987 | edit_type_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 1988 | edit_type_list->set_hide_root(true); |
| 1989 | edit_type_list->set_hide_folding(true); |
| 1990 | edit_type_list->set_columns(1); |
| 1991 | edit_type_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 1992 | edit_dialog_side_vb->add_child(edit_type_list); |
| 1993 | edit_type_list->connect(SceneStringName(item_selected), callable_mp(this, &ThemeItemEditorDialog::_edited_type_selected)); |
| 1994 | edit_type_list->connect("item_edited", callable_mp(this, &ThemeItemEditorDialog::_edited_type_edited)); |
| 1995 | edit_type_list->connect("button_clicked", callable_mp(this, &ThemeItemEditorDialog::_edited_type_button_pressed)); |
| 1996 | edit_type_list->set_theme_type_variation("TreeSecondary"); |
| 1997 | |
| 1998 | Label *edit_add_type_label = memnew(Label); |
| 1999 | edit_add_type_label->set_text(TTR("Add Type:")); |
| 2000 | edit_dialog_side_vb->add_child(edit_add_type_label); |
| 2001 | |
| 2002 | HBoxContainer *edit_add_type_hb = memnew(HBoxContainer); |
| 2003 | edit_dialog_side_vb->add_child(edit_add_type_hb); |
| 2004 | edit_add_type_value = memnew(LineEdit); |
| 2005 | edit_add_type_value->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2006 | edit_add_type_value->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeItemEditorDialog::_add_theme_type).unbind(1)); |
| 2007 | edit_add_type_hb->add_child(edit_add_type_value); |
| 2008 | edit_add_type_button = memnew(Button); |
| 2009 | edit_add_type_hb->add_child(edit_add_type_button); |
| 2010 | edit_add_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_add_theme_type)); |
| 2011 | |
| 2012 | VBoxContainer *edit_items_vb = memnew(VBoxContainer); |
| 2013 | edit_items_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2014 | edit_dialog_hs->add_child(edit_items_vb); |
| 2015 | |
| 2016 | HBoxContainer *edit_items_toolbar = memnew(HBoxContainer); |
| 2017 | edit_items_vb->add_child(edit_items_toolbar); |
| 2018 | |
| 2019 | Label *edit_items_toolbar_add_label = memnew(Label); |
| 2020 | edit_items_toolbar_add_label->set_text(TTR("Add Item:")); |
| 2021 | edit_items_toolbar->add_child(edit_items_toolbar_add_label); |
| 2022 | |
| 2023 | edit_items_add_color = memnew(Button); |
| 2024 | edit_items_add_color->set_tooltip_text(TTR("Add Color Item")); |
| 2025 | edit_items_add_color->set_flat(true); |
| 2026 | edit_items_add_color->set_disabled(true); |
| 2027 | edit_items_toolbar->add_child(edit_items_add_color); |
| 2028 | edit_items_add_color->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_COLOR)); |
| 2029 | |
| 2030 | edit_items_add_constant = memnew(Button); |
| 2031 | edit_items_add_constant->set_tooltip_text(TTR("Add Constant Item")); |
| 2032 | edit_items_add_constant->set_flat(true); |
| 2033 | edit_items_add_constant->set_disabled(true); |
| 2034 | edit_items_toolbar->add_child(edit_items_add_constant); |
| 2035 | edit_items_add_constant->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_CONSTANT)); |
| 2036 | |
| 2037 | edit_items_add_font = memnew(Button); |
| 2038 | edit_items_add_font->set_tooltip_text(TTR("Add Font Item")); |
| 2039 | edit_items_add_font->set_flat(true); |
| 2040 | edit_items_add_font->set_disabled(true); |
| 2041 | edit_items_toolbar->add_child(edit_items_add_font); |
| 2042 | edit_items_add_font->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_FONT)); |
| 2043 | |
| 2044 | edit_items_add_font_size = memnew(Button); |
| 2045 | edit_items_add_font_size->set_tooltip_text(TTR("Add Font Size Item")); |
| 2046 | edit_items_add_font_size->set_flat(true); |
| 2047 | edit_items_add_font_size->set_disabled(true); |
| 2048 | edit_items_toolbar->add_child(edit_items_add_font_size); |
| 2049 | edit_items_add_font_size->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_FONT_SIZE)); |
| 2050 | |
| 2051 | edit_items_add_icon = memnew(Button); |
| 2052 | edit_items_add_icon->set_tooltip_text(TTR("Add Icon Item")); |
| 2053 | edit_items_add_icon->set_flat(true); |
| 2054 | edit_items_add_icon->set_disabled(true); |
| 2055 | edit_items_toolbar->add_child(edit_items_add_icon); |
| 2056 | edit_items_add_icon->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_ICON)); |
| 2057 | |
| 2058 | edit_items_add_stylebox = memnew(Button); |
| 2059 | edit_items_add_stylebox->set_tooltip_text(TTR("Add StyleBox Item")); |
| 2060 | edit_items_add_stylebox->set_flat(true); |
| 2061 | edit_items_add_stylebox->set_disabled(true); |
| 2062 | edit_items_toolbar->add_child(edit_items_add_stylebox); |
| 2063 | edit_items_add_stylebox->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_add_theme_item_dialog).bind(Theme::DATA_TYPE_STYLEBOX)); |
| 2064 | |
| 2065 | edit_items_toolbar->add_child(memnew(VSeparator)); |
| 2066 | |
| 2067 | Label *edit_items_toolbar_remove_label = memnew(Label); |
| 2068 | edit_items_toolbar_remove_label->set_text(TTR("Remove Items:")); |
| 2069 | edit_items_toolbar->add_child(edit_items_toolbar_remove_label); |
| 2070 | |
| 2071 | edit_items_remove_class = memnew(Button); |
| 2072 | edit_items_remove_class->set_tooltip_text(TTR("Remove Class Items")); |
| 2073 | edit_items_remove_class->set_flat(true); |
| 2074 | edit_items_remove_class->set_disabled(true); |
| 2075 | edit_items_toolbar->add_child(edit_items_remove_class); |
| 2076 | edit_items_remove_class->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_remove_class_items)); |
| 2077 | |
| 2078 | edit_items_remove_custom = memnew(Button); |
| 2079 | edit_items_remove_custom->set_tooltip_text(TTR("Remove Custom Items")); |
| 2080 | edit_items_remove_custom->set_flat(true); |
| 2081 | edit_items_remove_custom->set_disabled(true); |
| 2082 | edit_items_toolbar->add_child(edit_items_remove_custom); |
| 2083 | edit_items_remove_custom->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_remove_custom_items)); |
| 2084 | |
| 2085 | edit_items_remove_all = memnew(Button); |
| 2086 | edit_items_remove_all->set_tooltip_text(TTR("Remove All Items")); |
| 2087 | edit_items_remove_all->set_flat(true); |
| 2088 | edit_items_remove_all->set_disabled(true); |
| 2089 | edit_items_toolbar->add_child(edit_items_remove_all); |
| 2090 | edit_items_remove_all->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_remove_all_items)); |
| 2091 | |
| 2092 | edit_items_tree = memnew(Tree); |
| 2093 | edit_items_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2094 | edit_items_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2095 | edit_items_tree->set_hide_root(true); |
| 2096 | edit_items_tree->set_columns(1); |
| 2097 | edit_items_tree->set_theme_type_variation("TreeSecondary"); |
| 2098 | edit_items_vb->add_child(edit_items_tree); |
| 2099 | edit_items_tree->connect("button_clicked", callable_mp(this, &ThemeItemEditorDialog::_item_tree_button_pressed)); |
| 2100 | |
| 2101 | edit_items_message = memnew(Label); |
| 2102 | edit_items_message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 2103 | edit_items_message->set_mouse_filter(Control::MOUSE_FILTER_STOP); |
| 2104 | edit_items_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2105 | edit_items_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 2106 | edit_items_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD); |
| 2107 | edit_items_tree->add_child(edit_items_message); |
| 2108 | |
| 2109 | edit_theme_item_dialog = memnew(ConfirmationDialog); |
| 2110 | edit_theme_item_dialog->set_title(TTR("Add Theme Item")); |
| 2111 | add_child(edit_theme_item_dialog); |
| 2112 | VBoxContainer *edit_theme_item_vb = memnew(VBoxContainer); |
| 2113 | edit_theme_item_dialog->add_child(edit_theme_item_vb); |
| 2114 | |
| 2115 | edit_theme_item_old_vb = memnew(VBoxContainer); |
| 2116 | edit_theme_item_vb->add_child(edit_theme_item_old_vb); |
| 2117 | Label *edit_theme_item_old = memnew(Label); |
| 2118 | edit_theme_item_old->set_text(TTR("Old Name:")); |
| 2119 | edit_theme_item_old_vb->add_child(edit_theme_item_old); |
| 2120 | theme_item_old_name = memnew(Label); |
| 2121 | edit_theme_item_old_vb->add_child(theme_item_old_name); |
| 2122 | |
| 2123 | Label *edit_theme_item_label = memnew(Label); |
| 2124 | edit_theme_item_label->set_text(TTR("Name:")); |
| 2125 | edit_theme_item_vb->add_child(edit_theme_item_label); |
| 2126 | theme_item_name = memnew(LineEdit); |
| 2127 | edit_theme_item_vb->add_child(theme_item_name); |
| 2128 | theme_item_name->connect(SceneStringName(gui_input), callable_mp(this, &ThemeItemEditorDialog::_edit_theme_item_gui_input)); |
| 2129 | edit_theme_item_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ThemeItemEditorDialog::_confirm_edit_theme_item)); |
| 2130 | |
| 2131 | // Import Items tab. |
| 2132 | TabContainer *import_tc = memnew(TabContainer); |
| 2133 | import_tc->set_theme_type_variation("TabContainerInner"); |
| 2134 | import_tc->set_tab_alignment(TabBar::ALIGNMENT_CENTER); |
| 2135 | tc->add_child(import_tc); |
| 2136 | tc->set_tab_title(1, TTR("Import Items")); |
| 2137 | |
| 2138 | import_default_theme_items = memnew(ThemeItemImportTree); |
| 2139 | import_tc->add_child(import_default_theme_items); |
| 2140 | import_tc->set_tab_title(0, TTR("Default Theme")); |
| 2141 | import_default_theme_items->connect("items_imported", callable_mp(this, &ThemeItemEditorDialog::_update_edit_types)); |
| 2142 | |
| 2143 | import_editor_theme_items = memnew(ThemeItemImportTree); |
| 2144 | import_tc->add_child(import_editor_theme_items); |
| 2145 | import_tc->set_tab_title(1, TTR("Editor Theme")); |
| 2146 | import_editor_theme_items->connect("items_imported", callable_mp(this, &ThemeItemEditorDialog::_update_edit_types)); |
| 2147 | |
| 2148 | VBoxContainer *import_another_theme_vb = memnew(VBoxContainer); |
| 2149 | |
| 2150 | HBoxContainer *import_another_file_hb = memnew(HBoxContainer); |
| 2151 | import_another_theme_vb->add_child(import_another_file_hb); |
| 2152 | import_another_theme_value = memnew(LineEdit); |
| 2153 | import_another_theme_value->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2154 | import_another_theme_value->set_editable(false); |
| 2155 | import_another_file_hb->add_child(import_another_theme_value); |
| 2156 | import_another_theme_button = memnew(Button); |
| 2157 | import_another_file_hb->add_child(import_another_theme_button); |
| 2158 | import_another_theme_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeItemEditorDialog::_open_select_another_theme)); |
| 2159 | |
| 2160 | import_another_theme_dialog = memnew(EditorFileDialog); |
| 2161 | import_another_theme_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
| 2162 | import_another_theme_dialog->set_title(TTR("Select Another Theme Resource:")); |
| 2163 | List<String> ext; |
| 2164 | ResourceLoader::get_recognized_extensions_for_type("Theme", &ext); |
| 2165 | for (const String &E : ext) { |
| 2166 | import_another_theme_dialog->add_filter("*." + E, TTR("Theme Resource")); |
| 2167 | } |
| 2168 | import_another_file_hb->add_child(import_another_theme_dialog); |
| 2169 | import_another_theme_dialog->connect("file_selected", callable_mp(this, &ThemeItemEditorDialog::_select_another_theme_cbk)); |
| 2170 | |
| 2171 | import_other_theme_items = memnew(ThemeItemImportTree); |
| 2172 | import_other_theme_items->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2173 | import_another_theme_vb->add_child(import_other_theme_items); |
| 2174 | |
| 2175 | import_tc->add_child(import_another_theme_vb); |
| 2176 | import_tc->set_tab_title(2, TTR("Another Theme")); |
| 2177 | import_other_theme_items->connect("items_imported", callable_mp(this, &ThemeItemEditorDialog::_update_edit_types)); |
| 2178 | |
| 2179 | confirm_closing_dialog = memnew(ConfirmationDialog); |
| 2180 | confirm_closing_dialog->set_autowrap(true); |
| 2181 | add_child(confirm_closing_dialog); |
| 2182 | confirm_closing_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ThemeItemEditorDialog::_close_dialog)); |
| 2183 | } |
| 2184 | |
| 2185 | /////////////////////// |
| 2186 | |
| 2187 | void ThemeTypeDialog::_dialog_about_to_show() { |
| 2188 | add_type_filter->set_text(""); |
| 2189 | add_type_filter->grab_focus(); |
| 2190 | |
| 2191 | _update_add_type_options(); |
| 2192 | } |
| 2193 | |
| 2194 | void ThemeTypeDialog::ok_pressed() { |
| 2195 | _add_type_selected(add_type_filter->get_text().strip_edges()); |
| 2196 | } |
| 2197 | |
| 2198 | void ThemeTypeDialog::_update_add_type_options(const String &p_filter) { |
| 2199 | add_type_options->clear(); |
| 2200 | |
| 2201 | List<StringName> names; |
| 2202 | ThemeDB::get_singleton()->get_default_theme()->get_type_list(&names); |
| 2203 | if (include_own_types) { |
| 2204 | edited_theme->get_type_list(&names); |
| 2205 | } |
| 2206 | names.sort_custom<StringName::AlphCompare>(); |
| 2207 | |
| 2208 | Vector<StringName> unique_names; |
| 2209 | for (const StringName &E : names) { |
| 2210 | // Filter out undesired values. |
| 2211 | if (!p_filter.is_subsequence_ofn(String(E))) { |
| 2212 | continue; |
| 2213 | } |
| 2214 | |
| 2215 | // Skip duplicate values. |
| 2216 | if (unique_names.has(E)) { |
| 2217 | continue; |
| 2218 | } |
| 2219 | unique_names.append(E); |
| 2220 | |
| 2221 | Ref<Texture2D> item_icon; |
| 2222 | if (E == "") { |
| 2223 | item_icon = get_editor_theme_icon(SNAME("NodeDisabled")); |
| 2224 | } else { |
| 2225 | item_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled"); |
| 2226 | } |
| 2227 | |
| 2228 | add_type_options->add_item(E, item_icon); |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | void ThemeTypeDialog::_add_type_filter_cbk(const String &p_value) { |
| 2233 | _update_add_type_options(p_value); |
| 2234 | } |
| 2235 | |
| 2236 | void ThemeTypeDialog::_type_filter_input(const Ref<InputEvent> &p_event) { |
| 2237 | // Redirect navigational key events to the item list. |
| 2238 | Ref<InputEventKey> key = p_event; |
| 2239 | if (key.is_valid()) { |
| 2240 | if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) { |
| 2241 | add_type_options->gui_input(key); |
| 2242 | add_type_filter->accept_event(); |
| 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | void ThemeTypeDialog::_add_type_options_cbk(int p_index) { |
| 2248 | add_type_filter->set_text(add_type_options->get_item_text(p_index)); |
| 2249 | add_type_filter->set_caret_column(add_type_filter->get_text().length()); |
| 2250 | } |
| 2251 | |
| 2252 | void ThemeTypeDialog::_add_type_dialog_entered(const String &p_value) { |
| 2253 | _add_type_selected(Theme::validate_type_name(p_value)); |
| 2254 | } |
| 2255 | |
| 2256 | void ThemeTypeDialog::_add_type_dialog_activated(int p_index) { |
| 2257 | _add_type_selected(add_type_options->get_item_text(p_index)); |
| 2258 | } |
| 2259 | |
| 2260 | void ThemeTypeDialog::_add_type_selected(const String &p_type_name) { |
| 2261 | pre_submitted_value = p_type_name; |
| 2262 | if (p_type_name.is_empty()) { |
| 2263 | add_type_confirmation->popup_centered(); |
| 2264 | return; |
| 2265 | } |
| 2266 | |
| 2267 | _add_type_confirmed(); |
| 2268 | } |
| 2269 | |
| 2270 | void ThemeTypeDialog::_add_type_confirmed() { |
| 2271 | emit_signal(SNAME("type_selected"), pre_submitted_value); |
| 2272 | hide(); |
| 2273 | } |
| 2274 | |
| 2275 | void ThemeTypeDialog::_notification(int p_what) { |
| 2276 | switch (p_what) { |
| 2277 | case NOTIFICATION_POSTINITIALIZE: { |
| 2278 | connect("about_to_popup", callable_mp(this, &ThemeTypeDialog::_dialog_about_to_show)); |
| 2279 | } break; |
| 2280 | |
| 2281 | case NOTIFICATION_THEME_CHANGED: { |
| 2282 | _update_add_type_options(); |
| 2283 | } break; |
| 2284 | |
| 2285 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 2286 | if (is_visible()) { |
| 2287 | add_type_filter->grab_focus(); |
| 2288 | } |
| 2289 | } break; |
| 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | void ThemeTypeDialog::_bind_methods() { |
| 2294 | ADD_SIGNAL(MethodInfo("type_selected", PropertyInfo(Variant::STRING, "type_name"))); |
| 2295 | } |
| 2296 | |
| 2297 | void ThemeTypeDialog::set_edited_theme(const Ref<Theme> &p_theme) { |
| 2298 | edited_theme = p_theme; |
| 2299 | } |
| 2300 | |
| 2301 | void ThemeTypeDialog::set_include_own_types(bool p_enable) { |
| 2302 | include_own_types = p_enable; |
| 2303 | } |
| 2304 | |
| 2305 | ThemeTypeDialog::ThemeTypeDialog() { |
| 2306 | set_hide_on_ok(false); |
| 2307 | |
| 2308 | VBoxContainer *add_type_vb = memnew(VBoxContainer); |
| 2309 | add_child(add_type_vb); |
| 2310 | |
| 2311 | Label *add_type_filter_label = memnew(Label); |
| 2312 | add_type_filter_label->set_text(TTR("Filter the list of types or create a new custom type:")); |
| 2313 | add_type_vb->add_child(add_type_filter_label); |
| 2314 | |
| 2315 | add_type_filter = memnew(LineEdit); |
| 2316 | add_type_vb->add_child(add_type_filter); |
| 2317 | add_type_filter->connect(SceneStringName(text_changed), callable_mp(this, &ThemeTypeDialog::_add_type_filter_cbk)); |
| 2318 | add_type_filter->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeTypeDialog::_add_type_dialog_entered)); |
| 2319 | add_type_filter->connect(SceneStringName(gui_input), callable_mp(this, &ThemeTypeDialog::_type_filter_input)); |
| 2320 | |
| 2321 | Label *add_type_options_label = memnew(Label); |
| 2322 | add_type_options_label->set_text(TTR("Available Node-based types:")); |
| 2323 | add_type_vb->add_child(add_type_options_label); |
| 2324 | |
| 2325 | MarginContainer *mc = memnew(MarginContainer); |
| 2326 | mc->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2327 | mc->set_theme_type_variation("NoBorderHorizontalWindow"); |
| 2328 | add_type_vb->add_child(mc); |
| 2329 | |
| 2330 | add_type_options = memnew(ItemList); |
| 2331 | add_type_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2332 | add_type_options->set_scroll_hint_mode(ItemList::SCROLL_HINT_MODE_BOTH); |
| 2333 | mc->add_child(add_type_options); |
| 2334 | add_type_options->connect(SceneStringName(item_selected), callable_mp(this, &ThemeTypeDialog::_add_type_options_cbk)); |
| 2335 | add_type_options->connect("item_activated", callable_mp(this, &ThemeTypeDialog::_add_type_dialog_activated)); |
| 2336 | |
| 2337 | add_type_confirmation = memnew(ConfirmationDialog); |
| 2338 | add_type_confirmation->set_title(TTR("Type name is empty!")); |
| 2339 | add_type_confirmation->set_text(TTR("Are you sure you want to create an empty type?")); |
| 2340 | add_type_confirmation->connect(SceneStringName(confirmed), callable_mp(this, &ThemeTypeDialog::_add_type_confirmed)); |
| 2341 | add_child(add_type_confirmation); |
| 2342 | } |
| 2343 | |
| 2344 | /////////////////////// |
| 2345 | |
| 2346 | Control *ThemeItemLabel::make_custom_tooltip(const String &p_text) const { |
| 2347 | return EditorHelpBitTooltip::make_tooltip(const_cast<ThemeItemLabel *>(this), p_text); |
| 2348 | } |
| 2349 | |
| 2350 | VBoxContainer *ThemeTypeEditor::_create_item_list(Theme::DataType p_data_type) { |
| 2351 | VBoxContainer *items_tab = memnew(VBoxContainer); |
| 2352 | items_tab->set_custom_minimum_size(Size2(0, 160) * EDSCALE); |
| 2353 | data_type_tabs->add_child(items_tab); |
| 2354 | data_type_tabs->set_tab_title(data_type_tabs->get_tab_count() - 1, ""); |
| 2355 | |
| 2356 | ScrollContainer *items_sc = memnew(ScrollContainer); |
| 2357 | items_sc->set_v_size_flags(SIZE_EXPAND_FILL); |
| 2358 | items_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 2359 | items_sc->set_theme_type_variation("ScrollContainerSecondary"); |
| 2360 | items_tab->add_child(items_sc); |
| 2361 | VBoxContainer *items_list = memnew(VBoxContainer); |
| 2362 | items_list->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2363 | items_sc->add_child(items_list); |
| 2364 | |
| 2365 | HBoxContainer *item_add_hb = memnew(HBoxContainer); |
| 2366 | items_tab->add_child(item_add_hb); |
| 2367 | LineEdit *item_add_edit = memnew(LineEdit); |
| 2368 | item_add_edit->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2369 | item_add_hb->add_child(item_add_edit); |
| 2370 | item_add_edit->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeTypeEditor::_item_add_lineedit_cbk).bind(p_data_type, item_add_edit)); |
| 2371 | Button *item_add_button = memnew(Button); |
| 2372 | item_add_button->set_text(TTR("Add")); |
| 2373 | item_add_button->set_disabled(true); |
| 2374 | item_add_hb->add_child(item_add_button); |
| 2375 | item_add_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_add_cbk).bind(p_data_type, item_add_edit)); |
| 2376 | item_add_edit->set_meta("button", item_add_button); |
| 2377 | item_add_edit->connect(SceneStringName(text_changed), callable_mp(this, &ThemeTypeEditor::_update_add_button).bind(item_add_edit)); |
| 2378 | |
| 2379 | return items_list; |
| 2380 | } |
| 2381 | |
| 2382 | void ThemeTypeEditor::_update_type_list() { |
| 2383 | ERR_FAIL_COND(edited_theme.is_null()); |
| 2384 | |
| 2385 | if (updating) { |
| 2386 | return; |
| 2387 | } |
| 2388 | updating = true; |
| 2389 | |
| 2390 | Control *focused = get_viewport()->gui_get_focus_owner(); |
| 2391 | if (focused) { |
| 2392 | if (focusables.has(focused)) { |
| 2393 | // If focus is currently on one of the internal property editors, don't update. |
| 2394 | updating = false; |
| 2395 | return; |
| 2396 | } |
| 2397 | |
| 2398 | Node *focus_parent = focused->get_parent(); |
| 2399 | while (focus_parent) { |
| 2400 | Control *c = Object::cast_to<Control>(focus_parent); |
| 2401 | if (c && focusables.has(c)) { |
| 2402 | // If focus is currently on one of the internal property editors, don't update. |
| 2403 | updating = false; |
| 2404 | return; |
| 2405 | } |
| 2406 | |
| 2407 | focus_parent = focus_parent->get_parent(); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | List<StringName> theme_types; |
| 2412 | edited_theme->get_type_list(&theme_types); |
| 2413 | theme_types.sort_custom<StringName::AlphCompare>(); |
| 2414 | |
| 2415 | theme_type_list->clear(); |
| 2416 | |
| 2417 | if (theme_types.is_empty()) { |
| 2418 | theme_type_list->set_disabled(true); |
| 2419 | theme_type_list->add_item(TTRC("None")); |
| 2420 | theme_type_list->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS); |
| 2421 | |
| 2422 | edited_type = ""; |
| 2423 | _update_type_items(); |
| 2424 | } else { |
| 2425 | theme_type_list->set_disabled(false); |
| 2426 | |
| 2427 | bool item_reselected = false; |
| 2428 | int e_idx = 0; |
| 2429 | for (const StringName &E : theme_types) { |
| 2430 | Ref<Texture2D> item_icon; |
| 2431 | if (E == "") { |
| 2432 | item_icon = get_editor_theme_icon(SNAME("NodeDisabled")); |
| 2433 | } else { |
| 2434 | item_icon = EditorNode::get_singleton()->get_class_icon(E, "NodeDisabled"); |
| 2435 | } |
| 2436 | theme_type_list->add_icon_item(item_icon, E); |
| 2437 | |
| 2438 | if (E == edited_type) { |
| 2439 | theme_type_list->select(e_idx); |
| 2440 | item_reselected = true; |
| 2441 | } |
| 2442 | e_idx++; |
| 2443 | } |
| 2444 | |
| 2445 | if (item_reselected) { |
| 2446 | _update_type_items(); |
| 2447 | } else { |
| 2448 | theme_type_list->select(0); |
| 2449 | _list_type_selected(0); |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | rename_type_button->set_disabled(theme_types.is_empty()); |
| 2454 | remove_type_button->set_disabled(theme_types.is_empty()); |
| 2455 | |
| 2456 | updating = false; |
| 2457 | } |
| 2458 | |
| 2459 | void ThemeTypeEditor::_update_type_list_debounced() { |
| 2460 | update_debounce_timer->start(); |
| 2461 | } |
| 2462 | |
| 2463 | HashMap<StringName, bool> ThemeTypeEditor::_get_type_items(String p_type_name, Theme::DataType p_type, bool p_include_default) { |
| 2464 | HashMap<StringName, bool> items; |
| 2465 | List<StringName> names; |
| 2466 | |
| 2467 | if (p_include_default) { |
| 2468 | names.clear(); |
| 2469 | String default_type; |
| 2470 | |
| 2471 | { |
| 2472 | const StringName variation_base = edited_theme->get_type_variation_base(p_type_name); |
| 2473 | if (variation_base != StringName()) { |
| 2474 | default_type = variation_base; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | if (default_type.is_empty()) { |
| 2479 | // If variation base was not found in the edited theme, look in the default theme. |
| 2480 | const StringName variation_base = ThemeDB::get_singleton()->get_default_theme()->get_type_variation_base(p_type_name); |
| 2481 | if (variation_base != StringName()) { |
| 2482 | default_type = variation_base; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | if (default_type.is_empty()) { |
| 2487 | default_type = p_type_name; |
| 2488 | } |
| 2489 | |
| 2490 | List<ThemeDB::ThemeItemBind> theme_binds; |
| 2491 | ThemeDB::get_singleton()->get_class_items(default_type, &theme_binds, true, p_type); |
| 2492 | for (const ThemeDB::ThemeItemBind &E : theme_binds) { |
| 2493 | names.push_back(E.item_name); |
| 2494 | } |
| 2495 | |
| 2496 | names.sort_custom<StringName::AlphCompare>(); |
| 2497 | for (const StringName &E : names) { |
| 2498 | items[E] = false; |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | { |
| 2503 | names.clear(); |
| 2504 | edited_theme->get_theme_item_list(p_type, p_type_name, &names); |
| 2505 | names.sort_custom<StringName::AlphCompare>(); |
| 2506 | for (const StringName &E : names) { |
| 2507 | items[E] = true; |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | List<StringName> keys; |
| 2512 | for (const KeyValue<StringName, bool> &E : items) { |
| 2513 | keys.push_back(E.key); |
| 2514 | } |
| 2515 | keys.sort_custom<StringName::AlphCompare>(); |
| 2516 | |
| 2517 | HashMap<StringName, bool> ordered_items; |
| 2518 | for (const StringName &E : keys) { |
| 2519 | ordered_items[E] = items[E]; |
| 2520 | } |
| 2521 | |
| 2522 | return ordered_items; |
| 2523 | } |
| 2524 | |
| 2525 | HBoxContainer *ThemeTypeEditor::_create_property_control(Theme::DataType p_data_type, String p_item_name, bool p_editable) { |
| 2526 | HBoxContainer *item_control = memnew(HBoxContainer); |
| 2527 | |
| 2528 | HBoxContainer *item_name_container = memnew(HBoxContainer); |
| 2529 | item_name_container->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2530 | item_name_container->set_stretch_ratio(2.0); |
| 2531 | item_control->add_child(item_name_container); |
| 2532 | |
| 2533 | Label *item_name = memnew(ThemeItemLabel); |
| 2534 | item_name->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2535 | item_name->set_clip_text(true); |
| 2536 | item_name->set_text(p_item_name); |
| 2537 | // `|` separators used in `EditorHelpBit`. |
| 2538 | item_name->set_tooltip_text("theme_item|" + edited_type + "|" + p_item_name); |
| 2539 | item_name->set_mouse_filter(Control::MOUSE_FILTER_STOP); |
| 2540 | item_name_container->add_child(item_name); |
| 2541 | |
| 2542 | if (p_editable) { |
| 2543 | LineEdit *item_name_edit = memnew(LineEdit); |
| 2544 | item_name_edit->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2545 | item_name_edit->set_text(p_item_name); |
| 2546 | item_name_container->add_child(item_name_edit); |
| 2547 | item_name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &ThemeTypeEditor::_item_rename_entered).bind(p_data_type, p_item_name, item_name_container)); |
| 2548 | item_name_edit->hide(); |
| 2549 | |
| 2550 | Button *item_rename_button = memnew(Button); |
| 2551 | item_rename_button->set_button_icon(get_editor_theme_icon(SNAME("Edit"))); |
| 2552 | item_rename_button->set_tooltip_text(TTR("Rename Item")); |
| 2553 | item_rename_button->set_flat(true); |
| 2554 | item_name_container->add_child(item_rename_button); |
| 2555 | item_rename_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_rename_cbk).bind(p_data_type, p_item_name, item_name_container)); |
| 2556 | |
| 2557 | Button *item_remove_button = memnew(Button); |
| 2558 | item_remove_button->set_button_icon(get_editor_theme_icon(SNAME("Remove"))); |
| 2559 | item_remove_button->set_tooltip_text(TTR("Remove Item")); |
| 2560 | item_remove_button->set_flat(true); |
| 2561 | item_name_container->add_child(item_remove_button); |
| 2562 | item_remove_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_remove_cbk).bind(p_data_type, p_item_name)); |
| 2563 | |
| 2564 | Button *item_rename_confirm_button = memnew(Button); |
| 2565 | item_rename_confirm_button->set_button_icon(get_editor_theme_icon(SNAME("ImportCheck"))); |
| 2566 | item_rename_confirm_button->set_tooltip_text(TTR("Confirm Item Rename")); |
| 2567 | item_rename_confirm_button->set_flat(true); |
| 2568 | item_name_container->add_child(item_rename_confirm_button); |
| 2569 | item_rename_confirm_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_rename_confirmed).bind(p_data_type, p_item_name, item_name_container)); |
| 2570 | item_rename_confirm_button->hide(); |
| 2571 | |
| 2572 | Button *item_rename_cancel_button = memnew(Button); |
| 2573 | item_rename_cancel_button->set_button_icon(get_editor_theme_icon(SNAME("ImportFail"))); |
| 2574 | item_rename_cancel_button->set_tooltip_text(TTR("Cancel Item Rename")); |
| 2575 | item_rename_cancel_button->set_flat(true); |
| 2576 | item_name_container->add_child(item_rename_cancel_button); |
| 2577 | item_rename_cancel_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_rename_canceled).bind(p_data_type, p_item_name, item_name_container)); |
| 2578 | item_rename_cancel_button->hide(); |
| 2579 | } else { |
| 2580 | item_name->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor))); |
| 2581 | |
| 2582 | Button *item_override_button = memnew(Button); |
| 2583 | item_override_button->set_button_icon(get_editor_theme_icon(SNAME("Add"))); |
| 2584 | item_override_button->set_tooltip_text(TTR("Override Item")); |
| 2585 | item_override_button->set_flat(true); |
| 2586 | item_name_container->add_child(item_override_button); |
| 2587 | item_override_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_item_override_cbk).bind(p_data_type, p_item_name)); |
| 2588 | } |
| 2589 | |
| 2590 | return item_control; |
| 2591 | } |
| 2592 | |
| 2593 | void ThemeTypeEditor::_add_focusable(Control *p_control) { |
| 2594 | focusables.append(p_control); |
| 2595 | } |
| 2596 | |
| 2597 | void ThemeTypeEditor::_update_type_items() { |
| 2598 | bool show_default = show_default_items_button->is_pressed(); |
| 2599 | |
| 2600 | focusables.clear(); |
| 2601 | |
| 2602 | // Colors. |
| 2603 | { |
| 2604 | for (int i = color_items_list->get_child_count() - 1; i >= 0; i--) { |
| 2605 | Node *node = color_items_list->get_child(i); |
| 2606 | node->queue_free(); |
| 2607 | color_items_list->remove_child(node); |
| 2608 | } |
| 2609 | |
| 2610 | HashMap<StringName, bool> color_items = _get_type_items(edited_type, Theme::DATA_TYPE_COLOR, show_default); |
| 2611 | for (const KeyValue<StringName, bool> &E : color_items) { |
| 2612 | HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_COLOR, E.key, E.value); |
| 2613 | ColorPickerButton *item_editor = memnew(ColorPickerButton); |
| 2614 | item_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2615 | item_control->add_child(item_editor); |
| 2616 | |
| 2617 | if (E.value) { |
| 2618 | item_editor->set_pick_color(edited_theme->get_color(E.key, edited_type)); |
| 2619 | item_editor->connect("color_changed", callable_mp(this, &ThemeTypeEditor::_color_item_changed).bind(E.key)); |
| 2620 | item_editor->get_popup()->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(item_editor->get_picker())); |
| 2621 | } else { |
| 2622 | item_editor->set_pick_color(ThemeDB::get_singleton()->get_default_theme()->get_color(E.key, edited_type)); |
| 2623 | item_editor->set_disabled(true); |
| 2624 | } |
| 2625 | |
| 2626 | _add_focusable(item_editor); |
| 2627 | color_items_list->add_child(item_control); |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | // Constants. |
| 2632 | { |
| 2633 | for (int i = constant_items_list->get_child_count() - 1; i >= 0; i--) { |
| 2634 | Node *node = constant_items_list->get_child(i); |
| 2635 | node->queue_free(); |
| 2636 | constant_items_list->remove_child(node); |
| 2637 | } |
| 2638 | |
| 2639 | HashMap<StringName, bool> constant_items = _get_type_items(edited_type, Theme::DATA_TYPE_CONSTANT, show_default); |
| 2640 | for (const KeyValue<StringName, bool> &E : constant_items) { |
| 2641 | HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_CONSTANT, E.key, E.value); |
| 2642 | EditorSpinSlider *item_editor = memnew(EditorSpinSlider); |
| 2643 | item_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2644 | item_editor->set_min(-100000); |
| 2645 | item_editor->set_max(100000); |
| 2646 | item_editor->set_editing_integer(true); |
| 2647 | item_editor->set_step(1); |
| 2648 | item_editor->set_allow_lesser(true); |
| 2649 | item_editor->set_allow_greater(true); |
| 2650 | item_control->add_child(item_editor); |
| 2651 | |
| 2652 | if (E.value) { |
| 2653 | item_editor->set_value(edited_theme->get_constant(E.key, edited_type)); |
| 2654 | item_editor->connect(SceneStringName(value_changed), callable_mp(this, &ThemeTypeEditor::_constant_item_changed).bind(E.key)); |
| 2655 | } else { |
| 2656 | item_editor->set_value(ThemeDB::get_singleton()->get_default_theme()->get_constant(E.key, edited_type)); |
| 2657 | item_editor->set_read_only(true); |
| 2658 | } |
| 2659 | |
| 2660 | _add_focusable(item_editor); |
| 2661 | constant_items_list->add_child(item_control); |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | // Fonts. |
| 2666 | { |
| 2667 | for (int i = font_items_list->get_child_count() - 1; i >= 0; i--) { |
| 2668 | Node *node = font_items_list->get_child(i); |
| 2669 | node->queue_free(); |
| 2670 | font_items_list->remove_child(node); |
| 2671 | } |
| 2672 | |
| 2673 | HashMap<StringName, bool> font_items = _get_type_items(edited_type, Theme::DATA_TYPE_FONT, show_default); |
| 2674 | for (const KeyValue<StringName, bool> &E : font_items) { |
| 2675 | HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_FONT, E.key, E.value); |
| 2676 | EditorResourcePicker *item_editor = memnew(EditorResourcePicker); |
| 2677 | item_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2678 | item_editor->set_base_type("Font"); |
| 2679 | item_editor->set_resource_owner(*edited_theme); |
| 2680 | item_control->add_child(item_editor); |
| 2681 | |
| 2682 | if (E.value) { |
| 2683 | if (edited_theme->has_font(E.key, edited_type)) { |
| 2684 | item_editor->set_edited_resource(edited_theme->get_font(E.key, edited_type)); |
| 2685 | } else { |
| 2686 | item_editor->set_edited_resource(Ref<Resource>()); |
| 2687 | } |
| 2688 | item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item)); |
| 2689 | item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_font_item_changed).bind(E.key)); |
| 2690 | } else { |
| 2691 | if (ThemeDB::get_singleton()->get_default_theme()->has_font(E.key, edited_type)) { |
| 2692 | item_editor->set_edited_resource(ThemeDB::get_singleton()->get_default_theme()->get_font(E.key, edited_type)); |
| 2693 | } else { |
| 2694 | item_editor->set_edited_resource(Ref<Resource>()); |
| 2695 | } |
| 2696 | item_editor->set_editable(false); |
| 2697 | } |
| 2698 | |
| 2699 | _add_focusable(item_editor); |
| 2700 | font_items_list->add_child(item_control); |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | // Fonts sizes. |
| 2705 | { |
| 2706 | for (int i = font_size_items_list->get_child_count() - 1; i >= 0; i--) { |
| 2707 | Node *node = font_size_items_list->get_child(i); |
| 2708 | node->queue_free(); |
| 2709 | font_size_items_list->remove_child(node); |
| 2710 | } |
| 2711 | |
| 2712 | HashMap<StringName, bool> font_size_items = _get_type_items(edited_type, Theme::DATA_TYPE_FONT_SIZE, show_default); |
| 2713 | for (const KeyValue<StringName, bool> &E : font_size_items) { |
| 2714 | HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_FONT_SIZE, E.key, E.value); |
| 2715 | EditorSpinSlider *item_editor = memnew(EditorSpinSlider); |
| 2716 | item_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2717 | item_editor->set_min(-100000); |
| 2718 | item_editor->set_max(100000); |
| 2719 | item_editor->set_editing_integer(true); |
| 2720 | item_editor->set_step(1); |
| 2721 | item_editor->set_allow_lesser(true); |
| 2722 | item_editor->set_allow_greater(true); |
| 2723 | item_control->add_child(item_editor); |
| 2724 | |
| 2725 | if (E.value) { |
| 2726 | item_editor->set_value(edited_theme->get_font_size(E.key, edited_type)); |
| 2727 | item_editor->connect(SceneStringName(value_changed), callable_mp(this, &ThemeTypeEditor::_font_size_item_changed).bind(E.key)); |
| 2728 | } else { |
| 2729 | item_editor->set_value(ThemeDB::get_singleton()->get_default_theme()->get_font_size(E.key, edited_type)); |
| 2730 | item_editor->set_read_only(true); |
| 2731 | } |
| 2732 | |
| 2733 | _add_focusable(item_editor); |
| 2734 | font_size_items_list->add_child(item_control); |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | // Icons. |
| 2739 | { |
| 2740 | for (int i = icon_items_list->get_child_count() - 1; i >= 0; i--) { |
| 2741 | Node *node = icon_items_list->get_child(i); |
| 2742 | node->queue_free(); |
| 2743 | icon_items_list->remove_child(node); |
| 2744 | } |
| 2745 | |
| 2746 | HashMap<StringName, bool> icon_items = _get_type_items(edited_type, Theme::DATA_TYPE_ICON, show_default); |
| 2747 | for (const KeyValue<StringName, bool> &E : icon_items) { |
| 2748 | HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_ICON, E.key, E.value); |
| 2749 | EditorResourcePicker *item_editor = memnew(EditorResourcePicker); |
| 2750 | item_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2751 | item_editor->set_base_type("Texture2D"); |
| 2752 | item_editor->set_resource_owner(*edited_theme); |
| 2753 | item_control->add_child(item_editor); |
| 2754 | |
| 2755 | if (E.value) { |
| 2756 | if (edited_theme->has_icon(E.key, edited_type)) { |
| 2757 | item_editor->set_edited_resource(edited_theme->get_icon(E.key, edited_type)); |
| 2758 | } else { |
| 2759 | item_editor->set_edited_resource(Ref<Resource>()); |
| 2760 | } |
| 2761 | item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item)); |
| 2762 | item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_icon_item_changed).bind(E.key)); |
| 2763 | } else { |
| 2764 | if (ThemeDB::get_singleton()->get_default_theme()->has_icon(E.key, edited_type)) { |
| 2765 | item_editor->set_edited_resource(ThemeDB::get_singleton()->get_default_theme()->get_icon(E.key, edited_type)); |
| 2766 | } else { |
| 2767 | item_editor->set_edited_resource(Ref<Resource>()); |
| 2768 | } |
| 2769 | item_editor->set_editable(false); |
| 2770 | } |
| 2771 | |
| 2772 | _add_focusable(item_editor); |
| 2773 | icon_items_list->add_child(item_control); |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | // Styleboxes. |
| 2778 | { |
| 2779 | for (int i = stylebox_items_list->get_child_count() - 1; i >= 0; i--) { |
| 2780 | Node *node = stylebox_items_list->get_child(i); |
| 2781 | node->queue_free(); |
| 2782 | stylebox_items_list->remove_child(node); |
| 2783 | } |
| 2784 | |
| 2785 | if (leading_stylebox.pinned) { |
| 2786 | HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_STYLEBOX, leading_stylebox.item_name, true); |
| 2787 | EditorResourcePicker *item_editor = memnew(EditorResourcePicker); |
| 2788 | item_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2789 | item_editor->set_stretch_ratio(1.5); |
| 2790 | item_editor->set_base_type("StyleBox"); |
| 2791 | item_editor->set_resource_owner(*edited_theme); |
| 2792 | |
| 2793 | Button *pin_leader_button = memnew(Button); |
| 2794 | pin_leader_button->set_flat(true); |
| 2795 | pin_leader_button->set_toggle_mode(true); |
| 2796 | pin_leader_button->set_pressed(true); |
| 2797 | pin_leader_button->set_button_icon(get_editor_theme_icon(SNAME("Pin"))); |
| 2798 | pin_leader_button->set_tooltip_text(TTR("Unpin this StyleBox as a main style.")); |
| 2799 | item_control->add_child(pin_leader_button); |
| 2800 | pin_leader_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_on_unpin_leader_button_pressed)); |
| 2801 | |
| 2802 | item_control->add_child(item_editor); |
| 2803 | |
| 2804 | if (edited_theme->has_stylebox(leading_stylebox.item_name, edited_type)) { |
| 2805 | item_editor->set_edited_resource(leading_stylebox.stylebox); |
| 2806 | } else { |
| 2807 | item_editor->set_edited_resource(Ref<Resource>()); |
| 2808 | } |
| 2809 | item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item)); |
| 2810 | item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_stylebox_item_changed).bind(leading_stylebox.item_name)); |
| 2811 | |
| 2812 | stylebox_items_list->add_child(item_control); |
| 2813 | stylebox_items_list->add_child(memnew(HSeparator)); |
| 2814 | } |
| 2815 | |
| 2816 | HashMap<StringName, bool> stylebox_items = _get_type_items(edited_type, Theme::DATA_TYPE_STYLEBOX, show_default); |
| 2817 | for (const KeyValue<StringName, bool> &E : stylebox_items) { |
| 2818 | if (leading_stylebox.pinned && leading_stylebox.item_name == E.key) { |
| 2819 | continue; |
| 2820 | } |
| 2821 | |
| 2822 | HBoxContainer *item_control = _create_property_control(Theme::DATA_TYPE_STYLEBOX, E.key, E.value); |
| 2823 | EditorResourcePicker *item_editor = memnew(EditorResourcePicker); |
| 2824 | item_editor->set_h_size_flags(SIZE_EXPAND_FILL); |
| 2825 | item_editor->set_stretch_ratio(1.5); |
| 2826 | item_editor->set_base_type("StyleBox"); |
| 2827 | item_editor->set_resource_owner(*edited_theme); |
| 2828 | |
| 2829 | if (E.value) { |
| 2830 | if (edited_theme->has_stylebox(E.key, edited_type)) { |
| 2831 | item_editor->set_edited_resource(edited_theme->get_stylebox(E.key, edited_type)); |
| 2832 | } else { |
| 2833 | item_editor->set_edited_resource(Ref<Resource>()); |
| 2834 | } |
| 2835 | item_editor->connect("resource_selected", callable_mp(this, &ThemeTypeEditor::_edit_resource_item)); |
| 2836 | item_editor->connect("resource_changed", callable_mp(this, &ThemeTypeEditor::_stylebox_item_changed).bind(E.key)); |
| 2837 | |
| 2838 | Button *pin_leader_button = memnew(Button); |
| 2839 | pin_leader_button->set_flat(true); |
| 2840 | pin_leader_button->set_toggle_mode(true); |
| 2841 | pin_leader_button->set_button_icon(get_editor_theme_icon(SNAME("Pin"))); |
| 2842 | pin_leader_button->set_tooltip_text(TTR("Pin this StyleBox as a main style. Editing its properties will update the same properties in all other StyleBoxes of this type.")); |
| 2843 | pin_leader_button->set_accessibility_name(TTRC("Pin this StyleBox as a main style.")); |
| 2844 | item_control->add_child(pin_leader_button); |
| 2845 | pin_leader_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_on_pin_leader_button_pressed).bind(item_editor, E.key)); |
| 2846 | } else { |
| 2847 | if (ThemeDB::get_singleton()->get_default_theme()->has_stylebox(E.key, edited_type)) { |
| 2848 | item_editor->set_edited_resource(ThemeDB::get_singleton()->get_default_theme()->get_stylebox(E.key, edited_type)); |
| 2849 | } else { |
| 2850 | item_editor->set_edited_resource(Ref<Resource>()); |
| 2851 | } |
| 2852 | item_editor->set_editable(false); |
| 2853 | } |
| 2854 | |
| 2855 | item_control->add_child(item_editor); |
| 2856 | _add_focusable(item_editor); |
| 2857 | stylebox_items_list->add_child(item_control); |
| 2858 | } |
| 2859 | } |
| 2860 | |
| 2861 | // Various type settings. |
| 2862 | if (edited_type.is_empty() || ClassDB::class_exists(edited_type)) { |
| 2863 | type_variation_edit->set_editable(false); |
| 2864 | type_variation_edit->set_text(""); |
| 2865 | type_variation_button->hide(); |
| 2866 | type_variation_locked->set_visible(!edited_type.is_empty()); |
| 2867 | } else { |
| 2868 | type_variation_edit->set_editable(true); |
| 2869 | type_variation_edit->set_text(edited_theme->get_type_variation_base(edited_type)); |
| 2870 | _add_focusable(type_variation_edit); |
| 2871 | type_variation_button->show(); |
| 2872 | type_variation_locked->hide(); |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | void ThemeTypeEditor::_list_type_selected(int p_index) { |
| 2877 | edited_type = theme_type_list->get_item_text(p_index); |
| 2878 | _update_type_items(); |
| 2879 | } |
| 2880 | |
| 2881 | void ThemeTypeEditor::_add_type_button_cbk() { |
| 2882 | add_type_mode = ADD_THEME_TYPE; |
| 2883 | add_type_dialog->set_title(TTR("Add Item Type")); |
| 2884 | add_type_dialog->set_ok_button_text(TTR("Add Type")); |
| 2885 | add_type_dialog->set_include_own_types(false); |
| 2886 | add_type_dialog->popup_centered(Size2(560, 420) * EDSCALE); |
| 2887 | } |
| 2888 | |
| 2889 | void ThemeTypeEditor::_rename_type_button_cbk() { |
| 2890 | theme_type_rename_line_edit->set_text(edited_type); |
| 2891 | theme_type_rename_dialog->reset_size(); |
| 2892 | theme_type_rename_dialog->popup_centered(); |
| 2893 | theme_type_rename_line_edit->grab_focus(); |
| 2894 | } |
| 2895 | |
| 2896 | void ThemeTypeEditor::_theme_type_rename_dialog_confirmed() { |
| 2897 | const String &new_type_name = Theme::validate_type_name(theme_type_rename_line_edit->get_text()); |
| 2898 | if (edited_type == new_type_name) { |
| 2899 | return; |
| 2900 | } |
| 2901 | |
| 2902 | List<StringName> theme_types; |
| 2903 | edited_theme->get_type_list(&theme_types); |
| 2904 | if (theme_types.find(new_type_name) != nullptr) { |
| 2905 | return; |
| 2906 | } |
| 2907 | |
| 2908 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 2909 | ur->create_action(TTR("Rename Theme Type")); |
| 2910 | |
| 2911 | _rename_theme_type(ur, *edited_theme, edited_type, new_type_name); |
| 2912 | |
| 2913 | ur->add_do_method(this, "select_type", new_type_name); |
| 2914 | ur->add_undo_method(this, "select_type", edited_type); |
| 2915 | |
| 2916 | ur->commit_action(); |
| 2917 | } |
| 2918 | |
| 2919 | void ThemeTypeEditor::_remove_type_button_cbk() { |
| 2920 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 2921 | |
| 2922 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 2923 | ur->create_action(TTR("Remove Theme Type")); |
| 2924 | |
| 2925 | ur->add_do_method(*edited_theme, "remove_type", edited_type); |
| 2926 | // If the type was empty, it cannot be restored with merge, but thankfully we can fake it. |
| 2927 | ur->add_undo_method(*edited_theme, "add_type", edited_type); |
| 2928 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 2929 | |
| 2930 | ur->commit_action(); |
| 2931 | } |
| 2932 | |
| 2933 | void ThemeTypeEditor::_add_default_type_items() { |
| 2934 | List<StringName> names; |
| 2935 | String default_type = edited_type; |
| 2936 | if (edited_theme->get_type_variation_base(edited_type) != StringName()) { |
| 2937 | default_type = edited_theme->get_type_variation_base(edited_type); |
| 2938 | } |
| 2939 | |
| 2940 | Ref<Theme> old_snapshot = edited_theme->duplicate(); |
| 2941 | Ref<Theme> new_snapshot = edited_theme->duplicate(); |
| 2942 | |
| 2943 | updating = true; |
| 2944 | |
| 2945 | { |
| 2946 | names.clear(); |
| 2947 | ThemeDB::get_singleton()->get_default_theme()->get_icon_list(default_type, &names); |
| 2948 | for (const StringName &E : names) { |
| 2949 | if (!new_snapshot->has_icon(E, edited_type)) { |
| 2950 | new_snapshot->set_icon(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_icon(E, edited_type)); |
| 2951 | } |
| 2952 | } |
| 2953 | } |
| 2954 | { |
| 2955 | names.clear(); |
| 2956 | ThemeDB::get_singleton()->get_default_theme()->get_stylebox_list(default_type, &names); |
| 2957 | for (const StringName &E : names) { |
| 2958 | if (!new_snapshot->has_stylebox(E, edited_type)) { |
| 2959 | new_snapshot->set_stylebox(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_stylebox(E, edited_type)); |
| 2960 | } |
| 2961 | } |
| 2962 | } |
| 2963 | { |
| 2964 | names.clear(); |
| 2965 | ThemeDB::get_singleton()->get_default_theme()->get_font_list(default_type, &names); |
| 2966 | for (const StringName &E : names) { |
| 2967 | if (!new_snapshot->has_font(E, edited_type)) { |
| 2968 | new_snapshot->set_font(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_font(E, edited_type)); |
| 2969 | } |
| 2970 | } |
| 2971 | } |
| 2972 | { |
| 2973 | names.clear(); |
| 2974 | ThemeDB::get_singleton()->get_default_theme()->get_font_size_list(default_type, &names); |
| 2975 | for (const StringName &E : names) { |
| 2976 | if (!new_snapshot->has_font_size(E, edited_type)) { |
| 2977 | new_snapshot->set_font_size(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_font_size(E, edited_type)); |
| 2978 | } |
| 2979 | } |
| 2980 | } |
| 2981 | { |
| 2982 | names.clear(); |
| 2983 | ThemeDB::get_singleton()->get_default_theme()->get_color_list(default_type, &names); |
| 2984 | for (const StringName &E : names) { |
| 2985 | if (!new_snapshot->has_color(E, edited_type)) { |
| 2986 | new_snapshot->set_color(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_color(E, edited_type)); |
| 2987 | } |
| 2988 | } |
| 2989 | } |
| 2990 | { |
| 2991 | names.clear(); |
| 2992 | ThemeDB::get_singleton()->get_default_theme()->get_constant_list(default_type, &names); |
| 2993 | for (const StringName &E : names) { |
| 2994 | if (!new_snapshot->has_constant(E, edited_type)) { |
| 2995 | new_snapshot->set_constant(E, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_constant(E, edited_type)); |
| 2996 | } |
| 2997 | } |
| 2998 | } |
| 2999 | |
| 3000 | updating = false; |
| 3001 | |
| 3002 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3003 | ur->create_action(TTR("Override All Default Theme Items")); |
| 3004 | |
| 3005 | ur->add_do_method(*edited_theme, "merge_with", new_snapshot); |
| 3006 | ur->add_undo_method(*edited_theme, "clear"); |
| 3007 | ur->add_undo_method(*edited_theme, "merge_with", old_snapshot); |
| 3008 | |
| 3009 | ur->add_do_method(this, "_update_type_items"); |
| 3010 | ur->add_undo_method(this, "_update_type_items"); |
| 3011 | |
| 3012 | ur->commit_action(); |
| 3013 | } |
| 3014 | |
| 3015 | void ThemeTypeEditor::_update_add_button(const String &p_text, LineEdit *p_for_edit) { |
| 3016 | Button *button = Object::cast_to<Button>(p_for_edit->get_meta("button")); |
| 3017 | button->set_disabled(p_text.strip_edges().is_empty()); |
| 3018 | } |
| 3019 | |
| 3020 | void ThemeTypeEditor::_item_add_cbk(int p_data_type, Control *p_control) { |
| 3021 | LineEdit *le = Object::cast_to<LineEdit>(p_control); |
| 3022 | if (le->get_text().strip_edges().is_empty()) { |
| 3023 | return; |
| 3024 | } |
| 3025 | |
| 3026 | const String item_name = le->get_text().strip_edges().validate_ascii_identifier(); |
| 3027 | |
| 3028 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3029 | ur->create_action(TTR("Add Theme Item")); |
| 3030 | |
| 3031 | switch (p_data_type) { |
| 3032 | case Theme::DATA_TYPE_COLOR: { |
| 3033 | ur->add_do_method(*edited_theme, "set_color", item_name, edited_type, Color()); |
| 3034 | ur->add_undo_method(*edited_theme, "clear_color", item_name, edited_type); |
| 3035 | } break; |
| 3036 | case Theme::DATA_TYPE_CONSTANT: { |
| 3037 | ur->add_do_method(*edited_theme, "set_constant", item_name, edited_type, 0); |
| 3038 | ur->add_undo_method(*edited_theme, "clear_constant", item_name, edited_type); |
| 3039 | } break; |
| 3040 | case Theme::DATA_TYPE_FONT: { |
| 3041 | ur->add_do_method(*edited_theme, "set_font", item_name, edited_type, Ref<Font>()); |
| 3042 | ur->add_undo_method(*edited_theme, "clear_font", item_name, edited_type); |
| 3043 | } break; |
| 3044 | case Theme::DATA_TYPE_FONT_SIZE: { |
| 3045 | ur->add_do_method(*edited_theme, "set_font_size", item_name, edited_type, -1); |
| 3046 | ur->add_undo_method(*edited_theme, "clear_font_size", item_name, edited_type); |
| 3047 | } break; |
| 3048 | case Theme::DATA_TYPE_ICON: { |
| 3049 | ur->add_do_method(*edited_theme, "set_icon", item_name, edited_type, Ref<Texture2D>()); |
| 3050 | ur->add_undo_method(*edited_theme, "clear_icon", item_name, edited_type); |
| 3051 | } break; |
| 3052 | case Theme::DATA_TYPE_STYLEBOX: { |
| 3053 | Ref<StyleBox> sb; |
| 3054 | ur->add_do_method(*edited_theme, "set_stylebox", item_name, edited_type, sb); |
| 3055 | ur->add_undo_method(*edited_theme, "clear_stylebox", item_name, edited_type); |
| 3056 | |
| 3057 | if (is_stylebox_pinned(sb)) { |
| 3058 | ur->add_undo_method(this, "_unpin_leading_stylebox"); |
| 3059 | } |
| 3060 | } break; |
| 3061 | } |
| 3062 | |
| 3063 | ur->commit_action(); |
| 3064 | |
| 3065 | le->set_text(""); |
| 3066 | _update_add_button("", le); |
| 3067 | } |
| 3068 | |
| 3069 | void ThemeTypeEditor::_item_add_lineedit_cbk(String p_value, int p_data_type, Control *p_control) { |
| 3070 | _item_add_cbk(p_data_type, p_control); |
| 3071 | } |
| 3072 | |
| 3073 | void ThemeTypeEditor::_item_override_cbk(int p_data_type, String p_item_name) { |
| 3074 | // Avoid errors if the action is triggered multiple times. |
| 3075 | if (edited_theme->has_theme_item_nocheck((Theme::DataType)p_data_type, p_item_name, edited_type)) { |
| 3076 | return; |
| 3077 | } |
| 3078 | |
| 3079 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3080 | ur->create_action(TTR("Override Theme Item")); |
| 3081 | |
| 3082 | switch (p_data_type) { |
| 3083 | case Theme::DATA_TYPE_COLOR: { |
| 3084 | ur->add_do_method(*edited_theme, "set_color", p_item_name, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_color(p_item_name, edited_type)); |
| 3085 | ur->add_undo_method(*edited_theme, "clear_color", p_item_name, edited_type); |
| 3086 | } break; |
| 3087 | case Theme::DATA_TYPE_CONSTANT: { |
| 3088 | ur->add_do_method(*edited_theme, "set_constant", p_item_name, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_constant(p_item_name, edited_type)); |
| 3089 | ur->add_undo_method(*edited_theme, "clear_constant", p_item_name, edited_type); |
| 3090 | } break; |
| 3091 | case Theme::DATA_TYPE_FONT: { |
| 3092 | ur->add_do_method(*edited_theme, "set_font", p_item_name, edited_type, Ref<Font>()); |
| 3093 | ur->add_undo_method(*edited_theme, "clear_font", p_item_name, edited_type); |
| 3094 | } break; |
| 3095 | case Theme::DATA_TYPE_FONT_SIZE: { |
| 3096 | ur->add_do_method(*edited_theme, "set_font_size", p_item_name, edited_type, ThemeDB::get_singleton()->get_default_theme()->get_font_size(p_item_name, edited_type)); |
| 3097 | ur->add_undo_method(*edited_theme, "clear_font_size", p_item_name, edited_type); |
| 3098 | } break; |
| 3099 | case Theme::DATA_TYPE_ICON: { |
| 3100 | ur->add_do_method(*edited_theme, "set_icon", p_item_name, edited_type, Ref<Texture2D>()); |
| 3101 | ur->add_undo_method(*edited_theme, "clear_icon", p_item_name, edited_type); |
| 3102 | } break; |
| 3103 | case Theme::DATA_TYPE_STYLEBOX: { |
| 3104 | Ref<StyleBox> sb; |
| 3105 | ur->add_do_method(*edited_theme, "set_stylebox", p_item_name, edited_type, sb); |
| 3106 | ur->add_undo_method(*edited_theme, "clear_stylebox", p_item_name, edited_type); |
| 3107 | |
| 3108 | if (is_stylebox_pinned(sb)) { |
| 3109 | ur->add_undo_method(this, "_unpin_leading_stylebox"); |
| 3110 | } |
| 3111 | } break; |
| 3112 | } |
| 3113 | |
| 3114 | ur->commit_action(); |
| 3115 | } |
| 3116 | |
| 3117 | void ThemeTypeEditor::_item_remove_cbk(int p_data_type, String p_item_name) { |
| 3118 | // Avoid errors if the action is triggered multiple times. |
| 3119 | if (!edited_theme->has_theme_item_nocheck((Theme::DataType)p_data_type, p_item_name, edited_type)) { |
| 3120 | return; |
| 3121 | } |
| 3122 | |
| 3123 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3124 | ur->create_action(TTR("Remove Theme Item")); |
| 3125 | |
| 3126 | switch (p_data_type) { |
| 3127 | case Theme::DATA_TYPE_COLOR: { |
| 3128 | ur->add_do_method(*edited_theme, "clear_color", p_item_name, edited_type); |
| 3129 | ur->add_undo_method(*edited_theme, "set_color", p_item_name, edited_type, edited_theme->get_color(p_item_name, edited_type)); |
| 3130 | } break; |
| 3131 | case Theme::DATA_TYPE_CONSTANT: { |
| 3132 | ur->add_do_method(*edited_theme, "clear_constant", p_item_name, edited_type); |
| 3133 | ur->add_undo_method(*edited_theme, "set_constant", p_item_name, edited_type, edited_theme->get_constant(p_item_name, edited_type)); |
| 3134 | } break; |
| 3135 | case Theme::DATA_TYPE_FONT: { |
| 3136 | ur->add_do_method(*edited_theme, "clear_font", p_item_name, edited_type); |
| 3137 | if (edited_theme->has_font(p_item_name, edited_type)) { |
| 3138 | ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, edited_theme->get_font(p_item_name, edited_type)); |
| 3139 | } else { |
| 3140 | ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, Ref<Font>()); |
| 3141 | } |
| 3142 | } break; |
| 3143 | case Theme::DATA_TYPE_FONT_SIZE: { |
| 3144 | ur->add_do_method(*edited_theme, "clear_font_size", p_item_name, edited_type); |
| 3145 | ur->add_undo_method(*edited_theme, "set_font_size", p_item_name, edited_type, edited_theme->get_font_size(p_item_name, edited_type)); |
| 3146 | } break; |
| 3147 | case Theme::DATA_TYPE_ICON: { |
| 3148 | ur->add_do_method(*edited_theme, "clear_icon", p_item_name, edited_type); |
| 3149 | if (edited_theme->has_icon(p_item_name, edited_type)) { |
| 3150 | ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, edited_theme->get_icon(p_item_name, edited_type)); |
| 3151 | } else { |
| 3152 | ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, Ref<Texture2D>()); |
| 3153 | } |
| 3154 | } break; |
| 3155 | case Theme::DATA_TYPE_STYLEBOX: { |
| 3156 | Ref<StyleBox> sb = edited_theme->get_stylebox(p_item_name, edited_type); |
| 3157 | ur->add_do_method(*edited_theme, "clear_stylebox", p_item_name, edited_type); |
| 3158 | if (edited_theme->has_stylebox(p_item_name, edited_type)) { |
| 3159 | ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, sb); |
| 3160 | } else { |
| 3161 | ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, Ref<StyleBox>()); |
| 3162 | } |
| 3163 | |
| 3164 | if (is_stylebox_pinned(sb)) { |
| 3165 | ur->add_do_method(this, "_unpin_leading_stylebox"); |
| 3166 | ur->add_undo_method(this, "_pin_leading_stylebox", p_item_name, sb); |
| 3167 | } |
| 3168 | } break; |
| 3169 | } |
| 3170 | |
| 3171 | ur->commit_action(); |
| 3172 | } |
| 3173 | |
| 3174 | void ThemeTypeEditor::_item_rename_cbk(int p_data_type, String p_item_name, Control *p_control) { |
| 3175 | // Label |
| 3176 | Object::cast_to<Label>(p_control->get_child(0))->hide(); |
| 3177 | // Label buttons |
| 3178 | Object::cast_to<Button>(p_control->get_child(2))->hide(); |
| 3179 | Object::cast_to<Button>(p_control->get_child(3))->hide(); |
| 3180 | |
| 3181 | // LineEdit |
| 3182 | Object::cast_to<LineEdit>(p_control->get_child(1))->set_text(p_item_name); |
| 3183 | Object::cast_to<LineEdit>(p_control->get_child(1))->show(); |
| 3184 | // LineEdit buttons |
| 3185 | Object::cast_to<Button>(p_control->get_child(4))->show(); |
| 3186 | Object::cast_to<Button>(p_control->get_child(5))->show(); |
| 3187 | } |
| 3188 | |
| 3189 | void ThemeTypeEditor::_item_rename_confirmed(int p_data_type, String p_item_name, Control *p_control) { |
| 3190 | LineEdit *le = Object::cast_to<LineEdit>(p_control->get_child(1)); |
| 3191 | if (le->get_text().strip_edges().is_empty()) { |
| 3192 | return; |
| 3193 | } |
| 3194 | |
| 3195 | const String new_name = le->get_text().strip_edges().validate_ascii_identifier(); |
| 3196 | if (new_name == p_item_name) { |
| 3197 | _item_rename_canceled(p_data_type, p_item_name, p_control); |
| 3198 | return; |
| 3199 | } |
| 3200 | |
| 3201 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3202 | ur->create_action(TTR("Rename Theme Item")); |
| 3203 | |
| 3204 | switch (p_data_type) { |
| 3205 | case Theme::DATA_TYPE_COLOR: { |
| 3206 | ur->add_do_method(*edited_theme, "rename_color", p_item_name, new_name, edited_type); |
| 3207 | ur->add_undo_method(*edited_theme, "rename_color", new_name, p_item_name, edited_type); |
| 3208 | } break; |
| 3209 | case Theme::DATA_TYPE_CONSTANT: { |
| 3210 | ur->add_do_method(*edited_theme, "rename_constant", p_item_name, new_name, edited_type); |
| 3211 | ur->add_undo_method(*edited_theme, "rename_constant", new_name, p_item_name, edited_type); |
| 3212 | } break; |
| 3213 | case Theme::DATA_TYPE_FONT: { |
| 3214 | ur->add_do_method(*edited_theme, "rename_font", p_item_name, new_name, edited_type); |
| 3215 | ur->add_undo_method(*edited_theme, "rename_font", new_name, p_item_name, edited_type); |
| 3216 | } break; |
| 3217 | case Theme::DATA_TYPE_FONT_SIZE: { |
| 3218 | ur->add_do_method(*edited_theme, "rename_font_size", p_item_name, new_name, edited_type); |
| 3219 | ur->add_undo_method(*edited_theme, "rename_font_size", new_name, p_item_name, edited_type); |
| 3220 | } break; |
| 3221 | case Theme::DATA_TYPE_ICON: { |
| 3222 | ur->add_do_method(*edited_theme, "rename_icon", p_item_name, new_name, edited_type); |
| 3223 | ur->add_undo_method(*edited_theme, "rename_icon", new_name, p_item_name, edited_type); |
| 3224 | } break; |
| 3225 | case Theme::DATA_TYPE_STYLEBOX: { |
| 3226 | ur->add_do_method(*edited_theme, "rename_stylebox", p_item_name, new_name, edited_type); |
| 3227 | ur->add_undo_method(*edited_theme, "rename_stylebox", new_name, p_item_name, edited_type); |
| 3228 | |
| 3229 | if (leading_stylebox.pinned && leading_stylebox.item_name == p_item_name) { |
| 3230 | leading_stylebox.item_name = new_name; |
| 3231 | } |
| 3232 | } break; |
| 3233 | } |
| 3234 | |
| 3235 | ur->commit_action(); |
| 3236 | } |
| 3237 | |
| 3238 | void ThemeTypeEditor::_item_rename_entered(String p_value, int p_data_type, String p_item_name, Control *p_control) { |
| 3239 | _item_rename_confirmed(p_data_type, p_item_name, p_control); |
| 3240 | } |
| 3241 | |
| 3242 | void ThemeTypeEditor::_item_rename_canceled(int p_data_type, String p_item_name, Control *p_control) { |
| 3243 | // LineEdit |
| 3244 | Object::cast_to<LineEdit>(p_control->get_child(1))->hide(); |
| 3245 | // LineEdit buttons |
| 3246 | Object::cast_to<Button>(p_control->get_child(4))->hide(); |
| 3247 | Object::cast_to<Button>(p_control->get_child(5))->hide(); |
| 3248 | |
| 3249 | // Label |
| 3250 | Object::cast_to<Label>(p_control->get_child(0))->show(); |
| 3251 | // Label buttons |
| 3252 | Object::cast_to<Button>(p_control->get_child(2))->show(); |
| 3253 | Object::cast_to<Button>(p_control->get_child(3))->show(); |
| 3254 | } |
| 3255 | |
| 3256 | void ThemeTypeEditor::_color_item_changed(Color p_value, String p_item_name) { |
| 3257 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3258 | ur->create_action(TTR("Set Color Item in Theme"), UndoRedo::MERGE_ENDS); |
| 3259 | ur->add_do_method(*edited_theme, "set_color", p_item_name, edited_type, p_value); |
| 3260 | ur->add_undo_method(*edited_theme, "set_color", p_item_name, edited_type, edited_theme->get_color(p_item_name, edited_type)); |
| 3261 | ur->commit_action(); |
| 3262 | } |
| 3263 | |
| 3264 | void ThemeTypeEditor::_constant_item_changed(float p_value, String p_item_name) { |
| 3265 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3266 | ur->create_action(TTR("Set Constant Item in Theme")); |
| 3267 | ur->add_do_method(*edited_theme, "set_constant", p_item_name, edited_type, p_value); |
| 3268 | ur->add_undo_method(*edited_theme, "set_constant", p_item_name, edited_type, edited_theme->get_constant(p_item_name, edited_type)); |
| 3269 | ur->commit_action(); |
| 3270 | } |
| 3271 | |
| 3272 | void ThemeTypeEditor::_font_size_item_changed(float p_value, String p_item_name) { |
| 3273 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3274 | ur->create_action(TTR("Set Font Size Item in Theme")); |
| 3275 | ur->add_do_method(*edited_theme, "set_font_size", p_item_name, edited_type, p_value); |
| 3276 | ur->add_undo_method(*edited_theme, "set_font_size", p_item_name, edited_type, edited_theme->get_font_size(p_item_name, edited_type)); |
| 3277 | ur->commit_action(); |
| 3278 | } |
| 3279 | |
| 3280 | void ThemeTypeEditor::_edit_resource_item(Ref<Resource> p_resource, bool p_edit) { |
| 3281 | EditorNode::get_singleton()->edit_resource(p_resource); |
| 3282 | } |
| 3283 | |
| 3284 | void ThemeTypeEditor::_font_item_changed(Ref<Font> p_value, String p_item_name) { |
| 3285 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3286 | ur->create_action(TTR("Set Font Item in Theme")); |
| 3287 | |
| 3288 | ur->add_do_method(*edited_theme, "set_font", p_item_name, edited_type, p_value.is_valid() ? p_value : Ref<Font>()); |
| 3289 | if (edited_theme->has_font(p_item_name, edited_type)) { |
| 3290 | ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, edited_theme->get_font(p_item_name, edited_type)); |
| 3291 | } else { |
| 3292 | ur->add_undo_method(*edited_theme, "set_font", p_item_name, edited_type, Ref<Font>()); |
| 3293 | } |
| 3294 | |
| 3295 | ur->add_do_method(this, CoreStringName(call_deferred), "_update_type_items"); |
| 3296 | ur->add_undo_method(this, CoreStringName(call_deferred), "_update_type_items"); |
| 3297 | |
| 3298 | ur->commit_action(); |
| 3299 | } |
| 3300 | |
| 3301 | void ThemeTypeEditor::_icon_item_changed(Ref<Texture2D> p_value, String p_item_name) { |
| 3302 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3303 | ur->create_action(TTR("Set Icon Item in Theme")); |
| 3304 | |
| 3305 | ur->add_do_method(*edited_theme, "set_icon", p_item_name, edited_type, p_value.is_valid() ? p_value : Ref<Texture2D>()); |
| 3306 | if (edited_theme->has_icon(p_item_name, edited_type)) { |
| 3307 | ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, edited_theme->get_icon(p_item_name, edited_type)); |
| 3308 | } else { |
| 3309 | ur->add_undo_method(*edited_theme, "set_icon", p_item_name, edited_type, Ref<Texture2D>()); |
| 3310 | } |
| 3311 | |
| 3312 | ur->add_do_method(this, CoreStringName(call_deferred), "_update_type_items"); |
| 3313 | ur->add_undo_method(this, CoreStringName(call_deferred), "_update_type_items"); |
| 3314 | |
| 3315 | ur->commit_action(); |
| 3316 | } |
| 3317 | |
| 3318 | void ThemeTypeEditor::_stylebox_item_changed(Ref<StyleBox> p_value, String p_item_name) { |
| 3319 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3320 | ur->create_action(TTR("Set Stylebox Item in Theme")); |
| 3321 | |
| 3322 | ur->add_do_method(*edited_theme, "set_stylebox", p_item_name, edited_type, p_value.is_valid() ? p_value : Ref<StyleBox>()); |
| 3323 | if (edited_theme->has_stylebox(p_item_name, edited_type)) { |
| 3324 | ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, edited_theme->get_stylebox(p_item_name, edited_type)); |
| 3325 | } else { |
| 3326 | ur->add_undo_method(*edited_theme, "set_stylebox", p_item_name, edited_type, Ref<StyleBox>()); |
| 3327 | } |
| 3328 | |
| 3329 | ur->add_do_method(this, "_change_pinned_stylebox"); |
| 3330 | ur->add_undo_method(this, "_change_pinned_stylebox"); |
| 3331 | |
| 3332 | ur->add_do_method(this, CoreStringName(call_deferred), "_update_type_items"); |
| 3333 | ur->add_undo_method(this, CoreStringName(call_deferred), "_update_type_items"); |
| 3334 | |
| 3335 | ur->commit_action(); |
| 3336 | } |
| 3337 | |
| 3338 | void ThemeTypeEditor::_change_pinned_stylebox() { |
| 3339 | if (leading_stylebox.pinned) { |
| 3340 | if (leading_stylebox.stylebox.is_valid()) { |
| 3341 | leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading)); |
| 3342 | } |
| 3343 | |
| 3344 | Ref<StyleBox> new_stylebox = edited_theme->get_stylebox(leading_stylebox.item_name, edited_type); |
| 3345 | leading_stylebox.stylebox = new_stylebox; |
| 3346 | leading_stylebox.ref_stylebox = (new_stylebox.is_valid() ? new_stylebox->duplicate() : Ref<Resource>()); |
| 3347 | |
| 3348 | if (leading_stylebox.stylebox.is_valid()) { |
| 3349 | new_stylebox->connect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading)); |
| 3350 | } |
| 3351 | } else if (leading_stylebox.stylebox.is_valid()) { |
| 3352 | leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading)); |
| 3353 | } |
| 3354 | } |
| 3355 | |
| 3356 | void ThemeTypeEditor::_on_pin_leader_button_pressed(Control *p_editor, String p_item_name) { |
| 3357 | Ref<StyleBox> stylebox; |
| 3358 | if (Object::cast_to<EditorResourcePicker>(p_editor)) { |
| 3359 | stylebox = Object::cast_to<EditorResourcePicker>(p_editor)->get_edited_resource(); |
| 3360 | } |
| 3361 | |
| 3362 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3363 | ur->create_action(TTR("Pin Stylebox")); |
| 3364 | ur->add_do_method(this, "_pin_leading_stylebox", p_item_name, stylebox); |
| 3365 | |
| 3366 | if (leading_stylebox.pinned) { |
| 3367 | ur->add_undo_method(this, "_pin_leading_stylebox", leading_stylebox.item_name, leading_stylebox.stylebox); |
| 3368 | } else { |
| 3369 | ur->add_undo_method(this, "_unpin_leading_stylebox"); |
| 3370 | } |
| 3371 | |
| 3372 | ur->commit_action(); |
| 3373 | } |
| 3374 | |
| 3375 | void ThemeTypeEditor::_pin_leading_stylebox(String p_item_name, Ref<StyleBox> p_stylebox) { |
| 3376 | if (leading_stylebox.stylebox.is_valid()) { |
| 3377 | leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading)); |
| 3378 | } |
| 3379 | |
| 3380 | LeadingStylebox leader; |
| 3381 | leader.pinned = true; |
| 3382 | leader.item_name = p_item_name; |
| 3383 | leader.stylebox = p_stylebox; |
| 3384 | leader.ref_stylebox = (p_stylebox.is_valid() ? p_stylebox->duplicate() : Ref<Resource>()); |
| 3385 | |
| 3386 | leading_stylebox = leader; |
| 3387 | if (p_stylebox.is_valid()) { |
| 3388 | p_stylebox->connect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading)); |
| 3389 | } |
| 3390 | |
| 3391 | _update_type_items(); |
| 3392 | } |
| 3393 | |
| 3394 | void ThemeTypeEditor::_on_unpin_leader_button_pressed() { |
| 3395 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3396 | ur->create_action(TTR("Unpin Stylebox")); |
| 3397 | ur->add_do_method(this, "_unpin_leading_stylebox"); |
| 3398 | ur->add_undo_method(this, "_pin_leading_stylebox", leading_stylebox.item_name, leading_stylebox.stylebox); |
| 3399 | ur->commit_action(); |
| 3400 | } |
| 3401 | |
| 3402 | void ThemeTypeEditor::_unpin_leading_stylebox() { |
| 3403 | if (leading_stylebox.stylebox.is_valid()) { |
| 3404 | leading_stylebox.stylebox->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_stylebox_from_leading)); |
| 3405 | } |
| 3406 | |
| 3407 | LeadingStylebox leader; |
| 3408 | leader.pinned = false; |
| 3409 | leading_stylebox = leader; |
| 3410 | |
| 3411 | _update_type_items(); |
| 3412 | } |
| 3413 | |
| 3414 | void ThemeTypeEditor::_update_stylebox_from_leading() { |
| 3415 | if (!leading_stylebox.pinned || leading_stylebox.stylebox.is_null()) { |
| 3416 | return; |
| 3417 | } |
| 3418 | ERR_FAIL_COND_MSG(edited_theme.is_null(), "Leading stylebox does not have an edited theme to update"); |
| 3419 | |
| 3420 | // Prevent changes from immediately being reported while the operation is still ongoing. |
| 3421 | edited_theme->_freeze_change_propagation(); |
| 3422 | |
| 3423 | List<StringName> names; |
| 3424 | edited_theme->get_stylebox_list(edited_type, &names); |
| 3425 | List<Ref<StyleBox>> styleboxes; |
| 3426 | for (const StringName &E : names) { |
| 3427 | Ref<StyleBox> sb = edited_theme->get_stylebox(E, edited_type); |
| 3428 | |
| 3429 | // Avoid itself, stylebox can be shared between items. |
| 3430 | if (sb == leading_stylebox.stylebox) { |
| 3431 | continue; |
| 3432 | } |
| 3433 | |
| 3434 | if (sb->get_class() == leading_stylebox.stylebox->get_class()) { |
| 3435 | styleboxes.push_back(sb); |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | List<PropertyInfo> props; |
| 3440 | leading_stylebox.stylebox->get_property_list(&props); |
| 3441 | for (const PropertyInfo &E : props) { |
| 3442 | if (!(E.usage & PROPERTY_USAGE_STORAGE)) { |
| 3443 | continue; |
| 3444 | } |
| 3445 | |
| 3446 | Variant value = leading_stylebox.stylebox->get(E.name); |
| 3447 | Variant ref_value = leading_stylebox.ref_stylebox->get(E.name); |
| 3448 | if (value == ref_value) { |
| 3449 | continue; |
| 3450 | } |
| 3451 | |
| 3452 | for (const Ref<StyleBox> &F : styleboxes) { |
| 3453 | Ref<StyleBox> sb = F; |
| 3454 | sb->set(E.name, value); |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | leading_stylebox.ref_stylebox = leading_stylebox.stylebox->duplicate(); |
| 3459 | |
| 3460 | // Allow changes to be reported now that the operation is finished. |
| 3461 | edited_theme->_unfreeze_and_propagate_changes(); |
| 3462 | } |
| 3463 | |
| 3464 | void ThemeTypeEditor::_type_variation_changed(const String p_value) { |
| 3465 | EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); |
| 3466 | ur->create_action(TTR("Set Theme Type Variation")); |
| 3467 | |
| 3468 | if (p_value.is_empty()) { |
| 3469 | ur->add_do_method(*edited_theme, "clear_type_variation", edited_type); |
| 3470 | } else { |
| 3471 | ur->add_do_method(*edited_theme, "set_type_variation", edited_type, StringName(p_value)); |
| 3472 | } |
| 3473 | |
| 3474 | if (edited_theme->get_type_variation_base(edited_type) == "") { |
| 3475 | ur->add_undo_method(*edited_theme, "clear_type_variation", edited_type); |
| 3476 | } else { |
| 3477 | ur->add_undo_method(*edited_theme, "set_type_variation", edited_type, edited_theme->get_type_variation_base(edited_type)); |
| 3478 | } |
| 3479 | |
| 3480 | ur->commit_action(); |
| 3481 | } |
| 3482 | |
| 3483 | void ThemeTypeEditor::_add_type_variation_cbk() { |
| 3484 | add_type_mode = ADD_VARIATION_BASE; |
| 3485 | add_type_dialog->set_title(TTR("Set Variation Base Type")); |
| 3486 | add_type_dialog->set_ok_button_text(TTR("Set Base Type")); |
| 3487 | add_type_dialog->set_include_own_types(true); |
| 3488 | add_type_dialog->popup_centered(Size2(560, 420) * EDSCALE); |
| 3489 | } |
| 3490 | |
| 3491 | void ThemeTypeEditor::_add_type_dialog_selected(const String p_type_name) { |
| 3492 | if (add_type_mode == ADD_THEME_TYPE) { |
| 3493 | select_type(p_type_name); |
| 3494 | } else if (add_type_mode == ADD_VARIATION_BASE) { |
| 3495 | _type_variation_changed(p_type_name); |
| 3496 | } |
| 3497 | } |
| 3498 | |
| 3499 | void ThemeTypeEditor::_notification(int p_what) { |
| 3500 | switch (p_what) { |
| 3501 | case NOTIFICATION_THEME_CHANGED: { |
| 3502 | add_type_button->set_button_icon(get_editor_theme_icon(SNAME("Add"))); |
| 3503 | rename_type_button->set_button_icon(get_editor_theme_icon(SNAME("Rename"))); |
| 3504 | remove_type_button->set_button_icon(get_editor_theme_icon(SNAME("Remove"))); |
| 3505 | |
| 3506 | const String theme_style = EDITOR_GET("interface/theme/style"); |
| 3507 | data_type_tabs->set_theme_type_variation(theme_style == "Classic" ? "TabContainerOdd" : "TabContainerInner"); |
| 3508 | |
| 3509 | data_type_tabs->set_tab_icon(0, get_editor_theme_icon(SNAME("Color"))); |
| 3510 | data_type_tabs->set_tab_icon(1, get_editor_theme_icon(SNAME("MemberConstant"))); |
| 3511 | data_type_tabs->set_tab_icon(2, get_editor_theme_icon(SNAME("FontItem"))); |
| 3512 | data_type_tabs->set_tab_icon(3, get_editor_theme_icon(SNAME("FontSize"))); |
| 3513 | data_type_tabs->set_tab_icon(4, get_editor_theme_icon(SNAME("ImageTexture"))); |
| 3514 | data_type_tabs->set_tab_icon(5, get_editor_theme_icon(SNAME("StyleBoxFlat"))); |
| 3515 | data_type_tabs->set_tab_icon(6, get_editor_theme_icon(SNAME("Tools"))); |
| 3516 | |
| 3517 | type_variation_button->set_button_icon(get_editor_theme_icon(SNAME("Add"))); |
| 3518 | } break; |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | void ThemeTypeEditor::_bind_methods() { |
| 3523 | ClassDB::bind_method(D_METHOD("_update_type_items"), &ThemeTypeEditor::_update_type_items); |
| 3524 | ClassDB::bind_method(D_METHOD("_pin_leading_stylebox"), &ThemeTypeEditor::_pin_leading_stylebox); |
| 3525 | ClassDB::bind_method(D_METHOD("_unpin_leading_stylebox"), &ThemeTypeEditor::_unpin_leading_stylebox); |
| 3526 | ClassDB::bind_method(D_METHOD("_change_pinned_stylebox"), &ThemeTypeEditor::_change_pinned_stylebox); |
| 3527 | ClassDB::bind_method(D_METHOD("select_type", "type_name"), &ThemeTypeEditor::select_type); |
| 3528 | } |
| 3529 | |
| 3530 | void ThemeTypeEditor::set_edited_theme(const Ref<Theme> &p_theme) { |
| 3531 | if (edited_theme.is_valid()) { |
| 3532 | edited_theme->disconnect_changed(callable_mp(this, &ThemeTypeEditor::_update_type_list_debounced)); |
| 3533 | } |
| 3534 | |
| 3535 | edited_theme = p_theme; |
| 3536 | if (edited_theme.is_valid()) { |
| 3537 | edited_theme->connect_changed(callable_mp(this, &ThemeTypeEditor::_update_type_list_debounced)); |
| 3538 | _update_type_list(); |
| 3539 | } |
| 3540 | |
| 3541 | add_type_dialog->set_edited_theme(edited_theme); |
| 3542 | } |
| 3543 | |
| 3544 | void ThemeTypeEditor::select_type(String p_type_name) { |
| 3545 | edited_type = p_type_name; |
| 3546 | bool type_exists = false; |
| 3547 | |
| 3548 | for (int i = 0; i < theme_type_list->get_item_count(); i++) { |
| 3549 | String type_name = theme_type_list->get_item_text(i); |
| 3550 | if (type_name == edited_type) { |
| 3551 | theme_type_list->select(i); |
| 3552 | type_exists = true; |
| 3553 | break; |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | if (type_exists) { |
| 3558 | _update_type_items(); |
| 3559 | } else { |
| 3560 | edited_theme->add_icon_type(edited_type); |
| 3561 | edited_theme->add_stylebox_type(edited_type); |
| 3562 | edited_theme->add_font_type(edited_type); |
| 3563 | edited_theme->add_font_size_type(edited_type); |
| 3564 | edited_theme->add_color_type(edited_type); |
| 3565 | edited_theme->add_constant_type(edited_type); |
| 3566 | |
| 3567 | _update_type_list(); |
| 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | bool ThemeTypeEditor::is_stylebox_pinned(Ref<StyleBox> p_stylebox) { |
| 3572 | return leading_stylebox.pinned && leading_stylebox.stylebox == p_stylebox; |
| 3573 | } |
| 3574 | |
| 3575 | ThemeTypeEditor::ThemeTypeEditor() { |
| 3576 | VBoxContainer *main_vb = memnew(VBoxContainer); |
| 3577 | add_child(main_vb); |
| 3578 | |
| 3579 | HBoxContainer *type_list_hb = memnew(HBoxContainer); |
| 3580 | main_vb->add_child(type_list_hb); |
| 3581 | |
| 3582 | Label *type_list_label = memnew(Label); |
| 3583 | type_list_label->set_text(TTR("Type:")); |
| 3584 | type_list_hb->add_child(type_list_label); |
| 3585 | |
| 3586 | theme_type_list = memnew(OptionButton); |
| 3587 | theme_type_list->set_h_size_flags(SIZE_EXPAND_FILL); |
| 3588 | theme_type_list->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 3589 | theme_type_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 3590 | theme_type_list->set_accessibility_name(TTRC("Type")); |
| 3591 | type_list_hb->add_child(theme_type_list); |
| 3592 | theme_type_list->connect(SceneStringName(item_selected), callable_mp(this, &ThemeTypeEditor::_list_type_selected)); |
| 3593 | |
| 3594 | add_type_button = memnew(Button); |
| 3595 | add_type_button->set_tooltip_text(TTR("Add a type from a list of available types or create a new one.")); |
| 3596 | type_list_hb->add_child(add_type_button); |
| 3597 | add_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_add_type_button_cbk)); |
| 3598 | |
| 3599 | rename_type_button = memnew(Button); |
| 3600 | rename_type_button->set_disabled(true); |
| 3601 | rename_type_button->set_tooltip_text(TTRC("Rename current type.")); |
| 3602 | type_list_hb->add_child(rename_type_button); |
| 3603 | rename_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_rename_type_button_cbk)); |
| 3604 | |
| 3605 | theme_type_rename_dialog = memnew(ConfirmationDialog); |
| 3606 | theme_type_rename_dialog->set_title(TTRC("Rename Theme Type")); |
| 3607 | theme_type_rename_dialog->set_min_size(Size2(256, 64) * EDSCALE); |
| 3608 | add_child(theme_type_rename_dialog); |
| 3609 | theme_type_rename_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ThemeTypeEditor::_theme_type_rename_dialog_confirmed)); |
| 3610 | |
| 3611 | theme_type_rename_line_edit = memnew(LineEdit); |
| 3612 | theme_type_rename_line_edit->set_select_all_on_focus(true); |
| 3613 | theme_type_rename_dialog->add_child(theme_type_rename_line_edit); |
| 3614 | theme_type_rename_dialog->register_text_enter(theme_type_rename_line_edit); |
| 3615 | |
| 3616 | remove_type_button = memnew(Button); |
| 3617 | remove_type_button->set_disabled(true); |
| 3618 | remove_type_button->set_tooltip_text(TTRC("Remove current type.")); |
| 3619 | type_list_hb->add_child(remove_type_button); |
| 3620 | remove_type_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_remove_type_button_cbk)); |
| 3621 | |
| 3622 | HBoxContainer *type_controls = memnew(HBoxContainer); |
| 3623 | main_vb->add_child(type_controls); |
| 3624 | |
| 3625 | show_default_items_button = memnew(CheckButton); |
| 3626 | show_default_items_button->set_h_size_flags(SIZE_EXPAND_FILL); |
| 3627 | show_default_items_button->set_text(TTR("Show Default")); |
| 3628 | show_default_items_button->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR); |
| 3629 | show_default_items_button->set_tooltip_text(TTR("Show default type items alongside items that have been overridden.")); |
| 3630 | show_default_items_button->set_pressed(true); |
| 3631 | type_controls->add_child(show_default_items_button); |
| 3632 | show_default_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_update_type_items)); |
| 3633 | |
| 3634 | Button *add_default_items_button = memnew(Button); |
| 3635 | add_default_items_button->set_h_size_flags(SIZE_EXPAND_FILL); |
| 3636 | add_default_items_button->set_text(TTR("Override All")); |
| 3637 | add_default_items_button->set_tooltip_text(TTR("Override all default type items.")); |
| 3638 | type_controls->add_child(add_default_items_button); |
| 3639 | add_default_items_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_add_default_type_items)); |
| 3640 | |
| 3641 | data_type_tabs = memnew(TabContainer); |
| 3642 | data_type_tabs->set_tab_alignment(TabBar::ALIGNMENT_CENTER); |
| 3643 | main_vb->add_child(data_type_tabs); |
| 3644 | data_type_tabs->set_v_size_flags(SIZE_EXPAND_FILL); |
| 3645 | data_type_tabs->set_use_hidden_tabs_for_min_size(true); |
| 3646 | |
| 3647 | color_items_list = _create_item_list(Theme::DATA_TYPE_COLOR); |
| 3648 | constant_items_list = _create_item_list(Theme::DATA_TYPE_CONSTANT); |
| 3649 | font_items_list = _create_item_list(Theme::DATA_TYPE_FONT); |
| 3650 | font_size_items_list = _create_item_list(Theme::DATA_TYPE_FONT_SIZE); |
| 3651 | icon_items_list = _create_item_list(Theme::DATA_TYPE_ICON); |
| 3652 | stylebox_items_list = _create_item_list(Theme::DATA_TYPE_STYLEBOX); |
| 3653 | |
| 3654 | VBoxContainer *type_settings_tab = memnew(VBoxContainer); |
| 3655 | type_settings_tab->set_custom_minimum_size(Size2(0, 160) * EDSCALE); |
| 3656 | data_type_tabs->add_child(type_settings_tab); |
| 3657 | data_type_tabs->set_tab_title(data_type_tabs->get_tab_count() - 1, ""); |
| 3658 | |
| 3659 | ScrollContainer *type_settings_sc = memnew(ScrollContainer); |
| 3660 | type_settings_sc->set_v_size_flags(SIZE_EXPAND_FILL); |
| 3661 | type_settings_sc->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED); |
| 3662 | type_settings_tab->add_child(type_settings_sc); |
| 3663 | VBoxContainer *type_settings_list = memnew(VBoxContainer); |
| 3664 | type_settings_list->set_h_size_flags(SIZE_EXPAND_FILL); |
| 3665 | type_settings_sc->add_child(type_settings_list); |
| 3666 | |
| 3667 | VBoxContainer *type_variation_vb = memnew(VBoxContainer); |
| 3668 | type_settings_list->add_child(type_variation_vb); |
| 3669 | |
| 3670 | HBoxContainer *type_variation_hb = memnew(HBoxContainer); |
| 3671 | type_variation_vb->add_child(type_variation_hb); |
| 3672 | Label *type_variation_label = memnew(Label); |
| 3673 | type_variation_hb->add_child(type_variation_label); |
| 3674 | type_variation_label->set_text(TTR("Base Type")); |
| 3675 | type_variation_label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 3676 | type_variation_edit = memnew(LineEdit); |
| 3677 | type_variation_hb->add_child(type_variation_edit); |
| 3678 | type_variation_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 3679 | type_variation_edit->connect(SceneStringName(text_changed), callable_mp(this, &ThemeTypeEditor::_type_variation_changed)); |
| 3680 | type_variation_edit->connect(SceneStringName(focus_exited), callable_mp(this, &ThemeTypeEditor::_update_type_items)); |
| 3681 | type_variation_edit->set_accessibility_name(TTRC("Base Type")); |
| 3682 | type_variation_button = memnew(Button); |
| 3683 | type_variation_hb->add_child(type_variation_button); |
| 3684 | type_variation_button->set_tooltip_text(TTR("Select the variation base type from a list of available types.")); |
| 3685 | type_variation_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeTypeEditor::_add_type_variation_cbk)); |
| 3686 | |
| 3687 | type_variation_locked = memnew(Label); |
| 3688 | type_variation_vb->add_child(type_variation_locked); |
| 3689 | type_variation_locked->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 3690 | type_variation_locked->set_autowrap_mode(TextServer::AUTOWRAP_WORD); |
| 3691 | type_variation_locked->set_text(TTR("A type associated with a built-in class cannot be marked as a variation of another type.")); |
| 3692 | type_variation_locked->hide(); |
| 3693 | |
| 3694 | add_type_dialog = memnew(ThemeTypeDialog); |
| 3695 | add_child(add_type_dialog); |
| 3696 | add_type_dialog->connect("type_selected", callable_mp(this, &ThemeTypeEditor::_add_type_dialog_selected)); |
| 3697 | |
| 3698 | update_debounce_timer = memnew(Timer); |
| 3699 | update_debounce_timer->set_one_shot(true); |
| 3700 | update_debounce_timer->set_wait_time(0.5); |
| 3701 | update_debounce_timer->connect("timeout", callable_mp(this, &ThemeTypeEditor::_update_type_list)); |
| 3702 | add_child(update_debounce_timer); |
| 3703 | } |
| 3704 | |
| 3705 | /////////////////////// |
| 3706 | |
| 3707 | void ThemeEditor::edit(const Ref<Theme> &p_theme) { |
| 3708 | if (theme == p_theme) { |
| 3709 | return; |
| 3710 | } |
| 3711 | |
| 3712 | theme = p_theme; |
| 3713 | theme_type_editor->set_edited_theme(p_theme); |
| 3714 | theme_edit_dialog->set_edited_theme(p_theme); |
| 3715 | |
| 3716 | for (int i = 0; i < preview_tabs_content->get_child_count(); i++) { |
| 3717 | ThemeEditorPreview *preview_tab = Object::cast_to<ThemeEditorPreview>(preview_tabs_content->get_child(i)); |
| 3718 | if (!preview_tab) { |
| 3719 | continue; |
| 3720 | } |
| 3721 | |
| 3722 | preview_tab->set_preview_theme(p_theme); |
| 3723 | } |
| 3724 | |
| 3725 | if (theme.is_valid()) { |
| 3726 | _update_theme_name(theme->get_path().get_file()); |
| 3727 | } |
| 3728 | } |
| 3729 | |
| 3730 | Ref<Theme> ThemeEditor::get_edited_theme() { |
| 3731 | return theme; |
| 3732 | } |
| 3733 | |
| 3734 | void ThemeEditor::_theme_save_button_cbk(bool p_save_as) { |
| 3735 | ERR_FAIL_COND_MSG(theme.is_null(), "Invalid state of the Theme Editor; the Theme resource is missing."); |
| 3736 | |
| 3737 | if (p_save_as) { |
| 3738 | EditorNode::get_singleton()->save_resource_as(theme); |
| 3739 | } else { |
| 3740 | EditorNode::get_singleton()->save_resource(theme); |
| 3741 | } |
| 3742 | } |
| 3743 | |
| 3744 | void ThemeEditor::_theme_edit_button_cbk() { |
| 3745 | theme_edit_dialog->popup_centered_clamped(Size2(850, 700) * EDSCALE, 0.8); |
| 3746 | } |
| 3747 | |
| 3748 | void ThemeEditor::_theme_close_button_cbk() { |
| 3749 | close(); |
| 3750 | _dock_closed_cbk(); |
| 3751 | } |
| 3752 | |
| 3753 | void ThemeEditor::_dock_closed_cbk() { |
| 3754 | if (theme.is_valid() && InspectorDock::get_inspector_singleton()->get_edited_object() == theme.ptr()) { |
| 3755 | EditorNode::get_singleton()->push_item(nullptr); |
| 3756 | } |
| 3757 | theme = Ref<Theme>(); |
| 3758 | } |
| 3759 | |
| 3760 | void ThemeEditor::_scene_closed(const String &p_path) { |
| 3761 | if (theme.is_valid() && theme->is_built_in() && theme->get_path().get_slice("::", 0) == p_path) { |
| 3762 | theme = Ref<Theme>(); |
| 3763 | EditorNode::get_singleton()->hide_unused_editors(plugin); |
| 3764 | } |
| 3765 | } |
| 3766 | |
| 3767 | void ThemeEditor::_resource_saved(const Ref<Resource> &p_resource) { |
| 3768 | if (theme.is_valid() && theme == p_resource) { |
| 3769 | _update_theme_name(theme->get_path().get_file()); |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | void ThemeEditor::_files_moved(const String &p_old_path, const String &p_new_path) { |
| 3774 | // Theme's path may not have been updated to new path yet - need to check both old and new. |
| 3775 | if (theme.is_valid() && (theme->get_path() == p_old_path || theme->get_path() == p_new_path)) { |
| 3776 | _update_theme_name(p_new_path.get_file()); |
| 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | void ThemeEditor::_update_theme_name(const String &p_name) { |
| 3781 | theme_name->set_text(p_name); |
| 3782 | theme_name->set_tooltip_text(p_name); |
| 3783 | |
| 3784 | int label_min_width = theme_name->get_minimum_size().x + theme_name->get_character_bounds(0).size.x; |
| 3785 | theme_name->set_custom_minimum_size(Size2(label_min_width, 0)); |
| 3786 | } |
| 3787 | |
| 3788 | void ThemeEditor::_add_preview_button_cbk() { |
| 3789 | preview_scene_dialog->popup_file_dialog(); |
| 3790 | } |
| 3791 | |
| 3792 | void ThemeEditor::_preview_scene_dialog_cbk(const String &p_path) { |
| 3793 | SceneThemeEditorPreview *preview_tab = memnew(SceneThemeEditorPreview); |
| 3794 | if (!preview_tab->set_preview_scene(p_path)) { |
| 3795 | memdelete(preview_tab); |
| 3796 | return; |
| 3797 | } |
| 3798 | |
| 3799 | _add_preview_tab(preview_tab, p_path.get_file(), get_editor_theme_icon(SNAME("PackedScene"))); |
| 3800 | preview_tab->connect("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid).bind(preview_tab)); |
| 3801 | preview_tab->connect("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab).bind(preview_tab)); |
| 3802 | } |
| 3803 | |
| 3804 | void ThemeEditor::_add_preview_tab(ThemeEditorPreview *p_preview_tab, const String &p_preview_name, const Ref<Texture2D> &p_icon) { |
| 3805 | p_preview_tab->set_preview_theme(theme); |
| 3806 | |
| 3807 | preview_tabs->add_tab(p_preview_name, p_icon); |
| 3808 | preview_tabs_content->add_child(p_preview_tab); |
| 3809 | preview_tabs->set_tab_button_icon(preview_tabs->get_tab_count() - 1, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("close"), SNAME("TabBar"))); |
| 3810 | p_preview_tab->connect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked)); |
| 3811 | |
| 3812 | preview_tabs->set_current_tab(preview_tabs->get_tab_count() - 1); |
| 3813 | _preview_tabs_resized(); |
| 3814 | } |
| 3815 | |
| 3816 | void ThemeEditor::_change_preview_tab(int p_tab) { |
| 3817 | ERR_FAIL_INDEX_MSG(p_tab, preview_tabs_content->get_child_count(), "Attempting to open a preview tab that doesn't exist."); |
| 3818 | |
| 3819 | for (int i = 0; i < preview_tabs_content->get_child_count(); i++) { |
| 3820 | Control *c = Object::cast_to<Control>(preview_tabs_content->get_child(i)); |
| 3821 | if (!c) { |
| 3822 | continue; |
| 3823 | } |
| 3824 | |
| 3825 | c->set_visible(i == p_tab); |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | void ThemeEditor::_remove_preview_tab(int p_tab) { |
| 3830 | ERR_FAIL_INDEX_MSG(p_tab, preview_tabs_content->get_child_count(), "Attempting to remove a preview tab that doesn't exist."); |
| 3831 | |
| 3832 | ThemeEditorPreview *preview_tab = Object::cast_to<ThemeEditorPreview>(preview_tabs_content->get_child(p_tab)); |
| 3833 | ERR_FAIL_COND_MSG(Object::cast_to<DefaultThemeEditorPreview>(preview_tab), "Attemptying to remove the default preview tab."); |
| 3834 | |
| 3835 | if (preview_tab) { |
| 3836 | preview_tab->disconnect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked)); |
| 3837 | if (preview_tab->is_connected("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid))) { |
| 3838 | preview_tab->disconnect("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid)); |
| 3839 | } |
| 3840 | if (preview_tab->is_connected("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab))) { |
| 3841 | preview_tab->disconnect("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab)); |
| 3842 | } |
| 3843 | |
| 3844 | preview_tabs_content->remove_child(preview_tab); |
| 3845 | preview_tab->queue_free(); |
| 3846 | |
| 3847 | preview_tabs->remove_tab(p_tab); |
| 3848 | _change_preview_tab(preview_tabs->get_current_tab()); |
| 3849 | _preview_tabs_resized(); |
| 3850 | } |
| 3851 | } |
| 3852 | |
| 3853 | void ThemeEditor::_remove_preview_tab_invalid(Node *p_tab_control) { |
| 3854 | int tab_index = p_tab_control->get_index(); |
| 3855 | _remove_preview_tab(tab_index); |
| 3856 | } |
| 3857 | |
| 3858 | void ThemeEditor::_update_preview_tab(Node *p_tab_control) { |
| 3859 | if (!Object::cast_to<SceneThemeEditorPreview>(p_tab_control)) { |
| 3860 | return; |
| 3861 | } |
| 3862 | |
| 3863 | int tab_index = p_tab_control->get_index(); |
| 3864 | SceneThemeEditorPreview *scene_preview = Object::cast_to<SceneThemeEditorPreview>(p_tab_control); |
| 3865 | preview_tabs->set_tab_title(tab_index, scene_preview->get_preview_scene_path().get_file()); |
| 3866 | |
| 3867 | _preview_tabs_resized(); |
| 3868 | } |
| 3869 | |
| 3870 | void ThemeEditor::_preview_control_picked(String p_class_name) { |
| 3871 | theme_type_editor->select_type(p_class_name); |
| 3872 | } |
| 3873 | |
| 3874 | bool ThemeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
| 3875 | const Dictionary d = p_data; |
| 3876 | if (!d.has("type")) { |
| 3877 | return false; |
| 3878 | } |
| 3879 | |
| 3880 | if (String(d["type"]) == "files") { |
| 3881 | const Vector<String> files = d["files"]; |
| 3882 | |
| 3883 | if (files.size() != 1) { |
| 3884 | return false; |
| 3885 | } |
| 3886 | |
| 3887 | const String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]); |
| 3888 | return ftype == "PackedScene"; |
| 3889 | } |
| 3890 | return false; |
| 3891 | } |
| 3892 | |
| 3893 | void ThemeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
| 3894 | Dictionary d = p_data; |
| 3895 | Vector<String> files = d["files"]; |
| 3896 | const String &path = files[0]; |
| 3897 | |
| 3898 | SceneThemeEditorPreview *preview_tab = memnew(SceneThemeEditorPreview); |
| 3899 | if (!preview_tab->set_preview_scene(path)) { |
| 3900 | memdelete(preview_tab); |
| 3901 | return; |
| 3902 | } |
| 3903 | |
| 3904 | Ref<Texture2D> icon = get_editor_theme_icon(SNAME("PackedScene")); |
| 3905 | |
| 3906 | preview_tab->set_preview_theme(theme); |
| 3907 | |
| 3908 | preview_tabs->add_tab(path.get_file(), icon); |
| 3909 | preview_tabs_content->add_child(preview_tab); |
| 3910 | preview_tabs->set_tab_button_icon(preview_tabs->get_tab_count() - 1, EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("close"), SNAME("TabBar"))); |
| 3911 | preview_tab->connect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked)); |
| 3912 | |
| 3913 | preview_tabs->set_current_tab(preview_tabs->get_tab_count() - 1); |
| 3914 | preview_tab->connect("scene_invalidated", callable_mp(this, &ThemeEditor::_remove_preview_tab_invalid).bind(preview_tab)); |
| 3915 | preview_tab->connect("scene_reloaded", callable_mp(this, &ThemeEditor::_update_preview_tab).bind(preview_tab)); |
| 3916 | } |
| 3917 | |
| 3918 | void ThemeEditor::_preview_tabs_resized() { |
| 3919 | const Size2 add_button_size = Size2(add_preview_button->get_size().x, preview_tabs->get_size().y); |
| 3920 | if (preview_tabs->get_offset_buttons_visible()) { |
| 3921 | // Move the add button to a fixed position. |
| 3922 | if (add_preview_button->get_parent() == preview_tabs) { |
| 3923 | add_preview_button->reparent(add_preview_button_ph); |
| 3924 | add_preview_button->set_rect(Rect2(Point2(), add_button_size)); |
| 3925 | } |
| 3926 | } else { |
| 3927 | // Move the add button to be after the last tab. |
| 3928 | if (add_preview_button->get_parent() == add_preview_button_ph) { |
| 3929 | add_preview_button->reparent(preview_tabs); |
| 3930 | } |
| 3931 | |
| 3932 | Rect2 last_tab = preview_tabs->get_tab_rect(preview_tabs->get_tab_count() - 1); |
| 3933 | int hsep = preview_tabs->get_theme_constant(SNAME("h_separation")); |
| 3934 | if (preview_tabs->is_layout_rtl()) { |
| 3935 | add_preview_button->set_rect(Rect2(Point2(last_tab.position.x - add_button_size.x - hsep, last_tab.position.y), add_button_size)); |
| 3936 | } else { |
| 3937 | add_preview_button->set_rect(Rect2(Point2(last_tab.position.x + last_tab.size.width + hsep, last_tab.position.y), add_button_size)); |
| 3938 | } |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | void ThemeEditor::_notification(int p_what) { |
| 3943 | switch (p_what) { |
| 3944 | case NOTIFICATION_READY: { |
| 3945 | connect("closed", callable_mp(this, &ThemeEditor::_dock_closed_cbk)); |
| 3946 | EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ThemeEditor::_scene_closed)); |
| 3947 | EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ThemeEditor::_resource_saved)); |
| 3948 | FileSystemDock::get_singleton()->connect("files_moved", callable_mp(this, &ThemeEditor::_files_moved)); |
| 3949 | } break; |
| 3950 | |
| 3951 | case NOTIFICATION_THEME_CHANGED: { |
| 3952 | theme_edit_button->set_button_icon(get_editor_theme_icon(SNAME("Tools"))); |
| 3953 | theme_close_button->set_button_icon(get_editor_theme_icon(SNAME("Close"))); |
| 3954 | |
| 3955 | if (EDITOR_GET("interface/theme/style") == "Classic") { |
| 3956 | preview_tabs->add_theme_style_override("tab_selected", get_theme_stylebox(SNAME("ThemeEditorPreviewFG"), EditorStringName(EditorStyles))); |
| 3957 | preview_tabs->add_theme_style_override("tab_unselected", get_theme_stylebox(SNAME("ThemeEditorPreviewBG"), EditorStringName(EditorStyles))); |
| 3958 | preview_tabs_content->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("TabContainerOdd"))); |
| 3959 | } |
| 3960 | |
| 3961 | add_preview_button->set_button_icon(get_editor_theme_icon(SNAME("Add"))); |
| 3962 | add_preview_button_ph->set_custom_minimum_size(add_preview_button->get_minimum_size()); |
| 3963 | |
| 3964 | int label_min_width = theme_name->get_minimum_size().x + theme_name->get_character_bounds(0).size.x; |
| 3965 | theme_name->set_custom_minimum_size(Size2(label_min_width, 0)); |
| 3966 | } break; |
| 3967 | |
| 3968 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
| 3969 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 3970 | _preview_tabs_resized(); |
| 3971 | } break; |
| 3972 | } |
| 3973 | } |
| 3974 | |
| 3975 | ThemeEditor::ThemeEditor() { |
| 3976 | set_name(TTRC("Theme")); |
| 3977 | set_icon_name("ThemeDock"); |
| 3978 | set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_theme_bottom_panel", TTRC("Toggle Theme Dock"))); |
| 3979 | set_default_slot(EditorDock::DOCK_SLOT_BOTTOM); |
| 3980 | set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING); |
| 3981 | set_global(false); |
| 3982 | set_transient(true); |
| 3983 | set_closable(true); |
| 3984 | set_custom_minimum_size(Size2(0, 200 * EDSCALE)); |
| 3985 | |
| 3986 | VBoxContainer *content_vb = memnew(VBoxContainer); |
| 3987 | add_child(content_vb); |
| 3988 | |
| 3989 | HBoxContainer *top_menu = memnew(HBoxContainer); |
| 3990 | content_vb->add_child(top_menu); |
| 3991 | |
| 3992 | Label *theme_label = memnew(Label); |
| 3993 | theme_label->set_text(TTRC("Theme:")); |
| 3994 | top_menu->add_child(theme_label); |
| 3995 | |
| 3996 | theme_name = memnew(Label); |
| 3997 | theme_name->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 3998 | theme_name->set_theme_type_variation("HeaderSmall"); |
| 3999 | theme_name->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 4000 | theme_name->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 4001 | theme_name->set_mouse_filter(Control::MOUSE_FILTER_PASS); |
| 4002 | top_menu->add_child(theme_name); |
| 4003 | |
| 4004 | theme_edit_button = memnew(Button); |
| 4005 | theme_edit_button->set_text(TTRC("Manage Items...")); |
| 4006 | theme_edit_button->set_tooltip_text(TTRC("Add, remove, organize, and import Theme items.")); |
| 4007 | theme_edit_button->set_flat(true); |
| 4008 | theme_edit_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_edit_button_cbk)); |
| 4009 | top_menu->add_child(theme_edit_button); |
| 4010 | |
| 4011 | top_menu->add_child(memnew(VSeparator)); |
| 4012 | |
| 4013 | Button *theme_save_button = memnew(Button); |
| 4014 | theme_save_button->set_text(TTR("Save")); |
| 4015 | theme_save_button->set_flat(true); |
| 4016 | theme_save_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_save_button_cbk).bind(false)); |
| 4017 | top_menu->add_child(theme_save_button); |
| 4018 | |
| 4019 | Button *theme_save_as_button = memnew(Button); |
| 4020 | theme_save_as_button->set_text(TTR("Save As...")); |
| 4021 | theme_save_as_button->set_flat(true); |
| 4022 | theme_save_as_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_save_button_cbk).bind(true)); |
| 4023 | top_menu->add_child(theme_save_as_button); |
| 4024 | |
| 4025 | top_menu->add_child(memnew(VSeparator)); |
| 4026 | |
| 4027 | theme_close_button = memnew(Button); |
| 4028 | theme_close_button->set_tooltip_text(TTRC("Close")); |
| 4029 | theme_close_button->set_flat(true); |
| 4030 | theme_close_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_theme_close_button_cbk)); |
| 4031 | top_menu->add_child(theme_close_button); |
| 4032 | |
| 4033 | theme_type_editor = memnew(ThemeTypeEditor); |
| 4034 | |
| 4035 | theme_edit_dialog = memnew(ThemeItemEditorDialog(theme_type_editor)); |
| 4036 | theme_edit_dialog->hide(); |
| 4037 | top_menu->add_child(theme_edit_dialog); |
| 4038 | |
| 4039 | HSplitContainer *main_hs = memnew(HSplitContainer); |
| 4040 | main_hs->set_v_size_flags(SIZE_EXPAND_FILL); |
| 4041 | content_vb->add_child(main_hs); |
| 4042 | |
| 4043 | main_hs->set_split_offset(520 * EDSCALE); |
| 4044 | |
| 4045 | VBoxContainer *preview_tabs_vb = memnew(VBoxContainer); |
| 4046 | preview_tabs_vb->set_h_size_flags(SIZE_EXPAND_FILL); |
| 4047 | preview_tabs_vb->add_theme_constant_override("separation", 2 * EDSCALE); |
| 4048 | main_hs->add_child(preview_tabs_vb); |
| 4049 | |
| 4050 | PanelContainer *tabs_panel = memnew(PanelContainer); |
| 4051 | tabs_panel->set_h_size_flags(SIZE_EXPAND_FILL); |
| 4052 | tabs_panel->set_theme_type_variation("PanelContainerTabbarInner"); |
| 4053 | preview_tabs_vb->add_child(tabs_panel); |
| 4054 | |
| 4055 | HBoxContainer *preview_tabbar_hb = memnew(HBoxContainer); |
| 4056 | tabs_panel->add_child(preview_tabbar_hb); |
| 4057 | |
| 4058 | preview_tabs_content = memnew(PanelContainer); |
| 4059 | preview_tabs_content->set_v_size_flags(SIZE_EXPAND_FILL); |
| 4060 | preview_tabs_content->set_draw_behind_parent(true); |
| 4061 | preview_tabs_vb->add_child(preview_tabs_content); |
| 4062 | |
| 4063 | preview_tabs = memnew(TabBar); |
| 4064 | preview_tabs->set_h_size_flags(SIZE_EXPAND_FILL); |
| 4065 | preview_tabs->set_theme_type_variation("TabContainerInner"); |
| 4066 | preview_tabbar_hb->add_child(preview_tabs); |
| 4067 | preview_tabs->connect("tab_changed", callable_mp(this, &ThemeEditor::_change_preview_tab)); |
| 4068 | preview_tabs->connect("tab_button_pressed", callable_mp(this, &ThemeEditor::_remove_preview_tab)); |
| 4069 | preview_tabs->connect(SceneStringName(resized), callable_mp(this, &ThemeEditor::_preview_tabs_resized), CONNECT_DEFERRED); |
| 4070 | |
| 4071 | add_preview_button = memnew(Button); |
| 4072 | add_preview_button->set_tooltip_text(TTRC("Add Preview")); |
| 4073 | add_preview_button->set_flat(true); |
| 4074 | preview_tabs->add_child(add_preview_button); |
| 4075 | add_preview_button->connect(SceneStringName(pressed), callable_mp(this, &ThemeEditor::_add_preview_button_cbk)); |
| 4076 | |
| 4077 | add_preview_button_ph = memnew(Control); |
| 4078 | add_preview_button_ph->set_mouse_filter(Control::MOUSE_FILTER_IGNORE); |
| 4079 | add_preview_button_ph->set_custom_minimum_size(add_preview_button->get_minimum_size()); |
| 4080 | preview_tabbar_hb->add_child(add_preview_button_ph); |
| 4081 | |
| 4082 | DefaultThemeEditorPreview *default_preview_tab = memnew(DefaultThemeEditorPreview); |
| 4083 | preview_tabs_content->add_child(default_preview_tab); |
| 4084 | default_preview_tab->connect("control_picked", callable_mp(this, &ThemeEditor::_preview_control_picked)); |
| 4085 | preview_tabs->add_tab(TTR("Default Preview")); |
| 4086 | |
| 4087 | preview_scene_dialog = memnew(EditorFileDialog); |
| 4088 | preview_scene_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); |
| 4089 | preview_scene_dialog->set_title(TTR("Select UI Scene:")); |
| 4090 | List<String> ext; |
| 4091 | ResourceLoader::get_recognized_extensions_for_type("PackedScene", &ext); |
| 4092 | for (const String &E : ext) { |
| 4093 | preview_scene_dialog->add_filter("*." + E, TTR("Scene")); |
| 4094 | } |
| 4095 | main_hs->add_child(preview_scene_dialog); |
| 4096 | preview_scene_dialog->connect("file_selected", callable_mp(this, &ThemeEditor::_preview_scene_dialog_cbk)); |
| 4097 | |
| 4098 | main_hs->add_child(theme_type_editor); |
| 4099 | theme_type_editor->set_custom_minimum_size(Size2(280, 0) * EDSCALE); |
| 4100 | |
| 4101 | SET_DRAG_FORWARDING_CD(top_menu, ThemeEditor); |
| 4102 | SET_DRAG_FORWARDING_CD(preview_tabs, ThemeEditor); |
| 4103 | } |
| 4104 | |
| 4105 | /////////////////////// |
| 4106 | |
| 4107 | void ThemeEditorPlugin::edit(Object *p_object) { |
| 4108 | theme_editor->edit(Ref<Theme>(p_object)); |
| 4109 | } |
| 4110 | |
| 4111 | bool ThemeEditorPlugin::handles(Object *p_object) const { |
| 4112 | return Object::cast_to<Theme>(p_object) != nullptr; |
| 4113 | } |
| 4114 | |
| 4115 | void ThemeEditorPlugin::make_visible(bool p_visible) { |
| 4116 | if (p_visible) { |
| 4117 | theme_editor->make_visible(); |
| 4118 | } else { |
| 4119 | theme_editor->close(); |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | bool ThemeEditorPlugin::can_auto_hide() const { |
| 4124 | return theme_editor->theme.is_null(); |
| 4125 | } |
| 4126 | |
| 4127 | ThemeEditorPlugin::ThemeEditorPlugin() { |
| 4128 | theme_editor = memnew(ThemeEditor); |
| 4129 | theme_editor->plugin = this; |
| 4130 | EditorDockManager::get_singleton()->add_dock(theme_editor); |
| 4131 | theme_editor->close(); |
| 4132 | } |