2
0

graph_edit.cpp 107 KB

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