animation_blend_tree_editor_plugin.cpp 53 KB

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