rename_dialog.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*************************************************************************/
  2. /* rename_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "rename_dialog.h"
  31. #include "core/string/print_string.h"
  32. #include "editor_node.h"
  33. #include "editor_scale.h"
  34. #include "editor_settings.h"
  35. #include "editor_themes.h"
  36. #include "modules/regex/regex.h"
  37. #include "plugins/script_editor_plugin.h"
  38. #include "scene/gui/control.h"
  39. #include "scene/gui/label.h"
  40. #include "scene/gui/tab_container.h"
  41. RenameDialog::RenameDialog(SceneTreeEditor *p_scene_tree_editor, UndoRedo *p_undo_redo) {
  42. scene_tree_editor = p_scene_tree_editor;
  43. undo_redo = p_undo_redo;
  44. preview_node = nullptr;
  45. set_title(TTR("Batch Rename"));
  46. VBoxContainer *vbc = memnew(VBoxContainer);
  47. add_child(vbc);
  48. // -- Search/Replace Area
  49. GridContainer *grd_main = memnew(GridContainer);
  50. grd_main->set_columns(2);
  51. grd_main->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  52. vbc->add_child(grd_main);
  53. // ---- 1st & 2nd row
  54. Label *lbl_search = memnew(Label);
  55. lbl_search->set_text(TTR("Search:"));
  56. lne_search = memnew(LineEdit);
  57. lne_search->set_name("lne_search");
  58. lne_search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  59. Label *lbl_replace = memnew(Label);
  60. lbl_replace->set_text(TTR("Replace:"));
  61. lne_replace = memnew(LineEdit);
  62. lne_replace->set_name("lne_replace");
  63. lne_replace->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  64. grd_main->add_child(lbl_search);
  65. grd_main->add_child(lbl_replace);
  66. grd_main->add_child(lne_search);
  67. grd_main->add_child(lne_replace);
  68. // ---- 3rd & 4th row
  69. Label *lbl_prefix = memnew(Label);
  70. lbl_prefix->set_text(TTR("Prefix:"));
  71. lne_prefix = memnew(LineEdit);
  72. lne_prefix->set_name("lne_prefix");
  73. lne_prefix->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  74. Label *lbl_suffix = memnew(Label);
  75. lbl_suffix->set_text(TTR("Suffix:"));
  76. lne_suffix = memnew(LineEdit);
  77. lne_suffix->set_name("lne_suffix");
  78. lne_suffix->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  79. grd_main->add_child(lbl_prefix);
  80. grd_main->add_child(lbl_suffix);
  81. grd_main->add_child(lne_prefix);
  82. grd_main->add_child(lne_suffix);
  83. // -- Feature Tabs
  84. cbut_regex = memnew(CheckButton);
  85. cbut_regex->set_text(TTR("Use Regular Expressions"));
  86. vbc->add_child(cbut_regex);
  87. CheckButton *cbut_collapse_features = memnew(CheckButton);
  88. cbut_collapse_features->set_text(TTR("Advanced Options"));
  89. vbc->add_child(cbut_collapse_features);
  90. tabc_features = memnew(TabContainer);
  91. tabc_features->set_tab_align(TabContainer::ALIGN_LEFT);
  92. tabc_features->set_use_hidden_tabs_for_min_size(true);
  93. vbc->add_child(tabc_features);
  94. // ---- Tab Substitute
  95. VBoxContainer *vbc_substitute = memnew(VBoxContainer);
  96. vbc_substitute->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  97. vbc_substitute->set_name(TTR("Substitute"));
  98. tabc_features->add_child(vbc_substitute);
  99. cbut_substitute = memnew(CheckBox);
  100. cbut_substitute->set_text(TTR("Substitute"));
  101. vbc_substitute->add_child(cbut_substitute);
  102. GridContainer *grd_substitute = memnew(GridContainer);
  103. grd_substitute->set_columns(3);
  104. vbc_substitute->add_child(grd_substitute);
  105. // Name
  106. but_insert_name = memnew(Button);
  107. but_insert_name->set_text("NAME");
  108. but_insert_name->set_tooltip(String("${NAME}\n") + TTR("Node name."));
  109. but_insert_name->set_focus_mode(Control::FOCUS_NONE);
  110. but_insert_name->connect("pressed", callable_mp(this, &RenameDialog::_insert_text), make_binds("${NAME}"));
  111. but_insert_name->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  112. grd_substitute->add_child(but_insert_name);
  113. // Parent
  114. but_insert_parent = memnew(Button);
  115. but_insert_parent->set_text("PARENT");
  116. but_insert_parent->set_tooltip(String("${PARENT}\n") + TTR("Node's parent name, if available."));
  117. but_insert_parent->set_focus_mode(Control::FOCUS_NONE);
  118. but_insert_parent->connect("pressed", callable_mp(this, &RenameDialog::_insert_text), make_binds("${PARENT}"));
  119. but_insert_parent->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  120. grd_substitute->add_child(but_insert_parent);
  121. // Type
  122. but_insert_type = memnew(Button);
  123. but_insert_type->set_text("TYPE");
  124. but_insert_type->set_tooltip(String("${TYPE}\n") + TTR("Node type."));
  125. but_insert_type->set_focus_mode(Control::FOCUS_NONE);
  126. but_insert_type->connect("pressed", callable_mp(this, &RenameDialog::_insert_text), make_binds("${TYPE}"));
  127. but_insert_type->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  128. grd_substitute->add_child(but_insert_type);
  129. // Scene
  130. but_insert_scene = memnew(Button);
  131. but_insert_scene->set_text("SCENE");
  132. but_insert_scene->set_tooltip(String("${SCENE}\n") + TTR("Current scene name."));
  133. but_insert_scene->set_focus_mode(Control::FOCUS_NONE);
  134. but_insert_scene->connect("pressed", callable_mp(this, &RenameDialog::_insert_text), make_binds("${SCENE}"));
  135. but_insert_scene->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  136. grd_substitute->add_child(but_insert_scene);
  137. // Root
  138. but_insert_root = memnew(Button);
  139. but_insert_root->set_text("ROOT");
  140. but_insert_root->set_tooltip(String("${ROOT}\n") + TTR("Root node name."));
  141. but_insert_root->set_focus_mode(Control::FOCUS_NONE);
  142. but_insert_root->connect("pressed", callable_mp(this, &RenameDialog::_insert_text), make_binds("${ROOT}"));
  143. but_insert_root->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  144. grd_substitute->add_child(but_insert_root);
  145. // Count
  146. but_insert_count = memnew(Button);
  147. but_insert_count->set_text("COUNTER");
  148. but_insert_count->set_tooltip(String("${COUNTER}\n") + TTR("Sequential integer counter.\nCompare counter options."));
  149. but_insert_count->set_focus_mode(Control::FOCUS_NONE);
  150. but_insert_count->connect("pressed", callable_mp(this, &RenameDialog::_insert_text), make_binds("${COUNTER}"));
  151. but_insert_count->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  152. grd_substitute->add_child(but_insert_count);
  153. chk_per_level_counter = memnew(CheckBox);
  154. chk_per_level_counter->set_text(TTR("Per-level Counter"));
  155. chk_per_level_counter->set_tooltip(TTR("If set, the counter restarts for each group of child nodes."));
  156. vbc_substitute->add_child(chk_per_level_counter);
  157. HBoxContainer *hbc_count_options = memnew(HBoxContainer);
  158. vbc_substitute->add_child(hbc_count_options);
  159. Label *lbl_count_start = memnew(Label);
  160. lbl_count_start->set_text(TTR("Start"));
  161. lbl_count_start->set_tooltip(TTR("Initial value for the counter."));
  162. hbc_count_options->add_child(lbl_count_start);
  163. spn_count_start = memnew(SpinBox);
  164. spn_count_start->set_tooltip(TTR("Initial value for the counter."));
  165. spn_count_start->set_step(1);
  166. spn_count_start->set_min(0);
  167. hbc_count_options->add_child(spn_count_start);
  168. Label *lbl_count_step = memnew(Label);
  169. lbl_count_step->set_text(TTR("Step"));
  170. lbl_count_step->set_tooltip(TTR("Amount by which counter is incremented for each node."));
  171. hbc_count_options->add_child(lbl_count_step);
  172. spn_count_step = memnew(SpinBox);
  173. spn_count_step->set_tooltip(TTR("Amount by which counter is incremented for each node."));
  174. spn_count_step->set_step(1);
  175. hbc_count_options->add_child(spn_count_step);
  176. Label *lbl_count_padding = memnew(Label);
  177. lbl_count_padding->set_text(TTR("Padding"));
  178. lbl_count_padding->set_tooltip(TTR("Minimum number of digits for the counter.\nMissing digits are padded with leading zeros."));
  179. hbc_count_options->add_child(lbl_count_padding);
  180. spn_count_padding = memnew(SpinBox);
  181. spn_count_padding->set_tooltip(TTR("Minimum number of digits for the counter.\nMissing digits are padded with leading zeros."));
  182. spn_count_padding->set_step(1);
  183. hbc_count_options->add_child(spn_count_padding);
  184. // ---- Tab Process
  185. VBoxContainer *vbc_process = memnew(VBoxContainer);
  186. vbc_process->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  187. vbc_process->set_name(TTR("Post-Process"));
  188. tabc_features->add_child(vbc_process);
  189. cbut_process = memnew(CheckBox);
  190. cbut_process->set_text(TTR("Post-Process"));
  191. vbc_process->add_child(cbut_process);
  192. // ------ Style
  193. HBoxContainer *hbc_style = memnew(HBoxContainer);
  194. vbc_process->add_child(hbc_style);
  195. Label *lbl_style = memnew(Label);
  196. lbl_style->set_text(TTR("Style"));
  197. hbc_style->add_child(lbl_style);
  198. opt_style = memnew(OptionButton);
  199. opt_style->add_item(TTR("Keep"));
  200. opt_style->add_item(TTR("PascalCase to snake_case"));
  201. opt_style->add_item(TTR("snake_case to PascalCase"));
  202. hbc_style->add_child(opt_style);
  203. // ------ Case
  204. HBoxContainer *hbc_case = memnew(HBoxContainer);
  205. vbc_process->add_child(hbc_case);
  206. Label *lbl_case = memnew(Label);
  207. lbl_case->set_text(TTR("Case"));
  208. hbc_case->add_child(lbl_case);
  209. opt_case = memnew(OptionButton);
  210. opt_case->add_item(TTR("Keep"));
  211. opt_case->add_item(TTR("To Lowercase"));
  212. opt_case->add_item(TTR("To Uppercase"));
  213. hbc_case->add_child(opt_case);
  214. // -- Preview
  215. HSeparator *sep_preview = memnew(HSeparator);
  216. sep_preview->set_custom_minimum_size(Size2(10, 20));
  217. vbc->add_child(sep_preview);
  218. lbl_preview_title = memnew(Label);
  219. vbc->add_child(lbl_preview_title);
  220. lbl_preview = memnew(Label);
  221. vbc->add_child(lbl_preview);
  222. // ---- Dialog related
  223. set_min_size(Size2(383, 0));
  224. get_ok_button()->set_text(TTR("Rename"));
  225. Button *but_reset = add_button(TTR("Reset"));
  226. eh.errfunc = _error_handler;
  227. eh.userdata = this;
  228. // ---- Connections
  229. cbut_collapse_features->connect("toggled", callable_mp(this, &RenameDialog::_features_toggled));
  230. // Substitute Buttons
  231. lne_search->connect("focus_entered", callable_mp(this, &RenameDialog::_update_substitute));
  232. lne_search->connect("focus_exited", callable_mp(this, &RenameDialog::_update_substitute));
  233. lne_replace->connect("focus_entered", callable_mp(this, &RenameDialog::_update_substitute));
  234. lne_replace->connect("focus_exited", callable_mp(this, &RenameDialog::_update_substitute));
  235. lne_prefix->connect("focus_entered", callable_mp(this, &RenameDialog::_update_substitute));
  236. lne_prefix->connect("focus_exited", callable_mp(this, &RenameDialog::_update_substitute));
  237. lne_suffix->connect("focus_entered", callable_mp(this, &RenameDialog::_update_substitute));
  238. lne_suffix->connect("focus_exited", callable_mp(this, &RenameDialog::_update_substitute));
  239. // Preview
  240. lne_prefix->connect("text_changed", callable_mp(this, &RenameDialog::_update_preview));
  241. lne_suffix->connect("text_changed", callable_mp(this, &RenameDialog::_update_preview));
  242. lne_search->connect("text_changed", callable_mp(this, &RenameDialog::_update_preview));
  243. lne_replace->connect("text_changed", callable_mp(this, &RenameDialog::_update_preview));
  244. spn_count_start->connect("value_changed", callable_mp(this, &RenameDialog::_update_preview_int));
  245. spn_count_step->connect("value_changed", callable_mp(this, &RenameDialog::_update_preview_int));
  246. spn_count_padding->connect("value_changed", callable_mp(this, &RenameDialog::_update_preview_int));
  247. opt_style->connect("item_selected", callable_mp(this, &RenameDialog::_update_preview_int));
  248. opt_case->connect("item_selected", callable_mp(this, &RenameDialog::_update_preview_int));
  249. cbut_substitute->connect("pressed", callable_mp(this, &RenameDialog::_update_preview), varray(""));
  250. cbut_regex->connect("pressed", callable_mp(this, &RenameDialog::_update_preview), varray(""));
  251. cbut_process->connect("pressed", callable_mp(this, &RenameDialog::_update_preview), varray(""));
  252. but_reset->connect("pressed", callable_mp(this, &RenameDialog::reset));
  253. reset();
  254. _features_toggled(false);
  255. }
  256. void RenameDialog::_bind_methods() {
  257. ClassDB::bind_method("rename", &RenameDialog::rename);
  258. }
  259. void RenameDialog::_update_substitute() {
  260. LineEdit *focus_owner_line_edit = Object::cast_to<LineEdit>(scene_tree_editor->get_focus_owner());
  261. bool is_main_field = _is_main_field(focus_owner_line_edit);
  262. but_insert_name->set_disabled(!is_main_field);
  263. but_insert_parent->set_disabled(!is_main_field);
  264. but_insert_type->set_disabled(!is_main_field);
  265. but_insert_scene->set_disabled(!is_main_field);
  266. but_insert_root->set_disabled(!is_main_field);
  267. but_insert_count->set_disabled(!is_main_field);
  268. // The focus mode seems to be reset when disabling/re-enabling
  269. but_insert_name->set_focus_mode(Control::FOCUS_NONE);
  270. but_insert_parent->set_focus_mode(Control::FOCUS_NONE);
  271. but_insert_type->set_focus_mode(Control::FOCUS_NONE);
  272. but_insert_scene->set_focus_mode(Control::FOCUS_NONE);
  273. but_insert_root->set_focus_mode(Control::FOCUS_NONE);
  274. but_insert_count->set_focus_mode(Control::FOCUS_NONE);
  275. }
  276. void RenameDialog::_post_popup() {
  277. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  278. preview_node = nullptr;
  279. Array selected_node_list = editor_selection->get_selected_nodes();
  280. ERR_FAIL_COND(selected_node_list.size() == 0);
  281. preview_node = selected_node_list[0];
  282. _update_preview();
  283. _update_substitute();
  284. }
  285. void RenameDialog::_update_preview_int(int new_value) {
  286. _update_preview();
  287. }
  288. void RenameDialog::_update_preview(String new_text) {
  289. if (lock_preview_update || preview_node == nullptr) {
  290. return;
  291. }
  292. has_errors = false;
  293. add_error_handler(&eh);
  294. String new_name = _apply_rename(preview_node, spn_count_start->get_value());
  295. if (!has_errors) {
  296. lbl_preview_title->set_text(TTR("Preview:"));
  297. lbl_preview->set_text(new_name);
  298. if (new_name == preview_node->get_name()) {
  299. // New name is identical to the old one. Don't color it as much to avoid distracting the user.
  300. const Color accent_color = EditorNode::get_singleton()->get_gui_base()->get_theme_color("accent_color", "Editor");
  301. const Color text_color = EditorNode::get_singleton()->get_gui_base()->get_theme_color("default_color", "RichTextLabel");
  302. lbl_preview->add_theme_color_override("font_color", accent_color.lerp(text_color, 0.5));
  303. } else {
  304. lbl_preview->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("success_color", "Editor"));
  305. }
  306. }
  307. remove_error_handler(&eh);
  308. }
  309. String RenameDialog::_apply_rename(const Node *node, int count) {
  310. String search = lne_search->get_text();
  311. String replace = lne_replace->get_text();
  312. String prefix = lne_prefix->get_text();
  313. String suffix = lne_suffix->get_text();
  314. String new_name = node->get_name();
  315. if (cbut_substitute->is_pressed()) {
  316. search = _substitute(search, node, count);
  317. replace = _substitute(replace, node, count);
  318. prefix = _substitute(prefix, node, count);
  319. suffix = _substitute(suffix, node, count);
  320. }
  321. if (cbut_regex->is_pressed()) {
  322. new_name = _regex(search, new_name, replace);
  323. } else {
  324. new_name = new_name.replace(search, replace);
  325. }
  326. new_name = prefix + new_name + suffix;
  327. if (cbut_process->is_pressed()) {
  328. new_name = _postprocess(new_name);
  329. }
  330. return new_name;
  331. }
  332. String RenameDialog::_substitute(const String &subject, const Node *node, int count) {
  333. String result = subject.replace("${COUNTER}", vformat("%0" + itos(spn_count_padding->get_value()) + "d", count));
  334. if (node) {
  335. result = result.replace("${NAME}", node->get_name());
  336. result = result.replace("${TYPE}", node->get_class());
  337. }
  338. int current = EditorNode::get_singleton()->get_editor_data().get_edited_scene();
  339. result = result.replace("${SCENE}", EditorNode::get_singleton()->get_editor_data().get_scene_title(current));
  340. Node *root_node = SceneTree::get_singleton()->get_edited_scene_root();
  341. if (root_node) {
  342. result = result.replace("${ROOT}", root_node->get_name());
  343. }
  344. if (node) {
  345. Node *parent_node = node->get_parent();
  346. if (parent_node) {
  347. if (node == root_node) {
  348. // Can not substitute parent of root.
  349. result = result.replace("${PARENT}", "");
  350. } else {
  351. result = result.replace("${PARENT}", parent_node->get_name());
  352. }
  353. }
  354. }
  355. return result;
  356. }
  357. void RenameDialog::_error_handler(void *p_self, const char *p_func, const char *p_file, int p_line, const char *p_error, const char *p_errorexp, ErrorHandlerType p_type) {
  358. RenameDialog *self = (RenameDialog *)p_self;
  359. String source_file(p_file);
  360. // Only show first error that is related to "regex"
  361. if (self->has_errors || source_file.find("regex") < 0) {
  362. return;
  363. }
  364. String err_str;
  365. if (p_errorexp && p_errorexp[0]) {
  366. err_str = p_errorexp;
  367. } else {
  368. err_str = p_error;
  369. }
  370. self->has_errors = true;
  371. self->lbl_preview_title->set_text(TTR("Regular Expression Error:"));
  372. self->lbl_preview->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color("error_color", "Editor"));
  373. self->lbl_preview->set_text(vformat(TTR("At character %s"), err_str));
  374. }
  375. String RenameDialog::_regex(const String &pattern, const String &subject, const String &replacement) {
  376. RegEx regex(pattern);
  377. return regex.sub(subject, replacement, true);
  378. }
  379. String RenameDialog::_postprocess(const String &subject) {
  380. int style_id = opt_style->get_selected();
  381. String result = subject;
  382. if (style_id == 1) {
  383. // PascalCase to snake_case
  384. result = result.camelcase_to_underscore(true);
  385. result = _regex("_+", result, "_");
  386. } else if (style_id == 2) {
  387. // snake_case to PascalCase
  388. RegEx pattern("_+(.?)");
  389. Array matches = pattern.search_all(result);
  390. // The name `_` would become empty; ignore it.
  391. if (matches.size() && result != "_") {
  392. String buffer;
  393. int start = 0;
  394. int end = 0;
  395. for (int i = 0; i < matches.size(); ++i) {
  396. start = ((Ref<RegExMatch>)matches[i])->get_start(1);
  397. buffer += result.substr(end, start - end - 1);
  398. buffer += result.substr(start, 1).to_upper();
  399. end = start + 1;
  400. }
  401. buffer += result.substr(end, result.size() - (end + 1));
  402. result = buffer.replace("_", "").capitalize();
  403. }
  404. }
  405. int case_id = opt_case->get_selected();
  406. if (case_id == 1) {
  407. // To Lowercase
  408. result = result.to_lower();
  409. } else if (case_id == 2) {
  410. // To Upercase
  411. result = result.to_upper();
  412. }
  413. return result;
  414. }
  415. void RenameDialog::_iterate_scene(const Node *node, const Array &selection, int *counter) {
  416. if (!node) {
  417. return;
  418. }
  419. if (selection.has(node)) {
  420. String new_name = _apply_rename(node, *counter);
  421. if (node->get_name() != new_name) {
  422. Pair<NodePath, String> rename_item;
  423. rename_item.first = node->get_path();
  424. rename_item.second = new_name;
  425. to_rename.push_back(rename_item);
  426. }
  427. *counter += spn_count_step->get_value();
  428. }
  429. int *cur_counter = counter;
  430. int level_counter = spn_count_start->get_value();
  431. if (chk_per_level_counter->is_pressed()) {
  432. cur_counter = &level_counter;
  433. }
  434. for (int i = 0; i < node->get_child_count(); ++i) {
  435. _iterate_scene(node->get_child(i), selection, cur_counter);
  436. }
  437. }
  438. void RenameDialog::rename() {
  439. // Editor selection is not ordered via scene tree. Instead iterate
  440. // over scene tree until all selected nodes are found in order.
  441. EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
  442. Array selected_node_list = editor_selection->get_selected_nodes();
  443. Node *root_node = SceneTree::get_singleton()->get_edited_scene_root();
  444. global_count = spn_count_start->get_value();
  445. to_rename.clear();
  446. // Forward recursive as opposed to the actual renaming.
  447. _iterate_scene(root_node, selected_node_list, &global_count);
  448. if (undo_redo && !to_rename.is_empty()) {
  449. undo_redo->create_action(TTR("Batch Rename"));
  450. // Make sure to iterate reversed so that child nodes will find parents.
  451. for (int i = to_rename.size() - 1; i >= 0; --i) {
  452. Node *n = root_node->get_node(to_rename[i].first);
  453. const String &new_name = to_rename[i].second;
  454. if (!n) {
  455. ERR_PRINT("Skipping missing node: " + to_rename[i].first.get_concatenated_subnames());
  456. continue;
  457. }
  458. scene_tree_editor->emit_signal("node_prerename", n, new_name);
  459. undo_redo->add_do_method(scene_tree_editor, "_rename_node", n->get_instance_id(), new_name);
  460. undo_redo->add_undo_method(scene_tree_editor, "_rename_node", n->get_instance_id(), n->get_name());
  461. }
  462. undo_redo->commit_action();
  463. }
  464. }
  465. void RenameDialog::reset() {
  466. lock_preview_update = true;
  467. lne_prefix->clear();
  468. lne_suffix->clear();
  469. lne_search->clear();
  470. lne_replace->clear();
  471. cbut_substitute->set_pressed(false);
  472. cbut_regex->set_pressed(false);
  473. cbut_process->set_pressed(false);
  474. chk_per_level_counter->set_pressed(true);
  475. spn_count_start->set_value(1);
  476. spn_count_step->set_value(1);
  477. spn_count_padding->set_value(1);
  478. opt_style->select(0);
  479. opt_case->select(0);
  480. lock_preview_update = false;
  481. _update_preview();
  482. }
  483. bool RenameDialog::_is_main_field(LineEdit *line_edit) {
  484. return line_edit &&
  485. (line_edit == lne_search || line_edit == lne_replace || line_edit == lne_prefix || line_edit == lne_suffix);
  486. }
  487. void RenameDialog::_insert_text(String text) {
  488. LineEdit *focus_owner = Object::cast_to<LineEdit>(scene_tree_editor->get_focus_owner());
  489. if (_is_main_field(focus_owner)) {
  490. focus_owner->selection_delete();
  491. focus_owner->append_at_cursor(text);
  492. _update_preview();
  493. }
  494. }
  495. void RenameDialog::_features_toggled(bool pressed) {
  496. if (pressed) {
  497. tabc_features->show();
  498. } else {
  499. tabc_features->hide();
  500. }
  501. // Adjust to minimum size in y
  502. Size2i size = get_size();
  503. size.y = 0;
  504. set_size(size);
  505. }