graph_edit.cpp 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. /**************************************************************************/
  2. /* graph_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "graph_edit.h"
  31. #include "graph_edit.compat.inc"
  32. #include "core/input/input.h"
  33. #include "core/math/geometry_2d.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/os/keyboard.h"
  36. #include "scene/2d/line_2d.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/button.h"
  39. #include "scene/gui/graph_edit_arranger.h"
  40. #include "scene/gui/label.h"
  41. #include "scene/gui/panel_container.h"
  42. #include "scene/gui/scroll_bar.h"
  43. #include "scene/gui/spin_box.h"
  44. #include "scene/gui/view_panner.h"
  45. #include "scene/resources/style_box_flat.h"
  46. #include "scene/theme/theme_db.h"
  47. constexpr int MINIMAP_OFFSET = 12;
  48. constexpr int MINIMAP_PADDING = 5;
  49. constexpr int MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION = 20;
  50. constexpr int MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES = 5;
  51. constexpr int GRID_MINOR_STEPS_PER_MAJOR_LINE = 10;
  52. constexpr int GRID_MINOR_STEPS_PER_MAJOR_DOT = 5;
  53. constexpr int GRID_MIN_SNAPPING_DISTANCE = 2;
  54. constexpr int GRID_MAX_SNAPPING_DISTANCE = 100;
  55. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  56. return ge->_filter_input(p_point);
  57. }
  58. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  59. ge = p_edit;
  60. }
  61. Control::CursorShape GraphEditMinimap::get_cursor_shape(const Point2 &p_pos) const {
  62. if (is_resizing || (p_pos.x < theme_cache.resizer->get_width() && p_pos.y < theme_cache.resizer->get_height())) {
  63. return CURSOR_FDIAGSIZE;
  64. }
  65. return Control::get_cursor_shape(p_pos);
  66. }
  67. void GraphEditMinimap::update_minimap() {
  68. Vector2 graph_offset = _get_graph_offset();
  69. Vector2 graph_size = _get_graph_size();
  70. camera_position = ge->get_scroll_offset() - graph_offset;
  71. camera_size = ge->get_size();
  72. Vector2 render_size = _get_render_size();
  73. float target_ratio = render_size.width / render_size.height;
  74. float graph_ratio = graph_size.width / graph_size.height;
  75. graph_proportions = graph_size;
  76. graph_padding = Vector2(0, 0);
  77. if (graph_ratio > target_ratio) {
  78. graph_proportions.width = graph_size.width;
  79. graph_proportions.height = graph_size.width / target_ratio;
  80. graph_padding.y = Math::abs(graph_size.height - graph_proportions.y) / 2;
  81. } else {
  82. graph_proportions.width = graph_size.height * target_ratio;
  83. graph_proportions.height = graph_size.height;
  84. graph_padding.x = Math::abs(graph_size.width - graph_proportions.x) / 2;
  85. }
  86. // This centers minimap inside the minimap rectangle.
  87. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  88. }
  89. Rect2 GraphEditMinimap::get_camera_rect() {
  90. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  91. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  92. Vector2 camera_pos = (camera_center - camera_viewport / 2);
  93. return Rect2(camera_pos, camera_viewport);
  94. }
  95. Vector2 GraphEditMinimap::_get_render_size() {
  96. if (!is_inside_tree()) {
  97. return Vector2(0, 0);
  98. }
  99. return get_size() - 2 * minimap_padding;
  100. }
  101. Vector2 GraphEditMinimap::_get_graph_offset() {
  102. return Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  103. }
  104. Vector2 GraphEditMinimap::_get_graph_size() {
  105. Vector2 graph_size = Vector2(ge->h_scrollbar->get_max(), ge->v_scrollbar->get_max()) - Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  106. if (graph_size.width == 0) {
  107. graph_size.width = 1;
  108. }
  109. if (graph_size.height == 0) {
  110. graph_size.height = 1;
  111. }
  112. return graph_size;
  113. }
  114. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  115. Vector2 map_position = Vector2(0, 0);
  116. Vector2 render_size = _get_render_size();
  117. map_position.x = p_position.x * render_size.width / graph_proportions.x;
  118. map_position.y = p_position.y * render_size.height / graph_proportions.y;
  119. return map_position;
  120. }
  121. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  122. Vector2 graph_position = Vector2(0, 0);
  123. Vector2 render_size = _get_render_size();
  124. graph_position.x = p_position.x * graph_proportions.x / render_size.width;
  125. graph_position.y = p_position.y * graph_proportions.y / render_size.height;
  126. return graph_position;
  127. }
  128. void GraphEditMinimap::gui_input(const Ref<InputEvent> &p_ev) {
  129. ERR_FAIL_COND(p_ev.is_null());
  130. if (!ge->is_minimap_enabled()) {
  131. return;
  132. }
  133. Ref<InputEventMouseButton> mb = p_ev;
  134. Ref<InputEventMouseMotion> mm = p_ev;
  135. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  136. if (mb->is_pressed()) {
  137. is_pressing = true;
  138. Rect2 resizer_hitbox = Rect2(Point2(), theme_cache.resizer->get_size());
  139. if (resizer_hitbox.has_point(mb->get_position())) {
  140. is_resizing = true;
  141. } else {
  142. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  143. _adjust_graph_scroll(click_position);
  144. }
  145. } else {
  146. is_pressing = false;
  147. is_resizing = false;
  148. }
  149. accept_event();
  150. } else if (mm.is_valid() && is_pressing) {
  151. if (is_resizing) {
  152. // Prevent setting minimap wider than GraphEdit.
  153. Vector2 new_minimap_size;
  154. new_minimap_size = (get_size() - mm->get_relative()).min(ge->get_size() - 2.0 * minimap_padding);
  155. ge->set_minimap_size(new_minimap_size);
  156. queue_redraw();
  157. } else {
  158. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  159. _adjust_graph_scroll(click_position);
  160. }
  161. accept_event();
  162. }
  163. }
  164. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  165. Vector2 graph_offset = _get_graph_offset();
  166. ge->set_scroll_offset(p_offset + graph_offset - camera_size / 2);
  167. }
  168. void GraphEditMinimap::_bind_methods() {
  169. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, panel);
  170. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, node_style, "node");
  171. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, camera_style, "camera");
  172. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEditMinimap, resizer);
  173. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEditMinimap, resizer_color);
  174. }
  175. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  176. ge = p_edit;
  177. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  178. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  179. }
  180. Ref<Shader> GraphEdit::default_connections_shader;
  181. void GraphEdit::init_shaders() {
  182. default_connections_shader.instantiate();
  183. default_connections_shader->set_code(R"(
  184. // Connection lines shader.
  185. shader_type canvas_item;
  186. render_mode blend_mix;
  187. uniform vec4 rim_color : source_color;
  188. uniform int from_type;
  189. uniform int to_type;
  190. uniform float line_width;
  191. void fragment(){
  192. float fake_aa_width = 1.5/line_width;
  193. float rim_width = 1.5/line_width;
  194. float dist = abs(UV.y - 0.5);
  195. float alpha = smoothstep(0.5, 0.5-fake_aa_width, dist);
  196. vec4 final_color = mix(rim_color, COLOR, smoothstep(0.5-rim_width, 0.5-fake_aa_width-rim_width, dist));
  197. COLOR = vec4(final_color.rgb, final_color.a*alpha);
  198. }
  199. )");
  200. }
  201. void GraphEdit::finish_shaders() {
  202. default_connections_shader.unref();
  203. }
  204. Control::CursorShape GraphEdit::get_cursor_shape(const Point2 &p_pos) const {
  205. if (moving_selection) {
  206. return CURSOR_MOVE;
  207. }
  208. return Control::get_cursor_shape(p_pos);
  209. }
  210. PackedStringArray GraphEdit::get_configuration_warnings() const {
  211. PackedStringArray warnings = Control::get_configuration_warnings();
  212. warnings.push_back(RTR("Please be aware that GraphEdit and GraphNode will undergo extensive refactoring in a future 4.x version involving compatibility-breaking API changes."));
  213. return warnings;
  214. }
  215. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  216. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  217. return OK;
  218. }
  219. Ref<Connection> c;
  220. c.instantiate();
  221. c->from_node = p_from;
  222. c->from_port = p_from_port;
  223. c->to_node = p_to;
  224. c->to_port = p_to_port;
  225. c->activity = 0;
  226. connections.push_back(c);
  227. connection_map[p_from].push_back(c);
  228. connection_map[p_to].push_back(c);
  229. Line2D *line = memnew(Line2D);
  230. line->set_texture_mode(Line2D::LineTextureMode::LINE_TEXTURE_STRETCH);
  231. Ref<ShaderMaterial> line_material;
  232. line_material.instantiate();
  233. line_material->set_shader(connections_shader);
  234. float line_width = _get_shader_line_width();
  235. line_material->set_shader_parameter("line_width", line_width);
  236. line_material->set_shader_parameter("from_type", c->from_port);
  237. line_material->set_shader_parameter("to_type", c->to_port);
  238. Ref<StyleBoxFlat> bg_panel = theme_cache.panel;
  239. Color connection_line_rim_color = bg_panel.is_valid() ? bg_panel->get_bg_color() : Color(0.0, 0.0, 0.0, 0.0);
  240. line_material->set_shader_parameter("rim_color", connection_line_rim_color);
  241. line->set_material(line_material);
  242. connections_layer->add_child(line);
  243. c->_cache.line = line;
  244. minimap->queue_redraw();
  245. queue_redraw();
  246. connections_layer->queue_redraw();
  247. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  248. return OK;
  249. }
  250. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  251. for (const Ref<Connection> &conn : connection_map[p_from]) {
  252. if (conn->from_node == p_from && conn->from_port == p_from_port && conn->to_node == p_to && conn->to_port == p_to_port) {
  253. return true;
  254. }
  255. }
  256. return false;
  257. }
  258. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  259. for (const List<Ref<Connection>>::Element *E = connections.front(); E; E = E->next()) {
  260. if (E->get()->from_node == p_from && E->get()->from_port == p_from_port && E->get()->to_node == p_to && E->get()->to_port == p_to_port) {
  261. connection_map[p_from].erase(E->get());
  262. connection_map[p_to].erase(E->get());
  263. E->get()->_cache.line->queue_free();
  264. connections.erase(E);
  265. minimap->queue_redraw();
  266. queue_redraw();
  267. connections_layer->queue_redraw();
  268. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  269. return;
  270. }
  271. }
  272. }
  273. const List<Ref<GraphEdit::Connection>> &GraphEdit::get_connection_list() const {
  274. return connections;
  275. }
  276. void GraphEdit::set_scroll_offset(const Vector2 &p_offset) {
  277. setting_scroll_offset = true;
  278. h_scrollbar->set_value(p_offset.x);
  279. v_scrollbar->set_value(p_offset.y);
  280. _update_scroll();
  281. setting_scroll_offset = false;
  282. }
  283. Vector2 GraphEdit::get_scroll_offset() const {
  284. return Vector2(h_scrollbar->get_value(), v_scrollbar->get_value());
  285. }
  286. void GraphEdit::_scroll_moved(double) {
  287. if (!awaiting_scroll_offset_update) {
  288. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  289. awaiting_scroll_offset_update = true;
  290. }
  291. minimap->queue_redraw();
  292. queue_redraw();
  293. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  294. }
  295. void GraphEdit::_update_scroll_offset() {
  296. set_block_minimum_size_adjust(true);
  297. for (int i = 0; i < get_child_count(); i++) {
  298. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  299. if (!graph_element) {
  300. continue;
  301. }
  302. Point2 pos = graph_element->get_position_offset() * zoom;
  303. pos -= Point2(h_scrollbar->get_value(), v_scrollbar->get_value());
  304. graph_element->set_position(pos);
  305. if (graph_element->get_scale() != Vector2(zoom, zoom)) {
  306. graph_element->set_scale(Vector2(zoom, zoom));
  307. }
  308. }
  309. connections_layer->set_position(-Point2(h_scrollbar->get_value(), v_scrollbar->get_value()));
  310. set_block_minimum_size_adjust(false);
  311. awaiting_scroll_offset_update = false;
  312. // In Godot, signals on value change are avoided by convention.
  313. if (!setting_scroll_offset) {
  314. emit_signal(SNAME("scroll_offset_changed"), get_scroll_offset());
  315. }
  316. }
  317. void GraphEdit::_update_scroll() {
  318. if (updating) {
  319. return;
  320. }
  321. updating = true;
  322. set_block_minimum_size_adjust(true);
  323. Rect2 screen_rect;
  324. for (int i = 0; i < get_child_count(); i++) {
  325. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  326. if (!graph_element) {
  327. continue;
  328. }
  329. Rect2 node_rect;
  330. node_rect.position = graph_element->get_position_offset() * zoom;
  331. node_rect.size = graph_element->get_size() * zoom;
  332. screen_rect = screen_rect.merge(node_rect);
  333. }
  334. screen_rect.position -= get_size();
  335. screen_rect.size += get_size() * 2.0;
  336. h_scrollbar->set_min(screen_rect.position.x);
  337. h_scrollbar->set_max(screen_rect.position.x + screen_rect.size.width);
  338. h_scrollbar->set_page(get_size().x);
  339. if (h_scrollbar->get_max() - h_scrollbar->get_min() <= h_scrollbar->get_page()) {
  340. h_scrollbar->hide();
  341. } else {
  342. h_scrollbar->show();
  343. }
  344. v_scrollbar->set_min(screen_rect.position.y);
  345. v_scrollbar->set_max(screen_rect.position.y + screen_rect.size.height);
  346. v_scrollbar->set_page(get_size().height);
  347. if (v_scrollbar->get_max() - v_scrollbar->get_min() <= v_scrollbar->get_page()) {
  348. v_scrollbar->hide();
  349. } else {
  350. v_scrollbar->show();
  351. }
  352. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  353. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  354. // Avoid scrollbar overlapping.
  355. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scrollbar->is_visible() ? -vmin.width : 0);
  356. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scrollbar->is_visible() ? -hmin.height : 0);
  357. set_block_minimum_size_adjust(false);
  358. if (!awaiting_scroll_offset_update) {
  359. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  360. awaiting_scroll_offset_update = true;
  361. }
  362. updating = false;
  363. }
  364. void GraphEdit::_ensure_node_order_from(Node *p_node) {
  365. GraphElement *graph_node = Object::cast_to<GraphElement>(p_node);
  366. ERR_FAIL_NULL(graph_node);
  367. GraphFrame *frame = Object::cast_to<GraphFrame>(p_node);
  368. // Move a non-frame node directly to the front.
  369. if (!frame) {
  370. graph_node->move_to_front();
  371. return;
  372. }
  373. // Reorder the frames behind the connection layer.
  374. List<GraphFrame *> attached_nodes_to_move;
  375. attached_nodes_to_move.push_back(frame);
  376. while (!attached_nodes_to_move.is_empty()) {
  377. GraphFrame *attached_frame = attached_nodes_to_move.front()->get();
  378. attached_nodes_to_move.pop_front();
  379. // Move the frame to the front of the background node index range.
  380. attached_frame->get_parent()->call_deferred("move_child", attached_frame, background_nodes_separator_idx - 1);
  381. if (!frame_attached_nodes.has(attached_frame->get_name())) {
  382. continue;
  383. }
  384. for (const StringName &attached_node_name : frame_attached_nodes.get(attached_frame->get_name())) {
  385. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  386. GraphFrame *attached_child_frame_node = Object::cast_to<GraphFrame>(attached_node);
  387. if (attached_child_frame_node && (attached_child_frame_node != frame)) {
  388. attached_nodes_to_move.push_back(attached_child_frame_node);
  389. }
  390. }
  391. }
  392. }
  393. void GraphEdit::_graph_element_selected(Node *p_node) {
  394. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  395. ERR_FAIL_NULL(graph_element);
  396. emit_signal(SNAME("node_selected"), graph_element);
  397. }
  398. void GraphEdit::_graph_element_deselected(Node *p_node) {
  399. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  400. ERR_FAIL_NULL(graph_element);
  401. emit_signal(SNAME("node_deselected"), graph_element);
  402. }
  403. void GraphEdit::_graph_element_resize_request(const Vector2 &p_new_minsize, Node *p_node) {
  404. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  405. ERR_FAIL_NULL(graph_element);
  406. // Snap the new size to the grid if snapping is enabled.
  407. Vector2 new_size = p_new_minsize;
  408. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  409. new_size = new_size.snappedf(snapping_distance);
  410. }
  411. // Disallow resizing the frame to a size smaller than the minimum size of the attached nodes.
  412. GraphFrame *frame = Object::cast_to<GraphFrame>(graph_element);
  413. if (frame && !frame->is_autoshrink_enabled()) {
  414. Rect2 frame_rect = _compute_shrinked_frame_rect(frame);
  415. Vector2 computed_min_size = (frame_rect.position + frame_rect.size) - frame->get_position_offset();
  416. frame->set_size(new_size.max(computed_min_size));
  417. } else {
  418. graph_element->set_size(new_size);
  419. }
  420. // Update all parent frames recursively bottom-up.
  421. if (linked_parent_map.has(graph_element->get_name())) {
  422. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[graph_element->get_name()])));
  423. if (parent_frame) {
  424. _update_graph_frame(parent_frame);
  425. }
  426. }
  427. }
  428. void GraphEdit::_graph_frame_autoshrink_changed(const Vector2 &p_new_minsize, GraphFrame *p_frame) {
  429. _update_graph_frame(p_frame);
  430. minimap->queue_redraw();
  431. queue_redraw();
  432. connections_layer->queue_redraw();
  433. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  434. }
  435. void GraphEdit::_graph_element_moved(Node *p_node) {
  436. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  437. ERR_FAIL_NULL(graph_element);
  438. minimap->queue_redraw();
  439. queue_redraw();
  440. connections_layer->queue_redraw();
  441. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  442. }
  443. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) {
  444. GraphNode *graph_node = Object::cast_to<GraphNode>(p_node);
  445. ERR_FAIL_NULL(graph_node);
  446. minimap->queue_redraw();
  447. queue_redraw();
  448. connections_layer->queue_redraw();
  449. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  450. }
  451. void GraphEdit::_graph_node_rect_changed(GraphNode *p_node) {
  452. // Only invalidate the cache when zooming or the node is moved/resized in graph space.
  453. if (panner->is_panning()) {
  454. return;
  455. }
  456. for (Ref<Connection> &c : connection_map[p_node->get_name()]) {
  457. c->_cache.dirty = true;
  458. }
  459. connections_layer->queue_redraw();
  460. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  461. // Update all parent frames recursively bottom-up.
  462. if (linked_parent_map.has(p_node->get_name())) {
  463. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[p_node->get_name()])));
  464. if (parent_frame) {
  465. _update_graph_frame(parent_frame);
  466. }
  467. }
  468. }
  469. void GraphEdit::_ensure_node_order_from_root(const StringName &p_node) {
  470. // Find the root frame node of the frame tree starting from p_node.
  471. GraphElement *root_frame = Object::cast_to<GraphElement>(get_node(NodePath(p_node)));
  472. ERR_FAIL_NULL(root_frame);
  473. while (linked_parent_map.has(root_frame->get_name())) {
  474. root_frame = Object::cast_to<GraphElement>(get_node(NodePath(linked_parent_map[root_frame->get_name()])));
  475. }
  476. _ensure_node_order_from(root_frame);
  477. }
  478. void GraphEdit::add_child_notify(Node *p_child) {
  479. Control::add_child_notify(p_child);
  480. // Keep the top layer always on top!
  481. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  482. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  483. if (graph_element) {
  484. graph_element->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved).bind(graph_element));
  485. graph_element->connect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected).bind(graph_element));
  486. graph_element->connect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected).bind(graph_element));
  487. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  488. if (graph_node) {
  489. graph_node->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(graph_element));
  490. graph_node->connect("item_rect_changed", callable_mp(this, &GraphEdit::_graph_node_rect_changed).bind(graph_node));
  491. _ensure_node_order_from(graph_node);
  492. }
  493. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(graph_element);
  494. if (graph_frame) {
  495. background_nodes_separator_idx++;
  496. callable_mp((Node *)this, &Node::move_child).call_deferred(graph_frame, 0);
  497. callable_mp((Node *)this, &Node::move_child).call_deferred(connections_layer, background_nodes_separator_idx);
  498. _update_graph_frame(graph_frame);
  499. graph_frame->connect("autoshrink_changed", callable_mp(this, &GraphEdit::_graph_frame_autoshrink_changed).bind(graph_element));
  500. }
  501. graph_element->connect("raise_request", callable_mp(this, &GraphEdit::_ensure_node_order_from).bind(graph_element));
  502. graph_element->connect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resize_request).bind(graph_element));
  503. graph_element->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  504. graph_element->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  505. graph_element->set_scale(Vector2(zoom, zoom));
  506. _graph_element_moved(graph_element);
  507. graph_element->set_mouse_filter(MOUSE_FILTER_PASS);
  508. }
  509. }
  510. void GraphEdit::remove_child_notify(Node *p_child) {
  511. Control::remove_child_notify(p_child);
  512. if (p_child == top_layer) {
  513. top_layer = nullptr;
  514. minimap = nullptr;
  515. } else if (p_child == connections_layer) {
  516. connections_layer = nullptr;
  517. }
  518. if (top_layer != nullptr && is_inside_tree()) {
  519. // Keep the top layer always on top!
  520. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  521. }
  522. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  523. if (graph_element) {
  524. graph_element->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved));
  525. graph_element->disconnect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected));
  526. graph_element->disconnect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected));
  527. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  528. if (graph_node) {
  529. graph_node->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  530. graph_node->disconnect("item_rect_changed", callable_mp(this, &GraphEdit::_graph_node_rect_changed));
  531. // Invalidate all adjacent connections, so that they are removed before the next redraw.
  532. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  533. conn->_cache.dirty = true;
  534. }
  535. connections_layer->queue_redraw();
  536. }
  537. GraphFrame *frame = Object::cast_to<GraphFrame>(graph_element);
  538. if (frame) {
  539. background_nodes_separator_idx--;
  540. graph_element->disconnect("autoshrink_changed", callable_mp(this, &GraphEdit::_graph_frame_autoshrink_changed));
  541. }
  542. if (linked_parent_map.has(graph_element->get_name())) {
  543. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[graph_element->get_name()])));
  544. if (parent_frame) {
  545. if (frame_attached_nodes.has(parent_frame->get_name())) {
  546. frame_attached_nodes.get(parent_frame->get_name()).erase(graph_element->get_name());
  547. }
  548. linked_parent_map.erase(graph_element->get_name());
  549. _update_graph_frame(parent_frame);
  550. }
  551. }
  552. if (frame_attached_nodes.has(graph_element->get_name())) {
  553. for (const StringName &attached_node_name : frame_attached_nodes.get(graph_element->get_name())) {
  554. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  555. if (attached_node) {
  556. linked_parent_map.erase(attached_node->get_name());
  557. }
  558. }
  559. frame_attached_nodes.erase(graph_element->get_name());
  560. }
  561. graph_element->disconnect("raise_request", callable_mp(this, &GraphEdit::_ensure_node_order_from));
  562. graph_element->disconnect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resize_request));
  563. // In case of the whole GraphEdit being destroyed these references can already be freed.
  564. if (minimap != nullptr && minimap->is_inside_tree()) {
  565. graph_element->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  566. }
  567. }
  568. }
  569. void GraphEdit::_update_theme_item_cache() {
  570. Control::_update_theme_item_cache();
  571. theme_cache.base_scale = get_theme_default_base_scale();
  572. }
  573. void GraphEdit::_notification(int p_what) {
  574. switch (p_what) {
  575. case NOTIFICATION_THEME_CHANGED: {
  576. zoom_minus_button->set_icon(theme_cache.zoom_out);
  577. zoom_reset_button->set_icon(theme_cache.zoom_reset);
  578. zoom_plus_button->set_icon(theme_cache.zoom_in);
  579. toggle_snapping_button->set_icon(theme_cache.snapping_toggle);
  580. toggle_grid_button->set_icon(theme_cache.grid_toggle);
  581. minimap_button->set_icon(theme_cache.minimap_toggle);
  582. arrange_button->set_icon(theme_cache.layout);
  583. zoom_label->set_custom_minimum_size(Size2(48, 0) * theme_cache.base_scale);
  584. menu_panel->add_theme_style_override("panel", theme_cache.menu_panel);
  585. } break;
  586. case NOTIFICATION_READY: {
  587. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  588. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  589. h_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  590. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  591. h_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  592. h_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  593. v_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  594. v_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  595. v_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  596. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  597. } break;
  598. case NOTIFICATION_DRAW: {
  599. // Draw background fill.
  600. draw_style_box(theme_cache.panel, Rect2(Point2(), get_size()));
  601. // Draw background grid.
  602. if (show_grid) {
  603. _draw_grid();
  604. }
  605. } break;
  606. case NOTIFICATION_RESIZED: {
  607. _update_scroll();
  608. minimap->queue_redraw();
  609. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  610. } break;
  611. }
  612. }
  613. Rect2 GraphEdit::_compute_shrinked_frame_rect(const GraphFrame *p_frame) {
  614. Vector2 min_point{ FLT_MAX, FLT_MAX };
  615. Vector2 max_point{ -FLT_MAX, -FLT_MAX };
  616. if (!frame_attached_nodes.has(p_frame->get_name())) {
  617. return Rect2(p_frame->get_position_offset(), Size2());
  618. }
  619. int autoshrink_margin = p_frame->get_autoshrink_margin();
  620. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  621. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node_or_null(NodePath(attached_node_name)));
  622. if (!attached_node || attached_node == p_frame) {
  623. if (!attached_node) {
  624. frame_attached_nodes.get(p_frame->get_name()).erase(attached_node_name);
  625. }
  626. continue;
  627. }
  628. Vector2 node_pos = attached_node->get_position_offset();
  629. Vector2 size = attached_node->get_size();
  630. min_point = min_point.min(node_pos);
  631. max_point = max_point.max(node_pos + size);
  632. }
  633. // It's sufficient to check only one value here.
  634. if (min_point.x == FLT_MAX) {
  635. return Rect2(p_frame->get_position_offset(), Size2());
  636. }
  637. min_point -= Size2(autoshrink_margin, autoshrink_margin);
  638. max_point += Size2(autoshrink_margin, autoshrink_margin);
  639. return Rect2(min_point, max_point - min_point);
  640. }
  641. void GraphEdit::_update_graph_frame(GraphFrame *p_frame) {
  642. Rect2 frame_rect = _compute_shrinked_frame_rect(p_frame);
  643. Vector2 min_point = frame_rect.position;
  644. Vector2 max_point = frame_rect.position + frame_rect.size;
  645. // Only update the size if there are attached nodes.
  646. if (frame_attached_nodes.has(p_frame->get_name()) && frame_attached_nodes.get(p_frame->get_name()).size() > 0) {
  647. if (!p_frame->is_autoshrink_enabled()) {
  648. Vector2 old_offset = p_frame->get_position_offset();
  649. min_point = min_point.min(old_offset);
  650. max_point = max_point.max(old_offset + p_frame->get_size());
  651. }
  652. Rect2 old_rect = p_frame->get_rect();
  653. p_frame->set_position_offset(min_point);
  654. p_frame->set_size(max_point - min_point);
  655. // Emit the signal only if the frame rect has changed.
  656. if (old_rect != p_frame->get_rect()) {
  657. emit_signal(SNAME("frame_rect_changed"), p_frame, p_frame->get_rect());
  658. }
  659. }
  660. // Update all parent frames recursively bottom-up.
  661. if (linked_parent_map.has(p_frame->get_name())) {
  662. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[p_frame->get_name()])));
  663. if (parent_frame) {
  664. _update_graph_frame(parent_frame);
  665. }
  666. }
  667. }
  668. void GraphEdit::_set_drag_frame_attached_nodes(GraphFrame *p_frame, bool p_drag) {
  669. if (!frame_attached_nodes.has(p_frame->get_name())) {
  670. return;
  671. }
  672. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  673. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  674. attached_node->set_drag(p_drag);
  675. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(attached_node);
  676. if (graph_frame) {
  677. _set_drag_frame_attached_nodes(graph_frame, p_drag);
  678. }
  679. }
  680. }
  681. void GraphEdit::_set_position_of_frame_attached_nodes(GraphFrame *p_frame, const Vector2 &p_pos) {
  682. if (!frame_attached_nodes.has(p_frame->get_name())) {
  683. return;
  684. }
  685. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  686. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node_or_null(NodePath(attached_node_name)));
  687. if (!attached_node) {
  688. continue;
  689. }
  690. Vector2 pos = (attached_node->get_drag_from() * zoom + drag_accum) / zoom;
  691. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  692. pos = pos.snappedf(snapping_distance);
  693. }
  694. // Recursively move graph frames.
  695. attached_node->set_position_offset(pos);
  696. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(attached_node);
  697. if (graph_frame) {
  698. _set_position_of_frame_attached_nodes(graph_frame, p_pos);
  699. }
  700. }
  701. }
  702. bool GraphEdit::_filter_input(const Point2 &p_point) {
  703. for (int i = get_child_count() - 1; i >= 0; i--) {
  704. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  705. if (!graph_node || !graph_node->is_visible_in_tree()) {
  706. continue;
  707. }
  708. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  709. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  710. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  711. // Determine slot height.
  712. int slot_index = graph_node->get_input_port_slot(j);
  713. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  714. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  715. if (is_in_input_hotzone(graph_node, j, p_point / zoom, port_size)) {
  716. return true;
  717. }
  718. }
  719. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  720. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  721. // Determine slot height.
  722. int slot_index = graph_node->get_output_port_slot(j);
  723. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  724. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  725. if (is_in_output_hotzone(graph_node, j, p_point / zoom, port_size)) {
  726. return true;
  727. }
  728. }
  729. }
  730. return false;
  731. }
  732. void GraphEdit::_top_connection_layer_input(const Ref<InputEvent> &p_ev) {
  733. Ref<InputEventMouseButton> mb = p_ev;
  734. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  735. connecting_valid = false;
  736. click_pos = mb->get_position() / zoom;
  737. for (int i = get_child_count() - 1; i >= 0; i--) {
  738. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  739. if (!graph_node || !graph_node->is_visible_in_tree()) {
  740. continue;
  741. }
  742. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  743. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  744. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  745. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  746. // Determine slot height.
  747. int slot_index = graph_node->get_output_port_slot(j);
  748. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  749. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  750. if (is_in_output_hotzone(graph_node, j, click_pos, port_size)) {
  751. if (valid_left_disconnect_types.has(graph_node->get_output_port_type(j))) {
  752. // Check disconnect.
  753. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  754. if (conn->from_node == graph_node->get_name() && conn->from_port == j) {
  755. Node *to = get_node(NodePath(conn->to_node));
  756. if (Object::cast_to<GraphNode>(to)) {
  757. connecting_from_node = conn->to_node;
  758. connecting_from_port_index = conn->to_port;
  759. connecting_from_output = false;
  760. connecting_type = Object::cast_to<GraphNode>(to)->get_input_port_type(conn->to_port);
  761. connecting_color = Object::cast_to<GraphNode>(to)->get_input_port_color(conn->to_port);
  762. connecting_target_valid = false;
  763. connecting_to_point = pos;
  764. if (connecting_type >= 0) {
  765. just_disconnected = true;
  766. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  767. to = get_node(NodePath(connecting_from_node)); // Maybe it was erased.
  768. if (Object::cast_to<GraphNode>(to)) {
  769. connecting = true;
  770. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  771. }
  772. }
  773. return;
  774. }
  775. }
  776. }
  777. }
  778. connecting_from_node = graph_node->get_name();
  779. connecting_from_port_index = j;
  780. connecting_from_output = true;
  781. connecting_type = graph_node->get_output_port_type(j);
  782. connecting_color = graph_node->get_output_port_color(j);
  783. connecting_target_valid = false;
  784. connecting_to_point = pos;
  785. if (connecting_type >= 0) {
  786. connecting = true;
  787. just_disconnected = false;
  788. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  789. }
  790. return;
  791. }
  792. }
  793. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  794. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  795. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  796. // Determine slot height.
  797. int slot_index = graph_node->get_input_port_slot(j);
  798. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  799. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  800. if (is_in_input_hotzone(graph_node, j, click_pos, port_size)) {
  801. if (right_disconnects || valid_right_disconnect_types.has(graph_node->get_input_port_type(j))) {
  802. // Check disconnect.
  803. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  804. if (conn->to_node == graph_node->get_name() && conn->to_port == j) {
  805. Node *fr = get_node(NodePath(conn->from_node));
  806. if (Object::cast_to<GraphNode>(fr)) {
  807. connecting_from_node = conn->from_node;
  808. connecting_from_port_index = conn->from_port;
  809. connecting_from_output = true;
  810. connecting_type = Object::cast_to<GraphNode>(fr)->get_output_port_type(conn->from_port);
  811. connecting_color = Object::cast_to<GraphNode>(fr)->get_output_port_color(conn->from_port);
  812. connecting_target_valid = false;
  813. connecting_to_point = pos;
  814. just_disconnected = true;
  815. if (connecting_type >= 0) {
  816. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  817. fr = get_node(NodePath(connecting_from_node));
  818. if (Object::cast_to<GraphNode>(fr)) {
  819. connecting = true;
  820. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  821. }
  822. }
  823. return;
  824. }
  825. }
  826. }
  827. }
  828. connecting_from_node = graph_node->get_name();
  829. connecting_from_port_index = j;
  830. connecting_from_output = false;
  831. connecting_type = graph_node->get_input_port_type(j);
  832. connecting_color = graph_node->get_input_port_color(j);
  833. connecting_target_valid = false;
  834. connecting_to_point = pos;
  835. if (connecting_type >= 0) {
  836. connecting = true;
  837. just_disconnected = false;
  838. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  839. }
  840. return;
  841. }
  842. }
  843. }
  844. }
  845. Ref<InputEventMouseMotion> mm = p_ev;
  846. if (mm.is_valid() && connecting) {
  847. connecting_to_point = mm->get_position();
  848. minimap->queue_redraw();
  849. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  850. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to_point / zoom) > MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION;
  851. if (connecting_valid) {
  852. Vector2 mpos = mm->get_position() / zoom;
  853. for (int i = get_child_count() - 1; i >= 0; i--) {
  854. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  855. if (!graph_node || !graph_node->is_visible_in_tree()) {
  856. continue;
  857. }
  858. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  859. if (!connecting_from_output) {
  860. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  861. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  862. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  863. // Determine slot height.
  864. int slot_index = graph_node->get_output_port_slot(j);
  865. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  866. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  867. int type = graph_node->get_output_port_type(j);
  868. if ((type == connecting_type ||
  869. valid_connection_types.has(ConnectionType(type, connecting_type))) &&
  870. is_in_output_hotzone(graph_node, j, mpos, port_size)) {
  871. if (!is_node_hover_valid(graph_node->get_name(), j, connecting_from_node, connecting_from_port_index)) {
  872. continue;
  873. }
  874. connecting_target_valid = true;
  875. connecting_to_point = pos;
  876. connecting_target_node = graph_node->get_name();
  877. connecting_target_port_index = j;
  878. return;
  879. }
  880. }
  881. connecting_target_valid = false;
  882. } else {
  883. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  884. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  885. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  886. // Determine slot height.
  887. int slot_index = graph_node->get_input_port_slot(j);
  888. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  889. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  890. int type = graph_node->get_input_port_type(j);
  891. if ((type == connecting_type || valid_connection_types.has(ConnectionType(connecting_type, type))) &&
  892. is_in_input_hotzone(graph_node, j, mpos, port_size)) {
  893. if (!is_node_hover_valid(connecting_from_node, connecting_from_port_index, graph_node->get_name(), j)) {
  894. continue;
  895. }
  896. connecting_target_valid = true;
  897. connecting_to_point = pos;
  898. connecting_target_node = graph_node->get_name();
  899. connecting_target_port_index = j;
  900. return;
  901. }
  902. }
  903. connecting_target_valid = false;
  904. }
  905. }
  906. }
  907. }
  908. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  909. if (connecting_valid) {
  910. if (connecting && connecting_target_valid) {
  911. if (connecting_from_output) {
  912. emit_signal(SNAME("connection_request"), connecting_from_node, connecting_from_port_index, connecting_target_node, connecting_target_port_index);
  913. } else {
  914. emit_signal(SNAME("connection_request"), connecting_target_node, connecting_target_port_index, connecting_from_node, connecting_from_port_index);
  915. }
  916. } else if (!just_disconnected) {
  917. if (connecting_from_output) {
  918. emit_signal(SNAME("connection_to_empty"), connecting_from_node, connecting_from_port_index, mb->get_position());
  919. } else {
  920. emit_signal(SNAME("connection_from_empty"), connecting_from_node, connecting_from_port_index, mb->get_position());
  921. }
  922. }
  923. }
  924. if (connecting) {
  925. force_connection_drag_end();
  926. }
  927. }
  928. }
  929. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) {
  930. if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) {
  931. return false;
  932. }
  933. Rect2 control_rect = p_control->get_rect();
  934. control_rect.position *= zoom;
  935. control_rect.size *= zoom;
  936. control_rect.position += p_offset;
  937. if (!control_rect.has_point(mpos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  938. // Test children.
  939. for (int i = 0; i < p_control->get_child_count(); i++) {
  940. Control *child_rect = Object::cast_to<Control>(p_control->get_child(i));
  941. if (!child_rect) {
  942. continue;
  943. }
  944. if (_check_clickable_control(child_rect, mpos, control_rect.position)) {
  945. return true;
  946. }
  947. }
  948. return false;
  949. } else {
  950. return true;
  951. }
  952. }
  953. bool GraphEdit::is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  954. bool success;
  955. if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  956. return success;
  957. } else {
  958. Vector2 pos = p_graph_node->get_input_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  959. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true);
  960. }
  961. }
  962. bool GraphEdit::is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  963. if (p_graph_node->is_resizable()) {
  964. Ref<Texture2D> resizer = p_graph_node->theme_cache.resizer;
  965. Rect2 resizer_rect = Rect2(p_graph_node->get_position() / zoom + p_graph_node->get_size() - resizer->get_size(), resizer->get_size());
  966. if (resizer_rect.has_point(p_mouse_pos)) {
  967. return false;
  968. }
  969. }
  970. bool success;
  971. if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  972. return success;
  973. } else {
  974. Vector2 pos = p_graph_node->get_output_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  975. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false);
  976. }
  977. }
  978. bool GraphEdit::is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  979. Rect2 hotzone = Rect2(
  980. p_pos.x - (p_left ? theme_cache.port_hotzone_outer_extent : theme_cache.port_hotzone_inner_extent),
  981. p_pos.y - p_port_size.height / 2.0,
  982. theme_cache.port_hotzone_inner_extent + theme_cache.port_hotzone_outer_extent,
  983. p_port_size.height);
  984. if (!hotzone.has_point(p_mouse_pos)) {
  985. return false;
  986. }
  987. for (int i = 0; i < get_child_count(); i++) {
  988. GraphNode *child = Object::cast_to<GraphNode>(get_child(i));
  989. if (!child) {
  990. continue;
  991. }
  992. Rect2 child_rect = child->get_rect();
  993. if (child_rect.has_point(p_mouse_pos * zoom)) {
  994. for (int j = 0; j < child->get_child_count(); j++) {
  995. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  996. if (!subchild) {
  997. continue;
  998. }
  999. if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) {
  1000. return false;
  1001. }
  1002. }
  1003. }
  1004. }
  1005. return true;
  1006. }
  1007. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) const {
  1008. Vector<Vector2> ret;
  1009. if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) {
  1010. return ret;
  1011. }
  1012. float x_diff = (p_to.x - p_from.x);
  1013. float cp_offset = x_diff * lines_curvature;
  1014. if (x_diff < 0) {
  1015. cp_offset *= -1;
  1016. }
  1017. Curve2D curve;
  1018. curve.add_point(p_from);
  1019. curve.set_point_out(0, Vector2(cp_offset, 0));
  1020. curve.add_point(p_to);
  1021. curve.set_point_in(1, Vector2(-cp_offset, 0));
  1022. if (lines_curvature > 0) {
  1023. return curve.tessellate(MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES, 2.0);
  1024. } else {
  1025. return curve.tessellate(1);
  1026. }
  1027. }
  1028. Ref<GraphEdit::Connection> GraphEdit::get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance) const {
  1029. Vector2 transformed_point = p_point + get_scroll_offset();
  1030. Ref<GraphEdit::Connection> closest_connection;
  1031. float closest_distance = p_max_distance;
  1032. for (const Ref<Connection> &c : connections) {
  1033. if (c->_cache.aabb.distance_to(transformed_point) > p_max_distance) {
  1034. continue;
  1035. }
  1036. Vector<Vector2> points = get_connection_line(c->_cache.from_pos * zoom, c->_cache.to_pos * zoom);
  1037. for (int i = 0; i < points.size() - 1; i++) {
  1038. float distance = Geometry2D::get_distance_to_segment(transformed_point, &points[i]);
  1039. if (distance <= lines_thickness * 0.5 + p_max_distance && distance < closest_distance) {
  1040. closest_connection = c;
  1041. closest_distance = distance;
  1042. }
  1043. }
  1044. }
  1045. return closest_connection;
  1046. }
  1047. List<Ref<GraphEdit::Connection>> GraphEdit::get_connections_intersecting_with_rect(const Rect2 &p_rect) const {
  1048. Rect2 transformed_rect = p_rect;
  1049. transformed_rect.position += get_scroll_offset();
  1050. List<Ref<Connection>> intersecting_connections;
  1051. for (const Ref<Connection> &c : connections) {
  1052. if (!c->_cache.aabb.intersects(transformed_rect)) {
  1053. continue;
  1054. }
  1055. Vector<Vector2> points = get_connection_line(c->_cache.from_pos * zoom, c->_cache.to_pos * zoom);
  1056. for (int i = 0; i < points.size() - 1; i++) {
  1057. if (Geometry2D::segment_intersects_rect(points[i], points[i + 1], transformed_rect)) {
  1058. intersecting_connections.push_back(c);
  1059. break;
  1060. }
  1061. }
  1062. }
  1063. return intersecting_connections;
  1064. }
  1065. void GraphEdit::_draw_minimap_connection_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_from_color, const Color &p_to_color) {
  1066. const Vector<Vector2> &points = get_connection_line(p_from, p_to);
  1067. LocalVector<Color> colors;
  1068. colors.reserve(points.size());
  1069. float length_inv = 1.0 / (p_from).distance_to(p_to);
  1070. for (const Vector2 &point : points) {
  1071. float normalized_curve_position = (p_from).distance_to(point) * length_inv;
  1072. colors.push_back(p_from_color.lerp(p_to_color, normalized_curve_position));
  1073. }
  1074. p_where->draw_polyline_colors(points, colors, 0.5, lines_antialiased);
  1075. }
  1076. void GraphEdit::_update_connections() {
  1077. // Collect all dead connections and remove them.
  1078. List<List<Ref<Connection>>::Element *> dead_connections;
  1079. for (List<Ref<Connection>>::Element *E = connections.front(); E; E = E->next()) {
  1080. Ref<Connection> &c = E->get();
  1081. if (c->_cache.dirty) {
  1082. Node *from = get_node_or_null(NodePath(c->from_node));
  1083. GraphNode *gnode_from = Object::cast_to<GraphNode>(from);
  1084. if (!gnode_from) {
  1085. dead_connections.push_back(E);
  1086. continue;
  1087. }
  1088. Node *to = get_node_or_null(NodePath(c->to_node));
  1089. GraphNode *gnode_to = Object::cast_to<GraphNode>(to);
  1090. if (!gnode_to) {
  1091. dead_connections.push_back(E);
  1092. continue;
  1093. }
  1094. const Vector2 from_pos = gnode_from->get_output_port_position(c->from_port) + gnode_from->get_position_offset();
  1095. const Vector2 to_pos = gnode_to->get_input_port_position(c->to_port) + gnode_to->get_position_offset();
  1096. const Color from_color = gnode_from->get_output_port_color(c->from_port);
  1097. const Color to_color = gnode_to->get_input_port_color(c->to_port);
  1098. const int from_type = gnode_from->get_output_port_type(c->from_port);
  1099. const int to_type = gnode_to->get_input_port_type(c->to_port);
  1100. c->_cache.from_pos = from_pos;
  1101. c->_cache.to_pos = to_pos;
  1102. c->_cache.from_color = from_color;
  1103. c->_cache.to_color = to_color;
  1104. PackedVector2Array line_points = get_connection_line(from_pos * zoom, to_pos * zoom);
  1105. c->_cache.line->set_points(line_points);
  1106. Ref<ShaderMaterial> line_material = c->_cache.line->get_material();
  1107. if (line_material.is_null()) {
  1108. line_material.instantiate();
  1109. c->_cache.line->set_material(line_material);
  1110. }
  1111. float line_width = _get_shader_line_width();
  1112. line_material->set_shader_parameter("line_width", line_width);
  1113. line_material->set_shader_parameter("from_type", from_type);
  1114. line_material->set_shader_parameter("to_type", to_type);
  1115. line_material->set_shader_parameter("rim_color", theme_cache.connection_rim_color);
  1116. // Compute bounding box of the line, including the line width.
  1117. c->_cache.aabb = Rect2(line_points[0], Vector2());
  1118. for (int i = 0; i < line_points.size(); i++) {
  1119. c->_cache.aabb.expand_to(line_points[i]);
  1120. }
  1121. c->_cache.aabb.grow_by(lines_thickness * 0.5);
  1122. c->_cache.dirty = false;
  1123. }
  1124. // Skip updating/drawing connections that are not visible.
  1125. Rect2 viewport_rect = get_viewport_rect();
  1126. viewport_rect.position += get_scroll_offset();
  1127. if (!c->_cache.aabb.intersects(viewport_rect)) {
  1128. continue;
  1129. }
  1130. Color from_color = c->_cache.from_color;
  1131. Color to_color = c->_cache.to_color;
  1132. if (c->activity > 0) {
  1133. from_color = from_color.lerp(theme_cache.activity_color, c->activity);
  1134. to_color = to_color.lerp(theme_cache.activity_color, c->activity);
  1135. }
  1136. if (c == hovered_connection) {
  1137. from_color = from_color.blend(theme_cache.connection_hover_tint_color);
  1138. to_color = to_color.blend(theme_cache.connection_hover_tint_color);
  1139. }
  1140. // Update Line2D node.
  1141. Ref<Gradient> line_gradient = memnew(Gradient);
  1142. float line_width = _get_shader_line_width();
  1143. c->_cache.line->set_width(line_width);
  1144. line_gradient->set_color(0, from_color);
  1145. line_gradient->set_color(1, to_color);
  1146. c->_cache.line->set_gradient(line_gradient);
  1147. }
  1148. for (const List<Ref<Connection>>::Element *E : dead_connections) {
  1149. List<Ref<Connection>> &connections_from = connection_map[E->get()->from_node];
  1150. List<Ref<Connection>> &connections_to = connection_map[E->get()->to_node];
  1151. connections_from.erase(E->get());
  1152. connections_to.erase(E->get());
  1153. E->get()->_cache.line->queue_free();
  1154. connections.erase(E->get());
  1155. }
  1156. }
  1157. void GraphEdit::_top_layer_draw() {
  1158. if (!box_selecting) {
  1159. return;
  1160. }
  1161. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_fill);
  1162. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_stroke, false);
  1163. }
  1164. void GraphEdit::_update_top_connection_layer() {
  1165. _update_scroll();
  1166. if (!connecting) {
  1167. dragged_connection_line->clear_points();
  1168. return;
  1169. }
  1170. GraphNode *graph_node_from = Object::cast_to<GraphNode>(get_node_or_null(NodePath(connecting_from_node)));
  1171. ERR_FAIL_NULL(graph_node_from);
  1172. Vector2 from_pos = graph_node_from->get_position() / zoom;
  1173. Vector2 to_pos = connecting_to_point / zoom;
  1174. int from_type;
  1175. int to_type = connecting_type;
  1176. Color from_color;
  1177. Color to_color = connecting_color;
  1178. if (connecting_from_output) {
  1179. from_pos += graph_node_from->get_output_port_position(connecting_from_port_index);
  1180. from_type = graph_node_from->get_output_port_type(connecting_from_port_index);
  1181. from_color = graph_node_from->get_output_port_color(connecting_from_port_index);
  1182. } else {
  1183. from_pos += graph_node_from->get_input_port_position(connecting_from_port_index);
  1184. from_type = graph_node_from->get_input_port_type(connecting_from_port_index);
  1185. from_color = graph_node_from->get_input_port_color(connecting_from_port_index);
  1186. }
  1187. if (connecting_target_valid) {
  1188. GraphNode *graph_node_to = Object::cast_to<GraphNode>(get_node_or_null(NodePath(connecting_target_node)));
  1189. ERR_FAIL_NULL(graph_node_to);
  1190. if (connecting_from_output) {
  1191. to_type = graph_node_to->get_input_port_type(connecting_target_port_index);
  1192. to_color = graph_node_to->get_input_port_color(connecting_target_port_index);
  1193. } else {
  1194. to_type = graph_node_to->get_output_port_type(connecting_target_port_index);
  1195. to_color = graph_node_to->get_output_port_color(connecting_target_port_index);
  1196. }
  1197. // Highlight the line to the mouse cursor when it's over a valid target port.
  1198. from_color = from_color.blend(theme_cache.connection_valid_target_tint_color);
  1199. to_color = to_color.blend(theme_cache.connection_valid_target_tint_color);
  1200. }
  1201. if (!connecting_from_output) {
  1202. SWAP(from_pos, to_pos);
  1203. SWAP(from_type, to_type);
  1204. SWAP(from_color, to_color);
  1205. }
  1206. PackedVector2Array line_points = get_connection_line(from_pos * zoom, to_pos * zoom);
  1207. dragged_connection_line->set_points(line_points);
  1208. Ref<ShaderMaterial> line_material = dragged_connection_line->get_material();
  1209. if (line_material.is_null()) {
  1210. line_material.instantiate();
  1211. line_material->set_shader(connections_shader);
  1212. dragged_connection_line->set_material(line_material);
  1213. }
  1214. float line_width = _get_shader_line_width();
  1215. line_material->set_shader_parameter("line_width", line_width);
  1216. line_material->set_shader_parameter("from_type", from_type);
  1217. line_material->set_shader_parameter("to_type", to_type);
  1218. line_material->set_shader_parameter("rim_color", theme_cache.connection_rim_color);
  1219. Ref<Gradient> line_gradient = memnew(Gradient);
  1220. dragged_connection_line->set_width(line_width);
  1221. line_gradient->set_color(0, from_color);
  1222. line_gradient->set_color(1, to_color);
  1223. dragged_connection_line->set_gradient(line_gradient);
  1224. }
  1225. void GraphEdit::_minimap_draw() {
  1226. if (!is_minimap_enabled()) {
  1227. return;
  1228. }
  1229. minimap->update_minimap();
  1230. // Draw the minimap background.
  1231. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  1232. minimap->draw_style_box(minimap->theme_cache.panel, minimap_rect);
  1233. Vector2 graph_offset = minimap->_get_graph_offset();
  1234. Vector2 minimap_offset = minimap->minimap_offset;
  1235. // Draw frame nodes.
  1236. for (int i = get_child_count() - 1; i >= 0; i--) {
  1237. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(get_child(i));
  1238. if (!graph_frame || !graph_frame->is_visible()) {
  1239. continue;
  1240. }
  1241. Vector2 node_position = minimap->_convert_from_graph_position(graph_frame->get_position_offset() * zoom - graph_offset) + minimap_offset;
  1242. Vector2 node_size = minimap->_convert_from_graph_position(graph_frame->get_size() * zoom);
  1243. Rect2 node_rect = Rect2(node_position, node_size);
  1244. Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate();
  1245. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  1246. Ref<StyleBoxFlat> sb_frame = graph_frame->get_theme_stylebox(graph_frame->is_selected() ? SNAME("panel_selected") : SNAME("panel"));
  1247. if (sb_frame.is_valid()) {
  1248. Color node_color = sb_frame->get_bg_color();
  1249. if (graph_frame->is_tint_color_enabled()) {
  1250. node_color = graph_frame->get_tint_color();
  1251. }
  1252. sb_minimap->set_bg_color(node_color);
  1253. }
  1254. minimap->draw_style_box(sb_minimap, node_rect);
  1255. }
  1256. // Draw regular graph nodes.
  1257. for (int i = get_child_count() - 1; i >= 0; i--) {
  1258. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  1259. if (!graph_node || !graph_node->is_visible()) {
  1260. continue;
  1261. }
  1262. Vector2 node_position = minimap->_convert_from_graph_position(graph_node->get_position_offset() * zoom - graph_offset) + minimap_offset;
  1263. Vector2 node_size = minimap->_convert_from_graph_position(graph_node->get_size() * zoom);
  1264. Rect2 node_rect = Rect2(node_position, node_size);
  1265. Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate();
  1266. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  1267. Ref<StyleBoxFlat> sb_frame = graph_node->is_selected() ? graph_node->theme_cache.panel_selected : graph_node->theme_cache.panel;
  1268. if (sb_frame.is_valid()) {
  1269. Color node_color = sb_frame->get_bg_color();
  1270. sb_minimap->set_bg_color(node_color);
  1271. }
  1272. minimap->draw_style_box(sb_minimap, node_rect);
  1273. }
  1274. // Draw node connections.
  1275. for (const Ref<Connection> &c : connections) {
  1276. Vector2 from_position = minimap->_convert_from_graph_position(c->_cache.from_pos * zoom - graph_offset) + minimap_offset;
  1277. Vector2 to_position = minimap->_convert_from_graph_position(c->_cache.to_pos * zoom - graph_offset) + minimap_offset;
  1278. Color from_color = c->_cache.from_color;
  1279. Color to_color = c->_cache.to_color;
  1280. if (c->activity > 0) {
  1281. from_color = from_color.lerp(theme_cache.activity_color, c->activity);
  1282. to_color = to_color.lerp(theme_cache.activity_color, c->activity);
  1283. }
  1284. _draw_minimap_connection_line(minimap, from_position, to_position, from_color, to_color);
  1285. }
  1286. // Draw the "camera" viewport.
  1287. Rect2 camera_rect = minimap->get_camera_rect();
  1288. minimap->draw_style_box(minimap->theme_cache.camera_style, camera_rect);
  1289. // Draw the resizer control.
  1290. Ref<Texture2D> resizer = minimap->theme_cache.resizer;
  1291. Color resizer_color = minimap->theme_cache.resizer_color;
  1292. minimap->draw_texture(resizer, Point2(), resizer_color);
  1293. }
  1294. void GraphEdit::_draw_grid() {
  1295. Vector2 offset = get_scroll_offset() / zoom;
  1296. Size2 size = get_size() / zoom;
  1297. Point2i from_pos = (offset / float(snapping_distance)).floor();
  1298. Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1);
  1299. switch (grid_pattern) {
  1300. case GRID_PATTERN_LINES: {
  1301. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  1302. Color color;
  1303. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  1304. color = theme_cache.grid_major;
  1305. } else {
  1306. color = theme_cache.grid_minor;
  1307. }
  1308. float base_offset = i * snapping_distance * zoom - offset.x * zoom;
  1309. draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color);
  1310. }
  1311. for (int i = from_pos.y; i < from_pos.y + len.y; i++) {
  1312. Color color;
  1313. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  1314. color = theme_cache.grid_major;
  1315. } else {
  1316. color = theme_cache.grid_minor;
  1317. }
  1318. float base_offset = i * snapping_distance * zoom - offset.y * zoom;
  1319. draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color);
  1320. }
  1321. } break;
  1322. case GRID_PATTERN_DOTS: {
  1323. Color transparent_grid_minor = theme_cache.grid_minor;
  1324. transparent_grid_minor.a *= CLAMP(1.0 * (zoom - 0.4), 0, 1);
  1325. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  1326. for (int j = from_pos.y; j < from_pos.y + len.y; j++) {
  1327. Color color = transparent_grid_minor;
  1328. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0 && ABS(j) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0) {
  1329. color = theme_cache.grid_major;
  1330. }
  1331. if (color.a == 0) {
  1332. continue;
  1333. }
  1334. float base_offset_x = i * snapping_distance * zoom - offset.x * zoom;
  1335. float base_offset_y = j * snapping_distance * zoom - offset.y * zoom;
  1336. draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), color);
  1337. }
  1338. }
  1339. } break;
  1340. }
  1341. }
  1342. void GraphEdit::set_selected(Node *p_child) {
  1343. for (int i = get_child_count() - 1; i >= 0; i--) {
  1344. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  1345. if (!graph_node) {
  1346. continue;
  1347. }
  1348. graph_node->set_selected(graph_node == p_child);
  1349. }
  1350. }
  1351. void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
  1352. ERR_FAIL_COND(p_ev.is_null());
  1353. if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) {
  1354. return;
  1355. }
  1356. // Highlight the connection close to the mouse cursor.
  1357. Ref<InputEventMouseMotion> mm = p_ev;
  1358. if (mm.is_valid()) {
  1359. Ref<Connection> new_highlighted_connection = get_closest_connection_at_point(mm->get_position());
  1360. if (new_highlighted_connection != hovered_connection) {
  1361. connections_layer->queue_redraw();
  1362. }
  1363. hovered_connection = new_highlighted_connection;
  1364. }
  1365. // Logic for moving graph controls via mouse drag.
  1366. if (mm.is_valid() && dragging) {
  1367. if (!moving_selection) {
  1368. emit_signal(SNAME("begin_node_move"));
  1369. moving_selection = true;
  1370. }
  1371. just_selected = true;
  1372. drag_accum += mm->get_relative();
  1373. for (int i = get_child_count() - 1; i >= 0; i--) {
  1374. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1375. if (graph_element && graph_element->is_selected() && graph_element->is_draggable()) {
  1376. Vector2 pos = (graph_element->get_drag_from() * zoom + drag_accum) / zoom;
  1377. // Snapping can be toggled temporarily by holding down Ctrl.
  1378. // This is done here as to not toggle the grid when holding down Ctrl.
  1379. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1380. pos = pos.snappedf(snapping_distance);
  1381. }
  1382. graph_element->set_position_offset(pos);
  1383. if (linked_parent_map.has(graph_element->get_name())) {
  1384. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[graph_element->get_name()])));
  1385. if (parent_frame) {
  1386. _update_graph_frame(parent_frame);
  1387. }
  1388. }
  1389. // Update all frame transforms recursively.
  1390. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(get_child(i));
  1391. if (graph_frame) {
  1392. _set_position_of_frame_attached_nodes(graph_frame, drag_accum);
  1393. }
  1394. }
  1395. }
  1396. }
  1397. // Box selection logic.
  1398. if (mm.is_valid() && box_selecting) {
  1399. box_selecting_to = mm->get_position();
  1400. box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());
  1401. for (int i = get_child_count() - 1; i >= 0; i--) {
  1402. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1403. if (!graph_element) {
  1404. continue;
  1405. }
  1406. // Only select frames when the box selection is fully enclosing them.
  1407. bool is_frame = Object::cast_to<GraphFrame>(graph_element);
  1408. Rect2 r = graph_element->get_rect();
  1409. bool should_be_selected = is_frame ? box_selecting_rect.encloses(r) : box_selecting_rect.intersects(r);
  1410. if (should_be_selected) {
  1411. graph_element->set_selected(box_selection_mode_additive);
  1412. } else {
  1413. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  1414. }
  1415. }
  1416. top_layer->queue_redraw();
  1417. minimap->queue_redraw();
  1418. }
  1419. Ref<InputEventMouseButton> mb = p_ev;
  1420. if (mb.is_valid()) {
  1421. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  1422. if (box_selecting) {
  1423. box_selecting = false;
  1424. for (int i = get_child_count() - 1; i >= 0; i--) {
  1425. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1426. if (!graph_element) {
  1427. continue;
  1428. }
  1429. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  1430. }
  1431. top_layer->queue_redraw();
  1432. minimap->queue_redraw();
  1433. } else {
  1434. if (connecting) {
  1435. force_connection_drag_end();
  1436. } else {
  1437. emit_signal(SNAME("popup_request"), mb->get_position());
  1438. }
  1439. }
  1440. }
  1441. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && dragging) {
  1442. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1443. // Deselect current node.
  1444. for (int i = get_child_count() - 1; i >= 0; i--) {
  1445. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1446. if (graph_element) {
  1447. Rect2 r = graph_element->get_rect();
  1448. if (r.has_point(mb->get_position())) {
  1449. graph_element->set_selected(false);
  1450. }
  1451. }
  1452. }
  1453. }
  1454. if (drag_accum != Vector2()) {
  1455. for (int i = get_child_count() - 1; i >= 0; i--) {
  1456. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1457. if (graph_element && graph_element->is_selected()) {
  1458. graph_element->set_drag(false);
  1459. GraphFrame *frame = Object::cast_to<GraphFrame>(get_child(i));
  1460. if (frame) {
  1461. _set_drag_frame_attached_nodes(frame, false);
  1462. }
  1463. }
  1464. }
  1465. }
  1466. if (moving_selection) {
  1467. emit_signal(SNAME("end_node_move"));
  1468. moving_selection = false;
  1469. Vector<GraphElement *> dragged_nodes;
  1470. for (int i = get_child_count() - 1; i >= 0; i--) {
  1471. GraphElement *moved_node = Object::cast_to<GraphElement>(get_child(i));
  1472. if (moved_node && moved_node->is_selected() && moved_node->is_draggable()) {
  1473. dragged_nodes.push_back(moved_node);
  1474. }
  1475. }
  1476. GraphFrame *frame_dropped_on = nullptr;
  1477. // Find frame on which the node(s) is/were dropped.
  1478. // Count down to find the topmost frame.
  1479. for (int i = get_child_count() - 1; i >= 0; i--) {
  1480. GraphFrame *frame = Object::cast_to<GraphFrame>(get_child(i));
  1481. if (!frame || frame->is_resizing()) {
  1482. continue;
  1483. }
  1484. Rect2 frame_rect = frame->get_rect();
  1485. if (frame_rect.has_point(mb->get_position()) && !dragged_nodes.has(frame)) {
  1486. frame_dropped_on = frame;
  1487. break;
  1488. }
  1489. }
  1490. if (frame_dropped_on) {
  1491. dragged_nodes.erase(frame_dropped_on);
  1492. TypedArray<StringName> dragged_node_names;
  1493. for (GraphElement *moved_node : dragged_nodes) {
  1494. if (!linked_parent_map.has(moved_node->get_name())) {
  1495. dragged_node_names.push_back(moved_node->get_name());
  1496. }
  1497. }
  1498. if (dragged_node_names.size() > 0) {
  1499. emit_signal(SNAME("graph_elements_linked_to_frame_request"), dragged_node_names, frame_dropped_on->get_name());
  1500. }
  1501. }
  1502. }
  1503. dragging = false;
  1504. minimap->queue_redraw();
  1505. queue_redraw();
  1506. connections_layer->queue_redraw();
  1507. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1508. }
  1509. // Node selection logic.
  1510. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  1511. GraphElement *graph_element = nullptr;
  1512. // Find node which was clicked on.
  1513. for (int i = get_child_count() - 1; i >= 0; i--) {
  1514. GraphElement *selected_element = Object::cast_to<GraphElement>(get_child(i));
  1515. if (!selected_element) {
  1516. continue;
  1517. }
  1518. if (selected_element->is_resizing()) {
  1519. continue;
  1520. }
  1521. if (selected_element->has_point((mb->get_position() - selected_element->get_position()) / zoom)) {
  1522. graph_element = selected_element;
  1523. break;
  1524. }
  1525. }
  1526. if (graph_element) {
  1527. if (_filter_input(mb->get_position())) {
  1528. return;
  1529. }
  1530. // Left-clicked on a node, select it.
  1531. dragging = true;
  1532. drag_accum = Vector2();
  1533. just_selected = !graph_element->is_selected();
  1534. if (!graph_element->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1535. for (int i = 0; i < get_child_count(); i++) {
  1536. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1537. if (!child_element) {
  1538. continue;
  1539. }
  1540. child_element->set_selected(child_element == graph_element);
  1541. }
  1542. }
  1543. graph_element->set_selected(true);
  1544. for (int i = 0; i < get_child_count(); i++) {
  1545. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1546. if (!child_element) {
  1547. continue;
  1548. }
  1549. if (child_element->is_selected()) {
  1550. child_element->set_drag(true);
  1551. GraphFrame *frame_node = Object::cast_to<GraphFrame>(get_child(i));
  1552. if (frame_node) {
  1553. _ensure_node_order_from(frame_node);
  1554. _set_drag_frame_attached_nodes(frame_node, true);
  1555. }
  1556. }
  1557. }
  1558. } else {
  1559. if (_filter_input(mb->get_position())) {
  1560. return;
  1561. }
  1562. if (panner->is_panning()) {
  1563. return;
  1564. }
  1565. // Left-clicked on empty space, start box select.
  1566. box_selecting = true;
  1567. box_selecting_from = mb->get_position();
  1568. if (mb->is_command_or_control_pressed()) {
  1569. box_selection_mode_additive = true;
  1570. prev_selected.clear();
  1571. for (int i = get_child_count() - 1; i >= 0; i--) {
  1572. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1573. if (!child_element || !child_element->is_selected()) {
  1574. continue;
  1575. }
  1576. prev_selected.push_back(child_element);
  1577. }
  1578. } else if (mb->is_shift_pressed()) {
  1579. box_selection_mode_additive = false;
  1580. prev_selected.clear();
  1581. for (int i = get_child_count() - 1; i >= 0; i--) {
  1582. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1583. if (!child_element || !child_element->is_selected()) {
  1584. continue;
  1585. }
  1586. prev_selected.push_back(child_element);
  1587. }
  1588. } else {
  1589. box_selection_mode_additive = true;
  1590. prev_selected.clear();
  1591. for (int i = get_child_count() - 1; i >= 0; i--) {
  1592. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1593. if (!child_element) {
  1594. continue;
  1595. }
  1596. child_element->set_selected(false);
  1597. }
  1598. }
  1599. }
  1600. }
  1601. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
  1602. // Box selection ended. Nodes were selected during mouse movement.
  1603. box_selecting = false;
  1604. box_selecting_rect = Rect2();
  1605. prev_selected.clear();
  1606. top_layer->queue_redraw();
  1607. minimap->queue_redraw();
  1608. }
  1609. }
  1610. if (p_ev->is_pressed()) {
  1611. if (p_ev->is_action("ui_graph_duplicate", true)) {
  1612. emit_signal(SNAME("duplicate_nodes_request"));
  1613. accept_event();
  1614. } else if (p_ev->is_action("ui_copy", true)) {
  1615. emit_signal(SNAME("copy_nodes_request"));
  1616. accept_event();
  1617. } else if (p_ev->is_action("ui_paste", true)) {
  1618. emit_signal(SNAME("paste_nodes_request"));
  1619. accept_event();
  1620. } else if (p_ev->is_action("ui_graph_delete", true)) {
  1621. TypedArray<StringName> nodes;
  1622. for (int i = 0; i < get_child_count(); i++) {
  1623. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1624. if (!graph_element) {
  1625. continue;
  1626. }
  1627. if (graph_element->is_selected()) {
  1628. nodes.push_back(graph_element->get_name());
  1629. }
  1630. }
  1631. emit_signal(SNAME("delete_nodes_request"), nodes);
  1632. accept_event();
  1633. }
  1634. }
  1635. }
  1636. void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1637. h_scrollbar->set_value(h_scrollbar->get_value() - p_scroll_vec.x);
  1638. v_scrollbar->set_value(v_scrollbar->get_value() - p_scroll_vec.y);
  1639. connections_layer->queue_redraw();
  1640. }
  1641. void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1642. // We need to invalidate all connections since we don't know whether
  1643. // the user is zooming/panning at the same time.
  1644. _invalidate_connection_line_cache();
  1645. set_zoom_custom(zoom * p_zoom_factor, p_origin);
  1646. }
  1647. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1648. for (Ref<Connection> &c : connection_map[p_from]) {
  1649. if (c->from_node == p_from && c->from_port == p_from_port && c->to_node == p_to && c->to_port == p_to_port) {
  1650. if (!Math::is_equal_approx(c->activity, p_activity)) {
  1651. // Update only if changed.
  1652. minimap->queue_redraw();
  1653. c->_cache.dirty = true;
  1654. connections_layer->queue_redraw();
  1655. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1656. }
  1657. c->activity = p_activity;
  1658. return;
  1659. }
  1660. }
  1661. }
  1662. void GraphEdit::reset_all_connection_activity() {
  1663. bool changed = false;
  1664. for (Ref<Connection> &conn : connections) {
  1665. if (conn->activity > 0) {
  1666. changed = true;
  1667. conn->_cache.dirty = true;
  1668. }
  1669. conn->activity = 0;
  1670. }
  1671. if (changed) {
  1672. connections_layer->queue_redraw();
  1673. }
  1674. }
  1675. void GraphEdit::clear_connections() {
  1676. for (Ref<Connection> &c : connections) {
  1677. c->_cache.line->queue_free();
  1678. }
  1679. connections.clear();
  1680. connection_map.clear();
  1681. minimap->queue_redraw();
  1682. queue_redraw();
  1683. connections_layer->queue_redraw();
  1684. }
  1685. void GraphEdit::force_connection_drag_end() {
  1686. ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
  1687. connecting = false;
  1688. connecting_valid = false;
  1689. minimap->queue_redraw();
  1690. queue_redraw();
  1691. connections_layer->queue_redraw();
  1692. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1693. emit_signal(SNAME("connection_drag_ended"));
  1694. }
  1695. bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) {
  1696. bool valid = true;
  1697. GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid);
  1698. return valid;
  1699. }
  1700. void GraphEdit::set_panning_scheme(PanningScheme p_scheme) {
  1701. panning_scheme = p_scheme;
  1702. panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme);
  1703. }
  1704. GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const {
  1705. return panning_scheme;
  1706. }
  1707. void GraphEdit::set_zoom(float p_zoom) {
  1708. set_zoom_custom(p_zoom, get_size() / 2);
  1709. }
  1710. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1711. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1712. if (zoom == p_zoom) {
  1713. return;
  1714. }
  1715. Vector2 scrollbar_offset = (Vector2(h_scrollbar->get_value(), v_scrollbar->get_value()) + p_center) / zoom;
  1716. zoom = p_zoom;
  1717. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1718. zoom_minus_button->set_disabled(zoom == zoom_min);
  1719. zoom_plus_button->set_disabled(zoom == zoom_max);
  1720. _update_scroll();
  1721. minimap->queue_redraw();
  1722. connections_layer->queue_redraw();
  1723. if (is_visible_in_tree()) {
  1724. Vector2 offset = scrollbar_offset * zoom - p_center;
  1725. h_scrollbar->set_value(offset.x);
  1726. v_scrollbar->set_value(offset.y);
  1727. }
  1728. _update_zoom_label();
  1729. queue_redraw();
  1730. }
  1731. float GraphEdit::get_zoom() const {
  1732. return zoom;
  1733. }
  1734. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1735. p_zoom_step = abs(p_zoom_step);
  1736. ERR_FAIL_COND(!isfinite(p_zoom_step));
  1737. if (zoom_step == p_zoom_step) {
  1738. return;
  1739. }
  1740. zoom_step = p_zoom_step;
  1741. panner->set_scroll_zoom_factor(zoom_step);
  1742. }
  1743. float GraphEdit::get_zoom_step() const {
  1744. return zoom_step;
  1745. }
  1746. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1747. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1748. if (zoom_min == p_zoom_min) {
  1749. return;
  1750. }
  1751. zoom_min = p_zoom_min;
  1752. set_zoom(zoom);
  1753. }
  1754. float GraphEdit::get_zoom_min() const {
  1755. return zoom_min;
  1756. }
  1757. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1758. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1759. if (zoom_max == p_zoom_max) {
  1760. return;
  1761. }
  1762. zoom_max = p_zoom_max;
  1763. set_zoom(zoom);
  1764. }
  1765. float GraphEdit::get_zoom_max() const {
  1766. return zoom_max;
  1767. }
  1768. void GraphEdit::set_right_disconnects(bool p_enable) {
  1769. right_disconnects = p_enable;
  1770. }
  1771. bool GraphEdit::is_right_disconnects_enabled() const {
  1772. return right_disconnects;
  1773. }
  1774. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1775. valid_right_disconnect_types.insert(p_type);
  1776. }
  1777. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1778. valid_right_disconnect_types.erase(p_type);
  1779. }
  1780. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1781. valid_left_disconnect_types.insert(p_type);
  1782. }
  1783. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1784. valid_left_disconnect_types.erase(p_type);
  1785. }
  1786. TypedArray<Dictionary> GraphEdit::_get_connection_list() const {
  1787. List<Ref<Connection>> conns = get_connection_list();
  1788. TypedArray<Dictionary> arr;
  1789. for (const Ref<Connection> &conn : conns) {
  1790. Dictionary d;
  1791. d["from_node"] = conn->from_node;
  1792. d["from_port"] = conn->from_port;
  1793. d["to_node"] = conn->to_node;
  1794. d["to_port"] = conn->to_port;
  1795. arr.push_back(d);
  1796. }
  1797. return arr;
  1798. }
  1799. Dictionary GraphEdit::_get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance) const {
  1800. Dictionary ret;
  1801. Ref<Connection> c = get_closest_connection_at_point(p_point, p_max_distance);
  1802. if (c.is_valid()) {
  1803. ret["from_node"] = c->from_node;
  1804. ret["from_port"] = c->from_port;
  1805. ret["to_node"] = c->to_node;
  1806. ret["to_port"] = c->to_port;
  1807. }
  1808. return ret;
  1809. }
  1810. TypedArray<Dictionary> GraphEdit::_get_connections_intersecting_with_rect(const Rect2 &p_rect) const {
  1811. List<Ref<Connection>> intersecting_connections = get_connections_intersecting_with_rect(p_rect);
  1812. TypedArray<Dictionary> arr;
  1813. for (const Ref<Connection> &conn : intersecting_connections) {
  1814. Dictionary d;
  1815. d["from_node"] = conn->from_node;
  1816. d["from_port"] = conn->from_port;
  1817. d["to_node"] = conn->to_node;
  1818. d["to_port"] = conn->to_port;
  1819. arr.push_back(d);
  1820. }
  1821. return arr;
  1822. }
  1823. void GraphEdit::_zoom_minus() {
  1824. set_zoom(zoom / zoom_step);
  1825. }
  1826. void GraphEdit::_zoom_reset() {
  1827. set_zoom(1);
  1828. }
  1829. void GraphEdit::_zoom_plus() {
  1830. set_zoom(zoom * zoom_step);
  1831. }
  1832. void GraphEdit::_update_zoom_label() {
  1833. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1834. String zoom_text = itos(zoom_percent) + "%";
  1835. zoom_label->set_text(zoom_text);
  1836. }
  1837. void GraphEdit::_invalidate_connection_line_cache() {
  1838. for (Ref<Connection> &c : connections) {
  1839. c->_cache.dirty = true;
  1840. }
  1841. }
  1842. float GraphEdit::_get_shader_line_width() {
  1843. return lines_thickness * theme_cache.base_scale + 4.0;
  1844. }
  1845. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1846. ConnectionType ct(p_type, p_with_type);
  1847. valid_connection_types.insert(ct);
  1848. }
  1849. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1850. ConnectionType ct(p_type, p_with_type);
  1851. valid_connection_types.erase(ct);
  1852. }
  1853. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1854. ConnectionType ct(p_type, p_with_type);
  1855. return valid_connection_types.has(ct);
  1856. }
  1857. void GraphEdit::attach_graph_element_to_frame(const StringName &p_graph_element, const StringName &p_parent_frame) {
  1858. GraphFrame *frame = Object::cast_to<GraphFrame>(get_node(NodePath(p_parent_frame)));
  1859. ERR_FAIL_NULL_MSG(frame, "Frame does not exist or is not of type GraphFrame.");
  1860. GraphElement *graph_element = Object::cast_to<GraphElement>(get_node(NodePath(p_graph_element)));
  1861. ERR_FAIL_NULL_MSG(graph_element, "Graph element to attach does not exist or is not of type GraphElement.");
  1862. ERR_FAIL_COND_MSG(frame == graph_element, "Cannot attach a frame to itself.");
  1863. linked_parent_map.insert(p_graph_element, p_parent_frame);
  1864. frame_attached_nodes[p_parent_frame].insert(p_graph_element);
  1865. _ensure_node_order_from_root(p_graph_element);
  1866. _update_graph_frame(frame);
  1867. }
  1868. void GraphEdit::detach_graph_element_from_frame(const StringName &p_graph_element) {
  1869. if (!linked_parent_map.has(p_graph_element)) {
  1870. return;
  1871. }
  1872. GraphFrame *frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[p_graph_element])));
  1873. ERR_FAIL_NULL_MSG(frame, "Frame does not exist or is not of type GraphFrame.");
  1874. GraphElement *graph_element = Object::cast_to<GraphElement>(get_node(NodePath(p_graph_element)));
  1875. ERR_FAIL_NULL_MSG(graph_element, "Graph element to detach does not exist or is not of type GraphElement.");
  1876. frame_attached_nodes.get(frame->get_name()).erase(p_graph_element);
  1877. linked_parent_map.erase(p_graph_element);
  1878. _update_graph_frame(frame);
  1879. }
  1880. GraphFrame *GraphEdit::get_element_frame(const StringName &p_attached_graph_element) {
  1881. if (!linked_parent_map.has(p_attached_graph_element)) {
  1882. return nullptr;
  1883. }
  1884. Node *parent = get_node_or_null(NodePath(linked_parent_map[p_attached_graph_element]));
  1885. return Object::cast_to<GraphFrame>(parent);
  1886. }
  1887. TypedArray<StringName> GraphEdit::get_attached_nodes_of_frame(const StringName &p_graph_frame) {
  1888. if (!frame_attached_nodes.has(p_graph_frame)) {
  1889. return TypedArray<StringName>();
  1890. }
  1891. TypedArray<StringName> attached_nodes;
  1892. for (const StringName &node : frame_attached_nodes.get(p_graph_frame)) {
  1893. attached_nodes.push_back(node);
  1894. }
  1895. return attached_nodes;
  1896. }
  1897. void GraphEdit::set_snapping_enabled(bool p_enable) {
  1898. if (snapping_enabled == p_enable) {
  1899. return;
  1900. }
  1901. snapping_enabled = p_enable;
  1902. toggle_snapping_button->set_pressed(p_enable);
  1903. queue_redraw();
  1904. }
  1905. bool GraphEdit::is_snapping_enabled() const {
  1906. return snapping_enabled;
  1907. }
  1908. void GraphEdit::set_snapping_distance(int p_snapping_distance) {
  1909. ERR_FAIL_COND_MSG(p_snapping_distance < GRID_MIN_SNAPPING_DISTANCE || p_snapping_distance > GRID_MAX_SNAPPING_DISTANCE,
  1910. vformat("GraphEdit's snapping distance must be between %d and %d (inclusive)", GRID_MIN_SNAPPING_DISTANCE, GRID_MAX_SNAPPING_DISTANCE));
  1911. snapping_distance = p_snapping_distance;
  1912. snapping_distance_spinbox->set_value(p_snapping_distance);
  1913. queue_redraw();
  1914. }
  1915. int GraphEdit::get_snapping_distance() const {
  1916. return snapping_distance;
  1917. }
  1918. void GraphEdit::set_show_grid(bool p_show) {
  1919. if (show_grid == p_show) {
  1920. return;
  1921. }
  1922. show_grid = p_show;
  1923. toggle_grid_button->set_pressed(p_show);
  1924. queue_redraw();
  1925. }
  1926. bool GraphEdit::is_showing_grid() const {
  1927. return show_grid;
  1928. }
  1929. void GraphEdit::set_grid_pattern(GridPattern p_pattern) {
  1930. if (grid_pattern == p_pattern) {
  1931. return;
  1932. }
  1933. grid_pattern = p_pattern;
  1934. queue_redraw();
  1935. }
  1936. GraphEdit::GridPattern GraphEdit::get_grid_pattern() const {
  1937. return grid_pattern;
  1938. }
  1939. void GraphEdit::_snapping_toggled() {
  1940. snapping_enabled = toggle_snapping_button->is_pressed();
  1941. }
  1942. void GraphEdit::_snapping_distance_changed(double) {
  1943. snapping_distance = snapping_distance_spinbox->get_value();
  1944. queue_redraw();
  1945. }
  1946. void GraphEdit::_show_grid_toggled() {
  1947. show_grid = toggle_grid_button->is_pressed();
  1948. queue_redraw();
  1949. }
  1950. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1951. minimap->set_size(p_size);
  1952. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1953. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1954. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  1955. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  1956. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1957. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1958. minimap->queue_redraw();
  1959. }
  1960. Vector2 GraphEdit::get_minimap_size() const {
  1961. return minimap->get_size();
  1962. }
  1963. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1964. if (minimap->get_modulate().a == p_opacity) {
  1965. return;
  1966. }
  1967. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1968. minimap->queue_redraw();
  1969. }
  1970. float GraphEdit::get_minimap_opacity() const {
  1971. Color minimap_modulate = minimap->get_modulate();
  1972. return minimap_modulate.a;
  1973. }
  1974. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1975. if (minimap_button->is_pressed() == p_enable) {
  1976. return;
  1977. }
  1978. minimap_button->set_pressed(p_enable);
  1979. _minimap_toggled();
  1980. minimap->queue_redraw();
  1981. }
  1982. bool GraphEdit::is_minimap_enabled() const {
  1983. return minimap_button->is_pressed();
  1984. }
  1985. void GraphEdit::set_show_menu(bool p_hidden) {
  1986. show_menu = p_hidden;
  1987. menu_panel->set_visible(show_menu);
  1988. }
  1989. bool GraphEdit::is_showing_menu() const {
  1990. return show_menu;
  1991. }
  1992. void GraphEdit::set_show_zoom_label(bool p_hidden) {
  1993. show_zoom_label = p_hidden;
  1994. zoom_label->set_visible(show_zoom_label);
  1995. }
  1996. bool GraphEdit::is_showing_zoom_label() const {
  1997. return show_zoom_label;
  1998. }
  1999. void GraphEdit::set_show_zoom_buttons(bool p_hidden) {
  2000. show_zoom_buttons = p_hidden;
  2001. zoom_minus_button->set_visible(show_zoom_buttons);
  2002. zoom_reset_button->set_visible(show_zoom_buttons);
  2003. zoom_plus_button->set_visible(show_zoom_buttons);
  2004. }
  2005. bool GraphEdit::is_showing_zoom_buttons() const {
  2006. return show_zoom_buttons;
  2007. }
  2008. void GraphEdit::set_show_grid_buttons(bool p_hidden) {
  2009. show_grid_buttons = p_hidden;
  2010. toggle_grid_button->set_visible(show_grid_buttons);
  2011. toggle_snapping_button->set_visible(show_grid_buttons);
  2012. snapping_distance_spinbox->set_visible(show_grid_buttons);
  2013. }
  2014. bool GraphEdit::is_showing_grid_buttons() const {
  2015. return show_grid_buttons;
  2016. }
  2017. void GraphEdit::set_show_minimap_button(bool p_hidden) {
  2018. show_minimap_button = p_hidden;
  2019. minimap_button->set_visible(show_minimap_button);
  2020. }
  2021. bool GraphEdit::is_showing_minimap_button() const {
  2022. return show_minimap_button;
  2023. }
  2024. void GraphEdit::set_show_arrange_button(bool p_hidden) {
  2025. show_arrange_button = p_hidden;
  2026. arrange_button->set_visible(show_arrange_button);
  2027. }
  2028. bool GraphEdit::is_showing_arrange_button() const {
  2029. return show_arrange_button;
  2030. }
  2031. void GraphEdit::override_connections_shader(const Ref<Shader> &p_shader) {
  2032. connections_shader = p_shader;
  2033. _invalidate_connection_line_cache();
  2034. connections_layer->queue_redraw();
  2035. minimap->queue_redraw();
  2036. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  2037. }
  2038. void GraphEdit::_minimap_toggled() {
  2039. if (is_minimap_enabled()) {
  2040. minimap->set_visible(true);
  2041. minimap->queue_redraw();
  2042. } else {
  2043. minimap->set_visible(false);
  2044. }
  2045. }
  2046. void GraphEdit::set_connection_lines_curvature(float p_curvature) {
  2047. lines_curvature = p_curvature;
  2048. _invalidate_connection_line_cache();
  2049. connections_layer->queue_redraw();
  2050. queue_redraw();
  2051. }
  2052. float GraphEdit::get_connection_lines_curvature() const {
  2053. return lines_curvature;
  2054. }
  2055. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  2056. ERR_FAIL_COND_MSG(p_thickness < 0, "Connection lines thickness must be greater than or equal to 0.");
  2057. if (lines_thickness == p_thickness) {
  2058. return;
  2059. }
  2060. lines_thickness = p_thickness;
  2061. _invalidate_connection_line_cache();
  2062. connections_layer->queue_redraw();
  2063. queue_redraw();
  2064. }
  2065. float GraphEdit::get_connection_lines_thickness() const {
  2066. return lines_thickness;
  2067. }
  2068. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  2069. if (lines_antialiased == p_antialiased) {
  2070. return;
  2071. }
  2072. lines_antialiased = p_antialiased;
  2073. _invalidate_connection_line_cache();
  2074. connections_layer->queue_redraw();
  2075. queue_redraw();
  2076. }
  2077. bool GraphEdit::is_connection_lines_antialiased() const {
  2078. return lines_antialiased;
  2079. }
  2080. HBoxContainer *GraphEdit::get_menu_hbox() {
  2081. return menu_hbox;
  2082. }
  2083. Ref<ViewPanner> GraphEdit::get_panner() {
  2084. return panner;
  2085. }
  2086. void GraphEdit::set_warped_panning(bool p_warped) {
  2087. warped_panning = p_warped;
  2088. }
  2089. void GraphEdit::arrange_nodes() {
  2090. arranger->arrange_nodes();
  2091. }
  2092. void GraphEdit::_bind_methods() {
  2093. ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::connect_node);
  2094. ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
  2095. ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
  2096. ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
  2097. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  2098. ClassDB::bind_method(D_METHOD("get_closest_connection_at_point", "point", "max_distance"), &GraphEdit::_get_closest_connection_at_point, DEFVAL(4.0));
  2099. ClassDB::bind_method(D_METHOD("get_connections_intersecting_with_rect", "rect"), &GraphEdit::_get_connections_intersecting_with_rect);
  2100. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  2101. ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end);
  2102. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &GraphEdit::get_scroll_offset);
  2103. ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &GraphEdit::set_scroll_offset);
  2104. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  2105. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  2106. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  2107. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  2108. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  2109. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  2110. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  2111. ClassDB::bind_method(D_METHOD("get_connection_line", "from_node", "to_node"), &GraphEdit::get_connection_line);
  2112. ClassDB::bind_method(D_METHOD("attach_graph_element_to_frame", "element", "frame"), &GraphEdit::attach_graph_element_to_frame);
  2113. ClassDB::bind_method(D_METHOD("detach_graph_element_from_frame", "element"), &GraphEdit::detach_graph_element_from_frame);
  2114. ClassDB::bind_method(D_METHOD("get_element_frame", "element"), &GraphEdit::get_element_frame);
  2115. ClassDB::bind_method(D_METHOD("get_attached_nodes_of_frame", "frame"), &GraphEdit::get_attached_nodes_of_frame);
  2116. ClassDB::bind_method(D_METHOD("set_panning_scheme", "scheme"), &GraphEdit::set_panning_scheme);
  2117. ClassDB::bind_method(D_METHOD("get_panning_scheme"), &GraphEdit::get_panning_scheme);
  2118. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  2119. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  2120. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  2121. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  2122. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  2123. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  2124. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  2125. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  2126. ClassDB::bind_method(D_METHOD("set_show_grid", "enable"), &GraphEdit::set_show_grid);
  2127. ClassDB::bind_method(D_METHOD("is_showing_grid"), &GraphEdit::is_showing_grid);
  2128. ClassDB::bind_method(D_METHOD("set_grid_pattern", "pattern"), &GraphEdit::set_grid_pattern);
  2129. ClassDB::bind_method(D_METHOD("get_grid_pattern"), &GraphEdit::get_grid_pattern);
  2130. ClassDB::bind_method(D_METHOD("set_snapping_enabled", "enable"), &GraphEdit::set_snapping_enabled);
  2131. ClassDB::bind_method(D_METHOD("is_snapping_enabled"), &GraphEdit::is_snapping_enabled);
  2132. ClassDB::bind_method(D_METHOD("set_snapping_distance", "pixels"), &GraphEdit::set_snapping_distance);
  2133. ClassDB::bind_method(D_METHOD("get_snapping_distance"), &GraphEdit::get_snapping_distance);
  2134. ClassDB::bind_method(D_METHOD("set_connection_lines_curvature", "curvature"), &GraphEdit::set_connection_lines_curvature);
  2135. ClassDB::bind_method(D_METHOD("get_connection_lines_curvature"), &GraphEdit::get_connection_lines_curvature);
  2136. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  2137. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  2138. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  2139. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  2140. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  2141. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  2142. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  2143. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  2144. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  2145. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  2146. ClassDB::bind_method(D_METHOD("set_show_menu", "hidden"), &GraphEdit::set_show_menu);
  2147. ClassDB::bind_method(D_METHOD("is_showing_menu"), &GraphEdit::is_showing_menu);
  2148. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  2149. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  2150. ClassDB::bind_method(D_METHOD("set_show_grid_buttons", "hidden"), &GraphEdit::set_show_grid_buttons);
  2151. ClassDB::bind_method(D_METHOD("is_showing_grid_buttons"), &GraphEdit::is_showing_grid_buttons);
  2152. ClassDB::bind_method(D_METHOD("set_show_zoom_buttons", "hidden"), &GraphEdit::set_show_zoom_buttons);
  2153. ClassDB::bind_method(D_METHOD("is_showing_zoom_buttons"), &GraphEdit::is_showing_zoom_buttons);
  2154. ClassDB::bind_method(D_METHOD("set_show_minimap_button", "hidden"), &GraphEdit::set_show_minimap_button);
  2155. ClassDB::bind_method(D_METHOD("is_showing_minimap_button"), &GraphEdit::is_showing_minimap_button);
  2156. ClassDB::bind_method(D_METHOD("set_show_arrange_button", "hidden"), &GraphEdit::set_show_arrange_button);
  2157. ClassDB::bind_method(D_METHOD("is_showing_arrange_button"), &GraphEdit::is_showing_arrange_button);
  2158. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  2159. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  2160. GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node", "in_port", "mouse_position");
  2161. GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node", "in_port", "mouse_position");
  2162. ClassDB::bind_method(D_METHOD("get_menu_hbox"), &GraphEdit::get_menu_hbox);
  2163. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  2164. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  2165. GDVIRTUAL_BIND(_get_connection_line, "from_position", "to_position")
  2166. GDVIRTUAL_BIND(_is_node_hover_valid, "from_node", "from_port", "to_node", "to_port");
  2167. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
  2168. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid"), "set_show_grid", "is_showing_grid");
  2169. ADD_PROPERTY(PropertyInfo(Variant::INT, "grid_pattern", PROPERTY_HINT_ENUM, "Lines,Dots"), "set_grid_pattern", "get_grid_pattern");
  2170. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snapping_enabled"), "set_snapping_enabled", "is_snapping_enabled");
  2171. ADD_PROPERTY(PropertyInfo(Variant::INT, "snapping_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snapping_distance", "get_snapping_distance");
  2172. ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
  2173. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  2174. ADD_GROUP("Connection Lines", "connection_lines");
  2175. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
  2176. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness", PROPERTY_HINT_RANGE, "0,100,0.1,suffix:px"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  2177. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  2178. ADD_GROUP("Zoom", "");
  2179. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  2180. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  2181. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  2182. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  2183. ADD_GROUP("Minimap", "minimap_");
  2184. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  2185. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_size", "get_minimap_size");
  2186. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  2187. ADD_GROUP("Toolbar Menu", "");
  2188. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_menu"), "set_show_menu", "is_showing_menu");
  2189. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  2190. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_buttons"), "set_show_zoom_buttons", "is_showing_zoom_buttons");
  2191. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid_buttons"), "set_show_grid_buttons", "is_showing_grid_buttons");
  2192. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_minimap_button"), "set_show_minimap_button", "is_showing_minimap_button");
  2193. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_arrange_button"), "set_show_arrange_button", "is_showing_arrange_button");
  2194. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  2195. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  2196. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2197. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2198. ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output")));
  2199. ADD_SIGNAL(MethodInfo("connection_drag_ended"));
  2200. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  2201. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  2202. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  2203. ADD_SIGNAL(MethodInfo("delete_nodes_request", PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_ARRAY_TYPE, "StringName")));
  2204. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2205. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2206. ADD_SIGNAL(MethodInfo("frame_rect_changed", PropertyInfo(Variant::OBJECT, "frame", PROPERTY_HINT_RESOURCE_TYPE, "GraphFrame"), PropertyInfo(Variant::VECTOR2, "new_rect")));
  2207. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "at_position")));
  2208. ADD_SIGNAL(MethodInfo("begin_node_move"));
  2209. ADD_SIGNAL(MethodInfo("end_node_move"));
  2210. ADD_SIGNAL(MethodInfo("graph_elements_linked_to_frame_request", PropertyInfo(Variant::ARRAY, "elements"), PropertyInfo(Variant::STRING_NAME, "frame")));
  2211. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "offset")));
  2212. BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
  2213. BIND_ENUM_CONSTANT(SCROLL_PANS);
  2214. BIND_ENUM_CONSTANT(GRID_PATTERN_LINES);
  2215. BIND_ENUM_CONSTANT(GRID_PATTERN_DOTS);
  2216. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, panel);
  2217. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_major);
  2218. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_minor);
  2219. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, GraphEdit, activity_color, "activity");
  2220. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_hover_tint_color);
  2221. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_valid_target_tint_color);
  2222. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_rim_color);
  2223. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_fill);
  2224. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_stroke);
  2225. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, menu_panel);
  2226. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_in);
  2227. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_out);
  2228. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_reset);
  2229. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, snapping_toggle);
  2230. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, grid_toggle);
  2231. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, minimap_toggle);
  2232. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, layout);
  2233. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_inner_extent);
  2234. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_outer_extent);
  2235. }
  2236. GraphEdit::GraphEdit() {
  2237. set_focus_mode(FOCUS_ALL);
  2238. // Allow dezooming 8 times from the default zoom level.
  2239. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  2240. // but this is still useful for previewing and navigation.
  2241. zoom_min = (1 / Math::pow(zoom_step, 8));
  2242. // Allow zooming 4 times from the default zoom level.
  2243. zoom_max = (1 * Math::pow(zoom_step, 4));
  2244. panner.instantiate();
  2245. panner->set_callbacks(callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback));
  2246. top_layer = memnew(Control);
  2247. add_child(top_layer, false, INTERNAL_MODE_BACK);
  2248. top_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2249. top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2250. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  2251. top_layer->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  2252. connections_layer = memnew(Control);
  2253. add_child(connections_layer, false);
  2254. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_update_connections));
  2255. connections_layer->set_name("_connection_layer");
  2256. connections_layer->set_disable_visibility_clip(true); // Necessary, so it can draw freely and be offset.
  2257. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2258. top_connection_layer = memnew(GraphEditFilter(this));
  2259. add_child(top_connection_layer, false, INTERNAL_MODE_BACK);
  2260. connections_shader = default_connections_shader;
  2261. top_connection_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  2262. top_connection_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2263. top_connection_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_connection_layer_input));
  2264. dragged_connection_line = memnew(Line2D);
  2265. dragged_connection_line->set_texture_mode(Line2D::LINE_TEXTURE_STRETCH);
  2266. top_connection_layer->add_child(dragged_connection_line);
  2267. h_scrollbar = memnew(HScrollBar);
  2268. h_scrollbar->set_name("_h_scroll");
  2269. top_layer->add_child(h_scrollbar);
  2270. v_scrollbar = memnew(VScrollBar);
  2271. v_scrollbar->set_name("_v_scroll");
  2272. top_layer->add_child(v_scrollbar);
  2273. // Set large minmax so it can scroll even if not resized yet.
  2274. h_scrollbar->set_min(-10000);
  2275. h_scrollbar->set_max(10000);
  2276. v_scrollbar->set_min(-10000);
  2277. v_scrollbar->set_max(10000);
  2278. h_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2279. v_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2280. // Toolbar menu.
  2281. menu_panel = memnew(PanelContainer);
  2282. menu_panel->set_visible(show_menu);
  2283. top_layer->add_child(menu_panel);
  2284. menu_panel->set_position(Vector2(10, 10));
  2285. menu_hbox = memnew(HBoxContainer);
  2286. menu_panel->add_child(menu_hbox);
  2287. // Zoom label and controls.
  2288. zoom_label = memnew(Label);
  2289. zoom_label->set_visible(show_zoom_label);
  2290. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  2291. zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2292. zoom_label->set_custom_minimum_size(Size2(48, 0));
  2293. menu_hbox->add_child(zoom_label);
  2294. _update_zoom_label();
  2295. zoom_minus_button = memnew(Button);
  2296. zoom_minus_button->set_theme_type_variation("FlatButton");
  2297. zoom_minus_button->set_visible(show_zoom_buttons);
  2298. zoom_minus_button->set_tooltip_text(ETR("Zoom Out"));
  2299. zoom_minus_button->set_focus_mode(FOCUS_NONE);
  2300. menu_hbox->add_child(zoom_minus_button);
  2301. zoom_minus_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  2302. zoom_reset_button = memnew(Button);
  2303. zoom_reset_button->set_theme_type_variation("FlatButton");
  2304. zoom_reset_button->set_visible(show_zoom_buttons);
  2305. zoom_reset_button->set_tooltip_text(ETR("Zoom Reset"));
  2306. zoom_reset_button->set_focus_mode(FOCUS_NONE);
  2307. menu_hbox->add_child(zoom_reset_button);
  2308. zoom_reset_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  2309. zoom_plus_button = memnew(Button);
  2310. zoom_plus_button->set_theme_type_variation("FlatButton");
  2311. zoom_plus_button->set_visible(show_zoom_buttons);
  2312. zoom_plus_button->set_tooltip_text(ETR("Zoom In"));
  2313. zoom_plus_button->set_focus_mode(FOCUS_NONE);
  2314. menu_hbox->add_child(zoom_plus_button);
  2315. zoom_plus_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  2316. // Grid controls.
  2317. toggle_grid_button = memnew(Button);
  2318. toggle_grid_button->set_theme_type_variation("FlatButton");
  2319. toggle_grid_button->set_visible(show_grid_buttons);
  2320. toggle_grid_button->set_toggle_mode(true);
  2321. toggle_grid_button->set_pressed(true);
  2322. toggle_grid_button->set_tooltip_text(ETR("Toggle the visual grid."));
  2323. toggle_grid_button->set_focus_mode(FOCUS_NONE);
  2324. menu_hbox->add_child(toggle_grid_button);
  2325. toggle_grid_button->connect("pressed", callable_mp(this, &GraphEdit::_show_grid_toggled));
  2326. toggle_snapping_button = memnew(Button);
  2327. toggle_snapping_button->set_theme_type_variation("FlatButton");
  2328. toggle_snapping_button->set_visible(show_grid_buttons);
  2329. toggle_snapping_button->set_toggle_mode(true);
  2330. toggle_snapping_button->set_tooltip_text(ETR("Toggle snapping to the grid."));
  2331. toggle_snapping_button->set_pressed(snapping_enabled);
  2332. toggle_snapping_button->set_focus_mode(FOCUS_NONE);
  2333. menu_hbox->add_child(toggle_snapping_button);
  2334. toggle_snapping_button->connect("pressed", callable_mp(this, &GraphEdit::_snapping_toggled));
  2335. snapping_distance_spinbox = memnew(SpinBox);
  2336. snapping_distance_spinbox->set_visible(show_grid_buttons);
  2337. snapping_distance_spinbox->set_min(GRID_MIN_SNAPPING_DISTANCE);
  2338. snapping_distance_spinbox->set_max(GRID_MAX_SNAPPING_DISTANCE);
  2339. snapping_distance_spinbox->set_step(1);
  2340. snapping_distance_spinbox->set_value(snapping_distance);
  2341. snapping_distance_spinbox->set_tooltip_text(ETR("Change the snapping distance."));
  2342. menu_hbox->add_child(snapping_distance_spinbox);
  2343. snapping_distance_spinbox->connect("value_changed", callable_mp(this, &GraphEdit::_snapping_distance_changed));
  2344. // Extra controls.
  2345. minimap_button = memnew(Button);
  2346. minimap_button->set_theme_type_variation("FlatButton");
  2347. minimap_button->set_visible(show_minimap_button);
  2348. minimap_button->set_toggle_mode(true);
  2349. minimap_button->set_tooltip_text(ETR("Toggle the graph minimap."));
  2350. minimap_button->set_pressed(show_grid);
  2351. minimap_button->set_focus_mode(FOCUS_NONE);
  2352. menu_hbox->add_child(minimap_button);
  2353. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  2354. arrange_button = memnew(Button);
  2355. arrange_button->set_theme_type_variation("FlatButton");
  2356. arrange_button->set_visible(show_arrange_button);
  2357. arrange_button->connect("pressed", callable_mp(this, &GraphEdit::arrange_nodes));
  2358. arrange_button->set_focus_mode(FOCUS_NONE);
  2359. menu_hbox->add_child(arrange_button);
  2360. arrange_button->set_tooltip_text(ETR("Automatically arrange selected nodes."));
  2361. // Minimap.
  2362. const Vector2 minimap_size = Vector2(240, 160);
  2363. const float minimap_opacity = 0.65;
  2364. minimap = memnew(GraphEditMinimap(this));
  2365. top_layer->add_child(minimap);
  2366. minimap->set_name("_minimap");
  2367. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  2368. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  2369. minimap->set_custom_minimum_size(Vector2(50, 50));
  2370. minimap->set_size(minimap_size);
  2371. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  2372. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  2373. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  2374. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2375. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2376. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  2377. set_clip_contents(true);
  2378. arranger = Ref<GraphEditArranger>(memnew(GraphEditArranger(this)));
  2379. }