graph_edit.cpp 122 KB

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