graph_edit.cpp 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352
  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 ge->min_scroll_offset;
  103. }
  104. Vector2 GraphEditMinimap::_get_graph_size() {
  105. Vector2 graph_size = ge->max_scroll_offset - ge->min_scroll_offset;
  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, bool p_keep_alive) {
  216. ERR_FAIL_NULL_V_MSG(connections_layer, FAILED, "connections_layer is missing.");
  217. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  218. return OK;
  219. }
  220. Ref<Connection> c;
  221. c.instantiate();
  222. c->from_node = p_from;
  223. c->from_port = p_from_port;
  224. c->to_node = p_to;
  225. c->to_port = p_to_port;
  226. c->activity = 0;
  227. c->keep_alive = p_keep_alive;
  228. connections.push_back(c);
  229. connection_map[p_from].push_back(c);
  230. connection_map[p_to].push_back(c);
  231. Line2D *line = memnew(Line2D);
  232. line->set_texture_mode(Line2D::LineTextureMode::LINE_TEXTURE_STRETCH);
  233. Ref<ShaderMaterial> line_material;
  234. line_material.instantiate();
  235. line_material->set_shader(connections_shader);
  236. float line_width = _get_shader_line_width();
  237. line_material->set_shader_parameter("line_width", line_width);
  238. line_material->set_shader_parameter("from_type", c->from_port);
  239. line_material->set_shader_parameter("to_type", c->to_port);
  240. Ref<StyleBoxFlat> bg_panel = theme_cache.panel;
  241. Color connection_line_rim_color = bg_panel.is_valid() ? bg_panel->get_bg_color() : Color(0.0, 0.0, 0.0, 0.0);
  242. line_material->set_shader_parameter("rim_color", connection_line_rim_color);
  243. line->set_material(line_material);
  244. connections_layer->add_child(line);
  245. c->_cache.line = line;
  246. minimap->queue_redraw();
  247. queue_redraw();
  248. connections_layer->queue_redraw();
  249. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  250. return OK;
  251. }
  252. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  253. for (const Ref<Connection> &conn : connection_map[p_from]) {
  254. if (conn->from_node == p_from && conn->from_port == p_from_port && conn->to_node == p_to && conn->to_port == p_to_port) {
  255. return true;
  256. }
  257. }
  258. return false;
  259. }
  260. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  261. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  262. Ref<Connection> conn_to_remove;
  263. for (const Ref<Connection> &conn : connections) {
  264. if (conn->from_node == p_from && conn->from_port == p_from_port && conn->to_node == p_to && conn->to_port == p_to_port) {
  265. conn_to_remove = conn;
  266. break;
  267. }
  268. }
  269. if (conn_to_remove.is_valid()) {
  270. connection_map[p_from].erase(conn_to_remove);
  271. connection_map[p_to].erase(conn_to_remove);
  272. conn_to_remove->_cache.line->queue_free();
  273. connections.erase(conn_to_remove);
  274. minimap->queue_redraw();
  275. queue_redraw();
  276. connections_layer->queue_redraw();
  277. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  278. }
  279. }
  280. const Vector<Ref<GraphEdit::Connection>> &GraphEdit::get_connections() const {
  281. return connections;
  282. }
  283. int GraphEdit::get_connection_count(const StringName &p_node, int p_port) {
  284. int count = 0;
  285. for (const Ref<Connection> &conn : connections) {
  286. if ((conn->from_node == p_node && conn->from_port == p_port) || (conn->to_node == p_node && conn->to_port == p_port)) {
  287. count += 1;
  288. }
  289. }
  290. return count;
  291. }
  292. GraphNode *GraphEdit::get_input_connection_target(const StringName &p_node, int p_port) {
  293. for (const Ref<Connection> &conn : connections) {
  294. if (conn->to_node == p_node && conn->to_port == p_port) {
  295. GraphNode *from = Object::cast_to<GraphNode>(get_node(NodePath(conn->from_node)));
  296. if (from) {
  297. return from;
  298. }
  299. }
  300. }
  301. return nullptr;
  302. }
  303. GraphNode *GraphEdit::get_output_connection_target(const StringName &p_node, int p_port) {
  304. for (const Ref<Connection> &conn : connections) {
  305. if (conn->from_node == p_node && conn->from_port == p_port) {
  306. GraphNode *to = Object::cast_to<GraphNode>(get_node(NodePath(conn->to_node)));
  307. if (to) {
  308. return to;
  309. }
  310. }
  311. }
  312. return nullptr;
  313. }
  314. String GraphEdit::get_connections_description(const StringName &p_node, int p_port) {
  315. String out;
  316. for (const Ref<Connection> &conn : connections) {
  317. if (conn->from_node == p_node && conn->from_port == p_port) {
  318. GraphNode *to = Object::cast_to<GraphNode>(get_node(NodePath(conn->to_node)));
  319. if (to) {
  320. if (!out.is_empty()) {
  321. out += ", ";
  322. }
  323. String name = to->get_accessibility_name();
  324. if (name.is_empty()) {
  325. name = to->get_name();
  326. }
  327. out += vformat(ETR("connection to %s (%s) port %d"), name, to->get_title(), conn->to_port);
  328. }
  329. } else if (conn->to_node == p_node && conn->to_port == p_port) {
  330. GraphNode *from = Object::cast_to<GraphNode>(get_node(NodePath(conn->from_node)));
  331. if (from) {
  332. if (!out.is_empty()) {
  333. out += ", ";
  334. }
  335. String name = from->get_accessibility_name();
  336. if (name.is_empty()) {
  337. name = from->get_name();
  338. }
  339. out += vformat(ETR("connection from %s (%s) port %d"), name, from->get_title(), conn->from_port);
  340. }
  341. }
  342. }
  343. return out;
  344. }
  345. void GraphEdit::set_scroll_offset(const Vector2 &p_offset) {
  346. setting_scroll_offset = true;
  347. scroll_offset = p_offset.clamp(min_scroll_offset, max_scroll_offset - get_size());
  348. if (!awaiting_scroll_offset_update) {
  349. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  350. awaiting_scroll_offset_update = true;
  351. }
  352. minimap->queue_redraw();
  353. queue_redraw();
  354. _update_scrollbars();
  355. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  356. setting_scroll_offset = false;
  357. }
  358. Vector2 GraphEdit::get_scroll_offset() const {
  359. return scroll_offset;
  360. }
  361. void GraphEdit::_scrollbar_moved(double) {
  362. scroll_offset.x = h_scrollbar->get_value();
  363. scroll_offset.y = v_scrollbar->get_value();
  364. if (!awaiting_scroll_offset_update) {
  365. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  366. awaiting_scroll_offset_update = true;
  367. }
  368. minimap->queue_redraw();
  369. queue_redraw();
  370. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  371. }
  372. void GraphEdit::_update_scroll_offset() {
  373. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  374. set_block_minimum_size_adjust(true);
  375. for (int i = 0; i < get_child_count(); i++) {
  376. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  377. if (!graph_element) {
  378. continue;
  379. }
  380. Point2 pos = graph_element->get_position_offset() * zoom;
  381. pos -= scroll_offset;
  382. graph_element->set_position(pos);
  383. if (graph_element->get_scale() != Vector2(zoom, zoom)) {
  384. graph_element->set_scale(Vector2(zoom, zoom));
  385. }
  386. }
  387. connections_layer->set_position(-scroll_offset);
  388. set_block_minimum_size_adjust(false);
  389. awaiting_scroll_offset_update = false;
  390. // In Godot, signals on value change are avoided by convention.
  391. if (!setting_scroll_offset) {
  392. emit_signal(SNAME("scroll_offset_changed"), get_scroll_offset());
  393. }
  394. }
  395. void GraphEdit::_update_scrollbars() {
  396. if (updating) {
  397. return;
  398. }
  399. updating = true;
  400. h_scrollbar->set_value_no_signal(scroll_offset.x);
  401. v_scrollbar->set_value_no_signal(scroll_offset.y);
  402. set_block_minimum_size_adjust(true);
  403. // Determine the graph "canvas" size in screen space.
  404. Rect2 screen_rect;
  405. for (int i = 0; i < get_child_count(); i++) {
  406. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  407. if (!graph_element) {
  408. continue;
  409. }
  410. Rect2 node_rect;
  411. node_rect.position = graph_element->get_position_offset() * zoom;
  412. node_rect.size = graph_element->get_size() * zoom;
  413. screen_rect = screen_rect.merge(node_rect);
  414. }
  415. screen_rect.position -= get_size();
  416. screen_rect.size += get_size() * 2.0;
  417. min_scroll_offset = screen_rect.position;
  418. max_scroll_offset = screen_rect.position + screen_rect.size;
  419. h_scrollbar->set_min(screen_rect.position.x);
  420. h_scrollbar->set_max(screen_rect.position.x + screen_rect.size.width);
  421. h_scrollbar->set_page(get_size().x);
  422. if (h_scrollbar->get_max() - h_scrollbar->get_min() <= h_scrollbar->get_page()) {
  423. h_scrollbar->hide();
  424. } else {
  425. h_scrollbar->show();
  426. }
  427. v_scrollbar->set_min(screen_rect.position.y);
  428. v_scrollbar->set_max(screen_rect.position.y + screen_rect.size.height);
  429. v_scrollbar->set_page(get_size().height);
  430. if (v_scrollbar->get_max() - v_scrollbar->get_min() <= v_scrollbar->get_page()) {
  431. v_scrollbar->hide();
  432. } else {
  433. v_scrollbar->show();
  434. }
  435. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  436. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  437. // Avoid scrollbar overlapping.
  438. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scrollbar->is_visible() ? -vmin.width : 0);
  439. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scrollbar->is_visible() ? -hmin.height : 0);
  440. set_block_minimum_size_adjust(false);
  441. if (!awaiting_scroll_offset_update) {
  442. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  443. awaiting_scroll_offset_update = true;
  444. }
  445. updating = false;
  446. }
  447. void GraphEdit::_ensure_node_order_from(Node *p_node) {
  448. GraphElement *graph_node = Object::cast_to<GraphElement>(p_node);
  449. ERR_FAIL_NULL(graph_node);
  450. GraphFrame *frame = Object::cast_to<GraphFrame>(p_node);
  451. // Move a non-frame node directly to the front.
  452. if (!frame) {
  453. graph_node->move_to_front();
  454. return;
  455. }
  456. // Reorder the frames behind the connection layer.
  457. List<GraphFrame *> attached_nodes_to_move;
  458. attached_nodes_to_move.push_back(frame);
  459. while (!attached_nodes_to_move.is_empty()) {
  460. GraphFrame *attached_frame = attached_nodes_to_move.front()->get();
  461. attached_nodes_to_move.pop_front();
  462. // Move the frame to the front of the background node index range.
  463. attached_frame->get_parent()->call_deferred("move_child", attached_frame, background_nodes_separator_idx - 1);
  464. if (!frame_attached_nodes.has(attached_frame->get_name())) {
  465. continue;
  466. }
  467. for (const StringName &attached_node_name : frame_attached_nodes.get(attached_frame->get_name())) {
  468. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  469. GraphFrame *attached_child_frame_node = Object::cast_to<GraphFrame>(attached_node);
  470. if (attached_child_frame_node && (attached_child_frame_node != frame)) {
  471. attached_nodes_to_move.push_back(attached_child_frame_node);
  472. }
  473. }
  474. }
  475. }
  476. void GraphEdit::_graph_element_selected(Node *p_node) {
  477. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  478. ERR_FAIL_NULL(graph_element);
  479. emit_signal(SNAME("node_selected"), graph_element);
  480. }
  481. void GraphEdit::_graph_element_deselected(Node *p_node) {
  482. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  483. ERR_FAIL_NULL(graph_element);
  484. emit_signal(SNAME("node_deselected"), graph_element);
  485. }
  486. void GraphEdit::_graph_element_visibility_changed(GraphElement *p_graph_element) {
  487. if (p_graph_element->is_selected() && !p_graph_element->is_visible()) {
  488. p_graph_element->set_selected(false);
  489. }
  490. }
  491. void GraphEdit::_graph_element_resize_request(const Vector2 &p_new_minsize, Node *p_node) {
  492. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  493. ERR_FAIL_NULL(graph_element);
  494. // Snap the new size to the grid if snapping is enabled.
  495. Vector2 new_size = p_new_minsize;
  496. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  497. new_size = new_size.snappedf(snapping_distance);
  498. }
  499. // Disallow resizing the frame to a size smaller than the minimum size of the attached nodes.
  500. GraphFrame *frame = Object::cast_to<GraphFrame>(graph_element);
  501. if (frame && !frame->is_autoshrink_enabled()) {
  502. Rect2 frame_rect = _compute_shrinked_frame_rect(frame);
  503. Vector2 computed_min_size = (frame_rect.position + frame_rect.size) - frame->get_position_offset();
  504. frame->set_size(new_size.max(computed_min_size));
  505. } else {
  506. graph_element->set_size(new_size);
  507. }
  508. // Update all parent frames recursively bottom-up.
  509. if (linked_parent_map.has(graph_element->get_name())) {
  510. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[graph_element->get_name()])));
  511. if (parent_frame) {
  512. _update_graph_frame(parent_frame);
  513. }
  514. }
  515. }
  516. void GraphEdit::_graph_frame_autoshrink_changed(const Vector2 &p_new_minsize, GraphFrame *p_frame) {
  517. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  518. _update_graph_frame(p_frame);
  519. minimap->queue_redraw();
  520. queue_redraw();
  521. connections_layer->queue_redraw();
  522. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  523. }
  524. void GraphEdit::_graph_element_moved(Node *p_node) {
  525. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  526. ERR_FAIL_NULL(graph_element);
  527. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  528. minimap->queue_redraw();
  529. queue_redraw();
  530. connections_layer->queue_redraw();
  531. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  532. }
  533. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) {
  534. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  535. GraphNode *graph_node = Object::cast_to<GraphNode>(p_node);
  536. ERR_FAIL_NULL(graph_node);
  537. // Update all adjacent connections during the next redraw.
  538. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  539. conn->_cache.dirty = true;
  540. }
  541. minimap->queue_redraw();
  542. queue_redraw();
  543. connections_layer->queue_redraw();
  544. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  545. }
  546. void GraphEdit::_graph_node_rect_changed(GraphNode *p_node) {
  547. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  548. // Only invalidate the cache when zooming or the node is moved/resized in graph space.
  549. if (panner->is_panning()) {
  550. return;
  551. }
  552. for (Ref<Connection> &conn : connection_map[p_node->get_name()]) {
  553. conn->_cache.dirty = true;
  554. }
  555. connections_layer->queue_redraw();
  556. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  557. // Update all parent frames recursively bottom-up.
  558. if (linked_parent_map.has(p_node->get_name())) {
  559. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[p_node->get_name()])));
  560. if (parent_frame) {
  561. _update_graph_frame(parent_frame);
  562. }
  563. }
  564. }
  565. void GraphEdit::_ensure_node_order_from_root(const StringName &p_node) {
  566. // Find the root frame node of the frame tree starting from p_node.
  567. GraphElement *root_frame = Object::cast_to<GraphElement>(get_node(NodePath(p_node)));
  568. ERR_FAIL_NULL(root_frame);
  569. while (linked_parent_map.has(root_frame->get_name())) {
  570. root_frame = Object::cast_to<GraphElement>(get_node(NodePath(linked_parent_map[root_frame->get_name()])));
  571. }
  572. _ensure_node_order_from(root_frame);
  573. }
  574. void GraphEdit::add_child_notify(Node *p_child) {
  575. Control::add_child_notify(p_child);
  576. // Keep the top layer always on top!
  577. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  578. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  579. if (graph_element) {
  580. graph_element->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved).bind(graph_element));
  581. graph_element->connect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected).bind(graph_element));
  582. graph_element->connect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected).bind(graph_element));
  583. graph_element->connect(SceneStringName(visibility_changed), callable_mp(this, &GraphEdit::_graph_element_visibility_changed).bind(graph_element));
  584. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  585. if (graph_node) {
  586. graph_node->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(graph_element));
  587. graph_node->connect("slot_sizes_changed", callable_mp(this, &GraphEdit::_graph_node_rect_changed).bind(graph_node));
  588. graph_node->connect(SceneStringName(item_rect_changed), callable_mp(this, &GraphEdit::_graph_node_rect_changed).bind(graph_node));
  589. _ensure_node_order_from(graph_node);
  590. }
  591. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(graph_element);
  592. if (graph_frame) {
  593. background_nodes_separator_idx++;
  594. callable_mp((Node *)this, &Node::move_child).call_deferred(graph_frame, 0);
  595. callable_mp((Node *)this, &Node::move_child).call_deferred(connections_layer, background_nodes_separator_idx);
  596. _update_graph_frame(graph_frame);
  597. graph_frame->connect("autoshrink_changed", callable_mp(this, &GraphEdit::_graph_frame_autoshrink_changed).bind(graph_element));
  598. }
  599. graph_element->connect("raise_request", callable_mp(this, &GraphEdit::_ensure_node_order_from).bind(graph_element));
  600. graph_element->connect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resize_request).bind(graph_element));
  601. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  602. graph_element->connect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  603. }
  604. graph_element->connect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  605. graph_element->set_scale(Vector2(zoom, zoom));
  606. _graph_element_moved(graph_element);
  607. graph_element->set_mouse_filter(MOUSE_FILTER_PASS);
  608. }
  609. }
  610. void GraphEdit::remove_child_notify(Node *p_child) {
  611. Control::remove_child_notify(p_child);
  612. if (p_child == top_layer) {
  613. top_layer = nullptr;
  614. minimap = nullptr;
  615. } else if (p_child == connections_layer) {
  616. connections_layer = nullptr;
  617. if (is_inside_tree()) {
  618. WARN_PRINT("GraphEdit's connection_layer removed. This should not be done. If you like to remove all GraphElements from a GraphEdit node, do not simply remove all non-internal children but check their type since the connection layer has to be kept non-internal due to technical reasons.");
  619. }
  620. }
  621. if (top_layer != nullptr && is_inside_tree()) {
  622. // Keep the top layer always on top!
  623. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  624. }
  625. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  626. if (graph_element) {
  627. graph_element->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved));
  628. graph_element->disconnect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected));
  629. graph_element->disconnect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected));
  630. graph_element->disconnect(SceneStringName(visibility_changed), callable_mp(this, &GraphEdit::_graph_element_visibility_changed));
  631. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  632. if (graph_node) {
  633. graph_node->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  634. graph_node->disconnect("slot_sizes_changed", callable_mp(this, &GraphEdit::_graph_node_rect_changed));
  635. graph_node->disconnect(SceneStringName(item_rect_changed), callable_mp(this, &GraphEdit::_graph_node_rect_changed));
  636. // Invalidate all adjacent connections, so that they are removed before the next redraw.
  637. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  638. conn->_cache.dirty = true;
  639. }
  640. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  641. connections_layer->queue_redraw();
  642. }
  643. }
  644. GraphFrame *frame = Object::cast_to<GraphFrame>(graph_element);
  645. if (frame) {
  646. background_nodes_separator_idx--;
  647. graph_element->disconnect("autoshrink_changed", callable_mp(this, &GraphEdit::_graph_frame_autoshrink_changed));
  648. }
  649. if (linked_parent_map.has(graph_element->get_name())) {
  650. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[graph_element->get_name()])));
  651. if (parent_frame) {
  652. if (frame_attached_nodes.has(parent_frame->get_name())) {
  653. frame_attached_nodes.get(parent_frame->get_name()).erase(graph_element->get_name());
  654. }
  655. linked_parent_map.erase(graph_element->get_name());
  656. _update_graph_frame(parent_frame);
  657. }
  658. }
  659. if (frame_attached_nodes.has(graph_element->get_name())) {
  660. for (const StringName &attached_node_name : frame_attached_nodes.get(graph_element->get_name())) {
  661. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  662. if (attached_node) {
  663. linked_parent_map.erase(attached_node->get_name());
  664. }
  665. }
  666. frame_attached_nodes.erase(graph_element->get_name());
  667. }
  668. graph_element->disconnect("raise_request", callable_mp(this, &GraphEdit::_ensure_node_order_from));
  669. graph_element->disconnect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resize_request));
  670. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  671. graph_element->disconnect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  672. }
  673. // In case of the whole GraphEdit being destroyed these references can already be freed.
  674. if (minimap != nullptr && minimap->is_inside_tree()) {
  675. graph_element->disconnect(SceneStringName(item_rect_changed), callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  676. }
  677. }
  678. }
  679. void GraphEdit::_update_theme_item_cache() {
  680. Control::_update_theme_item_cache();
  681. theme_cache.base_scale = get_theme_default_base_scale();
  682. }
  683. void GraphEdit::_notification(int p_what) {
  684. switch (p_what) {
  685. case NOTIFICATION_THEME_CHANGED: {
  686. zoom_minus_button->set_button_icon(theme_cache.zoom_out);
  687. zoom_reset_button->set_button_icon(theme_cache.zoom_reset);
  688. zoom_plus_button->set_button_icon(theme_cache.zoom_in);
  689. toggle_snapping_button->set_button_icon(theme_cache.snapping_toggle);
  690. toggle_grid_button->set_button_icon(theme_cache.grid_toggle);
  691. minimap_button->set_button_icon(theme_cache.minimap_toggle);
  692. arrange_button->set_button_icon(theme_cache.layout);
  693. zoom_label->set_custom_minimum_size(Size2(48, 0) * theme_cache.base_scale);
  694. menu_panel->add_theme_style_override(SceneStringName(panel), theme_cache.menu_panel);
  695. } break;
  696. case NOTIFICATION_READY: {
  697. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  698. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  699. h_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  700. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  701. h_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  702. h_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  703. v_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  704. v_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  705. v_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  706. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  707. } break;
  708. case NOTIFICATION_DRAW: {
  709. // Draw background fill.
  710. draw_style_box(theme_cache.panel, Rect2(Point2(), get_size()));
  711. if (has_focus()) {
  712. draw_style_box(theme_cache.panel_focus, Rect2(Point2(), get_size()));
  713. }
  714. // Draw background grid.
  715. if (show_grid) {
  716. _draw_grid();
  717. }
  718. } break;
  719. case NOTIFICATION_RESIZED: {
  720. _update_scrollbars();
  721. minimap->queue_redraw();
  722. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  723. } break;
  724. case NOTIFICATION_ENTER_TREE: {
  725. update_warped_panning();
  726. } break;
  727. }
  728. }
  729. Rect2 GraphEdit::_compute_shrinked_frame_rect(const GraphFrame *p_frame) {
  730. Vector2 min_point{ FLT_MAX, FLT_MAX };
  731. Vector2 max_point{ -FLT_MAX, -FLT_MAX };
  732. if (!frame_attached_nodes.has(p_frame->get_name())) {
  733. return Rect2(p_frame->get_position_offset(), Size2());
  734. }
  735. int autoshrink_margin = p_frame->get_autoshrink_margin();
  736. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  737. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node_or_null(NodePath(attached_node_name)));
  738. if (!attached_node || attached_node == p_frame) {
  739. if (!attached_node) {
  740. frame_attached_nodes.get(p_frame->get_name()).erase(attached_node_name);
  741. }
  742. continue;
  743. }
  744. Vector2 node_pos = attached_node->get_position_offset();
  745. Vector2 size = attached_node->get_size();
  746. min_point = min_point.min(node_pos);
  747. max_point = max_point.max(node_pos + size);
  748. }
  749. // It's sufficient to check only one value here.
  750. if (min_point.x == FLT_MAX) {
  751. return Rect2(p_frame->get_position_offset(), Size2());
  752. }
  753. const Size2 titlebar_size = p_frame->get_titlebar_size();
  754. min_point -= Size2(autoshrink_margin, MAX(autoshrink_margin, titlebar_size.y));
  755. max_point += Size2(autoshrink_margin, autoshrink_margin);
  756. return Rect2(min_point, max_point - min_point);
  757. }
  758. void GraphEdit::_update_graph_frame(GraphFrame *p_frame) {
  759. Rect2 frame_rect = _compute_shrinked_frame_rect(p_frame);
  760. Vector2 min_point = frame_rect.position;
  761. Vector2 max_point = frame_rect.position + frame_rect.size;
  762. // Only update the size if there are attached nodes.
  763. if (frame_attached_nodes.has(p_frame->get_name()) && frame_attached_nodes.get(p_frame->get_name()).size() > 0) {
  764. if (!p_frame->is_autoshrink_enabled()) {
  765. Vector2 old_offset = p_frame->get_position_offset();
  766. min_point = min_point.min(old_offset);
  767. max_point = max_point.max(old_offset + p_frame->get_size());
  768. }
  769. Rect2 old_rect = p_frame->get_rect();
  770. p_frame->set_position_offset(min_point);
  771. p_frame->set_size(max_point - min_point);
  772. // Emit the signal only if the frame rect has changed.
  773. if (old_rect != p_frame->get_rect()) {
  774. emit_signal(SNAME("frame_rect_changed"), p_frame, p_frame->get_rect());
  775. }
  776. }
  777. // Update all parent frames recursively bottom-up.
  778. if (linked_parent_map.has(p_frame->get_name())) {
  779. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[p_frame->get_name()])));
  780. if (parent_frame) {
  781. _update_graph_frame(parent_frame);
  782. }
  783. }
  784. }
  785. void GraphEdit::_set_drag_frame_attached_nodes(GraphFrame *p_frame, bool p_drag) {
  786. if (!frame_attached_nodes.has(p_frame->get_name())) {
  787. return;
  788. }
  789. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  790. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node(NodePath(attached_node_name)));
  791. attached_node->set_drag(p_drag);
  792. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(attached_node);
  793. if (graph_frame) {
  794. _set_drag_frame_attached_nodes(graph_frame, p_drag);
  795. }
  796. }
  797. }
  798. void GraphEdit::_set_position_of_frame_attached_nodes(GraphFrame *p_frame, const Vector2 &p_pos) {
  799. if (!frame_attached_nodes.has(p_frame->get_name())) {
  800. return;
  801. }
  802. for (const StringName &attached_node_name : frame_attached_nodes.get(p_frame->get_name())) {
  803. GraphElement *attached_node = Object::cast_to<GraphElement>(get_node_or_null(NodePath(attached_node_name)));
  804. if (!attached_node) {
  805. continue;
  806. }
  807. Vector2 pos = (attached_node->get_drag_from() * zoom + drag_accum) / zoom;
  808. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  809. pos = pos.snappedf(snapping_distance);
  810. }
  811. // Recursively move graph frames.
  812. attached_node->set_position_offset(pos);
  813. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(attached_node);
  814. if (graph_frame) {
  815. _set_position_of_frame_attached_nodes(graph_frame, p_pos);
  816. }
  817. }
  818. }
  819. bool GraphEdit::_filter_input(const Point2 &p_point) {
  820. for (int i = get_child_count() - 1; i >= 0; i--) {
  821. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  822. if (!graph_node || !graph_node->is_visible_in_tree()) {
  823. continue;
  824. }
  825. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  826. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  827. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  828. // Determine slot height.
  829. int slot_index = graph_node->get_input_port_slot(j);
  830. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  831. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  832. if (is_in_input_hotzone(graph_node, j, p_point / zoom, port_size)) {
  833. return true;
  834. }
  835. }
  836. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  837. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  838. // Determine slot height.
  839. int slot_index = graph_node->get_output_port_slot(j);
  840. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  841. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  842. if (is_in_output_hotzone(graph_node, j, p_point / zoom, port_size)) {
  843. return true;
  844. }
  845. }
  846. // This prevents interactions with a port hotzone that is behind another node.
  847. Rect2 graph_node_rect = Rect2(graph_node->get_position(), graph_node->get_size() * zoom);
  848. if (graph_node_rect.has_point(p_point)) {
  849. break;
  850. }
  851. }
  852. return false;
  853. }
  854. void GraphEdit::start_keyboard_connecting(GraphNode *p_node, int p_in_port, int p_out_port) {
  855. if (!p_node || p_in_port == p_out_port || (p_in_port != -1 && p_out_port != -1)) {
  856. return;
  857. }
  858. connecting_valid = false;
  859. keyboard_connecting = true;
  860. if (p_in_port != -1) {
  861. Vector2 pos = p_node->get_input_port_position(p_in_port) * zoom + p_node->get_position();
  862. if (right_disconnects || valid_right_disconnect_types.has(p_node->get_input_port_type(p_in_port))) {
  863. // Check disconnect.
  864. for (const Ref<Connection> &conn : connection_map[p_node->get_name()]) {
  865. if (conn->to_node == p_node->get_name() && conn->to_port == p_in_port) {
  866. Node *fr = get_node(NodePath(conn->from_node));
  867. if (Object::cast_to<GraphNode>(fr)) {
  868. connecting_from_node = conn->from_node;
  869. connecting_from_port_index = conn->from_port;
  870. connecting_from_output = true;
  871. connecting_type = Object::cast_to<GraphNode>(fr)->get_output_port_type(conn->from_port);
  872. connecting_color = Object::cast_to<GraphNode>(fr)->get_output_port_color(conn->from_port);
  873. connecting_target_valid = false;
  874. connecting_to_point = pos;
  875. just_disconnected = true;
  876. if (connecting_type >= 0) {
  877. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  878. fr = get_node(NodePath(connecting_from_node));
  879. if (Object::cast_to<GraphNode>(fr)) {
  880. connecting = true;
  881. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  882. }
  883. }
  884. return;
  885. }
  886. }
  887. }
  888. }
  889. connecting_from_node = p_node->get_name();
  890. connecting_from_port_index = p_in_port;
  891. connecting_from_output = false;
  892. connecting_type = p_node->get_input_port_type(p_in_port);
  893. connecting_color = p_node->get_input_port_color(p_in_port);
  894. connecting_target_valid = false;
  895. connecting_to_point = pos;
  896. if (connecting_type >= 0) {
  897. connecting = true;
  898. just_disconnected = false;
  899. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  900. }
  901. return;
  902. }
  903. if (p_out_port != -1) {
  904. Vector2 pos = p_node->get_output_port_position(p_out_port) * zoom + p_node->get_position();
  905. if (valid_left_disconnect_types.has(p_node->get_output_port_type(p_out_port))) {
  906. // Check disconnect.
  907. for (const Ref<Connection> &conn : connection_map[p_node->get_name()]) {
  908. if (conn->from_node == p_node->get_name() && conn->from_port == p_out_port) {
  909. Node *to = get_node(NodePath(conn->to_node));
  910. if (Object::cast_to<GraphNode>(to)) {
  911. connecting_from_node = conn->to_node;
  912. connecting_from_port_index = conn->to_port;
  913. connecting_from_output = false;
  914. connecting_type = Object::cast_to<GraphNode>(to)->get_input_port_type(conn->to_port);
  915. connecting_color = Object::cast_to<GraphNode>(to)->get_input_port_color(conn->to_port);
  916. connecting_target_valid = false;
  917. connecting_to_point = pos;
  918. if (connecting_type >= 0) {
  919. just_disconnected = true;
  920. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  921. to = get_node(NodePath(connecting_from_node)); // Maybe it was erased.
  922. if (Object::cast_to<GraphNode>(to)) {
  923. connecting = true;
  924. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  925. }
  926. }
  927. return;
  928. }
  929. }
  930. }
  931. }
  932. connecting_from_node = p_node->get_name();
  933. connecting_from_port_index = p_out_port;
  934. connecting_from_output = true;
  935. connecting_type = p_node->get_output_port_type(p_out_port);
  936. connecting_color = p_node->get_output_port_color(p_out_port);
  937. connecting_target_valid = false;
  938. connecting_to_point = pos;
  939. if (connecting_type >= 0) {
  940. connecting = true;
  941. just_disconnected = false;
  942. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  943. }
  944. return;
  945. }
  946. }
  947. void GraphEdit::end_keyboard_connecting(GraphNode *p_node, int p_in_port, int p_out_port) {
  948. if (!p_node) {
  949. return;
  950. }
  951. connecting_valid = true;
  952. connecting_target_valid = false;
  953. if (p_in_port != -1) {
  954. Vector2 pos = p_node->get_input_port_position(p_in_port) * zoom + p_node->get_position();
  955. int type = p_node->get_input_port_type(p_in_port);
  956. if (type == connecting_type || p_node->is_ignoring_valid_connection_type() || valid_connection_types.has(ConnectionType(connecting_type, type))) {
  957. connecting_target_valid = true;
  958. connecting_to_point = pos;
  959. connecting_target_node = p_node->get_name();
  960. connecting_target_port_index = p_in_port;
  961. }
  962. }
  963. if (p_out_port != -1) {
  964. Vector2 pos = p_node->get_output_port_position(p_out_port) * zoom + p_node->get_position();
  965. int type = p_node->get_output_port_type(p_out_port);
  966. if (type == connecting_type || p_node->is_ignoring_valid_connection_type() || valid_connection_types.has(ConnectionType(type, connecting_type))) {
  967. connecting_target_valid = true;
  968. connecting_to_point = pos;
  969. connecting_target_node = p_node->get_name();
  970. connecting_target_port_index = p_out_port;
  971. }
  972. }
  973. if (connecting_valid) {
  974. if (connecting && connecting_target_valid) {
  975. if (connecting_from_output) {
  976. emit_signal(SNAME("connection_request"), connecting_from_node, connecting_from_port_index, connecting_target_node, connecting_target_port_index);
  977. } else {
  978. emit_signal(SNAME("connection_request"), connecting_target_node, connecting_target_port_index, connecting_from_node, connecting_from_port_index);
  979. }
  980. } else if (!just_disconnected) {
  981. if (connecting_from_output) {
  982. emit_signal(SNAME("connection_to_empty"), connecting_from_node, connecting_from_port_index, Vector2());
  983. } else {
  984. emit_signal(SNAME("connection_from_empty"), connecting_from_node, connecting_from_port_index, Vector2());
  985. }
  986. }
  987. }
  988. keyboard_connecting = false;
  989. if (connecting) {
  990. force_connection_drag_end();
  991. }
  992. }
  993. Dictionary GraphEdit::get_type_names() const {
  994. return type_names;
  995. }
  996. void GraphEdit::set_type_names(const Dictionary &p_names) {
  997. type_names = p_names;
  998. }
  999. void GraphEdit::_top_connection_layer_input(const Ref<InputEvent> &p_ev) {
  1000. Ref<InputEventMouseButton> mb = p_ev;
  1001. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  1002. if (keyboard_connecting) {
  1003. force_connection_drag_end();
  1004. keyboard_connecting = false;
  1005. }
  1006. connecting_valid = false;
  1007. click_pos = mb->get_position() / zoom;
  1008. for (int i = get_child_count() - 1; i >= 0; i--) {
  1009. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  1010. if (!graph_node || !graph_node->is_visible_in_tree()) {
  1011. continue;
  1012. }
  1013. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  1014. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  1015. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  1016. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  1017. // Determine slot height.
  1018. int slot_index = graph_node->get_output_port_slot(j);
  1019. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  1020. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  1021. if (is_in_output_hotzone(graph_node, j, click_pos, port_size)) {
  1022. if (valid_left_disconnect_types.has(graph_node->get_output_port_type(j))) {
  1023. // Check disconnect.
  1024. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  1025. if (conn->from_node == graph_node->get_name() && conn->from_port == j) {
  1026. Node *to = get_node(NodePath(conn->to_node));
  1027. if (Object::cast_to<GraphNode>(to)) {
  1028. connecting_from_node = conn->to_node;
  1029. connecting_from_port_index = conn->to_port;
  1030. connecting_from_output = false;
  1031. connecting_type = Object::cast_to<GraphNode>(to)->get_input_port_type(conn->to_port);
  1032. connecting_color = Object::cast_to<GraphNode>(to)->get_input_port_color(conn->to_port);
  1033. connecting_target_valid = false;
  1034. connecting_to_point = pos;
  1035. if (connecting_type >= 0) {
  1036. just_disconnected = true;
  1037. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  1038. to = get_node(NodePath(connecting_from_node)); // Maybe it was erased.
  1039. if (Object::cast_to<GraphNode>(to)) {
  1040. connecting = true;
  1041. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  1042. }
  1043. }
  1044. return;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. connecting_from_node = graph_node->get_name();
  1050. connecting_from_port_index = j;
  1051. connecting_from_output = true;
  1052. connecting_type = graph_node->get_output_port_type(j);
  1053. connecting_color = graph_node->get_output_port_color(j);
  1054. connecting_target_valid = false;
  1055. connecting_to_point = pos;
  1056. if (connecting_type >= 0) {
  1057. connecting = true;
  1058. just_disconnected = false;
  1059. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  1060. }
  1061. return;
  1062. }
  1063. }
  1064. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  1065. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  1066. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  1067. // Determine slot height.
  1068. int slot_index = graph_node->get_input_port_slot(j);
  1069. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  1070. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  1071. if (is_in_input_hotzone(graph_node, j, click_pos, port_size)) {
  1072. if (right_disconnects || valid_right_disconnect_types.has(graph_node->get_input_port_type(j))) {
  1073. // Check disconnect.
  1074. for (const Ref<Connection> &conn : connection_map[graph_node->get_name()]) {
  1075. if (conn->to_node == graph_node->get_name() && conn->to_port == j) {
  1076. Node *fr = get_node(NodePath(conn->from_node));
  1077. if (Object::cast_to<GraphNode>(fr)) {
  1078. connecting_from_node = conn->from_node;
  1079. connecting_from_port_index = conn->from_port;
  1080. connecting_from_output = true;
  1081. connecting_type = Object::cast_to<GraphNode>(fr)->get_output_port_type(conn->from_port);
  1082. connecting_color = Object::cast_to<GraphNode>(fr)->get_output_port_color(conn->from_port);
  1083. connecting_target_valid = false;
  1084. connecting_to_point = pos;
  1085. just_disconnected = true;
  1086. if (connecting_type >= 0) {
  1087. emit_signal(SNAME("disconnection_request"), conn->from_node, conn->from_port, conn->to_node, conn->to_port);
  1088. fr = get_node(NodePath(connecting_from_node));
  1089. if (Object::cast_to<GraphNode>(fr)) {
  1090. connecting = true;
  1091. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, true);
  1092. }
  1093. }
  1094. return;
  1095. }
  1096. }
  1097. }
  1098. }
  1099. connecting_from_node = graph_node->get_name();
  1100. connecting_from_port_index = j;
  1101. connecting_from_output = false;
  1102. connecting_type = graph_node->get_input_port_type(j);
  1103. connecting_color = graph_node->get_input_port_color(j);
  1104. connecting_target_valid = false;
  1105. connecting_to_point = pos;
  1106. if (connecting_type >= 0) {
  1107. connecting = true;
  1108. just_disconnected = false;
  1109. emit_signal(SNAME("connection_drag_started"), connecting_from_node, connecting_from_port_index, false);
  1110. }
  1111. return;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. Ref<InputEventMouseMotion> mm = p_ev;
  1117. if (mm.is_valid() && connecting && !keyboard_connecting) {
  1118. connecting_to_point = mm->get_position();
  1119. minimap->queue_redraw();
  1120. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1121. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to_point / zoom) > MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION;
  1122. if (connecting_valid) {
  1123. Vector2 mpos = mm->get_position() / zoom;
  1124. for (int i = get_child_count() - 1; i >= 0; i--) {
  1125. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  1126. if (!graph_node || !graph_node->is_visible_in_tree()) {
  1127. continue;
  1128. }
  1129. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  1130. if (!connecting_from_output) {
  1131. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  1132. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  1133. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  1134. // Determine slot height.
  1135. int slot_index = graph_node->get_output_port_slot(j);
  1136. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  1137. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  1138. int type = graph_node->get_output_port_type(j);
  1139. if ((type == connecting_type || graph_node->is_ignoring_valid_connection_type() ||
  1140. valid_connection_types.has(ConnectionType(type, connecting_type))) &&
  1141. is_in_output_hotzone(graph_node, j, mpos, port_size)) {
  1142. if (!is_node_hover_valid(graph_node->get_name(), j, connecting_from_node, connecting_from_port_index)) {
  1143. continue;
  1144. }
  1145. connecting_target_valid = true;
  1146. connecting_to_point = pos;
  1147. connecting_target_node = graph_node->get_name();
  1148. connecting_target_port_index = j;
  1149. return;
  1150. }
  1151. }
  1152. connecting_target_valid = false;
  1153. } else {
  1154. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  1155. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  1156. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  1157. // Determine slot height.
  1158. int slot_index = graph_node->get_input_port_slot(j);
  1159. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index, false));
  1160. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  1161. int type = graph_node->get_input_port_type(j);
  1162. if ((type == connecting_type || graph_node->is_ignoring_valid_connection_type() || valid_connection_types.has(ConnectionType(connecting_type, type))) &&
  1163. is_in_input_hotzone(graph_node, j, mpos, port_size)) {
  1164. if (!is_node_hover_valid(connecting_from_node, connecting_from_port_index, graph_node->get_name(), j)) {
  1165. continue;
  1166. }
  1167. connecting_target_valid = true;
  1168. connecting_to_point = pos;
  1169. connecting_target_node = graph_node->get_name();
  1170. connecting_target_port_index = j;
  1171. return;
  1172. }
  1173. }
  1174. connecting_target_valid = false;
  1175. }
  1176. // This prevents interactions with a port hotzone that is behind another node.
  1177. Rect2 graph_node_rect = Rect2(graph_node->get_position(), graph_node->get_size() * zoom);
  1178. if (graph_node_rect.has_point(mm->get_position())) {
  1179. break;
  1180. }
  1181. }
  1182. }
  1183. }
  1184. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  1185. if (connecting_valid) {
  1186. if (connecting && connecting_target_valid) {
  1187. if (connecting_from_output) {
  1188. emit_signal(SNAME("connection_request"), connecting_from_node, connecting_from_port_index, connecting_target_node, connecting_target_port_index);
  1189. } else {
  1190. emit_signal(SNAME("connection_request"), connecting_target_node, connecting_target_port_index, connecting_from_node, connecting_from_port_index);
  1191. }
  1192. } else if (!just_disconnected) {
  1193. if (connecting_from_output) {
  1194. emit_signal(SNAME("connection_to_empty"), connecting_from_node, connecting_from_port_index, mb->get_position());
  1195. } else {
  1196. emit_signal(SNAME("connection_from_empty"), connecting_from_node, connecting_from_port_index, mb->get_position());
  1197. }
  1198. }
  1199. } else {
  1200. set_selected(get_node_or_null(NodePath(connecting_from_node)));
  1201. }
  1202. if (connecting) {
  1203. force_connection_drag_end();
  1204. }
  1205. }
  1206. }
  1207. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) {
  1208. if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) {
  1209. return false;
  1210. }
  1211. Rect2 control_rect = p_control->get_rect();
  1212. control_rect.position *= zoom;
  1213. control_rect.size *= zoom;
  1214. control_rect.position += p_offset;
  1215. if (!control_rect.has_point(mpos) || p_control->get_mouse_filter_with_override() == MOUSE_FILTER_IGNORE) {
  1216. // Test children.
  1217. for (int i = 0; i < p_control->get_child_count(); i++) {
  1218. Control *child_rect = Object::cast_to<Control>(p_control->get_child(i));
  1219. if (!child_rect) {
  1220. continue;
  1221. }
  1222. if (_check_clickable_control(child_rect, mpos, control_rect.position)) {
  1223. return true;
  1224. }
  1225. }
  1226. return false;
  1227. } else {
  1228. return true;
  1229. }
  1230. }
  1231. bool GraphEdit::is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  1232. bool success;
  1233. if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  1234. return success;
  1235. } else {
  1236. Vector2 pos = p_graph_node->get_input_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  1237. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true);
  1238. }
  1239. }
  1240. bool GraphEdit::is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  1241. if (p_graph_node->is_resizable()) {
  1242. Ref<Texture2D> resizer = p_graph_node->theme_cache.resizer;
  1243. Rect2 resizer_rect = Rect2(p_graph_node->get_position() / zoom + p_graph_node->get_size() - resizer->get_size(), resizer->get_size());
  1244. if (resizer_rect.has_point(p_mouse_pos)) {
  1245. return false;
  1246. }
  1247. }
  1248. bool success;
  1249. if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  1250. return success;
  1251. } else {
  1252. Vector2 pos = p_graph_node->get_output_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  1253. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false);
  1254. }
  1255. }
  1256. bool GraphEdit::is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  1257. Rect2 hotzone = Rect2(
  1258. p_pos.x - (p_left ? theme_cache.port_hotzone_outer_extent : theme_cache.port_hotzone_inner_extent),
  1259. p_pos.y - p_port_size.height / 2.0,
  1260. theme_cache.port_hotzone_inner_extent + theme_cache.port_hotzone_outer_extent,
  1261. p_port_size.height);
  1262. if (!hotzone.has_point(p_mouse_pos)) {
  1263. return false;
  1264. }
  1265. for (int i = 0; i < get_child_count(); i++) {
  1266. GraphNode *child = Object::cast_to<GraphNode>(get_child(i));
  1267. if (!child) {
  1268. continue;
  1269. }
  1270. Rect2 child_rect = child->get_rect();
  1271. if (child_rect.has_point(p_mouse_pos * zoom)) {
  1272. for (int j = 0; j < child->get_child_count(); j++) {
  1273. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  1274. if (!subchild) {
  1275. continue;
  1276. }
  1277. if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) {
  1278. return false;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. return true;
  1284. }
  1285. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) const {
  1286. Vector<Vector2> ret;
  1287. if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) {
  1288. return ret;
  1289. }
  1290. float x_diff = (p_to.x - p_from.x);
  1291. float cp_offset = x_diff * lines_curvature;
  1292. if (x_diff < 0) {
  1293. cp_offset *= -1;
  1294. }
  1295. Curve2D curve;
  1296. curve.add_point(p_from);
  1297. curve.set_point_out(0, Vector2(cp_offset, 0));
  1298. curve.add_point(p_to);
  1299. curve.set_point_in(1, Vector2(-cp_offset, 0));
  1300. if (lines_curvature > 0) {
  1301. return curve.tessellate(MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES, 2.0);
  1302. } else {
  1303. return curve.tessellate(1);
  1304. }
  1305. }
  1306. Ref<GraphEdit::Connection> GraphEdit::get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance) const {
  1307. Vector2 transformed_point = p_point + get_scroll_offset();
  1308. Ref<GraphEdit::Connection> closest_connection;
  1309. float closest_distance = p_max_distance;
  1310. for (const Ref<Connection> &conn : connections) {
  1311. if (conn->_cache.aabb.distance_to(transformed_point) > p_max_distance) {
  1312. continue;
  1313. }
  1314. Vector<Vector2> points = get_connection_line(conn->_cache.from_pos * zoom, conn->_cache.to_pos * zoom);
  1315. for (int i = 0; i < points.size() - 1; i++) {
  1316. const real_t distance = Geometry2D::get_distance_to_segment(transformed_point, points[i], points[i + 1]);
  1317. if (distance <= lines_thickness * 0.5 + p_max_distance && distance < closest_distance) {
  1318. closest_connection = conn;
  1319. closest_distance = distance;
  1320. }
  1321. }
  1322. }
  1323. return closest_connection;
  1324. }
  1325. List<Ref<GraphEdit::Connection>> GraphEdit::get_connections_intersecting_with_rect(const Rect2 &p_rect) const {
  1326. Rect2 transformed_rect = p_rect;
  1327. transformed_rect.position += get_scroll_offset();
  1328. List<Ref<Connection>> intersecting_connections;
  1329. for (const Ref<Connection> &conn : connections) {
  1330. if (!conn->_cache.aabb.intersects(transformed_rect)) {
  1331. continue;
  1332. }
  1333. Vector<Vector2> points = get_connection_line(conn->_cache.from_pos * zoom, conn->_cache.to_pos * zoom);
  1334. for (int i = 0; i < points.size() - 1; i++) {
  1335. if (Geometry2D::segment_intersects_rect(points[i], points[i + 1], transformed_rect)) {
  1336. intersecting_connections.push_back(conn);
  1337. break;
  1338. }
  1339. }
  1340. }
  1341. return intersecting_connections;
  1342. }
  1343. void GraphEdit::_draw_minimap_connection_line(const Vector2 &p_from_graph_position, const Vector2 &p_to_graph_position, const Color &p_from_color, const Color &p_to_color) {
  1344. Vector<Vector2> points = get_connection_line(p_from_graph_position, p_to_graph_position);
  1345. ERR_FAIL_COND_MSG(points.size() < 2, "\"_get_connection_line()\" returned an invalid line.");
  1346. // Convert to minimap points.
  1347. for (Vector2 &point : points) {
  1348. point = minimap->_convert_from_graph_position(point) + minimap->minimap_offset;
  1349. }
  1350. // Setup polyline colors.
  1351. LocalVector<Color> colors;
  1352. colors.reserve(points.size());
  1353. const Vector2 &from = points[0];
  1354. const Vector2 &to = points[points.size() - 1];
  1355. float length_inv = 1.0 / (from).distance_to(to);
  1356. for (const Vector2 &point : points) {
  1357. float normalized_curve_position = from.distance_to(point) * length_inv;
  1358. colors.push_back(p_from_color.lerp(p_to_color, normalized_curve_position));
  1359. }
  1360. minimap->draw_polyline_colors(points, colors, 0.5, lines_antialiased);
  1361. }
  1362. void GraphEdit::_update_connections() {
  1363. // Collect all dead connections and remove them.
  1364. LocalVector<Ref<Connection>> dead_connections;
  1365. for (const Ref<Connection> &conn : connections) {
  1366. if (conn->_cache.dirty) {
  1367. Node *from = get_node_or_null(NodePath(conn->from_node));
  1368. GraphNode *gnode_from = Object::cast_to<GraphNode>(from);
  1369. if (!gnode_from && !conn->keep_alive) {
  1370. dead_connections.push_back(conn);
  1371. continue;
  1372. }
  1373. Node *to = get_node_or_null(NodePath(conn->to_node));
  1374. GraphNode *gnode_to = Object::cast_to<GraphNode>(to);
  1375. if (!gnode_to && !conn->keep_alive) {
  1376. dead_connections.push_back(conn);
  1377. continue;
  1378. }
  1379. if (conn->keep_alive && (!gnode_from || !gnode_to)) {
  1380. continue;
  1381. }
  1382. const Vector2 from_pos = gnode_from->get_output_port_position(conn->from_port) + gnode_from->get_position_offset();
  1383. const Vector2 to_pos = gnode_to->get_input_port_position(conn->to_port) + gnode_to->get_position_offset();
  1384. const Color from_color = gnode_from->get_output_port_color(conn->from_port);
  1385. const Color to_color = gnode_to->get_input_port_color(conn->to_port);
  1386. const int from_type = gnode_from->get_output_port_type(conn->from_port);
  1387. const int to_type = gnode_to->get_input_port_type(conn->to_port);
  1388. conn->_cache.from_pos = from_pos;
  1389. conn->_cache.to_pos = to_pos;
  1390. conn->_cache.from_color = from_color;
  1391. conn->_cache.to_color = to_color;
  1392. PackedVector2Array line_points = get_connection_line(from_pos * zoom, to_pos * zoom);
  1393. conn->_cache.line->set_points(line_points);
  1394. Ref<ShaderMaterial> line_material = conn->_cache.line->get_material();
  1395. if (line_material.is_null()) {
  1396. line_material.instantiate();
  1397. conn->_cache.line->set_material(line_material);
  1398. }
  1399. float line_width = _get_shader_line_width();
  1400. line_material->set_shader_parameter("line_width", line_width);
  1401. line_material->set_shader_parameter("from_type", from_type);
  1402. line_material->set_shader_parameter("to_type", to_type);
  1403. line_material->set_shader_parameter("rim_color", theme_cache.connection_rim_color);
  1404. // Compute bounding box of the line, including the line width.
  1405. conn->_cache.aabb = Rect2(line_points[0], Vector2());
  1406. for (int i = 0; i < line_points.size(); i++) {
  1407. conn->_cache.aabb.expand_to(line_points[i]);
  1408. }
  1409. conn->_cache.aabb.grow_by(lines_thickness * 0.5);
  1410. conn->_cache.dirty = false;
  1411. }
  1412. // Skip updating/drawing connections that are not visible.
  1413. Rect2 viewport_rect = get_viewport_rect();
  1414. viewport_rect.position += get_scroll_offset();
  1415. if (!conn->_cache.aabb.intersects(viewport_rect)) {
  1416. continue;
  1417. }
  1418. Color from_color = conn->_cache.from_color;
  1419. Color to_color = conn->_cache.to_color;
  1420. if (conn->activity > 0) {
  1421. from_color = from_color.lerp(theme_cache.activity_color, conn->activity);
  1422. to_color = to_color.lerp(theme_cache.activity_color, conn->activity);
  1423. }
  1424. if (conn == hovered_connection) {
  1425. from_color = from_color.blend(theme_cache.connection_hover_tint_color);
  1426. to_color = to_color.blend(theme_cache.connection_hover_tint_color);
  1427. }
  1428. // Update Line2D node.
  1429. Ref<Gradient> line_gradient = memnew(Gradient);
  1430. float line_width = _get_shader_line_width();
  1431. if (conn == hovered_connection) {
  1432. line_width *= 1.0f + (theme_cache.connection_hover_thickness / 100.0f);
  1433. }
  1434. conn->_cache.line->set_width(line_width);
  1435. line_gradient->set_color(0, from_color);
  1436. line_gradient->set_color(1, to_color);
  1437. conn->_cache.line->set_gradient(line_gradient);
  1438. }
  1439. for (const Ref<Connection> &dead_conn : dead_connections) {
  1440. List<Ref<Connection>> &connections_from = connection_map[dead_conn->from_node];
  1441. List<Ref<Connection>> &connections_to = connection_map[dead_conn->to_node];
  1442. connections_from.erase(dead_conn);
  1443. connections_to.erase(dead_conn);
  1444. dead_conn->_cache.line->queue_free();
  1445. connections.erase(dead_conn);
  1446. }
  1447. }
  1448. void GraphEdit::_top_layer_draw() {
  1449. if (!box_selecting) {
  1450. return;
  1451. }
  1452. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_fill);
  1453. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_stroke, false);
  1454. }
  1455. void GraphEdit::_update_top_connection_layer() {
  1456. _update_scrollbars();
  1457. if (!connecting) {
  1458. dragged_connection_line->clear_points();
  1459. return;
  1460. }
  1461. GraphNode *graph_node_from = Object::cast_to<GraphNode>(get_node_or_null(NodePath(connecting_from_node)));
  1462. ERR_FAIL_NULL(graph_node_from);
  1463. Vector2 from_pos = graph_node_from->get_position() / zoom;
  1464. Vector2 to_pos = connecting_to_point / zoom;
  1465. int from_type;
  1466. int to_type = connecting_type;
  1467. Color from_color;
  1468. Color to_color = connecting_color;
  1469. if (connecting_from_output) {
  1470. from_pos += graph_node_from->get_output_port_position(connecting_from_port_index);
  1471. from_type = graph_node_from->get_output_port_type(connecting_from_port_index);
  1472. from_color = graph_node_from->get_output_port_color(connecting_from_port_index);
  1473. } else {
  1474. from_pos += graph_node_from->get_input_port_position(connecting_from_port_index);
  1475. from_type = graph_node_from->get_input_port_type(connecting_from_port_index);
  1476. from_color = graph_node_from->get_input_port_color(connecting_from_port_index);
  1477. }
  1478. if (connecting_target_valid) {
  1479. GraphNode *graph_node_to = Object::cast_to<GraphNode>(get_node_or_null(NodePath(connecting_target_node)));
  1480. ERR_FAIL_NULL(graph_node_to);
  1481. if (connecting_from_output) {
  1482. to_type = graph_node_to->get_input_port_type(connecting_target_port_index);
  1483. to_color = graph_node_to->get_input_port_color(connecting_target_port_index);
  1484. } else {
  1485. to_type = graph_node_to->get_output_port_type(connecting_target_port_index);
  1486. to_color = graph_node_to->get_output_port_color(connecting_target_port_index);
  1487. }
  1488. // Highlight the line to the mouse cursor when it's over a valid target port.
  1489. from_color = from_color.blend(theme_cache.connection_valid_target_tint_color);
  1490. to_color = to_color.blend(theme_cache.connection_valid_target_tint_color);
  1491. }
  1492. if (!connecting_from_output) {
  1493. SWAP(from_pos, to_pos);
  1494. SWAP(from_type, to_type);
  1495. SWAP(from_color, to_color);
  1496. }
  1497. PackedVector2Array line_points = get_connection_line(from_pos * zoom, to_pos * zoom);
  1498. dragged_connection_line->set_points(line_points);
  1499. Ref<ShaderMaterial> line_material = dragged_connection_line->get_material();
  1500. if (line_material.is_null()) {
  1501. line_material.instantiate();
  1502. line_material->set_shader(connections_shader);
  1503. dragged_connection_line->set_material(line_material);
  1504. }
  1505. float line_width = _get_shader_line_width();
  1506. line_material->set_shader_parameter("line_width", line_width);
  1507. line_material->set_shader_parameter("from_type", from_type);
  1508. line_material->set_shader_parameter("to_type", to_type);
  1509. line_material->set_shader_parameter("rim_color", theme_cache.connection_rim_color);
  1510. Ref<Gradient> line_gradient = memnew(Gradient);
  1511. dragged_connection_line->set_width(line_width);
  1512. line_gradient->set_color(0, from_color);
  1513. line_gradient->set_color(1, to_color);
  1514. dragged_connection_line->set_gradient(line_gradient);
  1515. }
  1516. void GraphEdit::_minimap_draw() {
  1517. if (!is_minimap_enabled()) {
  1518. return;
  1519. }
  1520. minimap->update_minimap();
  1521. // Draw the minimap background.
  1522. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  1523. minimap->draw_style_box(minimap->theme_cache.panel, minimap_rect);
  1524. Vector2 graph_offset = minimap->_get_graph_offset();
  1525. Vector2 minimap_offset = minimap->minimap_offset;
  1526. // Draw frame nodes.
  1527. for (int i = get_child_count() - 1; i >= 0; i--) {
  1528. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(get_child(i));
  1529. if (!graph_frame || !graph_frame->is_visible()) {
  1530. continue;
  1531. }
  1532. Vector2 node_position = minimap->_convert_from_graph_position(graph_frame->get_position_offset() * zoom - graph_offset) + minimap_offset;
  1533. Vector2 node_size = minimap->_convert_from_graph_position(graph_frame->get_size() * zoom);
  1534. Rect2 node_rect = Rect2(node_position, node_size);
  1535. Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate();
  1536. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  1537. Ref<StyleBoxFlat> sb_frame = graph_frame->get_theme_stylebox(graph_frame->is_selected() ? SNAME("panel_selected") : SceneStringName(panel));
  1538. if (sb_frame.is_valid()) {
  1539. Color node_color = sb_frame->get_bg_color();
  1540. if (graph_frame->is_tint_color_enabled()) {
  1541. node_color = graph_frame->get_tint_color();
  1542. }
  1543. sb_minimap->set_bg_color(node_color);
  1544. }
  1545. minimap->draw_style_box(sb_minimap, node_rect);
  1546. }
  1547. // Draw regular graph nodes.
  1548. for (int i = get_child_count() - 1; i >= 0; i--) {
  1549. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  1550. if (!graph_node || !graph_node->is_visible()) {
  1551. continue;
  1552. }
  1553. Vector2 node_position = minimap->_convert_from_graph_position(graph_node->get_position_offset() * zoom - graph_offset) + minimap_offset;
  1554. Vector2 node_size = minimap->_convert_from_graph_position(graph_node->get_size() * zoom);
  1555. Rect2 node_rect = Rect2(node_position, node_size);
  1556. Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate();
  1557. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  1558. Ref<StyleBoxFlat> sb_frame = graph_node->is_selected() ? graph_node->theme_cache.panel_selected : graph_node->theme_cache.panel;
  1559. if (sb_frame.is_valid()) {
  1560. Color node_color = sb_frame->get_bg_color();
  1561. sb_minimap->set_bg_color(node_color);
  1562. }
  1563. minimap->draw_style_box(sb_minimap, node_rect);
  1564. }
  1565. // Draw node connections.
  1566. for (const Ref<Connection> &conn : connections) {
  1567. Vector2 from_graph_position = conn->_cache.from_pos * zoom - graph_offset;
  1568. Vector2 to_graph_position = conn->_cache.to_pos * zoom - graph_offset;
  1569. Color from_color = conn->_cache.from_color;
  1570. Color to_color = conn->_cache.to_color;
  1571. if (conn->activity > 0) {
  1572. from_color = from_color.lerp(theme_cache.activity_color, conn->activity);
  1573. to_color = to_color.lerp(theme_cache.activity_color, conn->activity);
  1574. }
  1575. _draw_minimap_connection_line(from_graph_position, to_graph_position, from_color, to_color);
  1576. }
  1577. // Draw the "camera" viewport.
  1578. Rect2 camera_rect = minimap->get_camera_rect();
  1579. minimap->draw_style_box(minimap->theme_cache.camera_style, camera_rect);
  1580. // Draw the resizer control.
  1581. Ref<Texture2D> resizer = minimap->theme_cache.resizer;
  1582. Color resizer_color = minimap->theme_cache.resizer_color;
  1583. minimap->draw_texture(resizer, Point2(), resizer_color);
  1584. }
  1585. void GraphEdit::_draw_grid() {
  1586. Vector2 offset = get_scroll_offset() / zoom;
  1587. Size2 size = get_size() / zoom;
  1588. Point2i from_pos = (offset / float(snapping_distance)).floor();
  1589. Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1);
  1590. switch (grid_pattern) {
  1591. case GRID_PATTERN_LINES: {
  1592. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  1593. Color color;
  1594. if (Math::abs(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  1595. color = theme_cache.grid_major;
  1596. } else {
  1597. color = theme_cache.grid_minor;
  1598. }
  1599. float base_offset = i * snapping_distance * zoom - offset.x * zoom;
  1600. draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color);
  1601. }
  1602. for (int i = from_pos.y; i < from_pos.y + len.y; i++) {
  1603. Color color;
  1604. if (Math::abs(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  1605. color = theme_cache.grid_major;
  1606. } else {
  1607. color = theme_cache.grid_minor;
  1608. }
  1609. float base_offset = i * snapping_distance * zoom - offset.y * zoom;
  1610. draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color);
  1611. }
  1612. } break;
  1613. case GRID_PATTERN_DOTS: {
  1614. Color transparent_grid_minor = theme_cache.grid_minor;
  1615. transparent_grid_minor.a *= CLAMP(1.0 * (zoom - 0.4), 0, 1);
  1616. // Minor dots.
  1617. if (transparent_grid_minor.a != 0) {
  1618. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  1619. for (int j = from_pos.y; j < from_pos.y + len.y; j++) {
  1620. if (Math::abs(i) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0 && Math::abs(j) % GRID_MINOR_STEPS_PER_MAJOR_DOT == 0) {
  1621. continue;
  1622. }
  1623. float base_offset_x = i * snapping_distance * zoom - offset.x * zoom;
  1624. float base_offset_y = j * snapping_distance * zoom - offset.y * zoom;
  1625. draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), transparent_grid_minor);
  1626. }
  1627. }
  1628. }
  1629. // Major dots.
  1630. if (theme_cache.grid_major.a != 0) {
  1631. for (int i = from_pos.x - from_pos.x % GRID_MINOR_STEPS_PER_MAJOR_DOT; i < from_pos.x + len.x; i += GRID_MINOR_STEPS_PER_MAJOR_DOT) {
  1632. for (int j = from_pos.y - from_pos.y % GRID_MINOR_STEPS_PER_MAJOR_DOT; j < from_pos.y + len.y; j += GRID_MINOR_STEPS_PER_MAJOR_DOT) {
  1633. float base_offset_x = i * snapping_distance * zoom - offset.x * zoom;
  1634. float base_offset_y = j * snapping_distance * zoom - offset.y * zoom;
  1635. draw_rect(Rect2(base_offset_x - 1, base_offset_y - 1, 3, 3), theme_cache.grid_major);
  1636. }
  1637. }
  1638. }
  1639. } break;
  1640. }
  1641. }
  1642. void GraphEdit::set_selected(Node *p_child) {
  1643. for (int i = get_child_count() - 1; i >= 0; i--) {
  1644. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1645. if (!graph_element) {
  1646. continue;
  1647. }
  1648. graph_element->set_selected(graph_element == p_child);
  1649. }
  1650. }
  1651. void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
  1652. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  1653. ERR_FAIL_COND(p_ev.is_null());
  1654. if (panner->gui_input(p_ev, get_global_rect())) {
  1655. return;
  1656. }
  1657. // Highlight the connection close to the mouse cursor.
  1658. Ref<InputEventMouseMotion> mm = p_ev;
  1659. if (mm.is_valid()) {
  1660. Ref<Connection> new_highlighted_connection = get_closest_connection_at_point(mm->get_position());
  1661. if (new_highlighted_connection != hovered_connection) {
  1662. connections_layer->queue_redraw();
  1663. }
  1664. hovered_connection = new_highlighted_connection;
  1665. }
  1666. // Logic for moving graph controls via mouse drag.
  1667. if (mm.is_valid() && dragging) {
  1668. if (!moving_selection) {
  1669. emit_signal(SNAME("begin_node_move"));
  1670. moving_selection = true;
  1671. }
  1672. just_selected = true;
  1673. drag_accum += mm->get_relative();
  1674. for (int i = get_child_count() - 1; i >= 0; i--) {
  1675. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1676. if (graph_element && graph_element->is_selected() && graph_element->is_draggable()) {
  1677. Vector2 pos = (graph_element->get_drag_from() * zoom + drag_accum) / zoom;
  1678. // Snapping can be toggled temporarily by holding down Ctrl.
  1679. // This is done here as to not toggle the grid when holding down Ctrl.
  1680. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1681. pos = pos.snappedf(snapping_distance);
  1682. }
  1683. graph_element->set_position_offset(pos);
  1684. if (linked_parent_map.has(graph_element->get_name())) {
  1685. GraphFrame *parent_frame = Object::cast_to<GraphFrame>(get_node_or_null(NodePath(linked_parent_map[graph_element->get_name()])));
  1686. if (parent_frame) {
  1687. _update_graph_frame(parent_frame);
  1688. }
  1689. }
  1690. // Update all frame transforms recursively.
  1691. GraphFrame *graph_frame = Object::cast_to<GraphFrame>(get_child(i));
  1692. if (graph_frame) {
  1693. _set_position_of_frame_attached_nodes(graph_frame, drag_accum);
  1694. }
  1695. }
  1696. }
  1697. }
  1698. // Box selection logic.
  1699. if (mm.is_valid() && box_selecting) {
  1700. box_selecting_to = mm->get_position();
  1701. box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());
  1702. for (int i = get_child_count() - 1; i >= 0; i--) {
  1703. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1704. if (!graph_element) {
  1705. continue;
  1706. }
  1707. if (!graph_element->is_visible()) {
  1708. continue;
  1709. }
  1710. // Only select frames when the box selection is fully enclosing them.
  1711. bool is_frame = Object::cast_to<GraphFrame>(graph_element);
  1712. Rect2 r = graph_element->get_rect();
  1713. bool should_be_selected = is_frame ? box_selecting_rect.encloses(r) : box_selecting_rect.intersects(r);
  1714. if (should_be_selected) {
  1715. graph_element->set_selected(box_selection_mode_additive);
  1716. } else {
  1717. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  1718. }
  1719. }
  1720. top_layer->queue_redraw();
  1721. minimap->queue_redraw();
  1722. }
  1723. Ref<InputEventMouseButton> mb = p_ev;
  1724. if (mb.is_valid()) {
  1725. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  1726. if (box_selecting) {
  1727. box_selecting = false;
  1728. for (int i = get_child_count() - 1; i >= 0; i--) {
  1729. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1730. if (!graph_element) {
  1731. continue;
  1732. }
  1733. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  1734. }
  1735. top_layer->queue_redraw();
  1736. minimap->queue_redraw();
  1737. } else {
  1738. if (connecting) {
  1739. force_connection_drag_end();
  1740. } else {
  1741. emit_signal(SNAME("popup_request"), mb->get_position());
  1742. }
  1743. }
  1744. }
  1745. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && dragging) {
  1746. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1747. // Deselect current node.
  1748. for (int i = get_child_count() - 1; i >= 0; i--) {
  1749. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1750. if (graph_element) {
  1751. Rect2 r = graph_element->get_rect();
  1752. if (r.has_point(mb->get_position())) {
  1753. graph_element->set_selected(false);
  1754. }
  1755. }
  1756. }
  1757. }
  1758. if (drag_accum != Vector2()) {
  1759. for (int i = get_child_count() - 1; i >= 0; i--) {
  1760. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1761. if (graph_element && graph_element->is_selected()) {
  1762. graph_element->set_drag(false);
  1763. GraphFrame *frame = Object::cast_to<GraphFrame>(get_child(i));
  1764. if (frame) {
  1765. _set_drag_frame_attached_nodes(frame, false);
  1766. }
  1767. }
  1768. }
  1769. }
  1770. if (moving_selection) {
  1771. emit_signal(SNAME("end_node_move"));
  1772. moving_selection = false;
  1773. Vector<GraphElement *> dragged_nodes;
  1774. for (int i = get_child_count() - 1; i >= 0; i--) {
  1775. GraphElement *moved_node = Object::cast_to<GraphElement>(get_child(i));
  1776. if (moved_node && moved_node->is_selected() && moved_node->is_draggable()) {
  1777. dragged_nodes.push_back(moved_node);
  1778. }
  1779. }
  1780. GraphFrame *frame_dropped_on = nullptr;
  1781. // Find frame on which the node(s) is/were dropped.
  1782. // Count down to find the topmost frame.
  1783. for (int i = get_child_count() - 1; i >= 0; i--) {
  1784. GraphFrame *frame = Object::cast_to<GraphFrame>(get_child(i));
  1785. if (!frame || frame->is_resizing()) {
  1786. continue;
  1787. }
  1788. Rect2 frame_rect = frame->get_rect();
  1789. if (frame_rect.has_point(mb->get_position()) && !dragged_nodes.has(frame)) {
  1790. frame_dropped_on = frame;
  1791. break;
  1792. }
  1793. }
  1794. if (frame_dropped_on) {
  1795. dragged_nodes.erase(frame_dropped_on);
  1796. TypedArray<StringName> dragged_node_names;
  1797. for (GraphElement *moved_node : dragged_nodes) {
  1798. if (!linked_parent_map.has(moved_node->get_name())) {
  1799. dragged_node_names.push_back(moved_node->get_name());
  1800. }
  1801. }
  1802. if (dragged_node_names.size() > 0) {
  1803. emit_signal(SNAME("graph_elements_linked_to_frame_request"), dragged_node_names, frame_dropped_on->get_name());
  1804. }
  1805. }
  1806. }
  1807. dragging = false;
  1808. minimap->queue_redraw();
  1809. queue_redraw();
  1810. connections_layer->queue_redraw();
  1811. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1812. }
  1813. // Node selection logic.
  1814. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  1815. GraphElement *graph_element = nullptr;
  1816. // Find node which was clicked on.
  1817. for (int i = get_child_count() - 1; i >= 0; i--) {
  1818. GraphElement *selected_element = Object::cast_to<GraphElement>(get_child(i));
  1819. if (!selected_element) {
  1820. continue;
  1821. }
  1822. if (!selected_element->is_visible()) {
  1823. continue;
  1824. }
  1825. if (selected_element->is_resizing()) {
  1826. continue;
  1827. }
  1828. if (selected_element->has_point((mb->get_position() - selected_element->get_position()) / zoom)) {
  1829. graph_element = selected_element;
  1830. break;
  1831. }
  1832. }
  1833. if (graph_element) {
  1834. if (_filter_input(mb->get_position())) {
  1835. return;
  1836. }
  1837. // Left-clicked on a node, select it.
  1838. dragging = true;
  1839. drag_accum = Vector2();
  1840. just_selected = !graph_element->is_selected();
  1841. if (!graph_element->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1842. for (int i = 0; i < get_child_count(); i++) {
  1843. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1844. if (!child_element) {
  1845. continue;
  1846. }
  1847. child_element->set_selected(child_element == graph_element);
  1848. }
  1849. }
  1850. graph_element->set_selected(true);
  1851. for (int i = 0; i < get_child_count(); i++) {
  1852. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1853. if (!child_element) {
  1854. continue;
  1855. }
  1856. if (child_element->is_selected()) {
  1857. child_element->set_drag(true);
  1858. GraphFrame *frame_node = Object::cast_to<GraphFrame>(get_child(i));
  1859. if (frame_node) {
  1860. _ensure_node_order_from(frame_node);
  1861. _set_drag_frame_attached_nodes(frame_node, true);
  1862. }
  1863. }
  1864. }
  1865. } else {
  1866. if (_filter_input(mb->get_position())) {
  1867. return;
  1868. }
  1869. if (panner->is_panning()) {
  1870. return;
  1871. }
  1872. // Left-clicked on empty space, start box select.
  1873. box_selecting = true;
  1874. box_selecting_from = mb->get_position();
  1875. if (mb->is_command_or_control_pressed()) {
  1876. box_selection_mode_additive = true;
  1877. prev_selected.clear();
  1878. for (int i = get_child_count() - 1; i >= 0; i--) {
  1879. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1880. if (!child_element || !child_element->is_selected()) {
  1881. continue;
  1882. }
  1883. prev_selected.push_back(child_element);
  1884. }
  1885. } else if (mb->is_shift_pressed()) {
  1886. box_selection_mode_additive = false;
  1887. prev_selected.clear();
  1888. for (int i = get_child_count() - 1; i >= 0; i--) {
  1889. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1890. if (!child_element || !child_element->is_selected()) {
  1891. continue;
  1892. }
  1893. prev_selected.push_back(child_element);
  1894. }
  1895. } else {
  1896. box_selection_mode_additive = true;
  1897. prev_selected.clear();
  1898. for (int i = get_child_count() - 1; i >= 0; i--) {
  1899. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1900. if (!child_element) {
  1901. continue;
  1902. }
  1903. child_element->set_selected(false);
  1904. }
  1905. }
  1906. }
  1907. }
  1908. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
  1909. // Box selection ended. Nodes were selected during mouse movement.
  1910. box_selecting = false;
  1911. box_selecting_rect = Rect2();
  1912. prev_selected.clear();
  1913. top_layer->queue_redraw();
  1914. minimap->queue_redraw();
  1915. }
  1916. }
  1917. key_input(p_ev);
  1918. }
  1919. void GraphEdit::key_input(const Ref<InputEvent> &p_ev) {
  1920. if (p_ev->is_pressed()) {
  1921. if (p_ev->is_action("ui_graph_duplicate", true)) {
  1922. emit_signal(SNAME("duplicate_nodes_request"));
  1923. accept_event();
  1924. } else if (p_ev->is_action("ui_copy", true)) {
  1925. emit_signal(SNAME("copy_nodes_request"));
  1926. accept_event();
  1927. } else if (p_ev->is_action("ui_cut", true)) {
  1928. emit_signal(SNAME("cut_nodes_request"));
  1929. accept_event();
  1930. } else if (p_ev->is_action("ui_paste", true)) {
  1931. emit_signal(SNAME("paste_nodes_request"));
  1932. accept_event();
  1933. } else if (p_ev->is_action("ui_graph_delete", true)) {
  1934. TypedArray<StringName> nodes;
  1935. for (int i = 0; i < get_child_count(); i++) {
  1936. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1937. if (!graph_element) {
  1938. continue;
  1939. }
  1940. if (graph_element->is_selected()) {
  1941. nodes.push_back(graph_element->get_name());
  1942. }
  1943. }
  1944. emit_signal(SNAME("delete_nodes_request"), nodes);
  1945. accept_event();
  1946. }
  1947. }
  1948. }
  1949. void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1950. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  1951. scroll_offset = (scroll_offset - p_scroll_vec).clamp(min_scroll_offset, max_scroll_offset - get_size());
  1952. if (!awaiting_scroll_offset_update) {
  1953. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  1954. awaiting_scroll_offset_update = true;
  1955. }
  1956. minimap->queue_redraw();
  1957. queue_redraw();
  1958. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1959. connections_layer->queue_redraw();
  1960. }
  1961. void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1962. // We need to invalidate all connections since we don't know whether
  1963. // the user is zooming/panning at the same time.
  1964. _invalidate_connection_line_cache();
  1965. set_zoom_custom(zoom * p_zoom_factor, p_origin);
  1966. }
  1967. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1968. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  1969. for (Ref<Connection> &conn : connection_map[p_from]) {
  1970. if (conn->from_node == p_from && conn->from_port == p_from_port && conn->to_node == p_to && conn->to_port == p_to_port) {
  1971. if (!Math::is_equal_approx(conn->activity, p_activity)) {
  1972. // Update only if changed.
  1973. minimap->queue_redraw();
  1974. conn->_cache.dirty = true;
  1975. connections_layer->queue_redraw();
  1976. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  1977. }
  1978. conn->activity = p_activity;
  1979. return;
  1980. }
  1981. }
  1982. }
  1983. void GraphEdit::reset_all_connection_activity() {
  1984. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  1985. bool changed = false;
  1986. for (Ref<Connection> &conn : connections) {
  1987. if (conn->activity > 0) {
  1988. changed = true;
  1989. conn->_cache.dirty = true;
  1990. }
  1991. conn->activity = 0;
  1992. }
  1993. if (changed) {
  1994. connections_layer->queue_redraw();
  1995. }
  1996. }
  1997. void GraphEdit::clear_connections() {
  1998. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  1999. for (Ref<Connection> &conn : connections) {
  2000. conn->_cache.line->queue_free();
  2001. }
  2002. connections.clear();
  2003. connection_map.clear();
  2004. minimap->queue_redraw();
  2005. queue_redraw();
  2006. connections_layer->queue_redraw();
  2007. }
  2008. void GraphEdit::force_connection_drag_end() {
  2009. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  2010. ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
  2011. connecting = false;
  2012. connecting_valid = false;
  2013. keyboard_connecting = false;
  2014. minimap->queue_redraw();
  2015. queue_redraw();
  2016. connections_layer->queue_redraw();
  2017. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  2018. emit_signal(SNAME("connection_drag_ended"));
  2019. }
  2020. bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) {
  2021. bool valid = true;
  2022. GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid);
  2023. return valid;
  2024. }
  2025. void GraphEdit::set_panning_scheme(PanningScheme p_scheme) {
  2026. panning_scheme = p_scheme;
  2027. panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme);
  2028. }
  2029. GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const {
  2030. return panning_scheme;
  2031. }
  2032. void GraphEdit::set_zoom(float p_zoom) {
  2033. set_zoom_custom(p_zoom, get_size() / 2);
  2034. }
  2035. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  2036. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  2037. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  2038. if (zoom == p_zoom) {
  2039. return;
  2040. }
  2041. Point2 zoom_anchor = (scroll_offset + p_center) / zoom;
  2042. zoom = p_zoom;
  2043. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  2044. zoom_minus_button->set_disabled(zoom == zoom_min);
  2045. zoom_plus_button->set_disabled(zoom == zoom_max);
  2046. _update_scrollbars();
  2047. minimap->queue_redraw();
  2048. connections_layer->queue_redraw();
  2049. if (is_visible_in_tree()) {
  2050. scroll_offset = zoom_anchor * zoom - p_center;
  2051. }
  2052. _update_zoom_label();
  2053. queue_redraw();
  2054. }
  2055. float GraphEdit::get_zoom() const {
  2056. return zoom;
  2057. }
  2058. void GraphEdit::set_zoom_step(float p_zoom_step) {
  2059. p_zoom_step = std::abs(p_zoom_step);
  2060. ERR_FAIL_COND(!std::isfinite(p_zoom_step));
  2061. if (zoom_step == p_zoom_step) {
  2062. return;
  2063. }
  2064. zoom_step = p_zoom_step;
  2065. panner->set_scroll_zoom_factor(zoom_step);
  2066. }
  2067. float GraphEdit::get_zoom_step() const {
  2068. return zoom_step;
  2069. }
  2070. void GraphEdit::set_zoom_min(float p_zoom_min) {
  2071. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  2072. if (zoom_min == p_zoom_min) {
  2073. return;
  2074. }
  2075. zoom_min = p_zoom_min;
  2076. set_zoom(zoom);
  2077. }
  2078. float GraphEdit::get_zoom_min() const {
  2079. return zoom_min;
  2080. }
  2081. void GraphEdit::set_zoom_max(float p_zoom_max) {
  2082. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  2083. if (zoom_max == p_zoom_max) {
  2084. return;
  2085. }
  2086. zoom_max = p_zoom_max;
  2087. set_zoom(zoom);
  2088. }
  2089. float GraphEdit::get_zoom_max() const {
  2090. return zoom_max;
  2091. }
  2092. void GraphEdit::set_right_disconnects(bool p_enable) {
  2093. right_disconnects = p_enable;
  2094. }
  2095. bool GraphEdit::is_right_disconnects_enabled() const {
  2096. return right_disconnects;
  2097. }
  2098. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  2099. valid_right_disconnect_types.insert(p_type);
  2100. }
  2101. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  2102. valid_right_disconnect_types.erase(p_type);
  2103. }
  2104. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  2105. valid_left_disconnect_types.insert(p_type);
  2106. }
  2107. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  2108. valid_left_disconnect_types.erase(p_type);
  2109. }
  2110. void GraphEdit::set_connections(const TypedArray<Dictionary> &p_connections) {
  2111. clear_connections();
  2112. bool is_editor = Engine::get_singleton()->is_editor_hint();
  2113. for (const Dictionary d : p_connections) {
  2114. // Always keep the connection alive in case it is created using the inspector.
  2115. bool keep_alive = (is_editor && d.is_empty()) || d["keep_alive"];
  2116. connect_node(d["from_node"], d["from_port"], d["to_node"], d["to_port"], keep_alive);
  2117. }
  2118. }
  2119. TypedArray<Dictionary> GraphEdit::_get_connection_list() const {
  2120. Vector<Ref<Connection>> conns = get_connections();
  2121. TypedArray<Dictionary> arr;
  2122. for (const Ref<Connection> &conn : conns) {
  2123. Dictionary d;
  2124. d["from_node"] = conn->from_node;
  2125. d["from_port"] = conn->from_port;
  2126. d["to_node"] = conn->to_node;
  2127. d["to_port"] = conn->to_port;
  2128. d["keep_alive"] = conn->keep_alive;
  2129. arr.push_back(d);
  2130. }
  2131. return arr;
  2132. }
  2133. Dictionary GraphEdit::_get_closest_connection_at_point(const Vector2 &p_point, float p_max_distance) const {
  2134. Dictionary ret;
  2135. Ref<Connection> c = get_closest_connection_at_point(p_point, p_max_distance);
  2136. if (c.is_valid()) {
  2137. ret["from_node"] = c->from_node;
  2138. ret["from_port"] = c->from_port;
  2139. ret["to_node"] = c->to_node;
  2140. ret["to_port"] = c->to_port;
  2141. ret["keep_alive"] = c->keep_alive;
  2142. }
  2143. return ret;
  2144. }
  2145. TypedArray<Dictionary> GraphEdit::_get_connections_intersecting_with_rect(const Rect2 &p_rect) const {
  2146. List<Ref<Connection>> intersecting_connections = get_connections_intersecting_with_rect(p_rect);
  2147. TypedArray<Dictionary> arr;
  2148. for (const Ref<Connection> &conn : intersecting_connections) {
  2149. Dictionary d;
  2150. d["from_node"] = conn->from_node;
  2151. d["from_port"] = conn->from_port;
  2152. d["to_node"] = conn->to_node;
  2153. d["to_port"] = conn->to_port;
  2154. d["keep_alive"] = conn->keep_alive;
  2155. arr.push_back(d);
  2156. }
  2157. return arr;
  2158. }
  2159. TypedArray<Dictionary> GraphEdit::_get_connection_list_from_node(const StringName &p_node) const {
  2160. ERR_FAIL_COND_V(!connection_map.has(p_node), TypedArray<Dictionary>());
  2161. List<Ref<GraphEdit::Connection>> connections_from_node = connection_map.get(p_node);
  2162. TypedArray<Dictionary> connections_from_node_dict;
  2163. for (const Ref<Connection> &conn : connections_from_node) {
  2164. Dictionary d;
  2165. d["from_node"] = conn->from_node;
  2166. d["from_port"] = conn->from_port;
  2167. d["to_node"] = conn->to_node;
  2168. d["to_port"] = conn->to_port;
  2169. d["keep_alive"] = conn->keep_alive;
  2170. connections_from_node_dict.push_back(d);
  2171. }
  2172. return connections_from_node_dict;
  2173. }
  2174. void GraphEdit::_zoom_minus() {
  2175. set_zoom(zoom / zoom_step);
  2176. }
  2177. void GraphEdit::_zoom_reset() {
  2178. set_zoom(1);
  2179. }
  2180. void GraphEdit::_zoom_plus() {
  2181. set_zoom(zoom * zoom_step);
  2182. }
  2183. void GraphEdit::_update_zoom_label() {
  2184. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  2185. String zoom_text = itos(zoom_percent) + "%";
  2186. zoom_label->set_text(zoom_text);
  2187. }
  2188. void GraphEdit::_invalidate_connection_line_cache() {
  2189. for (Ref<Connection> &conn : connections) {
  2190. conn->_cache.dirty = true;
  2191. }
  2192. }
  2193. float GraphEdit::_get_shader_line_width() {
  2194. return lines_thickness * theme_cache.base_scale + 4.0;
  2195. }
  2196. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  2197. ConnectionType ct(p_type, p_with_type);
  2198. valid_connection_types.insert(ct);
  2199. }
  2200. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  2201. ConnectionType ct(p_type, p_with_type);
  2202. valid_connection_types.erase(ct);
  2203. }
  2204. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  2205. ConnectionType ct(p_type, p_with_type);
  2206. return valid_connection_types.has(ct);
  2207. }
  2208. void GraphEdit::attach_graph_element_to_frame(const StringName &p_graph_element, const StringName &p_parent_frame) {
  2209. GraphFrame *frame = Object::cast_to<GraphFrame>(get_node(NodePath(p_parent_frame)));
  2210. ERR_FAIL_NULL_MSG(frame, "Frame does not exist or is not of type GraphFrame.");
  2211. GraphElement *graph_element = Object::cast_to<GraphElement>(get_node(NodePath(p_graph_element)));
  2212. ERR_FAIL_NULL_MSG(graph_element, "Graph element to attach does not exist or is not of type GraphElement.");
  2213. ERR_FAIL_COND_MSG(frame == graph_element, "Cannot attach a frame to itself.");
  2214. linked_parent_map.insert(p_graph_element, p_parent_frame);
  2215. frame_attached_nodes[p_parent_frame].insert(p_graph_element);
  2216. _ensure_node_order_from_root(p_graph_element);
  2217. _update_graph_frame(frame);
  2218. }
  2219. void GraphEdit::detach_graph_element_from_frame(const StringName &p_graph_element) {
  2220. if (!linked_parent_map.has(p_graph_element)) {
  2221. return;
  2222. }
  2223. GraphFrame *frame = Object::cast_to<GraphFrame>(get_node(NodePath(linked_parent_map[p_graph_element])));
  2224. ERR_FAIL_NULL_MSG(frame, "Frame does not exist or is not of type GraphFrame.");
  2225. GraphElement *graph_element = Object::cast_to<GraphElement>(get_node(NodePath(p_graph_element)));
  2226. ERR_FAIL_NULL_MSG(graph_element, "Graph element to detach does not exist or is not of type GraphElement.");
  2227. frame_attached_nodes.get(frame->get_name()).erase(p_graph_element);
  2228. linked_parent_map.erase(p_graph_element);
  2229. _update_graph_frame(frame);
  2230. }
  2231. GraphFrame *GraphEdit::get_element_frame(const StringName &p_attached_graph_element) {
  2232. if (!linked_parent_map.has(p_attached_graph_element)) {
  2233. return nullptr;
  2234. }
  2235. Node *parent = get_node_or_null(NodePath(linked_parent_map[p_attached_graph_element]));
  2236. return Object::cast_to<GraphFrame>(parent);
  2237. }
  2238. TypedArray<StringName> GraphEdit::get_attached_nodes_of_frame(const StringName &p_graph_frame) {
  2239. if (!frame_attached_nodes.has(p_graph_frame)) {
  2240. return TypedArray<StringName>();
  2241. }
  2242. TypedArray<StringName> attached_nodes;
  2243. for (const StringName &node : frame_attached_nodes.get(p_graph_frame)) {
  2244. attached_nodes.push_back(node);
  2245. }
  2246. return attached_nodes;
  2247. }
  2248. void GraphEdit::set_snapping_enabled(bool p_enable) {
  2249. if (snapping_enabled == p_enable) {
  2250. return;
  2251. }
  2252. snapping_enabled = p_enable;
  2253. toggle_snapping_button->set_pressed(p_enable);
  2254. queue_redraw();
  2255. }
  2256. bool GraphEdit::is_snapping_enabled() const {
  2257. return snapping_enabled;
  2258. }
  2259. void GraphEdit::set_snapping_distance(int p_snapping_distance) {
  2260. ERR_FAIL_COND_MSG(p_snapping_distance < GRID_MIN_SNAPPING_DISTANCE || p_snapping_distance > GRID_MAX_SNAPPING_DISTANCE,
  2261. vformat("GraphEdit's snapping distance must be between %d and %d (inclusive)", GRID_MIN_SNAPPING_DISTANCE, GRID_MAX_SNAPPING_DISTANCE));
  2262. snapping_distance = p_snapping_distance;
  2263. snapping_distance_spinbox->set_value(p_snapping_distance);
  2264. queue_redraw();
  2265. }
  2266. int GraphEdit::get_snapping_distance() const {
  2267. return snapping_distance;
  2268. }
  2269. void GraphEdit::set_show_grid(bool p_show) {
  2270. if (show_grid == p_show) {
  2271. return;
  2272. }
  2273. show_grid = p_show;
  2274. toggle_grid_button->set_pressed(p_show);
  2275. queue_redraw();
  2276. }
  2277. bool GraphEdit::is_showing_grid() const {
  2278. return show_grid;
  2279. }
  2280. void GraphEdit::set_grid_pattern(GridPattern p_pattern) {
  2281. if (grid_pattern == p_pattern) {
  2282. return;
  2283. }
  2284. grid_pattern = p_pattern;
  2285. queue_redraw();
  2286. }
  2287. GraphEdit::GridPattern GraphEdit::get_grid_pattern() const {
  2288. return grid_pattern;
  2289. }
  2290. void GraphEdit::_snapping_toggled() {
  2291. snapping_enabled = toggle_snapping_button->is_pressed();
  2292. }
  2293. void GraphEdit::_snapping_distance_changed(double) {
  2294. snapping_distance = snapping_distance_spinbox->get_value();
  2295. queue_redraw();
  2296. }
  2297. void GraphEdit::_show_grid_toggled() {
  2298. show_grid = toggle_grid_button->is_pressed();
  2299. queue_redraw();
  2300. }
  2301. void GraphEdit::set_minimap_size(Vector2 p_size) {
  2302. minimap->set_size(p_size);
  2303. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  2304. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  2305. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  2306. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  2307. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2308. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2309. minimap->queue_redraw();
  2310. }
  2311. Vector2 GraphEdit::get_minimap_size() const {
  2312. return minimap->get_size();
  2313. }
  2314. void GraphEdit::set_minimap_opacity(float p_opacity) {
  2315. if (minimap->get_modulate().a == p_opacity) {
  2316. return;
  2317. }
  2318. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  2319. minimap->queue_redraw();
  2320. }
  2321. float GraphEdit::get_minimap_opacity() const {
  2322. Color minimap_modulate = minimap->get_modulate();
  2323. return minimap_modulate.a;
  2324. }
  2325. void GraphEdit::set_minimap_enabled(bool p_enable) {
  2326. if (minimap_button->is_pressed() == p_enable) {
  2327. return;
  2328. }
  2329. minimap_button->set_pressed(p_enable);
  2330. _minimap_toggled();
  2331. minimap->queue_redraw();
  2332. }
  2333. bool GraphEdit::is_minimap_enabled() const {
  2334. return minimap_button->is_pressed();
  2335. }
  2336. void GraphEdit::set_show_menu(bool p_hidden) {
  2337. show_menu = p_hidden;
  2338. menu_panel->set_visible(show_menu);
  2339. }
  2340. bool GraphEdit::is_showing_menu() const {
  2341. return show_menu;
  2342. }
  2343. void GraphEdit::set_show_zoom_label(bool p_hidden) {
  2344. show_zoom_label = p_hidden;
  2345. zoom_label->set_visible(show_zoom_label);
  2346. }
  2347. bool GraphEdit::is_showing_zoom_label() const {
  2348. return show_zoom_label;
  2349. }
  2350. void GraphEdit::set_show_zoom_buttons(bool p_hidden) {
  2351. show_zoom_buttons = p_hidden;
  2352. zoom_minus_button->set_visible(show_zoom_buttons);
  2353. zoom_reset_button->set_visible(show_zoom_buttons);
  2354. zoom_plus_button->set_visible(show_zoom_buttons);
  2355. }
  2356. bool GraphEdit::is_showing_zoom_buttons() const {
  2357. return show_zoom_buttons;
  2358. }
  2359. void GraphEdit::set_show_grid_buttons(bool p_hidden) {
  2360. show_grid_buttons = p_hidden;
  2361. toggle_grid_button->set_visible(show_grid_buttons);
  2362. toggle_snapping_button->set_visible(show_grid_buttons);
  2363. snapping_distance_spinbox->set_visible(show_grid_buttons);
  2364. }
  2365. bool GraphEdit::is_showing_grid_buttons() const {
  2366. return show_grid_buttons;
  2367. }
  2368. void GraphEdit::set_show_minimap_button(bool p_hidden) {
  2369. show_minimap_button = p_hidden;
  2370. minimap_button->set_visible(show_minimap_button);
  2371. }
  2372. bool GraphEdit::is_showing_minimap_button() const {
  2373. return show_minimap_button;
  2374. }
  2375. void GraphEdit::set_show_arrange_button(bool p_hidden) {
  2376. show_arrange_button = p_hidden;
  2377. arrange_button->set_visible(show_arrange_button);
  2378. }
  2379. bool GraphEdit::is_showing_arrange_button() const {
  2380. return show_arrange_button;
  2381. }
  2382. void GraphEdit::override_connections_shader(const Ref<Shader> &p_shader) {
  2383. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  2384. connections_shader = p_shader;
  2385. _invalidate_connection_line_cache();
  2386. connections_layer->queue_redraw();
  2387. minimap->queue_redraw();
  2388. callable_mp(this, &GraphEdit::_update_top_connection_layer).call_deferred();
  2389. }
  2390. void GraphEdit::_minimap_toggled() {
  2391. if (is_minimap_enabled()) {
  2392. minimap->set_visible(true);
  2393. minimap->queue_redraw();
  2394. } else {
  2395. minimap->set_visible(false);
  2396. }
  2397. }
  2398. void GraphEdit::set_connection_lines_curvature(float p_curvature) {
  2399. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  2400. lines_curvature = p_curvature;
  2401. _invalidate_connection_line_cache();
  2402. connections_layer->queue_redraw();
  2403. queue_redraw();
  2404. }
  2405. float GraphEdit::get_connection_lines_curvature() const {
  2406. return lines_curvature;
  2407. }
  2408. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  2409. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  2410. ERR_FAIL_COND_MSG(p_thickness < 0, "Connection lines thickness must be greater than or equal to 0.");
  2411. if (lines_thickness == p_thickness) {
  2412. return;
  2413. }
  2414. lines_thickness = p_thickness;
  2415. _invalidate_connection_line_cache();
  2416. connections_layer->queue_redraw();
  2417. queue_redraw();
  2418. }
  2419. float GraphEdit::get_connection_lines_thickness() const {
  2420. return lines_thickness;
  2421. }
  2422. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  2423. ERR_FAIL_NULL_MSG(connections_layer, "connections_layer is missing.");
  2424. if (lines_antialiased == p_antialiased) {
  2425. return;
  2426. }
  2427. lines_antialiased = p_antialiased;
  2428. _invalidate_connection_line_cache();
  2429. connections_layer->queue_redraw();
  2430. queue_redraw();
  2431. }
  2432. bool GraphEdit::is_connection_lines_antialiased() const {
  2433. return lines_antialiased;
  2434. }
  2435. HBoxContainer *GraphEdit::get_menu_hbox() {
  2436. return menu_hbox;
  2437. }
  2438. Ref<ViewPanner> GraphEdit::get_panner() {
  2439. return panner;
  2440. }
  2441. void GraphEdit::set_warped_panning(bool p_warped) {
  2442. warped_panning = p_warped;
  2443. update_warped_panning();
  2444. }
  2445. void GraphEdit::update_warped_panning() {
  2446. panner->setup_warped_panning(get_viewport(), warped_panning);
  2447. }
  2448. void GraphEdit::arrange_nodes() {
  2449. arranger->arrange_nodes();
  2450. }
  2451. void GraphEdit::_bind_methods() {
  2452. ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port", "keep_alive"), &GraphEdit::connect_node, DEFVAL(false));
  2453. ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
  2454. ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
  2455. ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
  2456. ClassDB::bind_method(D_METHOD("set_connections", "connections"), &GraphEdit::set_connections);
  2457. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  2458. ClassDB::bind_method(D_METHOD("get_connection_count", "from_node", "from_port"), &GraphEdit::get_connection_count);
  2459. ClassDB::bind_method(D_METHOD("get_closest_connection_at_point", "point", "max_distance"), &GraphEdit::_get_closest_connection_at_point, DEFVAL(4.0));
  2460. ClassDB::bind_method(D_METHOD("get_connection_list_from_node", "node"), &GraphEdit::_get_connection_list_from_node);
  2461. ClassDB::bind_method(D_METHOD("get_connections_intersecting_with_rect", "rect"), &GraphEdit::_get_connections_intersecting_with_rect);
  2462. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  2463. ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end);
  2464. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &GraphEdit::get_scroll_offset);
  2465. ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &GraphEdit::set_scroll_offset);
  2466. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  2467. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  2468. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  2469. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  2470. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  2471. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  2472. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  2473. ClassDB::bind_method(D_METHOD("get_connection_line", "from_node", "to_node"), &GraphEdit::get_connection_line);
  2474. ClassDB::bind_method(D_METHOD("attach_graph_element_to_frame", "element", "frame"), &GraphEdit::attach_graph_element_to_frame);
  2475. ClassDB::bind_method(D_METHOD("detach_graph_element_from_frame", "element"), &GraphEdit::detach_graph_element_from_frame);
  2476. ClassDB::bind_method(D_METHOD("get_element_frame", "element"), &GraphEdit::get_element_frame);
  2477. ClassDB::bind_method(D_METHOD("get_attached_nodes_of_frame", "frame"), &GraphEdit::get_attached_nodes_of_frame);
  2478. ClassDB::bind_method(D_METHOD("set_panning_scheme", "scheme"), &GraphEdit::set_panning_scheme);
  2479. ClassDB::bind_method(D_METHOD("get_panning_scheme"), &GraphEdit::get_panning_scheme);
  2480. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  2481. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  2482. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  2483. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  2484. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  2485. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  2486. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  2487. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  2488. ClassDB::bind_method(D_METHOD("set_show_grid", "enable"), &GraphEdit::set_show_grid);
  2489. ClassDB::bind_method(D_METHOD("is_showing_grid"), &GraphEdit::is_showing_grid);
  2490. ClassDB::bind_method(D_METHOD("set_grid_pattern", "pattern"), &GraphEdit::set_grid_pattern);
  2491. ClassDB::bind_method(D_METHOD("get_grid_pattern"), &GraphEdit::get_grid_pattern);
  2492. ClassDB::bind_method(D_METHOD("set_snapping_enabled", "enable"), &GraphEdit::set_snapping_enabled);
  2493. ClassDB::bind_method(D_METHOD("is_snapping_enabled"), &GraphEdit::is_snapping_enabled);
  2494. ClassDB::bind_method(D_METHOD("set_snapping_distance", "pixels"), &GraphEdit::set_snapping_distance);
  2495. ClassDB::bind_method(D_METHOD("get_snapping_distance"), &GraphEdit::get_snapping_distance);
  2496. ClassDB::bind_method(D_METHOD("set_connection_lines_curvature", "curvature"), &GraphEdit::set_connection_lines_curvature);
  2497. ClassDB::bind_method(D_METHOD("get_connection_lines_curvature"), &GraphEdit::get_connection_lines_curvature);
  2498. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  2499. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  2500. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  2501. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  2502. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  2503. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  2504. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  2505. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  2506. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  2507. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  2508. ClassDB::bind_method(D_METHOD("set_show_menu", "hidden"), &GraphEdit::set_show_menu);
  2509. ClassDB::bind_method(D_METHOD("is_showing_menu"), &GraphEdit::is_showing_menu);
  2510. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  2511. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  2512. ClassDB::bind_method(D_METHOD("set_show_grid_buttons", "hidden"), &GraphEdit::set_show_grid_buttons);
  2513. ClassDB::bind_method(D_METHOD("is_showing_grid_buttons"), &GraphEdit::is_showing_grid_buttons);
  2514. ClassDB::bind_method(D_METHOD("set_show_zoom_buttons", "hidden"), &GraphEdit::set_show_zoom_buttons);
  2515. ClassDB::bind_method(D_METHOD("is_showing_zoom_buttons"), &GraphEdit::is_showing_zoom_buttons);
  2516. ClassDB::bind_method(D_METHOD("set_show_minimap_button", "hidden"), &GraphEdit::set_show_minimap_button);
  2517. ClassDB::bind_method(D_METHOD("is_showing_minimap_button"), &GraphEdit::is_showing_minimap_button);
  2518. ClassDB::bind_method(D_METHOD("set_show_arrange_button", "hidden"), &GraphEdit::set_show_arrange_button);
  2519. ClassDB::bind_method(D_METHOD("is_showing_arrange_button"), &GraphEdit::is_showing_arrange_button);
  2520. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  2521. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  2522. ClassDB::bind_method(D_METHOD("set_type_names", "type_names"), &GraphEdit::set_type_names);
  2523. ClassDB::bind_method(D_METHOD("get_type_names"), &GraphEdit::get_type_names);
  2524. GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node", "in_port", "mouse_position");
  2525. GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node", "in_port", "mouse_position");
  2526. ClassDB::bind_method(D_METHOD("get_menu_hbox"), &GraphEdit::get_menu_hbox);
  2527. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  2528. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  2529. GDVIRTUAL_BIND(_get_connection_line, "from_position", "to_position")
  2530. GDVIRTUAL_BIND(_is_node_hover_valid, "from_node", "from_port", "to_node", "to_port");
  2531. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
  2532. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid"), "set_show_grid", "is_showing_grid");
  2533. ADD_PROPERTY(PropertyInfo(Variant::INT, "grid_pattern", PROPERTY_HINT_ENUM, "Lines,Dots"), "set_grid_pattern", "get_grid_pattern");
  2534. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snapping_enabled"), "set_snapping_enabled", "is_snapping_enabled");
  2535. ADD_PROPERTY(PropertyInfo(Variant::INT, "snapping_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snapping_distance", "get_snapping_distance");
  2536. ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
  2537. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  2538. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "type_names", PROPERTY_HINT_DICTIONARY_TYPE, "int;String"), "set_type_names", "get_type_names");
  2539. ADD_GROUP("Connection Lines", "connection_lines");
  2540. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
  2541. 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");
  2542. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  2543. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "connections", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::DICTIONARY, PROPERTY_HINT_NONE, String())), "set_connections", "get_connection_list");
  2544. ADD_GROUP("Zoom", "");
  2545. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  2546. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  2547. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  2548. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  2549. ADD_GROUP("Minimap", "minimap_");
  2550. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  2551. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_size", "get_minimap_size");
  2552. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  2553. ADD_GROUP("Toolbar Menu", "");
  2554. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_menu"), "set_show_menu", "is_showing_menu");
  2555. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  2556. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_buttons"), "set_show_zoom_buttons", "is_showing_zoom_buttons");
  2557. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid_buttons"), "set_show_grid_buttons", "is_showing_grid_buttons");
  2558. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_minimap_button"), "set_show_minimap_button", "is_showing_minimap_button");
  2559. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_arrange_button"), "set_show_arrange_button", "is_showing_arrange_button");
  2560. 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")));
  2561. 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")));
  2562. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2563. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2564. ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output")));
  2565. ADD_SIGNAL(MethodInfo("connection_drag_ended"));
  2566. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  2567. ADD_SIGNAL(MethodInfo("cut_nodes_request"));
  2568. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  2569. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  2570. ADD_SIGNAL(MethodInfo("delete_nodes_request", PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_ARRAY_TYPE, "StringName")));
  2571. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2572. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2573. ADD_SIGNAL(MethodInfo("frame_rect_changed", PropertyInfo(Variant::OBJECT, "frame", PROPERTY_HINT_RESOURCE_TYPE, "GraphFrame"), PropertyInfo(Variant::RECT2, "new_rect")));
  2574. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "at_position")));
  2575. ADD_SIGNAL(MethodInfo("begin_node_move"));
  2576. ADD_SIGNAL(MethodInfo("end_node_move"));
  2577. ADD_SIGNAL(MethodInfo("graph_elements_linked_to_frame_request", PropertyInfo(Variant::ARRAY, "elements"), PropertyInfo(Variant::STRING_NAME, "frame")));
  2578. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "offset")));
  2579. BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
  2580. BIND_ENUM_CONSTANT(SCROLL_PANS);
  2581. BIND_ENUM_CONSTANT(GRID_PATTERN_LINES);
  2582. BIND_ENUM_CONSTANT(GRID_PATTERN_DOTS);
  2583. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, panel);
  2584. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, panel_focus);
  2585. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_major);
  2586. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_minor);
  2587. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, GraphEdit, activity_color, "activity");
  2588. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_hover_tint_color);
  2589. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, connection_hover_thickness);
  2590. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_valid_target_tint_color);
  2591. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, connection_rim_color);
  2592. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_fill);
  2593. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_stroke);
  2594. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, menu_panel);
  2595. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_in);
  2596. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_out);
  2597. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_reset);
  2598. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, snapping_toggle);
  2599. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, grid_toggle);
  2600. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, minimap_toggle);
  2601. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, layout);
  2602. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_inner_extent);
  2603. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_outer_extent);
  2604. ADD_CLASS_DEPENDENCY("Button");
  2605. ADD_CLASS_DEPENDENCY("GraphFrame");
  2606. ADD_CLASS_DEPENDENCY("GraphNode");
  2607. ADD_CLASS_DEPENDENCY("HScrollBar");
  2608. ADD_CLASS_DEPENDENCY("SpinBox");
  2609. ADD_CLASS_DEPENDENCY("VScrollBar");
  2610. }
  2611. GraphEdit::GraphEdit() {
  2612. set_focus_mode(FOCUS_ALL);
  2613. // Allow dezooming 8 times from the default zoom level.
  2614. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  2615. // but this is still useful for previewing and navigation.
  2616. zoom_min = (1 / Math::pow(zoom_step, 8));
  2617. // Allow zooming 4 times from the default zoom level.
  2618. zoom_max = (1 * Math::pow(zoom_step, 4));
  2619. panner.instantiate();
  2620. panner->set_callbacks(callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback));
  2621. top_layer = memnew(Control);
  2622. add_child(top_layer, false, INTERNAL_MODE_BACK);
  2623. top_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2624. top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2625. top_layer->connect(SceneStringName(draw), callable_mp(this, &GraphEdit::_top_layer_draw));
  2626. top_layer->connect(SceneStringName(focus_exited), callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  2627. connections_layer = memnew(Control);
  2628. add_child(connections_layer, false);
  2629. connections_layer->connect(SceneStringName(draw), callable_mp(this, &GraphEdit::_update_connections));
  2630. connections_layer->set_name("_connection_layer");
  2631. connections_layer->set_disable_visibility_clip(true); // Necessary, so it can draw freely and be offset.
  2632. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2633. top_connection_layer = memnew(GraphEditFilter(this));
  2634. add_child(top_connection_layer, false, INTERNAL_MODE_BACK);
  2635. connections_shader = default_connections_shader;
  2636. top_connection_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  2637. top_connection_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2638. top_connection_layer->connect(SceneStringName(gui_input), callable_mp(this, &GraphEdit::_top_connection_layer_input));
  2639. dragged_connection_line = memnew(Line2D);
  2640. dragged_connection_line->set_texture_mode(Line2D::LINE_TEXTURE_STRETCH);
  2641. top_connection_layer->add_child(dragged_connection_line);
  2642. h_scrollbar = memnew(HScrollBar);
  2643. h_scrollbar->set_name("_h_scroll");
  2644. top_layer->add_child(h_scrollbar);
  2645. v_scrollbar = memnew(VScrollBar);
  2646. v_scrollbar->set_name("_v_scroll");
  2647. top_layer->add_child(v_scrollbar);
  2648. // Set large minmax so it can scroll even if not resized yet.
  2649. h_scrollbar->set_min(-10000);
  2650. h_scrollbar->set_max(10000);
  2651. v_scrollbar->set_min(-10000);
  2652. v_scrollbar->set_max(10000);
  2653. h_scrollbar->connect(SceneStringName(value_changed), callable_mp(this, &GraphEdit::_scrollbar_moved));
  2654. v_scrollbar->connect(SceneStringName(value_changed), callable_mp(this, &GraphEdit::_scrollbar_moved));
  2655. // Toolbar menu.
  2656. menu_panel = memnew(PanelContainer);
  2657. menu_panel->set_visible(show_menu);
  2658. top_layer->add_child(menu_panel);
  2659. menu_panel->set_position(Vector2(10, 10));
  2660. menu_hbox = memnew(HBoxContainer);
  2661. menu_panel->add_child(menu_hbox);
  2662. // Zoom label and controls.
  2663. zoom_label = memnew(Label);
  2664. zoom_label->set_visible(show_zoom_label);
  2665. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  2666. zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2667. zoom_label->set_custom_minimum_size(Size2(48, 0));
  2668. menu_hbox->add_child(zoom_label);
  2669. _update_zoom_label();
  2670. zoom_minus_button = memnew(Button);
  2671. zoom_minus_button->set_theme_type_variation(SceneStringName(FlatButton));
  2672. zoom_minus_button->set_visible(show_zoom_buttons);
  2673. zoom_minus_button->set_tooltip_text(ETR("Zoom Out"));
  2674. zoom_minus_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  2675. menu_hbox->add_child(zoom_minus_button);
  2676. zoom_minus_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_zoom_minus));
  2677. zoom_reset_button = memnew(Button);
  2678. zoom_reset_button->set_theme_type_variation(SceneStringName(FlatButton));
  2679. zoom_reset_button->set_visible(show_zoom_buttons);
  2680. zoom_reset_button->set_tooltip_text(ETR("Zoom Reset"));
  2681. zoom_reset_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  2682. menu_hbox->add_child(zoom_reset_button);
  2683. zoom_reset_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_zoom_reset));
  2684. zoom_plus_button = memnew(Button);
  2685. zoom_plus_button->set_theme_type_variation(SceneStringName(FlatButton));
  2686. zoom_plus_button->set_visible(show_zoom_buttons);
  2687. zoom_plus_button->set_tooltip_text(ETR("Zoom In"));
  2688. zoom_plus_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  2689. menu_hbox->add_child(zoom_plus_button);
  2690. zoom_plus_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_zoom_plus));
  2691. // Grid controls.
  2692. toggle_grid_button = memnew(Button);
  2693. toggle_grid_button->set_theme_type_variation(SceneStringName(FlatButton));
  2694. toggle_grid_button->set_visible(show_grid_buttons);
  2695. toggle_grid_button->set_toggle_mode(true);
  2696. toggle_grid_button->set_pressed(true);
  2697. toggle_grid_button->set_tooltip_text(ETR("Toggle the visual grid."));
  2698. toggle_grid_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  2699. menu_hbox->add_child(toggle_grid_button);
  2700. toggle_grid_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_show_grid_toggled));
  2701. toggle_snapping_button = memnew(Button);
  2702. toggle_snapping_button->set_theme_type_variation(SceneStringName(FlatButton));
  2703. toggle_snapping_button->set_visible(show_grid_buttons);
  2704. toggle_snapping_button->set_toggle_mode(true);
  2705. toggle_snapping_button->set_tooltip_text(ETR("Toggle snapping to the grid."));
  2706. toggle_snapping_button->set_pressed(snapping_enabled);
  2707. toggle_snapping_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  2708. menu_hbox->add_child(toggle_snapping_button);
  2709. toggle_snapping_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_snapping_toggled));
  2710. snapping_distance_spinbox = memnew(SpinBox);
  2711. snapping_distance_spinbox->set_visible(show_grid_buttons);
  2712. snapping_distance_spinbox->set_min(GRID_MIN_SNAPPING_DISTANCE);
  2713. snapping_distance_spinbox->set_max(GRID_MAX_SNAPPING_DISTANCE);
  2714. snapping_distance_spinbox->set_step(1);
  2715. snapping_distance_spinbox->set_value(snapping_distance);
  2716. snapping_distance_spinbox->set_tooltip_text(ETR("Change the snapping distance."));
  2717. menu_hbox->add_child(snapping_distance_spinbox);
  2718. snapping_distance_spinbox->connect(SceneStringName(value_changed), callable_mp(this, &GraphEdit::_snapping_distance_changed));
  2719. // Extra controls.
  2720. minimap_button = memnew(Button);
  2721. minimap_button->set_theme_type_variation(SceneStringName(FlatButton));
  2722. minimap_button->set_visible(show_minimap_button);
  2723. minimap_button->set_toggle_mode(true);
  2724. minimap_button->set_tooltip_text(ETR("Toggle the graph minimap."));
  2725. minimap_button->set_pressed(show_grid);
  2726. minimap_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  2727. menu_hbox->add_child(minimap_button);
  2728. minimap_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::_minimap_toggled));
  2729. arrange_button = memnew(Button);
  2730. arrange_button->set_theme_type_variation(SceneStringName(FlatButton));
  2731. arrange_button->set_visible(show_arrange_button);
  2732. arrange_button->set_tooltip_text(ETR("Automatically arrange selected nodes."));
  2733. arrange_button->connect(SceneStringName(pressed), callable_mp(this, &GraphEdit::arrange_nodes));
  2734. arrange_button->set_focus_mode(FOCUS_ACCESSIBILITY);
  2735. menu_hbox->add_child(arrange_button);
  2736. // Minimap.
  2737. const Vector2 minimap_size = Vector2(240, 160);
  2738. const float minimap_opacity = 0.65;
  2739. minimap = memnew(GraphEditMinimap(this));
  2740. top_layer->add_child(minimap);
  2741. minimap->set_name("_minimap");
  2742. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  2743. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  2744. minimap->set_custom_minimum_size(Vector2(50, 50));
  2745. minimap->set_size(minimap_size);
  2746. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  2747. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  2748. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  2749. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2750. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2751. minimap->connect(SceneStringName(draw), callable_mp(this, &GraphEdit::_minimap_draw));
  2752. set_clip_contents(true);
  2753. arranger.instantiate(this);
  2754. }