animation_state_machine_editor.cpp 83 KB

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