rename_dialog.cpp 23 KB

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