animation_blend_tree_editor_plugin.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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_properties.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/editor_string_names.h"
  40. #include "editor/editor_undo_redo_manager.h"
  41. #include "editor/gui/editor_file_dialog.h"
  42. #include "editor/themes/editor_scale.h"
  43. #include "scene/3d/skeleton_3d.h"
  44. #include "scene/animation/animation_player.h"
  45. #include "scene/gui/check_box.h"
  46. #include "scene/gui/grid_container.h"
  47. #include "scene/gui/menu_button.h"
  48. #include "scene/gui/option_button.h"
  49. #include "scene/gui/panel.h"
  50. #include "scene/gui/progress_bar.h"
  51. #include "scene/gui/separator.h"
  52. #include "scene/gui/view_panner.h"
  53. #include "scene/main/window.h"
  54. #include "scene/resources/style_box_flat.h"
  55. void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) {
  56. for (int i = 0; i < add_options.size(); i++) {
  57. ERR_FAIL_COND(add_options[i].script == p_script);
  58. }
  59. AddOption ao;
  60. ao.name = p_name;
  61. ao.script = p_script;
  62. add_options.push_back(ao);
  63. _update_options_menu();
  64. }
  65. void AnimationNodeBlendTreeEditor::remove_custom_type(const Ref<Script> &p_script) {
  66. for (int i = 0; i < add_options.size(); i++) {
  67. if (add_options[i].script == p_script) {
  68. add_options.remove_at(i);
  69. return;
  70. }
  71. }
  72. _update_options_menu();
  73. }
  74. void AnimationNodeBlendTreeEditor::_update_options_menu(bool p_has_input_ports) {
  75. add_node->get_popup()->clear();
  76. add_node->get_popup()->reset_size();
  77. for (int i = 0; i < add_options.size(); i++) {
  78. if (p_has_input_ports && add_options[i].input_port_count == 0) {
  79. continue;
  80. }
  81. add_node->get_popup()->add_item(add_options[i].name, i);
  82. }
  83. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  84. if (clipb.is_valid()) {
  85. add_node->get_popup()->add_separator();
  86. add_node->get_popup()->add_item(TTR("Paste"), MENU_PASTE);
  87. }
  88. add_node->get_popup()->add_separator();
  89. add_node->get_popup()->add_item(TTR("Load..."), MENU_LOAD_FILE);
  90. use_position_from_popup_menu = false;
  91. }
  92. Size2 AnimationNodeBlendTreeEditor::get_minimum_size() const {
  93. return Size2(10, 200);
  94. }
  95. void AnimationNodeBlendTreeEditor::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
  96. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  97. if (!tree) {
  98. return;
  99. }
  100. updating = true;
  101. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  102. undo_redo->create_action(vformat(TTR("Parameter Changed: %s"), p_property), UndoRedo::MERGE_ENDS);
  103. undo_redo->add_do_property(tree, p_property, p_value);
  104. undo_redo->add_undo_property(tree, p_property, tree->get(p_property));
  105. undo_redo->add_do_method(this, "update_graph");
  106. undo_redo->add_undo_method(this, "update_graph");
  107. undo_redo->commit_action();
  108. updating = false;
  109. }
  110. void AnimationNodeBlendTreeEditor::update_graph() {
  111. if (updating || blend_tree.is_null()) {
  112. return;
  113. }
  114. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  115. if (!tree) {
  116. return;
  117. }
  118. visible_properties.clear();
  119. graph->set_scroll_offset(blend_tree->get_graph_offset() * EDSCALE);
  120. graph->clear_connections();
  121. //erase all nodes
  122. for (int i = 0; i < graph->get_child_count(); i++) {
  123. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  124. memdelete(graph->get_child(i));
  125. i--;
  126. }
  127. }
  128. animations.clear();
  129. List<StringName> nodes;
  130. blend_tree->get_node_list(&nodes);
  131. for (const StringName &E : nodes) {
  132. GraphNode *node = memnew(GraphNode);
  133. graph->add_child(node);
  134. node->set_draggable(!read_only);
  135. Ref<AnimationNode> agnode = blend_tree->get_node(E);
  136. ERR_CONTINUE(!agnode.is_valid());
  137. node->set_position_offset(blend_tree->get_node_position(E) * EDSCALE);
  138. node->set_title(agnode->get_caption());
  139. node->set_name(E);
  140. int base = 0;
  141. if (E != SceneStringName(output)) {
  142. LineEdit *name = memnew(LineEdit);
  143. name->set_text(E);
  144. name->set_editable(!read_only);
  145. name->set_expand_to_text_length_enabled(true);
  146. name->set_custom_minimum_size(Vector2(100, 0) * EDSCALE);
  147. node->add_child(name);
  148. node->set_slot(0, false, 0, Color(), true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")));
  149. name->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed).bind(agnode), CONNECT_DEFERRED);
  150. name->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out).bind(agnode), CONNECT_DEFERRED);
  151. name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed), CONNECT_DEFERRED);
  152. base = 1;
  153. agnode->set_deletable(true);
  154. if (!read_only) {
  155. Button *delete_button = memnew(Button);
  156. delete_button->set_flat(true);
  157. delete_button->set_focus_mode(FOCUS_NONE);
  158. delete_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  159. delete_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_node_request).bind(E), CONNECT_DEFERRED);
  160. node->get_titlebar_hbox()->add_child(delete_button);
  161. }
  162. }
  163. for (int i = 0; i < agnode->get_input_count(); i++) {
  164. Label *in_name = memnew(Label);
  165. node->add_child(in_name);
  166. in_name->set_text(agnode->get_input_name(i));
  167. node->set_slot(base + i, true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")), false, 0, Color());
  168. }
  169. List<PropertyInfo> pinfo;
  170. agnode->get_parameter_list(&pinfo);
  171. for (const PropertyInfo &F : pinfo) {
  172. if (!(F.usage & PROPERTY_USAGE_EDITOR)) {
  173. continue;
  174. }
  175. String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E) + "/" + F.name;
  176. EditorProperty *prop = EditorInspector::instantiate_property_editor(tree, F.type, base_path, F.hint, F.hint_string, F.usage);
  177. if (prop) {
  178. prop->set_read_only(read_only || (F.usage & PROPERTY_USAGE_READ_ONLY));
  179. prop->set_object_and_property(tree, base_path);
  180. prop->update_property();
  181. prop->set_name_split_ratio(0);
  182. prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
  183. if (F.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  184. // Give the resource editor some more space to make the inside readable.
  185. prop->set_custom_minimum_size(Vector2(180, 0) * EDSCALE);
  186. // Align the size of the node with the resource editor, its un-expanding does not trigger a resize.
  187. prop->connect(SceneStringName(resized), Callable(node, "reset_size"));
  188. }
  189. node->add_child(prop);
  190. visible_properties.push_back(prop);
  191. }
  192. }
  193. node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));
  194. if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
  195. node->add_child(memnew(HSeparator));
  196. Button *open_in_editor = memnew(Button);
  197. open_in_editor->set_text(TTR("Open Editor"));
  198. open_in_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  199. node->add_child(open_in_editor);
  200. open_in_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_open_in_editor).bind(E), CONNECT_DEFERRED);
  201. open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);
  202. }
  203. if (agnode->has_filter()) {
  204. node->add_child(memnew(HSeparator));
  205. Button *inspect_filters = memnew(Button);
  206. if (read_only) {
  207. inspect_filters->set_text(TTR("Inspect Filters"));
  208. } else {
  209. inspect_filters->set_text(TTR("Edit Filters"));
  210. }
  211. inspect_filters->set_button_icon(get_editor_theme_icon(SNAME("AnimationFilter")));
  212. node->add_child(inspect_filters);
  213. inspect_filters->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_inspect_filters).bind(E), CONNECT_DEFERRED);
  214. inspect_filters->set_h_size_flags(SIZE_SHRINK_CENTER);
  215. }
  216. Ref<AnimationNodeAnimation> anim = agnode;
  217. if (anim.is_valid()) {
  218. MenuButton *mb = memnew(MenuButton);
  219. mb->set_text(anim->get_animation());
  220. mb->set_button_icon(get_editor_theme_icon(SNAME("Animation")));
  221. mb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  222. mb->set_disabled(read_only);
  223. Array options;
  224. node->add_child(memnew(HSeparator));
  225. node->add_child(mb);
  226. ProgressBar *pb = memnew(ProgressBar);
  227. List<StringName> anims;
  228. tree->get_animation_list(&anims);
  229. for (const StringName &F : anims) {
  230. mb->get_popup()->add_item(F);
  231. options.push_back(F);
  232. }
  233. pb->set_show_percentage(false);
  234. pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);
  235. animations[E] = pb;
  236. node->add_child(pb);
  237. mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);
  238. }
  239. Ref<StyleBox> sb_panel = node->get_theme_stylebox(SceneStringName(panel), "GraphNode")->duplicate();
  240. if (sb_panel.is_valid()) {
  241. sb_panel->set_content_margin(SIDE_TOP, 12 * EDSCALE);
  242. sb_panel->set_content_margin(SIDE_BOTTOM, 12 * EDSCALE);
  243. node->add_theme_style_override(SceneStringName(panel), sb_panel);
  244. }
  245. node->add_theme_constant_override("separation", 4 * EDSCALE);
  246. }
  247. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  248. blend_tree->get_node_connections(&node_connections);
  249. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  250. StringName from = E.output_node;
  251. StringName to = E.input_node;
  252. int to_idx = E.input_index;
  253. graph->connect_node(from, 0, to, to_idx);
  254. }
  255. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  256. graph->set_minimap_opacity(graph_minimap_opacity);
  257. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  258. graph->set_connection_lines_curvature(graph_lines_curvature);
  259. }
  260. void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
  261. file_loaded = ResourceLoader::load(p_file);
  262. if (file_loaded.is_valid()) {
  263. _add_node(MENU_LOAD_FILE_CONFIRM);
  264. } else {
  265. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
  266. }
  267. }
  268. void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
  269. Ref<AnimationNode> anode;
  270. String base_name;
  271. if (p_idx == MENU_LOAD_FILE) {
  272. open_file->clear_filters();
  273. List<String> ext_filters;
  274. ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &ext_filters);
  275. for (const String &E : ext_filters) {
  276. open_file->add_filter("*." + E);
  277. }
  278. open_file->popup_file_dialog();
  279. return;
  280. } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
  281. anode = file_loaded;
  282. file_loaded.unref();
  283. base_name = anode->get_class();
  284. } else if (p_idx == MENU_PASTE) {
  285. anode = EditorSettings::get_singleton()->get_resource_clipboard();
  286. ERR_FAIL_COND(!anode.is_valid());
  287. base_name = anode->get_class();
  288. } else if (!add_options[p_idx].type.is_empty()) {
  289. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(add_options[p_idx].type));
  290. ERR_FAIL_NULL(an);
  291. anode = Ref<AnimationNode>(an);
  292. base_name = add_options[p_idx].name;
  293. } else {
  294. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  295. StringName base_type = add_options[p_idx].script->get_instance_base_type();
  296. AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(base_type));
  297. ERR_FAIL_NULL(an);
  298. anode = Ref<AnimationNode>(an);
  299. anode->set_script(add_options[p_idx].script);
  300. base_name = add_options[p_idx].name;
  301. }
  302. Ref<AnimationNodeOutput> out = anode;
  303. if (out.is_valid()) {
  304. EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
  305. return;
  306. }
  307. if (!from_node.is_empty() && anode->get_input_count() == 0) {
  308. from_node = "";
  309. return;
  310. }
  311. Point2 instance_pos = graph->get_scroll_offset();
  312. if (use_position_from_popup_menu) {
  313. instance_pos += position_from_popup_menu;
  314. } else {
  315. instance_pos += graph->get_size() * 0.5;
  316. }
  317. instance_pos /= graph->get_zoom();
  318. int base = 1;
  319. String name = base_name;
  320. while (blend_tree->has_node(name)) {
  321. base++;
  322. name = base_name + " " + itos(base);
  323. }
  324. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  325. undo_redo->create_action(TTR("Add Node to BlendTree"));
  326. undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
  327. undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
  328. if (!from_node.is_empty()) {
  329. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", name, 0, from_node);
  330. from_node = "";
  331. }
  332. if (!to_node.is_empty() && to_slot != -1) {
  333. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", to_node, to_slot, name);
  334. to_node = "";
  335. to_slot = -1;
  336. }
  337. undo_redo->add_do_method(this, "update_graph");
  338. undo_redo->add_undo_method(this, "update_graph");
  339. undo_redo->commit_action();
  340. }
  341. void AnimationNodeBlendTreeEditor::_popup(bool p_has_input_ports, const Vector2 &p_node_position) {
  342. _update_options_menu(p_has_input_ports);
  343. use_position_from_popup_menu = true;
  344. position_from_popup_menu = p_node_position;
  345. add_node->get_popup()->set_position(graph->get_screen_position() + graph->get_local_mouse_position());
  346. add_node->get_popup()->reset_size();
  347. add_node->get_popup()->popup();
  348. }
  349. void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
  350. if (read_only) {
  351. return;
  352. }
  353. _popup(false, p_position);
  354. }
  355. void AnimationNodeBlendTreeEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  356. if (read_only) {
  357. return;
  358. }
  359. Ref<AnimationNode> node = blend_tree->get_node(p_from);
  360. if (node.is_valid()) {
  361. from_node = p_from;
  362. _popup(true, p_release_position);
  363. }
  364. }
  365. void AnimationNodeBlendTreeEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  366. if (read_only) {
  367. return;
  368. }
  369. Ref<AnimationNode> node = blend_tree->get_node(p_to);
  370. if (node.is_valid()) {
  371. to_node = p_to;
  372. to_slot = p_to_slot;
  373. _popup(false, p_release_position);
  374. }
  375. }
  376. void AnimationNodeBlendTreeEditor::_popup_hide() {
  377. to_node = "";
  378. to_slot = -1;
  379. }
  380. void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
  381. updating = true;
  382. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  383. undo_redo->create_action(TTR("Node Moved"));
  384. undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
  385. undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
  386. undo_redo->add_do_method(this, "update_graph");
  387. undo_redo->add_undo_method(this, "update_graph");
  388. undo_redo->commit_action();
  389. updating = false;
  390. }
  391. void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  392. if (read_only) {
  393. return;
  394. }
  395. AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  396. if (err == AnimationNodeBlendTree::CONNECTION_ERROR_CONNECTION_EXISTS) {
  397. blend_tree->disconnect_node(p_to, p_to_index);
  398. err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
  399. }
  400. if (err != AnimationNodeBlendTree::CONNECTION_OK) {
  401. EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
  402. return;
  403. }
  404. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  405. undo_redo->create_action(TTR("Nodes Connected"));
  406. undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  407. undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  408. undo_redo->add_do_method(this, "update_graph");
  409. undo_redo->add_undo_method(this, "update_graph");
  410. undo_redo->commit_action();
  411. }
  412. void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  413. if (read_only) {
  414. return;
  415. }
  416. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  417. updating = true;
  418. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  419. undo_redo->create_action(TTR("Nodes Disconnected"));
  420. undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
  421. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
  422. undo_redo->add_do_method(this, "update_graph");
  423. undo_redo->add_undo_method(this, "update_graph");
  424. undo_redo->commit_action();
  425. updating = false;
  426. }
  427. void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, const Array &p_options, const String &p_node) {
  428. String option = p_options[p_index];
  429. Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
  430. ERR_FAIL_COND(!anim.is_valid());
  431. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  432. undo_redo->create_action(TTR("Set Animation"));
  433. undo_redo->add_do_method(anim.ptr(), "set_animation", option);
  434. undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
  435. undo_redo->add_do_method(this, "update_graph");
  436. undo_redo->add_undo_method(this, "update_graph");
  437. undo_redo->commit_action();
  438. }
  439. void AnimationNodeBlendTreeEditor::_delete_node_request(const String &p_which) {
  440. if (read_only) {
  441. return;
  442. }
  443. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  444. undo_redo->create_action(TTR("Delete Node"));
  445. undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
  446. 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));
  447. List<AnimationNodeBlendTree::NodeConnection> conns;
  448. blend_tree->get_node_connections(&conns);
  449. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  450. if (E.output_node == p_which || E.input_node == p_which) {
  451. undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E.input_node, E.input_index, E.output_node);
  452. }
  453. }
  454. undo_redo->add_do_method(this, "update_graph");
  455. undo_redo->add_undo_method(this, "update_graph");
  456. undo_redo->commit_action();
  457. }
  458. void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
  459. if (read_only) {
  460. return;
  461. }
  462. List<StringName> to_erase;
  463. if (p_nodes.is_empty()) {
  464. for (int i = 0; i < graph->get_child_count(); i++) {
  465. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  466. if (gn && gn->is_selected()) {
  467. Ref<AnimationNode> anode = blend_tree->get_node(gn->get_name());
  468. if (anode->is_deletable()) {
  469. to_erase.push_back(gn->get_name());
  470. }
  471. }
  472. }
  473. } else {
  474. for (int i = 0; i < p_nodes.size(); i++) {
  475. Ref<AnimationNode> anode = blend_tree->get_node(p_nodes[i]);
  476. if (anode->is_deletable()) {
  477. to_erase.push_back(p_nodes[i]);
  478. }
  479. }
  480. }
  481. if (to_erase.is_empty()) {
  482. return;
  483. }
  484. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  485. undo_redo->create_action(TTR("Delete Node(s)"));
  486. for (const StringName &F : to_erase) {
  487. _delete_node_request(F);
  488. }
  489. undo_redo->commit_action();
  490. }
  491. void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
  492. if (read_only) {
  493. return;
  494. }
  495. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  496. ERR_FAIL_NULL(gn);
  497. String name = gn->get_name();
  498. Ref<AnimationNode> anode = blend_tree->get_node(name);
  499. ERR_FAIL_COND(!anode.is_valid());
  500. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  501. }
  502. void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
  503. Ref<AnimationNode> an = blend_tree->get_node(p_which);
  504. ERR_FAIL_COND(!an.is_valid());
  505. AnimationTreeEditor::get_singleton()->enter_editor(p_which);
  506. }
  507. void AnimationNodeBlendTreeEditor::_filter_toggled() {
  508. updating = true;
  509. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  510. undo_redo->create_action(TTR("Toggle Filter On/Off"));
  511. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
  512. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
  513. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  514. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  515. undo_redo->commit_action();
  516. updating = false;
  517. }
  518. void AnimationNodeBlendTreeEditor::_filter_edited() {
  519. TreeItem *edited = filters->get_edited();
  520. ERR_FAIL_NULL(edited);
  521. NodePath edited_path = edited->get_metadata(0);
  522. bool filtered = edited->is_checked(0);
  523. updating = true;
  524. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  525. undo_redo->create_action(TTR("Change Filter"));
  526. undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
  527. undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
  528. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  529. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  530. undo_redo->commit_action();
  531. updating = false;
  532. }
  533. void AnimationNodeBlendTreeEditor::_filter_fill_selection() {
  534. TreeItem *ti = filters->get_root();
  535. if (!ti) {
  536. return;
  537. }
  538. updating = true;
  539. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  540. undo_redo->create_action(TTR("Fill Selected Filter Children"));
  541. _filter_fill_selection_recursive(undo_redo, ti, false);
  542. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  543. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  544. undo_redo->commit_action();
  545. updating = false;
  546. _update_filters(_filter_edit);
  547. }
  548. void AnimationNodeBlendTreeEditor::_filter_invert_selection() {
  549. TreeItem *ti = filters->get_root();
  550. if (!ti) {
  551. return;
  552. }
  553. updating = true;
  554. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  555. undo_redo->create_action(TTR("Invert Filter Selection"));
  556. _filter_invert_selection_recursive(undo_redo, ti);
  557. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  558. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  559. undo_redo->commit_action();
  560. updating = false;
  561. _update_filters(_filter_edit);
  562. }
  563. void AnimationNodeBlendTreeEditor::_filter_clear_selection() {
  564. TreeItem *ti = filters->get_root();
  565. if (!ti) {
  566. return;
  567. }
  568. updating = true;
  569. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  570. undo_redo->create_action(TTR("Clear Filter Selection"));
  571. _filter_clear_selection_recursive(undo_redo, ti);
  572. undo_redo->add_do_method(this, "_update_filters", _filter_edit);
  573. undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
  574. undo_redo->commit_action();
  575. updating = false;
  576. _update_filters(_filter_edit);
  577. }
  578. void AnimationNodeBlendTreeEditor::_filter_fill_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item, bool p_parent_filtered) {
  579. TreeItem *ti = p_item->get_first_child();
  580. bool parent_filtered = p_parent_filtered;
  581. while (ti) {
  582. NodePath item_path = ti->get_metadata(0);
  583. bool filtered = _filter_edit->is_path_filtered(item_path);
  584. parent_filtered |= filtered;
  585. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, parent_filtered);
  586. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  587. _filter_fill_selection_recursive(p_undo_redo, ti, parent_filtered);
  588. ti = ti->get_next();
  589. parent_filtered = p_parent_filtered;
  590. }
  591. }
  592. void AnimationNodeBlendTreeEditor::_filter_invert_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  593. TreeItem *ti = p_item->get_first_child();
  594. while (ti) {
  595. NodePath item_path = ti->get_metadata(0);
  596. bool filtered = _filter_edit->is_path_filtered(item_path);
  597. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, !filtered);
  598. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  599. _filter_invert_selection_recursive(p_undo_redo, ti);
  600. ti = ti->get_next();
  601. }
  602. }
  603. void AnimationNodeBlendTreeEditor::_filter_clear_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
  604. TreeItem *ti = p_item->get_first_child();
  605. while (ti) {
  606. NodePath item_path = ti->get_metadata(0);
  607. bool filtered = _filter_edit->is_path_filtered(item_path);
  608. p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, false);
  609. p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
  610. _filter_clear_selection_recursive(p_undo_redo, ti);
  611. ti = ti->get_next();
  612. }
  613. }
  614. bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
  615. if (updating || _filter_edit != anode) {
  616. return false;
  617. }
  618. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  619. if (!tree) {
  620. return false;
  621. }
  622. Node *base = tree->get_node(tree->get_root_node());
  623. if (!base) {
  624. EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
  625. return false;
  626. }
  627. updating = true;
  628. HashSet<String> paths;
  629. HashMap<String, RBSet<String>> types;
  630. {
  631. List<StringName> animation_list;
  632. tree->get_animation_list(&animation_list);
  633. for (const StringName &E : animation_list) {
  634. Ref<Animation> anim = tree->get_animation(E);
  635. for (int i = 0; i < anim->get_track_count(); i++) {
  636. String track_path = anim->track_get_path(i);
  637. paths.insert(track_path);
  638. String track_type_name;
  639. Animation::TrackType track_type = anim->track_get_type(i);
  640. switch (track_type) {
  641. case Animation::TrackType::TYPE_ANIMATION: {
  642. track_type_name = TTR("Anim Clips");
  643. } break;
  644. case Animation::TrackType::TYPE_AUDIO: {
  645. track_type_name = TTR("Audio Clips");
  646. } break;
  647. case Animation::TrackType::TYPE_METHOD: {
  648. track_type_name = TTR("Functions");
  649. } break;
  650. default: {
  651. } break;
  652. }
  653. if (!track_type_name.is_empty()) {
  654. types[track_path].insert(track_type_name);
  655. }
  656. }
  657. }
  658. }
  659. filter_enabled->set_pressed(anode->is_filter_enabled());
  660. filters->clear();
  661. TreeItem *root = filters->create_item();
  662. HashMap<String, TreeItem *> parenthood;
  663. for (const String &E : paths) {
  664. NodePath path = E;
  665. TreeItem *ti = nullptr;
  666. String accum;
  667. for (int i = 0; i < path.get_name_count(); i++) {
  668. String name = path.get_name(i);
  669. if (!accum.is_empty()) {
  670. accum += "/";
  671. }
  672. accum += name;
  673. if (!parenthood.has(accum)) {
  674. if (ti) {
  675. ti = filters->create_item(ti);
  676. } else {
  677. ti = filters->create_item(root);
  678. }
  679. parenthood[accum] = ti;
  680. ti->set_text(0, name);
  681. ti->set_selectable(0, false);
  682. ti->set_editable(0, false);
  683. if (base->has_node(accum)) {
  684. Node *node = base->get_node(accum);
  685. ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
  686. }
  687. } else {
  688. ti = parenthood[accum];
  689. }
  690. }
  691. Node *node = nullptr;
  692. if (base->has_node(accum)) {
  693. node = base->get_node(accum);
  694. }
  695. if (!node) {
  696. continue; //no node, can't edit
  697. }
  698. if (path.get_subname_count()) {
  699. String concat = path.get_concatenated_subnames();
  700. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  701. if (skeleton && skeleton->find_bone(concat) != -1) {
  702. //path in skeleton
  703. const String &bone = concat;
  704. int idx = skeleton->find_bone(bone);
  705. List<String> bone_path;
  706. while (idx != -1) {
  707. bone_path.push_front(skeleton->get_bone_name(idx));
  708. idx = skeleton->get_bone_parent(idx);
  709. }
  710. accum += ":";
  711. for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
  712. if (F != bone_path.front()) {
  713. accum += "/";
  714. }
  715. accum += F->get();
  716. if (!parenthood.has(accum)) {
  717. ti = filters->create_item(ti);
  718. parenthood[accum] = ti;
  719. ti->set_text(0, F->get());
  720. ti->set_selectable(0, false);
  721. ti->set_editable(0, false);
  722. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  723. } else {
  724. ti = parenthood[accum];
  725. }
  726. }
  727. ti->set_editable(0, !read_only);
  728. ti->set_selectable(0, true);
  729. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  730. ti->set_text(0, concat);
  731. ti->set_checked(0, anode->is_path_filtered(path));
  732. ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
  733. ti->set_metadata(0, path);
  734. } else {
  735. //just a property
  736. ti = filters->create_item(ti);
  737. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  738. ti->set_text(0, concat);
  739. ti->set_editable(0, !read_only);
  740. ti->set_selectable(0, true);
  741. ti->set_checked(0, anode->is_path_filtered(path));
  742. ti->set_metadata(0, path);
  743. }
  744. } else {
  745. if (ti) {
  746. //just a node, not a property track
  747. String types_text = "[";
  748. if (types.has(path)) {
  749. RBSet<String>::Iterator F = types[path].begin();
  750. types_text += *F;
  751. while (F) {
  752. types_text += " / " + *F;
  753. ;
  754. ++F;
  755. }
  756. }
  757. types_text += "]";
  758. ti = filters->create_item(ti);
  759. ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  760. ti->set_text(0, types_text);
  761. ti->set_editable(0, !read_only);
  762. ti->set_selectable(0, true);
  763. ti->set_checked(0, anode->is_path_filtered(path));
  764. ti->set_metadata(0, path);
  765. }
  766. }
  767. }
  768. updating = false;
  769. return true;
  770. }
  771. void AnimationNodeBlendTreeEditor::_inspect_filters(const String &p_which) {
  772. if (read_only) {
  773. filter_dialog->set_title(TTR("Inspect Filtered Tracks:"));
  774. } else {
  775. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  776. }
  777. filter_enabled->set_disabled(read_only);
  778. Ref<AnimationNode> anode = blend_tree->get_node(p_which);
  779. ERR_FAIL_COND(!anode.is_valid());
  780. _filter_edit = anode;
  781. if (!_update_filters(anode)) {
  782. return;
  783. }
  784. filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
  785. }
  786. void AnimationNodeBlendTreeEditor::_update_editor_settings() {
  787. 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")));
  788. graph->set_warped_panning(bool(EDITOR_GET("editors/panning/warped_mouse_panning")));
  789. }
  790. void AnimationNodeBlendTreeEditor::_notification(int p_what) {
  791. switch (p_what) {
  792. case NOTIFICATION_ENTER_TREE: {
  793. _update_editor_settings();
  794. } break;
  795. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  796. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  797. _update_editor_settings();
  798. }
  799. } break;
  800. case NOTIFICATION_THEME_CHANGED: {
  801. error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  802. error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  803. if (is_visible_in_tree()) {
  804. update_graph();
  805. }
  806. } break;
  807. case NOTIFICATION_PROCESS: {
  808. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  809. if (!tree) {
  810. return; // Node has been changed.
  811. }
  812. String error;
  813. if (!tree->is_active()) {
  814. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  815. } else if (tree->is_state_invalid()) {
  816. error = tree->get_invalid_state_reason();
  817. }
  818. if (error != error_label->get_text()) {
  819. error_label->set_text(error);
  820. if (!error.is_empty()) {
  821. error_panel->show();
  822. } else {
  823. error_panel->hide();
  824. }
  825. }
  826. List<AnimationNodeBlendTree::NodeConnection> conns;
  827. blend_tree->get_node_connections(&conns);
  828. for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
  829. float activity = 0;
  830. StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  831. if (!tree->is_state_invalid()) {
  832. activity = tree->get_connection_activity(path, E.input_index);
  833. }
  834. graph->set_connection_activity(E.output_node, 0, E.input_node, E.input_index, activity);
  835. }
  836. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  837. Ref<AnimationNodeAnimation> an = blend_tree->get_node(E.key);
  838. if (an.is_valid()) {
  839. if (tree->has_animation(an->get_animation())) {
  840. Ref<Animation> anim = tree->get_animation(an->get_animation());
  841. if (anim.is_valid()) {
  842. //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
  843. StringName length_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_length";
  844. StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_position";
  845. E.value->set_max(tree->get(length_path));
  846. E.value->set_value(tree->get(time_path));
  847. }
  848. }
  849. }
  850. }
  851. for (int i = 0; i < visible_properties.size(); i++) {
  852. visible_properties[i]->update_property();
  853. }
  854. } break;
  855. case NOTIFICATION_VISIBILITY_CHANGED: {
  856. set_process(is_visible_in_tree());
  857. } break;
  858. }
  859. }
  860. void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
  861. if (read_only) {
  862. return;
  863. }
  864. if (updating) {
  865. return;
  866. }
  867. updating = true;
  868. blend_tree->set_graph_offset(p_scroll / EDSCALE);
  869. updating = false;
  870. }
  871. void AnimationNodeBlendTreeEditor::_bind_methods() {
  872. ClassDB::bind_method("update_graph", &AnimationNodeBlendTreeEditor::update_graph);
  873. ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
  874. }
  875. AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = nullptr;
  876. // AnimationNode's "node_changed" signal means almost update_input.
  877. void AnimationNodeBlendTreeEditor::_node_changed(const StringName &p_node_name) {
  878. // TODO:
  879. // Here is executed during the commit of EditorNode::undo_redo, it is not possible to create an undo_redo action here.
  880. // The disconnect when the number of enabled inputs decreases is done in AnimationNodeBlendTree and update_graph().
  881. // This means that there is no place to register undo_redo actions.
  882. // In order to implement undo_redo correctly, we may need to implement AnimationNodeEdit such as AnimationTrackKeyEdit
  883. // and add it to _node_selected() with EditorNode::get_singleton()->push_item(AnimationNodeEdit).
  884. update_graph();
  885. }
  886. void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
  887. if (blend_tree.is_null()) {
  888. return;
  889. }
  890. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  891. if (!tree) {
  892. return;
  893. }
  894. String prev_name = blend_tree->get_node_name(p_node);
  895. ERR_FAIL_COND(prev_name.is_empty());
  896. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
  897. ERR_FAIL_NULL(gn);
  898. const String &new_name = p_text;
  899. ERR_FAIL_COND(new_name.is_empty() || new_name.contains(".") || new_name.contains("/"));
  900. if (new_name == prev_name) {
  901. return; //nothing to do
  902. }
  903. const String &base_name = new_name;
  904. int base = 1;
  905. String name = base_name;
  906. while (blend_tree->has_node(name)) {
  907. base++;
  908. name = base_name + " " + itos(base);
  909. }
  910. String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
  911. updating = true;
  912. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  913. undo_redo->create_action(TTR("Node Renamed"));
  914. undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
  915. undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
  916. undo_redo->add_do_method(this, "update_graph");
  917. undo_redo->add_undo_method(this, "update_graph");
  918. undo_redo->commit_action();
  919. updating = false;
  920. gn->set_name(new_name);
  921. gn->set_size(gn->get_minimum_size());
  922. //change editors accordingly
  923. for (int i = 0; i < visible_properties.size(); i++) {
  924. String pname = visible_properties[i]->get_edited_property().operator String();
  925. if (pname.begins_with(base_path + prev_name)) {
  926. String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
  927. visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
  928. }
  929. }
  930. //recreate connections
  931. graph->clear_connections();
  932. List<AnimationNodeBlendTree::NodeConnection> node_connections;
  933. blend_tree->get_node_connections(&node_connections);
  934. for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
  935. StringName from = E.output_node;
  936. StringName to = E.input_node;
  937. int to_idx = E.input_index;
  938. graph->connect_node(from, 0, to, to_idx);
  939. }
  940. //update animations
  941. for (const KeyValue<StringName, ProgressBar *> &E : animations) {
  942. if (E.key == prev_name) {
  943. animations[new_name] = animations[prev_name];
  944. animations.erase(prev_name);
  945. break;
  946. }
  947. }
  948. update_graph(); // Needed to update the signal connections with the new name.
  949. current_node_rename_text = String();
  950. }
  951. void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node) {
  952. if (current_node_rename_text.is_empty()) {
  953. return; // The text_submitted signal triggered the graph update and freed the LineEdit.
  954. }
  955. _node_renamed(current_node_rename_text, p_node);
  956. }
  957. void AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed(const String &p_text) {
  958. current_node_rename_text = p_text;
  959. }
  960. bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
  961. Ref<AnimationNodeBlendTree> bt = p_node;
  962. return bt.is_valid();
  963. }
  964. void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
  965. if (blend_tree.is_valid()) {
  966. blend_tree->disconnect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  967. }
  968. blend_tree = p_node;
  969. read_only = false;
  970. if (blend_tree.is_null()) {
  971. hide();
  972. } else {
  973. read_only = EditorNode::get_singleton()->is_resource_read_only(blend_tree);
  974. blend_tree->connect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
  975. update_graph();
  976. }
  977. add_node->set_disabled(read_only);
  978. graph->set_show_arrange_button(!read_only);
  979. }
  980. AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
  981. singleton = this;
  982. updating = false;
  983. use_position_from_popup_menu = false;
  984. graph = memnew(GraphEdit);
  985. add_child(graph);
  986. graph->add_valid_right_disconnect_type(0);
  987. graph->add_valid_left_disconnect_type(0);
  988. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  989. graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), CONNECT_DEFERRED);
  990. graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), CONNECT_DEFERRED);
  991. graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));
  992. graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));
  993. graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));
  994. graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));
  995. graph->connect("connection_to_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_to_empty));
  996. graph->connect("connection_from_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_from_empty));
  997. float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
  998. graph->set_minimap_opacity(graph_minimap_opacity);
  999. float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
  1000. graph->set_connection_lines_curvature(graph_lines_curvature);
  1001. VSeparator *vs = memnew(VSeparator);
  1002. graph->get_menu_hbox()->add_child(vs);
  1003. graph->get_menu_hbox()->move_child(vs, 0);
  1004. add_node = memnew(MenuButton);
  1005. graph->get_menu_hbox()->add_child(add_node);
  1006. add_node->set_text(TTR("Add Node..."));
  1007. graph->get_menu_hbox()->move_child(add_node, 0);
  1008. add_node->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_add_node));
  1009. add_node->get_popup()->connect("popup_hide", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_hide), CONNECT_DEFERRED);
  1010. add_node->connect("about_to_popup", callable_mp(this, &AnimationNodeBlendTreeEditor::_update_options_menu).bind(false));
  1011. add_node->set_disabled(read_only);
  1012. add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
  1013. add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot", 2));
  1014. add_options.push_back(AddOption("Add2", "AnimationNodeAdd2", 2));
  1015. add_options.push_back(AddOption("Add3", "AnimationNodeAdd3", 3));
  1016. add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2", 2));
  1017. add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3", 3));
  1018. add_options.push_back(AddOption("Sub2", "AnimationNodeSub2", 2));
  1019. add_options.push_back(AddOption("TimeSeek", "AnimationNodeTimeSeek", 1));
  1020. add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale", 1));
  1021. add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
  1022. add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
  1023. add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
  1024. add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
  1025. add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
  1026. _update_options_menu();
  1027. error_panel = memnew(PanelContainer);
  1028. add_child(error_panel);
  1029. error_label = memnew(Label);
  1030. error_panel->add_child(error_label);
  1031. error_label->set_text("eh");
  1032. filter_dialog = memnew(AcceptDialog);
  1033. add_child(filter_dialog);
  1034. filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
  1035. VBoxContainer *filter_vbox = memnew(VBoxContainer);
  1036. filter_dialog->add_child(filter_vbox);
  1037. HBoxContainer *filter_hbox = memnew(HBoxContainer);
  1038. filter_vbox->add_child(filter_hbox);
  1039. filter_enabled = memnew(CheckBox);
  1040. filter_enabled->set_text(TTR("Enable Filtering"));
  1041. filter_enabled->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_toggled));
  1042. filter_hbox->add_child(filter_enabled);
  1043. filter_fill_selection = memnew(Button);
  1044. filter_fill_selection->set_text(TTR("Fill Selected Children"));
  1045. filter_fill_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_fill_selection));
  1046. filter_hbox->add_child(filter_fill_selection);
  1047. filter_invert_selection = memnew(Button);
  1048. filter_invert_selection->set_text(TTR("Invert"));
  1049. filter_invert_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_invert_selection));
  1050. filter_hbox->add_child(filter_invert_selection);
  1051. filter_clear_selection = memnew(Button);
  1052. filter_clear_selection->set_text(TTR("Clear"));
  1053. filter_clear_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_clear_selection));
  1054. filter_hbox->add_child(filter_clear_selection);
  1055. filters = memnew(Tree);
  1056. filter_vbox->add_child(filters);
  1057. filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1058. filters->set_v_size_flags(SIZE_EXPAND_FILL);
  1059. filters->set_hide_root(true);
  1060. filters->connect("item_edited", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_edited));
  1061. open_file = memnew(EditorFileDialog);
  1062. add_child(open_file);
  1063. open_file->set_title(TTR("Open Animation Node"));
  1064. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1065. open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_file_opened));
  1066. animation_node_inspector_plugin.instantiate();
  1067. EditorInspector::add_inspector_plugin(animation_node_inspector_plugin);
  1068. }
  1069. AnimationNodeBlendTreeEditor::~AnimationNodeBlendTreeEditor() {
  1070. }
  1071. // EditorPluginAnimationNodeAnimation
  1072. void AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog() {
  1073. AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
  1074. StringName anim_name = animation_node_animation->get_animation();
  1075. PackedStringArray markers = tree->has_animation(anim_name) ? tree->get_animation(anim_name)->get_marker_names() : PackedStringArray();
  1076. dialog->select_start->clear();
  1077. dialog->select_start->add_icon_item(get_editor_theme_icon(SNAME("PlayStart")), TTR("Start of Animation"));
  1078. dialog->select_start->add_separator();
  1079. dialog->select_end->clear();
  1080. dialog->select_end->add_icon_item(get_editor_theme_icon(SNAME("PlayStartBackwards")), TTR("End of Animation"));
  1081. dialog->select_end->add_separator();
  1082. for (const String &marker : markers) {
  1083. dialog->select_start->add_item(marker);
  1084. dialog->select_end->add_item(marker);
  1085. }
  1086. // Because the default selections are always valid, and marker times won't change during the dialog, we can ensure that the user can only select valid markers.
  1087. // This invariant is maintained by _validate_markers.
  1088. dialog->select_start->select(0);
  1089. dialog->select_end->select(0);
  1090. dialog->popup_centered(Size2(200, 0) * EDSCALE);
  1091. }
  1092. void AnimationNodeAnimationEditor::_validate_markers(int p_id) {
  1093. // Note: p_id is ignored. It is included because OptionButton's item_changed signal always passes it.
  1094. int start_id = dialog->select_start->get_selected_id();
  1095. int end_id = dialog->select_end->get_selected_id();
  1096. StringName anim_name = animation_node_animation->get_animation();
  1097. Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(anim_name);
  1098. ERR_FAIL_COND(animation.is_null());
  1099. double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));
  1100. double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));
  1101. // p_start and p_end have the same item count.
  1102. for (int i = 2; i < dialog->select_start->get_item_count(); i++) {
  1103. String start_marker = dialog->select_start->get_item_text(i);
  1104. String end_marker = dialog->select_end->get_item_text(i);
  1105. dialog->select_start->set_item_disabled(i, end_id >= 2 && (i == end_id || animation->get_marker_time(start_marker) > end_time));
  1106. dialog->select_end->set_item_disabled(i, start_id >= 2 && (i == start_id || start_time > animation->get_marker_time(end_marker)));
  1107. }
  1108. }
  1109. void AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog() {
  1110. int start_id = dialog->select_start->get_selected_id();
  1111. int end_id = dialog->select_end->get_selected_id();
  1112. Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(animation_node_animation->get_animation());
  1113. ERR_FAIL_COND(animation.is_null());
  1114. double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));
  1115. double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));
  1116. double length = end_time - start_time;
  1117. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1118. undo_redo->create_action(TTR("Set Custom Timeline from Marker"));
  1119. undo_redo->add_do_method(*animation_node_animation, "set_start_offset", start_time);
  1120. undo_redo->add_undo_method(*animation_node_animation, "set_start_offset", animation_node_animation->get_start_offset());
  1121. undo_redo->add_do_method(*animation_node_animation, "set_stretch_time_scale", false);
  1122. undo_redo->add_undo_method(*animation_node_animation, "set_stretch_time_scale", animation_node_animation->is_stretching_time_scale());
  1123. undo_redo->add_do_method(*animation_node_animation, "set_timeline_length", length);
  1124. undo_redo->add_undo_method(*animation_node_animation, "set_timeline_length", animation_node_animation->get_timeline_length());
  1125. undo_redo->add_do_method(*animation_node_animation, "notify_property_list_changed");
  1126. undo_redo->add_undo_method(*animation_node_animation, "notify_property_list_changed");
  1127. undo_redo->commit_action();
  1128. }
  1129. AnimationNodeAnimationEditor::AnimationNodeAnimationEditor(Ref<AnimationNodeAnimation> p_animation_node_animation) {
  1130. animation_node_animation = p_animation_node_animation;
  1131. dialog = memnew(AnimationNodeAnimationEditorDialog);
  1132. add_child(dialog);
  1133. dialog->set_hide_on_ok(false);
  1134. dialog->select_start->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));
  1135. dialog->select_end->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));
  1136. dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog));
  1137. Control *top_spacer = memnew(Control);
  1138. add_child(top_spacer);
  1139. top_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);
  1140. button = memnew(Button);
  1141. add_child(button);
  1142. button->set_text(TTR("Set Custom Timeline from Marker"));
  1143. button->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  1144. button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog));
  1145. Control *bottom_spacer = memnew(Control);
  1146. add_child(bottom_spacer);
  1147. bottom_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);
  1148. }
  1149. AnimationNodeAnimationEditor::~AnimationNodeAnimationEditor() {
  1150. }
  1151. void AnimationNodeAnimationEditor::_notification(int p_what) {
  1152. switch (p_what) {
  1153. case NOTIFICATION_THEME_CHANGED: {
  1154. button->set_theme_type_variation(SNAME("InspectorActionButton"));
  1155. button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  1156. } break;
  1157. }
  1158. }
  1159. bool EditorInspectorPluginAnimationNodeAnimation::can_handle(Object *p_object) {
  1160. Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
  1161. return ana.is_valid() && ana->is_using_custom_timeline();
  1162. }
  1163. bool EditorInspectorPluginAnimationNodeAnimation::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  1164. Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
  1165. ERR_FAIL_COND_V(ana.is_null(), false);
  1166. if (p_path == "timeline_length") {
  1167. add_custom_control(memnew(AnimationNodeAnimationEditor(ana)));
  1168. }
  1169. return false;
  1170. }
  1171. AnimationNodeAnimationEditorDialog::AnimationNodeAnimationEditorDialog() {
  1172. set_title(TTR("Select Markers"));
  1173. GridContainer *grid = memnew(GridContainer);
  1174. grid->set_columns(2);
  1175. grid->set_offsets_preset(Control::PRESET_FULL_RECT);
  1176. add_child(grid);
  1177. Label *label_start = memnew(Label(TTR("Start Marker")));
  1178. grid->add_child(label_start);
  1179. label_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1180. label_start->set_stretch_ratio(1);
  1181. select_start = memnew(OptionButton);
  1182. select_start->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1183. grid->add_child(select_start);
  1184. select_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1185. select_start->set_stretch_ratio(2);
  1186. Label *label_end = memnew(Label(TTR("End Marker")));
  1187. grid->add_child(label_end);
  1188. label_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1189. label_end->set_stretch_ratio(1);
  1190. select_end = memnew(OptionButton);
  1191. select_end->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1192. grid->add_child(select_end);
  1193. select_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1194. select_end->set_stretch_ratio(2);
  1195. }
  1196. AnimationNodeAnimationEditorDialog::~AnimationNodeAnimationEditorDialog() {
  1197. }