animation_state_machine_editor.cpp 64 KB

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