animation_blend_tree_editor_plugin.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /**************************************************************************/
  2. /* animation_blend_tree_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation_blend_tree_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/resource_loader.h"
  33. #include "editor/editor_inspector.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_properties.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/gui/editor_file_dialog.h"
  40. #include "editor/themes/editor_scale.h"
  41. #include "scene/3d/skeleton_3d.h"
  42. #include "scene/gui/check_box.h"
  43. #include "scene/gui/grid_container.h"
  44. #include "scene/gui/menu_button.h"
  45. #include "scene/gui/option_button.h"
  46. #include "scene/gui/progress_bar.h"
  47. #include "scene/gui/separator.h"
  48. #include "scene/gui/view_panner.h"
  49. #include "scene/main/window.h"
  50. void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) {
  51. for (int i = 0; i < add_options.size(); i++) {
  52. ERR_FAIL_COND(add_options[i].script == p_script);
  53. }
  54. AddOption ao;
  55. ao.name = p_name;
  56. ao.script = p_script;
  57. add_options.push_back(ao);
  58. _update_options_menu();
  59. }
  60. void AnimationNodeBlendTreeEditor::remove_custom_type(const Ref<Script> &p_script) {
  61. for (int i = 0; i < add_options.size(); i++) {
  62. if (add_options[i].script == p_script) {
  63. add_options.remove_at(i);
  64. return;
  65. }
  66. }
  67. _update_options_menu();
  68. }
  69. void AnimationNodeBlendTreeEditor::_update_options_menu(bool p_has_input_ports) {
  70. add_node->get_popup()->clear();
  71. add_node->get_popup()->reset_size();
  72. for (int i = 0; i < add_options.size(); i++) {
  73. if (p_has_input_ports && add_options[i].input_port_count == 0) {
  74. continue;
  75. }
  76. add_node->get_popup()->add_item(add_options[i].name, i);
  77. }
  78. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  79. if (clipb.is_valid()) {
  80. add_node->get_popup()->add_separator();
  81. add_node->get_popup()->add_item(TTR("Paste"), MENU_PASTE);
  82. }
  83. add_node->get_popup()->add_separator();
  84. add_node->get_popup()->add_item(TTR("Load..."), MENU_LOAD_FILE);
  85. use_position_from_popup_menu = false;
  86. }
  87. Size2 AnimationNodeBlendTreeEditor::get_minimum_size() const {
  88. return Size2(10, 200);
  89. }
  90. void AnimationNodeBlendTreeEditor::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
  91. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  92. if (!tree) {
  93. return;
  94. }
  95. updating = true;
  96. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  97. undo_redo->create_action(vformat(TTR("Parameter Changed: %s"), p_property), UndoRedo::MERGE_ENDS);
  98. undo_redo->add_do_property(tree, p_property, p_value);
  99. undo_redo->add_undo_property(tree, p_property, tree->get(p_property));
  100. undo_redo->add_do_method(this, "update_graph");
  101. undo_redo->add_undo_method(this, "update_graph");
  102. undo_redo->commit_action();
  103. updating = false;
  104. }
  105. void AnimationNodeBlendTreeEditor::update_graph() {
  106. if (updating || blend_tree.is_null()) {
  107. return;
  108. }
  109. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  110. if (!tree) {
  111. return;
  112. }
  113. visible_properties.clear();
  114. graph->set_scroll_offset(blend_tree->get_graph_offset() * EDSCALE);
  115. graph->clear_connections();
  116. //erase all nodes
  117. for (int i = 0; i < graph->get_child_count(); i++) {
  118. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  119. memdelete(graph->get_child(i));
  120. i--;
  121. }
  122. }
  123. animations.clear();
  124. List<StringName> nodes;
  125. blend_tree->get_node_list(&nodes);
  126. for (const StringName &E : nodes) {
  127. GraphNode *node = memnew(GraphNode);
  128. graph->add_child(node);
  129. node->set_draggable(!read_only);
  130. Ref<AnimationNode> agnode = blend_tree->get_node(E);
  131. ERR_CONTINUE(agnode.is_null());
  132. node->set_position_offset(blend_tree->get_node_position(E) * EDSCALE);
  133. node->set_title(agnode->get_caption());
  134. node->set_name(E);
  135. int base = 0;
  136. if (E != SceneStringName(output)) {
  137. LineEdit *name = memnew(LineEdit);
  138. name->set_text(E);
  139. name->set_editable(!read_only);
  140. name->set_expand_to_text_length_enabled(true);
  141. name->set_custom_minimum_size(Vector2(100, 0) * EDSCALE);
  142. node->add_child(name);
  143. node->set_slot(0, false, 0, Color(), true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")));
  144. name->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed).bind(agnode), CONNECT_DEFERRED);
  145. name->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out).bind(agnode), CONNECT_DEFERRED);
  146. name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed), CONNECT_DEFERRED);
  147. base = 1;
  148. agnode->set_deletable(true);
  149. if (!read_only) {
  150. Button *delete_button = memnew(Button);
  151. delete_button->set_flat(true);
  152. delete_button->set_focus_mode(FOCUS_NONE);
  153. delete_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  154. delete_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_node_request).bind(E), CONNECT_DEFERRED);
  155. node->get_titlebar_hbox()->add_child(delete_button);
  156. }
  157. }
  158. for (int i = 0; i < agnode->get_input_count(); i++) {
  159. Label *in_name = memnew(Label);
  160. node->add_child(in_name);
  161. in_name->set_text(agnode->get_input_name(i));
  162. node->set_slot(base + i, true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")), false, 0, Color());
  163. }
  164. List<PropertyInfo> pinfo;
  165. agnode->get_parameter_list(&pinfo);
  166. for (const PropertyInfo &F : pinfo) {
  167. if (!(F.usage & PROPERTY_USAGE_EDITOR)) {
  168. continue;
  169. }
  170. String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E) + "/" + F.name;
  171. EditorProperty *prop = EditorInspector::instantiate_property_editor(tree, F.type, base_path, F.hint, F.hint_string, F.usage);
  172. if (prop) {
  173. prop->set_read_only(read_only || (F.usage & PROPERTY_USAGE_READ_ONLY));
  174. prop->set_object_and_property(tree, base_path);
  175. prop->update_property();
  176. prop->set_name_split_ratio(0);
  177. prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
  178. if (F.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  179. // Give the resource editor some more space to make the inside readable.
  180. prop->set_custom_minimum_size(Vector2(180, 0) * EDSCALE);
  181. // Align the size of the node with the resource editor, its un-expanding does not trigger a resize.
  182. prop->connect(SceneStringName(resized), Callable(node, "reset_size"));
  183. }
  184. node->add_child(prop);
  185. visible_properties.push_back(prop);
  186. }
  187. }
  188. node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));
  189. if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
  190. node->add_child(memnew(HSeparator));
  191. Button *open_in_editor = memnew(Button);
  192. open_in_editor->set_text(TTR("Open Editor"));
  193. open_in_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  194. node->add_child(open_in_editor);
  195. open_in_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_open_in_editor).bind(E), CONNECT_DEFERRED);
  196. open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);
  197. }
  198. if (agnode->has_filter()) {
  199. node->add_child(memnew(HSeparator));
  200. Button *inspect_filters = memnew(Button);
  201. if (read_only) {
  202. inspect_filters->set_text(TTR("Inspect Filters"));
  203. } else {
  204. inspect_filters->set_text(TTR("Edit Filters"));
  205. }
  206. inspect_filters->set_button_icon(get_editor_theme_icon(SNAME("AnimationFilter")));
  207. node->add_child(inspect_filters);
  208. inspect_filters->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_inspect_filters).bind(E), CONNECT_DEFERRED);
  209. inspect_filters->set_h_size_flags(SIZE_SHRINK_CENTER);
  210. }
  211. Ref<AnimationNodeAnimation> anim = agnode;
  212. if (anim.is_valid()) {
  213. MenuButton *mb = memnew(MenuButton);
  214. mb->set_text(anim->get_animation());
  215. mb->set_button_icon(get_editor_theme_icon(SNAME("Animation")));
  216. mb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  217. mb->set_disabled(read_only);
  218. Array options;
  219. node->add_child(memnew(HSeparator));
  220. node->add_child(mb);
  221. ProgressBar *pb = memnew(ProgressBar);
  222. List<StringName> anims;
  223. tree->get_animation_list(&anims);
  224. for (const StringName &F : anims) {
  225. mb->get_popup()->add_item(F);
  226. options.push_back(F);
  227. }
  228. pb->set_show_percentage(false);
  229. pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);
  230. animations[E] = pb;
  231. node->add_child(pb);
  232. mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);
  233. }
  234. Ref<StyleBox> sb_panel = node->get_theme_stylebox(SceneStringName(panel), "GraphNode")->duplicate();
  235. if (sb_panel.is_valid()) {
  236. sb_panel->set_content_margin(SIDE_TOP, 12 * EDSCALE);
  237. sb_panel->set_content_margin(SIDE_BOTTOM, 12 * EDSCALE);
  238. node->add_theme_style_override(SceneStringName(panel), sb_panel);
  239. }
  240. node->add_theme_constant_override("separation", 4 * EDSCALE);
  241. }
  242. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  243. blend_tree->get_node_connections(&node_connections);
  244. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  245. StringName from = E.output_node;
  246. StringName to = E.input_node;
  247. int to_idx = E.input_index;
  248. graph->connect_node(from, 0, to, to_idx);
  249. }
  250. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  251. graph->set_minimap_opacity(graph_minimap_opacity);
  252. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  253. graph->set_connection_lines_curvature(graph_lines_curvature);
  254. }
  255. void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
  256. file_loaded = ResourceLoader::load(p_file);
  257. if (file_loaded.is_valid()) {
  258. _add_node(MENU_LOAD_FILE_CONFIRM);
  259. } else {
  260. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
  261. }
  262. }
  263. void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
  264. Ref<AnimationNode> anode;
  265. String base_name;
  266. if (p_idx == MENU_LOAD_FILE) {
  267. open_file->clear_filters();
  268. List<String> ext_filters;
  269. ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &ext_filters);
  270. for (const String &E : ext_filters) {
  271. open_file->add_filter("*." + E);
  272. }
  273. open_file->popup_file_dialog();
  274. return;
  275. } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
  276. anode = file_loaded;
  277. file_loaded.unref();
  278. base_name = anode->get_class();
  279. } else if (p_idx == MENU_PASTE) {
  280. anode = EditorSettings::get_singleton()->get_resource_clipboard();
  281. ERR_FAIL_COND(anode.is_null());
  282. base_name = anode->get_class();
  283. } else if (!add_options[p_idx].type.is_empty()) {
  284. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(add_options[p_idx].type));
  285. ERR_FAIL_NULL(an);
  286. anode = Ref<AnimationNode>(an);
  287. base_name = add_options[p_idx].name;
  288. } else {
  289. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  290. StringName base_type = add_options[p_idx].script->get_instance_base_type();
  291. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(base_type));
  292. ERR_FAIL_NULL(an);
  293. anode = Ref<AnimationNode>(an);
  294. anode->set_script(add_options[p_idx].script);
  295. base_name = add_options[p_idx].name;
  296. }
  297. Ref<AnimationNodeOutput> out = anode;
  298. if (out.is_valid()) {
  299. EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
  300. return;
  301. }
  302. if (!from_node.is_empty() && anode->get_input_count() == 0) {
  303. from_node = "";
  304. return;
  305. }
  306. Point2 instance_pos = graph->get_scroll_offset();
  307. if (use_position_from_popup_menu) {
  308. instance_pos += position_from_popup_menu;
  309. } else {
  310. instance_pos += graph->get_size() * 0.5;
  311. }
  312. instance_pos /= graph->get_zoom();
  313. int base = 1;
  314. String name = base_name;
  315. while (blend_tree->has_node(name)) {
  316. base++;
  317. name = base_name + " " + itos(base);
  318. }
  319. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  320. undo_redo->create_action(TTR("Add Node to BlendTree"));
  321. undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
  322. undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
  323. if (!from_node.is_empty()) {
  324. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", name, 0, from_node);
  325. from_node = "";
  326. }
  327. if (!to_node.is_empty() && to_slot != -1) {
  328. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", to_node, to_slot, name);
  329. to_node = "";
  330. to_slot = -1;
  331. }
  332. undo_redo->add_do_method(this, "update_graph");
  333. undo_redo->add_undo_method(this, "update_graph");
  334. undo_redo->commit_action();
  335. }
  336. void AnimationNodeBlendTreeEditor::_popup(bool p_has_input_ports, const Vector2 &p_node_position) {
  337. _update_options_menu(p_has_input_ports);
  338. use_position_from_popup_menu = true;
  339. position_from_popup_menu = p_node_position;
  340. add_node->get_popup()->set_position(graph->get_screen_position() + graph->get_local_mouse_position());
  341. add_node->get_popup()->reset_size();
  342. add_node->get_popup()->popup();
  343. }
  344. void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
  345. if (read_only) {
  346. return;
  347. }
  348. _popup(false, p_position);
  349. }
  350. void AnimationNodeBlendTreeEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  351. if (read_only) {
  352. return;
  353. }
  354. Ref<AnimationNode> node = blend_tree->get_node(p_from);
  355. if (node.is_valid()) {
  356. from_node = p_from;
  357. _popup(true, p_release_position);
  358. }
  359. }
  360. void AnimationNodeBlendTreeEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  361. if (read_only) {
  362. return;
  363. }
  364. Ref<AnimationNode> node = blend_tree->get_node(p_to);
  365. if (node.is_valid()) {
  366. to_node = p_to;
  367. to_slot = p_to_slot;
  368. _popup(false, p_release_position);
  369. }
  370. }
  371. void AnimationNodeBlendTreeEditor::_popup_hide() {
  372. to_node = "";
  373. to_slot = -1;
  374. }
  375. void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
  376. updating = true;
  377. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  378. undo_redo->create_action(TTR("Node Moved"));
  379. undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
  380. undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
  381. undo_redo->add_do_method(this, "update_graph");
  382. undo_redo->add_undo_method(this, "update_graph");
  383. undo_redo->commit_action();
  384. updating = false;
  385. }
  386. void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  387. if (read_only) {
  388. return;
  389. }
  390. AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  391. if (err == AnimationNodeBlendTree::CONNECTION_ERROR_CONNECTION_EXISTS) {
  392. blend_tree->disconnect_node(p_to, p_to_index);
  393. err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  394. }
  395. if (err != AnimationNodeBlendTree::CONNECTION_OK) {
  396. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  397. return;
  398. }
  399. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  400. undo_redo->create_action(TTR("Nodes Connected"));
  401. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  402. undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  403. undo_redo->add_do_method(this, "update_graph");
  404. undo_redo->add_undo_method(this, "update_graph");
  405. undo_redo->commit_action();
  406. }
  407. void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  408. if (read_only) {
  409. return;
  410. }
  411. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  412. updating = true;
  413. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  414. undo_redo->create_action(TTR("Nodes Disconnected"));
  415. undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  416. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  417. undo_redo->add_do_method(this, "update_graph");
  418. undo_redo->add_undo_method(this, "update_graph");
  419. undo_redo->commit_action();
  420. updating = false;
  421. }
  422. void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, const Array &p_options, const String &p_node) {
  423. String option = p_options[p_index];
  424. Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
  425. ERR_FAIL_COND(anim.is_null());
  426. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  427. undo_redo->create_action(TTR("Set Animation"));
  428. undo_redo->add_do_method(anim.ptr(), "set_animation", option);
  429. undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
  430. undo_redo->add_do_method(this, "update_graph");
  431. undo_redo->add_undo_method(this, "update_graph");
  432. undo_redo->commit_action();
  433. }
  434. void AnimationNodeBlendTreeEditor::_delete_node_request(const String &p_which) {
  435. if (read_only) {
  436. return;
  437. }
  438. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  439. undo_redo->create_action(TTR("Delete Node"));
  440. undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
  441. undo_redo->add_undo_method(blend_tree.ptr(), "add_node", p_which, blend_tree->get_node(p_which), blend_tree.ptr()->get_node_position(p_which));
  442. List<AnimationNodeBlendTree::NodeConnection> conns;
  443. blend_tree->get_node_connections(&conns);
  444. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  445. if (E.output_node == p_which || E.input_node == p_which) {
  446. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E.input_node, E.input_index, E.output_node);
  447. }
  448. }
  449. undo_redo->add_do_method(this, "update_graph");
  450. undo_redo->add_undo_method(this, "update_graph");
  451. undo_redo->commit_action();
  452. }
  453. void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
  454. if (read_only) {
  455. return;
  456. }
  457. List<StringName> to_erase;
  458. if (p_nodes.is_empty()) {
  459. for (int i = 0; i < graph->get_child_count(); i++) {
  460. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  461. if (gn && gn->is_selected()) {
  462. Ref<AnimationNode> anode = blend_tree->get_node(gn->get_name());
  463. if (anode->is_deletable()) {
  464. to_erase.push_back(gn->get_name());
  465. }
  466. }
  467. }
  468. } else {
  469. for (int i = 0; i < p_nodes.size(); i++) {
  470. Ref<AnimationNode> anode = blend_tree->get_node(p_nodes[i]);
  471. if (anode->is_deletable()) {
  472. to_erase.push_back(p_nodes[i]);
  473. }
  474. }
  475. }
  476. if (to_erase.is_empty()) {
  477. return;
  478. }
  479. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  480. undo_redo->create_action(TTR("Delete Node(s)"));
  481. for (const StringName &F : to_erase) {
  482. _delete_node_request(F);
  483. }
  484. undo_redo->commit_action();
  485. }
  486. void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
  487. if (read_only) {
  488. return;
  489. }
  490. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  491. ERR_FAIL_NULL(gn);
  492. String name = gn->get_name();
  493. Ref<AnimationNode> anode = blend_tree->get_node(name);
  494. ERR_FAIL_COND(anode.is_null());
  495. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  496. }
  497. void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
  498. Ref<AnimationNode> an = blend_tree->get_node(p_which);
  499. ERR_FAIL_COND(an.is_null());
  500. AnimationTreeEditor::get_singleton()->enter_editor(p_which);
  501. }
  502. void AnimationNodeBlendTreeEditor::_filter_toggled() {
  503. updating = true;
  504. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  505. undo_redo->create_action(TTR("Toggle Filter On/Off"));
  506. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
  507. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
  508. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  509. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  510. undo_redo->commit_action();
  511. updating = false;
  512. }
  513. void AnimationNodeBlendTreeEditor::_filter_edited() {
  514. TreeItem *edited = filters->get_edited();
  515. ERR_FAIL_NULL(edited);
  516. NodePath edited_path = edited->get_metadata(0);
  517. bool filtered = edited->is_checked(0);
  518. updating = true;
  519. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  520. undo_redo->create_action(TTR("Change Filter"));
  521. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
  522. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
  523. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  524. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  525. undo_redo->commit_action();
  526. updating = false;
  527. }
  528. void AnimationNodeBlendTreeEditor::_filter_fill_selection() {
  529. TreeItem *ti = filters->get_root();
  530. if (!ti) {
  531. return;
  532. }
  533. updating = true;
  534. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  535. undo_redo->create_action(TTR("Fill Selected Filter Children"));
  536. _filter_fill_selection_recursive(undo_redo, ti, false);
  537. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  538. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  539. undo_redo->commit_action();
  540. updating = false;
  541. _update_filters(_filter_edit);
  542. }
  543. void AnimationNodeBlendTreeEditor::_filter_invert_selection() {
  544. TreeItem *ti = filters->get_root();
  545. if (!ti) {
  546. return;
  547. }
  548. updating = true;
  549. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  550. undo_redo->create_action(TTR("Invert Filter Selection"));
  551. _filter_invert_selection_recursive(undo_redo, ti);
  552. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  553. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  554. undo_redo->commit_action();
  555. updating = false;
  556. _update_filters(_filter_edit);
  557. }
  558. void AnimationNodeBlendTreeEditor::_filter_clear_selection() {
  559. TreeItem *ti = filters->get_root();
  560. if (!ti) {
  561. return;
  562. }
  563. updating = true;
  564. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  565. undo_redo->create_action(TTR("Clear Filter Selection"));
  566. _filter_clear_selection_recursive(undo_redo, ti);
  567. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  568. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  569. undo_redo->commit_action();
  570. updating = false;
  571. _update_filters(_filter_edit);
  572. }
  573. void AnimationNodeBlendTreeEditor::_filter_fill_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item, bool p_parent_filtered) {
  574. TreeItem *ti = p_item->get_first_child();
  575. bool parent_filtered = p_parent_filtered;
  576. while (ti) {
  577. NodePath item_path = ti->get_metadata(0);
  578. bool filtered = _filter_edit->is_path_filtered(item_path);
  579. parent_filtered |= filtered;
  580. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, parent_filtered);
  581. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  582. _filter_fill_selection_recursive(p_undo_redo, ti, parent_filtered);
  583. ti = ti->get_next();
  584. parent_filtered = p_parent_filtered;
  585. }
  586. }
  587. void AnimationNodeBlendTreeEditor::_filter_invert_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  588. TreeItem *ti = p_item->get_first_child();
  589. while (ti) {
  590. NodePath item_path = ti->get_metadata(0);
  591. bool filtered = _filter_edit->is_path_filtered(item_path);
  592. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, !filtered);
  593. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  594. _filter_invert_selection_recursive(p_undo_redo, ti);
  595. ti = ti->get_next();
  596. }
  597. }
  598. void AnimationNodeBlendTreeEditor::_filter_clear_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  599. TreeItem *ti = p_item->get_first_child();
  600. while (ti) {
  601. NodePath item_path = ti->get_metadata(0);
  602. bool filtered = _filter_edit->is_path_filtered(item_path);
  603. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, false);
  604. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  605. _filter_clear_selection_recursive(p_undo_redo, ti);
  606. ti = ti->get_next();
  607. }
  608. }
  609. bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
  610. if (updating || _filter_edit != anode) {
  611. return false;
  612. }
  613. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  614. if (!tree) {
  615. return false;
  616. }
  617. Node *base = tree->get_node(tree->get_root_node());
  618. if (!base) {
  619. EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
  620. return false;
  621. }
  622. updating = true;
  623. HashSet<String> paths;
  624. HashMap<String, RBSet<String>> types;
  625. {
  626. List<StringName> animation_list;
  627. tree->get_animation_list(&animation_list);
  628. for (const StringName &E : animation_list) {
  629. Ref<Animation> anim = tree->get_animation(E);
  630. for (int i = 0; i < anim->get_track_count(); i++) {
  631. String track_path = anim->track_get_path(i);
  632. paths.insert(track_path);
  633. String track_type_name;
  634. Animation::TrackType track_type = anim->track_get_type(i);
  635. switch (track_type) {
  636. case Animation::TrackType::TYPE_ANIMATION: {
  637. track_type_name = TTR("Anim Clips");
  638. } break;
  639. case Animation::TrackType::TYPE_AUDIO: {
  640. track_type_name = TTR("Audio Clips");
  641. } break;
  642. case Animation::TrackType::TYPE_METHOD: {
  643. track_type_name = TTR("Functions");
  644. } break;
  645. default: {
  646. } break;
  647. }
  648. if (!track_type_name.is_empty()) {
  649. types[track_path].insert(track_type_name);
  650. }
  651. }
  652. }
  653. }
  654. filter_enabled->set_pressed(anode->is_filter_enabled());
  655. filters->clear();
  656. TreeItem *root = filters->create_item();
  657. HashMap<String, TreeItem *> parenthood;
  658. for (const String &E : paths) {
  659. NodePath path = E;
  660. TreeItem *ti = nullptr;
  661. String accum;
  662. for (int i = 0; i < path.get_name_count(); i++) {
  663. String name = path.get_name(i);
  664. if (!accum.is_empty()) {
  665. accum += "/";
  666. }
  667. accum += name;
  668. if (!parenthood.has(accum)) {
  669. if (ti) {
  670. ti = filters->create_item(ti);
  671. } else {
  672. ti = filters->create_item(root);
  673. }
  674. parenthood[accum] = ti;
  675. ti->set_text(0, name);
  676. ti->set_selectable(0, false);
  677. ti->set_editable(0, false);
  678. if (base->has_node(accum)) {
  679. Node *node = base->get_node(accum);
  680. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  681. }
  682. } else {
  683. ti = parenthood[accum];
  684. }
  685. }
  686. Node *node = nullptr;
  687. if (base->has_node(accum)) {
  688. node = base->get_node(accum);
  689. }
  690. if (!node) {
  691. continue; //no node, can't edit
  692. }
  693. if (path.get_subname_count()) {
  694. String concat = path.get_concatenated_subnames();
  695. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  696. if (skeleton && skeleton->find_bone(concat) != -1) {
  697. //path in skeleton
  698. const String &bone = concat;
  699. int idx = skeleton->find_bone(bone);
  700. List<String> bone_path;
  701. while (idx != -1) {
  702. bone_path.push_front(skeleton->get_bone_name(idx));
  703. idx = skeleton->get_bone_parent(idx);
  704. }
  705. accum += ":";
  706. for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
  707. if (F != bone_path.front()) {
  708. accum += "/";
  709. }
  710. accum += F->get();
  711. if (!parenthood.has(accum)) {
  712. ti = filters->create_item(ti);
  713. parenthood[accum] = ti;
  714. ti->set_text(0, F->get());
  715. ti->set_selectable(0, false);
  716. ti->set_editable(0, false);
  717. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  718. } else {
  719. ti = parenthood[accum];
  720. }
  721. }
  722. ti->set_editable(0, !read_only);
  723. ti->set_selectable(0, true);
  724. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  725. ti->set_text(0, concat);
  726. ti->set_checked(0, anode->is_path_filtered(path));
  727. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  728. ti->set_metadata(0, path);
  729. } else {
  730. //just a property
  731. ti = filters->create_item(ti);
  732. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  733. ti->set_text(0, concat);
  734. ti->set_editable(0, !read_only);
  735. ti->set_selectable(0, true);
  736. ti->set_checked(0, anode->is_path_filtered(path));
  737. ti->set_metadata(0, path);
  738. }
  739. } else {
  740. if (ti) {
  741. //just a node, not a property track
  742. String types_text = "[";
  743. if (types.has(path)) {
  744. RBSet<String>::Iterator F = types[path].begin();
  745. types_text += *F;
  746. while (F) {
  747. types_text += " / " + *F;
  748. ;
  749. ++F;
  750. }
  751. }
  752. types_text += "]";
  753. ti = filters->create_item(ti);
  754. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  755. ti->set_text(0, types_text);
  756. ti->set_editable(0, !read_only);
  757. ti->set_selectable(0, true);
  758. ti->set_checked(0, anode->is_path_filtered(path));
  759. ti->set_metadata(0, path);
  760. }
  761. }
  762. }
  763. updating = false;
  764. return true;
  765. }
  766. void AnimationNodeBlendTreeEditor::_inspect_filters(const String &p_which) {
  767. if (read_only) {
  768. filter_dialog->set_title(TTR("Inspect Filtered Tracks:"));
  769. } else {
  770. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  771. }
  772. filter_enabled->set_disabled(read_only);
  773. Ref<AnimationNode> anode = blend_tree->get_node(p_which);
  774. ERR_FAIL_COND(anode.is_null());
  775. _filter_edit = anode;
  776. if (!_update_filters(anode)) {
  777. return;
  778. }
  779. filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
  780. }
  781. void AnimationNodeBlendTreeEditor::_update_editor_settings() {
  782. graph->get_panner()->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
  783. graph->set_warped_panning(EDITOR_GET("editors/panning/warped_mouse_panning"));
  784. }
  785. void AnimationNodeBlendTreeEditor::_notification(int p_what) {
  786. switch (p_what) {
  787. case NOTIFICATION_ENTER_TREE: {
  788. _update_editor_settings();
  789. } break;
  790. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  791. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  792. _update_editor_settings();
  793. }
  794. } break;
  795. case NOTIFICATION_THEME_CHANGED: {
  796. error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  797. error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  798. if (is_visible_in_tree()) {
  799. update_graph();
  800. }
  801. } break;
  802. case NOTIFICATION_PROCESS: {
  803. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  804. if (!tree) {
  805. return; // Node has been changed.
  806. }
  807. String error;
  808. if (!tree->is_active()) {
  809. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  810. } else if (tree->is_state_invalid()) {
  811. error = tree->get_invalid_state_reason();
  812. }
  813. if (error != error_label->get_text()) {
  814. error_label->set_text(error);
  815. if (!error.is_empty()) {
  816. error_panel->show();
  817. } else {
  818. error_panel->hide();
  819. }
  820. }
  821. List<AnimationNodeBlendTree::NodeConnection> conns;
  822. blend_tree->get_node_connections(&conns);
  823. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  824. float activity = 0;
  825. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  826. if (!tree->is_state_invalid()) {
  827. activity = tree->get_connection_activity(path, E.input_index);
  828. }
  829. graph->set_connection_activity(E.output_node, 0, E.input_node, E.input_index, activity);
  830. }
  831. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  832. Ref<AnimationNodeAnimation> an = blend_tree->get_node(E.key);
  833. if (an.is_valid()) {
  834. if (tree->has_animation(an->get_animation())) {
  835. Ref<Animation> anim = tree->get_animation(an->get_animation());
  836. if (anim.is_valid()) {
  837. //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  838. StringName length_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_length";
  839. StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_position";
  840. E.value->set_max(tree->get(length_path));
  841. E.value->set_value(tree->get(time_path));
  842. }
  843. }
  844. }
  845. }
  846. for (int i = 0; i < visible_properties.size(); i++) {
  847. visible_properties[i]->update_property();
  848. }
  849. } break;
  850. case NOTIFICATION_VISIBILITY_CHANGED: {
  851. set_process(is_visible_in_tree());
  852. } break;
  853. }
  854. }
  855. void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
  856. if (read_only) {
  857. return;
  858. }
  859. if (updating) {
  860. return;
  861. }
  862. updating = true;
  863. blend_tree->set_graph_offset(p_scroll / EDSCALE);
  864. updating = false;
  865. }
  866. void AnimationNodeBlendTreeEditor::_bind_methods() {
  867. ClassDB::bind_method("update_graph", &AnimationNodeBlendTreeEditor::update_graph);
  868. ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
  869. }
  870. AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = nullptr;
  871. // AnimationNode's "node_changed" signal means almost update_input.
  872. void AnimationNodeBlendTreeEditor::_node_changed(const StringName &p_node_name) {
  873. // TODO:
  874. // Here is executed during the commit of EditorNode::undo_redo, it is not possible to create an undo_redo action here.
  875. // The disconnect when the number of enabled inputs decreases is done in AnimationNodeBlendTree and update_graph().
  876. // This means that there is no place to register undo_redo actions.
  877. // In order to implement undo_redo correctly, we may need to implement AnimationNodeEdit such as AnimationTrackKeyEdit
  878. // and add it to _node_selected() with EditorNode::get_singleton()->push_item(AnimationNodeEdit).
  879. update_graph();
  880. }
  881. void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
  882. if (blend_tree.is_null()) {
  883. return;
  884. }
  885. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  886. if (!tree) {
  887. return;
  888. }
  889. String prev_name = blend_tree->get_node_name(p_node);
  890. ERR_FAIL_COND(prev_name.is_empty());
  891. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
  892. ERR_FAIL_NULL(gn);
  893. const String &new_name = p_text;
  894. ERR_FAIL_COND(new_name.is_empty() || new_name.contains_char('.') || new_name.contains_char('/'));
  895. if (new_name == prev_name) {
  896. return; //nothing to do
  897. }
  898. const String &base_name = new_name;
  899. int base = 1;
  900. String name = base_name;
  901. while (blend_tree->has_node(name)) {
  902. base++;
  903. name = base_name + " " + itos(base);
  904. }
  905. String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
  906. updating = true;
  907. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  908. undo_redo->create_action(TTR("Node Renamed"));
  909. undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
  910. undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
  911. undo_redo->add_do_method(this, "update_graph");
  912. undo_redo->add_undo_method(this, "update_graph");
  913. undo_redo->commit_action();
  914. updating = false;
  915. gn->set_name(new_name);
  916. gn->set_size(gn->get_minimum_size());
  917. //change editors accordingly
  918. for (int i = 0; i < visible_properties.size(); i++) {
  919. String pname = visible_properties[i]->get_edited_property().operator String();
  920. if (pname.begins_with(base_path + prev_name)) {
  921. String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
  922. visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
  923. }
  924. }
  925. //recreate connections
  926. graph->clear_connections();
  927. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  928. blend_tree->get_node_connections(&node_connections);
  929. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  930. StringName from = E.output_node;
  931. StringName to = E.input_node;
  932. int to_idx = E.input_index;
  933. graph->connect_node(from, 0, to, to_idx);
  934. }
  935. //update animations
  936. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  937. if (E.key == prev_name) {
  938. animations[new_name] = animations[prev_name];
  939. animations.erase(prev_name);
  940. break;
  941. }
  942. }
  943. update_graph(); // Needed to update the signal connections with the new name.
  944. current_node_rename_text = String();
  945. }
  946. void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node) {
  947. if (current_node_rename_text.is_empty()) {
  948. return; // The text_submitted signal triggered the graph update and freed the LineEdit.
  949. }
  950. _node_renamed(current_node_rename_text, p_node);
  951. }
  952. void AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed(const String &p_text) {
  953. current_node_rename_text = p_text;
  954. }
  955. bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
  956. Ref<AnimationNodeBlendTree> bt = p_node;
  957. return bt.is_valid();
  958. }
  959. void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
  960. if (blend_tree.is_valid()) {
  961. blend_tree->disconnect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  962. }
  963. blend_tree = p_node;
  964. read_only = false;
  965. if (blend_tree.is_null()) {
  966. hide();
  967. } else {
  968. read_only = EditorNode::get_singleton()->is_resource_read_only(blend_tree);
  969. blend_tree->connect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  970. update_graph();
  971. }
  972. add_node->set_disabled(read_only);
  973. graph->set_show_arrange_button(!read_only);
  974. }
  975. AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
  976. singleton = this;
  977. updating = false;
  978. use_position_from_popup_menu = false;
  979. graph = memnew(GraphEdit);
  980. add_child(graph);
  981. graph->add_valid_right_disconnect_type(0);
  982. graph->add_valid_left_disconnect_type(0);
  983. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  984. graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), CONNECT_DEFERRED);
  985. graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), CONNECT_DEFERRED);
  986. graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));
  987. graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));
  988. graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));
  989. graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));
  990. graph->connect("connection_to_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_to_empty));
  991. graph->connect("connection_from_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_from_empty));
  992. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  993. graph->set_minimap_opacity(graph_minimap_opacity);
  994. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  995. graph->set_connection_lines_curvature(graph_lines_curvature);
  996. VSeparator *vs = memnew(VSeparator);
  997. graph->get_menu_hbox()->add_child(vs);
  998. graph->get_menu_hbox()->move_child(vs, 0);
  999. add_node = memnew(MenuButton);
  1000. graph->get_menu_hbox()->add_child(add_node);
  1001. add_node->set_text(TTR("Add Node..."));
  1002. graph->get_menu_hbox()->move_child(add_node, 0);
  1003. add_node->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_add_node));
  1004. add_node->get_popup()->connect("popup_hide", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_hide), CONNECT_DEFERRED);
  1005. add_node->connect("about_to_popup", callable_mp(this, &AnimationNodeBlendTreeEditor::_update_options_menu).bind(false));
  1006. add_node->set_disabled(read_only);
  1007. add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
  1008. add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot", 2));
  1009. add_options.push_back(AddOption("Add2", "AnimationNodeAdd2", 2));
  1010. add_options.push_back(AddOption("Add3", "AnimationNodeAdd3", 3));
  1011. add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2", 2));
  1012. add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3", 3));
  1013. add_options.push_back(AddOption("Sub2", "AnimationNodeSub2", 2));
  1014. add_options.push_back(AddOption("TimeSeek", "AnimationNodeTimeSeek", 1));
  1015. add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale", 1));
  1016. add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
  1017. add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
  1018. add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
  1019. add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
  1020. add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
  1021. _update_options_menu();
  1022. error_panel = memnew(PanelContainer);
  1023. add_child(error_panel);
  1024. error_label = memnew(Label);
  1025. error_panel->add_child(error_label);
  1026. error_label->set_text("eh");
  1027. filter_dialog = memnew(AcceptDialog);
  1028. add_child(filter_dialog);
  1029. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  1030. VBoxContainer *filter_vbox = memnew(VBoxContainer);
  1031. filter_dialog->add_child(filter_vbox);
  1032. HBoxContainer *filter_hbox = memnew(HBoxContainer);
  1033. filter_vbox->add_child(filter_hbox);
  1034. filter_enabled = memnew(CheckBox);
  1035. filter_enabled->set_text(TTR("Enable Filtering"));
  1036. filter_enabled->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_toggled));
  1037. filter_hbox->add_child(filter_enabled);
  1038. filter_fill_selection = memnew(Button);
  1039. filter_fill_selection->set_text(TTR("Fill Selected Children"));
  1040. filter_fill_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_fill_selection));
  1041. filter_hbox->add_child(filter_fill_selection);
  1042. filter_invert_selection = memnew(Button);
  1043. filter_invert_selection->set_text(TTR("Invert"));
  1044. filter_invert_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_invert_selection));
  1045. filter_hbox->add_child(filter_invert_selection);
  1046. filter_clear_selection = memnew(Button);
  1047. filter_clear_selection->set_text(TTR("Clear"));
  1048. filter_clear_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_clear_selection));
  1049. filter_hbox->add_child(filter_clear_selection);
  1050. filters = memnew(Tree);
  1051. filter_vbox->add_child(filters);
  1052. filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1053. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  1054. filters->set_hide_root(true);
  1055. filters->connect("item_edited", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_edited));
  1056. open_file = memnew(EditorFileDialog);
  1057. add_child(open_file);
  1058. open_file->set_title(TTR("Open Animation Node"));
  1059. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1060. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_file_opened));
  1061. animation_node_inspector_plugin.instantiate();
  1062. EditorInspector::add_inspector_plugin(animation_node_inspector_plugin);
  1063. }
  1064. AnimationNodeBlendTreeEditor::~AnimationNodeBlendTreeEditor() {
  1065. }
  1066. // EditorPluginAnimationNodeAnimation
  1067. void AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog() {
  1068. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  1069. StringName anim_name = animation_node_animation->get_animation();
  1070. PackedStringArray markers = tree->has_animation(anim_name) ? tree->get_animation(anim_name)->get_marker_names() : PackedStringArray();
  1071. dialog->select_start->clear();
  1072. dialog->select_start->add_icon_item(get_editor_theme_icon(SNAME("PlayStart")), TTR("Start of Animation"));
  1073. dialog->select_start->add_separator();
  1074. dialog->select_end->clear();
  1075. dialog->select_end->add_icon_item(get_editor_theme_icon(SNAME("PlayStartBackwards")), TTR("End of Animation"));
  1076. dialog->select_end->add_separator();
  1077. for (const String &marker : markers) {
  1078. dialog->select_start->add_item(marker);
  1079. dialog->select_end->add_item(marker);
  1080. }
  1081. // Because the default selections are always valid, and marker times won't change during the dialog, we can ensure that the user can only select valid markers.
  1082. // This invariant is maintained by _validate_markers.
  1083. dialog->select_start->select(0);
  1084. dialog->select_end->select(0);
  1085. dialog->popup_centered(Size2(200, 0) * EDSCALE);
  1086. }
  1087. void AnimationNodeAnimationEditor::_validate_markers(int p_id) {
  1088. // Note: p_id is ignored. It is included because OptionButton's item_changed signal always passes it.
  1089. int start_id = dialog->select_start->get_selected_id();
  1090. int end_id = dialog->select_end->get_selected_id();
  1091. StringName anim_name = animation_node_animation->get_animation();
  1092. Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(anim_name);
  1093. ERR_FAIL_COND(animation.is_null());
  1094. double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));
  1095. double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));
  1096. // p_start and p_end have the same item count.
  1097. for (int i = 2; i < dialog->select_start->get_item_count(); i++) {
  1098. String start_marker = dialog->select_start->get_item_text(i);
  1099. String end_marker = dialog->select_end->get_item_text(i);
  1100. dialog->select_start->set_item_disabled(i, end_id >= 2 && (i == end_id || animation->get_marker_time(start_marker) > end_time));
  1101. dialog->select_end->set_item_disabled(i, start_id >= 2 && (i == start_id || start_time > animation->get_marker_time(end_marker)));
  1102. }
  1103. }
  1104. void AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog() {
  1105. int start_id = dialog->select_start->get_selected_id();
  1106. int end_id = dialog->select_end->get_selected_id();
  1107. Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(animation_node_animation->get_animation());
  1108. ERR_FAIL_COND(animation.is_null());
  1109. double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));
  1110. double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));
  1111. double length = end_time - start_time;
  1112. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1113. undo_redo->create_action(TTR("Set Custom Timeline from Marker"));
  1114. undo_redo->add_do_method(*animation_node_animation, "set_start_offset", start_time);
  1115. undo_redo->add_undo_method(*animation_node_animation, "set_start_offset", animation_node_animation->get_start_offset());
  1116. undo_redo->add_do_method(*animation_node_animation, "set_stretch_time_scale", false);
  1117. undo_redo->add_undo_method(*animation_node_animation, "set_stretch_time_scale", animation_node_animation->is_stretching_time_scale());
  1118. undo_redo->add_do_method(*animation_node_animation, "set_timeline_length", length);
  1119. undo_redo->add_undo_method(*animation_node_animation, "set_timeline_length", animation_node_animation->get_timeline_length());
  1120. undo_redo->add_do_method(*animation_node_animation, "notify_property_list_changed");
  1121. undo_redo->add_undo_method(*animation_node_animation, "notify_property_list_changed");
  1122. undo_redo->commit_action();
  1123. }
  1124. AnimationNodeAnimationEditor::AnimationNodeAnimationEditor(Ref<AnimationNodeAnimation> p_animation_node_animation) {
  1125. animation_node_animation = p_animation_node_animation;
  1126. dialog = memnew(AnimationNodeAnimationEditorDialog);
  1127. add_child(dialog);
  1128. dialog->set_hide_on_ok(false);
  1129. dialog->select_start->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));
  1130. dialog->select_end->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));
  1131. dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog));
  1132. Control *top_spacer = memnew(Control);
  1133. add_child(top_spacer);
  1134. top_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);
  1135. button = memnew(Button);
  1136. add_child(button);
  1137. button->set_text(TTR("Set Custom Timeline from Marker"));
  1138. button->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  1139. button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog));
  1140. Control *bottom_spacer = memnew(Control);
  1141. add_child(bottom_spacer);
  1142. bottom_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);
  1143. }
  1144. AnimationNodeAnimationEditor::~AnimationNodeAnimationEditor() {
  1145. }
  1146. void AnimationNodeAnimationEditor::_notification(int p_what) {
  1147. switch (p_what) {
  1148. case NOTIFICATION_THEME_CHANGED: {
  1149. button->set_theme_type_variation(SNAME("InspectorActionButton"));
  1150. button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  1151. } break;
  1152. }
  1153. }
  1154. bool EditorInspectorPluginAnimationNodeAnimation::can_handle(Object *p_object) {
  1155. Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
  1156. return ana.is_valid() && ana->is_using_custom_timeline();
  1157. }
  1158. bool EditorInspectorPluginAnimationNodeAnimation::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  1159. Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
  1160. ERR_FAIL_COND_V(ana.is_null(), false);
  1161. if (p_path == "timeline_length") {
  1162. add_custom_control(memnew(AnimationNodeAnimationEditor(ana)));
  1163. }
  1164. return false;
  1165. }
  1166. AnimationNodeAnimationEditorDialog::AnimationNodeAnimationEditorDialog() {
  1167. set_title(TTR("Select Markers"));
  1168. GridContainer *grid = memnew(GridContainer);
  1169. grid->set_columns(2);
  1170. grid->set_offsets_preset(Control::PRESET_FULL_RECT);
  1171. add_child(grid);
  1172. Label *label_start = memnew(Label(TTR("Start Marker")));
  1173. grid->add_child(label_start);
  1174. label_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1175. label_start->set_stretch_ratio(1);
  1176. select_start = memnew(OptionButton);
  1177. select_start->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1178. grid->add_child(select_start);
  1179. select_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1180. select_start->set_stretch_ratio(2);
  1181. Label *label_end = memnew(Label(TTR("End Marker")));
  1182. grid->add_child(label_end);
  1183. label_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1184. label_end->set_stretch_ratio(1);
  1185. select_end = memnew(OptionButton);
  1186. select_end->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1187. grid->add_child(select_end);
  1188. select_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1189. select_end->set_stretch_ratio(2);
  1190. }
  1191. AnimationNodeAnimationEditorDialog::~AnimationNodeAnimationEditorDialog() {
  1192. }