animation_state_machine_editor.cpp 70 KB

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