animation_blend_tree_editor_plugin.cpp 33 KB

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