animation_blend_tree_editor_plugin.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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), CONNECT_DEFERRED);
  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. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  218. graph->set_minimap_opacity(graph_minimap_opacity);
  219. }
  220. void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
  221. file_loaded = ResourceLoader::load(p_file);
  222. if (file_loaded.is_valid()) {
  223. _add_node(MENU_LOAD_FILE_CONFIRM);
  224. }
  225. }
  226. void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
  227. Ref<AnimationNode> anode;
  228. String base_name;
  229. if (p_idx == MENU_LOAD_FILE) {
  230. open_file->clear_filters();
  231. List<String> filters;
  232. ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &filters);
  233. for (List<String>::Element *E = filters.front(); E; E = E->next()) {
  234. open_file->add_filter("*." + E->get());
  235. }
  236. open_file->popup_centered_ratio();
  237. return;
  238. } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
  239. anode = file_loaded;
  240. file_loaded.unref();
  241. base_name = anode->get_class();
  242. } else if (p_idx == MENU_PASTE) {
  243. anode = EditorSettings::get_singleton()->get_resource_clipboard();
  244. ERR_FAIL_COND(!anode.is_valid());
  245. base_name = anode->get_class();
  246. } else if (add_options[p_idx].type != String()) {
  247. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instance(add_options[p_idx].type));
  248. ERR_FAIL_COND(!an);
  249. anode = Ref<AnimationNode>(an);
  250. base_name = add_options[p_idx].name;
  251. } else {
  252. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  253. String base_type = add_options[p_idx].script->get_instance_base_type();
  254. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instance(base_type));
  255. ERR_FAIL_COND(!an);
  256. anode = Ref<AnimationNode>(an);
  257. anode->set_script(add_options[p_idx].script.get_ref_ptr());
  258. base_name = add_options[p_idx].name;
  259. }
  260. Ref<AnimationNodeOutput> out = anode;
  261. if (out.is_valid()) {
  262. EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
  263. return;
  264. }
  265. Point2 instance_pos = graph->get_scroll_ofs();
  266. if (use_popup_menu_position) {
  267. instance_pos += popup_menu_position;
  268. } else {
  269. instance_pos += graph->get_size() * 0.5;
  270. }
  271. instance_pos /= graph->get_zoom();
  272. int base = 1;
  273. String name = base_name;
  274. while (blend_tree->has_node(name)) {
  275. base++;
  276. name = base_name + " " + itos(base);
  277. }
  278. undo_redo->create_action(TTR("Add Node to BlendTree"));
  279. undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
  280. undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
  281. undo_redo->add_do_method(this, "_update_graph");
  282. undo_redo->add_undo_method(this, "_update_graph");
  283. undo_redo->commit_action();
  284. }
  285. void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
  286. updating = true;
  287. undo_redo->create_action(TTR("Node Moved"));
  288. undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
  289. undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
  290. undo_redo->add_do_method(this, "_update_graph");
  291. undo_redo->add_undo_method(this, "_update_graph");
  292. undo_redo->commit_action();
  293. updating = false;
  294. }
  295. void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  296. AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  297. if (err != AnimationNodeBlendTree::CONNECTION_OK) {
  298. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  299. return;
  300. }
  301. undo_redo->create_action(TTR("Nodes Connected"));
  302. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  303. undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  304. undo_redo->add_do_method(this, "_update_graph");
  305. undo_redo->add_undo_method(this, "_update_graph");
  306. undo_redo->commit_action();
  307. }
  308. void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  309. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  310. updating = true;
  311. undo_redo->create_action(TTR("Nodes Disconnected"));
  312. undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  313. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  314. undo_redo->add_do_method(this, "_update_graph");
  315. undo_redo->add_undo_method(this, "_update_graph");
  316. undo_redo->commit_action();
  317. updating = false;
  318. }
  319. void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, Array p_options, const String &p_node) {
  320. String option = p_options[p_index];
  321. Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
  322. ERR_FAIL_COND(!anim.is_valid());
  323. undo_redo->create_action(TTR("Set Animation"));
  324. undo_redo->add_do_method(anim.ptr(), "set_animation", option);
  325. undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
  326. undo_redo->add_do_method(this, "_update_graph");
  327. undo_redo->add_undo_method(this, "_update_graph");
  328. undo_redo->commit_action();
  329. }
  330. void AnimationNodeBlendTreeEditor::_delete_request(const String &p_which) {
  331. undo_redo->create_action(TTR("Delete Node"));
  332. undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
  333. 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));
  334. List<AnimationNodeBlendTree::NodeConnection> conns;
  335. blend_tree->get_node_connections(&conns);
  336. for (List<AnimationNodeBlendTree::NodeConnection>::Element *E = conns.front(); E; E = E->next()) {
  337. if (E->get().output_node == p_which || E->get().input_node == p_which) {
  338. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E->get().input_node, E->get().input_index, E->get().output_node);
  339. }
  340. }
  341. undo_redo->add_do_method(this, "_update_graph");
  342. undo_redo->add_undo_method(this, "_update_graph");
  343. undo_redo->commit_action();
  344. }
  345. void AnimationNodeBlendTreeEditor::_delete_nodes_request() {
  346. List<StringName> to_erase;
  347. for (int i = 0; i < graph->get_child_count(); i++) {
  348. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  349. if (gn) {
  350. if (gn->is_selected() && gn->is_close_button_visible()) {
  351. to_erase.push_back(gn->get_name());
  352. }
  353. }
  354. }
  355. if (to_erase.empty())
  356. return;
  357. undo_redo->create_action(TTR("Delete Node(s)"));
  358. for (List<StringName>::Element *F = to_erase.front(); F; F = F->next()) {
  359. _delete_request(F->get());
  360. }
  361. undo_redo->commit_action();
  362. }
  363. void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
  364. _update_options_menu();
  365. use_popup_menu_position = true;
  366. popup_menu_position = graph->get_local_mouse_position();
  367. add_node->get_popup()->set_position(p_position);
  368. add_node->get_popup()->popup();
  369. }
  370. void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
  371. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  372. ERR_FAIL_COND(!gn);
  373. String name = gn->get_name();
  374. Ref<AnimationNode> anode = blend_tree->get_node(name);
  375. ERR_FAIL_COND(!anode.is_valid());
  376. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  377. }
  378. void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
  379. Ref<AnimationNode> an = blend_tree->get_node(p_which);
  380. ERR_FAIL_COND(!an.is_valid());
  381. AnimationTreeEditor::get_singleton()->enter_editor(p_which);
  382. }
  383. void AnimationNodeBlendTreeEditor::_filter_toggled() {
  384. updating = true;
  385. undo_redo->create_action(TTR("Toggle Filter On/Off"));
  386. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
  387. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
  388. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  389. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  390. undo_redo->commit_action();
  391. updating = false;
  392. }
  393. void AnimationNodeBlendTreeEditor::_filter_edited() {
  394. TreeItem *edited = filters->get_edited();
  395. ERR_FAIL_COND(!edited);
  396. NodePath edited_path = edited->get_metadata(0);
  397. bool filtered = edited->is_checked(0);
  398. updating = true;
  399. undo_redo->create_action(TTR("Change Filter"));
  400. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
  401. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
  402. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  403. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  404. undo_redo->commit_action();
  405. updating = false;
  406. }
  407. bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
  408. if (updating || _filter_edit != anode)
  409. return false;
  410. NodePath player_path = AnimationTreeEditor::get_singleton()->get_tree()->get_animation_player();
  411. if (!AnimationTreeEditor::get_singleton()->get_tree()->has_node(player_path)) {
  412. EditorNode::get_singleton()->show_warning(TTR("No animation player set, so unable to retrieve track names."));
  413. return false;
  414. }
  415. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(AnimationTreeEditor::get_singleton()->get_tree()->get_node(player_path));
  416. if (!player) {
  417. EditorNode::get_singleton()->show_warning(TTR("Player path set is invalid, so unable to retrieve track names."));
  418. return false;
  419. }
  420. Node *base = player->get_node(player->get_root());
  421. if (!base) {
  422. EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
  423. return false;
  424. }
  425. updating = true;
  426. Set<String> paths;
  427. HashMap<String, Set<String> > types;
  428. {
  429. List<StringName> animations;
  430. player->get_animation_list(&animations);
  431. for (List<StringName>::Element *E = animations.front(); E; E = E->next()) {
  432. Ref<Animation> anim = player->get_animation(E->get());
  433. for (int i = 0; i < anim->get_track_count(); i++) {
  434. String track_path = anim->track_get_path(i);
  435. paths.insert(track_path);
  436. String track_type_name;
  437. Animation::TrackType track_type = anim->track_get_type(i);
  438. switch (track_type) {
  439. case Animation::TrackType::TYPE_ANIMATION: {
  440. track_type_name = TTR("Anim Clips");
  441. } break;
  442. case Animation::TrackType::TYPE_AUDIO: {
  443. track_type_name = TTR("Audio Clips");
  444. } break;
  445. case Animation::TrackType::TYPE_METHOD: {
  446. track_type_name = TTR("Functions");
  447. } break;
  448. default: {
  449. } break;
  450. }
  451. if (!track_type_name.empty()) {
  452. types[track_path].insert(track_type_name);
  453. }
  454. }
  455. }
  456. }
  457. filter_enabled->set_pressed(anode->is_filter_enabled());
  458. filters->clear();
  459. TreeItem *root = filters->create_item();
  460. Map<String, TreeItem *> parenthood;
  461. for (Set<String>::Element *E = paths.front(); E; E = E->next()) {
  462. NodePath path = E->get();
  463. TreeItem *ti = NULL;
  464. String accum;
  465. for (int i = 0; i < path.get_name_count(); i++) {
  466. String name = path.get_name(i);
  467. if (accum != String()) {
  468. accum += "/";
  469. }
  470. accum += name;
  471. if (!parenthood.has(accum)) {
  472. if (ti) {
  473. ti = filters->create_item(ti);
  474. } else {
  475. ti = filters->create_item(root);
  476. }
  477. parenthood[accum] = ti;
  478. ti->set_text(0, name);
  479. ti->set_selectable(0, false);
  480. ti->set_editable(0, false);
  481. if (base->has_node(accum)) {
  482. Node *node = base->get_node(accum);
  483. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  484. }
  485. } else {
  486. ti = parenthood[accum];
  487. }
  488. }
  489. Node *node = NULL;
  490. if (base->has_node(accum)) {
  491. node = base->get_node(accum);
  492. }
  493. if (!node)
  494. continue; //no node, can't edit
  495. if (path.get_subname_count()) {
  496. String concat = path.get_concatenated_subnames();
  497. Skeleton *skeleton = Object::cast_to<Skeleton>(node);
  498. if (skeleton && skeleton->find_bone(concat) != -1) {
  499. //path in skeleton
  500. const String &bone = concat;
  501. int idx = skeleton->find_bone(bone);
  502. List<String> bone_path;
  503. while (idx != -1) {
  504. bone_path.push_front(skeleton->get_bone_name(idx));
  505. idx = skeleton->get_bone_parent(idx);
  506. }
  507. accum += ":";
  508. for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
  509. if (F != bone_path.front()) {
  510. accum += "/";
  511. }
  512. accum += F->get();
  513. if (!parenthood.has(accum)) {
  514. ti = filters->create_item(ti);
  515. parenthood[accum] = ti;
  516. ti->set_text(0, F->get());
  517. ti->set_selectable(0, false);
  518. ti->set_editable(0, false);
  519. ti->set_icon(0, get_icon("BoneAttachment", "EditorIcons"));
  520. } else {
  521. ti = parenthood[accum];
  522. }
  523. }
  524. ti->set_editable(0, true);
  525. ti->set_selectable(0, true);
  526. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  527. ti->set_text(0, concat);
  528. ti->set_checked(0, anode->is_path_filtered(path));
  529. ti->set_icon(0, get_icon("BoneAttachment", "EditorIcons"));
  530. ti->set_metadata(0, path);
  531. } else {
  532. //just a property
  533. ti = filters->create_item(ti);
  534. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  535. ti->set_text(0, concat);
  536. ti->set_editable(0, true);
  537. ti->set_selectable(0, true);
  538. ti->set_checked(0, anode->is_path_filtered(path));
  539. ti->set_metadata(0, path);
  540. }
  541. } else {
  542. if (ti) {
  543. //just a node, not a property track
  544. String types_text = "[";
  545. if (types.has(path)) {
  546. Set<String>::Element *F = types[path].front();
  547. types_text += F->get();
  548. while (F->next()) {
  549. F = F->next();
  550. types_text += " / " + F->get();
  551. }
  552. }
  553. types_text += "]";
  554. ti = filters->create_item(ti);
  555. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  556. ti->set_text(0, types_text);
  557. ti->set_editable(0, true);
  558. ti->set_selectable(0, true);
  559. ti->set_checked(0, anode->is_path_filtered(path));
  560. ti->set_metadata(0, path);
  561. }
  562. }
  563. }
  564. updating = false;
  565. return true;
  566. }
  567. void AnimationNodeBlendTreeEditor::_edit_filters(const String &p_which) {
  568. Ref<AnimationNode> anode = blend_tree->get_node(p_which);
  569. ERR_FAIL_COND(!anode.is_valid());
  570. _filter_edit = anode;
  571. if (!_update_filters(anode))
  572. return;
  573. filter_dialog->popup_centered_minsize(Size2(500, 500) * EDSCALE);
  574. }
  575. void AnimationNodeBlendTreeEditor::_removed_from_graph() {
  576. if (is_visible()) {
  577. EditorNode::get_singleton()->edit_item(NULL);
  578. }
  579. }
  580. void AnimationNodeBlendTreeEditor::_notification(int p_what) {
  581. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  582. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  583. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  584. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree())
  585. _update_graph();
  586. }
  587. if (p_what == NOTIFICATION_PROCESS) {
  588. String error;
  589. if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  590. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  591. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  592. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  593. }
  594. if (error != error_label->get_text()) {
  595. error_label->set_text(error);
  596. if (error != String()) {
  597. error_panel->show();
  598. } else {
  599. error_panel->hide();
  600. }
  601. }
  602. List<AnimationNodeBlendTree::NodeConnection> conns;
  603. blend_tree->get_node_connections(&conns);
  604. for (List<AnimationNodeBlendTree::NodeConnection>::Element *E = conns.front(); E; E = E->next()) {
  605. float activity = 0;
  606. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E->get().input_node;
  607. if (AnimationTreeEditor::get_singleton()->get_tree() && !AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  608. activity = AnimationTreeEditor::get_singleton()->get_tree()->get_connection_activity(path, E->get().input_index);
  609. }
  610. graph->set_connection_activity(E->get().output_node, 0, E->get().input_node, E->get().input_index, activity);
  611. }
  612. AnimationTree *graph_player = AnimationTreeEditor::get_singleton()->get_tree();
  613. AnimationPlayer *player = NULL;
  614. if (graph_player->has_node(graph_player->get_animation_player())) {
  615. player = Object::cast_to<AnimationPlayer>(graph_player->get_node(graph_player->get_animation_player()));
  616. }
  617. if (player) {
  618. for (Map<StringName, ProgressBar *>::Element *E = animations.front(); E; E = E->next()) {
  619. Ref<AnimationNodeAnimation> an = blend_tree->get_node(E->key());
  620. if (an.is_valid()) {
  621. if (player->has_animation(an->get_animation())) {
  622. Ref<Animation> anim = player->get_animation(an->get_animation());
  623. if (anim.is_valid()) {
  624. E->get()->set_max(anim->get_length());
  625. //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E->get().input_node;
  626. StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E->key()) + "/time";
  627. E->get()->set_value(AnimationTreeEditor::get_singleton()->get_tree()->get(time_path));
  628. }
  629. }
  630. }
  631. }
  632. }
  633. for (int i = 0; i < visible_properties.size(); i++) {
  634. visible_properties[i]->update_property();
  635. }
  636. }
  637. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  638. set_process(is_visible_in_tree());
  639. }
  640. }
  641. void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
  642. if (updating)
  643. return;
  644. updating = true;
  645. blend_tree->set_graph_offset(p_scroll / EDSCALE);
  646. updating = false;
  647. }
  648. void AnimationNodeBlendTreeEditor::_bind_methods() {
  649. ClassDB::bind_method("_update_graph", &AnimationNodeBlendTreeEditor::_update_graph);
  650. ClassDB::bind_method("_add_node", &AnimationNodeBlendTreeEditor::_add_node);
  651. ClassDB::bind_method("_node_dragged", &AnimationNodeBlendTreeEditor::_node_dragged);
  652. ClassDB::bind_method("_node_renamed", &AnimationNodeBlendTreeEditor::_node_renamed);
  653. ClassDB::bind_method("_node_renamed_focus_out", &AnimationNodeBlendTreeEditor::_node_renamed_focus_out);
  654. ClassDB::bind_method("_connection_request", &AnimationNodeBlendTreeEditor::_connection_request);
  655. ClassDB::bind_method("_disconnection_request", &AnimationNodeBlendTreeEditor::_disconnection_request);
  656. ClassDB::bind_method("_node_selected", &AnimationNodeBlendTreeEditor::_node_selected);
  657. ClassDB::bind_method("_open_in_editor", &AnimationNodeBlendTreeEditor::_open_in_editor);
  658. ClassDB::bind_method("_scroll_changed", &AnimationNodeBlendTreeEditor::_scroll_changed);
  659. ClassDB::bind_method("_delete_request", &AnimationNodeBlendTreeEditor::_delete_request);
  660. ClassDB::bind_method("_delete_nodes_request", &AnimationNodeBlendTreeEditor::_delete_nodes_request);
  661. ClassDB::bind_method("_popup_request", &AnimationNodeBlendTreeEditor::_popup_request);
  662. ClassDB::bind_method("_edit_filters", &AnimationNodeBlendTreeEditor::_edit_filters);
  663. ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
  664. ClassDB::bind_method("_filter_edited", &AnimationNodeBlendTreeEditor::_filter_edited);
  665. ClassDB::bind_method("_filter_toggled", &AnimationNodeBlendTreeEditor::_filter_toggled);
  666. ClassDB::bind_method("_removed_from_graph", &AnimationNodeBlendTreeEditor::_removed_from_graph);
  667. ClassDB::bind_method("_property_changed", &AnimationNodeBlendTreeEditor::_property_changed);
  668. ClassDB::bind_method("_file_opened", &AnimationNodeBlendTreeEditor::_file_opened);
  669. ClassDB::bind_method("_update_options_menu", &AnimationNodeBlendTreeEditor::_update_options_menu);
  670. ClassDB::bind_method("_anim_selected", &AnimationNodeBlendTreeEditor::_anim_selected);
  671. }
  672. AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = NULL;
  673. void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
  674. String prev_name = blend_tree->get_node_name(p_node);
  675. ERR_FAIL_COND(prev_name == String());
  676. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
  677. ERR_FAIL_COND(!gn);
  678. const String &new_name = p_text;
  679. ERR_FAIL_COND(new_name == "" || new_name.find(".") != -1 || new_name.find("/") != -1);
  680. if (new_name == prev_name) {
  681. return; //nothing to do
  682. }
  683. const String &base_name = new_name;
  684. int base = 1;
  685. String name = base_name;
  686. while (blend_tree->has_node(name)) {
  687. base++;
  688. name = base_name + " " + itos(base);
  689. }
  690. String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
  691. updating = true;
  692. undo_redo->create_action(TTR("Node Renamed"));
  693. undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
  694. undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
  695. undo_redo->add_do_method(AnimationTreeEditor::get_singleton()->get_tree(), "rename_parameter", base_path + prev_name, base_path + name);
  696. undo_redo->add_undo_method(AnimationTreeEditor::get_singleton()->get_tree(), "rename_parameter", base_path + name, base_path + prev_name);
  697. undo_redo->add_do_method(this, "_update_graph");
  698. undo_redo->add_undo_method(this, "_update_graph");
  699. undo_redo->commit_action();
  700. updating = false;
  701. gn->set_name(new_name);
  702. gn->set_size(gn->get_minimum_size());
  703. //change editors accordingly
  704. for (int i = 0; i < visible_properties.size(); i++) {
  705. String pname = visible_properties[i]->get_edited_property().operator String();
  706. if (pname.begins_with(base_path + prev_name)) {
  707. String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
  708. visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
  709. }
  710. }
  711. //recreate connections
  712. graph->clear_connections();
  713. List<AnimationNodeBlendTree::NodeConnection> connections;
  714. blend_tree->get_node_connections(&connections);
  715. for (List<AnimationNodeBlendTree::NodeConnection>::Element *E = connections.front(); E; E = E->next()) {
  716. StringName from = E->get().output_node;
  717. StringName to = E->get().input_node;
  718. int to_idx = E->get().input_index;
  719. graph->connect_node(from, 0, to, to_idx);
  720. }
  721. //update animations
  722. for (Map<StringName, ProgressBar *>::Element *E = animations.front(); E; E = E->next()) {
  723. if (E->key() == prev_name) {
  724. animations[new_name] = animations[prev_name];
  725. animations.erase(prev_name);
  726. break;
  727. }
  728. }
  729. _update_graph(); // Needed to update the signal connections with the new name.
  730. }
  731. void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Node *le, Ref<AnimationNode> p_node) {
  732. _node_renamed(le->call("get_text"), p_node);
  733. }
  734. bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
  735. Ref<AnimationNodeBlendTree> bt = p_node;
  736. return bt.is_valid();
  737. }
  738. void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
  739. if (blend_tree.is_valid()) {
  740. blend_tree->disconnect("removed_from_graph", this, "_removed_from_graph");
  741. }
  742. blend_tree = p_node;
  743. if (blend_tree.is_null()) {
  744. hide();
  745. } else {
  746. blend_tree->connect("removed_from_graph", this, "_removed_from_graph");
  747. _update_graph();
  748. }
  749. }
  750. AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
  751. singleton = this;
  752. updating = false;
  753. use_popup_menu_position = false;
  754. graph = memnew(GraphEdit);
  755. add_child(graph);
  756. graph->add_valid_right_disconnect_type(0);
  757. graph->add_valid_left_disconnect_type(0);
  758. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  759. graph->connect("connection_request", this, "_connection_request", varray(), CONNECT_DEFERRED);
  760. graph->connect("disconnection_request", this, "_disconnection_request", varray(), CONNECT_DEFERRED);
  761. graph->connect("node_selected", this, "_node_selected");
  762. graph->connect("scroll_offset_changed", this, "_scroll_changed");
  763. graph->connect("delete_nodes_request", this, "_delete_nodes_request");
  764. graph->connect("popup_request", this, "_popup_request");
  765. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  766. graph->set_minimap_opacity(graph_minimap_opacity);
  767. VSeparator *vs = memnew(VSeparator);
  768. graph->get_zoom_hbox()->add_child(vs);
  769. graph->get_zoom_hbox()->move_child(vs, 0);
  770. add_node = memnew(MenuButton);
  771. graph->get_zoom_hbox()->add_child(add_node);
  772. add_node->set_text(TTR("Add Node..."));
  773. graph->get_zoom_hbox()->move_child(add_node, 0);
  774. add_node->get_popup()->connect("id_pressed", this, "_add_node");
  775. add_node->connect("about_to_show", this, "_update_options_menu");
  776. add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
  777. add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot"));
  778. add_options.push_back(AddOption("Add2", "AnimationNodeAdd2"));
  779. add_options.push_back(AddOption("Add3", "AnimationNodeAdd3"));
  780. add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2"));
  781. add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3"));
  782. add_options.push_back(AddOption("Seek", "AnimationNodeTimeSeek"));
  783. add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale"));
  784. add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
  785. add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
  786. add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
  787. add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
  788. add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
  789. _update_options_menu();
  790. error_panel = memnew(PanelContainer);
  791. add_child(error_panel);
  792. error_label = memnew(Label);
  793. error_panel->add_child(error_label);
  794. error_label->set_text("eh");
  795. filter_dialog = memnew(AcceptDialog);
  796. add_child(filter_dialog);
  797. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  798. VBoxContainer *filter_vbox = memnew(VBoxContainer);
  799. filter_dialog->add_child(filter_vbox);
  800. filter_enabled = memnew(CheckBox);
  801. filter_enabled->set_text(TTR("Enable Filtering"));
  802. filter_enabled->connect("pressed", this, "_filter_toggled");
  803. filter_vbox->add_child(filter_enabled);
  804. filters = memnew(Tree);
  805. filter_vbox->add_child(filters);
  806. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  807. filters->set_hide_root(true);
  808. filters->connect("item_edited", this, "_filter_edited");
  809. open_file = memnew(EditorFileDialog);
  810. add_child(open_file);
  811. open_file->set_title(TTR("Open Animation Node"));
  812. open_file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  813. open_file->connect("file_selected", this, "_file_opened");
  814. undo_redo = EditorNode::get_undo_redo();
  815. }