graph_edit.cpp 83 KB

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