graph_edit.cpp 112 KB

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