graph_edit.cpp 111 KB

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