graph_edit.cpp 81 KB

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