animation_state_machine_editor.cpp 69 KB

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