rename_dialog.cpp 23 KB

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