animation_state_machine_editor.cpp 48 KB

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