animation_blend_tree_editor_plugin.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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->set_accessibility_name(TTRC("Delete"));
  155. delete_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_node_request).bind(E), CONNECT_DEFERRED);
  156. node->get_titlebar_hbox()->add_child(delete_button);
  157. }
  158. }
  159. for (int i = 0; i < agnode->get_input_count(); i++) {
  160. Label *in_name = memnew(Label);
  161. node->add_child(in_name);
  162. in_name->set_text(agnode->get_input_name(i));
  163. node->set_slot(base + i, true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")), false, 0, Color());
  164. }
  165. List<PropertyInfo> pinfo;
  166. agnode->get_parameter_list(&pinfo);
  167. for (const PropertyInfo &F : pinfo) {
  168. if (!(F.usage & PROPERTY_USAGE_EDITOR)) {
  169. continue;
  170. }
  171. String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E) + "/" + F.name;
  172. EditorProperty *prop = EditorInspector::instantiate_property_editor(tree, F.type, base_path, F.hint, F.hint_string, F.usage);
  173. if (prop) {
  174. prop->set_read_only(read_only || (F.usage & PROPERTY_USAGE_READ_ONLY));
  175. prop->set_object_and_property(tree, base_path);
  176. prop->update_property();
  177. prop->set_name_split_ratio(0);
  178. prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
  179. if (F.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  180. // Give the resource editor some more space to make the inside readable.
  181. prop->set_custom_minimum_size(Vector2(180, 0) * EDSCALE);
  182. // Align the size of the node with the resource editor, its un-expanding does not trigger a resize.
  183. prop->connect(SceneStringName(resized), Callable(node, "reset_size"));
  184. }
  185. node->add_child(prop);
  186. visible_properties.push_back(prop);
  187. }
  188. }
  189. node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));
  190. if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
  191. node->add_child(memnew(HSeparator));
  192. Button *open_in_editor = memnew(Button);
  193. open_in_editor->set_text(TTR("Open Editor"));
  194. open_in_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  195. node->add_child(open_in_editor);
  196. open_in_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_open_in_editor).bind(E), CONNECT_DEFERRED);
  197. open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);
  198. }
  199. if (agnode->has_filter()) {
  200. node->add_child(memnew(HSeparator));
  201. Button *inspect_filters = memnew(Button);
  202. if (read_only) {
  203. inspect_filters->set_text(TTR("Inspect Filters"));
  204. } else {
  205. inspect_filters->set_text(TTR("Edit Filters"));
  206. }
  207. inspect_filters->set_button_icon(get_editor_theme_icon(SNAME("AnimationFilter")));
  208. node->add_child(inspect_filters);
  209. inspect_filters->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_inspect_filters).bind(E), CONNECT_DEFERRED);
  210. inspect_filters->set_h_size_flags(SIZE_SHRINK_CENTER);
  211. }
  212. Ref<AnimationNodeAnimation> anim = agnode;
  213. if (anim.is_valid()) {
  214. MenuButton *mb = memnew(MenuButton);
  215. mb->set_text(anim->get_animation());
  216. mb->set_button_icon(get_editor_theme_icon(SNAME("Animation")));
  217. mb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  218. mb->set_disabled(read_only);
  219. Array options;
  220. node->add_child(memnew(HSeparator));
  221. node->add_child(mb);
  222. ProgressBar *pb = memnew(ProgressBar);
  223. List<StringName> anims;
  224. tree->get_animation_list(&anims);
  225. for (const StringName &F : anims) {
  226. mb->get_popup()->add_item(F);
  227. options.push_back(F);
  228. }
  229. pb->set_show_percentage(false);
  230. pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);
  231. animations[E] = pb;
  232. node->add_child(pb);
  233. mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);
  234. }
  235. Ref<StyleBox> sb_panel = node->get_theme_stylebox(SceneStringName(panel), "GraphNode")->duplicate();
  236. if (sb_panel.is_valid()) {
  237. sb_panel->set_content_margin(SIDE_TOP, 12 * EDSCALE);
  238. sb_panel->set_content_margin(SIDE_BOTTOM, 12 * EDSCALE);
  239. node->add_theme_style_override(SceneStringName(panel), sb_panel);
  240. }
  241. node->add_theme_constant_override("separation", 4 * EDSCALE);
  242. }
  243. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  244. blend_tree->get_node_connections(&node_connections);
  245. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  246. StringName from = E.output_node;
  247. StringName to = E.input_node;
  248. int to_idx = E.input_index;
  249. graph->connect_node(from, 0, to, to_idx);
  250. }
  251. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  252. graph->set_minimap_opacity(graph_minimap_opacity);
  253. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  254. graph->set_connection_lines_curvature(graph_lines_curvature);
  255. }
  256. void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
  257. file_loaded = ResourceLoader::load(p_file);
  258. if (file_loaded.is_valid()) {
  259. _add_node(MENU_LOAD_FILE_CONFIRM);
  260. } else {
  261. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
  262. }
  263. }
  264. void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
  265. Ref<AnimationNode> anode;
  266. String base_name;
  267. if (p_idx == MENU_LOAD_FILE) {
  268. open_file->clear_filters();
  269. List<String> ext_filters;
  270. ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &ext_filters);
  271. for (const String &E : ext_filters) {
  272. open_file->add_filter("*." + E);
  273. }
  274. open_file->popup_file_dialog();
  275. return;
  276. } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
  277. anode = file_loaded;
  278. file_loaded.unref();
  279. base_name = anode->get_class();
  280. } else if (p_idx == MENU_PASTE) {
  281. anode = EditorSettings::get_singleton()->get_resource_clipboard();
  282. ERR_FAIL_COND(anode.is_null());
  283. base_name = anode->get_class();
  284. } else if (!add_options[p_idx].type.is_empty()) {
  285. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(add_options[p_idx].type));
  286. ERR_FAIL_NULL(an);
  287. anode = Ref<AnimationNode>(an);
  288. base_name = add_options[p_idx].name;
  289. } else {
  290. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  291. StringName base_type = add_options[p_idx].script->get_instance_base_type();
  292. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(base_type));
  293. ERR_FAIL_NULL(an);
  294. anode = Ref<AnimationNode>(an);
  295. anode->set_script(add_options[p_idx].script);
  296. base_name = add_options[p_idx].name;
  297. }
  298. Ref<AnimationNodeOutput> out = anode;
  299. if (out.is_valid()) {
  300. EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
  301. return;
  302. }
  303. if (!from_node.is_empty() && anode->get_input_count() == 0) {
  304. from_node = "";
  305. return;
  306. }
  307. Point2 instance_pos = graph->get_scroll_offset();
  308. if (use_position_from_popup_menu) {
  309. instance_pos += position_from_popup_menu;
  310. } else {
  311. instance_pos += graph->get_size() * 0.5;
  312. }
  313. instance_pos /= graph->get_zoom();
  314. int base = 1;
  315. String name = base_name;
  316. while (blend_tree->has_node(name)) {
  317. base++;
  318. name = base_name + " " + itos(base);
  319. }
  320. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  321. undo_redo->create_action(TTR("Add Node to BlendTree"));
  322. undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
  323. undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
  324. if (!from_node.is_empty()) {
  325. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", name, 0, from_node);
  326. from_node = "";
  327. }
  328. if (!to_node.is_empty() && to_slot != -1) {
  329. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", to_node, to_slot, name);
  330. to_node = "";
  331. to_slot = -1;
  332. }
  333. undo_redo->add_do_method(this, "update_graph");
  334. undo_redo->add_undo_method(this, "update_graph");
  335. undo_redo->commit_action();
  336. }
  337. void AnimationNodeBlendTreeEditor::_popup(bool p_has_input_ports, const Vector2 &p_node_position) {
  338. _update_options_menu(p_has_input_ports);
  339. use_position_from_popup_menu = true;
  340. position_from_popup_menu = p_node_position;
  341. add_node->get_popup()->set_position(graph->get_screen_position() + graph->get_local_mouse_position());
  342. add_node->get_popup()->reset_size();
  343. add_node->get_popup()->popup();
  344. }
  345. void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
  346. if (read_only) {
  347. return;
  348. }
  349. _popup(false, p_position);
  350. }
  351. void AnimationNodeBlendTreeEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  352. if (read_only) {
  353. return;
  354. }
  355. Ref<AnimationNode> node = blend_tree->get_node(p_from);
  356. if (node.is_valid()) {
  357. from_node = p_from;
  358. _popup(true, p_release_position);
  359. }
  360. }
  361. void AnimationNodeBlendTreeEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  362. if (read_only) {
  363. return;
  364. }
  365. Ref<AnimationNode> node = blend_tree->get_node(p_to);
  366. if (node.is_valid()) {
  367. to_node = p_to;
  368. to_slot = p_to_slot;
  369. _popup(false, p_release_position);
  370. }
  371. }
  372. void AnimationNodeBlendTreeEditor::_popup_hide() {
  373. to_node = "";
  374. to_slot = -1;
  375. }
  376. void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
  377. updating = true;
  378. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  379. undo_redo->create_action(TTR("Node Moved"));
  380. undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
  381. undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
  382. undo_redo->add_do_method(this, "update_graph");
  383. undo_redo->add_undo_method(this, "update_graph");
  384. undo_redo->commit_action();
  385. updating = false;
  386. }
  387. void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  388. if (read_only) {
  389. return;
  390. }
  391. AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  392. if (err == AnimationNodeBlendTree::CONNECTION_ERROR_CONNECTION_EXISTS) {
  393. blend_tree->disconnect_node(p_to, p_to_index);
  394. err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  395. }
  396. if (err != AnimationNodeBlendTree::CONNECTION_OK) {
  397. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  398. return;
  399. }
  400. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  401. undo_redo->create_action(TTR("Nodes Connected"));
  402. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  403. undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  404. undo_redo->add_do_method(this, "update_graph");
  405. undo_redo->add_undo_method(this, "update_graph");
  406. undo_redo->commit_action();
  407. }
  408. void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  409. if (read_only) {
  410. return;
  411. }
  412. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  413. updating = true;
  414. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  415. undo_redo->create_action(TTR("Nodes Disconnected"));
  416. undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  417. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  418. undo_redo->add_do_method(this, "update_graph");
  419. undo_redo->add_undo_method(this, "update_graph");
  420. undo_redo->commit_action();
  421. updating = false;
  422. }
  423. void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, const Array &p_options, const String &p_node) {
  424. String option = p_options[p_index];
  425. Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
  426. ERR_FAIL_COND(anim.is_null());
  427. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  428. undo_redo->create_action(TTR("Set Animation"));
  429. undo_redo->add_do_method(anim.ptr(), "set_animation", option);
  430. undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
  431. undo_redo->add_do_method(this, "update_graph");
  432. undo_redo->add_undo_method(this, "update_graph");
  433. undo_redo->commit_action();
  434. }
  435. void AnimationNodeBlendTreeEditor::_delete_node_request(const String &p_which) {
  436. if (read_only) {
  437. return;
  438. }
  439. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  440. undo_redo->create_action(TTR("Delete Node"));
  441. undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
  442. 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));
  443. List<AnimationNodeBlendTree::NodeConnection> conns;
  444. blend_tree->get_node_connections(&conns);
  445. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  446. if (E.output_node == p_which || E.input_node == p_which) {
  447. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E.input_node, E.input_index, E.output_node);
  448. }
  449. }
  450. undo_redo->add_do_method(this, "update_graph");
  451. undo_redo->add_undo_method(this, "update_graph");
  452. undo_redo->commit_action();
  453. }
  454. void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
  455. if (read_only) {
  456. return;
  457. }
  458. List<StringName> to_erase;
  459. if (p_nodes.is_empty()) {
  460. for (int i = 0; i < graph->get_child_count(); i++) {
  461. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  462. if (gn && gn->is_selected()) {
  463. Ref<AnimationNode> anode = blend_tree->get_node(gn->get_name());
  464. if (anode->is_deletable()) {
  465. to_erase.push_back(gn->get_name());
  466. }
  467. }
  468. }
  469. } else {
  470. for (int i = 0; i < p_nodes.size(); i++) {
  471. Ref<AnimationNode> anode = blend_tree->get_node(p_nodes[i]);
  472. if (anode->is_deletable()) {
  473. to_erase.push_back(p_nodes[i]);
  474. }
  475. }
  476. }
  477. if (to_erase.is_empty()) {
  478. return;
  479. }
  480. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  481. undo_redo->create_action(TTR("Delete Node(s)"));
  482. for (const StringName &F : to_erase) {
  483. _delete_node_request(F);
  484. }
  485. undo_redo->commit_action();
  486. }
  487. void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
  488. if (read_only) {
  489. return;
  490. }
  491. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  492. ERR_FAIL_NULL(gn);
  493. String name = gn->get_name();
  494. Ref<AnimationNode> anode = blend_tree->get_node(name);
  495. ERR_FAIL_COND(anode.is_null());
  496. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  497. }
  498. void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
  499. Ref<AnimationNode> an = blend_tree->get_node(p_which);
  500. ERR_FAIL_COND(an.is_null());
  501. AnimationTreeEditor::get_singleton()->enter_editor(p_which);
  502. }
  503. void AnimationNodeBlendTreeEditor::_filter_toggled() {
  504. updating = true;
  505. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  506. undo_redo->create_action(TTR("Toggle Filter On/Off"));
  507. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
  508. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
  509. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  510. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  511. undo_redo->commit_action();
  512. updating = false;
  513. }
  514. void AnimationNodeBlendTreeEditor::_filter_edited() {
  515. TreeItem *edited = filters->get_edited();
  516. ERR_FAIL_NULL(edited);
  517. NodePath edited_path = edited->get_metadata(0);
  518. bool filtered = edited->is_checked(0);
  519. updating = true;
  520. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  521. undo_redo->create_action(TTR("Change Filter"));
  522. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
  523. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
  524. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  525. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  526. undo_redo->commit_action();
  527. updating = false;
  528. }
  529. void AnimationNodeBlendTreeEditor::_filter_fill_selection() {
  530. TreeItem *ti = filters->get_root();
  531. if (!ti) {
  532. return;
  533. }
  534. updating = true;
  535. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  536. undo_redo->create_action(TTR("Fill Selected Filter Children"));
  537. _filter_fill_selection_recursive(undo_redo, ti, false);
  538. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  539. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  540. undo_redo->commit_action();
  541. updating = false;
  542. _update_filters(_filter_edit);
  543. }
  544. void AnimationNodeBlendTreeEditor::_filter_invert_selection() {
  545. TreeItem *ti = filters->get_root();
  546. if (!ti) {
  547. return;
  548. }
  549. updating = true;
  550. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  551. undo_redo->create_action(TTR("Invert Filter Selection"));
  552. _filter_invert_selection_recursive(undo_redo, ti);
  553. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  554. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  555. undo_redo->commit_action();
  556. updating = false;
  557. _update_filters(_filter_edit);
  558. }
  559. void AnimationNodeBlendTreeEditor::_filter_clear_selection() {
  560. TreeItem *ti = filters->get_root();
  561. if (!ti) {
  562. return;
  563. }
  564. updating = true;
  565. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  566. undo_redo->create_action(TTR("Clear Filter Selection"));
  567. _filter_clear_selection_recursive(undo_redo, ti);
  568. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  569. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  570. undo_redo->commit_action();
  571. updating = false;
  572. _update_filters(_filter_edit);
  573. }
  574. void AnimationNodeBlendTreeEditor::_filter_fill_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item, bool p_parent_filtered) {
  575. TreeItem *ti = p_item->get_first_child();
  576. bool parent_filtered = p_parent_filtered;
  577. while (ti) {
  578. NodePath item_path = ti->get_metadata(0);
  579. bool filtered = _filter_edit->is_path_filtered(item_path);
  580. parent_filtered |= filtered;
  581. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, parent_filtered);
  582. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  583. _filter_fill_selection_recursive(p_undo_redo, ti, parent_filtered);
  584. ti = ti->get_next();
  585. parent_filtered = p_parent_filtered;
  586. }
  587. }
  588. void AnimationNodeBlendTreeEditor::_filter_invert_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  589. TreeItem *ti = p_item->get_first_child();
  590. while (ti) {
  591. NodePath item_path = ti->get_metadata(0);
  592. bool filtered = _filter_edit->is_path_filtered(item_path);
  593. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, !filtered);
  594. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  595. _filter_invert_selection_recursive(p_undo_redo, ti);
  596. ti = ti->get_next();
  597. }
  598. }
  599. void AnimationNodeBlendTreeEditor::_filter_clear_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  600. TreeItem *ti = p_item->get_first_child();
  601. while (ti) {
  602. NodePath item_path = ti->get_metadata(0);
  603. bool filtered = _filter_edit->is_path_filtered(item_path);
  604. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, false);
  605. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  606. _filter_clear_selection_recursive(p_undo_redo, ti);
  607. ti = ti->get_next();
  608. }
  609. }
  610. bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
  611. if (updating || _filter_edit != anode) {
  612. return false;
  613. }
  614. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  615. if (!tree) {
  616. return false;
  617. }
  618. Node *base = tree->get_node(tree->get_root_node());
  619. if (!base) {
  620. EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
  621. return false;
  622. }
  623. updating = true;
  624. HashSet<String> paths;
  625. HashMap<String, RBSet<String>> types;
  626. {
  627. List<StringName> animation_list;
  628. tree->get_animation_list(&animation_list);
  629. for (const StringName &E : animation_list) {
  630. Ref<Animation> anim = tree->get_animation(E);
  631. for (int i = 0; i < anim->get_track_count(); i++) {
  632. String track_path = anim->track_get_path(i);
  633. paths.insert(track_path);
  634. String track_type_name;
  635. Animation::TrackType track_type = anim->track_get_type(i);
  636. switch (track_type) {
  637. case Animation::TrackType::TYPE_ANIMATION: {
  638. track_type_name = TTR("Anim Clips");
  639. } break;
  640. case Animation::TrackType::TYPE_AUDIO: {
  641. track_type_name = TTR("Audio Clips");
  642. } break;
  643. case Animation::TrackType::TYPE_METHOD: {
  644. track_type_name = TTR("Functions");
  645. } break;
  646. default: {
  647. } break;
  648. }
  649. if (!track_type_name.is_empty()) {
  650. types[track_path].insert(track_type_name);
  651. }
  652. }
  653. }
  654. }
  655. filter_enabled->set_pressed(anode->is_filter_enabled());
  656. filters->clear();
  657. TreeItem *root = filters->create_item();
  658. HashMap<String, TreeItem *> parenthood;
  659. for (const String &E : paths) {
  660. NodePath path = E;
  661. TreeItem *ti = nullptr;
  662. String accum;
  663. for (int i = 0; i < path.get_name_count(); i++) {
  664. String name = path.get_name(i);
  665. if (!accum.is_empty()) {
  666. accum += "/";
  667. }
  668. accum += name;
  669. if (!parenthood.has(accum)) {
  670. if (ti) {
  671. ti = filters->create_item(ti);
  672. } else {
  673. ti = filters->create_item(root);
  674. }
  675. parenthood[accum] = ti;
  676. ti->set_text(0, name);
  677. ti->set_selectable(0, false);
  678. ti->set_editable(0, false);
  679. if (base->has_node(accum)) {
  680. Node *node = base->get_node(accum);
  681. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  682. }
  683. } else {
  684. ti = parenthood[accum];
  685. }
  686. }
  687. Node *node = nullptr;
  688. if (base->has_node(accum)) {
  689. node = base->get_node(accum);
  690. }
  691. if (!node) {
  692. continue; //no node, can't edit
  693. }
  694. if (path.get_subname_count()) {
  695. String concat = path.get_concatenated_subnames();
  696. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  697. if (skeleton && skeleton->find_bone(concat) != -1) {
  698. //path in skeleton
  699. const String &bone = concat;
  700. int idx = skeleton->find_bone(bone);
  701. List<String> bone_path;
  702. while (idx != -1) {
  703. bone_path.push_front(skeleton->get_bone_name(idx));
  704. idx = skeleton->get_bone_parent(idx);
  705. }
  706. accum += ":";
  707. for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
  708. if (F != bone_path.front()) {
  709. accum += "/";
  710. }
  711. accum += F->get();
  712. if (!parenthood.has(accum)) {
  713. ti = filters->create_item(ti);
  714. parenthood[accum] = ti;
  715. ti->set_text(0, F->get());
  716. ti->set_selectable(0, false);
  717. ti->set_editable(0, false);
  718. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  719. } else {
  720. ti = parenthood[accum];
  721. }
  722. }
  723. ti->set_editable(0, !read_only);
  724. ti->set_selectable(0, true);
  725. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  726. ti->set_text(0, concat);
  727. ti->set_checked(0, anode->is_path_filtered(path));
  728. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  729. ti->set_metadata(0, path);
  730. } else {
  731. //just a property
  732. ti = filters->create_item(ti);
  733. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  734. ti->set_text(0, concat);
  735. ti->set_editable(0, !read_only);
  736. ti->set_selectable(0, true);
  737. ti->set_checked(0, anode->is_path_filtered(path));
  738. ti->set_metadata(0, path);
  739. }
  740. } else {
  741. if (ti) {
  742. //just a node, not a property track
  743. String types_text = "[";
  744. if (types.has(path)) {
  745. RBSet<String>::Iterator F = types[path].begin();
  746. types_text += *F;
  747. while (F) {
  748. types_text += " / " + *F;
  749. ;
  750. ++F;
  751. }
  752. }
  753. types_text += "]";
  754. ti = filters->create_item(ti);
  755. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  756. ti->set_text(0, types_text);
  757. ti->set_editable(0, !read_only);
  758. ti->set_selectable(0, true);
  759. ti->set_checked(0, anode->is_path_filtered(path));
  760. ti->set_metadata(0, path);
  761. }
  762. }
  763. }
  764. updating = false;
  765. return true;
  766. }
  767. void AnimationNodeBlendTreeEditor::_inspect_filters(const String &p_which) {
  768. if (read_only) {
  769. filter_dialog->set_title(TTR("Inspect Filtered Tracks:"));
  770. } else {
  771. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  772. }
  773. filter_enabled->set_disabled(read_only);
  774. Ref<AnimationNode> anode = blend_tree->get_node(p_which);
  775. ERR_FAIL_COND(anode.is_null());
  776. _filter_edit = anode;
  777. if (!_update_filters(anode)) {
  778. return;
  779. }
  780. filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
  781. }
  782. void AnimationNodeBlendTreeEditor::_update_editor_settings() {
  783. 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")));
  784. graph->set_warped_panning(EDITOR_GET("editors/panning/warped_mouse_panning"));
  785. }
  786. void AnimationNodeBlendTreeEditor::_notification(int p_what) {
  787. switch (p_what) {
  788. case NOTIFICATION_ENTER_TREE: {
  789. _update_editor_settings();
  790. } break;
  791. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  792. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  793. _update_editor_settings();
  794. }
  795. } break;
  796. case NOTIFICATION_THEME_CHANGED: {
  797. error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  798. error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  799. if (is_visible_in_tree()) {
  800. update_graph();
  801. }
  802. } break;
  803. case NOTIFICATION_PROCESS: {
  804. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  805. if (!tree) {
  806. return; // Node has been changed.
  807. }
  808. String error;
  809. if (!tree->is_active()) {
  810. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  811. } else if (tree->is_state_invalid()) {
  812. error = tree->get_invalid_state_reason();
  813. }
  814. if (error != error_label->get_text()) {
  815. error_label->set_text(error);
  816. if (!error.is_empty()) {
  817. error_panel->show();
  818. } else {
  819. error_panel->hide();
  820. }
  821. }
  822. List<AnimationNodeBlendTree::NodeConnection> conns;
  823. blend_tree->get_node_connections(&conns);
  824. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  825. float activity = 0;
  826. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  827. if (!tree->is_state_invalid()) {
  828. activity = tree->get_connection_activity(path, E.input_index);
  829. }
  830. graph->set_connection_activity(E.output_node, 0, E.input_node, E.input_index, activity);
  831. }
  832. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  833. Ref<AnimationNodeAnimation> an = blend_tree->get_node(E.key);
  834. if (an.is_valid()) {
  835. if (tree->has_animation(an->get_animation())) {
  836. Ref<Animation> anim = tree->get_animation(an->get_animation());
  837. if (anim.is_valid()) {
  838. //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  839. StringName length_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_length";
  840. StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_position";
  841. E.value->set_max(tree->get(length_path));
  842. E.value->set_value(tree->get(time_path));
  843. }
  844. }
  845. }
  846. }
  847. for (int i = 0; i < visible_properties.size(); i++) {
  848. visible_properties[i]->update_property();
  849. }
  850. } break;
  851. case NOTIFICATION_VISIBILITY_CHANGED: {
  852. set_process(is_visible_in_tree());
  853. } break;
  854. }
  855. }
  856. void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
  857. if (read_only) {
  858. return;
  859. }
  860. if (updating) {
  861. return;
  862. }
  863. updating = true;
  864. blend_tree->set_graph_offset(p_scroll / EDSCALE);
  865. updating = false;
  866. }
  867. void AnimationNodeBlendTreeEditor::_bind_methods() {
  868. ClassDB::bind_method("update_graph", &AnimationNodeBlendTreeEditor::update_graph);
  869. ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
  870. }
  871. AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = nullptr;
  872. // AnimationNode's "node_changed" signal means almost update_input.
  873. void AnimationNodeBlendTreeEditor::_node_changed(const StringName &p_node_name) {
  874. // TODO:
  875. // Here is executed during the commit of EditorNode::undo_redo, it is not possible to create an undo_redo action here.
  876. // The disconnect when the number of enabled inputs decreases is done in AnimationNodeBlendTree and update_graph().
  877. // This means that there is no place to register undo_redo actions.
  878. // In order to implement undo_redo correctly, we may need to implement AnimationNodeEdit such as AnimationTrackKeyEdit
  879. // and add it to _node_selected() with EditorNode::get_singleton()->push_item(AnimationNodeEdit).
  880. update_graph();
  881. }
  882. void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
  883. if (blend_tree.is_null()) {
  884. return;
  885. }
  886. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  887. if (!tree) {
  888. return;
  889. }
  890. String prev_name = blend_tree->get_node_name(p_node);
  891. ERR_FAIL_COND(prev_name.is_empty());
  892. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
  893. ERR_FAIL_NULL(gn);
  894. const String &new_name = p_text;
  895. ERR_FAIL_COND(new_name.is_empty() || new_name.contains_char('.') || new_name.contains_char('/'));
  896. if (new_name == prev_name) {
  897. return; //nothing to do
  898. }
  899. const String &base_name = new_name;
  900. int base = 1;
  901. String name = base_name;
  902. while (blend_tree->has_node(name)) {
  903. base++;
  904. name = base_name + " " + itos(base);
  905. }
  906. String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
  907. updating = true;
  908. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  909. undo_redo->create_action(TTR("Node Renamed"));
  910. undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
  911. undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
  912. undo_redo->add_do_method(this, "update_graph");
  913. undo_redo->add_undo_method(this, "update_graph");
  914. undo_redo->commit_action();
  915. updating = false;
  916. gn->set_name(new_name);
  917. gn->set_size(gn->get_minimum_size());
  918. //change editors accordingly
  919. for (int i = 0; i < visible_properties.size(); i++) {
  920. String pname = visible_properties[i]->get_edited_property().operator String();
  921. if (pname.begins_with(base_path + prev_name)) {
  922. String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
  923. visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
  924. }
  925. }
  926. //recreate connections
  927. graph->clear_connections();
  928. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  929. blend_tree->get_node_connections(&node_connections);
  930. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  931. StringName from = E.output_node;
  932. StringName to = E.input_node;
  933. int to_idx = E.input_index;
  934. graph->connect_node(from, 0, to, to_idx);
  935. }
  936. //update animations
  937. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  938. if (E.key == prev_name) {
  939. animations[new_name] = animations[prev_name];
  940. animations.erase(prev_name);
  941. break;
  942. }
  943. }
  944. update_graph(); // Needed to update the signal connections with the new name.
  945. current_node_rename_text = String();
  946. }
  947. void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node) {
  948. if (current_node_rename_text.is_empty()) {
  949. return; // The text_submitted signal triggered the graph update and freed the LineEdit.
  950. }
  951. _node_renamed(current_node_rename_text, p_node);
  952. }
  953. void AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed(const String &p_text) {
  954. current_node_rename_text = p_text;
  955. }
  956. bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
  957. Ref<AnimationNodeBlendTree> bt = p_node;
  958. return bt.is_valid();
  959. }
  960. void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
  961. if (blend_tree.is_valid()) {
  962. blend_tree->disconnect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  963. }
  964. blend_tree = p_node;
  965. read_only = false;
  966. if (blend_tree.is_null()) {
  967. hide();
  968. } else {
  969. read_only = EditorNode::get_singleton()->is_resource_read_only(blend_tree);
  970. blend_tree->connect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  971. update_graph();
  972. }
  973. add_node->set_disabled(read_only);
  974. graph->set_show_arrange_button(!read_only);
  975. }
  976. AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
  977. singleton = this;
  978. updating = false;
  979. use_position_from_popup_menu = false;
  980. graph = memnew(GraphEdit);
  981. add_child(graph);
  982. graph->add_valid_right_disconnect_type(0);
  983. graph->add_valid_left_disconnect_type(0);
  984. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  985. graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), CONNECT_DEFERRED);
  986. graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), CONNECT_DEFERRED);
  987. graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));
  988. graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));
  989. graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));
  990. graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));
  991. graph->connect("connection_to_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_to_empty));
  992. graph->connect("connection_from_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_from_empty));
  993. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  994. graph->set_minimap_opacity(graph_minimap_opacity);
  995. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  996. graph->set_connection_lines_curvature(graph_lines_curvature);
  997. VSeparator *vs = memnew(VSeparator);
  998. graph->get_menu_hbox()->add_child(vs);
  999. graph->get_menu_hbox()->move_child(vs, 0);
  1000. add_node = memnew(MenuButton);
  1001. graph->get_menu_hbox()->add_child(add_node);
  1002. add_node->set_text(TTR("Add Node..."));
  1003. graph->get_menu_hbox()->move_child(add_node, 0);
  1004. add_node->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_add_node));
  1005. add_node->get_popup()->connect("popup_hide", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_hide), CONNECT_DEFERRED);
  1006. add_node->connect("about_to_popup", callable_mp(this, &AnimationNodeBlendTreeEditor::_update_options_menu).bind(false));
  1007. add_node->set_disabled(read_only);
  1008. add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
  1009. add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot", 2));
  1010. add_options.push_back(AddOption("Add2", "AnimationNodeAdd2", 2));
  1011. add_options.push_back(AddOption("Add3", "AnimationNodeAdd3", 3));
  1012. add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2", 2));
  1013. add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3", 3));
  1014. add_options.push_back(AddOption("Sub2", "AnimationNodeSub2", 2));
  1015. add_options.push_back(AddOption("TimeSeek", "AnimationNodeTimeSeek", 1));
  1016. add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale", 1));
  1017. add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
  1018. add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
  1019. add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
  1020. add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
  1021. add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
  1022. _update_options_menu();
  1023. error_panel = memnew(PanelContainer);
  1024. add_child(error_panel);
  1025. error_label = memnew(Label);
  1026. error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  1027. error_panel->add_child(error_label);
  1028. error_label->set_text("eh");
  1029. filter_dialog = memnew(AcceptDialog);
  1030. add_child(filter_dialog);
  1031. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  1032. VBoxContainer *filter_vbox = memnew(VBoxContainer);
  1033. filter_dialog->add_child(filter_vbox);
  1034. HBoxContainer *filter_hbox = memnew(HBoxContainer);
  1035. filter_vbox->add_child(filter_hbox);
  1036. filter_enabled = memnew(CheckBox);
  1037. filter_enabled->set_text(TTR("Enable Filtering"));
  1038. filter_enabled->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_toggled));
  1039. filter_hbox->add_child(filter_enabled);
  1040. filter_fill_selection = memnew(Button);
  1041. filter_fill_selection->set_text(TTR("Fill Selected Children"));
  1042. filter_fill_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_fill_selection));
  1043. filter_hbox->add_child(filter_fill_selection);
  1044. filter_invert_selection = memnew(Button);
  1045. filter_invert_selection->set_text(TTR("Invert"));
  1046. filter_invert_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_invert_selection));
  1047. filter_hbox->add_child(filter_invert_selection);
  1048. filter_clear_selection = memnew(Button);
  1049. filter_clear_selection->set_text(TTR("Clear"));
  1050. filter_clear_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_clear_selection));
  1051. filter_hbox->add_child(filter_clear_selection);
  1052. filters = memnew(Tree);
  1053. filter_vbox->add_child(filters);
  1054. filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1055. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  1056. filters->set_hide_root(true);
  1057. filters->connect("item_edited", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_edited));
  1058. open_file = memnew(EditorFileDialog);
  1059. add_child(open_file);
  1060. open_file->set_title(TTR("Open Animation Node"));
  1061. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1062. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_file_opened));
  1063. animation_node_inspector_plugin.instantiate();
  1064. EditorInspector::add_inspector_plugin(animation_node_inspector_plugin);
  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. void AnimationNodeAnimationEditor::_notification(int p_what) {
  1145. switch (p_what) {
  1146. case NOTIFICATION_THEME_CHANGED: {
  1147. button->set_theme_type_variation(SNAME("InspectorActionButton"));
  1148. button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  1149. } break;
  1150. }
  1151. }
  1152. bool EditorInspectorPluginAnimationNodeAnimation::can_handle(Object *p_object) {
  1153. Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
  1154. return ana.is_valid() && ana->is_using_custom_timeline();
  1155. }
  1156. 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) {
  1157. Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
  1158. ERR_FAIL_COND_V(ana.is_null(), false);
  1159. if (p_path == "timeline_length") {
  1160. add_custom_control(memnew(AnimationNodeAnimationEditor(ana)));
  1161. }
  1162. return false;
  1163. }
  1164. AnimationNodeAnimationEditorDialog::AnimationNodeAnimationEditorDialog() {
  1165. set_title(TTR("Select Markers"));
  1166. GridContainer *grid = memnew(GridContainer);
  1167. grid->set_columns(2);
  1168. grid->set_offsets_preset(Control::PRESET_FULL_RECT);
  1169. add_child(grid);
  1170. Label *label_start = memnew(Label(TTR("Start Marker")));
  1171. grid->add_child(label_start);
  1172. label_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1173. label_start->set_stretch_ratio(1);
  1174. select_start = memnew(OptionButton);
  1175. select_start->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1176. select_start->set_accessibility_name(TTRC("Start Marker"));
  1177. grid->add_child(select_start);
  1178. select_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1179. select_start->set_stretch_ratio(2);
  1180. Label *label_end = memnew(Label(TTR("End Marker")));
  1181. grid->add_child(label_end);
  1182. label_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1183. label_end->set_stretch_ratio(1);
  1184. select_end = memnew(OptionButton);
  1185. select_end->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1186. select_end->set_accessibility_name(TTRC("End Marker"));
  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. }