animation_state_machine_editor.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  1. /*************************************************************************/
  2. /* animation_state_machine_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_state_machine_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/geometry_2d.h"
  35. #include "core/os/keyboard.h"
  36. #include "editor/editor_file_dialog.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "scene/animation/animation_blend_tree.h"
  40. #include "scene/animation/animation_player.h"
  41. #include "scene/gui/menu_button.h"
  42. #include "scene/gui/panel.h"
  43. #include "scene/gui/tree.h"
  44. #include "scene/main/viewport.h"
  45. #include "scene/main/window.h"
  46. bool AnimationNodeStateMachineEditor::can_edit(const Ref<AnimationNode> &p_node) {
  47. Ref<AnimationNodeStateMachine> ansm = p_node;
  48. return ansm.is_valid();
  49. }
  50. void AnimationNodeStateMachineEditor::edit(const Ref<AnimationNode> &p_node) {
  51. state_machine = p_node;
  52. if (state_machine.is_valid()) {
  53. selected_transition_from = StringName();
  54. selected_transition_to = StringName();
  55. selected_transition_index = -1;
  56. selected_multi_transition = TransitionLine();
  57. selected_node = StringName();
  58. selected_nodes.clear();
  59. _update_mode();
  60. _update_graph();
  61. }
  62. }
  63. void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref<InputEvent> &p_event) {
  64. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  65. if (playback.is_null()) {
  66. return;
  67. }
  68. Ref<InputEventKey> k = p_event;
  69. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
  70. if (selected_node != StringName() || !selected_nodes.is_empty() || selected_transition_to != StringName() || selected_transition_from != StringName()) {
  71. _erase_selected();
  72. accept_event();
  73. }
  74. }
  75. // Group selected nodes on a state machine
  76. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->is_ctrl_pressed() && !k->is_shift_pressed() && k->get_keycode() == Key::G && !k->is_echo()) {
  77. _group_selected_nodes();
  78. }
  79. // Ungroup state machine
  80. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->is_ctrl_pressed() && k->is_shift_pressed() && k->get_keycode() == Key::G && !k->is_echo()) {
  81. _ungroup_selected_nodes();
  82. }
  83. Ref<InputEventMouseButton> mb = p_event;
  84. // Add new node
  85. if (mb.is_valid() && mb->is_pressed() && !box_selecting && !connecting && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (tool_create->is_pressed() && mb->get_button_index() == MouseButton::LEFT))) {
  86. connecting_from = StringName();
  87. _open_menu(mb->get_position());
  88. }
  89. // Select node or push a field inside
  90. if (mb.is_valid() && !mb->is_shift_pressed() && !mb->is_ctrl_pressed() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  91. selected_transition_from = StringName();
  92. selected_transition_to = StringName();
  93. selected_transition_index = -1;
  94. selected_multi_transition = TransitionLine();
  95. selected_node = StringName();
  96. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  97. if (node_rects[i].play.has_point(mb->get_position())) { //edit name
  98. if (play_mode->get_selected() == 1 || !playback->is_playing()) {
  99. //start
  100. playback->start(node_rects[i].node_name);
  101. } else {
  102. //travel
  103. playback->travel(node_rects[i].node_name);
  104. }
  105. state_machine_draw->update();
  106. return;
  107. }
  108. if (node_rects[i].name.has_point(mb->get_position()) && state_machine->can_edit_node(node_rects[i].node_name)) { // edit name
  109. Ref<StyleBox> line_sb = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"));
  110. Rect2 edit_rect = node_rects[i].name;
  111. edit_rect.position -= line_sb->get_offset();
  112. edit_rect.size += line_sb->get_minimum_size();
  113. name_edit_popup->set_position(state_machine_draw->get_screen_position() + edit_rect.position);
  114. name_edit_popup->set_size(edit_rect.size);
  115. name_edit->set_text(node_rects[i].node_name);
  116. name_edit_popup->popup();
  117. name_edit->grab_focus();
  118. name_edit->select_all();
  119. prev_name = node_rects[i].node_name;
  120. return;
  121. }
  122. if (node_rects[i].edit.has_point(mb->get_position())) { //edit name
  123. call_deferred(SNAME("_open_editor"), node_rects[i].node_name);
  124. return;
  125. }
  126. if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
  127. selected_node = node_rects[i].node_name;
  128. if (!selected_nodes.has(selected_node)) {
  129. selected_nodes.clear();
  130. }
  131. selected_nodes.insert(selected_node);
  132. Ref<AnimationNode> anode = state_machine->get_node(selected_node);
  133. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  134. state_machine_draw->update();
  135. dragging_selected_attempt = true;
  136. dragging_selected = false;
  137. drag_from = mb->get_position();
  138. snap_x = StringName();
  139. snap_y = StringName();
  140. _update_mode();
  141. return;
  142. }
  143. }
  144. //test the lines now
  145. int closest = -1;
  146. float closest_d = 1e20;
  147. for (int i = 0; i < transition_lines.size(); i++) {
  148. Vector2 s[2] = {
  149. transition_lines[i].from,
  150. transition_lines[i].to
  151. };
  152. Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mb->get_position(), s);
  153. float d = cpoint.distance_to(mb->get_position());
  154. if (d > transition_lines[i].width) {
  155. continue;
  156. }
  157. if (d < closest_d) {
  158. closest = i;
  159. closest_d = d;
  160. }
  161. }
  162. if (closest >= 0) {
  163. selected_transition_from = transition_lines[closest].from_node;
  164. selected_transition_to = transition_lines[closest].to_node;
  165. selected_transition_index = closest;
  166. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(closest);
  167. EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
  168. if (!transition_lines[closest].multi_transitions.is_empty()) {
  169. selected_transition_index = -1;
  170. selected_multi_transition = transition_lines[closest];
  171. Ref<EditorAnimationMultiTransitionEdit> multi;
  172. multi.instantiate();
  173. multi->add_transition(selected_transition_from, selected_transition_to, tr);
  174. for (int i = 0; i < transition_lines[closest].multi_transitions.size(); i++) {
  175. int index = transition_lines[closest].multi_transitions[i].transition_index;
  176. Ref<AnimationNodeStateMachineTransition> transition = state_machine->get_transition(index);
  177. StringName from = transition_lines[closest].multi_transitions[i].from_node;
  178. StringName to = transition_lines[closest].multi_transitions[i].to_node;
  179. multi->add_transition(from, to, transition);
  180. }
  181. EditorNode::get_singleton()->push_item(multi.ptr(), "", true);
  182. }
  183. }
  184. state_machine_draw->update();
  185. _update_mode();
  186. }
  187. // End moving node
  188. if (mb.is_valid() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  189. if (dragging_selected) {
  190. Ref<AnimationNode> an = state_machine->get_node(selected_node);
  191. updating = true;
  192. undo_redo->create_action(TTR("Move Node"));
  193. for (int i = 0; i < node_rects.size(); i++) {
  194. if (!selected_nodes.has(node_rects[i].node_name)) {
  195. continue;
  196. }
  197. undo_redo->add_do_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name) + drag_ofs / EDSCALE);
  198. undo_redo->add_undo_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name));
  199. }
  200. undo_redo->add_do_method(this, "_update_graph");
  201. undo_redo->add_undo_method(this, "_update_graph");
  202. undo_redo->commit_action();
  203. updating = false;
  204. }
  205. snap_x = StringName();
  206. snap_y = StringName();
  207. dragging_selected_attempt = false;
  208. dragging_selected = false;
  209. state_machine_draw->update();
  210. }
  211. // Connect nodes
  212. if (mb.is_valid() && ((tool_select->is_pressed() && mb->is_shift_pressed()) || tool_connect->is_pressed()) && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  213. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  214. if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
  215. connecting = true;
  216. connecting_from = node_rects[i].node_name;
  217. connecting_to = mb->get_position();
  218. connecting_to_node = StringName();
  219. return;
  220. }
  221. }
  222. }
  223. // End connecting nodes
  224. if (mb.is_valid() && connecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  225. if (connecting_to_node != StringName()) {
  226. Ref<AnimationNode> node = state_machine->get_node(connecting_to_node);
  227. Ref<AnimationNodeStateMachine> anodesm = node;
  228. Ref<AnimationNodeEndState> end_node = node;
  229. if (state_machine->has_transition(connecting_from, connecting_to_node) && state_machine->can_edit_node(connecting_to_node) && !anodesm.is_valid()) {
  230. EditorNode::get_singleton()->show_warning(TTR("Transition exists!"));
  231. connecting = false;
  232. } else {
  233. if (anodesm.is_valid() || end_node.is_valid()) {
  234. _open_connect_menu(mb->get_position());
  235. } else {
  236. _add_transition();
  237. }
  238. }
  239. } else {
  240. _open_menu(mb->get_position());
  241. }
  242. connecting_to_node = StringName();
  243. state_machine_draw->update();
  244. }
  245. // Start box selecting
  246. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && tool_select->is_pressed()) {
  247. box_selecting = true;
  248. box_selecting_from = box_selecting_to = state_machine_draw->get_local_mouse_position();
  249. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  250. MIN(box_selecting_from.y, box_selecting_to.y),
  251. ABS(box_selecting_from.x - box_selecting_to.x),
  252. ABS(box_selecting_from.y - box_selecting_to.y));
  253. if (mb->is_ctrl_pressed() || mb->is_shift_pressed()) {
  254. previous_selected = selected_nodes;
  255. } else {
  256. selected_nodes.clear();
  257. previous_selected.clear();
  258. }
  259. }
  260. // End box selecting
  261. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
  262. box_selecting = false;
  263. state_machine_draw->update();
  264. _update_mode();
  265. }
  266. Ref<InputEventMouseMotion> mm = p_event;
  267. // Pan window
  268. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_MIDDLE) != MouseButton::NONE) {
  269. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  270. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  271. }
  272. // Move mouse while connecting
  273. if (mm.is_valid() && connecting) {
  274. connecting_to = mm->get_position();
  275. connecting_to_node = StringName();
  276. state_machine_draw->update();
  277. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  278. if (node_rects[i].node_name != connecting_from && node_rects[i].node.has_point(connecting_to)) { //select node since nothing else was selected
  279. connecting_to_node = node_rects[i].node_name;
  280. return;
  281. }
  282. }
  283. }
  284. // Move mouse while moving a node
  285. if (mm.is_valid() && dragging_selected_attempt) {
  286. dragging_selected = true;
  287. drag_ofs = mm->get_position() - drag_from;
  288. snap_x = StringName();
  289. snap_y = StringName();
  290. {
  291. //snap
  292. Vector2 cpos = state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE;
  293. List<StringName> nodes;
  294. state_machine->get_node_list(&nodes);
  295. float best_d_x = 1e20;
  296. float best_d_y = 1e20;
  297. for (const StringName &E : nodes) {
  298. if (E == selected_node) {
  299. continue;
  300. }
  301. Vector2 npos = state_machine->get_node_position(E);
  302. float d_x = ABS(npos.x - cpos.x);
  303. if (d_x < MIN(5, best_d_x)) {
  304. drag_ofs.x -= cpos.x - npos.x;
  305. best_d_x = d_x;
  306. snap_x = E;
  307. }
  308. float d_y = ABS(npos.y - cpos.y);
  309. if (d_y < MIN(5, best_d_y)) {
  310. drag_ofs.y -= cpos.y - npos.y;
  311. best_d_y = d_y;
  312. snap_y = E;
  313. }
  314. }
  315. }
  316. state_machine_draw->update();
  317. }
  318. // Move mouse while moving box select
  319. if (mm.is_valid() && box_selecting) {
  320. box_selecting_to = state_machine_draw->get_local_mouse_position();
  321. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  322. MIN(box_selecting_from.y, box_selecting_to.y),
  323. ABS(box_selecting_from.x - box_selecting_to.x),
  324. ABS(box_selecting_from.y - box_selecting_to.y));
  325. for (int i = 0; i < node_rects.size(); i++) {
  326. bool in_box = node_rects[i].node.intersects(box_selecting_rect);
  327. if (in_box) {
  328. if (previous_selected.has(node_rects[i].node_name)) {
  329. selected_nodes.erase(node_rects[i].node_name);
  330. } else {
  331. selected_nodes.insert(node_rects[i].node_name);
  332. }
  333. } else {
  334. if (previous_selected.has(node_rects[i].node_name)) {
  335. selected_nodes.insert(node_rects[i].node_name);
  336. } else {
  337. selected_nodes.erase(node_rects[i].node_name);
  338. }
  339. }
  340. }
  341. state_machine_draw->update();
  342. }
  343. if (mm.is_valid()) {
  344. state_machine_draw->grab_focus();
  345. String new_over_node = StringName();
  346. int new_over_node_what = -1;
  347. if (tool_select->is_pressed()) {
  348. for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.
  349. if (!state_machine->can_edit_node(node_rects[i].node_name)) {
  350. continue; // start/end node can't be edited
  351. }
  352. if (node_rects[i].node.has_point(mm->get_position())) {
  353. new_over_node = node_rects[i].node_name;
  354. if (node_rects[i].play.has_point(mm->get_position())) {
  355. new_over_node_what = 0;
  356. } else if (node_rects[i].edit.has_point(mm->get_position())) {
  357. new_over_node_what = 1;
  358. }
  359. break;
  360. }
  361. }
  362. }
  363. if (new_over_node != over_node || new_over_node_what != over_node_what) {
  364. over_node = new_over_node;
  365. over_node_what = new_over_node_what;
  366. state_machine_draw->update();
  367. }
  368. // set tooltip for transition
  369. if (tool_select->is_pressed()) {
  370. int closest = -1;
  371. float closest_d = 1e20;
  372. for (int i = 0; i < transition_lines.size(); i++) {
  373. Vector2 s[2] = {
  374. transition_lines[i].from,
  375. transition_lines[i].to
  376. };
  377. Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mm->get_position(), s);
  378. float d = cpoint.distance_to(mm->get_position());
  379. if (d > transition_lines[i].width) {
  380. continue;
  381. }
  382. if (d < closest_d) {
  383. closest = i;
  384. closest_d = d;
  385. }
  386. }
  387. if (closest >= 0) {
  388. String from = String(transition_lines[closest].from_node);
  389. String to = String(transition_lines[closest].to_node);
  390. String tooltip = from + " -> " + to;
  391. for (int i = 0; i < transition_lines[closest].multi_transitions.size(); i++) {
  392. from = String(transition_lines[closest].multi_transitions[i].from_node);
  393. to = String(transition_lines[closest].multi_transitions[i].to_node);
  394. tooltip += "\n" + from + " -> " + to;
  395. }
  396. state_machine_draw->set_tooltip(tooltip);
  397. } else {
  398. state_machine_draw->set_tooltip("");
  399. }
  400. }
  401. }
  402. Ref<InputEventPanGesture> pan_gesture = p_event;
  403. if (pan_gesture.is_valid()) {
  404. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  405. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  406. }
  407. }
  408. Control::CursorShape AnimationNodeStateMachineEditor::get_cursor_shape(const Point2 &p_pos) const {
  409. // Put ibeam (text cursor) over names to make it clearer that they are editable.
  410. Transform2D xform = panel->get_transform() * state_machine_draw->get_transform();
  411. Point2 pos = xform.xform_inv(p_pos);
  412. Control::CursorShape cursor_shape = get_default_cursor_shape();
  413. for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.
  414. if (node_rects[i].node.has_point(pos)) {
  415. if (node_rects[i].name.has_point(pos)) {
  416. cursor_shape = Control::CURSOR_IBEAM;
  417. }
  418. break;
  419. }
  420. }
  421. return cursor_shape;
  422. }
  423. void AnimationNodeStateMachineEditor::_group_selected_nodes() {
  424. if (!selected_nodes.is_empty()) {
  425. if (selected_nodes.size() == 1 && (*selected_nodes.begin() == state_machine->start_node || *selected_nodes.begin() == state_machine->end_node))
  426. return;
  427. Ref<AnimationNodeStateMachine> group_sm = memnew(AnimationNodeStateMachine);
  428. Vector2 group_position;
  429. Vector<NodeUR> nodes_ur;
  430. Vector<TransitionUR> transitions_ur;
  431. int base = 1;
  432. String base_name = group_sm->get_caption();
  433. String group_name = base_name;
  434. while (state_machine->has_node(group_name) && !selected_nodes.has(group_name)) {
  435. base++;
  436. group_name = base_name + " " + itos(base);
  437. }
  438. updating = true;
  439. undo_redo->create_action("Group");
  440. // Move selected nodes to the new state machine
  441. for (const StringName &E : selected_nodes) {
  442. if (!state_machine->can_edit_node(E)) {
  443. continue;
  444. }
  445. Ref<AnimationNode> node = state_machine->get_node(E);
  446. Vector2 node_position = state_machine->get_node_position(E);
  447. group_position += node_position;
  448. NodeUR new_node;
  449. new_node.name = E;
  450. new_node.node = node;
  451. new_node.position = node_position;
  452. nodes_ur.push_back(new_node);
  453. }
  454. // Add the transitions to the new state machine
  455. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  456. String from = state_machine->get_transition_from(i);
  457. String to = state_machine->get_transition_to(i);
  458. String local_from = from.get_slicec('/', 0);
  459. String local_to = to.get_slicec('/', 0);
  460. String old_from = from;
  461. String old_to = to;
  462. bool from_selected = false;
  463. bool to_selected = false;
  464. if (selected_nodes.has(local_from) && local_from != state_machine->start_node) {
  465. from_selected = true;
  466. }
  467. if (selected_nodes.has(local_to) && local_to != state_machine->end_node) {
  468. to_selected = true;
  469. }
  470. if (!from_selected && !to_selected) {
  471. continue;
  472. }
  473. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);
  474. if (!from_selected) {
  475. from = "../" + old_from;
  476. }
  477. if (!to_selected) {
  478. to = "../" + old_to;
  479. }
  480. TransitionUR new_tr;
  481. new_tr.new_from = from;
  482. new_tr.new_to = to;
  483. new_tr.old_from = old_from;
  484. new_tr.old_to = old_to;
  485. new_tr.transition = tr;
  486. transitions_ur.push_back(new_tr);
  487. }
  488. for (int i = 0; i < nodes_ur.size(); i++) {
  489. undo_redo->add_do_method(state_machine.ptr(), "remove_node", nodes_ur[i].name);
  490. undo_redo->add_undo_method(group_sm.ptr(), "remove_node", nodes_ur[i].name);
  491. }
  492. undo_redo->add_do_method(state_machine.ptr(), "add_node", group_name, group_sm, group_position / nodes_ur.size());
  493. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", group_name);
  494. for (int i = 0; i < nodes_ur.size(); i++) {
  495. undo_redo->add_do_method(group_sm.ptr(), "add_node", nodes_ur[i].name, nodes_ur[i].node, nodes_ur[i].position);
  496. undo_redo->add_undo_method(state_machine.ptr(), "add_node", nodes_ur[i].name, nodes_ur[i].node, nodes_ur[i].position);
  497. }
  498. for (int i = 0; i < transitions_ur.size(); i++) {
  499. undo_redo->add_do_method(group_sm.ptr(), "add_transition", transitions_ur[i].new_from, transitions_ur[i].new_to, transitions_ur[i].transition);
  500. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", transitions_ur[i].old_from, transitions_ur[i].old_to, transitions_ur[i].transition);
  501. }
  502. undo_redo->add_do_method(this, "_update_graph");
  503. undo_redo->add_undo_method(this, "_update_graph");
  504. undo_redo->commit_action();
  505. updating = false;
  506. selected_nodes.clear();
  507. selected_nodes.insert(group_name);
  508. state_machine_draw->update();
  509. accept_event();
  510. _update_mode();
  511. }
  512. }
  513. void AnimationNodeStateMachineEditor::_ungroup_selected_nodes() {
  514. bool find = false;
  515. HashSet<StringName> new_selected_nodes;
  516. for (const StringName &E : selected_nodes) {
  517. Ref<AnimationNodeStateMachine> group_sm = state_machine->get_node(E);
  518. if (group_sm.is_valid()) {
  519. find = true;
  520. Vector2 group_position = state_machine->get_node_position(E);
  521. StringName group_name = E;
  522. List<AnimationNode::ChildNode> nodes;
  523. group_sm->get_child_nodes(&nodes);
  524. Vector<NodeUR> nodes_ur;
  525. Vector<TransitionUR> transitions_ur;
  526. updating = true;
  527. undo_redo->create_action("Ungroup");
  528. // Move all child nodes to current state machine
  529. for (int i = 0; i < nodes.size(); i++) {
  530. if (!group_sm->can_edit_node(nodes[i].name)) {
  531. continue;
  532. }
  533. Vector2 node_position = group_sm->get_node_position(nodes[i].name);
  534. NodeUR new_node;
  535. new_node.name = nodes[i].name;
  536. new_node.position = node_position;
  537. new_node.node = nodes[i].node;
  538. nodes_ur.push_back(new_node);
  539. }
  540. for (int i = 0; i < group_sm->get_transition_count(); i++) {
  541. String from = group_sm->get_transition_from(i);
  542. String to = group_sm->get_transition_to(i);
  543. Ref<AnimationNodeStateMachineTransition> tr = group_sm->get_transition(i);
  544. TransitionUR new_tr;
  545. new_tr.new_from = from.replace_first("../", "");
  546. new_tr.new_to = to.replace_first("../", "");
  547. new_tr.old_from = from;
  548. new_tr.old_to = to;
  549. new_tr.transition = tr;
  550. transitions_ur.push_back(new_tr);
  551. }
  552. for (int i = 0; i < nodes_ur.size(); i++) {
  553. undo_redo->add_do_method(group_sm.ptr(), "remove_node", nodes_ur[i].name);
  554. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", nodes_ur[i].name);
  555. }
  556. undo_redo->add_do_method(state_machine.ptr(), "remove_node", group_name);
  557. undo_redo->add_undo_method(state_machine.ptr(), "add_node", group_name, group_sm, group_position);
  558. for (int i = 0; i < nodes_ur.size(); i++) {
  559. new_selected_nodes.insert(nodes_ur[i].name);
  560. undo_redo->add_do_method(state_machine.ptr(), "add_node", nodes_ur[i].name, nodes_ur[i].node, nodes_ur[i].position);
  561. undo_redo->add_undo_method(group_sm.ptr(), "add_node", nodes_ur[i].name, nodes_ur[i].node, nodes_ur[i].position);
  562. }
  563. for (int i = 0; i < transitions_ur.size(); i++) {
  564. if (transitions_ur[i].old_from != state_machine->start_node && transitions_ur[i].old_to != state_machine->end_node) {
  565. undo_redo->add_do_method(state_machine.ptr(), "add_transition", transitions_ur[i].new_from, transitions_ur[i].new_to, transitions_ur[i].transition);
  566. }
  567. undo_redo->add_undo_method(group_sm.ptr(), "add_transition", transitions_ur[i].old_from, transitions_ur[i].old_to, transitions_ur[i].transition);
  568. }
  569. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  570. String from = state_machine->get_transition_from(i);
  571. String to = state_machine->get_transition_to(i);
  572. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);
  573. if (from == group_name || to == group_name) {
  574. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, tr);
  575. }
  576. }
  577. undo_redo->add_do_method(this, "_update_graph");
  578. undo_redo->add_undo_method(this, "_update_graph");
  579. undo_redo->commit_action();
  580. updating = false;
  581. }
  582. }
  583. if (find) {
  584. selected_nodes = new_selected_nodes;
  585. selected_node = StringName();
  586. state_machine_draw->update();
  587. accept_event();
  588. _update_mode();
  589. }
  590. }
  591. void AnimationNodeStateMachineEditor::_open_menu(const Vector2 &p_position) {
  592. menu->clear();
  593. animations_menu->clear();
  594. animations_to_add.clear();
  595. List<StringName> classes;
  596. classes.sort_custom<StringName::AlphCompare>();
  597. ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
  598. menu->add_submenu_item(TTR("Add Animation"), "animations");
  599. AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
  600. ERR_FAIL_COND(!gp);
  601. if (gp && gp->has_node(gp->get_animation_player())) {
  602. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));
  603. if (ap) {
  604. List<StringName> names;
  605. ap->get_animation_list(&names);
  606. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  607. animations_menu->add_icon_item(get_theme_icon("Animation", "EditorIcons"), E->get());
  608. animations_to_add.push_back(E->get());
  609. }
  610. }
  611. }
  612. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  613. String name = String(E->get()).replace_first("AnimationNode", "");
  614. if (name == "Animation" || name == "StartState" || name == "EndState") {
  615. continue; // nope
  616. }
  617. int idx = menu->get_item_count();
  618. menu->add_item(vformat(TTR("Add %s"), name), idx);
  619. menu->set_item_metadata(idx, E->get());
  620. }
  621. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  622. if (clipb.is_valid()) {
  623. menu->add_separator();
  624. menu->add_item(TTR("Paste"), MENU_PASTE);
  625. }
  626. menu->add_separator();
  627. menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
  628. menu->set_position(state_machine_draw->get_screen_transform().xform(p_position));
  629. menu->popup();
  630. add_node_pos = p_position / EDSCALE + state_machine->get_graph_offset();
  631. }
  632. void AnimationNodeStateMachineEditor::_open_connect_menu(const Vector2 &p_position) {
  633. ERR_FAIL_COND(connecting_to_node == StringName());
  634. Ref<AnimationNode> node = state_machine->get_node(connecting_to_node);
  635. Ref<AnimationNodeStateMachine> anodesm = node;
  636. Ref<AnimationNodeEndState> end_node = node;
  637. ERR_FAIL_COND(!anodesm.is_valid() && !end_node.is_valid());
  638. connect_menu->clear();
  639. state_machine_menu->clear();
  640. end_menu->clear();
  641. nodes_to_connect.clear();
  642. for (int i = connect_menu->get_child_count() - 1; i >= 0; i--) {
  643. Node *child = connect_menu->get_child(i);
  644. if (child->is_class("PopupMenu")) {
  645. connect_menu->remove_child(child);
  646. }
  647. }
  648. connect_menu->reset_size();
  649. state_machine_menu->reset_size();
  650. end_menu->reset_size();
  651. if (anodesm.is_valid()) {
  652. _create_submenu(connect_menu, anodesm, connecting_to_node, connecting_to_node);
  653. } else {
  654. Ref<AnimationNodeStateMachine> prev = state_machine;
  655. _create_submenu(connect_menu, prev, connecting_to_node, connecting_to_node, true);
  656. }
  657. connect_menu->add_submenu_item(TTR("To") + " Animation", connecting_to_node);
  658. if (state_machine_menu->get_item_count() > 0 || !end_node.is_valid()) {
  659. connect_menu->add_submenu_item(TTR("To") + " StateMachine", "state_machines");
  660. connect_menu->add_child(state_machine_menu);
  661. }
  662. if (end_node.is_valid()) {
  663. connect_menu->add_submenu_item(TTR("To") + " End", "end_nodes");
  664. connect_menu->add_child(end_menu);
  665. } else {
  666. state_machine_menu->add_item(connecting_to_node, nodes_to_connect.size());
  667. }
  668. nodes_to_connect.push_back(connecting_to_node);
  669. if (nodes_to_connect.size() == 1) {
  670. _add_transition();
  671. return;
  672. }
  673. connect_menu->set_position(state_machine_draw->get_screen_transform().xform(p_position));
  674. connect_menu->popup();
  675. }
  676. bool AnimationNodeStateMachineEditor::_create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path, bool from_root, Vector<Ref<AnimationNodeStateMachine>> p_parents) {
  677. String prev_path;
  678. Vector<Ref<AnimationNodeStateMachine>> parents = p_parents;
  679. if (from_root) {
  680. AnimationNodeStateMachine *prev = p_nodesm->get_prev_state_machine();
  681. while (prev != nullptr) {
  682. parents.push_back(prev);
  683. p_nodesm = Ref<AnimationNodeStateMachine>(prev);
  684. prev_path += "../";
  685. prev = prev->get_prev_state_machine();
  686. }
  687. prev_path.remove_at(prev_path.size() - 1);
  688. }
  689. List<StringName> nodes;
  690. p_nodesm->get_node_list(&nodes);
  691. PopupMenu *nodes_menu = memnew(PopupMenu);
  692. nodes_menu->set_name(p_name);
  693. nodes_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
  694. p_menu->add_child(nodes_menu);
  695. bool node_added = false;
  696. for (const StringName &E : nodes) {
  697. if (p_nodesm->can_edit_node(E)) {
  698. Ref<AnimationNodeStateMachine> ansm = p_nodesm->get_node(E);
  699. String path;
  700. if (from_root) {
  701. path = prev_path + "/" + E;
  702. } else {
  703. path = String(p_path) + "/" + E;
  704. }
  705. if (ansm == state_machine) {
  706. end_menu->add_item(E, nodes_to_connect.size());
  707. nodes_to_connect.push_back(state_machine->end_node);
  708. continue;
  709. }
  710. if (ansm.is_valid()) {
  711. bool found = false;
  712. for (int i = 0; i < parents.size(); i++) {
  713. if (parents[i] == ansm) {
  714. path = path.replace_first("/../" + E, "");
  715. found = true;
  716. break;
  717. }
  718. }
  719. if (!found) {
  720. state_machine_menu->add_item(E, nodes_to_connect.size());
  721. nodes_to_connect.push_back(path);
  722. } else {
  723. end_menu->add_item(E, nodes_to_connect.size());
  724. nodes_to_connect.push_back(path + "/" + state_machine->end_node);
  725. }
  726. if (_create_submenu(nodes_menu, ansm, E, path, false, parents)) {
  727. nodes_menu->add_submenu_item(E, E);
  728. node_added = true;
  729. }
  730. } else {
  731. nodes_menu->add_item(E, nodes_to_connect.size());
  732. nodes_to_connect.push_back(path);
  733. node_added = true;
  734. }
  735. }
  736. }
  737. return node_added;
  738. }
  739. void AnimationNodeStateMachineEditor::_stop_connecting() {
  740. connecting = false;
  741. state_machine_draw->update();
  742. }
  743. void AnimationNodeStateMachineEditor::_delete_selected() {
  744. TreeItem *item = delete_tree->get_next_selected(nullptr);
  745. while (item) {
  746. if (!updating) {
  747. updating = true;
  748. selected_multi_transition = TransitionLine();
  749. undo_redo->create_action("Transition(s) Removed");
  750. }
  751. Vector<String> path = item->get_text(0).split(" -> ");
  752. selected_transition_from = path[0];
  753. selected_transition_to = path[1];
  754. _erase_selected(true);
  755. item = delete_tree->get_next_selected(item);
  756. }
  757. if (updating) {
  758. undo_redo->commit_action();
  759. updating = false;
  760. }
  761. }
  762. void AnimationNodeStateMachineEditor::_delete_all() {
  763. Vector<TransitionLine> multi_transitions = selected_multi_transition.multi_transitions;
  764. selected_multi_transition = TransitionLine();
  765. updating = true;
  766. undo_redo->create_action("Transition(s) Removed");
  767. _erase_selected(true);
  768. for (int i = 0; i < multi_transitions.size(); i++) {
  769. selected_transition_from = multi_transitions[i].from_node;
  770. selected_transition_to = multi_transitions[i].to_node;
  771. _erase_selected(true);
  772. }
  773. undo_redo->commit_action();
  774. updating = false;
  775. delete_window->hide();
  776. }
  777. void AnimationNodeStateMachineEditor::_delete_tree_draw() {
  778. TreeItem *item = delete_tree->get_next_selected(nullptr);
  779. while (item) {
  780. delete_window->get_cancel_button()->set_disabled(false);
  781. return;
  782. }
  783. delete_window->get_cancel_button()->set_disabled(true);
  784. }
  785. void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) {
  786. file_loaded = ResourceLoader::load(p_file);
  787. if (file_loaded.is_valid()) {
  788. _add_menu_type(MENU_LOAD_FILE_CONFIRM);
  789. }
  790. }
  791. void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) {
  792. String base_name;
  793. Ref<AnimationRootNode> node;
  794. if (p_index == MENU_LOAD_FILE) {
  795. open_file->clear_filters();
  796. List<String> filters;
  797. ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
  798. for (const String &E : filters) {
  799. open_file->add_filter("*." + E);
  800. }
  801. open_file->popup_file_dialog();
  802. return;
  803. } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
  804. node = file_loaded;
  805. file_loaded.unref();
  806. } else if (p_index == MENU_PASTE) {
  807. node = EditorSettings::get_singleton()->get_resource_clipboard();
  808. } else {
  809. String type = menu->get_item_metadata(p_index);
  810. Object *obj = ClassDB::instantiate(type);
  811. ERR_FAIL_COND(!obj);
  812. AnimationNode *an = Object::cast_to<AnimationNode>(obj);
  813. ERR_FAIL_COND(!an);
  814. node = Ref<AnimationNode>(an);
  815. base_name = type.replace_first("AnimationNode", "");
  816. }
  817. if (!node.is_valid()) {
  818. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
  819. return;
  820. }
  821. if (base_name.is_empty()) {
  822. base_name = node->get_class().replace_first("AnimationNode", "");
  823. }
  824. int base = 1;
  825. String name = base_name;
  826. while (state_machine->has_node(name)) {
  827. base++;
  828. name = base_name + " " + itos(base);
  829. }
  830. updating = true;
  831. undo_redo->create_action(TTR("Add Node"));
  832. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos);
  833. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  834. undo_redo->add_do_method(this, "_update_graph");
  835. undo_redo->add_undo_method(this, "_update_graph");
  836. connecting_to_node = name;
  837. _add_transition(true);
  838. undo_redo->commit_action();
  839. updating = false;
  840. state_machine_draw->update();
  841. }
  842. void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) {
  843. Ref<AnimationNodeAnimation> anim;
  844. anim.instantiate();
  845. anim->set_animation(animations_to_add[p_index]);
  846. String base_name = animations_to_add[p_index];
  847. int base = 1;
  848. String name = base_name;
  849. while (state_machine->has_node(name)) {
  850. base++;
  851. name = base_name + " " + itos(base);
  852. }
  853. updating = true;
  854. undo_redo->create_action(TTR("Add Node"));
  855. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos);
  856. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  857. undo_redo->add_do_method(this, "_update_graph");
  858. undo_redo->add_undo_method(this, "_update_graph");
  859. connecting_to_node = name;
  860. _add_transition(true);
  861. undo_redo->commit_action();
  862. updating = false;
  863. state_machine_draw->update();
  864. }
  865. void AnimationNodeStateMachineEditor::_connect_to(int p_index) {
  866. connecting_to_node = nodes_to_connect[p_index];
  867. _add_transition();
  868. }
  869. void AnimationNodeStateMachineEditor::_add_transition(const bool p_nested_action) {
  870. if (connecting_from != StringName() && connecting_to_node != StringName()) {
  871. if (state_machine->has_transition(connecting_from, connecting_to_node)) {
  872. EditorNode::get_singleton()->show_warning("Transition exists!");
  873. connecting = false;
  874. return;
  875. }
  876. Ref<AnimationNodeStateMachineTransition> tr;
  877. tr.instantiate();
  878. tr->set_switch_mode(AnimationNodeStateMachineTransition::SwitchMode(transition_mode->get_selected()));
  879. if (!p_nested_action) {
  880. updating = true;
  881. }
  882. undo_redo->create_action(TTR("Add Transition"));
  883. undo_redo->add_do_method(state_machine.ptr(), "add_transition", connecting_from, connecting_to_node, tr);
  884. undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", connecting_from, connecting_to_node);
  885. undo_redo->add_do_method(this, "_update_graph");
  886. undo_redo->add_undo_method(this, "_update_graph");
  887. undo_redo->commit_action();
  888. if (!p_nested_action) {
  889. updating = false;
  890. }
  891. selected_transition_from = connecting_from;
  892. selected_transition_to = connecting_to_node;
  893. selected_transition_index = transition_lines.size();
  894. EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
  895. _update_mode();
  896. }
  897. connecting = false;
  898. }
  899. void AnimationNodeStateMachineEditor::_connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, bool p_auto_advance, bool p_multi_transitions) {
  900. Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
  901. Color icon_color(1, 1, 1);
  902. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  903. if (!p_enabled) {
  904. linecolor.a *= 0.2;
  905. icon_color.a *= 0.2;
  906. accent.a *= 0.6;
  907. }
  908. const Ref<Texture2D> icons[6] = {
  909. get_theme_icon(SNAME("TransitionImmediateBig"), SNAME("EditorIcons")),
  910. get_theme_icon(SNAME("TransitionSyncBig"), SNAME("EditorIcons")),
  911. get_theme_icon(SNAME("TransitionEndBig"), SNAME("EditorIcons")),
  912. get_theme_icon(SNAME("TransitionImmediateAutoBig"), SNAME("EditorIcons")),
  913. get_theme_icon(SNAME("TransitionSyncAutoBig"), SNAME("EditorIcons")),
  914. get_theme_icon(SNAME("TransitionEndAutoBig"), SNAME("EditorIcons"))
  915. };
  916. if (p_selected) {
  917. state_machine_draw->draw_line(p_from, p_to, accent, 6);
  918. }
  919. if (p_travel) {
  920. linecolor = accent;
  921. linecolor.set_hsv(1.0, linecolor.get_s(), linecolor.get_v());
  922. }
  923. state_machine_draw->draw_line(p_from, p_to, linecolor, 2);
  924. Ref<Texture2D> icon = icons[p_mode + (p_auto_advance ? 3 : 0)];
  925. Transform2D xf;
  926. xf.columns[0] = (p_to - p_from).normalized();
  927. xf.columns[1] = xf.columns[0].orthogonal();
  928. xf.columns[2] = (p_from + p_to) * 0.5 - xf.columns[1] * icon->get_height() * 0.5 - xf.columns[0] * icon->get_height() * 0.5;
  929. state_machine_draw->draw_set_transform_matrix(xf);
  930. if (p_multi_transitions) {
  931. state_machine_draw->draw_texture(icons[0], Vector2(-icon->get_width(), 0), icon_color);
  932. state_machine_draw->draw_texture(icons[0], Vector2(), icon_color);
  933. state_machine_draw->draw_texture(icons[0], Vector2(icon->get_width(), 0), icon_color);
  934. } else {
  935. state_machine_draw->draw_texture(icon, Vector2(), icon_color);
  936. }
  937. state_machine_draw->draw_set_transform_matrix(Transform2D());
  938. }
  939. void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect) {
  940. if (p_to == r_from) {
  941. return;
  942. }
  943. //this could be optimized...
  944. Vector2 n = (p_to - r_from).normalized();
  945. while (p_rect.has_point(r_from)) {
  946. r_from += n;
  947. }
  948. }
  949. void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect) {
  950. if (r_to == p_from) {
  951. return;
  952. }
  953. //this could be optimized...
  954. Vector2 n = (r_to - p_from).normalized();
  955. while (p_rect.has_point(r_to)) {
  956. r_to -= n;
  957. }
  958. }
  959. void AnimationNodeStateMachineEditor::_state_machine_draw() {
  960. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  961. Ref<StyleBoxFlat> style = get_theme_stylebox(SNAME("state_machine_frame"), SNAME("GraphNode"));
  962. Ref<StyleBoxFlat> style_selected = get_theme_stylebox(SNAME("state_machine_selected_frame"), SNAME("GraphNode"));
  963. Ref<Font> font = get_theme_font(SNAME("title_font"), SNAME("GraphNode"));
  964. int font_size = get_theme_font_size(SNAME("title_font_size"), SNAME("GraphNode"));
  965. Color font_color = get_theme_color(SNAME("title_color"), SNAME("GraphNode"));
  966. Ref<Texture2D> play = get_theme_icon(SNAME("Play"), SNAME("EditorIcons"));
  967. Ref<Texture2D> edit = get_theme_icon(SNAME("Edit"), SNAME("EditorIcons"));
  968. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  969. Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
  970. linecolor.a *= 0.3;
  971. Ref<StyleBox> playing_overlay = get_theme_stylebox(SNAME("position"), SNAME("GraphNode"));
  972. Ref<StyleBoxFlat> start_overlay = style->duplicate();
  973. start_overlay->set_border_width_all(1 * EDSCALE);
  974. start_overlay->set_border_color(Color::html("#80f6cf"));
  975. Ref<StyleBoxFlat> end_overlay = style->duplicate();
  976. end_overlay->set_border_width_all(1 * EDSCALE);
  977. end_overlay->set_border_color(Color::html("#f26661"));
  978. bool playing = false;
  979. StringName current;
  980. StringName blend_from;
  981. Vector<StringName> travel_path;
  982. if (playback.is_valid()) {
  983. playing = playback->is_playing();
  984. current = playback->get_current_node();
  985. blend_from = playback->get_blend_from_node();
  986. travel_path = playback->get_travel_path();
  987. }
  988. if (state_machine_draw->has_focus()) {
  989. state_machine_draw->draw_rect(Rect2(Point2(), state_machine_draw->get_size()), accent, false);
  990. }
  991. int sep = 3 * EDSCALE;
  992. List<StringName> nodes;
  993. state_machine->get_node_list(&nodes);
  994. node_rects.clear();
  995. Rect2 scroll_range;
  996. //snap lines
  997. if (dragging_selected) {
  998. Vector2 from = (state_machine->get_node_position(selected_node) * EDSCALE) + drag_ofs - state_machine->get_graph_offset() * EDSCALE;
  999. if (snap_x != StringName()) {
  1000. Vector2 to = (state_machine->get_node_position(snap_x) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  1001. state_machine_draw->draw_line(from, to, linecolor, 2);
  1002. }
  1003. if (snap_y != StringName()) {
  1004. Vector2 to = (state_machine->get_node_position(snap_y) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  1005. state_machine_draw->draw_line(from, to, linecolor, 2);
  1006. }
  1007. }
  1008. //pre pass nodes so we know the rectangles
  1009. for (const StringName &E : nodes) {
  1010. Ref<AnimationNode> anode = state_machine->get_node(E);
  1011. String name = E;
  1012. bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);
  1013. Ref<StyleBox> sb = selected_nodes.has(E) ? style_selected : style;
  1014. Size2 s = sb->get_minimum_size();
  1015. int strsize = font->get_string_size(name, font_size).width;
  1016. s.width += strsize;
  1017. s.height += MAX(font->get_height(font_size), play->get_height());
  1018. s.width += sep + play->get_width();
  1019. if (needs_editor) {
  1020. s.width += sep + edit->get_width();
  1021. }
  1022. Vector2 offset;
  1023. offset += state_machine->get_node_position(E) * EDSCALE;
  1024. if (selected_nodes.has(E) && dragging_selected) {
  1025. offset += drag_ofs;
  1026. }
  1027. offset -= s / 2;
  1028. offset = offset.floor();
  1029. //prepre rect
  1030. NodeRect nr;
  1031. nr.node = Rect2(offset, s);
  1032. nr.node_name = E;
  1033. scroll_range = scroll_range.merge(nr.node); //merge with range
  1034. //now scroll it to draw
  1035. nr.node.position -= state_machine->get_graph_offset() * EDSCALE;
  1036. node_rects.push_back(nr);
  1037. }
  1038. transition_lines.clear();
  1039. //draw connecting line for potential new transition
  1040. if (connecting) {
  1041. Vector2 from = (state_machine->get_node_position(connecting_from) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  1042. Vector2 to;
  1043. if (connecting_to_node != StringName()) {
  1044. to = (state_machine->get_node_position(connecting_to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  1045. } else {
  1046. to = connecting_to;
  1047. }
  1048. for (int i = 0; i < node_rects.size(); i++) {
  1049. if (node_rects[i].node_name == connecting_from) {
  1050. _clip_src_line_to_rect(from, to, node_rects[i].node);
  1051. }
  1052. if (node_rects[i].node_name == connecting_to_node) {
  1053. _clip_dst_line_to_rect(from, to, node_rects[i].node);
  1054. }
  1055. }
  1056. _connection_draw(from, to, AnimationNodeStateMachineTransition::SwitchMode(transition_mode->get_selected()), true, false, false, false, false);
  1057. }
  1058. Ref<Texture2D> tr_reference_icon = get_theme_icon(SNAME("TransitionImmediateBig"), SNAME("EditorIcons"));
  1059. float tr_bidi_offset = int(tr_reference_icon->get_height() * 0.8);
  1060. //draw transition lines
  1061. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  1062. TransitionLine tl;
  1063. tl.transition_index = i;
  1064. tl.from_node = state_machine->get_transition_from(i);
  1065. StringName local_from = String(tl.from_node).get_slicec('/', 0);
  1066. local_from = local_from == ".." ? state_machine->start_node : local_from;
  1067. Vector2 ofs_from = (dragging_selected && selected_nodes.has(local_from)) ? drag_ofs : Vector2();
  1068. tl.from = (state_machine->get_node_position(local_from) * EDSCALE) + ofs_from - state_machine->get_graph_offset() * EDSCALE;
  1069. tl.to_node = state_machine->get_transition_to(i);
  1070. StringName local_to = String(tl.to_node).get_slicec('/', 0);
  1071. local_to = local_to == ".." ? state_machine->end_node : local_to;
  1072. Vector2 ofs_to = (dragging_selected && selected_nodes.has(local_to)) ? drag_ofs : Vector2();
  1073. tl.to = (state_machine->get_node_position(local_to) * EDSCALE) + ofs_to - state_machine->get_graph_offset() * EDSCALE;
  1074. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);
  1075. tl.disabled = tr->is_disabled();
  1076. tl.auto_advance = tr->has_auto_advance();
  1077. tl.advance_condition_name = tr->get_advance_condition_name();
  1078. tl.advance_condition_state = false;
  1079. tl.mode = tr->get_switch_mode();
  1080. tl.width = tr_bidi_offset;
  1081. tl.travel = false;
  1082. tl.hidden = false;
  1083. if (state_machine->has_local_transition(local_to, local_from)) { //offset if same exists
  1084. Vector2 offset = -(tl.from - tl.to).normalized().orthogonal() * tr_bidi_offset;
  1085. tl.from += offset;
  1086. tl.to += offset;
  1087. }
  1088. for (int j = 0; j < node_rects.size(); j++) {
  1089. if (node_rects[j].node_name == local_from) {
  1090. _clip_src_line_to_rect(tl.from, tl.to, node_rects[j].node);
  1091. }
  1092. if (node_rects[j].node_name == local_to) {
  1093. _clip_dst_line_to_rect(tl.from, tl.to, node_rects[j].node);
  1094. }
  1095. }
  1096. tl.selected = selected_transition_from == tl.from_node && selected_transition_to == tl.to_node;
  1097. if (blend_from == local_from && current == local_to) {
  1098. tl.travel = true;
  1099. }
  1100. if (travel_path.size()) {
  1101. if (current == local_from && travel_path[0] == local_to) {
  1102. tl.travel = true;
  1103. } else {
  1104. for (int j = 0; j < travel_path.size() - 1; j++) {
  1105. if (travel_path[j] == local_from && travel_path[j + 1] == local_to) {
  1106. tl.travel = true;
  1107. break;
  1108. }
  1109. }
  1110. }
  1111. }
  1112. StringName fullpath = AnimationTreeEditor::get_singleton()->get_base_path() + String(tl.advance_condition_name);
  1113. if (tl.advance_condition_name != StringName() && bool(AnimationTreeEditor::get_singleton()->get_tree()->get(fullpath))) {
  1114. tl.advance_condition_state = true;
  1115. tl.auto_advance = true;
  1116. }
  1117. // check if already have this local transition
  1118. for (int j = 0; j < transition_lines.size(); j++) {
  1119. StringName from = String(transition_lines[j].from_node).get_slicec('/', 0);
  1120. StringName to = String(transition_lines[j].to_node).get_slicec('/', 0);
  1121. from = from == ".." ? state_machine->start_node : from;
  1122. to = to == ".." ? state_machine->end_node : to;
  1123. if (from == local_from && to == local_to) {
  1124. tl.hidden = true;
  1125. transition_lines.write[j].disabled = transition_lines[j].disabled && tl.disabled;
  1126. transition_lines.write[j].multi_transitions.push_back(tl);
  1127. }
  1128. }
  1129. transition_lines.push_back(tl);
  1130. }
  1131. for (int i = 0; i < transition_lines.size(); i++) {
  1132. TransitionLine tl = transition_lines[i];
  1133. if (!tl.hidden) {
  1134. _connection_draw(tl.from, tl.to, tl.mode, !tl.disabled, tl.selected, tl.travel, tl.auto_advance, !tl.multi_transitions.is_empty());
  1135. }
  1136. }
  1137. //draw actual nodes
  1138. for (int i = 0; i < node_rects.size(); i++) {
  1139. String name = node_rects[i].node_name;
  1140. Ref<AnimationNode> anode = state_machine->get_node(name);
  1141. bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);
  1142. Ref<StyleBox> sb = selected_nodes.has(name) ? style_selected : style;
  1143. int strsize = font->get_string_size(name, font_size).width;
  1144. NodeRect &nr = node_rects.write[i];
  1145. Vector2 offset = nr.node.position;
  1146. int h = nr.node.size.height;
  1147. //prepre rect
  1148. //now scroll it to draw
  1149. state_machine_draw->draw_style_box(sb, nr.node);
  1150. if (state_machine->start_node == name) {
  1151. state_machine_draw->draw_style_box(sb == style_selected ? style_selected : start_overlay, nr.node);
  1152. }
  1153. if (state_machine->end_node == name) {
  1154. state_machine_draw->draw_style_box(sb == style_selected ? style_selected : end_overlay, nr.node);
  1155. }
  1156. if (playing && (blend_from == name || current == name || travel_path.has(name))) {
  1157. state_machine_draw->draw_style_box(playing_overlay, nr.node);
  1158. }
  1159. offset.x += sb->get_offset().x;
  1160. nr.play.position = offset + Vector2(0, (h - play->get_height()) / 2).floor();
  1161. nr.play.size = play->get_size();
  1162. Ref<Texture2D> play_tex = play;
  1163. if (over_node == name && over_node_what == 0) {
  1164. state_machine_draw->draw_texture(play_tex, nr.play.position, accent);
  1165. } else {
  1166. state_machine_draw->draw_texture(play_tex, nr.play.position);
  1167. }
  1168. offset.x += sep + play->get_width();
  1169. nr.name.position = offset + Vector2(0, (h - font->get_height(font_size)) / 2).floor();
  1170. nr.name.size = Vector2(strsize, font->get_height(font_size));
  1171. state_machine_draw->draw_string(font, nr.name.position + Vector2(0, font->get_ascent(font_size)), name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
  1172. offset.x += strsize + sep;
  1173. if (needs_editor) {
  1174. nr.edit.position = offset + Vector2(0, (h - edit->get_height()) / 2).floor();
  1175. nr.edit.size = edit->get_size();
  1176. if (over_node == name && over_node_what == 1) {
  1177. state_machine_draw->draw_texture(edit, nr.edit.position, accent);
  1178. } else {
  1179. state_machine_draw->draw_texture(edit, nr.edit.position);
  1180. }
  1181. }
  1182. }
  1183. //draw box select
  1184. if (box_selecting) {
  1185. state_machine_draw->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3));
  1186. }
  1187. scroll_range.position -= state_machine_draw->get_size();
  1188. scroll_range.size += state_machine_draw->get_size() * 2.0;
  1189. //adjust scrollbars
  1190. updating = true;
  1191. h_scroll->set_min(scroll_range.position.x);
  1192. h_scroll->set_max(scroll_range.position.x + scroll_range.size.x);
  1193. h_scroll->set_page(state_machine_draw->get_size().x);
  1194. h_scroll->set_value(state_machine->get_graph_offset().x);
  1195. v_scroll->set_min(scroll_range.position.y);
  1196. v_scroll->set_max(scroll_range.position.y + scroll_range.size.y);
  1197. v_scroll->set_page(state_machine_draw->get_size().y);
  1198. v_scroll->set_value(state_machine->get_graph_offset().y);
  1199. updating = false;
  1200. state_machine_play_pos->update();
  1201. }
  1202. void AnimationNodeStateMachineEditor::_state_machine_pos_draw() {
  1203. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  1204. if (!playback.is_valid() || !playback->is_playing()) {
  1205. return;
  1206. }
  1207. if (playback->get_current_node() == state_machine->start_node || playback->get_current_node() == state_machine->end_node) {
  1208. return;
  1209. }
  1210. int idx = -1;
  1211. for (int i = 0; i < node_rects.size(); i++) {
  1212. if (node_rects[i].node_name == playback->get_current_node()) {
  1213. idx = i;
  1214. break;
  1215. }
  1216. }
  1217. if (idx == -1) {
  1218. return;
  1219. }
  1220. const NodeRect &nr = node_rects[idx];
  1221. Vector2 from;
  1222. from.x = nr.play.position.x;
  1223. from.y = (nr.play.position.y + nr.play.size.y + nr.node.position.y + nr.node.size.y) * 0.5;
  1224. Vector2 to;
  1225. if (nr.edit.size.x) {
  1226. to.x = nr.edit.position.x + nr.edit.size.x;
  1227. } else {
  1228. to.x = nr.name.position.x + nr.name.size.x;
  1229. }
  1230. to.y = from.y;
  1231. float len = MAX(0.0001, current_length);
  1232. float pos = CLAMP(play_pos, 0, len);
  1233. float c = pos / len;
  1234. Color fg = get_theme_color(SNAME("font_color"), SNAME("Label"));
  1235. Color bg = fg;
  1236. bg.a *= 0.3;
  1237. state_machine_play_pos->draw_line(from, to, bg, 2);
  1238. to = from.lerp(to, c);
  1239. state_machine_play_pos->draw_line(from, to, fg, 2);
  1240. }
  1241. void AnimationNodeStateMachineEditor::_update_graph() {
  1242. if (updating) {
  1243. return;
  1244. }
  1245. updating = true;
  1246. state_machine_draw->update();
  1247. updating = false;
  1248. }
  1249. void AnimationNodeStateMachineEditor::_notification(int p_what) {
  1250. switch (p_what) {
  1251. case NOTIFICATION_ENTER_TREE:
  1252. case NOTIFICATION_THEME_CHANGED:
  1253. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  1254. case NOTIFICATION_TRANSLATION_CHANGED: {
  1255. error_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  1256. error_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  1257. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  1258. tool_select->set_icon(get_theme_icon(SNAME("ToolSelect"), SNAME("EditorIcons")));
  1259. tool_create->set_icon(get_theme_icon(SNAME("ToolAddNode"), SNAME("EditorIcons")));
  1260. tool_connect->set_icon(get_theme_icon(SNAME("ToolConnect"), SNAME("EditorIcons")));
  1261. transition_mode->clear();
  1262. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionImmediate"), SNAME("EditorIcons")), TTR("Immediate"));
  1263. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionSync"), SNAME("EditorIcons")), TTR("Sync"));
  1264. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionEnd"), SNAME("EditorIcons")), TTR("At End"));
  1265. tool_erase->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  1266. tool_group->set_icon(get_theme_icon(SNAME("Group"), SNAME("EditorIcons")));
  1267. tool_ungroup->set_icon(get_theme_icon(SNAME("Ungroup"), SNAME("EditorIcons")));
  1268. play_mode->clear();
  1269. play_mode->add_icon_item(get_theme_icon(SNAME("PlayTravel"), SNAME("EditorIcons")), TTR("Travel"));
  1270. play_mode->add_icon_item(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")), TTR("Immediate"));
  1271. } break;
  1272. case NOTIFICATION_PROCESS: {
  1273. String error;
  1274. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  1275. if (error_time > 0) {
  1276. error = error_text;
  1277. error_time -= get_process_delta_time();
  1278. } else if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  1279. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  1280. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  1281. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  1282. /*} else if (state_machine->get_parent().is_valid() && state_machine->get_parent()->is_class("AnimationNodeStateMachine")) {
  1283. if (state_machine->get_start_node() == StringName() || state_machine->get_end_node() == StringName()) {
  1284. error = TTR("Start and end nodes are needed for a sub-transition.");
  1285. }*/
  1286. } else if (playback.is_null()) {
  1287. error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  1288. }
  1289. if (error != error_label->get_text()) {
  1290. error_label->set_text(error);
  1291. if (!error.is_empty()) {
  1292. error_panel->show();
  1293. } else {
  1294. error_panel->hide();
  1295. }
  1296. }
  1297. for (int i = 0; i < transition_lines.size(); i++) {
  1298. int tidx = -1;
  1299. for (int j = 0; j < state_machine->get_transition_count(); j++) {
  1300. if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) {
  1301. tidx = j;
  1302. break;
  1303. }
  1304. }
  1305. if (tidx == -1) { //missing transition, should redraw
  1306. state_machine_draw->update();
  1307. break;
  1308. }
  1309. if (transition_lines[i].disabled != state_machine->get_transition(tidx)->is_disabled()) {
  1310. state_machine_draw->update();
  1311. break;
  1312. }
  1313. if (transition_lines[i].auto_advance != state_machine->get_transition(tidx)->has_auto_advance()) {
  1314. state_machine_draw->update();
  1315. break;
  1316. }
  1317. if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) {
  1318. state_machine_draw->update();
  1319. break;
  1320. }
  1321. if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) {
  1322. state_machine_draw->update();
  1323. break;
  1324. }
  1325. bool acstate = transition_lines[i].advance_condition_name != StringName() && bool(AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + String(transition_lines[i].advance_condition_name)));
  1326. if (transition_lines[i].advance_condition_state != acstate) {
  1327. state_machine_draw->update();
  1328. break;
  1329. }
  1330. }
  1331. bool same_travel_path = true;
  1332. Vector<StringName> tp;
  1333. bool is_playing = false;
  1334. StringName current_node;
  1335. StringName blend_from_node;
  1336. play_pos = 0;
  1337. current_length = 0;
  1338. if (playback.is_valid()) {
  1339. tp = playback->get_travel_path();
  1340. is_playing = playback->is_playing();
  1341. current_node = playback->get_current_node();
  1342. blend_from_node = playback->get_blend_from_node();
  1343. play_pos = playback->get_current_play_pos();
  1344. current_length = playback->get_current_length();
  1345. }
  1346. {
  1347. if (last_travel_path.size() != tp.size()) {
  1348. same_travel_path = false;
  1349. } else {
  1350. for (int i = 0; i < last_travel_path.size(); i++) {
  1351. if (last_travel_path[i] != tp[i]) {
  1352. same_travel_path = false;
  1353. break;
  1354. }
  1355. }
  1356. }
  1357. }
  1358. //update if travel state changed
  1359. if (!same_travel_path || last_active != is_playing || last_current_node != current_node || last_blend_from_node != blend_from_node) {
  1360. state_machine_draw->update();
  1361. last_travel_path = tp;
  1362. last_current_node = current_node;
  1363. last_active = is_playing;
  1364. last_blend_from_node = blend_from_node;
  1365. state_machine_play_pos->update();
  1366. }
  1367. {
  1368. if (current_node != StringName() && state_machine->has_node(current_node)) {
  1369. String next = current_node;
  1370. Ref<AnimationNodeStateMachine> anodesm = state_machine->get_node(next);
  1371. Ref<AnimationNodeStateMachinePlayback> current_node_playback;
  1372. while (anodesm.is_valid()) {
  1373. current_node_playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + next + "/playback");
  1374. next += "/" + current_node_playback->get_current_node();
  1375. anodesm = anodesm->get_node(current_node_playback->get_current_node());
  1376. }
  1377. // when current_node is a state machine, use playback of current_node to set play_pos
  1378. if (current_node_playback.is_valid()) {
  1379. play_pos = current_node_playback->get_current_play_pos();
  1380. current_length = current_node_playback->get_current_length();
  1381. }
  1382. }
  1383. }
  1384. if (last_play_pos != play_pos) {
  1385. last_play_pos = play_pos;
  1386. state_machine_play_pos->update();
  1387. }
  1388. } break;
  1389. case NOTIFICATION_VISIBILITY_CHANGED: {
  1390. over_node = StringName();
  1391. set_process(is_visible_in_tree());
  1392. } break;
  1393. }
  1394. }
  1395. void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) {
  1396. AnimationTreeEditor::get_singleton()->enter_editor(p_name);
  1397. }
  1398. void AnimationNodeStateMachineEditor::_removed_from_graph() {
  1399. EditorNode::get_singleton()->edit_item(nullptr);
  1400. }
  1401. void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) {
  1402. const String &new_name = p_text;
  1403. ERR_FAIL_COND(new_name.is_empty() || new_name.contains(".") || new_name.contains("/"));
  1404. if (new_name == prev_name) {
  1405. return; // Nothing to do.
  1406. }
  1407. const String &base_name = new_name;
  1408. int base = 1;
  1409. String name = base_name;
  1410. while (state_machine->has_node(name)) {
  1411. base++;
  1412. name = base_name + " " + itos(base);
  1413. }
  1414. updating = true;
  1415. undo_redo->create_action(TTR("Node Renamed"));
  1416. undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name);
  1417. undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name);
  1418. undo_redo->add_do_method(this, "_update_graph");
  1419. undo_redo->add_undo_method(this, "_update_graph");
  1420. undo_redo->commit_action();
  1421. name_edit_popup->hide();
  1422. updating = false;
  1423. state_machine_draw->update();
  1424. }
  1425. void AnimationNodeStateMachineEditor::_name_edited_focus_out() {
  1426. if (updating) {
  1427. return;
  1428. }
  1429. _name_edited(name_edit->get_text());
  1430. }
  1431. void AnimationNodeStateMachineEditor::_scroll_changed(double) {
  1432. if (updating) {
  1433. return;
  1434. }
  1435. state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value()));
  1436. state_machine_draw->update();
  1437. }
  1438. void AnimationNodeStateMachineEditor::_erase_selected(const bool p_nested_action) {
  1439. if (!selected_nodes.is_empty()) {
  1440. if (!p_nested_action) {
  1441. updating = true;
  1442. }
  1443. undo_redo->create_action(TTR("Node Removed"));
  1444. for (int i = 0; i < node_rects.size(); i++) {
  1445. if (node_rects[i].node_name == state_machine->start_node || node_rects[i].node_name == state_machine->end_node) {
  1446. continue;
  1447. }
  1448. if (!selected_nodes.has(node_rects[i].node_name)) {
  1449. continue;
  1450. }
  1451. undo_redo->add_do_method(state_machine.ptr(), "remove_node", node_rects[i].node_name);
  1452. undo_redo->add_undo_method(state_machine.ptr(), "add_node", node_rects[i].node_name,
  1453. state_machine->get_node(node_rects[i].node_name),
  1454. state_machine->get_node_position(node_rects[i].node_name));
  1455. for (int j = 0; j < state_machine->get_transition_count(); j++) {
  1456. String from = state_machine->get_transition_from(j);
  1457. String to = state_machine->get_transition_to(j);
  1458. String local_from = from.get_slicec('/', 0);
  1459. String local_to = to.get_slicec('/', 0);
  1460. if (local_from == node_rects[i].node_name || local_to == node_rects[i].node_name) {
  1461. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(j));
  1462. }
  1463. }
  1464. }
  1465. undo_redo->add_do_method(this, "_update_graph");
  1466. undo_redo->add_undo_method(this, "_update_graph");
  1467. undo_redo->commit_action();
  1468. if (!p_nested_action) {
  1469. updating = false;
  1470. }
  1471. selected_nodes.clear();
  1472. }
  1473. if (!selected_multi_transition.multi_transitions.is_empty()) {
  1474. delete_tree->clear();
  1475. TreeItem *root = delete_tree->create_item();
  1476. TreeItem *item = delete_tree->create_item(root);
  1477. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1478. item->set_text(0, String(selected_transition_from) + " -> " + selected_transition_to);
  1479. item->set_editable(0, true);
  1480. for (int i = 0; i < selected_multi_transition.multi_transitions.size(); i++) {
  1481. String from = selected_multi_transition.multi_transitions[i].from_node;
  1482. String to = selected_multi_transition.multi_transitions[i].to_node;
  1483. item = delete_tree->create_item(root);
  1484. item->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1485. item->set_text(0, from + " -> " + to);
  1486. item->set_editable(0, true);
  1487. }
  1488. delete_window->popup_centered(Vector2(400, 200));
  1489. return;
  1490. }
  1491. if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {
  1492. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to));
  1493. if (!p_nested_action) {
  1494. updating = true;
  1495. }
  1496. undo_redo->create_action(TTR("Transition Removed"));
  1497. undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to);
  1498. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr);
  1499. undo_redo->add_do_method(this, "_update_graph");
  1500. undo_redo->add_undo_method(this, "_update_graph");
  1501. undo_redo->commit_action();
  1502. if (!p_nested_action) {
  1503. updating = false;
  1504. }
  1505. selected_transition_from = StringName();
  1506. selected_transition_to = StringName();
  1507. selected_transition_index = -1;
  1508. selected_multi_transition = TransitionLine();
  1509. }
  1510. state_machine_draw->update();
  1511. }
  1512. void AnimationNodeStateMachineEditor::_update_mode() {
  1513. if (tool_select->is_pressed()) {
  1514. tool_erase_hb->show();
  1515. bool nothing_selected = selected_nodes.is_empty() && selected_transition_from == StringName() && selected_transition_to == StringName();
  1516. bool start_end_selected = selected_nodes.size() == 1 && (*selected_nodes.begin() == state_machine->start_node || *selected_nodes.begin() == state_machine->end_node);
  1517. tool_erase->set_disabled(nothing_selected || start_end_selected);
  1518. if (selected_nodes.is_empty() || start_end_selected) {
  1519. tool_group->set_disabled(true);
  1520. tool_group->set_visible(true);
  1521. tool_ungroup->set_visible(false);
  1522. } else {
  1523. Ref<AnimationNodeStateMachine> ansm = state_machine->get_node(*selected_nodes.begin());
  1524. if (selected_nodes.size() == 1 && ansm.is_valid()) {
  1525. tool_group->set_disabled(true);
  1526. tool_group->set_visible(false);
  1527. tool_ungroup->set_visible(true);
  1528. } else {
  1529. tool_group->set_disabled(false);
  1530. tool_group->set_visible(true);
  1531. tool_ungroup->set_visible(false);
  1532. }
  1533. }
  1534. } else {
  1535. tool_erase_hb->hide();
  1536. }
  1537. }
  1538. void AnimationNodeStateMachineEditor::_bind_methods() {
  1539. ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph);
  1540. ClassDB::bind_method("_removed_from_graph", &AnimationNodeStateMachineEditor::_removed_from_graph);
  1541. ClassDB::bind_method("_open_editor", &AnimationNodeStateMachineEditor::_open_editor);
  1542. ClassDB::bind_method("_connect_to", &AnimationNodeStateMachineEditor::_connect_to);
  1543. ClassDB::bind_method("_stop_connecting", &AnimationNodeStateMachineEditor::_stop_connecting);
  1544. ClassDB::bind_method("_delete_selected", &AnimationNodeStateMachineEditor::_delete_selected);
  1545. ClassDB::bind_method("_delete_all", &AnimationNodeStateMachineEditor::_delete_all);
  1546. ClassDB::bind_method("_delete_tree_draw", &AnimationNodeStateMachineEditor::_delete_tree_draw);
  1547. }
  1548. AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = nullptr;
  1549. AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {
  1550. singleton = this;
  1551. HBoxContainer *top_hb = memnew(HBoxContainer);
  1552. add_child(top_hb);
  1553. Ref<ButtonGroup> bg;
  1554. bg.instantiate();
  1555. tool_select = memnew(Button);
  1556. tool_select->set_flat(true);
  1557. top_hb->add_child(tool_select);
  1558. tool_select->set_toggle_mode(true);
  1559. tool_select->set_button_group(bg);
  1560. tool_select->set_pressed(true);
  1561. tool_select->set_tooltip(TTR("Select and move nodes.\nRMB: Add node at position clicked.\nShift+LMB+Drag: Connects the selected node with another node or creates a new node if you select an area without nodes."));
  1562. tool_select->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1563. tool_create = memnew(Button);
  1564. tool_create->set_flat(true);
  1565. top_hb->add_child(tool_create);
  1566. tool_create->set_toggle_mode(true);
  1567. tool_create->set_button_group(bg);
  1568. tool_create->set_tooltip(TTR("Create new nodes."));
  1569. tool_create->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1570. tool_connect = memnew(Button);
  1571. tool_connect->set_flat(true);
  1572. top_hb->add_child(tool_connect);
  1573. tool_connect->set_toggle_mode(true);
  1574. tool_connect->set_button_group(bg);
  1575. tool_connect->set_tooltip(TTR("Connect nodes."));
  1576. tool_connect->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1577. tool_erase_hb = memnew(HBoxContainer);
  1578. top_hb->add_child(tool_erase_hb);
  1579. tool_erase_hb->add_child(memnew(VSeparator));
  1580. tool_group = memnew(Button);
  1581. tool_group->set_flat(true);
  1582. tool_group->set_tooltip(TTR("Group Selected Node(s)") + " (Ctrl+G)");
  1583. tool_group->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_group_selected_nodes));
  1584. tool_group->set_disabled(true);
  1585. tool_erase_hb->add_child(tool_group);
  1586. tool_ungroup = memnew(Button);
  1587. tool_ungroup->set_flat(true);
  1588. tool_ungroup->set_tooltip(TTR("Ungroup Selected Node") + " (Ctrl+Shift+G)");
  1589. tool_ungroup->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_ungroup_selected_nodes));
  1590. tool_ungroup->set_visible(false);
  1591. tool_erase_hb->add_child(tool_ungroup);
  1592. tool_erase = memnew(Button);
  1593. tool_erase->set_flat(true);
  1594. tool_erase->set_tooltip(TTR("Remove selected node or transition."));
  1595. tool_erase->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_erase_selected), varray(false));
  1596. tool_erase->set_disabled(true);
  1597. tool_erase_hb->add_child(tool_erase);
  1598. top_hb->add_child(memnew(VSeparator));
  1599. top_hb->add_child(memnew(Label(TTR("Transition:"))));
  1600. transition_mode = memnew(OptionButton);
  1601. top_hb->add_child(transition_mode);
  1602. top_hb->add_spacer();
  1603. top_hb->add_child(memnew(Label(TTR("Play Mode:"))));
  1604. play_mode = memnew(OptionButton);
  1605. top_hb->add_child(play_mode);
  1606. panel = memnew(PanelContainer);
  1607. panel->set_clip_contents(true);
  1608. panel->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1609. add_child(panel);
  1610. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1611. state_machine_draw = memnew(Control);
  1612. panel->add_child(state_machine_draw);
  1613. state_machine_draw->connect("gui_input", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_gui_input));
  1614. state_machine_draw->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_draw));
  1615. state_machine_draw->set_focus_mode(FOCUS_ALL);
  1616. state_machine_draw->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1617. state_machine_play_pos = memnew(Control);
  1618. state_machine_draw->add_child(state_machine_play_pos);
  1619. state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent
  1620. state_machine_play_pos->set_anchors_and_offsets_preset(PRESET_WIDE);
  1621. state_machine_play_pos->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_pos_draw));
  1622. v_scroll = memnew(VScrollBar);
  1623. state_machine_draw->add_child(v_scroll);
  1624. v_scroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);
  1625. v_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
  1626. h_scroll = memnew(HScrollBar);
  1627. state_machine_draw->add_child(h_scroll);
  1628. h_scroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);
  1629. h_scroll->set_offset(SIDE_RIGHT, -v_scroll->get_size().x * EDSCALE);
  1630. h_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
  1631. error_panel = memnew(PanelContainer);
  1632. add_child(error_panel);
  1633. error_label = memnew(Label);
  1634. error_panel->add_child(error_label);
  1635. error_panel->hide();
  1636. undo_redo = EditorNode::get_undo_redo();
  1637. set_custom_minimum_size(Size2(0, 300 * EDSCALE));
  1638. menu = memnew(PopupMenu);
  1639. add_child(menu);
  1640. menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_menu_type));
  1641. menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));
  1642. animations_menu = memnew(PopupMenu);
  1643. menu->add_child(animations_menu);
  1644. animations_menu->set_name("animations");
  1645. animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type));
  1646. connect_menu = memnew(PopupMenu);
  1647. add_child(connect_menu);
  1648. connect_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
  1649. connect_menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));
  1650. state_machine_menu = memnew(PopupMenu);
  1651. state_machine_menu->set_name("state_machines");
  1652. state_machine_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
  1653. connect_menu->add_child(state_machine_menu);
  1654. end_menu = memnew(PopupMenu);
  1655. end_menu->set_name("end_nodes");
  1656. end_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
  1657. connect_menu->add_child(end_menu);
  1658. name_edit_popup = memnew(Popup);
  1659. add_child(name_edit_popup);
  1660. name_edit = memnew(LineEdit);
  1661. name_edit_popup->add_child(name_edit);
  1662. name_edit->set_anchors_and_offsets_preset(PRESET_WIDE);
  1663. name_edit->connect("text_submitted", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited));
  1664. name_edit->connect("focus_exited", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited_focus_out));
  1665. open_file = memnew(EditorFileDialog);
  1666. add_child(open_file);
  1667. open_file->set_title(TTR("Open Animation Node"));
  1668. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1669. open_file->connect("file_selected", callable_mp(this, &AnimationNodeStateMachineEditor::_file_opened));
  1670. undo_redo = EditorNode::get_undo_redo();
  1671. delete_window = memnew(ConfirmationDialog);
  1672. delete_window->set_flag(Window::FLAG_RESIZE_DISABLED, true);
  1673. add_child(delete_window);
  1674. delete_tree = memnew(Tree);
  1675. delete_tree->set_hide_root(true);
  1676. delete_tree->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_delete_tree_draw));
  1677. delete_window->add_child(delete_tree);
  1678. Button *ok = delete_window->get_cancel_button();
  1679. ok->set_text(TTR("Delete Selected"));
  1680. ok->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_delete_selected));
  1681. Button *delete_all = delete_window->add_button(TTR("Delete All"), true);
  1682. delete_all->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_delete_all));
  1683. over_node_what = -1;
  1684. dragging_selected_attempt = false;
  1685. connecting = false;
  1686. selected_transition_index = -1;
  1687. last_active = false;
  1688. error_time = 0;
  1689. }
  1690. void EditorAnimationMultiTransitionEdit::add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition) {
  1691. Transition tr;
  1692. tr.from = p_from;
  1693. tr.to = p_to;
  1694. tr.transition = p_transition;
  1695. transitions.push_back(tr);
  1696. }
  1697. bool EditorAnimationMultiTransitionEdit::_set(const StringName &p_name, const Variant &p_property) {
  1698. int index = String(p_name).get_slicec('/', 0).to_int();
  1699. StringName prop = String(p_name).get_slicec('/', 1);
  1700. bool found;
  1701. transitions.write[index].transition->set(prop, p_property, &found);
  1702. if (found) {
  1703. return true;
  1704. }
  1705. return false;
  1706. }
  1707. bool EditorAnimationMultiTransitionEdit::_get(const StringName &p_name, Variant &r_property) const {
  1708. int index = String(p_name).get_slicec('/', 0).to_int();
  1709. StringName prop = String(p_name).get_slicec('/', 1);
  1710. if (prop == "transition_path") {
  1711. r_property = String(transitions[index].from) + " -> " + transitions[index].to;
  1712. return true;
  1713. }
  1714. bool found;
  1715. r_property = transitions[index].transition->get(prop, &found);
  1716. if (found) {
  1717. return true;
  1718. }
  1719. return false;
  1720. }
  1721. void EditorAnimationMultiTransitionEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  1722. for (int i = 0; i < transitions.size(); i++) {
  1723. List<PropertyInfo> plist;
  1724. transitions[i].transition->get_property_list(&plist, true);
  1725. PropertyInfo prop_transition_path;
  1726. prop_transition_path.type = Variant::STRING;
  1727. prop_transition_path.name = itos(i) + "/" + "transition_path";
  1728. p_list->push_back(prop_transition_path);
  1729. for (List<PropertyInfo>::Element *F = plist.front(); F; F = F->next()) {
  1730. if (F->get().name == "script" || F->get().name == "resource_name" || F->get().name == "resource_path" || F->get().name == "resource_local_to_scene") {
  1731. continue;
  1732. }
  1733. if (F->get().usage != PROPERTY_USAGE_DEFAULT) {
  1734. continue;
  1735. }
  1736. PropertyInfo prop = F->get();
  1737. prop.name = itos(i) + "/" + prop.name;
  1738. p_list->push_back(prop);
  1739. }
  1740. }
  1741. }