animation_state_machine_editor.cpp 69 KB

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