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