animation_state_machine_editor.cpp 72 KB

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