animation_blend_tree_editor_plugin.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  1. /**************************************************************************/
  2. /* animation_blend_tree_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation_blend_tree_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/editor_inspector.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/editor_file_dialog.h"
  41. #include "editor/themes/editor_scale.h"
  42. #include "scene/3d/skeleton_3d.h"
  43. #include "scene/animation/animation_player.h"
  44. #include "scene/gui/check_box.h"
  45. #include "scene/gui/menu_button.h"
  46. #include "scene/gui/panel.h"
  47. #include "scene/gui/progress_bar.h"
  48. #include "scene/gui/separator.h"
  49. #include "scene/gui/view_panner.h"
  50. #include "scene/main/window.h"
  51. #include "scene/resources/style_box_flat.h"
  52. void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) {
  53. for (int i = 0; i < add_options.size(); i++) {
  54. ERR_FAIL_COND(add_options[i].script == p_script);
  55. }
  56. AddOption ao;
  57. ao.name = p_name;
  58. ao.script = p_script;
  59. add_options.push_back(ao);
  60. _update_options_menu();
  61. }
  62. void AnimationNodeBlendTreeEditor::remove_custom_type(const Ref<Script> &p_script) {
  63. for (int i = 0; i < add_options.size(); i++) {
  64. if (add_options[i].script == p_script) {
  65. add_options.remove_at(i);
  66. return;
  67. }
  68. }
  69. _update_options_menu();
  70. }
  71. void AnimationNodeBlendTreeEditor::_update_options_menu(bool p_has_input_ports) {
  72. add_node->get_popup()->clear();
  73. add_node->get_popup()->reset_size();
  74. for (int i = 0; i < add_options.size(); i++) {
  75. if (p_has_input_ports && add_options[i].input_port_count == 0) {
  76. continue;
  77. }
  78. add_node->get_popup()->add_item(add_options[i].name, i);
  79. }
  80. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  81. if (clipb.is_valid()) {
  82. add_node->get_popup()->add_separator();
  83. add_node->get_popup()->add_item(TTR("Paste"), MENU_PASTE);
  84. }
  85. add_node->get_popup()->add_separator();
  86. add_node->get_popup()->add_item(TTR("Load..."), MENU_LOAD_FILE);
  87. use_position_from_popup_menu = false;
  88. }
  89. Size2 AnimationNodeBlendTreeEditor::get_minimum_size() const {
  90. return Size2(10, 200);
  91. }
  92. void AnimationNodeBlendTreeEditor::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
  93. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  94. if (!tree) {
  95. return;
  96. }
  97. updating = true;
  98. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  99. undo_redo->create_action(vformat(TTR("Parameter Changed: %s"), p_property), UndoRedo::MERGE_ENDS);
  100. undo_redo->add_do_property(tree, p_property, p_value);
  101. undo_redo->add_undo_property(tree, p_property, tree->get(p_property));
  102. undo_redo->add_do_method(this, "update_graph");
  103. undo_redo->add_undo_method(this, "update_graph");
  104. undo_redo->commit_action();
  105. updating = false;
  106. }
  107. void AnimationNodeBlendTreeEditor::update_graph() {
  108. if (updating || blend_tree.is_null()) {
  109. return;
  110. }
  111. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  112. if (!tree) {
  113. return;
  114. }
  115. visible_properties.clear();
  116. graph->set_scroll_offset(blend_tree->get_graph_offset() * EDSCALE);
  117. graph->clear_connections();
  118. //erase all nodes
  119. for (int i = 0; i < graph->get_child_count(); i++) {
  120. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  121. memdelete(graph->get_child(i));
  122. i--;
  123. }
  124. }
  125. animations.clear();
  126. List<StringName> nodes;
  127. blend_tree->get_node_list(&nodes);
  128. for (const StringName &E : nodes) {
  129. GraphNode *node = memnew(GraphNode);
  130. graph->add_child(node);
  131. node->set_draggable(!read_only);
  132. Ref<AnimationNode> agnode = blend_tree->get_node(E);
  133. ERR_CONTINUE(!agnode.is_valid());
  134. node->set_position_offset(blend_tree->get_node_position(E) * EDSCALE);
  135. node->set_title(agnode->get_caption());
  136. node->set_name(E);
  137. int base = 0;
  138. if (String(E) != "output") {
  139. LineEdit *name = memnew(LineEdit);
  140. name->set_text(E);
  141. name->set_editable(!read_only);
  142. name->set_expand_to_text_length_enabled(true);
  143. node->add_child(name);
  144. node->set_slot(0, false, 0, Color(), true, read_only ? -1 : 0, get_theme_color(SNAME("font_color"), SNAME("Label")));
  145. name->connect("text_submitted", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed).bind(agnode), CONNECT_DEFERRED);
  146. name->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out).bind(agnode), CONNECT_DEFERRED);
  147. name->connect("text_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed), CONNECT_DEFERRED);
  148. base = 1;
  149. agnode->set_deletable(true);
  150. if (!read_only) {
  151. Button *delete_button = memnew(Button);
  152. delete_button->set_flat(true);
  153. delete_button->set_focus_mode(FOCUS_NONE);
  154. delete_button->set_icon(get_editor_theme_icon(SNAME("Close")));
  155. delete_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_node_request).bind(E), CONNECT_DEFERRED);
  156. node->get_titlebar_hbox()->add_child(delete_button);
  157. }
  158. }
  159. for (int i = 0; i < agnode->get_input_count(); i++) {
  160. Label *in_name = memnew(Label);
  161. node->add_child(in_name);
  162. in_name->set_text(agnode->get_input_name(i));
  163. node->set_slot(base + i, true, read_only ? -1 : 0, get_theme_color(SNAME("font_color"), SNAME("Label")), false, 0, Color());
  164. }
  165. List<PropertyInfo> pinfo;
  166. agnode->get_parameter_list(&pinfo);
  167. for (const PropertyInfo &F : pinfo) {
  168. if (!(F.usage & PROPERTY_USAGE_EDITOR)) {
  169. continue;
  170. }
  171. String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E) + "/" + F.name;
  172. EditorProperty *prop = EditorInspector::instantiate_property_editor(tree, F.type, base_path, F.hint, F.hint_string, F.usage);
  173. if (prop) {
  174. prop->set_read_only(read_only || (F.usage & PROPERTY_USAGE_READ_ONLY));
  175. prop->set_object_and_property(tree, base_path);
  176. prop->update_property();
  177. prop->set_name_split_ratio(0);
  178. prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
  179. node->add_child(prop);
  180. visible_properties.push_back(prop);
  181. }
  182. }
  183. node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));
  184. if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
  185. node->add_child(memnew(HSeparator));
  186. Button *open_in_editor = memnew(Button);
  187. open_in_editor->set_text(TTR("Open Editor"));
  188. open_in_editor->set_icon(get_editor_theme_icon(SNAME("Edit")));
  189. node->add_child(open_in_editor);
  190. open_in_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_open_in_editor).bind(E), CONNECT_DEFERRED);
  191. open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);
  192. }
  193. if (agnode->has_filter()) {
  194. node->add_child(memnew(HSeparator));
  195. Button *inspect_filters = memnew(Button);
  196. if (read_only) {
  197. inspect_filters->set_text(TTR("Inspect Filters"));
  198. } else {
  199. inspect_filters->set_text(TTR("Edit Filters"));
  200. }
  201. inspect_filters->set_icon(get_editor_theme_icon(SNAME("AnimationFilter")));
  202. node->add_child(inspect_filters);
  203. inspect_filters->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_inspect_filters).bind(E), CONNECT_DEFERRED);
  204. inspect_filters->set_h_size_flags(SIZE_SHRINK_CENTER);
  205. }
  206. Ref<AnimationNodeAnimation> anim = agnode;
  207. if (anim.is_valid()) {
  208. MenuButton *mb = memnew(MenuButton);
  209. mb->set_text(anim->get_animation());
  210. mb->set_icon(get_editor_theme_icon(SNAME("Animation")));
  211. mb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  212. mb->set_disabled(read_only);
  213. Array options;
  214. node->add_child(memnew(HSeparator));
  215. node->add_child(mb);
  216. ProgressBar *pb = memnew(ProgressBar);
  217. List<StringName> anims;
  218. tree->get_animation_list(&anims);
  219. for (const StringName &F : anims) {
  220. mb->get_popup()->add_item(F);
  221. options.push_back(F);
  222. }
  223. pb->set_show_percentage(false);
  224. pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);
  225. animations[E] = pb;
  226. node->add_child(pb);
  227. mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);
  228. }
  229. // TODO: Avoid using strings, expose a method on GraphNode instead.
  230. Ref<StyleBoxFlat> sb = node->get_theme_stylebox(SNAME("panel"));
  231. Color c = sb->get_border_color();
  232. 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);
  233. mono_color.a = 0.85;
  234. c = mono_color;
  235. node->add_theme_color_override("title_color", c);
  236. c.a = 0.7;
  237. node->add_theme_color_override("close_color", c);
  238. node->add_theme_color_override("resizer_color", c);
  239. }
  240. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  241. blend_tree->get_node_connections(&node_connections);
  242. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  243. StringName from = E.output_node;
  244. StringName to = E.input_node;
  245. int to_idx = E.input_index;
  246. graph->connect_node(from, 0, to, to_idx);
  247. }
  248. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  249. graph->set_minimap_opacity(graph_minimap_opacity);
  250. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  251. graph->set_connection_lines_curvature(graph_lines_curvature);
  252. }
  253. void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
  254. file_loaded = ResourceLoader::load(p_file);
  255. if (file_loaded.is_valid()) {
  256. _add_node(MENU_LOAD_FILE_CONFIRM);
  257. } else {
  258. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
  259. }
  260. }
  261. void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
  262. Ref<AnimationNode> anode;
  263. String base_name;
  264. if (p_idx == MENU_LOAD_FILE) {
  265. open_file->clear_filters();
  266. List<String> ext_filters;
  267. ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &ext_filters);
  268. for (const String &E : ext_filters) {
  269. open_file->add_filter("*." + E);
  270. }
  271. open_file->popup_file_dialog();
  272. return;
  273. } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
  274. anode = file_loaded;
  275. file_loaded.unref();
  276. base_name = anode->get_class();
  277. } else if (p_idx == MENU_PASTE) {
  278. anode = EditorSettings::get_singleton()->get_resource_clipboard();
  279. ERR_FAIL_COND(!anode.is_valid());
  280. base_name = anode->get_class();
  281. } else if (!add_options[p_idx].type.is_empty()) {
  282. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(add_options[p_idx].type));
  283. ERR_FAIL_NULL(an);
  284. anode = Ref<AnimationNode>(an);
  285. base_name = add_options[p_idx].name;
  286. } else {
  287. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  288. StringName base_type = add_options[p_idx].script->get_instance_base_type();
  289. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(base_type));
  290. ERR_FAIL_NULL(an);
  291. anode = Ref<AnimationNode>(an);
  292. anode->set_script(add_options[p_idx].script);
  293. base_name = add_options[p_idx].name;
  294. }
  295. Ref<AnimationNodeOutput> out = anode;
  296. if (out.is_valid()) {
  297. EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
  298. return;
  299. }
  300. if (!from_node.is_empty() && anode->get_input_count() == 0) {
  301. from_node = "";
  302. return;
  303. }
  304. Point2 instance_pos = graph->get_scroll_offset();
  305. if (use_position_from_popup_menu) {
  306. instance_pos += position_from_popup_menu;
  307. } else {
  308. instance_pos += graph->get_size() * 0.5;
  309. }
  310. instance_pos /= graph->get_zoom();
  311. int base = 1;
  312. String name = base_name;
  313. while (blend_tree->has_node(name)) {
  314. base++;
  315. name = base_name + " " + itos(base);
  316. }
  317. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  318. undo_redo->create_action(TTR("Add Node to BlendTree"));
  319. undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
  320. undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
  321. if (!from_node.is_empty()) {
  322. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", name, 0, from_node);
  323. from_node = "";
  324. }
  325. if (!to_node.is_empty() && to_slot != -1) {
  326. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", to_node, to_slot, name);
  327. to_node = "";
  328. to_slot = -1;
  329. }
  330. undo_redo->add_do_method(this, "update_graph");
  331. undo_redo->add_undo_method(this, "update_graph");
  332. undo_redo->commit_action();
  333. }
  334. void AnimationNodeBlendTreeEditor::_popup(bool p_has_input_ports, const Vector2 &p_node_position) {
  335. _update_options_menu(p_has_input_ports);
  336. use_position_from_popup_menu = true;
  337. position_from_popup_menu = p_node_position;
  338. add_node->get_popup()->set_position(graph->get_screen_position() + graph->get_local_mouse_position());
  339. add_node->get_popup()->reset_size();
  340. add_node->get_popup()->popup();
  341. }
  342. void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
  343. if (read_only) {
  344. return;
  345. }
  346. _popup(false, p_position);
  347. }
  348. void AnimationNodeBlendTreeEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  349. if (read_only) {
  350. return;
  351. }
  352. Ref<AnimationNode> node = blend_tree->get_node(p_from);
  353. if (node.is_valid()) {
  354. from_node = p_from;
  355. _popup(true, p_release_position);
  356. }
  357. }
  358. void AnimationNodeBlendTreeEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  359. if (read_only) {
  360. return;
  361. }
  362. Ref<AnimationNode> node = blend_tree->get_node(p_to);
  363. if (node.is_valid()) {
  364. to_node = p_to;
  365. to_slot = p_to_slot;
  366. _popup(false, p_release_position);
  367. }
  368. }
  369. void AnimationNodeBlendTreeEditor::_popup_hide() {
  370. to_node = "";
  371. to_slot = -1;
  372. }
  373. void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
  374. updating = true;
  375. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  376. undo_redo->create_action(TTR("Node Moved"));
  377. undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
  378. undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
  379. undo_redo->add_do_method(this, "update_graph");
  380. undo_redo->add_undo_method(this, "update_graph");
  381. undo_redo->commit_action();
  382. updating = false;
  383. }
  384. void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  385. if (read_only) {
  386. return;
  387. }
  388. AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  389. if (err == AnimationNodeBlendTree::CONNECTION_ERROR_CONNECTION_EXISTS) {
  390. blend_tree->disconnect_node(p_to, p_to_index);
  391. err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  392. }
  393. if (err != AnimationNodeBlendTree::CONNECTION_OK) {
  394. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  395. return;
  396. }
  397. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  398. undo_redo->create_action(TTR("Nodes Connected"));
  399. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  400. undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  401. undo_redo->add_do_method(this, "update_graph");
  402. undo_redo->add_undo_method(this, "update_graph");
  403. undo_redo->commit_action();
  404. }
  405. void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  406. if (read_only) {
  407. return;
  408. }
  409. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  410. updating = true;
  411. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  412. undo_redo->create_action(TTR("Nodes Disconnected"));
  413. undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  414. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  415. undo_redo->add_do_method(this, "update_graph");
  416. undo_redo->add_undo_method(this, "update_graph");
  417. undo_redo->commit_action();
  418. updating = false;
  419. }
  420. void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, const Array &p_options, const String &p_node) {
  421. String option = p_options[p_index];
  422. Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
  423. ERR_FAIL_COND(!anim.is_valid());
  424. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  425. undo_redo->create_action(TTR("Set Animation"));
  426. undo_redo->add_do_method(anim.ptr(), "set_animation", option);
  427. undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
  428. undo_redo->add_do_method(this, "update_graph");
  429. undo_redo->add_undo_method(this, "update_graph");
  430. undo_redo->commit_action();
  431. }
  432. void AnimationNodeBlendTreeEditor::_delete_node_request(const String &p_which) {
  433. if (read_only) {
  434. return;
  435. }
  436. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  437. undo_redo->create_action(TTR("Delete Node"));
  438. undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
  439. 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));
  440. List<AnimationNodeBlendTree::NodeConnection> conns;
  441. blend_tree->get_node_connections(&conns);
  442. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  443. if (E.output_node == p_which || E.input_node == p_which) {
  444. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E.input_node, E.input_index, E.output_node);
  445. }
  446. }
  447. undo_redo->add_do_method(this, "update_graph");
  448. undo_redo->add_undo_method(this, "update_graph");
  449. undo_redo->commit_action();
  450. }
  451. void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
  452. if (read_only) {
  453. return;
  454. }
  455. List<StringName> to_erase;
  456. if (p_nodes.is_empty()) {
  457. for (int i = 0; i < graph->get_child_count(); i++) {
  458. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  459. if (gn && gn->is_selected()) {
  460. Ref<AnimationNode> anode = blend_tree->get_node(gn->get_name());
  461. if (anode->is_deletable()) {
  462. to_erase.push_back(gn->get_name());
  463. }
  464. }
  465. }
  466. } else {
  467. for (int i = 0; i < p_nodes.size(); i++) {
  468. Ref<AnimationNode> anode = blend_tree->get_node(p_nodes[i]);
  469. if (anode->is_deletable()) {
  470. to_erase.push_back(p_nodes[i]);
  471. }
  472. }
  473. }
  474. if (to_erase.is_empty()) {
  475. return;
  476. }
  477. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  478. undo_redo->create_action(TTR("Delete Node(s)"));
  479. for (const StringName &F : to_erase) {
  480. _delete_node_request(F);
  481. }
  482. undo_redo->commit_action();
  483. }
  484. void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
  485. if (read_only) {
  486. return;
  487. }
  488. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  489. ERR_FAIL_NULL(gn);
  490. String name = gn->get_name();
  491. Ref<AnimationNode> anode = blend_tree->get_node(name);
  492. ERR_FAIL_COND(!anode.is_valid());
  493. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  494. }
  495. void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
  496. Ref<AnimationNode> an = blend_tree->get_node(p_which);
  497. ERR_FAIL_COND(!an.is_valid());
  498. AnimationTreeEditor::get_singleton()->enter_editor(p_which);
  499. }
  500. void AnimationNodeBlendTreeEditor::_filter_toggled() {
  501. updating = true;
  502. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  503. undo_redo->create_action(TTR("Toggle Filter On/Off"));
  504. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
  505. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
  506. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  507. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  508. undo_redo->commit_action();
  509. updating = false;
  510. }
  511. void AnimationNodeBlendTreeEditor::_filter_edited() {
  512. TreeItem *edited = filters->get_edited();
  513. ERR_FAIL_NULL(edited);
  514. NodePath edited_path = edited->get_metadata(0);
  515. bool filtered = edited->is_checked(0);
  516. updating = true;
  517. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  518. undo_redo->create_action(TTR("Change Filter"));
  519. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
  520. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
  521. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  522. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  523. undo_redo->commit_action();
  524. updating = false;
  525. }
  526. void AnimationNodeBlendTreeEditor::_filter_fill_selection() {
  527. TreeItem *ti = filters->get_root();
  528. if (!ti) {
  529. return;
  530. }
  531. updating = true;
  532. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  533. undo_redo->create_action(TTR("Fill Selected Filter Children"));
  534. _filter_fill_selection_recursive(undo_redo, ti, false);
  535. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  536. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  537. undo_redo->commit_action();
  538. updating = false;
  539. _update_filters(_filter_edit);
  540. }
  541. void AnimationNodeBlendTreeEditor::_filter_invert_selection() {
  542. TreeItem *ti = filters->get_root();
  543. if (!ti) {
  544. return;
  545. }
  546. updating = true;
  547. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  548. undo_redo->create_action(TTR("Invert Filter Selection"));
  549. _filter_invert_selection_recursive(undo_redo, ti);
  550. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  551. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  552. undo_redo->commit_action();
  553. updating = false;
  554. _update_filters(_filter_edit);
  555. }
  556. void AnimationNodeBlendTreeEditor::_filter_clear_selection() {
  557. TreeItem *ti = filters->get_root();
  558. if (!ti) {
  559. return;
  560. }
  561. updating = true;
  562. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  563. undo_redo->create_action(TTR("Clear Filter Selection"));
  564. _filter_clear_selection_recursive(undo_redo, ti);
  565. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  566. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  567. undo_redo->commit_action();
  568. updating = false;
  569. _update_filters(_filter_edit);
  570. }
  571. void AnimationNodeBlendTreeEditor::_filter_fill_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item, bool p_parent_filtered) {
  572. TreeItem *ti = p_item->get_first_child();
  573. bool parent_filtered = p_parent_filtered;
  574. while (ti) {
  575. NodePath item_path = ti->get_metadata(0);
  576. bool filtered = _filter_edit->is_path_filtered(item_path);
  577. parent_filtered |= filtered;
  578. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, parent_filtered);
  579. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  580. _filter_fill_selection_recursive(p_undo_redo, ti, parent_filtered);
  581. ti = ti->get_next();
  582. parent_filtered = p_parent_filtered;
  583. }
  584. }
  585. void AnimationNodeBlendTreeEditor::_filter_invert_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  586. TreeItem *ti = p_item->get_first_child();
  587. while (ti) {
  588. NodePath item_path = ti->get_metadata(0);
  589. bool filtered = _filter_edit->is_path_filtered(item_path);
  590. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, !filtered);
  591. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  592. _filter_invert_selection_recursive(p_undo_redo, ti);
  593. ti = ti->get_next();
  594. }
  595. }
  596. void AnimationNodeBlendTreeEditor::_filter_clear_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  597. TreeItem *ti = p_item->get_first_child();
  598. while (ti) {
  599. NodePath item_path = ti->get_metadata(0);
  600. bool filtered = _filter_edit->is_path_filtered(item_path);
  601. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, false);
  602. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  603. _filter_clear_selection_recursive(p_undo_redo, ti);
  604. ti = ti->get_next();
  605. }
  606. }
  607. bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
  608. if (updating || _filter_edit != anode) {
  609. return false;
  610. }
  611. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  612. if (!tree) {
  613. return false;
  614. }
  615. Node *base = tree->get_node(tree->get_root_node());
  616. if (!base) {
  617. EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
  618. return false;
  619. }
  620. updating = true;
  621. HashSet<String> paths;
  622. HashMap<String, RBSet<String>> types;
  623. {
  624. List<StringName> animation_list;
  625. tree->get_animation_list(&animation_list);
  626. for (const StringName &E : animation_list) {
  627. Ref<Animation> anim = tree->get_animation(E);
  628. for (int i = 0; i < anim->get_track_count(); i++) {
  629. String track_path = anim->track_get_path(i);
  630. paths.insert(track_path);
  631. String track_type_name;
  632. Animation::TrackType track_type = anim->track_get_type(i);
  633. switch (track_type) {
  634. case Animation::TrackType::TYPE_ANIMATION: {
  635. track_type_name = TTR("Anim Clips");
  636. } break;
  637. case Animation::TrackType::TYPE_AUDIO: {
  638. track_type_name = TTR("Audio Clips");
  639. } break;
  640. case Animation::TrackType::TYPE_METHOD: {
  641. track_type_name = TTR("Functions");
  642. } break;
  643. default: {
  644. } break;
  645. }
  646. if (!track_type_name.is_empty()) {
  647. types[track_path].insert(track_type_name);
  648. }
  649. }
  650. }
  651. }
  652. filter_enabled->set_pressed(anode->is_filter_enabled());
  653. filters->clear();
  654. TreeItem *root = filters->create_item();
  655. HashMap<String, TreeItem *> parenthood;
  656. for (const String &E : paths) {
  657. NodePath path = E;
  658. TreeItem *ti = nullptr;
  659. String accum;
  660. for (int i = 0; i < path.get_name_count(); i++) {
  661. String name = path.get_name(i);
  662. if (!accum.is_empty()) {
  663. accum += "/";
  664. }
  665. accum += name;
  666. if (!parenthood.has(accum)) {
  667. if (ti) {
  668. ti = filters->create_item(ti);
  669. } else {
  670. ti = filters->create_item(root);
  671. }
  672. parenthood[accum] = ti;
  673. ti->set_text(0, name);
  674. ti->set_selectable(0, false);
  675. ti->set_editable(0, false);
  676. if (base->has_node(accum)) {
  677. Node *node = base->get_node(accum);
  678. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  679. }
  680. } else {
  681. ti = parenthood[accum];
  682. }
  683. }
  684. Node *node = nullptr;
  685. if (base->has_node(accum)) {
  686. node = base->get_node(accum);
  687. }
  688. if (!node) {
  689. continue; //no node, can't edit
  690. }
  691. if (path.get_subname_count()) {
  692. String concat = path.get_concatenated_subnames();
  693. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  694. if (skeleton && skeleton->find_bone(concat) != -1) {
  695. //path in skeleton
  696. const String &bone = concat;
  697. int idx = skeleton->find_bone(bone);
  698. List<String> bone_path;
  699. while (idx != -1) {
  700. bone_path.push_front(skeleton->get_bone_name(idx));
  701. idx = skeleton->get_bone_parent(idx);
  702. }
  703. accum += ":";
  704. for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
  705. if (F != bone_path.front()) {
  706. accum += "/";
  707. }
  708. accum += F->get();
  709. if (!parenthood.has(accum)) {
  710. ti = filters->create_item(ti);
  711. parenthood[accum] = ti;
  712. ti->set_text(0, F->get());
  713. ti->set_selectable(0, false);
  714. ti->set_editable(0, false);
  715. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  716. } else {
  717. ti = parenthood[accum];
  718. }
  719. }
  720. ti->set_editable(0, !read_only);
  721. ti->set_selectable(0, true);
  722. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  723. ti->set_text(0, concat);
  724. ti->set_checked(0, anode->is_path_filtered(path));
  725. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  726. ti->set_metadata(0, path);
  727. } else {
  728. //just a property
  729. ti = filters->create_item(ti);
  730. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  731. ti->set_text(0, concat);
  732. ti->set_editable(0, !read_only);
  733. ti->set_selectable(0, true);
  734. ti->set_checked(0, anode->is_path_filtered(path));
  735. ti->set_metadata(0, path);
  736. }
  737. } else {
  738. if (ti) {
  739. //just a node, not a property track
  740. String types_text = "[";
  741. if (types.has(path)) {
  742. RBSet<String>::Iterator F = types[path].begin();
  743. types_text += *F;
  744. while (F) {
  745. types_text += " / " + *F;
  746. ;
  747. ++F;
  748. }
  749. }
  750. types_text += "]";
  751. ti = filters->create_item(ti);
  752. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  753. ti->set_text(0, types_text);
  754. ti->set_editable(0, !read_only);
  755. ti->set_selectable(0, true);
  756. ti->set_checked(0, anode->is_path_filtered(path));
  757. ti->set_metadata(0, path);
  758. }
  759. }
  760. }
  761. updating = false;
  762. return true;
  763. }
  764. void AnimationNodeBlendTreeEditor::_inspect_filters(const String &p_which) {
  765. if (read_only) {
  766. filter_dialog->set_title(TTR("Inspect Filtered Tracks:"));
  767. } else {
  768. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  769. }
  770. filter_enabled->set_disabled(read_only);
  771. Ref<AnimationNode> anode = blend_tree->get_node(p_which);
  772. ERR_FAIL_COND(!anode.is_valid());
  773. _filter_edit = anode;
  774. if (!_update_filters(anode)) {
  775. return;
  776. }
  777. filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
  778. }
  779. void AnimationNodeBlendTreeEditor::_update_editor_settings() {
  780. graph->get_panner()->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
  781. graph->set_warped_panning(bool(EDITOR_GET("editors/panning/warped_mouse_panning")));
  782. }
  783. void AnimationNodeBlendTreeEditor::_notification(int p_what) {
  784. switch (p_what) {
  785. case NOTIFICATION_ENTER_TREE: {
  786. _update_editor_settings();
  787. } break;
  788. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  789. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  790. _update_editor_settings();
  791. }
  792. } break;
  793. case NOTIFICATION_THEME_CHANGED: {
  794. error_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  795. error_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  796. if (is_visible_in_tree()) {
  797. update_graph();
  798. }
  799. } break;
  800. case NOTIFICATION_PROCESS: {
  801. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  802. if (!tree) {
  803. return; // Node has been changed.
  804. }
  805. String error;
  806. if (!tree->is_active()) {
  807. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  808. } else if (tree->is_state_invalid()) {
  809. error = tree->get_invalid_state_reason();
  810. }
  811. if (error != error_label->get_text()) {
  812. error_label->set_text(error);
  813. if (!error.is_empty()) {
  814. error_panel->show();
  815. } else {
  816. error_panel->hide();
  817. }
  818. }
  819. List<AnimationNodeBlendTree::NodeConnection> conns;
  820. blend_tree->get_node_connections(&conns);
  821. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  822. float activity = 0;
  823. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  824. if (!tree->is_state_invalid()) {
  825. activity = tree->get_connection_activity(path, E.input_index);
  826. }
  827. graph->set_connection_activity(E.output_node, 0, E.input_node, E.input_index, activity);
  828. }
  829. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  830. Ref<AnimationNodeAnimation> an = blend_tree->get_node(E.key);
  831. if (an.is_valid()) {
  832. if (tree->has_animation(an->get_animation())) {
  833. Ref<Animation> anim = tree->get_animation(an->get_animation());
  834. if (anim.is_valid()) {
  835. //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  836. StringName length_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_length";
  837. StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_position";
  838. E.value->set_max(tree->get(length_path));
  839. E.value->set_value(tree->get(time_path));
  840. }
  841. }
  842. }
  843. }
  844. for (int i = 0; i < visible_properties.size(); i++) {
  845. visible_properties[i]->update_property();
  846. }
  847. } break;
  848. case NOTIFICATION_VISIBILITY_CHANGED: {
  849. set_process(is_visible_in_tree());
  850. } break;
  851. }
  852. }
  853. void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
  854. if (read_only) {
  855. return;
  856. }
  857. if (updating) {
  858. return;
  859. }
  860. updating = true;
  861. blend_tree->set_graph_offset(p_scroll / EDSCALE);
  862. updating = false;
  863. }
  864. void AnimationNodeBlendTreeEditor::_bind_methods() {
  865. ClassDB::bind_method("update_graph", &AnimationNodeBlendTreeEditor::update_graph);
  866. ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
  867. }
  868. AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = nullptr;
  869. // AnimationNode's "node_changed" signal means almost update_input.
  870. void AnimationNodeBlendTreeEditor::_node_changed(const StringName &p_node_name) {
  871. // TODO:
  872. // Here is executed during the commit of EditorNode::undo_redo, it is not possible to create an undo_redo action here.
  873. // The disconnect when the number of enabled inputs decreases is done in AnimationNodeBlendTree and update_graph().
  874. // This means that there is no place to register undo_redo actions.
  875. // In order to implement undo_redo correctly, we may need to implement AnimationNodeEdit such as AnimationTrackKeyEdit
  876. // and add it to _node_selected() with EditorNode::get_singleton()->push_item(AnimationNodeEdit).
  877. update_graph();
  878. }
  879. void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
  880. if (blend_tree.is_null()) {
  881. return;
  882. }
  883. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  884. if (!tree) {
  885. return;
  886. }
  887. String prev_name = blend_tree->get_node_name(p_node);
  888. ERR_FAIL_COND(prev_name.is_empty());
  889. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
  890. ERR_FAIL_NULL(gn);
  891. const String &new_name = p_text;
  892. ERR_FAIL_COND(new_name.is_empty() || new_name.contains(".") || new_name.contains("/"));
  893. if (new_name == prev_name) {
  894. return; //nothing to do
  895. }
  896. const String &base_name = new_name;
  897. int base = 1;
  898. String name = base_name;
  899. while (blend_tree->has_node(name)) {
  900. base++;
  901. name = base_name + " " + itos(base);
  902. }
  903. String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
  904. updating = true;
  905. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  906. undo_redo->create_action(TTR("Node Renamed"));
  907. undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
  908. undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
  909. undo_redo->add_do_method(this, "update_graph");
  910. undo_redo->add_undo_method(this, "update_graph");
  911. undo_redo->commit_action();
  912. updating = false;
  913. gn->set_name(new_name);
  914. gn->set_size(gn->get_minimum_size());
  915. //change editors accordingly
  916. for (int i = 0; i < visible_properties.size(); i++) {
  917. String pname = visible_properties[i]->get_edited_property().operator String();
  918. if (pname.begins_with(base_path + prev_name)) {
  919. String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
  920. visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
  921. }
  922. }
  923. //recreate connections
  924. graph->clear_connections();
  925. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  926. blend_tree->get_node_connections(&node_connections);
  927. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  928. StringName from = E.output_node;
  929. StringName to = E.input_node;
  930. int to_idx = E.input_index;
  931. graph->connect_node(from, 0, to, to_idx);
  932. }
  933. //update animations
  934. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  935. if (E.key == prev_name) {
  936. animations[new_name] = animations[prev_name];
  937. animations.erase(prev_name);
  938. break;
  939. }
  940. }
  941. update_graph(); // Needed to update the signal connections with the new name.
  942. current_node_rename_text = String();
  943. }
  944. void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node) {
  945. if (current_node_rename_text.is_empty()) {
  946. return; // The text_submitted signal triggered the graph update and freed the LineEdit.
  947. }
  948. _node_renamed(current_node_rename_text, p_node);
  949. }
  950. void AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed(const String &p_text) {
  951. current_node_rename_text = p_text;
  952. }
  953. bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
  954. Ref<AnimationNodeBlendTree> bt = p_node;
  955. return bt.is_valid();
  956. }
  957. void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
  958. if (blend_tree.is_valid()) {
  959. blend_tree->disconnect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  960. }
  961. blend_tree = p_node;
  962. read_only = false;
  963. if (blend_tree.is_null()) {
  964. hide();
  965. } else {
  966. read_only = EditorNode::get_singleton()->is_resource_read_only(blend_tree);
  967. blend_tree->connect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  968. update_graph();
  969. }
  970. add_node->set_disabled(read_only);
  971. graph->set_show_arrange_button(!read_only);
  972. }
  973. AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
  974. singleton = this;
  975. updating = false;
  976. use_position_from_popup_menu = false;
  977. graph = memnew(GraphEdit);
  978. add_child(graph);
  979. graph->add_valid_right_disconnect_type(0);
  980. graph->add_valid_left_disconnect_type(0);
  981. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  982. graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), CONNECT_DEFERRED);
  983. graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), CONNECT_DEFERRED);
  984. graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));
  985. graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));
  986. graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));
  987. graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));
  988. graph->connect("connection_to_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_to_empty));
  989. graph->connect("connection_from_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_from_empty));
  990. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  991. graph->set_minimap_opacity(graph_minimap_opacity);
  992. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  993. graph->set_connection_lines_curvature(graph_lines_curvature);
  994. VSeparator *vs = memnew(VSeparator);
  995. graph->get_menu_hbox()->add_child(vs);
  996. graph->get_menu_hbox()->move_child(vs, 0);
  997. add_node = memnew(MenuButton);
  998. graph->get_menu_hbox()->add_child(add_node);
  999. add_node->set_text(TTR("Add Node..."));
  1000. graph->get_menu_hbox()->move_child(add_node, 0);
  1001. add_node->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_add_node));
  1002. add_node->get_popup()->connect("popup_hide", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_hide), CONNECT_DEFERRED);
  1003. add_node->connect("about_to_popup", callable_mp(this, &AnimationNodeBlendTreeEditor::_update_options_menu).bind(false));
  1004. add_node->set_disabled(read_only);
  1005. add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
  1006. add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot", 2));
  1007. add_options.push_back(AddOption("Add2", "AnimationNodeAdd2", 2));
  1008. add_options.push_back(AddOption("Add3", "AnimationNodeAdd3", 3));
  1009. add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2", 2));
  1010. add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3", 3));
  1011. add_options.push_back(AddOption("Sub2", "AnimationNodeSub2", 2));
  1012. add_options.push_back(AddOption("TimeSeek", "AnimationNodeTimeSeek", 1));
  1013. add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale", 1));
  1014. add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
  1015. add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
  1016. add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
  1017. add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
  1018. add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
  1019. _update_options_menu();
  1020. error_panel = memnew(PanelContainer);
  1021. add_child(error_panel);
  1022. error_label = memnew(Label);
  1023. error_panel->add_child(error_label);
  1024. error_label->set_text("eh");
  1025. filter_dialog = memnew(AcceptDialog);
  1026. add_child(filter_dialog);
  1027. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  1028. VBoxContainer *filter_vbox = memnew(VBoxContainer);
  1029. filter_dialog->add_child(filter_vbox);
  1030. HBoxContainer *filter_hbox = memnew(HBoxContainer);
  1031. filter_vbox->add_child(filter_hbox);
  1032. filter_enabled = memnew(CheckBox);
  1033. filter_enabled->set_text(TTR("Enable Filtering"));
  1034. filter_enabled->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_toggled));
  1035. filter_hbox->add_child(filter_enabled);
  1036. filter_fill_selection = memnew(Button);
  1037. filter_fill_selection->set_text(TTR("Fill Selected Children"));
  1038. filter_fill_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_fill_selection));
  1039. filter_hbox->add_child(filter_fill_selection);
  1040. filter_invert_selection = memnew(Button);
  1041. filter_invert_selection->set_text(TTR("Invert"));
  1042. filter_invert_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_invert_selection));
  1043. filter_hbox->add_child(filter_invert_selection);
  1044. filter_clear_selection = memnew(Button);
  1045. filter_clear_selection->set_text(TTR("Clear"));
  1046. filter_clear_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_clear_selection));
  1047. filter_hbox->add_child(filter_clear_selection);
  1048. filters = memnew(Tree);
  1049. filter_vbox->add_child(filters);
  1050. filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1051. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  1052. filters->set_hide_root(true);
  1053. filters->connect("item_edited", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_edited));
  1054. open_file = memnew(EditorFileDialog);
  1055. add_child(open_file);
  1056. open_file->set_title(TTR("Open Animation Node"));
  1057. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1058. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_file_opened));
  1059. }