2
0

animation_state_machine_editor.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*************************************************************************/
  2. /* animation_state_machine_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_state_machine_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/geometry_2d.h"
  35. #include "core/os/keyboard.h"
  36. #include "editor/editor_file_dialog.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "scene/animation/animation_blend_tree.h"
  40. #include "scene/animation/animation_player.h"
  41. #include "scene/gui/menu_button.h"
  42. #include "scene/gui/panel.h"
  43. #include "scene/main/window.h"
  44. bool AnimationNodeStateMachineEditor::can_edit(const Ref<AnimationNode> &p_node) {
  45. Ref<AnimationNodeStateMachine> ansm = p_node;
  46. return ansm.is_valid();
  47. }
  48. void AnimationNodeStateMachineEditor::edit(const Ref<AnimationNode> &p_node) {
  49. state_machine = p_node;
  50. if (state_machine.is_valid()) {
  51. selected_transition_from = StringName();
  52. selected_transition_to = StringName();
  53. selected_node = StringName();
  54. _update_mode();
  55. _update_graph();
  56. }
  57. }
  58. void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref<InputEvent> &p_event) {
  59. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  60. if (playback.is_null()) {
  61. return;
  62. }
  63. Ref<InputEventKey> k = p_event;
  64. if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
  65. if (selected_node != StringName() || selected_transition_to != StringName() || selected_transition_from != StringName()) {
  66. _erase_selected();
  67. accept_event();
  68. }
  69. }
  70. Ref<InputEventMouseButton> mb = p_event;
  71. //Add new node
  72. if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (tool_create->is_pressed() && mb->get_button_index() == MouseButton::LEFT))) {
  73. menu->clear();
  74. animations_menu->clear();
  75. animations_to_add.clear();
  76. List<StringName> classes;
  77. classes.sort_custom<StringName::AlphCompare>();
  78. ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
  79. menu->add_submenu_item(TTR("Add Animation"), "animations");
  80. AnimationTree *gp = AnimationTreeEditor::get_singleton()->get_tree();
  81. ERR_FAIL_COND(!gp);
  82. if (gp && gp->has_node(gp->get_animation_player())) {
  83. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(gp->get_node(gp->get_animation_player()));
  84. if (ap) {
  85. List<StringName> names;
  86. ap->get_animation_list(&names);
  87. for (const StringName &E : names) {
  88. animations_menu->add_icon_item(get_theme_icon(SNAME("Animation"), SNAME("EditorIcons")), E);
  89. animations_to_add.push_back(E);
  90. }
  91. }
  92. }
  93. for (const StringName &E : classes) {
  94. String name = String(E).replace_first("AnimationNode", "");
  95. if (name == "Animation") {
  96. continue; // nope
  97. }
  98. int idx = menu->get_item_count();
  99. menu->add_item(vformat("Add %s", name), idx);
  100. menu->set_item_metadata(idx, E);
  101. }
  102. Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
  103. if (clipb.is_valid()) {
  104. menu->add_separator();
  105. menu->add_item(TTR("Paste"), MENU_PASTE);
  106. }
  107. menu->add_separator();
  108. menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
  109. menu->set_position(state_machine_draw->get_screen_position() + mb->get_position());
  110. menu->reset_size();
  111. menu->popup();
  112. add_node_pos = mb->get_position() / EDSCALE + state_machine->get_graph_offset();
  113. }
  114. // select node or push a field inside
  115. if (mb.is_valid() && !mb->is_shift_pressed() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  116. selected_transition_from = StringName();
  117. selected_transition_to = StringName();
  118. selected_node = StringName();
  119. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  120. if (node_rects[i].play.has_point(mb->get_position())) { //edit name
  121. if (play_mode->get_selected() == 1 || !playback->is_playing()) {
  122. //start
  123. playback->start(node_rects[i].node_name);
  124. } else {
  125. //travel
  126. playback->travel(node_rects[i].node_name);
  127. }
  128. state_machine_draw->update();
  129. return;
  130. }
  131. if (node_rects[i].name.has_point(mb->get_position())) { //edit name
  132. Ref<StyleBox> line_sb = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"));
  133. Rect2 edit_rect = node_rects[i].name;
  134. edit_rect.position -= line_sb->get_offset();
  135. edit_rect.size += line_sb->get_minimum_size();
  136. name_edit_popup->set_position(state_machine_draw->get_screen_position() + edit_rect.position);
  137. name_edit_popup->set_size(edit_rect.size);
  138. name_edit->set_text(node_rects[i].node_name);
  139. name_edit_popup->popup();
  140. name_edit->grab_focus();
  141. name_edit->select_all();
  142. prev_name = node_rects[i].node_name;
  143. return;
  144. }
  145. if (node_rects[i].edit.has_point(mb->get_position())) { //edit name
  146. call_deferred(SNAME("_open_editor"), node_rects[i].node_name);
  147. return;
  148. }
  149. if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
  150. selected_node = node_rects[i].node_name;
  151. Ref<AnimationNode> anode = state_machine->get_node(selected_node);
  152. EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
  153. state_machine_draw->update();
  154. dragging_selected_attempt = true;
  155. dragging_selected = false;
  156. drag_from = mb->get_position();
  157. snap_x = StringName();
  158. snap_y = StringName();
  159. _update_mode();
  160. return;
  161. }
  162. }
  163. //test the lines now
  164. int closest = -1;
  165. float closest_d = 1e20;
  166. for (int i = 0; i < transition_lines.size(); i++) {
  167. Vector2 s[2] = {
  168. transition_lines[i].from,
  169. transition_lines[i].to
  170. };
  171. Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mb->get_position(), s);
  172. float d = cpoint.distance_to(mb->get_position());
  173. if (d > transition_lines[i].width) {
  174. continue;
  175. }
  176. if (d < closest_d) {
  177. closest = i;
  178. closest_d = d;
  179. }
  180. }
  181. if (closest >= 0) {
  182. selected_transition_from = transition_lines[closest].from_node;
  183. selected_transition_to = transition_lines[closest].to_node;
  184. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(closest);
  185. EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
  186. }
  187. state_machine_draw->update();
  188. _update_mode();
  189. }
  190. //end moving node
  191. if (mb.is_valid() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  192. if (dragging_selected) {
  193. Ref<AnimationNode> an = state_machine->get_node(selected_node);
  194. updating = true;
  195. undo_redo->create_action(TTR("Move Node"));
  196. undo_redo->add_do_method(state_machine.ptr(), "set_node_position", selected_node, state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE);
  197. undo_redo->add_undo_method(state_machine.ptr(), "set_node_position", selected_node, state_machine->get_node_position(selected_node));
  198. undo_redo->add_do_method(this, "_update_graph");
  199. undo_redo->add_undo_method(this, "_update_graph");
  200. undo_redo->commit_action();
  201. updating = false;
  202. }
  203. snap_x = StringName();
  204. snap_y = StringName();
  205. dragging_selected_attempt = false;
  206. dragging_selected = false;
  207. state_machine_draw->update();
  208. }
  209. //connect nodes
  210. 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()) {
  211. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  212. if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
  213. connecting = true;
  214. connecting_from = node_rects[i].node_name;
  215. connecting_to = mb->get_position();
  216. connecting_to_node = StringName();
  217. return;
  218. }
  219. }
  220. }
  221. //end connecting nodes
  222. if (mb.is_valid() && connecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  223. if (connecting_to_node != StringName()) {
  224. if (state_machine->has_transition(connecting_from, connecting_to_node)) {
  225. EditorNode::get_singleton()->show_warning(TTR("Transition exists!"));
  226. } else {
  227. Ref<AnimationNodeStateMachineTransition> tr;
  228. tr.instantiate();
  229. tr->set_switch_mode(AnimationNodeStateMachineTransition::SwitchMode(transition_mode->get_selected()));
  230. updating = true;
  231. undo_redo->create_action(TTR("Add Transition"));
  232. undo_redo->add_do_method(state_machine.ptr(), "add_transition", connecting_from, connecting_to_node, tr);
  233. undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", connecting_from, connecting_to_node);
  234. undo_redo->add_do_method(this, "_update_graph");
  235. undo_redo->add_undo_method(this, "_update_graph");
  236. undo_redo->commit_action();
  237. updating = false;
  238. selected_transition_from = connecting_from;
  239. selected_transition_to = connecting_to_node;
  240. EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
  241. _update_mode();
  242. }
  243. }
  244. connecting_to_node = StringName();
  245. connecting = false;
  246. state_machine_draw->update();
  247. }
  248. Ref<InputEventMouseMotion> mm = p_event;
  249. //pan window
  250. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_MIDDLE) != MouseButton::NONE) {
  251. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  252. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  253. }
  254. //move mouse while connecting
  255. if (mm.is_valid() && connecting) {
  256. connecting_to = mm->get_position();
  257. connecting_to_node = StringName();
  258. state_machine_draw->update();
  259. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  260. if (node_rects[i].node_name != connecting_from && node_rects[i].node.has_point(connecting_to)) { //select node since nothing else was selected
  261. connecting_to_node = node_rects[i].node_name;
  262. return;
  263. }
  264. }
  265. }
  266. //move mouse while moving a node
  267. if (mm.is_valid() && dragging_selected_attempt) {
  268. dragging_selected = true;
  269. drag_ofs = mm->get_position() - drag_from;
  270. snap_x = StringName();
  271. snap_y = StringName();
  272. {
  273. //snap
  274. Vector2 cpos = state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE;
  275. List<StringName> nodes;
  276. state_machine->get_node_list(&nodes);
  277. float best_d_x = 1e20;
  278. float best_d_y = 1e20;
  279. for (const StringName &E : nodes) {
  280. if (E == selected_node) {
  281. continue;
  282. }
  283. Vector2 npos = state_machine->get_node_position(E);
  284. float d_x = ABS(npos.x - cpos.x);
  285. if (d_x < MIN(5, best_d_x)) {
  286. drag_ofs.x -= cpos.x - npos.x;
  287. best_d_x = d_x;
  288. snap_x = E;
  289. }
  290. float d_y = ABS(npos.y - cpos.y);
  291. if (d_y < MIN(5, best_d_y)) {
  292. drag_ofs.y -= cpos.y - npos.y;
  293. best_d_y = d_y;
  294. snap_y = E;
  295. }
  296. }
  297. }
  298. state_machine_draw->update();
  299. }
  300. //put ibeam (text cursor) over names to make it clearer that they are editable
  301. if (mm.is_valid()) {
  302. state_machine_draw->grab_focus();
  303. bool over_text_now = false;
  304. String new_over_node = StringName();
  305. int new_over_node_what = -1;
  306. if (tool_select->is_pressed()) {
  307. for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
  308. if (node_rects[i].name.has_point(mm->get_position())) {
  309. over_text_now = true;
  310. break;
  311. }
  312. if (node_rects[i].node.has_point(mm->get_position())) {
  313. new_over_node = node_rects[i].node_name;
  314. if (node_rects[i].play.has_point(mm->get_position())) {
  315. new_over_node_what = 0;
  316. }
  317. if (node_rects[i].edit.has_point(mm->get_position())) {
  318. new_over_node_what = 1;
  319. }
  320. }
  321. }
  322. }
  323. if (new_over_node != over_node || new_over_node_what != over_node_what) {
  324. over_node = new_over_node;
  325. over_node_what = new_over_node_what;
  326. state_machine_draw->update();
  327. }
  328. if (over_text != over_text_now) {
  329. if (over_text_now) {
  330. state_machine_draw->set_default_cursor_shape(CURSOR_IBEAM);
  331. } else {
  332. state_machine_draw->set_default_cursor_shape(CURSOR_ARROW);
  333. }
  334. over_text = over_text_now;
  335. }
  336. }
  337. Ref<InputEventPanGesture> pan_gesture = p_event;
  338. if (pan_gesture.is_valid()) {
  339. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  340. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  341. }
  342. }
  343. void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) {
  344. file_loaded = ResourceLoader::load(p_file);
  345. if (file_loaded.is_valid()) {
  346. _add_menu_type(MENU_LOAD_FILE_CONFIRM);
  347. }
  348. }
  349. void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) {
  350. String base_name;
  351. Ref<AnimationRootNode> node;
  352. if (p_index == MENU_LOAD_FILE) {
  353. open_file->clear_filters();
  354. List<String> filters;
  355. ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
  356. for (const String &E : filters) {
  357. open_file->add_filter("*." + E);
  358. }
  359. open_file->popup_file_dialog();
  360. return;
  361. } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
  362. node = file_loaded;
  363. file_loaded.unref();
  364. } else if (p_index == MENU_PASTE) {
  365. node = EditorSettings::get_singleton()->get_resource_clipboard();
  366. } else {
  367. String type = menu->get_item_metadata(p_index);
  368. Object *obj = ClassDB::instantiate(type);
  369. ERR_FAIL_COND(!obj);
  370. AnimationNode *an = Object::cast_to<AnimationNode>(obj);
  371. ERR_FAIL_COND(!an);
  372. node = Ref<AnimationNode>(an);
  373. base_name = type.replace_first("AnimationNode", "");
  374. }
  375. if (!node.is_valid()) {
  376. EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
  377. return;
  378. }
  379. if (base_name.is_empty()) {
  380. base_name = node->get_class().replace_first("AnimationNode", "");
  381. }
  382. int base = 1;
  383. String name = base_name;
  384. while (state_machine->has_node(name)) {
  385. base++;
  386. name = base_name + " " + itos(base);
  387. }
  388. updating = true;
  389. undo_redo->create_action(TTR("Add Node"));
  390. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos);
  391. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  392. undo_redo->add_do_method(this, "_update_graph");
  393. undo_redo->add_undo_method(this, "_update_graph");
  394. undo_redo->commit_action();
  395. updating = false;
  396. state_machine_draw->update();
  397. }
  398. void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) {
  399. Ref<AnimationNodeAnimation> anim;
  400. anim.instantiate();
  401. anim->set_animation(animations_to_add[p_index]);
  402. String base_name = animations_to_add[p_index];
  403. int base = 1;
  404. String name = base_name;
  405. while (state_machine->has_node(name)) {
  406. base++;
  407. name = base_name + " " + itos(base);
  408. }
  409. updating = true;
  410. undo_redo->create_action(TTR("Add Node"));
  411. undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos);
  412. undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
  413. undo_redo->add_do_method(this, "_update_graph");
  414. undo_redo->add_undo_method(this, "_update_graph");
  415. undo_redo->commit_action();
  416. updating = false;
  417. state_machine_draw->update();
  418. }
  419. void AnimationNodeStateMachineEditor::_connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, bool p_auto_advance) {
  420. Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
  421. Color icon_color(1, 1, 1);
  422. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  423. if (!p_enabled) {
  424. linecolor.a *= 0.2;
  425. icon_color.a *= 0.2;
  426. accent.a *= 0.6;
  427. }
  428. Ref<Texture2D> icons[6] = {
  429. get_theme_icon(SNAME("TransitionImmediateBig"), SNAME("EditorIcons")),
  430. get_theme_icon(SNAME("TransitionSyncBig"), SNAME("EditorIcons")),
  431. get_theme_icon(SNAME("TransitionEndBig"), SNAME("EditorIcons")),
  432. get_theme_icon(SNAME("TransitionImmediateAutoBig"), SNAME("EditorIcons")),
  433. get_theme_icon(SNAME("TransitionSyncAutoBig"), SNAME("EditorIcons")),
  434. get_theme_icon(SNAME("TransitionEndAutoBig"), SNAME("EditorIcons"))
  435. };
  436. if (p_selected) {
  437. state_machine_draw->draw_line(p_from, p_to, accent, 6);
  438. }
  439. if (p_travel) {
  440. linecolor = accent;
  441. linecolor.set_hsv(1.0, linecolor.get_s(), linecolor.get_v());
  442. }
  443. state_machine_draw->draw_line(p_from, p_to, linecolor, 2);
  444. Ref<Texture2D> icon = icons[p_mode + (p_auto_advance ? 3 : 0)];
  445. Transform2D xf;
  446. xf.elements[0] = (p_to - p_from).normalized();
  447. xf.elements[1] = xf.elements[0].orthogonal();
  448. xf.elements[2] = (p_from + p_to) * 0.5 - xf.elements[1] * icon->get_height() * 0.5 - xf.elements[0] * icon->get_height() * 0.5;
  449. state_machine_draw->draw_set_transform_matrix(xf);
  450. state_machine_draw->draw_texture(icon, Vector2(), icon_color);
  451. state_machine_draw->draw_set_transform_matrix(Transform2D());
  452. }
  453. void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect) {
  454. if (r_to == r_from) {
  455. return;
  456. }
  457. //this could be optimized...
  458. Vector2 n = (r_to - r_from).normalized();
  459. while (p_rect.has_point(r_from)) {
  460. r_from += n;
  461. }
  462. }
  463. void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect) {
  464. if (r_to == r_from) {
  465. return;
  466. }
  467. //this could be optimized...
  468. Vector2 n = (r_to - r_from).normalized();
  469. while (p_rect.has_point(r_to)) {
  470. r_to -= n;
  471. }
  472. }
  473. void AnimationNodeStateMachineEditor::_state_machine_draw() {
  474. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  475. Ref<StyleBox> style = get_theme_stylebox(SNAME("state_machine_frame"), SNAME("GraphNode"));
  476. Ref<StyleBox> style_selected = get_theme_stylebox(SNAME("state_machine_selectedframe"), SNAME("GraphNode"));
  477. Ref<Font> font = get_theme_font(SNAME("title_font"), SNAME("GraphNode"));
  478. int font_size = get_theme_font_size(SNAME("title_font_size"), SNAME("GraphNode"));
  479. Color font_color = get_theme_color(SNAME("title_color"), SNAME("GraphNode"));
  480. Ref<Texture2D> play = get_theme_icon(SNAME("Play"), SNAME("EditorIcons"));
  481. Ref<Texture2D> auto_play = get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons"));
  482. Ref<Texture2D> edit = get_theme_icon(SNAME("Edit"), SNAME("EditorIcons"));
  483. Color accent = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  484. Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
  485. linecolor.a *= 0.3;
  486. Ref<StyleBox> playing_overlay = get_theme_stylebox(SNAME("position"), SNAME("GraphNode"));
  487. bool playing = false;
  488. StringName current;
  489. StringName blend_from;
  490. Vector<StringName> travel_path;
  491. if (playback.is_valid()) {
  492. playing = playback->is_playing();
  493. current = playback->get_current_node();
  494. blend_from = playback->get_blend_from_node();
  495. travel_path = playback->get_travel_path();
  496. }
  497. if (state_machine_draw->has_focus()) {
  498. state_machine_draw->draw_rect(Rect2(Point2(), state_machine_draw->get_size()), accent, false);
  499. }
  500. int sep = 3 * EDSCALE;
  501. List<StringName> nodes;
  502. state_machine->get_node_list(&nodes);
  503. node_rects.clear();
  504. Rect2 scroll_range;
  505. //snap lines
  506. if (dragging_selected) {
  507. Vector2 from = (state_machine->get_node_position(selected_node) * EDSCALE) + drag_ofs - state_machine->get_graph_offset() * EDSCALE;
  508. if (snap_x != StringName()) {
  509. Vector2 to = (state_machine->get_node_position(snap_x) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  510. state_machine_draw->draw_line(from, to, linecolor, 2);
  511. }
  512. if (snap_y != StringName()) {
  513. Vector2 to = (state_machine->get_node_position(snap_y) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  514. state_machine_draw->draw_line(from, to, linecolor, 2);
  515. }
  516. }
  517. //pre pass nodes so we know the rectangles
  518. for (const StringName &E : nodes) {
  519. Ref<AnimationNode> anode = state_machine->get_node(E);
  520. String name = E;
  521. bool needs_editor = EditorNode::get_singleton()->item_has_editor(anode.ptr());
  522. Ref<StyleBox> sb = E == selected_node ? style_selected : style;
  523. Size2 s = sb->get_minimum_size();
  524. int strsize = font->get_string_size(name, font_size).width;
  525. s.width += strsize;
  526. s.height += MAX(font->get_height(font_size), play->get_height());
  527. s.width += sep + play->get_width();
  528. if (needs_editor) {
  529. s.width += sep + edit->get_width();
  530. }
  531. Vector2 offset;
  532. offset += state_machine->get_node_position(E) * EDSCALE;
  533. if (selected_node == E && dragging_selected) {
  534. offset += drag_ofs;
  535. }
  536. offset -= s / 2;
  537. offset = offset.floor();
  538. //prepre rect
  539. NodeRect nr;
  540. nr.node = Rect2(offset, s);
  541. nr.node_name = E;
  542. scroll_range = scroll_range.merge(nr.node); //merge with range
  543. //now scroll it to draw
  544. nr.node.position -= state_machine->get_graph_offset() * EDSCALE;
  545. node_rects.push_back(nr);
  546. }
  547. transition_lines.clear();
  548. //draw connecting line for potential new transition
  549. if (connecting) {
  550. Vector2 from = (state_machine->get_node_position(connecting_from) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  551. Vector2 to;
  552. if (connecting_to_node != StringName()) {
  553. to = (state_machine->get_node_position(connecting_to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
  554. } else {
  555. to = connecting_to;
  556. }
  557. for (int i = 0; i < node_rects.size(); i++) {
  558. if (node_rects[i].node_name == connecting_from) {
  559. _clip_src_line_to_rect(from, to, node_rects[i].node);
  560. }
  561. if (node_rects[i].node_name == connecting_to_node) {
  562. _clip_dst_line_to_rect(from, to, node_rects[i].node);
  563. }
  564. }
  565. _connection_draw(from, to, AnimationNodeStateMachineTransition::SwitchMode(transition_mode->get_selected()), true, false, false, false);
  566. }
  567. Ref<Texture2D> tr_reference_icon = get_theme_icon(SNAME("TransitionImmediateBig"), SNAME("EditorIcons"));
  568. float tr_bidi_offset = int(tr_reference_icon->get_height() * 0.8);
  569. //draw transition lines
  570. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  571. TransitionLine tl;
  572. tl.from_node = state_machine->get_transition_from(i);
  573. Vector2 ofs_from = (dragging_selected && tl.from_node == selected_node) ? drag_ofs : Vector2();
  574. tl.from = (state_machine->get_node_position(tl.from_node) * EDSCALE) + ofs_from - state_machine->get_graph_offset() * EDSCALE;
  575. tl.to_node = state_machine->get_transition_to(i);
  576. Vector2 ofs_to = (dragging_selected && tl.to_node == selected_node) ? drag_ofs : Vector2();
  577. tl.to = (state_machine->get_node_position(tl.to_node) * EDSCALE) + ofs_to - state_machine->get_graph_offset() * EDSCALE;
  578. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);
  579. tl.disabled = tr->is_disabled();
  580. tl.auto_advance = tr->has_auto_advance();
  581. tl.advance_condition_name = tr->get_advance_condition_name();
  582. tl.advance_condition_state = false;
  583. tl.mode = tr->get_switch_mode();
  584. tl.width = tr_bidi_offset;
  585. if (state_machine->has_transition(tl.to_node, tl.from_node)) { //offset if same exists
  586. Vector2 offset = -(tl.from - tl.to).normalized().orthogonal() * tr_bidi_offset;
  587. tl.from += offset;
  588. tl.to += offset;
  589. }
  590. for (int j = 0; j < node_rects.size(); j++) {
  591. if (node_rects[j].node_name == tl.from_node) {
  592. _clip_src_line_to_rect(tl.from, tl.to, node_rects[j].node);
  593. }
  594. if (node_rects[j].node_name == tl.to_node) {
  595. _clip_dst_line_to_rect(tl.from, tl.to, node_rects[j].node);
  596. }
  597. }
  598. bool selected = selected_transition_from == tl.from_node && selected_transition_to == tl.to_node;
  599. bool travel = false;
  600. if (blend_from == tl.from_node && current == tl.to_node) {
  601. travel = true;
  602. }
  603. if (travel_path.size()) {
  604. if (current == tl.from_node && travel_path[0] == tl.to_node) {
  605. travel = true;
  606. } else {
  607. for (int j = 0; j < travel_path.size() - 1; j++) {
  608. if (travel_path[j] == tl.from_node && travel_path[j + 1] == tl.to_node) {
  609. travel = true;
  610. break;
  611. }
  612. }
  613. }
  614. }
  615. bool auto_advance = tl.auto_advance;
  616. StringName fullpath = AnimationTreeEditor::get_singleton()->get_base_path() + String(tl.advance_condition_name);
  617. if (tl.advance_condition_name != StringName() && bool(AnimationTreeEditor::get_singleton()->get_tree()->get(fullpath))) {
  618. tl.advance_condition_state = true;
  619. auto_advance = true;
  620. }
  621. _connection_draw(tl.from, tl.to, tl.mode, !tl.disabled, selected, travel, auto_advance);
  622. transition_lines.push_back(tl);
  623. }
  624. //draw actual nodes
  625. for (int i = 0; i < node_rects.size(); i++) {
  626. String name = node_rects[i].node_name;
  627. Ref<AnimationNode> anode = state_machine->get_node(name);
  628. bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);
  629. Ref<StyleBox> sb = name == selected_node ? style_selected : style;
  630. int strsize = font->get_string_size(name, font_size).width;
  631. NodeRect &nr = node_rects.write[i];
  632. Vector2 offset = nr.node.position;
  633. int h = nr.node.size.height;
  634. //prepre rect
  635. //now scroll it to draw
  636. state_machine_draw->draw_style_box(sb, nr.node);
  637. if (playing && (blend_from == name || current == name || travel_path.has(name))) {
  638. state_machine_draw->draw_style_box(playing_overlay, nr.node);
  639. }
  640. bool onstart = state_machine->get_start_node() == name;
  641. if (onstart) {
  642. state_machine_draw->draw_string(font, offset + Vector2(0, -font->get_height(font_size) - 3 * EDSCALE + font->get_ascent(font_size)), TTR("Start"), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
  643. }
  644. if (state_machine->get_end_node() == name) {
  645. int endofs = nr.node.size.x - font->get_string_size(TTR("End"), font_size).x;
  646. state_machine_draw->draw_string(font, offset + Vector2(endofs, -font->get_height(font_size) - 3 * EDSCALE + font->get_ascent(font_size)), TTR("End"), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
  647. }
  648. offset.x += sb->get_offset().x;
  649. nr.play.position = offset + Vector2(0, (h - play->get_height()) / 2).floor();
  650. nr.play.size = play->get_size();
  651. Ref<Texture2D> play_tex = onstart ? auto_play : play;
  652. if (over_node == name && over_node_what == 0) {
  653. state_machine_draw->draw_texture(play_tex, nr.play.position, accent);
  654. } else {
  655. state_machine_draw->draw_texture(play_tex, nr.play.position);
  656. }
  657. offset.x += sep + play->get_width();
  658. nr.name.position = offset + Vector2(0, (h - font->get_height(font_size)) / 2).floor();
  659. nr.name.size = Vector2(strsize, font->get_height(font_size));
  660. 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);
  661. offset.x += strsize + sep;
  662. if (needs_editor) {
  663. nr.edit.position = offset + Vector2(0, (h - edit->get_height()) / 2).floor();
  664. nr.edit.size = edit->get_size();
  665. if (over_node == name && over_node_what == 1) {
  666. state_machine_draw->draw_texture(edit, nr.edit.position, accent);
  667. } else {
  668. state_machine_draw->draw_texture(edit, nr.edit.position);
  669. }
  670. offset.x += sep + edit->get_width();
  671. }
  672. }
  673. scroll_range.position -= state_machine_draw->get_size();
  674. scroll_range.size += state_machine_draw->get_size() * 2.0;
  675. //adjust scrollbars
  676. updating = true;
  677. h_scroll->set_min(scroll_range.position.x);
  678. h_scroll->set_max(scroll_range.position.x + scroll_range.size.x);
  679. h_scroll->set_page(state_machine_draw->get_size().x);
  680. h_scroll->set_value(state_machine->get_graph_offset().x);
  681. v_scroll->set_min(scroll_range.position.y);
  682. v_scroll->set_max(scroll_range.position.y + scroll_range.size.y);
  683. v_scroll->set_page(state_machine_draw->get_size().y);
  684. v_scroll->set_value(state_machine->get_graph_offset().y);
  685. updating = false;
  686. state_machine_play_pos->update();
  687. }
  688. void AnimationNodeStateMachineEditor::_state_machine_pos_draw() {
  689. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  690. if (!playback.is_valid() || !playback->is_playing()) {
  691. return;
  692. }
  693. int idx = -1;
  694. for (int i = 0; i < node_rects.size(); i++) {
  695. if (node_rects[i].node_name == playback->get_current_node()) {
  696. idx = i;
  697. break;
  698. }
  699. }
  700. if (idx == -1) {
  701. return;
  702. }
  703. const NodeRect &nr = node_rects[idx];
  704. Vector2 from;
  705. from.x = nr.play.position.x;
  706. from.y = (nr.play.position.y + nr.play.size.y + nr.node.position.y + nr.node.size.y) * 0.5;
  707. Vector2 to;
  708. if (nr.edit.size.x) {
  709. to.x = nr.edit.position.x + nr.edit.size.x;
  710. } else {
  711. to.x = nr.name.position.x + nr.name.size.x;
  712. }
  713. to.y = from.y;
  714. float len = MAX(0.0001, current_length);
  715. float pos = CLAMP(play_pos, 0, len);
  716. float c = pos / len;
  717. Color fg = get_theme_color(SNAME("font_color"), SNAME("Label"));
  718. Color bg = fg;
  719. bg.a *= 0.3;
  720. state_machine_play_pos->draw_line(from, to, bg, 2);
  721. to = from.lerp(to, c);
  722. state_machine_play_pos->draw_line(from, to, fg, 2);
  723. }
  724. void AnimationNodeStateMachineEditor::_update_graph() {
  725. if (updating) {
  726. return;
  727. }
  728. updating = true;
  729. state_machine_draw->update();
  730. updating = false;
  731. }
  732. void AnimationNodeStateMachineEditor::_notification(int p_what) {
  733. switch (p_what) {
  734. case NOTIFICATION_ENTER_TREE:
  735. case NOTIFICATION_THEME_CHANGED:
  736. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  737. case NOTIFICATION_TRANSLATION_CHANGED: {
  738. error_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  739. error_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  740. panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  741. tool_select->set_icon(get_theme_icon(SNAME("ToolSelect"), SNAME("EditorIcons")));
  742. tool_create->set_icon(get_theme_icon(SNAME("ToolAddNode"), SNAME("EditorIcons")));
  743. tool_connect->set_icon(get_theme_icon(SNAME("ToolConnect"), SNAME("EditorIcons")));
  744. transition_mode->clear();
  745. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionImmediate"), SNAME("EditorIcons")), TTR("Immediate"));
  746. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionSync"), SNAME("EditorIcons")), TTR("Sync"));
  747. transition_mode->add_icon_item(get_theme_icon(SNAME("TransitionEnd"), SNAME("EditorIcons")), TTR("At End"));
  748. tool_erase->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  749. tool_autoplay->set_icon(get_theme_icon(SNAME("AutoPlay"), SNAME("EditorIcons")));
  750. tool_end->set_icon(get_theme_icon(SNAME("AutoEnd"), SNAME("EditorIcons")));
  751. play_mode->clear();
  752. play_mode->add_icon_item(get_theme_icon(SNAME("PlayTravel"), SNAME("EditorIcons")), TTR("Travel"));
  753. play_mode->add_icon_item(get_theme_icon(SNAME("Play"), SNAME("EditorIcons")), TTR("Immediate"));
  754. } break;
  755. case NOTIFICATION_PROCESS: {
  756. String error;
  757. Ref<AnimationNodeStateMachinePlayback> playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  758. if (error_time > 0) {
  759. error = error_text;
  760. error_time -= get_process_delta_time();
  761. } else if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
  762. error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
  763. } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
  764. error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
  765. /*} else if (state_machine->get_parent().is_valid() && state_machine->get_parent()->is_class("AnimationNodeStateMachine")) {
  766. if (state_machine->get_start_node() == StringName() || state_machine->get_end_node() == StringName()) {
  767. error = TTR("Start and end nodes are needed for a sub-transition.");
  768. }*/
  769. } else if (playback.is_null()) {
  770. error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
  771. }
  772. if (error != error_label->get_text()) {
  773. error_label->set_text(error);
  774. if (!error.is_empty()) {
  775. error_panel->show();
  776. } else {
  777. error_panel->hide();
  778. }
  779. }
  780. for (int i = 0; i < transition_lines.size(); i++) {
  781. int tidx = -1;
  782. for (int j = 0; j < state_machine->get_transition_count(); j++) {
  783. if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) {
  784. tidx = j;
  785. break;
  786. }
  787. }
  788. if (tidx == -1) { //missing transition, should redraw
  789. state_machine_draw->update();
  790. break;
  791. }
  792. if (transition_lines[i].disabled != state_machine->get_transition(tidx)->is_disabled()) {
  793. state_machine_draw->update();
  794. break;
  795. }
  796. if (transition_lines[i].auto_advance != state_machine->get_transition(tidx)->has_auto_advance()) {
  797. state_machine_draw->update();
  798. break;
  799. }
  800. if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) {
  801. state_machine_draw->update();
  802. break;
  803. }
  804. if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) {
  805. state_machine_draw->update();
  806. break;
  807. }
  808. bool acstate = transition_lines[i].advance_condition_name != StringName() && bool(AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + String(transition_lines[i].advance_condition_name)));
  809. if (transition_lines[i].advance_condition_state != acstate) {
  810. state_machine_draw->update();
  811. break;
  812. }
  813. }
  814. bool same_travel_path = true;
  815. Vector<StringName> tp;
  816. bool is_playing = false;
  817. StringName current_node;
  818. StringName blend_from_node;
  819. play_pos = 0;
  820. current_length = 0;
  821. if (playback.is_valid()) {
  822. tp = playback->get_travel_path();
  823. is_playing = playback->is_playing();
  824. current_node = playback->get_current_node();
  825. blend_from_node = playback->get_blend_from_node();
  826. play_pos = playback->get_current_play_pos();
  827. current_length = playback->get_current_length();
  828. }
  829. {
  830. if (last_travel_path.size() != tp.size()) {
  831. same_travel_path = false;
  832. } else {
  833. for (int i = 0; i < last_travel_path.size(); i++) {
  834. if (last_travel_path[i] != tp[i]) {
  835. same_travel_path = false;
  836. break;
  837. }
  838. }
  839. }
  840. }
  841. //update if travel state changed
  842. if (!same_travel_path || last_active != is_playing || last_current_node != current_node || last_blend_from_node != blend_from_node) {
  843. state_machine_draw->update();
  844. last_travel_path = tp;
  845. last_current_node = current_node;
  846. last_active = is_playing;
  847. last_blend_from_node = blend_from_node;
  848. state_machine_play_pos->update();
  849. }
  850. {
  851. if (current_node != StringName() && state_machine->has_node(current_node)) {
  852. String next = current_node;
  853. Ref<AnimationNodeStateMachine> anodesm = state_machine->get_node(next);
  854. Ref<AnimationNodeStateMachinePlayback> current_node_playback;
  855. while (anodesm.is_valid()) {
  856. current_node_playback = AnimationTreeEditor::get_singleton()->get_tree()->get(AnimationTreeEditor::get_singleton()->get_base_path() + next + "/playback");
  857. next += "/" + current_node_playback->get_current_node();
  858. anodesm = anodesm->get_node(current_node_playback->get_current_node());
  859. }
  860. // when current_node is a state machine, use playback of current_node to set play_pos
  861. if (current_node_playback.is_valid()) {
  862. play_pos = current_node_playback->get_current_play_pos();
  863. current_length = current_node_playback->get_current_length();
  864. }
  865. }
  866. }
  867. if (last_play_pos != play_pos) {
  868. last_play_pos = play_pos;
  869. state_machine_play_pos->update();
  870. }
  871. } break;
  872. case NOTIFICATION_VISIBILITY_CHANGED: {
  873. over_node = StringName();
  874. set_process(is_visible_in_tree());
  875. } break;
  876. }
  877. }
  878. void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) {
  879. AnimationTreeEditor::get_singleton()->enter_editor(p_name);
  880. }
  881. void AnimationNodeStateMachineEditor::_removed_from_graph() {
  882. EditorNode::get_singleton()->edit_item(nullptr);
  883. }
  884. void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) {
  885. const String &new_name = p_text;
  886. ERR_FAIL_COND(new_name.is_empty() || new_name.contains(".") || new_name.contains("/"));
  887. if (new_name == prev_name) {
  888. return; // Nothing to do.
  889. }
  890. const String &base_name = new_name;
  891. int base = 1;
  892. String name = base_name;
  893. while (state_machine->has_node(name)) {
  894. base++;
  895. name = base_name + " " + itos(base);
  896. }
  897. updating = true;
  898. undo_redo->create_action(TTR("Node Renamed"));
  899. undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name);
  900. undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name);
  901. undo_redo->add_do_method(this, "_update_graph");
  902. undo_redo->add_undo_method(this, "_update_graph");
  903. undo_redo->commit_action();
  904. name_edit_popup->hide();
  905. updating = false;
  906. state_machine_draw->update();
  907. }
  908. void AnimationNodeStateMachineEditor::_name_edited_focus_out() {
  909. if (updating) {
  910. return;
  911. }
  912. _name_edited(name_edit->get_text());
  913. }
  914. void AnimationNodeStateMachineEditor::_scroll_changed(double) {
  915. if (updating) {
  916. return;
  917. }
  918. state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value()));
  919. state_machine_draw->update();
  920. }
  921. void AnimationNodeStateMachineEditor::_erase_selected() {
  922. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  923. updating = true;
  924. undo_redo->create_action(TTR("Node Removed"));
  925. undo_redo->add_do_method(state_machine.ptr(), "remove_node", selected_node);
  926. undo_redo->add_undo_method(state_machine.ptr(), "add_node", selected_node, state_machine->get_node(selected_node), state_machine->get_node_position(selected_node));
  927. for (int i = 0; i < state_machine->get_transition_count(); i++) {
  928. String from = state_machine->get_transition_from(i);
  929. String to = state_machine->get_transition_to(i);
  930. if (from == selected_node || to == selected_node) {
  931. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(i));
  932. }
  933. }
  934. if (String(state_machine->get_start_node()) == selected_node) {
  935. undo_redo->add_undo_method(state_machine.ptr(), "set_start_node", selected_node);
  936. }
  937. undo_redo->add_do_method(this, "_update_graph");
  938. undo_redo->add_undo_method(this, "_update_graph");
  939. undo_redo->commit_action();
  940. updating = false;
  941. selected_node = StringName();
  942. }
  943. if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {
  944. Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to));
  945. updating = true;
  946. undo_redo->create_action(TTR("Transition Removed"));
  947. undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to);
  948. undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr);
  949. undo_redo->add_do_method(this, "_update_graph");
  950. undo_redo->add_undo_method(this, "_update_graph");
  951. undo_redo->commit_action();
  952. updating = false;
  953. selected_transition_from = StringName();
  954. selected_transition_to = StringName();
  955. }
  956. state_machine_draw->update();
  957. }
  958. void AnimationNodeStateMachineEditor::_autoplay_selected() {
  959. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  960. StringName new_start_node;
  961. if (state_machine->get_start_node() == selected_node) { //toggle it
  962. new_start_node = StringName();
  963. } else {
  964. new_start_node = selected_node;
  965. }
  966. updating = true;
  967. undo_redo->create_action(TTR("Set Start Node (Autoplay)"));
  968. undo_redo->add_do_method(state_machine.ptr(), "set_start_node", new_start_node);
  969. undo_redo->add_undo_method(state_machine.ptr(), "set_start_node", state_machine->get_start_node());
  970. undo_redo->add_do_method(this, "_update_graph");
  971. undo_redo->add_undo_method(this, "_update_graph");
  972. undo_redo->commit_action();
  973. updating = false;
  974. state_machine_draw->update();
  975. }
  976. }
  977. void AnimationNodeStateMachineEditor::_end_selected() {
  978. if (selected_node != StringName() && state_machine->has_node(selected_node)) {
  979. StringName new_end_node;
  980. if (state_machine->get_end_node() == selected_node) { //toggle it
  981. new_end_node = StringName();
  982. } else {
  983. new_end_node = selected_node;
  984. }
  985. updating = true;
  986. undo_redo->create_action(TTR("Set Start Node (Autoplay)"));
  987. undo_redo->add_do_method(state_machine.ptr(), "set_end_node", new_end_node);
  988. undo_redo->add_undo_method(state_machine.ptr(), "set_end_node", state_machine->get_end_node());
  989. undo_redo->add_do_method(this, "_update_graph");
  990. undo_redo->add_undo_method(this, "_update_graph");
  991. undo_redo->commit_action();
  992. updating = false;
  993. state_machine_draw->update();
  994. }
  995. }
  996. void AnimationNodeStateMachineEditor::_update_mode() {
  997. if (tool_select->is_pressed()) {
  998. tool_erase_hb->show();
  999. tool_erase->set_disabled(selected_node == StringName() && selected_transition_from == StringName() && selected_transition_to == StringName());
  1000. tool_autoplay->set_disabled(selected_node == StringName());
  1001. tool_end->set_disabled(selected_node == StringName());
  1002. } else {
  1003. tool_erase_hb->hide();
  1004. }
  1005. }
  1006. void AnimationNodeStateMachineEditor::_bind_methods() {
  1007. ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph);
  1008. ClassDB::bind_method("_removed_from_graph", &AnimationNodeStateMachineEditor::_removed_from_graph);
  1009. ClassDB::bind_method("_open_editor", &AnimationNodeStateMachineEditor::_open_editor);
  1010. }
  1011. AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = nullptr;
  1012. AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {
  1013. singleton = this;
  1014. updating = false;
  1015. HBoxContainer *top_hb = memnew(HBoxContainer);
  1016. add_child(top_hb);
  1017. Ref<ButtonGroup> bg;
  1018. bg.instantiate();
  1019. tool_select = memnew(Button);
  1020. tool_select->set_flat(true);
  1021. top_hb->add_child(tool_select);
  1022. tool_select->set_toggle_mode(true);
  1023. tool_select->set_button_group(bg);
  1024. tool_select->set_pressed(true);
  1025. tool_select->set_tooltip(TTR("Select and move nodes.\nRMB to add new nodes.\nShift+LMB to create connections."));
  1026. tool_select->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1027. tool_create = memnew(Button);
  1028. tool_create->set_flat(true);
  1029. top_hb->add_child(tool_create);
  1030. tool_create->set_toggle_mode(true);
  1031. tool_create->set_button_group(bg);
  1032. tool_create->set_tooltip(TTR("Create new nodes."));
  1033. tool_create->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1034. tool_connect = memnew(Button);
  1035. tool_connect->set_flat(true);
  1036. top_hb->add_child(tool_connect);
  1037. tool_connect->set_toggle_mode(true);
  1038. tool_connect->set_button_group(bg);
  1039. tool_connect->set_tooltip(TTR("Connect nodes."));
  1040. tool_connect->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), varray(), CONNECT_DEFERRED);
  1041. tool_erase_hb = memnew(HBoxContainer);
  1042. top_hb->add_child(tool_erase_hb);
  1043. tool_erase_hb->add_child(memnew(VSeparator));
  1044. tool_erase = memnew(Button);
  1045. tool_erase->set_flat(true);
  1046. tool_erase->set_tooltip(TTR("Remove selected node or transition."));
  1047. tool_erase_hb->add_child(tool_erase);
  1048. tool_erase->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_erase_selected));
  1049. tool_erase->set_disabled(true);
  1050. tool_erase_hb->add_child(memnew(VSeparator));
  1051. tool_autoplay = memnew(Button);
  1052. tool_autoplay->set_flat(true);
  1053. tool_autoplay->set_tooltip(TTR("Toggle autoplay this animation on start, restart or seek to zero."));
  1054. tool_erase_hb->add_child(tool_autoplay);
  1055. tool_autoplay->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_autoplay_selected));
  1056. tool_autoplay->set_disabled(true);
  1057. tool_end = memnew(Button);
  1058. tool_end->set_flat(true);
  1059. tool_end->set_tooltip(TTR("Set the end animation. This is useful for sub-transitions."));
  1060. tool_erase_hb->add_child(tool_end);
  1061. tool_end->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_end_selected));
  1062. tool_end->set_disabled(true);
  1063. top_hb->add_child(memnew(VSeparator));
  1064. top_hb->add_child(memnew(Label(TTR("Transition: "))));
  1065. transition_mode = memnew(OptionButton);
  1066. top_hb->add_child(transition_mode);
  1067. top_hb->add_spacer();
  1068. top_hb->add_child(memnew(Label(TTR("Play Mode:"))));
  1069. play_mode = memnew(OptionButton);
  1070. top_hb->add_child(play_mode);
  1071. panel = memnew(PanelContainer);
  1072. panel->set_clip_contents(true);
  1073. add_child(panel);
  1074. panel->set_v_size_flags(SIZE_EXPAND_FILL);
  1075. state_machine_draw = memnew(Control);
  1076. panel->add_child(state_machine_draw);
  1077. state_machine_draw->connect("gui_input", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_gui_input));
  1078. state_machine_draw->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_draw));
  1079. state_machine_draw->set_focus_mode(FOCUS_ALL);
  1080. state_machine_play_pos = memnew(Control);
  1081. state_machine_draw->add_child(state_machine_play_pos);
  1082. state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent
  1083. state_machine_play_pos->set_anchors_and_offsets_preset(PRESET_WIDE);
  1084. state_machine_play_pos->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_pos_draw));
  1085. v_scroll = memnew(VScrollBar);
  1086. state_machine_draw->add_child(v_scroll);
  1087. v_scroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);
  1088. v_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
  1089. h_scroll = memnew(HScrollBar);
  1090. state_machine_draw->add_child(h_scroll);
  1091. h_scroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);
  1092. h_scroll->set_offset(SIDE_RIGHT, -v_scroll->get_size().x * EDSCALE);
  1093. h_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
  1094. error_panel = memnew(PanelContainer);
  1095. add_child(error_panel);
  1096. error_label = memnew(Label);
  1097. error_panel->add_child(error_label);
  1098. error_panel->hide();
  1099. undo_redo = EditorNode::get_undo_redo();
  1100. set_custom_minimum_size(Size2(0, 300 * EDSCALE));
  1101. menu = memnew(PopupMenu);
  1102. add_child(menu);
  1103. menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_menu_type));
  1104. animations_menu = memnew(PopupMenu);
  1105. menu->add_child(animations_menu);
  1106. animations_menu->set_name("animations");
  1107. animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type));
  1108. name_edit_popup = memnew(Popup);
  1109. add_child(name_edit_popup);
  1110. name_edit = memnew(LineEdit);
  1111. name_edit_popup->add_child(name_edit);
  1112. name_edit->set_anchors_and_offsets_preset(PRESET_WIDE);
  1113. name_edit->connect("text_submitted", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited));
  1114. name_edit->connect("focus_exited", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited_focus_out));
  1115. open_file = memnew(EditorFileDialog);
  1116. add_child(open_file);
  1117. open_file->set_title(TTR("Open Animation Node"));
  1118. open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1119. open_file->connect("file_selected", callable_mp(this, &AnimationNodeStateMachineEditor::_file_opened));
  1120. undo_redo = EditorNode::get_undo_redo();
  1121. over_text = false;
  1122. over_node_what = -1;
  1123. dragging_selected_attempt = false;
  1124. connecting = false;
  1125. last_active = false;
  1126. error_time = 0;
  1127. }