animation_state_machine_editor.cpp 74 KB

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