animation_blend_tree_editor_plugin.cpp 52 KB

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