animation_blend_tree_editor_plugin.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/editor_file_dialog.h"
  36. #include "editor/editor_inspector.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "scene/animation/animation_player.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/panel.h"
  42. #include "scene/gui/progress_bar.h"
  43. #include "scene/gui/view_panner.h"
  44. #include "scene/main/window.h"
  45. void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) {
  46. for (int i = 0; i < add_options.size(); i++) {
  47. ERR_FAIL_COND(add_options[i].script == p_script);
  48. }
  49. AddOption ao;
  50. ao.name = p_name;
  51. ao.script = p_script;
  52. add_options.push_back(ao);
  53. _update_options_menu();
  54. }
  55. void AnimationNodeBlendTreeEditor::remove_custom_type(const Ref<Script> &p_script) {
  56. for (int i = 0; i < add_options.size(); i++) {
  57. if (add_options[i].script == p_script) {
  58. add_options.remove_at(i);
  59. return;
  60. }
  61. }
  62. _update_options_menu();
  63. }
  64. void AnimationNodeBlendTreeEditor::_update_options_menu(bool p_has_input_ports) {
  65. add_node->get_popup()->clear();
  66. add_node->get_popup()->reset_size();
  67. for (int i = 0; i < add_options.size(); i++) {
  68. if (p_has_input_ports && add_options[i].input_port_count == 0) {
  69. continue;
  70. }
  71. add_node->get_popup()->add_item(add_options[i].name, i);
  72. }
  73. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  74. if (clipb.is_valid()) {
  75. add_node->get_popup()->add_separator();
  76. add_node->get_popup()->add_item(TTR("Paste"), MENU_PASTE);
  77. }
  78. add_node->get_popup()->add_separator();
  79. add_node->get_popup()->add_item(TTR("Load..."), MENU_LOAD_FILE);
  80. use_position_from_popup_menu = false;
  81. }
  82. Size2 AnimationNodeBlendTreeEditor::get_minimum_size() const {
  83. return Size2(10, 200);
  84. }
  85. void AnimationNodeBlendTreeEditor::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
  86. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_tree();
  87. updating = true;
  88. undo_redo->create_action(TTR("Parameter Changed:") + " " + String(p_property), UndoRedo::MERGE_ENDS);
  89. undo_redo->add_do_property(tree, p_property, p_value);
  90. undo_redo->add_undo_property(tree, p_property, tree->get(p_property));
  91. undo_redo->add_do_method(this, "_update_graph");
  92. undo_redo->add_undo_method(this, "_update_graph");
  93. undo_redo->commit_action();
  94. updating = false;
  95. }
  96. void AnimationNodeBlendTreeEditor::_update_graph() {
  97. if (updating || blend_tree.is_null()) {
  98. return;
  99. }
  100. visible_properties.clear();
  101. graph->set_scroll_ofs(blend_tree->get_graph_offset() * EDSCALE);
  102. graph->clear_connections();
  103. //erase all nodes
  104. for (int i = 0; i < graph->get_child_count(); i++) {
  105. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  106. memdelete(graph->get_child(i));
  107. i--;
  108. }
  109. }
  110. animations.clear();
  111. List<StringName> nodes;
  112. blend_tree->get_node_list(&nodes);
  113. for (const StringName &E : nodes) {
  114. GraphNode *node = memnew(GraphNode);
  115. graph->add_child(node);
  116. Ref<AnimationNode> agnode = blend_tree->get_node(E);
  117. ERR_CONTINUE(!agnode.is_valid());
  118. node->set_position_offset(blend_tree->get_node_position(E) * EDSCALE);
  119. node->set_title(agnode->get_caption());
  120. node->set_name(E);
  121. int base = 0;
  122. if (String(E) != "output") {
  123. LineEdit *name = memnew(LineEdit);
  124. name->set_text(E);
  125. name->set_expand_to_text_length_enabled(true);
  126. node->add_child(name);
  127. node->set_slot(0, false, 0, Color(), true, 0, get_theme_color(SNAME("font_color"), SNAME("Label")));
  128. name->connect("text_submitted", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed), varray(agnode), CONNECT_DEFERRED);
  129. name->connect("focus_exited", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out), varray(name, agnode), CONNECT_DEFERRED);
  130. base = 1;
  131. node->set_show_close_button(true);
  132. node->connect("close_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_request), varray(E), CONNECT_DEFERRED);
  133. }
  134. for (int i = 0; i < agnode->get_input_count(); i++) {
  135. Label *in_name = memnew(Label);
  136. node->add_child(in_name);
  137. in_name->set_text(agnode->get_input_name(i));
  138. node->set_slot(base + i, true, 0, get_theme_color(SNAME("font_color"), SNAME("Label")), false, 0, Color());
  139. }
  140. List<PropertyInfo> pinfo;
  141. agnode->get_parameter_list(&pinfo);
  142. for (const PropertyInfo &F : pinfo) {
  143. if (!(F.usage & PROPERTY_USAGE_EDITOR)) {
  144. continue;
  145. }
  146. String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E) + "/" + F.name;
  147. EditorProperty *prop = EditorInspector::instantiate_property_editor(AnimationTreeEditor::get_singleton()->get_tree(), F.type, base_path, F.hint, F.hint_string, F.usage);
  148. if (prop) {
  149. prop->set_object_and_property(AnimationTreeEditor::get_singleton()->get_tree(), base_path);
  150. prop->update_property();
  151. prop->set_name_split_ratio(0);
  152. prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
  153. node->add_child(prop);
  154. visible_properties.push_back(prop);
  155. }
  156. }
  157. node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged), varray(E));
  158. if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
  159. node->add_child(memnew(HSeparator));
  160. Button *open_in_editor = memnew(Button);
  161. open_in_editor->set_text(TTR("Open Editor"));
  162. open_in_editor->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  163. node->add_child(open_in_editor);
  164. open_in_editor->connect("pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_open_in_editor), varray(E), CONNECT_DEFERRED);
  165. open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);
  166. }
  167. if (agnode->has_filter()) {
  168. node->add_child(memnew(HSeparator));
  169. Button *edit_filters = memnew(Button);
  170. edit_filters->set_text(TTR("Edit Filters"));
  171. edit_filters->set_icon(get_theme_icon(SNAME("AnimationFilter"), SNAME("EditorIcons")));
  172. node->add_child(edit_filters);
  173. edit_filters->connect("pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_edit_filters), varray(E), CONNECT_DEFERRED);
  174. edit_filters->set_h_size_flags(SIZE_SHRINK_CENTER);
  175. }
  176. Ref<AnimationNodeAnimation> anim = agnode;
  177. if (anim.is_valid()) {
  178. MenuButton *mb = memnew(MenuButton);
  179. mb->set_text(anim->get_animation());
  180. mb->set_icon(get_theme_icon(SNAME("Animation"), SNAME("EditorIcons")));
  181. Array options;
  182. node->add_child(memnew(HSeparator));
  183. node->add_child(mb);
  184. ProgressBar *pb = memnew(ProgressBar);
  185. AnimationTree *player = AnimationTreeEditor::get_singleton()->get_tree();
  186. if (player->has_node(player->get_animation_player())) {
  187. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(player->get_node(player->get_animation_player()));
  188. if (ap) {
  189. List<StringName> anims;
  190. ap->get_animation_list(&anims);
  191. for (const StringName &F : anims) {
  192. mb->get_popup()->add_item(F);
  193. options.push_back(F);
  194. }
  195. if (ap->has_animation(anim->get_animation())) {
  196. pb->set_max(ap->get_animation(anim->get_animation())->get_length());
  197. }
  198. }
  199. }
  200. pb->set_percent_visible(false);
  201. pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);
  202. animations[E] = pb;
  203. node->add_child(pb);
  204. mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected), varray(options, E), CONNECT_DEFERRED);
  205. }
  206. Ref<StyleBoxFlat> sb = node->get_theme_stylebox(SNAME("frame"), SNAME("GraphNode"));
  207. Color c = sb->get_border_color();
  208. Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0) : Color(0.0, 0.0, 0.0);
  209. mono_color.a = 0.85;
  210. c = mono_color;
  211. node->add_theme_color_override("title_color", c);
  212. c.a = 0.7;
  213. node->add_theme_color_override("close_color", c);
  214. node->add_theme_color_override("resizer_color", c);
  215. }
  216. List<AnimationNodeBlendTree::NodeConnection> connections;
  217. blend_tree->get_node_connections(&connections);
  218. for (const AnimationNodeBlendTree::NodeConnection &E : connections) {
  219. StringName from = E.output_node;
  220. StringName to = E.input_node;
  221. int to_idx = E.input_index;
  222. graph->connect_node(from, 0, to, to_idx);
  223. }
  224. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  225. graph->set_minimap_opacity(graph_minimap_opacity);
  226. }
  227. void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
  228. file_loaded = ResourceLoader::load(p_file);
  229. if (file_loaded.is_valid()) {
  230. _add_node(MENU_LOAD_FILE_CONFIRM);
  231. }
  232. }
  233. void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
  234. Ref<AnimationNode> anode;
  235. String base_name;
  236. if (p_idx == MENU_LOAD_FILE) {
  237. open_file->clear_filters();
  238. List<String> filters;
  239. ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &filters);
  240. for (const String &E : filters) {
  241. open_file->add_filter("*." + E);
  242. }
  243. open_file->popup_file_dialog();
  244. return;
  245. } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
  246. anode = file_loaded;
  247. file_loaded.unref();
  248. base_name = anode->get_class();
  249. } else if (p_idx == MENU_PASTE) {
  250. anode = EditorSettings::get_singleton()->get_resource_clipboard();
  251. ERR_FAIL_COND(!anode.is_valid());
  252. base_name = anode->get_class();
  253. } else if (!add_options[p_idx].type.is_empty()) {
  254. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(add_options[p_idx].type));
  255. ERR_FAIL_COND(!an);
  256. anode = Ref<AnimationNode>(an);
  257. base_name = add_options[p_idx].name;
  258. } else {
  259. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  260. StringName base_type = add_options[p_idx].script->get_instance_base_type();
  261. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(base_type));
  262. ERR_FAIL_COND(!an);
  263. anode = Ref<AnimationNode>(an);
  264. anode->set_script(add_options[p_idx].script);
  265. base_name = add_options[p_idx].name;
  266. }
  267. Ref<AnimationNodeOutput> out = anode;
  268. if (out.is_valid()) {
  269. EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
  270. return;
  271. }
  272. if (!from_node.is_empty() && anode->get_input_count() == 0) {
  273. from_node = "";
  274. return;
  275. }
  276. Point2 instance_pos = graph->get_scroll_ofs();
  277. if (use_position_from_popup_menu) {
  278. instance_pos += position_from_popup_menu;
  279. } else {
  280. instance_pos += graph->get_size() * 0.5;
  281. }
  282. instance_pos /= graph->get_zoom();
  283. int base = 1;
  284. String name = base_name;
  285. while (blend_tree->has_node(name)) {
  286. base++;
  287. name = base_name + " " + itos(base);
  288. }
  289. undo_redo->create_action(TTR("Add Node to BlendTree"));
  290. undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
  291. undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
  292. if (!from_node.is_empty()) {
  293. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", name, 0, from_node);
  294. from_node = "";
  295. }
  296. if (!to_node.is_empty() && to_slot != -1) {
  297. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", to_node, to_slot, name);
  298. to_node = "";
  299. to_slot = -1;
  300. }
  301. undo_redo->add_do_method(this, "_update_graph");
  302. undo_redo->add_undo_method(this, "_update_graph");
  303. undo_redo->commit_action();
  304. }
  305. void AnimationNodeBlendTreeEditor::_popup(bool p_has_input_ports, const Vector2 &p_popup_position, const Vector2 &p_node_position) {
  306. _update_options_menu(p_has_input_ports);
  307. use_position_from_popup_menu = true;
  308. position_from_popup_menu = p_node_position;
  309. add_node->get_popup()->set_position(p_popup_position);
  310. add_node->get_popup()->reset_size();
  311. add_node->get_popup()->popup();
  312. }
  313. void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
  314. _popup(false, graph->get_screen_position() + graph->get_local_mouse_position(), p_position);
  315. }
  316. void AnimationNodeBlendTreeEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  317. Ref<AnimationNode> node = blend_tree->get_node(p_from);
  318. if (node.is_valid()) {
  319. from_node = p_from;
  320. _popup(true, p_release_position, graph->get_global_mouse_position());
  321. }
  322. }
  323. void AnimationNodeBlendTreeEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  324. Ref<AnimationNode> node = blend_tree->get_node(p_to);
  325. if (node.is_valid()) {
  326. to_node = p_to;
  327. to_slot = p_to_slot;
  328. _popup(false, p_release_position, graph->get_global_mouse_position());
  329. }
  330. }
  331. void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
  332. updating = true;
  333. undo_redo->create_action(TTR("Node Moved"));
  334. undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
  335. undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
  336. undo_redo->add_do_method(this, "_update_graph");
  337. undo_redo->add_undo_method(this, "_update_graph");
  338. undo_redo->commit_action();
  339. updating = false;
  340. }
  341. void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  342. AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  343. if (err != AnimationNodeBlendTree::CONNECTION_OK) {
  344. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  345. return;
  346. }
  347. undo_redo->create_action(TTR("Nodes Connected"));
  348. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  349. undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  350. undo_redo->add_do_method(this, "_update_graph");
  351. undo_redo->add_undo_method(this, "_update_graph");
  352. undo_redo->commit_action();
  353. }
  354. void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  355. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  356. updating = true;
  357. undo_redo->create_action(TTR("Nodes Disconnected"));
  358. undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  359. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  360. undo_redo->add_do_method(this, "_update_graph");
  361. undo_redo->add_undo_method(this, "_update_graph");
  362. undo_redo->commit_action();
  363. updating = false;
  364. }
  365. void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, Array p_options, const String &p_node) {
  366. String option = p_options[p_index];
  367. Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
  368. ERR_FAIL_COND(!anim.is_valid());
  369. undo_redo->create_action(TTR("Set Animation"));
  370. undo_redo->add_do_method(anim.ptr(), "set_animation", option);
  371. undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
  372. undo_redo->add_do_method(this, "_update_graph");
  373. undo_redo->add_undo_method(this, "_update_graph");
  374. undo_redo->commit_action();
  375. }
  376. void AnimationNodeBlendTreeEditor::_delete_request(const String &p_which) {
  377. undo_redo->create_action(TTR("Delete Node"));
  378. undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
  379. 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));
  380. List<AnimationNodeBlendTree::NodeConnection> conns;
  381. blend_tree->get_node_connections(&conns);
  382. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  383. if (E.output_node == p_which || E.input_node == p_which) {
  384. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E.input_node, E.input_index, E.output_node);
  385. }
  386. }
  387. undo_redo->add_do_method(this, "_update_graph");
  388. undo_redo->add_undo_method(this, "_update_graph");
  389. undo_redo->commit_action();
  390. }
  391. void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
  392. List<StringName> to_erase;
  393. if (p_nodes.is_empty()) {
  394. for (int i = 0; i < graph->get_child_count(); i++) {
  395. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  396. if (gn) {
  397. if (gn->is_selected() && gn->is_close_button_visible()) {
  398. to_erase.push_back(gn->get_name());
  399. }
  400. }
  401. }
  402. } else {
  403. for (int i = 0; i < p_nodes.size(); i++) {
  404. to_erase.push_back(p_nodes[i]);
  405. }
  406. }
  407. if (to_erase.is_empty()) {
  408. return;
  409. }
  410. undo_redo->create_action(TTR("Delete Node(s)"));
  411. for (const StringName &F : to_erase) {
  412. _delete_request(F);
  413. }
  414. undo_redo->commit_action();
  415. }
  416. void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
  417. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  418. ERR_FAIL_COND(!gn);
  419. String name = gn->get_name();
  420. Ref<AnimationNode> anode = blend_tree->get_node(name);
  421. ERR_FAIL_COND(!anode.is_valid());
  422. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  423. }
  424. void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
  425. Ref<AnimationNode> an = blend_tree->get_node(p_which);
  426. ERR_FAIL_COND(!an.is_valid());
  427. AnimationTreeEditor::get_singleton()->enter_editor(p_which);
  428. }
  429. void AnimationNodeBlendTreeEditor::_filter_toggled() {
  430. updating = true;
  431. undo_redo->create_action(TTR("Toggle Filter On/Off"));
  432. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
  433. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
  434. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  435. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  436. undo_redo->commit_action();
  437. updating = false;
  438. }
  439. void AnimationNodeBlendTreeEditor::_filter_edited() {
  440. TreeItem *edited = filters->get_edited();
  441. ERR_FAIL_COND(!edited);
  442. NodePath edited_path = edited->get_metadata(0);
  443. bool filtered = edited->is_checked(0);
  444. updating = true;
  445. undo_redo->create_action(TTR("Change Filter"));
  446. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
  447. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
  448. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  449. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  450. undo_redo->commit_action();
  451. updating = false;
  452. }
  453. bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
  454. if (updating || _filter_edit != anode) {
  455. return false;
  456. }
  457. NodePath player_path = AnimationTreeEditor::get_singleton()->get_tree()->get_animation_player();
  458. if (!AnimationTreeEditor::get_singleton()->get_tree()->has_node(player_path)) {
  459. EditorNode::get_singleton()->show_warning(TTR("No animation player set, so unable to retrieve track names."));
  460. return false;
  461. }
  462. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(AnimationTreeEditor::get_singleton()->get_tree()->get_node(player_path));
  463. if (!player) {
  464. EditorNode::get_singleton()->show_warning(TTR("Player path set is invalid, so unable to retrieve track names."));
  465. return false;
  466. }
  467. Node *base = player->get_node(player->get_root());
  468. if (!base) {
  469. EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
  470. return false;
  471. }
  472. updating = true;
  473. HashSet<String> paths;
  474. HashMap<String, RBSet<String>> types;
  475. {
  476. List<StringName> animations;
  477. player->get_animation_list(&animations);
  478. for (const StringName &E : animations) {
  479. Ref<Animation> anim = player->get_animation(E);
  480. for (int i = 0; i < anim->get_track_count(); i++) {
  481. String track_path = anim->track_get_path(i);
  482. paths.insert(track_path);
  483. String track_type_name;
  484. Animation::TrackType track_type = anim->track_get_type(i);
  485. switch (track_type) {
  486. case Animation::TrackType::TYPE_ANIMATION: {
  487. track_type_name = TTR("Anim Clips");
  488. } break;
  489. case Animation::TrackType::TYPE_AUDIO: {
  490. track_type_name = TTR("Audio Clips");
  491. } break;
  492. case Animation::TrackType::TYPE_METHOD: {
  493. track_type_name = TTR("Functions");
  494. } break;
  495. default: {
  496. } break;
  497. }
  498. if (!track_type_name.is_empty()) {
  499. types[track_path].insert(track_type_name);
  500. }
  501. }
  502. }
  503. }
  504. filter_enabled->set_pressed(anode->is_filter_enabled());
  505. filters->clear();
  506. TreeItem *root = filters->create_item();
  507. HashMap<String, TreeItem *> parenthood;
  508. for (const String &E : paths) {
  509. NodePath path = E;
  510. TreeItem *ti = nullptr;
  511. String accum;
  512. for (int i = 0; i < path.get_name_count(); i++) {
  513. String name = path.get_name(i);
  514. if (!accum.is_empty()) {
  515. accum += "/";
  516. }
  517. accum += name;
  518. if (!parenthood.has(accum)) {
  519. if (ti) {
  520. ti = filters->create_item(ti);
  521. } else {
  522. ti = filters->create_item(root);
  523. }
  524. parenthood[accum] = ti;
  525. ti->set_text(0, name);
  526. ti->set_selectable(0, false);
  527. ti->set_editable(0, false);
  528. if (base->has_node(accum)) {
  529. Node *node = base->get_node(accum);
  530. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  531. }
  532. } else {
  533. ti = parenthood[accum];
  534. }
  535. }
  536. Node *node = nullptr;
  537. if (base->has_node(accum)) {
  538. node = base->get_node(accum);
  539. }
  540. if (!node) {
  541. continue; //no node, can't edit
  542. }
  543. if (path.get_subname_count()) {
  544. String concat = path.get_concatenated_subnames();
  545. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  546. if (skeleton && skeleton->find_bone(concat) != -1) {
  547. //path in skeleton
  548. const String &bone = concat;
  549. int idx = skeleton->find_bone(bone);
  550. List<String> bone_path;
  551. while (idx != -1) {
  552. bone_path.push_front(skeleton->get_bone_name(idx));
  553. idx = skeleton->get_bone_parent(idx);
  554. }
  555. accum += ":";
  556. for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
  557. if (F != bone_path.front()) {
  558. accum += "/";
  559. }
  560. accum += F->get();
  561. if (!parenthood.has(accum)) {
  562. ti = filters->create_item(ti);
  563. parenthood[accum] = ti;
  564. ti->set_text(0, F->get());
  565. ti->set_selectable(0, false);
  566. ti->set_editable(0, false);
  567. ti->set_icon(0, get_theme_icon(SNAME("BoneAttachment3D"), SNAME("EditorIcons")));
  568. } else {
  569. ti = parenthood[accum];
  570. }
  571. }
  572. ti->set_editable(0, true);
  573. ti->set_selectable(0, true);
  574. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  575. ti->set_text(0, concat);
  576. ti->set_checked(0, anode->is_path_filtered(path));
  577. ti->set_icon(0, get_theme_icon(SNAME("BoneAttachment3D"), SNAME("EditorIcons")));
  578. ti->set_metadata(0, path);
  579. } else {
  580. //just a property
  581. ti = filters->create_item(ti);
  582. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  583. ti->set_text(0, concat);
  584. ti->set_editable(0, true);
  585. ti->set_selectable(0, true);
  586. ti->set_checked(0, anode->is_path_filtered(path));
  587. ti->set_metadata(0, path);
  588. }
  589. } else {
  590. if (ti) {
  591. //just a node, not a property track
  592. String types_text = "[";
  593. if (types.has(path)) {
  594. RBSet<String>::Iterator F = types[path].begin();
  595. types_text += *F;
  596. while (F) {
  597. types_text += " / " + *F;
  598. ;
  599. ++F;
  600. }
  601. }
  602. types_text += "]";
  603. ti = filters->create_item(ti);
  604. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  605. ti->set_text(0, types_text);
  606. ti->set_editable(0, true);
  607. ti->set_selectable(0, true);
  608. ti->set_checked(0, anode->is_path_filtered(path));
  609. ti->set_metadata(0, path);
  610. }
  611. }
  612. }
  613. updating = false;
  614. return true;
  615. }
  616. void AnimationNodeBlendTreeEditor::_edit_filters(const String &p_which) {
  617. Ref<AnimationNode> anode = blend_tree->get_node(p_which);
  618. ERR_FAIL_COND(!anode.is_valid());
  619. _filter_edit = anode;
  620. if (!_update_filters(anode)) {
  621. return;
  622. }
  623. filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
  624. }
  625. void AnimationNodeBlendTreeEditor::_removed_from_graph() {
  626. if (is_visible()) {
  627. EditorNode::get_singleton()->edit_item(nullptr);
  628. }
  629. }
  630. void AnimationNodeBlendTreeEditor::_update_editor_settings() {
  631. 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(EditorSettings::get_singleton()->get("editors/panning/simple_panning")));
  632. graph->set_warped_panning(bool(EditorSettings::get_singleton()->get("editors/panning/warped_mouse_panning")));
  633. }
  634. void AnimationNodeBlendTreeEditor::_update_theme() {
  635. error_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  636. error_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  637. }
  638. void AnimationNodeBlendTreeEditor::_notification(int p_what) {
  639. switch (p_what) {
  640. case NOTIFICATION_ENTER_TREE: {
  641. _update_editor_settings();
  642. _update_theme();
  643. } break;
  644. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  645. _update_editor_settings();
  646. } break;
  647. case NOTIFICATION_THEME_CHANGED: {
  648. _update_theme();
  649. if (is_visible_in_tree()) {
  650. _update_graph();
  651. }
  652. } break;
  653. case NOTIFICATION_PROCESS: {
  654. String error;
  655. if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  656. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  657. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  658. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  659. }
  660. if (error != error_label->get_text()) {
  661. error_label->set_text(error);
  662. if (!error.is_empty()) {
  663. error_panel->show();
  664. } else {
  665. error_panel->hide();
  666. }
  667. }
  668. List<AnimationNodeBlendTree::NodeConnection> conns;
  669. blend_tree->get_node_connections(&conns);
  670. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  671. float activity = 0;
  672. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  673. if (AnimationTreeEditor::get_singleton()->get_tree() && !AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  674. activity = AnimationTreeEditor::get_singleton()->get_tree()->get_connection_activity(path, E.input_index);
  675. }
  676. graph->set_connection_activity(E.output_node, 0, E.input_node, E.input_index, activity);
  677. }
  678. AnimationTree *graph_player = AnimationTreeEditor::get_singleton()->get_tree();
  679. AnimationPlayer *player = nullptr;
  680. if (graph_player->has_node(graph_player->get_animation_player())) {
  681. player = Object::cast_to<AnimationPlayer>(graph_player->get_node(graph_player->get_animation_player()));
  682. }
  683. if (player) {
  684. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  685. Ref<AnimationNodeAnimation> an = blend_tree->get_node(E.key);
  686. if (an.is_valid()) {
  687. if (player->has_animation(an->get_animation())) {
  688. Ref<Animation> anim = player->get_animation(an->get_animation());
  689. if (anim.is_valid()) {
  690. E.value->set_max(anim->get_length());
  691. //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  692. StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/time";
  693. E.value->set_value(AnimationTreeEditor::get_singleton()->get_tree()->get(time_path));
  694. }
  695. }
  696. }
  697. }
  698. }
  699. for (int i = 0; i < visible_properties.size(); i++) {
  700. visible_properties[i]->update_property();
  701. }
  702. } break;
  703. case NOTIFICATION_VISIBILITY_CHANGED: {
  704. set_process(is_visible_in_tree());
  705. } break;
  706. }
  707. }
  708. void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
  709. if (updating) {
  710. return;
  711. }
  712. updating = true;
  713. blend_tree->set_graph_offset(p_scroll / EDSCALE);
  714. updating = false;
  715. }
  716. void AnimationNodeBlendTreeEditor::_bind_methods() {
  717. ClassDB::bind_method("_update_graph", &AnimationNodeBlendTreeEditor::_update_graph);
  718. ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
  719. }
  720. AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = nullptr;
  721. void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
  722. String prev_name = blend_tree->get_node_name(p_node);
  723. ERR_FAIL_COND(prev_name.is_empty());
  724. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
  725. ERR_FAIL_COND(!gn);
  726. const String &new_name = p_text;
  727. ERR_FAIL_COND(new_name.is_empty() || new_name.contains(".") || new_name.contains("/"));
  728. if (new_name == prev_name) {
  729. return; //nothing to do
  730. }
  731. const String &base_name = new_name;
  732. int base = 1;
  733. String name = base_name;
  734. while (blend_tree->has_node(name)) {
  735. base++;
  736. name = base_name + " " + itos(base);
  737. }
  738. String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
  739. updating = true;
  740. undo_redo->create_action(TTR("Node Renamed"));
  741. undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
  742. undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
  743. undo_redo->add_do_method(AnimationTreeEditor::get_singleton()->get_tree(), "rename_parameter", base_path + prev_name, base_path + name);
  744. undo_redo->add_undo_method(AnimationTreeEditor::get_singleton()->get_tree(), "rename_parameter", base_path + name, base_path + prev_name);
  745. undo_redo->add_do_method(this, "_update_graph");
  746. undo_redo->add_undo_method(this, "_update_graph");
  747. undo_redo->commit_action();
  748. updating = false;
  749. gn->set_name(new_name);
  750. gn->set_size(gn->get_minimum_size());
  751. //change editors accordingly
  752. for (int i = 0; i < visible_properties.size(); i++) {
  753. String pname = visible_properties[i]->get_edited_property().operator String();
  754. if (pname.begins_with(base_path + prev_name)) {
  755. String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
  756. visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
  757. }
  758. }
  759. //recreate connections
  760. graph->clear_connections();
  761. List<AnimationNodeBlendTree::NodeConnection> connections;
  762. blend_tree->get_node_connections(&connections);
  763. for (const AnimationNodeBlendTree::NodeConnection &E : connections) {
  764. StringName from = E.output_node;
  765. StringName to = E.input_node;
  766. int to_idx = E.input_index;
  767. graph->connect_node(from, 0, to, to_idx);
  768. }
  769. //update animations
  770. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  771. if (E.key == prev_name) {
  772. animations[new_name] = animations[prev_name];
  773. animations.erase(prev_name);
  774. break;
  775. }
  776. }
  777. _update_graph(); // Needed to update the signal connections with the new name.
  778. }
  779. void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Node *le, Ref<AnimationNode> p_node) {
  780. if (le == nullptr) {
  781. return; // The text_submitted signal triggered the graph update and freed the LineEdit.
  782. }
  783. _node_renamed(le->call("get_text"), p_node);
  784. }
  785. bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
  786. Ref<AnimationNodeBlendTree> bt = p_node;
  787. return bt.is_valid();
  788. }
  789. void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
  790. if (blend_tree.is_valid()) {
  791. blend_tree->disconnect("removed_from_graph", callable_mp(this, &AnimationNodeBlendTreeEditor::_removed_from_graph));
  792. }
  793. blend_tree = p_node;
  794. if (blend_tree.is_null()) {
  795. hide();
  796. } else {
  797. blend_tree->connect("removed_from_graph", callable_mp(this, &AnimationNodeBlendTreeEditor::_removed_from_graph));
  798. _update_graph();
  799. }
  800. }
  801. AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
  802. singleton = this;
  803. updating = false;
  804. use_position_from_popup_menu = false;
  805. graph = memnew(GraphEdit);
  806. add_child(graph);
  807. graph->add_valid_right_disconnect_type(0);
  808. graph->add_valid_left_disconnect_type(0);
  809. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  810. graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), varray(), CONNECT_DEFERRED);
  811. graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), varray(), CONNECT_DEFERRED);
  812. graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));
  813. graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));
  814. graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));
  815. graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));
  816. graph->connect("connection_to_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_to_empty));
  817. graph->connect("connection_from_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_from_empty));
  818. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  819. graph->set_minimap_opacity(graph_minimap_opacity);
  820. VSeparator *vs = memnew(VSeparator);
  821. graph->get_zoom_hbox()->add_child(vs);
  822. graph->get_zoom_hbox()->move_child(vs, 0);
  823. add_node = memnew(MenuButton);
  824. graph->get_zoom_hbox()->add_child(add_node);
  825. add_node->set_text(TTR("Add Node..."));
  826. graph->get_zoom_hbox()->move_child(add_node, 0);
  827. add_node->get_popup()->connect("id_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_add_node));
  828. add_node->connect("about_to_popup", callable_mp(this, &AnimationNodeBlendTreeEditor::_update_options_menu), varray(false));
  829. add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
  830. add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot", 2));
  831. add_options.push_back(AddOption("Add2", "AnimationNodeAdd2", 2));
  832. add_options.push_back(AddOption("Add3", "AnimationNodeAdd3", 3));
  833. add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2", 2));
  834. add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3", 3));
  835. add_options.push_back(AddOption("Seek", "AnimationNodeTimeSeek", 1));
  836. add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale", 1));
  837. add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
  838. add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
  839. add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
  840. add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
  841. add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
  842. _update_options_menu();
  843. error_panel = memnew(PanelContainer);
  844. add_child(error_panel);
  845. error_label = memnew(Label);
  846. error_panel->add_child(error_label);
  847. error_label->set_text("eh");
  848. filter_dialog = memnew(AcceptDialog);
  849. add_child(filter_dialog);
  850. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  851. VBoxContainer *filter_vbox = memnew(VBoxContainer);
  852. filter_dialog->add_child(filter_vbox);
  853. filter_enabled = memnew(CheckBox);
  854. filter_enabled->set_text(TTR("Enable Filtering"));
  855. filter_enabled->connect("pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_toggled));
  856. filter_vbox->add_child(filter_enabled);
  857. filters = memnew(Tree);
  858. filter_vbox->add_child(filters);
  859. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  860. filters->set_hide_root(true);
  861. filters->connect("item_edited", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_edited));
  862. open_file = memnew(EditorFileDialog);
  863. add_child(open_file);
  864. open_file->set_title(TTR("Open Animation Node"));
  865. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  866. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_file_opened));
  867. undo_redo = EditorNode::get_undo_redo();
  868. }