animation_blend_tree_editor_plugin.cpp 35 KB

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