animation_state_machine_editor.cpp 77 KB

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