animation_state_machine_editor.cpp 74 KB

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