animation_state_machine_editor.cpp 69 KB

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