graph_edit.cpp 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "graph_edit.h"
  31. #include "core/input/input.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/keyboard.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/view_panner.h"
  37. constexpr int MINIMAP_OFFSET = 12;
  38. constexpr int MINIMAP_PADDING = 5;
  39. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  40. return ge->_filter_input(p_point);
  41. }
  42. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  43. ge = p_edit;
  44. }
  45. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  46. ge = p_edit;
  47. graph_proportions = Vector2(1, 1);
  48. graph_padding = Vector2(0, 0);
  49. camera_position = Vector2(100, 50);
  50. camera_size = Vector2(200, 200);
  51. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  52. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  53. is_pressing = false;
  54. is_resizing = false;
  55. }
  56. void GraphEditMinimap::update_minimap() {
  57. Vector2 graph_offset = _get_graph_offset();
  58. Vector2 graph_size = _get_graph_size();
  59. camera_position = ge->get_scroll_ofs() - graph_offset;
  60. camera_size = ge->get_size();
  61. Vector2 render_size = _get_render_size();
  62. float target_ratio = render_size.x / render_size.y;
  63. float graph_ratio = graph_size.x / graph_size.y;
  64. graph_proportions = graph_size;
  65. graph_padding = Vector2(0, 0);
  66. if (graph_ratio > target_ratio) {
  67. graph_proportions.x = graph_size.x;
  68. graph_proportions.y = graph_size.x / target_ratio;
  69. graph_padding.y = Math::abs(graph_size.y - graph_proportions.y) / 2;
  70. } else {
  71. graph_proportions.x = graph_size.y * target_ratio;
  72. graph_proportions.y = graph_size.y;
  73. graph_padding.x = Math::abs(graph_size.x - graph_proportions.x) / 2;
  74. }
  75. // This centers minimap inside the minimap rectangle.
  76. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  77. }
  78. Rect2 GraphEditMinimap::get_camera_rect() {
  79. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  80. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  81. Vector2 camera_pos = (camera_center - camera_viewport / 2);
  82. return Rect2(camera_pos, camera_viewport);
  83. }
  84. Vector2 GraphEditMinimap::_get_render_size() {
  85. if (!is_inside_tree()) {
  86. return Vector2(0, 0);
  87. }
  88. return get_size() - 2 * minimap_padding;
  89. }
  90. Vector2 GraphEditMinimap::_get_graph_offset() {
  91. return Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  92. }
  93. Vector2 GraphEditMinimap::_get_graph_size() {
  94. Vector2 graph_size = Vector2(ge->h_scroll->get_max(), ge->v_scroll->get_max()) - Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  95. if (graph_size.x == 0) {
  96. graph_size.x = 1;
  97. }
  98. if (graph_size.y == 0) {
  99. graph_size.y = 1;
  100. }
  101. return graph_size;
  102. }
  103. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  104. Vector2 map_position = Vector2(0, 0);
  105. Vector2 render_size = _get_render_size();
  106. map_position.x = p_position.x * render_size.x / graph_proportions.x;
  107. map_position.y = p_position.y * render_size.y / graph_proportions.y;
  108. return map_position;
  109. }
  110. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  111. Vector2 graph_position = Vector2(0, 0);
  112. Vector2 render_size = _get_render_size();
  113. graph_position.x = p_position.x * graph_proportions.x / render_size.x;
  114. graph_position.y = p_position.y * graph_proportions.y / render_size.y;
  115. return graph_position;
  116. }
  117. void GraphEditMinimap::gui_input(const Ref<InputEvent> &p_ev) {
  118. ERR_FAIL_COND(p_ev.is_null());
  119. if (!ge->is_minimap_enabled()) {
  120. return;
  121. }
  122. Ref<InputEventMouseButton> mb = p_ev;
  123. Ref<InputEventMouseMotion> mm = p_ev;
  124. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  125. if (mb->is_pressed()) {
  126. is_pressing = true;
  127. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  128. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  129. if (resizer_hitbox.has_point(mb->get_position())) {
  130. is_resizing = true;
  131. } else {
  132. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  133. _adjust_graph_scroll(click_position);
  134. }
  135. } else {
  136. is_pressing = false;
  137. is_resizing = false;
  138. }
  139. accept_event();
  140. } else if (mm.is_valid() && is_pressing) {
  141. if (is_resizing) {
  142. // Prevent setting minimap wider than GraphEdit
  143. Vector2 new_minimap_size;
  144. new_minimap_size.x = MIN(get_size().x - mm->get_relative().x, ge->get_size().x - 2.0 * minimap_padding.x);
  145. new_minimap_size.y = MIN(get_size().y - mm->get_relative().y, ge->get_size().y - 2.0 * minimap_padding.y);
  146. ge->set_minimap_size(new_minimap_size);
  147. queue_redraw();
  148. } else {
  149. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  150. _adjust_graph_scroll(click_position);
  151. }
  152. accept_event();
  153. }
  154. }
  155. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  156. Vector2 graph_offset = _get_graph_offset();
  157. ge->set_scroll_ofs(p_offset + graph_offset - camera_size / 2);
  158. }
  159. PackedStringArray GraphEdit::get_configuration_warnings() const {
  160. PackedStringArray warnings = Control::get_configuration_warnings();
  161. warnings.push_back(RTR("Please be aware that GraphEdit and GraphNode will undergo extensive refactoring in a future beta version involving compatibility-breaking API changes."));
  162. return warnings;
  163. }
  164. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  165. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  166. return OK;
  167. }
  168. Connection c;
  169. c.from = p_from;
  170. c.from_port = p_from_port;
  171. c.to = p_to;
  172. c.to_port = p_to_port;
  173. c.activity = 0;
  174. connections.push_back(c);
  175. top_layer->queue_redraw();
  176. minimap->queue_redraw();
  177. queue_redraw();
  178. connections_layer->queue_redraw();
  179. return OK;
  180. }
  181. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  182. for (const Connection &E : connections) {
  183. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  190. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  191. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  192. connections.erase(E);
  193. top_layer->queue_redraw();
  194. minimap->queue_redraw();
  195. queue_redraw();
  196. connections_layer->queue_redraw();
  197. return;
  198. }
  199. }
  200. }
  201. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  202. *r_connections = connections;
  203. }
  204. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  205. setting_scroll_ofs = true;
  206. h_scroll->set_value(p_ofs.x);
  207. v_scroll->set_value(p_ofs.y);
  208. _update_scroll();
  209. setting_scroll_ofs = false;
  210. }
  211. Vector2 GraphEdit::get_scroll_ofs() const {
  212. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  213. }
  214. void GraphEdit::_scroll_moved(double) {
  215. if (!awaiting_scroll_offset_update) {
  216. call_deferred(SNAME("_update_scroll_offset"));
  217. awaiting_scroll_offset_update = true;
  218. }
  219. top_layer->queue_redraw();
  220. minimap->queue_redraw();
  221. queue_redraw();
  222. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  223. emit_signal(SNAME("scroll_offset_changed"), get_scroll_ofs());
  224. }
  225. }
  226. void GraphEdit::_update_scroll_offset() {
  227. set_block_minimum_size_adjust(true);
  228. for (int i = 0; i < get_child_count(); i++) {
  229. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  230. if (!gn) {
  231. continue;
  232. }
  233. Point2 pos = gn->get_position_offset() * zoom;
  234. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  235. gn->set_position(pos);
  236. if (gn->get_scale() != Vector2(zoom, zoom)) {
  237. gn->set_scale(Vector2(zoom, zoom));
  238. }
  239. }
  240. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  241. set_block_minimum_size_adjust(false);
  242. awaiting_scroll_offset_update = false;
  243. }
  244. void GraphEdit::_update_scroll() {
  245. if (updating) {
  246. return;
  247. }
  248. updating = true;
  249. set_block_minimum_size_adjust(true);
  250. Rect2 screen;
  251. for (int i = 0; i < get_child_count(); i++) {
  252. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  253. if (!gn) {
  254. continue;
  255. }
  256. Rect2 r;
  257. r.position = gn->get_position_offset() * zoom;
  258. r.size = gn->get_size() * zoom;
  259. screen = screen.merge(r);
  260. }
  261. screen.position -= get_size();
  262. screen.size += get_size() * 2.0;
  263. h_scroll->set_min(screen.position.x);
  264. h_scroll->set_max(screen.position.x + screen.size.x);
  265. h_scroll->set_page(get_size().x);
  266. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  267. h_scroll->hide();
  268. } else {
  269. h_scroll->show();
  270. }
  271. v_scroll->set_min(screen.position.y);
  272. v_scroll->set_max(screen.position.y + screen.size.y);
  273. v_scroll->set_page(get_size().y);
  274. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  275. v_scroll->hide();
  276. } else {
  277. v_scroll->show();
  278. }
  279. Size2 hmin = h_scroll->get_combined_minimum_size();
  280. Size2 vmin = v_scroll->get_combined_minimum_size();
  281. // Avoid scrollbar overlapping.
  282. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  283. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  284. set_block_minimum_size_adjust(false);
  285. if (!awaiting_scroll_offset_update) {
  286. call_deferred(SNAME("_update_scroll_offset"));
  287. awaiting_scroll_offset_update = true;
  288. }
  289. updating = false;
  290. }
  291. void GraphEdit::_graph_node_raised(Node *p_gn) {
  292. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  293. ERR_FAIL_COND(!gn);
  294. if (gn->is_comment()) {
  295. move_child(gn, 0);
  296. } else {
  297. gn->move_to_front();
  298. }
  299. }
  300. void GraphEdit::_graph_node_selected(Node *p_gn) {
  301. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  302. ERR_FAIL_COND(!gn);
  303. emit_signal(SNAME("node_selected"), gn);
  304. }
  305. void GraphEdit::_graph_node_deselected(Node *p_gn) {
  306. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  307. ERR_FAIL_COND(!gn);
  308. emit_signal(SNAME("node_deselected"), gn);
  309. }
  310. void GraphEdit::_graph_node_moved(Node *p_gn) {
  311. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  312. ERR_FAIL_COND(!gn);
  313. top_layer->queue_redraw();
  314. minimap->queue_redraw();
  315. queue_redraw();
  316. connections_layer->queue_redraw();
  317. }
  318. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_gn) {
  319. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  320. ERR_FAIL_COND(!gn);
  321. top_layer->queue_redraw();
  322. minimap->queue_redraw();
  323. queue_redraw();
  324. connections_layer->queue_redraw();
  325. }
  326. void GraphEdit::add_child_notify(Node *p_child) {
  327. Control::add_child_notify(p_child);
  328. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  329. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  330. if (gn) {
  331. gn->set_scale(Vector2(zoom, zoom));
  332. gn->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved).bind(gn));
  333. gn->connect("selected", callable_mp(this, &GraphEdit::_graph_node_selected).bind(gn));
  334. gn->connect("deselected", callable_mp(this, &GraphEdit::_graph_node_deselected).bind(gn));
  335. gn->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(gn));
  336. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised).bind(gn));
  337. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  338. gn->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  339. _graph_node_moved(gn);
  340. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  341. }
  342. }
  343. void GraphEdit::remove_child_notify(Node *p_child) {
  344. Control::remove_child_notify(p_child);
  345. if (p_child == top_layer) {
  346. top_layer = nullptr;
  347. minimap = nullptr;
  348. } else if (p_child == connections_layer) {
  349. connections_layer = nullptr;
  350. }
  351. if (top_layer != nullptr && is_inside_tree()) {
  352. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  353. }
  354. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  355. if (gn) {
  356. gn->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  357. gn->disconnect("selected", callable_mp(this, &GraphEdit::_graph_node_selected));
  358. gn->disconnect("deselected", callable_mp(this, &GraphEdit::_graph_node_deselected));
  359. gn->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  360. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  361. // In case of the whole GraphEdit being destroyed these references can already be freed.
  362. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  363. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  364. }
  365. if (minimap != nullptr && minimap->is_inside_tree()) {
  366. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  367. }
  368. }
  369. }
  370. void GraphEdit::_notification(int p_what) {
  371. switch (p_what) {
  372. case NOTIFICATION_ENTER_TREE:
  373. case NOTIFICATION_THEME_CHANGED: {
  374. port_hotzone_inner_extent = get_theme_constant("port_hotzone_inner_extent");
  375. port_hotzone_outer_extent = get_theme_constant("port_hotzone_outer_extent");
  376. zoom_minus->set_icon(get_theme_icon(SNAME("minus")));
  377. zoom_reset->set_icon(get_theme_icon(SNAME("reset")));
  378. zoom_plus->set_icon(get_theme_icon(SNAME("more")));
  379. snap_button->set_icon(get_theme_icon(SNAME("snap")));
  380. minimap_button->set_icon(get_theme_icon(SNAME("minimap")));
  381. layout_button->set_icon(get_theme_icon(SNAME("layout")));
  382. zoom_label->set_custom_minimum_size(Size2(48, 0) * get_theme_default_base_scale());
  383. } break;
  384. case NOTIFICATION_READY: {
  385. Size2 hmin = h_scroll->get_combined_minimum_size();
  386. Size2 vmin = v_scroll->get_combined_minimum_size();
  387. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  388. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  389. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  390. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  391. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  392. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  393. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  394. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  395. } break;
  396. case NOTIFICATION_DRAW: {
  397. draw_style_box(get_theme_stylebox(SNAME("bg")), Rect2(Point2(), get_size()));
  398. if (is_using_snap()) {
  399. // Draw grid.
  400. int snap = get_snap();
  401. Vector2 offset = get_scroll_ofs() / zoom;
  402. Size2 size = get_size() / zoom;
  403. Point2i from = (offset / float(snap)).floor();
  404. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  405. Color grid_minor = get_theme_color(SNAME("grid_minor"));
  406. Color grid_major = get_theme_color(SNAME("grid_major"));
  407. for (int i = from.x; i < from.x + len.x; i++) {
  408. Color color;
  409. if (ABS(i) % 10 == 0) {
  410. color = grid_major;
  411. } else {
  412. color = grid_minor;
  413. }
  414. float base_ofs = i * snap * zoom - offset.x * zoom;
  415. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  416. }
  417. for (int i = from.y; i < from.y + len.y; i++) {
  418. Color color;
  419. if (ABS(i) % 10 == 0) {
  420. color = grid_major;
  421. } else {
  422. color = grid_minor;
  423. }
  424. float base_ofs = i * snap * zoom - offset.y * zoom;
  425. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  426. }
  427. }
  428. } break;
  429. case NOTIFICATION_RESIZED: {
  430. _update_scroll();
  431. top_layer->queue_redraw();
  432. minimap->queue_redraw();
  433. } break;
  434. }
  435. }
  436. void GraphEdit::_update_comment_enclosed_nodes_list(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes) {
  437. Rect2 comment_node_rect = p_node->get_rect();
  438. comment_node_rect.size *= zoom;
  439. Vector<GraphNode *> enclosed_nodes;
  440. for (int i = 0; i < get_child_count(); i++) {
  441. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  442. if (!gn || gn->is_selected()) {
  443. continue;
  444. }
  445. Rect2 node_rect = gn->get_rect();
  446. node_rect.size *= zoom;
  447. bool included = comment_node_rect.encloses(node_rect);
  448. if (included) {
  449. enclosed_nodes.push_back(gn);
  450. }
  451. }
  452. p_comment_enclosed_nodes.insert(p_node->get_name(), enclosed_nodes);
  453. }
  454. void GraphEdit::_set_drag_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, bool p_drag) {
  455. for (int i = 0; i < p_comment_enclosed_nodes[p_node->get_name()].size(); i++) {
  456. p_comment_enclosed_nodes[p_node->get_name()][i]->set_drag(p_drag);
  457. }
  458. }
  459. void GraphEdit::_set_position_of_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, Vector2 p_drag_accum) {
  460. for (int i = 0; i < p_comment_enclosed_nodes[p_node->get_name()].size(); i++) {
  461. Vector2 pos = (p_comment_enclosed_nodes[p_node->get_name()][i]->get_drag_from() * zoom + drag_accum) / zoom;
  462. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  463. const int snap = get_snap();
  464. pos = pos.snapped(Vector2(snap, snap));
  465. }
  466. p_comment_enclosed_nodes[p_node->get_name()][i]->set_position_offset(pos);
  467. }
  468. }
  469. bool GraphEdit::_filter_input(const Point2 &p_point) {
  470. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  471. for (int i = get_child_count() - 1; i >= 0; i--) {
  472. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  473. if (!gn) {
  474. continue;
  475. }
  476. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  477. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  478. port_size.height = MAX(port_size.height, gn->get_connection_input_height(j));
  479. if (is_in_input_hotzone(gn, j, p_point / zoom, port_size)) {
  480. return true;
  481. }
  482. }
  483. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  484. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  485. port_size.height = MAX(port_size.height, gn->get_connection_output_height(j));
  486. if (is_in_output_hotzone(gn, j, p_point / zoom, port_size)) {
  487. return true;
  488. }
  489. }
  490. }
  491. return false;
  492. }
  493. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  494. Ref<InputEventMouseButton> mb = p_ev;
  495. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  496. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  497. connecting_valid = false;
  498. click_pos = mb->get_position() / zoom;
  499. for (int i = get_child_count() - 1; i >= 0; i--) {
  500. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  501. if (!gn) {
  502. continue;
  503. }
  504. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  505. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  506. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  507. port_size.height = MAX(port_size.height, gn->get_connection_output_height(j));
  508. if (is_in_output_hotzone(gn, j, click_pos, port_size)) {
  509. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  510. //check disconnect
  511. for (const Connection &E : connections) {
  512. if (E.from == gn->get_name() && E.from_port == j) {
  513. Node *to = get_node(NodePath(E.to));
  514. if (Object::cast_to<GraphNode>(to)) {
  515. connecting_from = E.to;
  516. connecting_index = E.to_port;
  517. connecting_out = false;
  518. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E.to_port);
  519. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E.to_port);
  520. connecting_target = false;
  521. connecting_to = pos;
  522. if (connecting_type >= 0) {
  523. just_disconnected = true;
  524. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  525. to = get_node(NodePath(connecting_from)); // Maybe it was erased.
  526. if (Object::cast_to<GraphNode>(to)) {
  527. connecting = true;
  528. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  529. }
  530. }
  531. return;
  532. }
  533. }
  534. }
  535. }
  536. connecting_from = gn->get_name();
  537. connecting_index = j;
  538. connecting_out = true;
  539. connecting_type = gn->get_connection_output_type(j);
  540. connecting_color = gn->get_connection_output_color(j);
  541. connecting_target = false;
  542. connecting_to = pos;
  543. if (connecting_type >= 0) {
  544. connecting = true;
  545. just_disconnected = false;
  546. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  547. }
  548. return;
  549. }
  550. }
  551. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  552. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  553. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  554. port_size.height = MAX(port_size.height, gn->get_connection_input_height(j));
  555. if (is_in_input_hotzone(gn, j, click_pos, port_size)) {
  556. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  557. // Check disconnect.
  558. for (const Connection &E : connections) {
  559. if (E.to == gn->get_name() && E.to_port == j) {
  560. Node *fr = get_node(NodePath(E.from));
  561. if (Object::cast_to<GraphNode>(fr)) {
  562. connecting_from = E.from;
  563. connecting_index = E.from_port;
  564. connecting_out = true;
  565. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E.from_port);
  566. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E.from_port);
  567. connecting_target = false;
  568. connecting_to = pos;
  569. just_disconnected = true;
  570. if (connecting_type >= 0) {
  571. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  572. fr = get_node(NodePath(connecting_from)); // Maybe it was erased.
  573. if (Object::cast_to<GraphNode>(fr)) {
  574. connecting = true;
  575. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  576. }
  577. }
  578. return;
  579. }
  580. }
  581. }
  582. }
  583. connecting_from = gn->get_name();
  584. connecting_index = j;
  585. connecting_out = false;
  586. connecting_type = gn->get_connection_input_type(j);
  587. connecting_color = gn->get_connection_input_color(j);
  588. connecting_target = false;
  589. connecting_to = pos;
  590. if (connecting_type >= 0) {
  591. connecting = true;
  592. just_disconnected = false;
  593. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  594. }
  595. return;
  596. }
  597. }
  598. }
  599. }
  600. Ref<InputEventMouseMotion> mm = p_ev;
  601. if (mm.is_valid() && connecting) {
  602. connecting_to = mm->get_position();
  603. connecting_target = false;
  604. top_layer->queue_redraw();
  605. minimap->queue_redraw();
  606. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > 20.0;
  607. if (connecting_valid) {
  608. Vector2 mpos = mm->get_position() / zoom;
  609. for (int i = get_child_count() - 1; i >= 0; i--) {
  610. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  611. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  612. if (!gn) {
  613. continue;
  614. }
  615. if (!connecting_out) {
  616. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  617. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  618. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  619. port_size.height = MAX(port_size.height, gn->get_connection_output_height(j));
  620. int type = gn->get_connection_output_type(j);
  621. if ((type == connecting_type || valid_connection_types.has(ConnType(connecting_type, type))) && is_in_output_hotzone(gn, j, mpos, port_size)) {
  622. if (!is_node_hover_valid(gn->get_name(), j, connecting_from, connecting_index)) {
  623. continue;
  624. }
  625. connecting_target = true;
  626. connecting_to = pos;
  627. connecting_target_to = gn->get_name();
  628. connecting_target_index = j;
  629. return;
  630. }
  631. }
  632. } else {
  633. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  634. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  635. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  636. port_size.height = MAX(port_size.height, gn->get_connection_input_height(j));
  637. int type = gn->get_connection_input_type(j);
  638. if ((type == connecting_type || valid_connection_types.has(ConnType(connecting_type, type))) && is_in_input_hotzone(gn, j, mpos, port_size)) {
  639. if (!is_node_hover_valid(connecting_from, connecting_index, gn->get_name(), j)) {
  640. continue;
  641. }
  642. connecting_target = true;
  643. connecting_to = pos;
  644. connecting_target_to = gn->get_name();
  645. connecting_target_index = j;
  646. return;
  647. }
  648. }
  649. }
  650. }
  651. }
  652. }
  653. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  654. if (connecting_valid) {
  655. if (connecting && connecting_target) {
  656. if (connecting_out) {
  657. emit_signal(SNAME("connection_request"), connecting_from, connecting_index, connecting_target_to, connecting_target_index);
  658. } else {
  659. emit_signal(SNAME("connection_request"), connecting_target_to, connecting_target_index, connecting_from, connecting_index);
  660. }
  661. } else if (!just_disconnected) {
  662. if (connecting_out) {
  663. emit_signal(SNAME("connection_to_empty"), connecting_from, connecting_index, mb->get_position());
  664. } else {
  665. emit_signal(SNAME("connection_from_empty"), connecting_from, connecting_index, mb->get_position());
  666. }
  667. }
  668. }
  669. if (connecting) {
  670. force_connection_drag_end();
  671. }
  672. }
  673. }
  674. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) {
  675. if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) {
  676. return false;
  677. }
  678. Rect2 control_rect = p_control->get_rect();
  679. control_rect.size *= zoom;
  680. control_rect.position *= zoom;
  681. control_rect.position += p_offset;
  682. if (!control_rect.has_point(mpos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  683. // Test children.
  684. for (int i = 0; i < p_control->get_child_count(); i++) {
  685. Control *child_rect = Object::cast_to<Control>(p_control->get_child(i));
  686. if (!child_rect) {
  687. continue;
  688. }
  689. if (_check_clickable_control(child_rect, mpos, control_rect.position)) {
  690. return true;
  691. }
  692. }
  693. return false;
  694. } else {
  695. return true;
  696. }
  697. }
  698. bool GraphEdit::is_in_input_hotzone(GraphNode *p_node, int p_port, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  699. bool success;
  700. if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_node, p_port, p_mouse_pos, success)) {
  701. return success;
  702. } else {
  703. Vector2 pos = p_node->get_connection_input_position(p_port) + p_node->get_position();
  704. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true);
  705. }
  706. }
  707. bool GraphEdit::is_in_output_hotzone(GraphNode *p_node, int p_port, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  708. bool success;
  709. if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_node, p_port, p_mouse_pos, success)) {
  710. return success;
  711. } else {
  712. Vector2 pos = p_node->get_connection_output_position(p_port) + p_node->get_position();
  713. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false);
  714. }
  715. }
  716. bool GraphEdit::is_in_port_hotzone(const Vector2 &pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  717. Rect2 hotzone = Rect2(
  718. pos.x - (p_left ? port_hotzone_outer_extent : port_hotzone_inner_extent),
  719. pos.y - p_port_size.height / 2.0,
  720. port_hotzone_inner_extent + port_hotzone_outer_extent,
  721. p_port_size.height);
  722. if (!hotzone.has_point(p_mouse_pos)) {
  723. return false;
  724. }
  725. for (int i = 0; i < get_child_count(); i++) {
  726. Control *child = Object::cast_to<Control>(get_child(i));
  727. if (!child) {
  728. continue;
  729. }
  730. Rect2 child_rect = child->get_rect();
  731. child_rect.size *= zoom;
  732. if (child_rect.has_point(p_mouse_pos * zoom)) {
  733. for (int j = 0; j < child->get_child_count(); j++) {
  734. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  735. if (!subchild) {
  736. continue;
  737. }
  738. if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) {
  739. return false;
  740. }
  741. }
  742. }
  743. }
  744. return true;
  745. }
  746. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) {
  747. Vector<Vector2> ret;
  748. if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) {
  749. return ret;
  750. }
  751. float x_diff = (p_to.x - p_from.x);
  752. float cp_offset = x_diff * lines_curvature;
  753. if (x_diff < 0) {
  754. cp_offset *= -1;
  755. }
  756. Curve2D curve;
  757. curve.add_point(p_from);
  758. curve.set_point_out(0, Vector2(cp_offset, 0));
  759. curve.add_point(p_to);
  760. curve.set_point_in(1, Vector2(-cp_offset, 0));
  761. if (lines_curvature > 0) {
  762. return curve.tessellate(5, 2.0);
  763. } else {
  764. return curve.tessellate(1);
  765. }
  766. }
  767. void GraphEdit::_draw_connection_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color, float p_width, float p_zoom) {
  768. Vector<Vector2> points = get_connection_line(p_from / p_zoom, p_to / p_zoom);
  769. Vector<Vector2> scaled_points;
  770. Vector<Color> colors;
  771. float length = (p_from / p_zoom).distance_to(p_to / p_zoom);
  772. for (int i = 0; i < points.size(); i++) {
  773. float d = (p_from / p_zoom).distance_to(points[i]) / length;
  774. colors.push_back(p_color.lerp(p_to_color, d));
  775. scaled_points.push_back(points[i] * p_zoom);
  776. }
  777. p_where->draw_polyline_colors(scaled_points, colors, Math::floor(p_width * get_theme_default_base_scale()), lines_antialiased);
  778. }
  779. void GraphEdit::_connections_layer_draw() {
  780. Color activity_color = get_theme_color(SNAME("activity"));
  781. // Draw connections.
  782. List<List<Connection>::Element *> to_erase;
  783. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  784. const Connection &c = E->get();
  785. Node *from = get_node(NodePath(c.from));
  786. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  787. if (!gfrom) {
  788. to_erase.push_back(E);
  789. continue;
  790. }
  791. Node *to = get_node(NodePath(c.to));
  792. GraphNode *gto = Object::cast_to<GraphNode>(to);
  793. if (!gto) {
  794. to_erase.push_back(E);
  795. continue;
  796. }
  797. Vector2 frompos = gfrom->get_connection_output_position(c.from_port) + gfrom->get_position_offset() * zoom;
  798. Color color = gfrom->get_connection_output_color(c.from_port);
  799. Vector2 topos = gto->get_connection_input_position(c.to_port) + gto->get_position_offset() * zoom;
  800. Color tocolor = gto->get_connection_input_color(c.to_port);
  801. if (c.activity > 0) {
  802. color = color.lerp(activity_color, c.activity);
  803. tocolor = tocolor.lerp(activity_color, c.activity);
  804. }
  805. _draw_connection_line(connections_layer, frompos, topos, color, tocolor, lines_thickness, zoom);
  806. }
  807. for (List<Connection>::Element *&E : to_erase) {
  808. connections.erase(E);
  809. }
  810. }
  811. void GraphEdit::_top_layer_draw() {
  812. _update_scroll();
  813. if (connecting) {
  814. Node *fromn = get_node(NodePath(connecting_from));
  815. ERR_FAIL_COND(!fromn);
  816. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  817. ERR_FAIL_COND(!from);
  818. Vector2 pos;
  819. if (connecting_out) {
  820. pos = from->get_connection_output_position(connecting_index);
  821. } else {
  822. pos = from->get_connection_input_position(connecting_index);
  823. }
  824. pos += from->get_position();
  825. Vector2 topos;
  826. topos = connecting_to;
  827. Color col = connecting_color;
  828. if (connecting_target) {
  829. col.r += 0.4;
  830. col.g += 0.4;
  831. col.b += 0.4;
  832. }
  833. if (!connecting_out) {
  834. SWAP(pos, topos);
  835. }
  836. _draw_connection_line(top_layer, pos, topos, col, col, lines_thickness, zoom);
  837. }
  838. if (box_selecting) {
  839. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_fill")));
  840. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_stroke")), false);
  841. }
  842. }
  843. void GraphEdit::_minimap_draw() {
  844. if (!is_minimap_enabled()) {
  845. return;
  846. }
  847. minimap->update_minimap();
  848. // Draw the minimap background.
  849. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  850. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("bg")), minimap_rect);
  851. Vector2 graph_offset = minimap->_get_graph_offset();
  852. Vector2 minimap_offset = minimap->minimap_offset;
  853. // Draw comment graph nodes.
  854. for (int i = get_child_count() - 1; i >= 0; i--) {
  855. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  856. if (!gn || !gn->is_comment()) {
  857. continue;
  858. }
  859. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  860. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  861. Rect2 node_rect = Rect2(node_position, node_size);
  862. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  863. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  864. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "comment_focus" : "comment");
  865. if (sbf.is_valid()) {
  866. Color node_color = sbf->get_bg_color();
  867. sb_minimap->set_bg_color(node_color);
  868. }
  869. minimap->draw_style_box(sb_minimap, node_rect);
  870. }
  871. // Draw regular graph nodes.
  872. for (int i = get_child_count() - 1; i >= 0; i--) {
  873. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  874. if (!gn || gn->is_comment()) {
  875. continue;
  876. }
  877. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  878. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  879. Rect2 node_rect = Rect2(node_position, node_size);
  880. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  881. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  882. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "selected_frame" : "frame");
  883. if (sbf.is_valid()) {
  884. Color node_color = sbf->get_border_color();
  885. sb_minimap->set_bg_color(node_color);
  886. }
  887. minimap->draw_style_box(sb_minimap, node_rect);
  888. }
  889. // Draw node connections.
  890. Color activity_color = get_theme_color(SNAME("activity"));
  891. for (const Connection &E : connections) {
  892. Node *from = get_node(NodePath(E.from));
  893. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  894. if (!gfrom) {
  895. continue;
  896. }
  897. Node *to = get_node(NodePath(E.to));
  898. GraphNode *gto = Object::cast_to<GraphNode>(to);
  899. if (!gto) {
  900. continue;
  901. }
  902. Vector2 from_port_position = gfrom->get_position_offset() * zoom + gfrom->get_connection_output_position(E.from_port);
  903. Vector2 from_position = minimap->_convert_from_graph_position(from_port_position - graph_offset) + minimap_offset;
  904. Color from_color = gfrom->get_connection_output_color(E.from_port);
  905. Vector2 to_port_position = gto->get_position_offset() * zoom + gto->get_connection_input_position(E.to_port);
  906. Vector2 to_position = minimap->_convert_from_graph_position(to_port_position - graph_offset) + minimap_offset;
  907. Color to_color = gto->get_connection_input_color(E.to_port);
  908. if (E.activity > 0) {
  909. from_color = from_color.lerp(activity_color, E.activity);
  910. to_color = to_color.lerp(activity_color, E.activity);
  911. }
  912. _draw_connection_line(minimap, from_position, to_position, from_color, to_color, 0.1, minimap->_convert_from_graph_position(Vector2(zoom, zoom)).length());
  913. }
  914. // Draw the "camera" viewport.
  915. Rect2 camera_rect = minimap->get_camera_rect();
  916. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("camera")), camera_rect);
  917. // Draw the resizer control.
  918. Ref<Texture2D> resizer = minimap->get_theme_icon(SNAME("resizer"));
  919. Color resizer_color = minimap->get_theme_color(SNAME("resizer_color"));
  920. minimap->draw_texture(resizer, Point2(), resizer_color);
  921. }
  922. void GraphEdit::set_selected(Node *p_child) {
  923. for (int i = get_child_count() - 1; i >= 0; i--) {
  924. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  925. if (!gn) {
  926. continue;
  927. }
  928. gn->set_selected(gn == p_child);
  929. }
  930. }
  931. void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
  932. ERR_FAIL_COND(p_ev.is_null());
  933. if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) {
  934. return;
  935. }
  936. Ref<InputEventMouseMotion> mm = p_ev;
  937. if (mm.is_valid() && dragging) {
  938. if (!moving_selection) {
  939. emit_signal(SNAME("begin_node_move"));
  940. moving_selection = true;
  941. }
  942. just_selected = true;
  943. drag_accum += mm->get_relative();
  944. for (int i = get_child_count() - 1; i >= 0; i--) {
  945. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  946. if (gn && gn->is_selected() && gn->is_draggable()) {
  947. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  948. // Snapping can be toggled temporarily by holding down Ctrl.
  949. // This is done here as to not toggle the grid when holding down Ctrl.
  950. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  951. const int snap = get_snap();
  952. pos = pos.snapped(Vector2(snap, snap));
  953. }
  954. gn->set_position_offset(pos);
  955. if (gn->is_comment()) {
  956. _set_position_of_comment_enclosed_nodes(gn, comment_enclosed_nodes, drag_accum);
  957. }
  958. }
  959. }
  960. }
  961. if (mm.is_valid() && box_selecting) {
  962. box_selecting_to = mm->get_position();
  963. box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());
  964. for (int i = get_child_count() - 1; i >= 0; i--) {
  965. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  966. if (!gn) {
  967. continue;
  968. }
  969. Rect2 r = gn->get_rect();
  970. r.size *= zoom;
  971. bool in_box = r.intersects(box_selecting_rect);
  972. if (in_box) {
  973. gn->set_selected(box_selection_mode_additive);
  974. } else {
  975. gn->set_selected(previous_selected.find(gn) != nullptr);
  976. }
  977. }
  978. top_layer->queue_redraw();
  979. minimap->queue_redraw();
  980. }
  981. Ref<InputEventMouseButton> b = p_ev;
  982. if (b.is_valid()) {
  983. if (b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) {
  984. if (box_selecting) {
  985. box_selecting = false;
  986. for (int i = get_child_count() - 1; i >= 0; i--) {
  987. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  988. if (!gn) {
  989. continue;
  990. }
  991. gn->set_selected(previous_selected.find(gn) != nullptr);
  992. }
  993. top_layer->queue_redraw();
  994. minimap->queue_redraw();
  995. } else {
  996. if (connecting) {
  997. force_connection_drag_end();
  998. } else {
  999. emit_signal(SNAME("popup_request"), b->get_position());
  1000. }
  1001. }
  1002. }
  1003. if (b->get_button_index() == MouseButton::LEFT && !b->is_pressed() && dragging) {
  1004. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1005. //deselect current node
  1006. for (int i = get_child_count() - 1; i >= 0; i--) {
  1007. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1008. if (gn) {
  1009. Rect2 r = gn->get_rect();
  1010. r.size *= zoom;
  1011. if (r.has_point(b->get_position())) {
  1012. gn->set_selected(false);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. if (drag_accum != Vector2()) {
  1018. for (int i = get_child_count() - 1; i >= 0; i--) {
  1019. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1020. if (gn && gn->is_selected()) {
  1021. gn->set_drag(false);
  1022. if (gn->is_comment()) {
  1023. _set_drag_comment_enclosed_nodes(gn, comment_enclosed_nodes, false);
  1024. }
  1025. }
  1026. }
  1027. }
  1028. if (moving_selection) {
  1029. emit_signal(SNAME("end_node_move"));
  1030. moving_selection = false;
  1031. }
  1032. dragging = false;
  1033. top_layer->queue_redraw();
  1034. minimap->queue_redraw();
  1035. queue_redraw();
  1036. connections_layer->queue_redraw();
  1037. }
  1038. if (b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
  1039. GraphNode *gn = nullptr;
  1040. // Find node which was clicked on.
  1041. for (int i = get_child_count() - 1; i >= 0; i--) {
  1042. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  1043. if (!gn_selected) {
  1044. continue;
  1045. }
  1046. if (gn_selected->is_resizing()) {
  1047. continue;
  1048. }
  1049. if (gn_selected->has_point((b->get_position() - gn_selected->get_position()) / zoom)) {
  1050. gn = gn_selected;
  1051. break;
  1052. }
  1053. }
  1054. if (gn) {
  1055. if (_filter_input(b->get_position())) {
  1056. return;
  1057. }
  1058. // Left-clicked on a node, select it.
  1059. dragging = true;
  1060. drag_accum = Vector2();
  1061. just_selected = !gn->is_selected();
  1062. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1063. for (int i = 0; i < get_child_count(); i++) {
  1064. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1065. if (!o_gn) {
  1066. continue;
  1067. }
  1068. o_gn->set_selected(o_gn == gn);
  1069. }
  1070. }
  1071. gn->set_selected(true);
  1072. for (int i = 0; i < get_child_count(); i++) {
  1073. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1074. if (!o_gn) {
  1075. continue;
  1076. }
  1077. if (o_gn->is_selected()) {
  1078. o_gn->set_drag(true);
  1079. if (o_gn->is_comment()) {
  1080. _update_comment_enclosed_nodes_list(o_gn, comment_enclosed_nodes);
  1081. _set_drag_comment_enclosed_nodes(o_gn, comment_enclosed_nodes, true);
  1082. }
  1083. }
  1084. }
  1085. } else {
  1086. if (_filter_input(b->get_position())) {
  1087. return;
  1088. }
  1089. if (panner->is_panning()) {
  1090. return;
  1091. }
  1092. // Left-clicked on empty space, start box select.
  1093. box_selecting = true;
  1094. box_selecting_from = b->get_position();
  1095. if (b->is_ctrl_pressed()) {
  1096. box_selection_mode_additive = true;
  1097. previous_selected.clear();
  1098. for (int i = get_child_count() - 1; i >= 0; i--) {
  1099. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1100. if (!gn2 || !gn2->is_selected()) {
  1101. continue;
  1102. }
  1103. previous_selected.push_back(gn2);
  1104. }
  1105. } else if (b->is_shift_pressed()) {
  1106. box_selection_mode_additive = false;
  1107. previous_selected.clear();
  1108. for (int i = get_child_count() - 1; i >= 0; i--) {
  1109. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1110. if (!gn2 || !gn2->is_selected()) {
  1111. continue;
  1112. }
  1113. previous_selected.push_back(gn2);
  1114. }
  1115. } else {
  1116. box_selection_mode_additive = true;
  1117. previous_selected.clear();
  1118. for (int i = get_child_count() - 1; i >= 0; i--) {
  1119. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1120. if (!gn2) {
  1121. continue;
  1122. }
  1123. gn2->set_selected(false);
  1124. }
  1125. }
  1126. }
  1127. }
  1128. if (b->get_button_index() == MouseButton::LEFT && !b->is_pressed() && box_selecting) {
  1129. // Box selection ended. Nodes were selected during mouse movement.
  1130. box_selecting = false;
  1131. box_selecting_rect = Rect2();
  1132. previous_selected.clear();
  1133. top_layer->queue_redraw();
  1134. minimap->queue_redraw();
  1135. }
  1136. }
  1137. if (p_ev->is_pressed()) {
  1138. if (p_ev->is_action("ui_graph_duplicate", true)) {
  1139. emit_signal(SNAME("duplicate_nodes_request"));
  1140. accept_event();
  1141. } else if (p_ev->is_action("ui_copy", true)) {
  1142. emit_signal(SNAME("copy_nodes_request"));
  1143. accept_event();
  1144. } else if (p_ev->is_action("ui_paste", true)) {
  1145. emit_signal(SNAME("paste_nodes_request"));
  1146. accept_event();
  1147. } else if (p_ev->is_action("ui_graph_delete", true)) {
  1148. TypedArray<StringName> nodes;
  1149. for (int i = 0; i < get_child_count(); i++) {
  1150. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1151. if (!gn) {
  1152. continue;
  1153. }
  1154. if (gn->is_selected() && gn->is_close_button_visible()) {
  1155. nodes.push_back(gn->get_name());
  1156. }
  1157. }
  1158. emit_signal(SNAME("delete_nodes_request"), nodes);
  1159. accept_event();
  1160. }
  1161. }
  1162. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  1163. if (magnify_gesture.is_valid()) {
  1164. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  1165. }
  1166. Ref<InputEventPanGesture> pan_gesture = p_ev;
  1167. if (pan_gesture.is_valid()) {
  1168. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  1169. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  1170. }
  1171. }
  1172. void GraphEdit::_scroll_callback(Vector2 p_scroll_vec, bool p_alt) {
  1173. if (p_scroll_vec.x != 0) {
  1174. h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * Math::abs(p_scroll_vec.x) / 8) * SIGN(p_scroll_vec.x));
  1175. } else {
  1176. v_scroll->set_value(v_scroll->get_value() + (v_scroll->get_page() * Math::abs(p_scroll_vec.y) / 8) * SIGN(p_scroll_vec.y));
  1177. }
  1178. }
  1179. void GraphEdit::_pan_callback(Vector2 p_scroll_vec) {
  1180. h_scroll->set_value(h_scroll->get_value() - p_scroll_vec.x);
  1181. v_scroll->set_value(v_scroll->get_value() - p_scroll_vec.y);
  1182. }
  1183. void GraphEdit::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt) {
  1184. set_zoom_custom(p_scroll_vec.y < 0 ? zoom * zoom_step : zoom / zoom_step, p_origin);
  1185. }
  1186. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1187. for (Connection &E : connections) {
  1188. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  1189. if (Math::is_equal_approx(E.activity, p_activity)) {
  1190. //update only if changed
  1191. top_layer->queue_redraw();
  1192. minimap->queue_redraw();
  1193. connections_layer->queue_redraw();
  1194. }
  1195. E.activity = p_activity;
  1196. return;
  1197. }
  1198. }
  1199. }
  1200. void GraphEdit::clear_connections() {
  1201. connections.clear();
  1202. minimap->queue_redraw();
  1203. queue_redraw();
  1204. connections_layer->queue_redraw();
  1205. }
  1206. void GraphEdit::force_connection_drag_end() {
  1207. ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
  1208. connecting = false;
  1209. connecting_valid = false;
  1210. top_layer->queue_redraw();
  1211. minimap->queue_redraw();
  1212. queue_redraw();
  1213. connections_layer->queue_redraw();
  1214. emit_signal(SNAME("connection_drag_ended"));
  1215. }
  1216. bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) {
  1217. bool valid = true;
  1218. GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid);
  1219. return valid;
  1220. }
  1221. void GraphEdit::set_panning_scheme(PanningScheme p_scheme) {
  1222. panning_scheme = p_scheme;
  1223. panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme);
  1224. }
  1225. GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const {
  1226. return panning_scheme;
  1227. }
  1228. void GraphEdit::set_zoom(float p_zoom) {
  1229. set_zoom_custom(p_zoom, get_size() / 2);
  1230. }
  1231. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1232. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1233. if (zoom == p_zoom) {
  1234. return;
  1235. }
  1236. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  1237. zoom = p_zoom;
  1238. top_layer->queue_redraw();
  1239. zoom_minus->set_disabled(zoom == zoom_min);
  1240. zoom_plus->set_disabled(zoom == zoom_max);
  1241. _update_scroll();
  1242. minimap->queue_redraw();
  1243. connections_layer->queue_redraw();
  1244. if (is_visible_in_tree()) {
  1245. Vector2 ofs = sbofs * zoom - p_center;
  1246. h_scroll->set_value(ofs.x);
  1247. v_scroll->set_value(ofs.y);
  1248. }
  1249. _update_zoom_label();
  1250. queue_redraw();
  1251. }
  1252. float GraphEdit::get_zoom() const {
  1253. return zoom;
  1254. }
  1255. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1256. p_zoom_step = abs(p_zoom_step);
  1257. if (zoom_step == p_zoom_step) {
  1258. return;
  1259. }
  1260. zoom_step = p_zoom_step;
  1261. }
  1262. float GraphEdit::get_zoom_step() const {
  1263. return zoom_step;
  1264. }
  1265. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1266. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1267. if (zoom_min == p_zoom_min) {
  1268. return;
  1269. }
  1270. zoom_min = p_zoom_min;
  1271. set_zoom(zoom);
  1272. }
  1273. float GraphEdit::get_zoom_min() const {
  1274. return zoom_min;
  1275. }
  1276. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1277. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1278. if (zoom_max == p_zoom_max) {
  1279. return;
  1280. }
  1281. zoom_max = p_zoom_max;
  1282. set_zoom(zoom);
  1283. }
  1284. float GraphEdit::get_zoom_max() const {
  1285. return zoom_max;
  1286. }
  1287. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1288. if (zoom_label->is_visible() == p_enable) {
  1289. return;
  1290. }
  1291. zoom_label->set_visible(p_enable);
  1292. }
  1293. bool GraphEdit::is_showing_zoom_label() const {
  1294. return zoom_label->is_visible();
  1295. }
  1296. void GraphEdit::set_right_disconnects(bool p_enable) {
  1297. right_disconnects = p_enable;
  1298. }
  1299. bool GraphEdit::is_right_disconnects_enabled() const {
  1300. return right_disconnects;
  1301. }
  1302. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1303. valid_right_disconnect_types.insert(p_type);
  1304. }
  1305. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1306. valid_right_disconnect_types.erase(p_type);
  1307. }
  1308. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1309. valid_left_disconnect_types.insert(p_type);
  1310. }
  1311. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1312. valid_left_disconnect_types.erase(p_type);
  1313. }
  1314. TypedArray<Dictionary> GraphEdit::_get_connection_list() const {
  1315. List<Connection> conns;
  1316. get_connection_list(&conns);
  1317. TypedArray<Dictionary> arr;
  1318. for (const Connection &E : conns) {
  1319. Dictionary d;
  1320. d["from"] = E.from;
  1321. d["from_port"] = E.from_port;
  1322. d["to"] = E.to;
  1323. d["to_port"] = E.to_port;
  1324. arr.push_back(d);
  1325. }
  1326. return arr;
  1327. }
  1328. void GraphEdit::_zoom_minus() {
  1329. set_zoom(zoom / zoom_step);
  1330. }
  1331. void GraphEdit::_zoom_reset() {
  1332. set_zoom(1);
  1333. }
  1334. void GraphEdit::_zoom_plus() {
  1335. set_zoom(zoom * zoom_step);
  1336. }
  1337. void GraphEdit::_update_zoom_label() {
  1338. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1339. String zoom_text = itos(zoom_percent) + "%";
  1340. zoom_label->set_text(zoom_text);
  1341. }
  1342. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1343. ConnType ct(p_type, p_with_type);
  1344. valid_connection_types.insert(ct);
  1345. }
  1346. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1347. ConnType ct(p_type, p_with_type);
  1348. valid_connection_types.erase(ct);
  1349. }
  1350. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1351. ConnType ct(p_type, p_with_type);
  1352. return valid_connection_types.has(ct);
  1353. }
  1354. void GraphEdit::set_use_snap(bool p_enable) {
  1355. if (snap_button->is_pressed() == p_enable) {
  1356. return;
  1357. }
  1358. snap_button->set_pressed(p_enable);
  1359. queue_redraw();
  1360. }
  1361. bool GraphEdit::is_using_snap() const {
  1362. return snap_button->is_pressed();
  1363. }
  1364. int GraphEdit::get_snap() const {
  1365. return snap_amount->get_value();
  1366. }
  1367. void GraphEdit::set_snap(int p_snap) {
  1368. ERR_FAIL_COND(p_snap < 5);
  1369. snap_amount->set_value(p_snap);
  1370. queue_redraw();
  1371. }
  1372. void GraphEdit::_snap_toggled() {
  1373. queue_redraw();
  1374. }
  1375. void GraphEdit::_snap_value_changed(double) {
  1376. queue_redraw();
  1377. }
  1378. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1379. minimap->set_size(p_size);
  1380. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1381. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1382. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1383. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1384. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1385. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1386. minimap->queue_redraw();
  1387. }
  1388. Vector2 GraphEdit::get_minimap_size() const {
  1389. return minimap->get_size();
  1390. }
  1391. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1392. if (minimap->get_modulate().a == p_opacity) {
  1393. return;
  1394. }
  1395. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1396. minimap->queue_redraw();
  1397. }
  1398. float GraphEdit::get_minimap_opacity() const {
  1399. Color minimap_modulate = minimap->get_modulate();
  1400. return minimap_modulate.a;
  1401. }
  1402. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1403. if (minimap_button->is_pressed() == p_enable) {
  1404. return;
  1405. }
  1406. minimap_button->set_pressed(p_enable);
  1407. _minimap_toggled();
  1408. minimap->queue_redraw();
  1409. }
  1410. bool GraphEdit::is_minimap_enabled() const {
  1411. return minimap_button->is_pressed();
  1412. }
  1413. void GraphEdit::set_arrange_nodes_button_hidden(bool p_enable) {
  1414. arrange_nodes_button_hidden = p_enable;
  1415. if (arrange_nodes_button_hidden) {
  1416. layout_button->hide();
  1417. } else {
  1418. layout_button->show();
  1419. }
  1420. }
  1421. bool GraphEdit::is_arrange_nodes_button_hidden() const {
  1422. return arrange_nodes_button_hidden;
  1423. }
  1424. void GraphEdit::_minimap_toggled() {
  1425. if (is_minimap_enabled()) {
  1426. minimap->set_visible(true);
  1427. minimap->queue_redraw();
  1428. } else {
  1429. minimap->set_visible(false);
  1430. }
  1431. }
  1432. void GraphEdit::set_connection_lines_curvature(float p_curvature) {
  1433. lines_curvature = p_curvature;
  1434. queue_redraw();
  1435. }
  1436. float GraphEdit::get_connection_lines_curvature() const {
  1437. return lines_curvature;
  1438. }
  1439. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  1440. if (lines_thickness == p_thickness) {
  1441. return;
  1442. }
  1443. lines_thickness = p_thickness;
  1444. queue_redraw();
  1445. }
  1446. float GraphEdit::get_connection_lines_thickness() const {
  1447. return lines_thickness;
  1448. }
  1449. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  1450. if (lines_antialiased == p_antialiased) {
  1451. return;
  1452. }
  1453. lines_antialiased = p_antialiased;
  1454. queue_redraw();
  1455. }
  1456. bool GraphEdit::is_connection_lines_antialiased() const {
  1457. return lines_antialiased;
  1458. }
  1459. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1460. return zoom_hb;
  1461. }
  1462. Ref<ViewPanner> GraphEdit::get_panner() {
  1463. return panner;
  1464. }
  1465. void GraphEdit::set_warped_panning(bool p_warped) {
  1466. warped_panning = p_warped;
  1467. }
  1468. int GraphEdit::_set_operations(SET_OPERATIONS p_operation, HashSet<StringName> &r_u, const HashSet<StringName> &r_v) {
  1469. switch (p_operation) {
  1470. case GraphEdit::IS_EQUAL: {
  1471. for (const StringName &E : r_u) {
  1472. if (!r_v.has(E)) {
  1473. return 0;
  1474. }
  1475. }
  1476. return r_u.size() == r_v.size();
  1477. } break;
  1478. case GraphEdit::IS_SUBSET: {
  1479. if (r_u.size() == r_v.size() && !r_u.size()) {
  1480. return 1;
  1481. }
  1482. for (const StringName &E : r_u) {
  1483. if (!r_v.has(E)) {
  1484. return 0;
  1485. }
  1486. }
  1487. return 1;
  1488. } break;
  1489. case GraphEdit::DIFFERENCE: {
  1490. for (HashSet<StringName>::Iterator E = r_u.begin(); E;) {
  1491. HashSet<StringName>::Iterator N = E;
  1492. ++N;
  1493. if (r_v.has(*E)) {
  1494. r_u.remove(E);
  1495. }
  1496. E = N;
  1497. }
  1498. return r_u.size();
  1499. } break;
  1500. case GraphEdit::UNION: {
  1501. for (const StringName &E : r_v) {
  1502. if (!r_u.has(E)) {
  1503. r_u.insert(E);
  1504. }
  1505. }
  1506. return r_u.size();
  1507. } break;
  1508. default:
  1509. break;
  1510. }
  1511. return -1;
  1512. }
  1513. HashMap<int, Vector<StringName>> GraphEdit::_layering(const HashSet<StringName> &r_selected_nodes, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours) {
  1514. HashMap<int, Vector<StringName>> l;
  1515. HashSet<StringName> p = r_selected_nodes, q = r_selected_nodes, u, z;
  1516. int current_layer = 0;
  1517. bool selected = false;
  1518. while (!_set_operations(GraphEdit::IS_EQUAL, q, u)) {
  1519. _set_operations(GraphEdit::DIFFERENCE, p, u);
  1520. for (const StringName &E : p) {
  1521. HashSet<StringName> n = r_upper_neighbours[E];
  1522. if (_set_operations(GraphEdit::IS_SUBSET, n, z)) {
  1523. Vector<StringName> t;
  1524. t.push_back(E);
  1525. if (!l.has(current_layer)) {
  1526. l.insert(current_layer, Vector<StringName>{});
  1527. }
  1528. selected = true;
  1529. t.append_array(l[current_layer]);
  1530. l.insert(current_layer, t);
  1531. HashSet<StringName> V;
  1532. V.insert(E);
  1533. _set_operations(GraphEdit::UNION, u, V);
  1534. }
  1535. }
  1536. if (!selected) {
  1537. current_layer++;
  1538. uint32_t previous_size_z = z.size();
  1539. _set_operations(GraphEdit::UNION, z, u);
  1540. if (z.size() == previous_size_z) {
  1541. WARN_PRINT("Graph contains cycle(s). The cycle(s) will not be rearranged accurately.");
  1542. Vector<StringName> t;
  1543. if (l.has(0)) {
  1544. t.append_array(l[0]);
  1545. }
  1546. for (const StringName &E : p) {
  1547. t.push_back(E);
  1548. }
  1549. l.insert(0, t);
  1550. break;
  1551. }
  1552. }
  1553. selected = false;
  1554. }
  1555. return l;
  1556. }
  1557. Vector<StringName> GraphEdit::_split(const Vector<StringName> &r_layer, const HashMap<StringName, Dictionary> &r_crossings) {
  1558. if (!r_layer.size()) {
  1559. return Vector<StringName>();
  1560. }
  1561. StringName p = r_layer[Math::random(0, r_layer.size() - 1)];
  1562. Vector<StringName> left;
  1563. Vector<StringName> right;
  1564. for (int i = 0; i < r_layer.size(); i++) {
  1565. if (p != r_layer[i]) {
  1566. StringName q = r_layer[i];
  1567. int cross_pq = r_crossings[p][q];
  1568. int cross_qp = r_crossings[q][p];
  1569. if (cross_pq > cross_qp) {
  1570. left.push_back(q);
  1571. } else {
  1572. right.push_back(q);
  1573. }
  1574. }
  1575. }
  1576. left.push_back(p);
  1577. left.append_array(right);
  1578. return left;
  1579. }
  1580. void GraphEdit::_horizontal_alignment(Dictionary &r_root, Dictionary &r_align, const HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours, const HashSet<StringName> &r_selected_nodes) {
  1581. for (const StringName &E : r_selected_nodes) {
  1582. r_root[E] = E;
  1583. r_align[E] = E;
  1584. }
  1585. if (r_layers.size() == 1) {
  1586. return;
  1587. }
  1588. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1589. Vector<StringName> lower_layer = r_layers[i];
  1590. Vector<StringName> upper_layer = r_layers[i - 1];
  1591. int r = -1;
  1592. for (int j = 0; j < lower_layer.size(); j++) {
  1593. Vector<Pair<int, StringName>> up;
  1594. StringName current_node = lower_layer[j];
  1595. for (int k = 0; k < upper_layer.size(); k++) {
  1596. StringName adjacent_neighbour = upper_layer[k];
  1597. if (r_upper_neighbours[current_node].has(adjacent_neighbour)) {
  1598. up.push_back(Pair<int, StringName>(k, adjacent_neighbour));
  1599. }
  1600. }
  1601. int start = (up.size() - 1) / 2;
  1602. int end = (up.size() - 1) % 2 ? start + 1 : start;
  1603. for (int p = start; p <= end; p++) {
  1604. StringName Align = r_align[current_node];
  1605. if (Align == current_node && r < up[p].first) {
  1606. r_align[up[p].second] = lower_layer[j];
  1607. r_root[current_node] = r_root[up[p].second];
  1608. r_align[current_node] = r_root[up[p].second];
  1609. r = up[p].first;
  1610. }
  1611. }
  1612. }
  1613. }
  1614. }
  1615. void GraphEdit::_crossing_minimisation(HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours) {
  1616. if (r_layers.size() == 1) {
  1617. return;
  1618. }
  1619. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1620. Vector<StringName> upper_layer = r_layers[i - 1];
  1621. Vector<StringName> lower_layer = r_layers[i];
  1622. HashMap<StringName, Dictionary> c;
  1623. for (int j = 0; j < lower_layer.size(); j++) {
  1624. StringName p = lower_layer[j];
  1625. Dictionary d;
  1626. for (int k = 0; k < lower_layer.size(); k++) {
  1627. unsigned int crossings = 0;
  1628. StringName q = lower_layer[k];
  1629. if (j != k) {
  1630. for (int h = 1; h < upper_layer.size(); h++) {
  1631. if (r_upper_neighbours[p].has(upper_layer[h])) {
  1632. for (int g = 0; g < h; g++) {
  1633. if (r_upper_neighbours[q].has(upper_layer[g])) {
  1634. crossings++;
  1635. }
  1636. }
  1637. }
  1638. }
  1639. }
  1640. d[q] = crossings;
  1641. }
  1642. c.insert(p, d);
  1643. }
  1644. r_layers.insert(i, _split(lower_layer, c));
  1645. }
  1646. }
  1647. void GraphEdit::_calculate_inner_shifts(Dictionary &r_inner_shifts, const Dictionary &r_root, const Dictionary &r_node_names, const Dictionary &r_align, const HashSet<StringName> &r_block_heads, const HashMap<StringName, Pair<int, int>> &r_port_info) {
  1648. for (const StringName &E : r_block_heads) {
  1649. real_t left = 0;
  1650. StringName u = E;
  1651. StringName v = r_align[u];
  1652. while (u != v && (StringName)r_root[u] != v) {
  1653. String _connection = String(u) + " " + String(v);
  1654. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[u]);
  1655. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[v]);
  1656. Pair<int, int> ports = r_port_info[_connection];
  1657. int pfrom = ports.first;
  1658. int pto = ports.second;
  1659. Vector2 frompos = gfrom->get_connection_output_position(pfrom);
  1660. Vector2 topos = gto->get_connection_input_position(pto);
  1661. real_t s = (real_t)r_inner_shifts[u] + (frompos.y - topos.y) / zoom;
  1662. r_inner_shifts[v] = s;
  1663. left = MIN(left, s);
  1664. u = v;
  1665. v = (StringName)r_align[v];
  1666. }
  1667. u = E;
  1668. do {
  1669. r_inner_shifts[u] = (real_t)r_inner_shifts[u] - left;
  1670. u = (StringName)r_align[u];
  1671. } while (u != E);
  1672. }
  1673. }
  1674. float GraphEdit::_calculate_threshold(StringName p_v, StringName p_w, const Dictionary &r_node_names, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_inner_shift, real_t p_current_threshold, const HashMap<StringName, Vector2> &r_node_positions) {
  1675. #define MAX_ORDER 2147483647
  1676. #define ORDER(node, layers) \
  1677. for (unsigned int i = 0; i < layers.size(); i++) { \
  1678. int index = layers[i].find(node); \
  1679. if (index > 0) { \
  1680. order = index; \
  1681. break; \
  1682. } \
  1683. order = MAX_ORDER; \
  1684. }
  1685. int order = MAX_ORDER;
  1686. float threshold = p_current_threshold;
  1687. if (p_v == p_w) {
  1688. int min_order = MAX_ORDER;
  1689. Connection incoming;
  1690. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1691. if (E->get().to == p_w) {
  1692. ORDER(E->get().from, r_layers);
  1693. if (min_order > order) {
  1694. min_order = order;
  1695. incoming = E->get();
  1696. }
  1697. }
  1698. }
  1699. if (incoming.from != StringName()) {
  1700. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[incoming.from]);
  1701. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1702. Vector2 frompos = gfrom->get_connection_output_position(incoming.from_port);
  1703. Vector2 topos = gto->get_connection_input_position(incoming.to_port);
  1704. //If connected block node is selected, calculate thershold or add current block to list
  1705. if (gfrom->is_selected()) {
  1706. Vector2 connected_block_pos = r_node_positions[r_root[incoming.from]];
  1707. if (connected_block_pos.y != FLT_MAX) {
  1708. //Connected block is placed. Calculate threshold
  1709. threshold = connected_block_pos.y + (real_t)r_inner_shift[incoming.from] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1710. }
  1711. }
  1712. }
  1713. }
  1714. if (threshold == FLT_MIN && (StringName)r_align[p_w] == p_v) {
  1715. //This time, pick an outgoing edge and repeat as above!
  1716. int min_order = MAX_ORDER;
  1717. Connection outgoing;
  1718. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1719. if (E->get().from == p_w) {
  1720. ORDER(E->get().to, r_layers);
  1721. if (min_order > order) {
  1722. min_order = order;
  1723. outgoing = E->get();
  1724. }
  1725. }
  1726. }
  1727. if (outgoing.to != StringName()) {
  1728. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1729. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[outgoing.to]);
  1730. Vector2 frompos = gfrom->get_connection_output_position(outgoing.from_port);
  1731. Vector2 topos = gto->get_connection_input_position(outgoing.to_port);
  1732. //If connected block node is selected, calculate thershold or add current block to list
  1733. if (gto->is_selected()) {
  1734. Vector2 connected_block_pos = r_node_positions[r_root[outgoing.to]];
  1735. if (connected_block_pos.y != FLT_MAX) {
  1736. //Connected block is placed. Calculate threshold
  1737. threshold = connected_block_pos.y + (real_t)r_inner_shift[outgoing.to] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1738. }
  1739. }
  1740. }
  1741. }
  1742. #undef MAX_ORDER
  1743. #undef ORDER
  1744. return threshold;
  1745. }
  1746. void GraphEdit::_place_block(StringName p_v, float p_delta, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_node_name, const Dictionary &r_inner_shift, Dictionary &r_sink, Dictionary &r_shift, HashMap<StringName, Vector2> &r_node_positions) {
  1747. #define PRED(node, layers) \
  1748. for (unsigned int i = 0; i < layers.size(); i++) { \
  1749. int index = layers[i].find(node); \
  1750. if (index > 0) { \
  1751. predecessor = layers[i][index - 1]; \
  1752. break; \
  1753. } \
  1754. predecessor = StringName(); \
  1755. }
  1756. StringName predecessor;
  1757. StringName successor;
  1758. Vector2 pos = r_node_positions[p_v];
  1759. if (pos.y == FLT_MAX) {
  1760. pos.y = 0;
  1761. bool initial = false;
  1762. StringName w = p_v;
  1763. real_t threshold = FLT_MIN;
  1764. do {
  1765. PRED(w, r_layers);
  1766. if (predecessor != StringName()) {
  1767. StringName u = r_root[predecessor];
  1768. _place_block(u, p_delta, r_layers, r_root, r_align, r_node_name, r_inner_shift, r_sink, r_shift, r_node_positions);
  1769. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1770. if ((StringName)r_sink[p_v] == p_v) {
  1771. r_sink[p_v] = r_sink[u];
  1772. }
  1773. Vector2 predecessor_root_pos = r_node_positions[u];
  1774. Vector2 predecessor_node_size = Object::cast_to<GraphNode>(r_node_name[predecessor])->get_size();
  1775. if (r_sink[p_v] != r_sink[u]) {
  1776. real_t sc = pos.y + (real_t)r_inner_shift[w] - predecessor_root_pos.y - (real_t)r_inner_shift[predecessor] - predecessor_node_size.y - p_delta;
  1777. r_shift[r_sink[u]] = MIN(sc, (real_t)r_shift[r_sink[u]]);
  1778. } else {
  1779. real_t sb = predecessor_root_pos.y + (real_t)r_inner_shift[predecessor] + predecessor_node_size.y - (real_t)r_inner_shift[w] + p_delta;
  1780. sb = MAX(sb, threshold);
  1781. if (initial) {
  1782. pos.y = sb;
  1783. } else {
  1784. pos.y = MAX(pos.y, sb);
  1785. }
  1786. initial = false;
  1787. }
  1788. }
  1789. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1790. w = r_align[w];
  1791. } while (w != p_v);
  1792. r_node_positions.insert(p_v, pos);
  1793. }
  1794. #undef PRED
  1795. }
  1796. void GraphEdit::arrange_nodes() {
  1797. if (!arranging_graph) {
  1798. arranging_graph = true;
  1799. } else {
  1800. return;
  1801. }
  1802. Dictionary node_names;
  1803. HashSet<StringName> selected_nodes;
  1804. bool arrange_entire_graph = true;
  1805. for (int i = get_child_count() - 1; i >= 0; i--) {
  1806. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1807. if (!gn) {
  1808. continue;
  1809. }
  1810. node_names[gn->get_name()] = gn;
  1811. if (gn->is_selected()) {
  1812. arrange_entire_graph = false;
  1813. }
  1814. }
  1815. HashMap<StringName, HashSet<StringName>> upper_neighbours;
  1816. HashMap<StringName, Pair<int, int>> port_info;
  1817. Vector2 origin(FLT_MAX, FLT_MAX);
  1818. float gap_v = 100.0f;
  1819. float gap_h = 100.0f;
  1820. for (int i = get_child_count() - 1; i >= 0; i--) {
  1821. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1822. if (!gn) {
  1823. continue;
  1824. }
  1825. if (gn->is_selected() || arrange_entire_graph) {
  1826. selected_nodes.insert(gn->get_name());
  1827. HashSet<StringName> s;
  1828. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1829. GraphNode *p_from = Object::cast_to<GraphNode>(node_names[E->get().from]);
  1830. if (E->get().to == gn->get_name() && (p_from->is_selected() || arrange_entire_graph) && E->get().to != E->get().from) {
  1831. if (!s.has(p_from->get_name())) {
  1832. s.insert(p_from->get_name());
  1833. }
  1834. String s_connection = String(p_from->get_name()) + " " + String(E->get().to);
  1835. StringName _connection(s_connection);
  1836. Pair<int, int> ports(E->get().from_port, E->get().to_port);
  1837. if (port_info.has(_connection)) {
  1838. Pair<int, int> p_ports = port_info[_connection];
  1839. if (p_ports.first < ports.first) {
  1840. ports = p_ports;
  1841. }
  1842. }
  1843. port_info.insert(_connection, ports);
  1844. }
  1845. }
  1846. upper_neighbours.insert(gn->get_name(), s);
  1847. }
  1848. }
  1849. if (!selected_nodes.size()) {
  1850. arranging_graph = false;
  1851. return;
  1852. }
  1853. HashMap<int, Vector<StringName>> layers = _layering(selected_nodes, upper_neighbours);
  1854. _crossing_minimisation(layers, upper_neighbours);
  1855. Dictionary root, align, sink, shift;
  1856. _horizontal_alignment(root, align, layers, upper_neighbours, selected_nodes);
  1857. HashMap<StringName, Vector2> new_positions;
  1858. Vector2 default_position(FLT_MAX, FLT_MAX);
  1859. Dictionary inner_shift;
  1860. HashSet<StringName> block_heads;
  1861. for (const StringName &E : selected_nodes) {
  1862. inner_shift[E] = 0.0f;
  1863. sink[E] = E;
  1864. shift[E] = FLT_MAX;
  1865. new_positions.insert(E, default_position);
  1866. if ((StringName)root[E] == E) {
  1867. block_heads.insert(E);
  1868. }
  1869. }
  1870. _calculate_inner_shifts(inner_shift, root, node_names, align, block_heads, port_info);
  1871. for (const StringName &E : block_heads) {
  1872. _place_block(E, gap_v, layers, root, align, node_names, inner_shift, sink, shift, new_positions);
  1873. }
  1874. origin.y = Object::cast_to<GraphNode>(node_names[layers[0][0]])->get_position_offset().y - (new_positions[layers[0][0]].y + (float)inner_shift[layers[0][0]]);
  1875. origin.x = Object::cast_to<GraphNode>(node_names[layers[0][0]])->get_position_offset().x;
  1876. for (const StringName &E : block_heads) {
  1877. StringName u = E;
  1878. float start_from = origin.y + new_positions[E].y;
  1879. do {
  1880. Vector2 cal_pos;
  1881. cal_pos.y = start_from + (real_t)inner_shift[u];
  1882. new_positions.insert(u, cal_pos);
  1883. u = align[u];
  1884. } while (u != E);
  1885. }
  1886. // Compute horizontal coordinates individually for layers to get uniform gap.
  1887. float start_from = origin.x;
  1888. float largest_node_size = 0.0f;
  1889. for (unsigned int i = 0; i < layers.size(); i++) {
  1890. Vector<StringName> layer = layers[i];
  1891. for (int j = 0; j < layer.size(); j++) {
  1892. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1893. largest_node_size = MAX(largest_node_size, current_node_size);
  1894. }
  1895. for (int j = 0; j < layer.size(); j++) {
  1896. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1897. Vector2 cal_pos = new_positions[layer[j]];
  1898. if (current_node_size == largest_node_size) {
  1899. cal_pos.x = start_from;
  1900. } else {
  1901. float current_node_start_pos = start_from;
  1902. if (current_node_size < largest_node_size / 2) {
  1903. if (!(i || j)) {
  1904. start_from -= (largest_node_size - current_node_size);
  1905. }
  1906. current_node_start_pos = start_from + largest_node_size - current_node_size;
  1907. }
  1908. cal_pos.x = current_node_start_pos;
  1909. }
  1910. new_positions.insert(layer[j], cal_pos);
  1911. }
  1912. start_from += largest_node_size + gap_h;
  1913. largest_node_size = 0.0f;
  1914. }
  1915. emit_signal(SNAME("begin_node_move"));
  1916. for (const StringName &E : selected_nodes) {
  1917. GraphNode *gn = Object::cast_to<GraphNode>(node_names[E]);
  1918. gn->set_drag(true);
  1919. Vector2 pos = (new_positions[E]);
  1920. if (is_using_snap()) {
  1921. const int snap = get_snap();
  1922. pos = pos.snapped(Vector2(snap, snap));
  1923. }
  1924. gn->set_position_offset(pos);
  1925. gn->set_drag(false);
  1926. }
  1927. emit_signal(SNAME("end_node_move"));
  1928. arranging_graph = false;
  1929. }
  1930. void GraphEdit::_bind_methods() {
  1931. ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::connect_node);
  1932. ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
  1933. ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
  1934. ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1935. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1936. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1937. ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end);
  1938. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1939. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "offset"), &GraphEdit::set_scroll_ofs);
  1940. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1941. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1942. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1943. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1944. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1945. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1946. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1947. ClassDB::bind_method(D_METHOD("get_connection_line", "from_node", "to_node"), &GraphEdit::get_connection_line);
  1948. ClassDB::bind_method(D_METHOD("set_panning_scheme", "scheme"), &GraphEdit::set_panning_scheme);
  1949. ClassDB::bind_method(D_METHOD("get_panning_scheme"), &GraphEdit::get_panning_scheme);
  1950. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  1951. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1952. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1953. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1954. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1955. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1956. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1957. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1958. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1959. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1960. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1961. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1962. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1963. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1964. ClassDB::bind_method(D_METHOD("set_connection_lines_curvature", "curvature"), &GraphEdit::set_connection_lines_curvature);
  1965. ClassDB::bind_method(D_METHOD("get_connection_lines_curvature"), &GraphEdit::get_connection_lines_curvature);
  1966. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  1967. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  1968. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  1969. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  1970. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  1971. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1972. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  1973. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1974. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1975. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1976. ClassDB::bind_method(D_METHOD("set_arrange_nodes_button_hidden", "enable"), &GraphEdit::set_arrange_nodes_button_hidden);
  1977. ClassDB::bind_method(D_METHOD("is_arrange_nodes_button_hidden"), &GraphEdit::is_arrange_nodes_button_hidden);
  1978. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1979. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1980. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1981. GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node", "in_port", "mouse_position");
  1982. GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node", "in_port", "mouse_position");
  1983. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1984. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  1985. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1986. GDVIRTUAL_BIND(_get_connection_line, "from_position", "to_position")
  1987. GDVIRTUAL_BIND(_is_node_hover_valid, "from_node", "from_port", "to_node", "to_port");
  1988. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1989. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_ofs", "get_scroll_ofs");
  1990. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snap", "get_snap");
  1991. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1992. ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
  1993. ADD_GROUP("Connection Lines", "connection_lines");
  1994. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
  1995. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness", PROPERTY_HINT_NONE, "suffix:px"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  1996. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  1997. ADD_GROUP("Zoom", "");
  1998. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1999. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  2000. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  2001. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  2002. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  2003. ADD_GROUP("Minimap", "minimap_");
  2004. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  2005. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_size", "get_minimap_size");
  2006. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  2007. ADD_GROUP("UI", "");
  2008. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arrange_nodes_button_hidden"), "set_arrange_nodes_button_hidden", "is_arrange_nodes_button_hidden");
  2009. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  2010. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  2011. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  2012. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  2013. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  2014. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  2015. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2016. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2017. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2018. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2019. ADD_SIGNAL(MethodInfo("delete_nodes_request", PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_ARRAY_TYPE, "StringName")));
  2020. ADD_SIGNAL(MethodInfo("begin_node_move"));
  2021. ADD_SIGNAL(MethodInfo("end_node_move"));
  2022. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "offset")));
  2023. ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output")));
  2024. ADD_SIGNAL(MethodInfo("connection_drag_ended"));
  2025. BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
  2026. BIND_ENUM_CONSTANT(SCROLL_PANS);
  2027. }
  2028. GraphEdit::GraphEdit() {
  2029. set_focus_mode(FOCUS_ALL);
  2030. // Allow dezooming 8 times from the default zoom level.
  2031. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  2032. // but this is still useful for previewing and navigation.
  2033. zoom_min = (1 / Math::pow(zoom_step, 8));
  2034. // Allow zooming 4 times from the default zoom level.
  2035. zoom_max = (1 * Math::pow(zoom_step, 4));
  2036. panner.instantiate();
  2037. panner->set_callbacks(callable_mp(this, &GraphEdit::_scroll_callback), callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback));
  2038. top_layer = memnew(GraphEditFilter(this));
  2039. add_child(top_layer, false, INTERNAL_MODE_BACK);
  2040. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  2041. top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2042. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  2043. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  2044. top_layer->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  2045. connections_layer = memnew(Control);
  2046. add_child(connections_layer, false, INTERNAL_MODE_FRONT);
  2047. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  2048. connections_layer->set_name("CLAYER");
  2049. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  2050. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2051. h_scroll = memnew(HScrollBar);
  2052. h_scroll->set_name("_h_scroll");
  2053. top_layer->add_child(h_scroll);
  2054. v_scroll = memnew(VScrollBar);
  2055. v_scroll->set_name("_v_scroll");
  2056. top_layer->add_child(v_scroll);
  2057. //set large minmax so it can scroll even if not resized yet
  2058. h_scroll->set_min(-10000);
  2059. h_scroll->set_max(10000);
  2060. v_scroll->set_min(-10000);
  2061. v_scroll->set_max(10000);
  2062. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2063. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2064. zoom_hb = memnew(HBoxContainer);
  2065. top_layer->add_child(zoom_hb);
  2066. zoom_hb->set_position(Vector2(10, 10));
  2067. zoom_label = memnew(Label);
  2068. zoom_hb->add_child(zoom_label);
  2069. zoom_label->set_visible(false);
  2070. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  2071. zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2072. zoom_label->set_custom_minimum_size(Size2(48, 0));
  2073. _update_zoom_label();
  2074. zoom_minus = memnew(Button);
  2075. zoom_minus->set_flat(true);
  2076. zoom_hb->add_child(zoom_minus);
  2077. zoom_minus->set_tooltip_text(RTR("Zoom Out"));
  2078. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  2079. zoom_minus->set_focus_mode(FOCUS_NONE);
  2080. zoom_reset = memnew(Button);
  2081. zoom_reset->set_flat(true);
  2082. zoom_hb->add_child(zoom_reset);
  2083. zoom_reset->set_tooltip_text(RTR("Zoom Reset"));
  2084. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  2085. zoom_reset->set_focus_mode(FOCUS_NONE);
  2086. zoom_plus = memnew(Button);
  2087. zoom_plus->set_flat(true);
  2088. zoom_hb->add_child(zoom_plus);
  2089. zoom_plus->set_tooltip_text(RTR("Zoom In"));
  2090. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  2091. zoom_plus->set_focus_mode(FOCUS_NONE);
  2092. snap_button = memnew(Button);
  2093. snap_button->set_flat(true);
  2094. snap_button->set_toggle_mode(true);
  2095. snap_button->set_tooltip_text(RTR("Enable snap and show grid."));
  2096. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  2097. snap_button->set_pressed(true);
  2098. snap_button->set_focus_mode(FOCUS_NONE);
  2099. zoom_hb->add_child(snap_button);
  2100. snap_amount = memnew(SpinBox);
  2101. snap_amount->set_min(5);
  2102. snap_amount->set_max(100);
  2103. snap_amount->set_step(1);
  2104. snap_amount->set_value(20);
  2105. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  2106. zoom_hb->add_child(snap_amount);
  2107. minimap_button = memnew(Button);
  2108. minimap_button->set_flat(true);
  2109. minimap_button->set_toggle_mode(true);
  2110. minimap_button->set_tooltip_text(RTR("Enable grid minimap."));
  2111. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  2112. minimap_button->set_pressed(true);
  2113. minimap_button->set_focus_mode(FOCUS_NONE);
  2114. zoom_hb->add_child(minimap_button);
  2115. layout_button = memnew(Button);
  2116. layout_button->set_flat(true);
  2117. zoom_hb->add_child(layout_button);
  2118. layout_button->set_tooltip_text(RTR("Arrange nodes."));
  2119. layout_button->connect("pressed", callable_mp(this, &GraphEdit::arrange_nodes));
  2120. layout_button->set_focus_mode(FOCUS_NONE);
  2121. Vector2 minimap_size = Vector2(240, 160);
  2122. float minimap_opacity = 0.65;
  2123. minimap = memnew(GraphEditMinimap(this));
  2124. top_layer->add_child(minimap);
  2125. minimap->set_name("_minimap");
  2126. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  2127. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  2128. minimap->set_custom_minimum_size(Vector2(50, 50));
  2129. minimap->set_size(minimap_size);
  2130. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  2131. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  2132. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  2133. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2134. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2135. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  2136. set_clip_contents(true);
  2137. }