graph_edit.cpp 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  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 "core/input/input.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/keyboard.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/view_panner.h"
  37. constexpr int MINIMAP_OFFSET = 12;
  38. constexpr int MINIMAP_PADDING = 5;
  39. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  40. return ge->_filter_input(p_point);
  41. }
  42. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  43. ge = p_edit;
  44. }
  45. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  46. ge = p_edit;
  47. graph_proportions = Vector2(1, 1);
  48. graph_padding = Vector2(0, 0);
  49. camera_position = Vector2(100, 50);
  50. camera_size = Vector2(200, 200);
  51. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  52. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  53. is_pressing = false;
  54. is_resizing = false;
  55. }
  56. Control::CursorShape GraphEditMinimap::get_cursor_shape(const Point2 &p_pos) const {
  57. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  58. if (is_resizing || (p_pos.x < resizer->get_width() && p_pos.y < resizer->get_height())) {
  59. return CURSOR_FDIAGSIZE;
  60. }
  61. return Control::get_cursor_shape(p_pos);
  62. }
  63. void GraphEditMinimap::update_minimap() {
  64. Vector2 graph_offset = _get_graph_offset();
  65. Vector2 graph_size = _get_graph_size();
  66. camera_position = ge->get_scroll_ofs() - graph_offset;
  67. camera_size = ge->get_size();
  68. Vector2 render_size = _get_render_size();
  69. float target_ratio = render_size.x / render_size.y;
  70. float graph_ratio = graph_size.x / graph_size.y;
  71. graph_proportions = graph_size;
  72. graph_padding = Vector2(0, 0);
  73. if (graph_ratio > target_ratio) {
  74. graph_proportions.x = graph_size.x;
  75. graph_proportions.y = graph_size.x / target_ratio;
  76. graph_padding.y = Math::abs(graph_size.y - graph_proportions.y) / 2;
  77. } else {
  78. graph_proportions.x = graph_size.y * target_ratio;
  79. graph_proportions.y = graph_size.y;
  80. graph_padding.x = Math::abs(graph_size.x - graph_proportions.x) / 2;
  81. }
  82. // This centers minimap inside the minimap rectangle.
  83. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  84. }
  85. Rect2 GraphEditMinimap::get_camera_rect() {
  86. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  87. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  88. Vector2 camera_pos = (camera_center - camera_viewport / 2);
  89. return Rect2(camera_pos, camera_viewport);
  90. }
  91. Vector2 GraphEditMinimap::_get_render_size() {
  92. if (!is_inside_tree()) {
  93. return Vector2(0, 0);
  94. }
  95. return get_size() - 2 * minimap_padding;
  96. }
  97. Vector2 GraphEditMinimap::_get_graph_offset() {
  98. return Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  99. }
  100. Vector2 GraphEditMinimap::_get_graph_size() {
  101. Vector2 graph_size = Vector2(ge->h_scroll->get_max(), ge->v_scroll->get_max()) - Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  102. if (graph_size.x == 0) {
  103. graph_size.x = 1;
  104. }
  105. if (graph_size.y == 0) {
  106. graph_size.y = 1;
  107. }
  108. return graph_size;
  109. }
  110. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  111. Vector2 map_position = Vector2(0, 0);
  112. Vector2 render_size = _get_render_size();
  113. map_position.x = p_position.x * render_size.x / graph_proportions.x;
  114. map_position.y = p_position.y * render_size.y / graph_proportions.y;
  115. return map_position;
  116. }
  117. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  118. Vector2 graph_position = Vector2(0, 0);
  119. Vector2 render_size = _get_render_size();
  120. graph_position.x = p_position.x * graph_proportions.x / render_size.x;
  121. graph_position.y = p_position.y * graph_proportions.y / render_size.y;
  122. return graph_position;
  123. }
  124. void GraphEditMinimap::gui_input(const Ref<InputEvent> &p_ev) {
  125. ERR_FAIL_COND(p_ev.is_null());
  126. if (!ge->is_minimap_enabled()) {
  127. return;
  128. }
  129. Ref<InputEventMouseButton> mb = p_ev;
  130. Ref<InputEventMouseMotion> mm = p_ev;
  131. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  132. if (mb->is_pressed()) {
  133. is_pressing = true;
  134. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  135. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  136. if (resizer_hitbox.has_point(mb->get_position())) {
  137. is_resizing = true;
  138. } else {
  139. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  140. _adjust_graph_scroll(click_position);
  141. }
  142. } else {
  143. is_pressing = false;
  144. is_resizing = false;
  145. }
  146. accept_event();
  147. } else if (mm.is_valid() && is_pressing) {
  148. if (is_resizing) {
  149. // Prevent setting minimap wider than GraphEdit
  150. Vector2 new_minimap_size;
  151. new_minimap_size.x = MIN(get_size().x - mm->get_relative().x, ge->get_size().x - 2.0 * minimap_padding.x);
  152. new_minimap_size.y = MIN(get_size().y - mm->get_relative().y, ge->get_size().y - 2.0 * minimap_padding.y);
  153. ge->set_minimap_size(new_minimap_size);
  154. queue_redraw();
  155. } else {
  156. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  157. _adjust_graph_scroll(click_position);
  158. }
  159. accept_event();
  160. }
  161. }
  162. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  163. Vector2 graph_offset = _get_graph_offset();
  164. ge->set_scroll_ofs(p_offset + graph_offset - camera_size / 2);
  165. }
  166. Control::CursorShape GraphEdit::get_cursor_shape(const Point2 &p_pos) const {
  167. if (moving_selection) {
  168. return CURSOR_MOVE;
  169. }
  170. return Control::get_cursor_shape(p_pos);
  171. }
  172. PackedStringArray GraphEdit::get_configuration_warnings() const {
  173. PackedStringArray warnings = Control::get_configuration_warnings();
  174. 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."));
  175. return warnings;
  176. }
  177. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  178. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  179. return OK;
  180. }
  181. Connection c;
  182. c.from = p_from;
  183. c.from_port = p_from_port;
  184. c.to = p_to;
  185. c.to_port = p_to_port;
  186. c.activity = 0;
  187. connections.push_back(c);
  188. top_layer->queue_redraw();
  189. minimap->queue_redraw();
  190. queue_redraw();
  191. connections_layer->queue_redraw();
  192. return OK;
  193. }
  194. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  195. for (const Connection &E : connections) {
  196. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  203. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  204. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  205. connections.erase(E);
  206. top_layer->queue_redraw();
  207. minimap->queue_redraw();
  208. queue_redraw();
  209. connections_layer->queue_redraw();
  210. return;
  211. }
  212. }
  213. }
  214. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  215. *r_connections = connections;
  216. }
  217. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  218. setting_scroll_ofs = true;
  219. h_scroll->set_value(p_ofs.x);
  220. v_scroll->set_value(p_ofs.y);
  221. _update_scroll();
  222. setting_scroll_ofs = false;
  223. }
  224. Vector2 GraphEdit::get_scroll_ofs() const {
  225. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  226. }
  227. void GraphEdit::_scroll_moved(double) {
  228. if (!awaiting_scroll_offset_update) {
  229. call_deferred(SNAME("_update_scroll_offset"));
  230. awaiting_scroll_offset_update = true;
  231. }
  232. top_layer->queue_redraw();
  233. minimap->queue_redraw();
  234. queue_redraw();
  235. }
  236. void GraphEdit::_update_scroll_offset() {
  237. set_block_minimum_size_adjust(true);
  238. for (int i = 0; i < get_child_count(); i++) {
  239. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  240. if (!gn) {
  241. continue;
  242. }
  243. Point2 pos = gn->get_position_offset() * zoom;
  244. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  245. gn->set_position(pos);
  246. if (gn->get_scale() != Vector2(zoom, zoom)) {
  247. gn->set_scale(Vector2(zoom, zoom));
  248. }
  249. }
  250. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  251. set_block_minimum_size_adjust(false);
  252. awaiting_scroll_offset_update = false;
  253. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  254. emit_signal(SNAME("scroll_offset_changed"), get_scroll_ofs());
  255. }
  256. }
  257. void GraphEdit::_update_scroll() {
  258. if (updating) {
  259. return;
  260. }
  261. updating = true;
  262. set_block_minimum_size_adjust(true);
  263. Rect2 screen;
  264. for (int i = 0; i < get_child_count(); i++) {
  265. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  266. if (!gn) {
  267. continue;
  268. }
  269. Rect2 r;
  270. r.position = gn->get_position_offset() * zoom;
  271. r.size = gn->get_size() * zoom;
  272. screen = screen.merge(r);
  273. }
  274. screen.position -= get_size();
  275. screen.size += get_size() * 2.0;
  276. h_scroll->set_min(screen.position.x);
  277. h_scroll->set_max(screen.position.x + screen.size.x);
  278. h_scroll->set_page(get_size().x);
  279. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  280. h_scroll->hide();
  281. } else {
  282. h_scroll->show();
  283. }
  284. v_scroll->set_min(screen.position.y);
  285. v_scroll->set_max(screen.position.y + screen.size.y);
  286. v_scroll->set_page(get_size().y);
  287. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  288. v_scroll->hide();
  289. } else {
  290. v_scroll->show();
  291. }
  292. Size2 hmin = h_scroll->get_combined_minimum_size();
  293. Size2 vmin = v_scroll->get_combined_minimum_size();
  294. // Avoid scrollbar overlapping.
  295. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  296. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  297. set_block_minimum_size_adjust(false);
  298. if (!awaiting_scroll_offset_update) {
  299. call_deferred(SNAME("_update_scroll_offset"));
  300. awaiting_scroll_offset_update = true;
  301. }
  302. updating = false;
  303. }
  304. void GraphEdit::_graph_node_raised(Node *p_gn) {
  305. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  306. ERR_FAIL_NULL(gn);
  307. if (gn->is_comment()) {
  308. move_child(gn, 0);
  309. } else {
  310. gn->move_to_front();
  311. }
  312. }
  313. void GraphEdit::_graph_node_selected(Node *p_gn) {
  314. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  315. ERR_FAIL_NULL(gn);
  316. emit_signal(SNAME("node_selected"), gn);
  317. }
  318. void GraphEdit::_graph_node_deselected(Node *p_gn) {
  319. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  320. ERR_FAIL_NULL(gn);
  321. emit_signal(SNAME("node_deselected"), gn);
  322. }
  323. void GraphEdit::_graph_node_moved(Node *p_gn) {
  324. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  325. ERR_FAIL_NULL(gn);
  326. top_layer->queue_redraw();
  327. minimap->queue_redraw();
  328. queue_redraw();
  329. connections_layer->queue_redraw();
  330. }
  331. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_gn) {
  332. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  333. ERR_FAIL_NULL(gn);
  334. top_layer->queue_redraw();
  335. minimap->queue_redraw();
  336. queue_redraw();
  337. connections_layer->queue_redraw();
  338. }
  339. void GraphEdit::add_child_notify(Node *p_child) {
  340. Control::add_child_notify(p_child);
  341. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  342. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  343. if (gn) {
  344. gn->set_scale(Vector2(zoom, zoom));
  345. gn->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved).bind(gn));
  346. gn->connect("node_selected", callable_mp(this, &GraphEdit::_graph_node_selected).bind(gn));
  347. gn->connect("node_deselected", callable_mp(this, &GraphEdit::_graph_node_deselected).bind(gn));
  348. gn->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(gn));
  349. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised).bind(gn));
  350. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  351. gn->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  352. _graph_node_moved(gn);
  353. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  354. }
  355. }
  356. void GraphEdit::remove_child_notify(Node *p_child) {
  357. Control::remove_child_notify(p_child);
  358. if (p_child == top_layer) {
  359. top_layer = nullptr;
  360. minimap = nullptr;
  361. } else if (p_child == connections_layer) {
  362. connections_layer = nullptr;
  363. }
  364. if (top_layer != nullptr && is_inside_tree()) {
  365. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  366. }
  367. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  368. if (gn) {
  369. gn->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  370. gn->disconnect("node_selected", callable_mp(this, &GraphEdit::_graph_node_selected));
  371. gn->disconnect("node_deselected", callable_mp(this, &GraphEdit::_graph_node_deselected));
  372. gn->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  373. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  374. // In case of the whole GraphEdit being destroyed these references can already be freed.
  375. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  376. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  377. }
  378. if (minimap != nullptr && minimap->is_inside_tree()) {
  379. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  380. }
  381. }
  382. }
  383. void GraphEdit::_notification(int p_what) {
  384. switch (p_what) {
  385. case NOTIFICATION_ENTER_TREE:
  386. case NOTIFICATION_THEME_CHANGED: {
  387. port_hotzone_inner_extent = get_theme_constant("port_hotzone_inner_extent");
  388. port_hotzone_outer_extent = get_theme_constant("port_hotzone_outer_extent");
  389. zoom_minus->set_icon(get_theme_icon(SNAME("minus")));
  390. zoom_reset->set_icon(get_theme_icon(SNAME("reset")));
  391. zoom_plus->set_icon(get_theme_icon(SNAME("more")));
  392. snap_button->set_icon(get_theme_icon(SNAME("snap")));
  393. minimap_button->set_icon(get_theme_icon(SNAME("minimap")));
  394. layout_button->set_icon(get_theme_icon(SNAME("layout")));
  395. zoom_label->set_custom_minimum_size(Size2(48, 0) * get_theme_default_base_scale());
  396. } break;
  397. case NOTIFICATION_READY: {
  398. Size2 hmin = h_scroll->get_combined_minimum_size();
  399. Size2 vmin = v_scroll->get_combined_minimum_size();
  400. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  401. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  402. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  403. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  404. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  405. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  406. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  407. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  408. } break;
  409. case NOTIFICATION_DRAW: {
  410. draw_style_box(get_theme_stylebox(SNAME("bg")), Rect2(Point2(), get_size()));
  411. if (is_using_snap()) {
  412. // Draw grid.
  413. int snap = get_snap();
  414. Vector2 offset = get_scroll_ofs() / zoom;
  415. Size2 size = get_size() / zoom;
  416. Point2i from = (offset / float(snap)).floor();
  417. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  418. Color grid_minor = get_theme_color(SNAME("grid_minor"));
  419. Color grid_major = get_theme_color(SNAME("grid_major"));
  420. for (int i = from.x; i < from.x + len.x; i++) {
  421. Color color;
  422. if (ABS(i) % 10 == 0) {
  423. color = grid_major;
  424. } else {
  425. color = grid_minor;
  426. }
  427. float base_ofs = i * snap * zoom - offset.x * zoom;
  428. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  429. }
  430. for (int i = from.y; i < from.y + len.y; i++) {
  431. Color color;
  432. if (ABS(i) % 10 == 0) {
  433. color = grid_major;
  434. } else {
  435. color = grid_minor;
  436. }
  437. float base_ofs = i * snap * zoom - offset.y * zoom;
  438. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  439. }
  440. }
  441. } break;
  442. case NOTIFICATION_RESIZED: {
  443. _update_scroll();
  444. top_layer->queue_redraw();
  445. minimap->queue_redraw();
  446. } break;
  447. }
  448. }
  449. void GraphEdit::_update_comment_enclosed_nodes_list(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes) {
  450. Rect2 comment_node_rect = p_node->get_rect();
  451. Vector<GraphNode *> enclosed_nodes;
  452. for (int i = 0; i < get_child_count(); i++) {
  453. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  454. if (!gn || gn->is_selected()) {
  455. continue;
  456. }
  457. Rect2 node_rect = gn->get_rect();
  458. bool included = comment_node_rect.encloses(node_rect);
  459. if (included) {
  460. enclosed_nodes.push_back(gn);
  461. }
  462. }
  463. p_comment_enclosed_nodes.insert(p_node->get_name(), enclosed_nodes);
  464. }
  465. void GraphEdit::_set_drag_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, bool p_drag) {
  466. for (int i = 0; i < p_comment_enclosed_nodes[p_node->get_name()].size(); i++) {
  467. p_comment_enclosed_nodes[p_node->get_name()][i]->set_drag(p_drag);
  468. }
  469. }
  470. void GraphEdit::_set_position_of_comment_enclosed_nodes(GraphNode *p_node, HashMap<StringName, Vector<GraphNode *>> &p_comment_enclosed_nodes, Vector2 p_drag_accum) {
  471. for (int i = 0; i < p_comment_enclosed_nodes[p_node->get_name()].size(); i++) {
  472. Vector2 pos = (p_comment_enclosed_nodes[p_node->get_name()][i]->get_drag_from() * zoom + drag_accum) / zoom;
  473. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  474. const int snap = get_snap();
  475. pos = pos.snapped(Vector2(snap, snap));
  476. }
  477. p_comment_enclosed_nodes[p_node->get_name()][i]->set_position_offset(pos);
  478. }
  479. }
  480. bool GraphEdit::_filter_input(const Point2 &p_point) {
  481. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  482. for (int i = get_child_count() - 1; i >= 0; i--) {
  483. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  484. if (!gn) {
  485. continue;
  486. }
  487. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  488. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  489. port_size.height = MAX(port_size.height, gn->get_connection_input_height(j));
  490. if (is_in_input_hotzone(gn, j, p_point / zoom, port_size)) {
  491. return true;
  492. }
  493. }
  494. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  495. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  496. port_size.height = MAX(port_size.height, gn->get_connection_output_height(j));
  497. if (is_in_output_hotzone(gn, j, p_point / zoom, port_size)) {
  498. return true;
  499. }
  500. }
  501. }
  502. return false;
  503. }
  504. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  505. Ref<InputEventMouseButton> mb = p_ev;
  506. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  507. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  508. connecting_valid = false;
  509. click_pos = mb->get_position() / zoom;
  510. for (int i = get_child_count() - 1; i >= 0; i--) {
  511. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  512. if (!gn) {
  513. continue;
  514. }
  515. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  516. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  517. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  518. port_size.height = MAX(port_size.height, gn->get_connection_output_height(j));
  519. if (is_in_output_hotzone(gn, j, click_pos, port_size)) {
  520. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  521. //check disconnect
  522. for (const Connection &E : connections) {
  523. if (E.from == gn->get_name() && E.from_port == j) {
  524. Node *to = get_node(NodePath(E.to));
  525. if (Object::cast_to<GraphNode>(to)) {
  526. connecting_from = E.to;
  527. connecting_index = E.to_port;
  528. connecting_out = false;
  529. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E.to_port);
  530. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E.to_port);
  531. connecting_target = false;
  532. connecting_to = pos;
  533. if (connecting_type >= 0) {
  534. just_disconnected = true;
  535. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  536. to = get_node(NodePath(connecting_from)); // Maybe it was erased.
  537. if (Object::cast_to<GraphNode>(to)) {
  538. connecting = true;
  539. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  540. }
  541. }
  542. return;
  543. }
  544. }
  545. }
  546. }
  547. connecting_from = gn->get_name();
  548. connecting_index = j;
  549. connecting_out = true;
  550. connecting_type = gn->get_connection_output_type(j);
  551. connecting_color = gn->get_connection_output_color(j);
  552. connecting_target = false;
  553. connecting_to = pos;
  554. if (connecting_type >= 0) {
  555. connecting = true;
  556. just_disconnected = false;
  557. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  558. }
  559. return;
  560. }
  561. }
  562. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  563. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  564. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  565. port_size.height = MAX(port_size.height, gn->get_connection_input_height(j));
  566. if (is_in_input_hotzone(gn, j, click_pos, port_size)) {
  567. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  568. // Check disconnect.
  569. for (const Connection &E : connections) {
  570. if (E.to == gn->get_name() && E.to_port == j) {
  571. Node *fr = get_node(NodePath(E.from));
  572. if (Object::cast_to<GraphNode>(fr)) {
  573. connecting_from = E.from;
  574. connecting_index = E.from_port;
  575. connecting_out = true;
  576. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E.from_port);
  577. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E.from_port);
  578. connecting_target = false;
  579. connecting_to = pos;
  580. just_disconnected = true;
  581. if (connecting_type >= 0) {
  582. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  583. fr = get_node(NodePath(connecting_from)); // Maybe it was erased.
  584. if (Object::cast_to<GraphNode>(fr)) {
  585. connecting = true;
  586. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  587. }
  588. }
  589. return;
  590. }
  591. }
  592. }
  593. }
  594. connecting_from = gn->get_name();
  595. connecting_index = j;
  596. connecting_out = false;
  597. connecting_type = gn->get_connection_input_type(j);
  598. connecting_color = gn->get_connection_input_color(j);
  599. connecting_target = false;
  600. connecting_to = pos;
  601. if (connecting_type >= 0) {
  602. connecting = true;
  603. just_disconnected = false;
  604. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  605. }
  606. return;
  607. }
  608. }
  609. }
  610. }
  611. Ref<InputEventMouseMotion> mm = p_ev;
  612. if (mm.is_valid() && connecting) {
  613. connecting_to = mm->get_position();
  614. connecting_target = false;
  615. top_layer->queue_redraw();
  616. minimap->queue_redraw();
  617. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > 20.0;
  618. if (connecting_valid) {
  619. Vector2 mpos = mm->get_position() / zoom;
  620. for (int i = get_child_count() - 1; i >= 0; i--) {
  621. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  622. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  623. if (!gn) {
  624. continue;
  625. }
  626. if (!connecting_out) {
  627. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  628. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  629. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  630. port_size.height = MAX(port_size.height, gn->get_connection_output_height(j));
  631. int type = gn->get_connection_output_type(j);
  632. if ((type == connecting_type || valid_connection_types.has(ConnType(connecting_type, type))) && is_in_output_hotzone(gn, j, mpos, port_size)) {
  633. if (!is_node_hover_valid(gn->get_name(), j, connecting_from, connecting_index)) {
  634. continue;
  635. }
  636. connecting_target = true;
  637. connecting_to = pos;
  638. connecting_target_to = gn->get_name();
  639. connecting_target_index = j;
  640. return;
  641. }
  642. }
  643. } else {
  644. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  645. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  646. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  647. port_size.height = MAX(port_size.height, gn->get_connection_input_height(j));
  648. int type = gn->get_connection_input_type(j);
  649. if ((type == connecting_type || valid_connection_types.has(ConnType(connecting_type, type))) && is_in_input_hotzone(gn, j, mpos, port_size)) {
  650. if (!is_node_hover_valid(connecting_from, connecting_index, gn->get_name(), j)) {
  651. continue;
  652. }
  653. connecting_target = true;
  654. connecting_to = pos;
  655. connecting_target_to = gn->get_name();
  656. connecting_target_index = j;
  657. return;
  658. }
  659. }
  660. }
  661. }
  662. }
  663. }
  664. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  665. if (connecting_valid) {
  666. if (connecting && connecting_target) {
  667. if (connecting_out) {
  668. emit_signal(SNAME("connection_request"), connecting_from, connecting_index, connecting_target_to, connecting_target_index);
  669. } else {
  670. emit_signal(SNAME("connection_request"), connecting_target_to, connecting_target_index, connecting_from, connecting_index);
  671. }
  672. } else if (!just_disconnected) {
  673. if (connecting_out) {
  674. emit_signal(SNAME("connection_to_empty"), connecting_from, connecting_index, mb->get_position());
  675. } else {
  676. emit_signal(SNAME("connection_from_empty"), connecting_from, connecting_index, mb->get_position());
  677. }
  678. }
  679. }
  680. if (connecting) {
  681. force_connection_drag_end();
  682. }
  683. }
  684. }
  685. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) {
  686. if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) {
  687. return false;
  688. }
  689. Rect2 control_rect = p_control->get_rect();
  690. control_rect.position *= zoom;
  691. control_rect.size *= zoom;
  692. control_rect.position += p_offset;
  693. if (!control_rect.has_point(mpos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  694. // Test children.
  695. for (int i = 0; i < p_control->get_child_count(); i++) {
  696. Control *child_rect = Object::cast_to<Control>(p_control->get_child(i));
  697. if (!child_rect) {
  698. continue;
  699. }
  700. if (_check_clickable_control(child_rect, mpos, control_rect.position)) {
  701. return true;
  702. }
  703. }
  704. return false;
  705. } else {
  706. return true;
  707. }
  708. }
  709. bool GraphEdit::is_in_input_hotzone(GraphNode *p_node, int p_port, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  710. bool success;
  711. if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_node, p_port, p_mouse_pos, success)) {
  712. return success;
  713. } else {
  714. Vector2 pos = p_node->get_connection_input_position(p_port) + p_node->get_position();
  715. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true);
  716. }
  717. }
  718. bool GraphEdit::is_in_output_hotzone(GraphNode *p_node, int p_port, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  719. if (p_node->is_resizable()) {
  720. Ref<Texture2D> resizer = p_node->get_theme_icon(SNAME("resizer"));
  721. Rect2 resizer_rect = Rect2(p_node->get_position() / zoom + p_node->get_size() - resizer->get_size(), resizer->get_size());
  722. if (resizer_rect.has_point(p_mouse_pos)) {
  723. return false;
  724. }
  725. }
  726. bool success;
  727. if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_node, p_port, p_mouse_pos, success)) {
  728. return success;
  729. } else {
  730. Vector2 pos = p_node->get_connection_output_position(p_port) + p_node->get_position();
  731. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false);
  732. }
  733. }
  734. bool GraphEdit::is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  735. Rect2 hotzone = Rect2(
  736. p_pos.x - (p_left ? port_hotzone_outer_extent : port_hotzone_inner_extent),
  737. p_pos.y - p_port_size.height / 2.0,
  738. port_hotzone_inner_extent + port_hotzone_outer_extent,
  739. p_port_size.height);
  740. if (!hotzone.has_point(p_mouse_pos)) {
  741. return false;
  742. }
  743. for (int i = 0; i < get_child_count(); i++) {
  744. GraphNode *child = Object::cast_to<GraphNode>(get_child(i));
  745. if (!child) {
  746. continue;
  747. }
  748. Rect2 child_rect = child->get_rect();
  749. if (child_rect.has_point(p_mouse_pos * zoom)) {
  750. for (int j = 0; j < child->get_child_count(); j++) {
  751. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  752. if (!subchild) {
  753. continue;
  754. }
  755. if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) {
  756. return false;
  757. }
  758. }
  759. }
  760. }
  761. return true;
  762. }
  763. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) {
  764. Vector<Vector2> ret;
  765. if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) {
  766. return ret;
  767. }
  768. float x_diff = (p_to.x - p_from.x);
  769. float cp_offset = x_diff * lines_curvature;
  770. if (x_diff < 0) {
  771. cp_offset *= -1;
  772. }
  773. Curve2D curve;
  774. curve.add_point(p_from);
  775. curve.set_point_out(0, Vector2(cp_offset, 0));
  776. curve.add_point(p_to);
  777. curve.set_point_in(1, Vector2(-cp_offset, 0));
  778. if (lines_curvature > 0) {
  779. return curve.tessellate(5, 2.0);
  780. } else {
  781. return curve.tessellate(1);
  782. }
  783. }
  784. void GraphEdit::_draw_connection_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color, float p_width, float p_zoom) {
  785. Vector<Vector2> points = get_connection_line(p_from / p_zoom, p_to / p_zoom);
  786. Vector<Vector2> scaled_points;
  787. Vector<Color> colors;
  788. float length = (p_from / p_zoom).distance_to(p_to / p_zoom);
  789. for (int i = 0; i < points.size(); i++) {
  790. float d = (p_from / p_zoom).distance_to(points[i]) / length;
  791. colors.push_back(p_color.lerp(p_to_color, d));
  792. scaled_points.push_back(points[i] * p_zoom);
  793. }
  794. // Thickness below 0.5 doesn't look good on the graph or its minimap.
  795. p_where->draw_polyline_colors(scaled_points, colors, MAX(0.5, Math::floor(p_width * get_theme_default_base_scale())), lines_antialiased);
  796. }
  797. void GraphEdit::_connections_layer_draw() {
  798. Color activity_color = get_theme_color(SNAME("activity"));
  799. // Draw connections.
  800. List<List<Connection>::Element *> to_erase;
  801. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  802. const Connection &c = E->get();
  803. Node *from = get_node(NodePath(c.from));
  804. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  805. if (!gfrom) {
  806. to_erase.push_back(E);
  807. continue;
  808. }
  809. Node *to = get_node(NodePath(c.to));
  810. GraphNode *gto = Object::cast_to<GraphNode>(to);
  811. if (!gto) {
  812. to_erase.push_back(E);
  813. continue;
  814. }
  815. Vector2 frompos = gfrom->get_connection_output_position(c.from_port) + gfrom->get_position_offset() * zoom;
  816. Color color = gfrom->get_connection_output_color(c.from_port);
  817. Vector2 topos = gto->get_connection_input_position(c.to_port) + gto->get_position_offset() * zoom;
  818. Color tocolor = gto->get_connection_input_color(c.to_port);
  819. if (c.activity > 0) {
  820. color = color.lerp(activity_color, c.activity);
  821. tocolor = tocolor.lerp(activity_color, c.activity);
  822. }
  823. _draw_connection_line(connections_layer, frompos, topos, color, tocolor, lines_thickness, zoom);
  824. }
  825. for (List<Connection>::Element *&E : to_erase) {
  826. connections.erase(E);
  827. }
  828. }
  829. void GraphEdit::_top_layer_draw() {
  830. _update_scroll();
  831. if (connecting) {
  832. Node *fromn = get_node_or_null(NodePath(connecting_from));
  833. ERR_FAIL_NULL(fromn);
  834. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  835. ERR_FAIL_NULL(from);
  836. Vector2 pos;
  837. if (connecting_out) {
  838. pos = from->get_connection_output_position(connecting_index);
  839. } else {
  840. pos = from->get_connection_input_position(connecting_index);
  841. }
  842. pos += from->get_position();
  843. Vector2 topos;
  844. topos = connecting_to;
  845. Color col = connecting_color;
  846. if (connecting_target) {
  847. col.r += 0.4;
  848. col.g += 0.4;
  849. col.b += 0.4;
  850. }
  851. if (!connecting_out) {
  852. SWAP(pos, topos);
  853. }
  854. _draw_connection_line(top_layer, pos, topos, col, col, lines_thickness, zoom);
  855. }
  856. if (box_selecting) {
  857. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_fill")));
  858. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_stroke")), false);
  859. }
  860. }
  861. void GraphEdit::_minimap_draw() {
  862. if (!is_minimap_enabled()) {
  863. return;
  864. }
  865. minimap->update_minimap();
  866. // Draw the minimap background.
  867. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  868. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("bg")), minimap_rect);
  869. Vector2 graph_offset = minimap->_get_graph_offset();
  870. Vector2 minimap_offset = minimap->minimap_offset;
  871. // Draw comment graph nodes.
  872. for (int i = get_child_count() - 1; i >= 0; i--) {
  873. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  874. if (!gn || !gn->is_comment()) {
  875. continue;
  876. }
  877. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  878. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  879. Rect2 node_rect = Rect2(node_position, node_size);
  880. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  881. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  882. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "comment_focus" : "comment");
  883. if (sbf.is_valid()) {
  884. Color node_color = sbf->get_bg_color();
  885. sb_minimap->set_bg_color(node_color);
  886. }
  887. minimap->draw_style_box(sb_minimap, node_rect);
  888. }
  889. // Draw regular graph nodes.
  890. for (int i = get_child_count() - 1; i >= 0; i--) {
  891. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  892. if (!gn || gn->is_comment()) {
  893. continue;
  894. }
  895. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  896. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  897. Rect2 node_rect = Rect2(node_position, node_size);
  898. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  899. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  900. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "selected_frame" : "frame");
  901. if (sbf.is_valid()) {
  902. Color node_color = sbf->get_border_color();
  903. sb_minimap->set_bg_color(node_color);
  904. }
  905. minimap->draw_style_box(sb_minimap, node_rect);
  906. }
  907. // Draw node connections.
  908. Color activity_color = get_theme_color(SNAME("activity"));
  909. for (const Connection &E : connections) {
  910. Node *from = get_node(NodePath(E.from));
  911. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  912. if (!gfrom) {
  913. continue;
  914. }
  915. Node *to = get_node(NodePath(E.to));
  916. GraphNode *gto = Object::cast_to<GraphNode>(to);
  917. if (!gto) {
  918. continue;
  919. }
  920. Vector2 from_port_position = gfrom->get_position_offset() * zoom + gfrom->get_connection_output_position(E.from_port);
  921. Vector2 from_position = minimap->_convert_from_graph_position(from_port_position - graph_offset) + minimap_offset;
  922. Color from_color = gfrom->get_connection_output_color(E.from_port);
  923. Vector2 to_port_position = gto->get_position_offset() * zoom + gto->get_connection_input_position(E.to_port);
  924. Vector2 to_position = minimap->_convert_from_graph_position(to_port_position - graph_offset) + minimap_offset;
  925. Color to_color = gto->get_connection_input_color(E.to_port);
  926. if (E.activity > 0) {
  927. from_color = from_color.lerp(activity_color, E.activity);
  928. to_color = to_color.lerp(activity_color, E.activity);
  929. }
  930. _draw_connection_line(minimap, from_position, to_position, from_color, to_color, 0.5, minimap->_convert_from_graph_position(Vector2(zoom, zoom)).length());
  931. }
  932. // Draw the "camera" viewport.
  933. Rect2 camera_rect = minimap->get_camera_rect();
  934. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("camera")), camera_rect);
  935. // Draw the resizer control.
  936. Ref<Texture2D> resizer = minimap->get_theme_icon(SNAME("resizer"));
  937. Color resizer_color = minimap->get_theme_color(SNAME("resizer_color"));
  938. minimap->draw_texture(resizer, Point2(), resizer_color);
  939. }
  940. void GraphEdit::set_selected(Node *p_child) {
  941. for (int i = get_child_count() - 1; i >= 0; i--) {
  942. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  943. if (!gn) {
  944. continue;
  945. }
  946. gn->set_selected(gn == p_child);
  947. }
  948. }
  949. void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
  950. ERR_FAIL_COND(p_ev.is_null());
  951. if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) {
  952. return;
  953. }
  954. Ref<InputEventMouseMotion> mm = p_ev;
  955. if (mm.is_valid() && dragging) {
  956. if (!moving_selection) {
  957. emit_signal(SNAME("begin_node_move"));
  958. moving_selection = true;
  959. }
  960. just_selected = true;
  961. drag_accum += mm->get_relative();
  962. for (int i = get_child_count() - 1; i >= 0; i--) {
  963. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  964. if (gn && gn->is_selected() && gn->is_draggable()) {
  965. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  966. // Snapping can be toggled temporarily by holding down Ctrl.
  967. // This is done here as to not toggle the grid when holding down Ctrl.
  968. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  969. const int snap = get_snap();
  970. pos = pos.snapped(Vector2(snap, snap));
  971. }
  972. gn->set_position_offset(pos);
  973. if (gn->is_comment()) {
  974. _set_position_of_comment_enclosed_nodes(gn, comment_enclosed_nodes, drag_accum);
  975. }
  976. }
  977. }
  978. }
  979. if (mm.is_valid() && box_selecting) {
  980. box_selecting_to = mm->get_position();
  981. box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());
  982. for (int i = get_child_count() - 1; i >= 0; i--) {
  983. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  984. if (!gn) {
  985. continue;
  986. }
  987. Rect2 r = gn->get_rect();
  988. bool in_box = r.intersects(box_selecting_rect);
  989. if (in_box) {
  990. gn->set_selected(box_selection_mode_additive);
  991. } else {
  992. gn->set_selected(previous_selected.find(gn) != nullptr);
  993. }
  994. }
  995. top_layer->queue_redraw();
  996. minimap->queue_redraw();
  997. }
  998. Ref<InputEventMouseButton> mb = p_ev;
  999. if (mb.is_valid()) {
  1000. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  1001. if (box_selecting) {
  1002. box_selecting = false;
  1003. for (int i = get_child_count() - 1; i >= 0; i--) {
  1004. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1005. if (!gn) {
  1006. continue;
  1007. }
  1008. gn->set_selected(previous_selected.find(gn) != nullptr);
  1009. }
  1010. top_layer->queue_redraw();
  1011. minimap->queue_redraw();
  1012. } else {
  1013. if (connecting) {
  1014. force_connection_drag_end();
  1015. } else {
  1016. emit_signal(SNAME("popup_request"), mb->get_position());
  1017. }
  1018. }
  1019. }
  1020. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && dragging) {
  1021. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1022. //deselect current node
  1023. for (int i = get_child_count() - 1; i >= 0; i--) {
  1024. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1025. if (gn) {
  1026. Rect2 r = gn->get_rect();
  1027. if (r.has_point(mb->get_position())) {
  1028. gn->set_selected(false);
  1029. }
  1030. }
  1031. }
  1032. }
  1033. if (drag_accum != Vector2()) {
  1034. for (int i = get_child_count() - 1; i >= 0; i--) {
  1035. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1036. if (gn && gn->is_selected()) {
  1037. gn->set_drag(false);
  1038. if (gn->is_comment()) {
  1039. _set_drag_comment_enclosed_nodes(gn, comment_enclosed_nodes, false);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. if (moving_selection) {
  1045. emit_signal(SNAME("end_node_move"));
  1046. moving_selection = false;
  1047. }
  1048. dragging = false;
  1049. top_layer->queue_redraw();
  1050. minimap->queue_redraw();
  1051. queue_redraw();
  1052. connections_layer->queue_redraw();
  1053. }
  1054. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  1055. GraphNode *gn = nullptr;
  1056. // Find node which was clicked on.
  1057. for (int i = get_child_count() - 1; i >= 0; i--) {
  1058. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  1059. if (!gn_selected) {
  1060. continue;
  1061. }
  1062. if (gn_selected->is_resizing()) {
  1063. continue;
  1064. }
  1065. if (gn_selected->has_point((mb->get_position() - gn_selected->get_position()) / zoom)) {
  1066. gn = gn_selected;
  1067. break;
  1068. }
  1069. }
  1070. if (gn) {
  1071. if (_filter_input(mb->get_position())) {
  1072. return;
  1073. }
  1074. // Left-clicked on a node, select it.
  1075. dragging = true;
  1076. drag_accum = Vector2();
  1077. just_selected = !gn->is_selected();
  1078. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1079. for (int i = 0; i < get_child_count(); i++) {
  1080. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1081. if (!o_gn) {
  1082. continue;
  1083. }
  1084. o_gn->set_selected(o_gn == gn);
  1085. }
  1086. }
  1087. gn->set_selected(true);
  1088. for (int i = 0; i < get_child_count(); i++) {
  1089. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1090. if (!o_gn) {
  1091. continue;
  1092. }
  1093. if (o_gn->is_selected()) {
  1094. o_gn->set_drag(true);
  1095. if (o_gn->is_comment()) {
  1096. _update_comment_enclosed_nodes_list(o_gn, comment_enclosed_nodes);
  1097. _set_drag_comment_enclosed_nodes(o_gn, comment_enclosed_nodes, true);
  1098. }
  1099. }
  1100. }
  1101. } else {
  1102. if (_filter_input(mb->get_position())) {
  1103. return;
  1104. }
  1105. if (panner->is_panning()) {
  1106. return;
  1107. }
  1108. // Left-clicked on empty space, start box select.
  1109. box_selecting = true;
  1110. box_selecting_from = mb->get_position();
  1111. if (mb->is_ctrl_pressed()) {
  1112. box_selection_mode_additive = true;
  1113. previous_selected.clear();
  1114. for (int i = get_child_count() - 1; i >= 0; i--) {
  1115. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1116. if (!gn2 || !gn2->is_selected()) {
  1117. continue;
  1118. }
  1119. previous_selected.push_back(gn2);
  1120. }
  1121. } else if (mb->is_shift_pressed()) {
  1122. box_selection_mode_additive = false;
  1123. previous_selected.clear();
  1124. for (int i = get_child_count() - 1; i >= 0; i--) {
  1125. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1126. if (!gn2 || !gn2->is_selected()) {
  1127. continue;
  1128. }
  1129. previous_selected.push_back(gn2);
  1130. }
  1131. } else {
  1132. box_selection_mode_additive = true;
  1133. previous_selected.clear();
  1134. for (int i = get_child_count() - 1; i >= 0; i--) {
  1135. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1136. if (!gn2) {
  1137. continue;
  1138. }
  1139. gn2->set_selected(false);
  1140. }
  1141. }
  1142. }
  1143. }
  1144. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
  1145. // Box selection ended. Nodes were selected during mouse movement.
  1146. box_selecting = false;
  1147. box_selecting_rect = Rect2();
  1148. previous_selected.clear();
  1149. top_layer->queue_redraw();
  1150. minimap->queue_redraw();
  1151. }
  1152. }
  1153. if (p_ev->is_pressed()) {
  1154. if (p_ev->is_action("ui_graph_duplicate", true)) {
  1155. emit_signal(SNAME("duplicate_nodes_request"));
  1156. accept_event();
  1157. } else if (p_ev->is_action("ui_copy", true)) {
  1158. emit_signal(SNAME("copy_nodes_request"));
  1159. accept_event();
  1160. } else if (p_ev->is_action("ui_paste", true)) {
  1161. emit_signal(SNAME("paste_nodes_request"));
  1162. accept_event();
  1163. } else if (p_ev->is_action("ui_graph_delete", true)) {
  1164. TypedArray<StringName> nodes;
  1165. for (int i = 0; i < get_child_count(); i++) {
  1166. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1167. if (!gn) {
  1168. continue;
  1169. }
  1170. if (gn->is_selected() && gn->is_close_button_visible()) {
  1171. nodes.push_back(gn->get_name());
  1172. }
  1173. }
  1174. emit_signal(SNAME("delete_nodes_request"), nodes);
  1175. accept_event();
  1176. }
  1177. }
  1178. }
  1179. void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1180. h_scroll->set_value(h_scroll->get_value() - p_scroll_vec.x);
  1181. v_scroll->set_value(v_scroll->get_value() - p_scroll_vec.y);
  1182. }
  1183. void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1184. set_zoom_custom(zoom * p_zoom_factor, p_origin);
  1185. }
  1186. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1187. for (Connection &E : connections) {
  1188. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  1189. if (Math::is_equal_approx(E.activity, p_activity)) {
  1190. //update only if changed
  1191. top_layer->queue_redraw();
  1192. minimap->queue_redraw();
  1193. connections_layer->queue_redraw();
  1194. }
  1195. E.activity = p_activity;
  1196. return;
  1197. }
  1198. }
  1199. }
  1200. void GraphEdit::clear_connections() {
  1201. connections.clear();
  1202. minimap->queue_redraw();
  1203. queue_redraw();
  1204. connections_layer->queue_redraw();
  1205. }
  1206. void GraphEdit::force_connection_drag_end() {
  1207. ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
  1208. connecting = false;
  1209. connecting_valid = false;
  1210. top_layer->queue_redraw();
  1211. minimap->queue_redraw();
  1212. queue_redraw();
  1213. connections_layer->queue_redraw();
  1214. emit_signal(SNAME("connection_drag_ended"));
  1215. }
  1216. bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) {
  1217. bool valid = true;
  1218. GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid);
  1219. return valid;
  1220. }
  1221. void GraphEdit::set_panning_scheme(PanningScheme p_scheme) {
  1222. panning_scheme = p_scheme;
  1223. panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme);
  1224. }
  1225. GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const {
  1226. return panning_scheme;
  1227. }
  1228. void GraphEdit::set_zoom(float p_zoom) {
  1229. set_zoom_custom(p_zoom, get_size() / 2);
  1230. }
  1231. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1232. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1233. if (zoom == p_zoom) {
  1234. return;
  1235. }
  1236. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  1237. zoom = p_zoom;
  1238. top_layer->queue_redraw();
  1239. zoom_minus->set_disabled(zoom == zoom_min);
  1240. zoom_plus->set_disabled(zoom == zoom_max);
  1241. _update_scroll();
  1242. minimap->queue_redraw();
  1243. connections_layer->queue_redraw();
  1244. if (is_visible_in_tree()) {
  1245. Vector2 ofs = sbofs * zoom - p_center;
  1246. h_scroll->set_value(ofs.x);
  1247. v_scroll->set_value(ofs.y);
  1248. }
  1249. _update_zoom_label();
  1250. queue_redraw();
  1251. }
  1252. float GraphEdit::get_zoom() const {
  1253. return zoom;
  1254. }
  1255. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1256. p_zoom_step = abs(p_zoom_step);
  1257. ERR_FAIL_COND(!isfinite(p_zoom_step));
  1258. if (zoom_step == p_zoom_step) {
  1259. return;
  1260. }
  1261. zoom_step = p_zoom_step;
  1262. panner->set_scroll_zoom_factor(zoom_step);
  1263. }
  1264. float GraphEdit::get_zoom_step() const {
  1265. return zoom_step;
  1266. }
  1267. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1268. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1269. if (zoom_min == p_zoom_min) {
  1270. return;
  1271. }
  1272. zoom_min = p_zoom_min;
  1273. set_zoom(zoom);
  1274. }
  1275. float GraphEdit::get_zoom_min() const {
  1276. return zoom_min;
  1277. }
  1278. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1279. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1280. if (zoom_max == p_zoom_max) {
  1281. return;
  1282. }
  1283. zoom_max = p_zoom_max;
  1284. set_zoom(zoom);
  1285. }
  1286. float GraphEdit::get_zoom_max() const {
  1287. return zoom_max;
  1288. }
  1289. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1290. if (zoom_label->is_visible() == p_enable) {
  1291. return;
  1292. }
  1293. zoom_label->set_visible(p_enable);
  1294. }
  1295. bool GraphEdit::is_showing_zoom_label() const {
  1296. return zoom_label->is_visible();
  1297. }
  1298. void GraphEdit::set_right_disconnects(bool p_enable) {
  1299. right_disconnects = p_enable;
  1300. }
  1301. bool GraphEdit::is_right_disconnects_enabled() const {
  1302. return right_disconnects;
  1303. }
  1304. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1305. valid_right_disconnect_types.insert(p_type);
  1306. }
  1307. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1308. valid_right_disconnect_types.erase(p_type);
  1309. }
  1310. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1311. valid_left_disconnect_types.insert(p_type);
  1312. }
  1313. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1314. valid_left_disconnect_types.erase(p_type);
  1315. }
  1316. TypedArray<Dictionary> GraphEdit::_get_connection_list() const {
  1317. List<Connection> conns;
  1318. get_connection_list(&conns);
  1319. TypedArray<Dictionary> arr;
  1320. for (const Connection &E : conns) {
  1321. Dictionary d;
  1322. d["from"] = E.from;
  1323. d["from_port"] = E.from_port;
  1324. d["to"] = E.to;
  1325. d["to_port"] = E.to_port;
  1326. arr.push_back(d);
  1327. }
  1328. return arr;
  1329. }
  1330. void GraphEdit::_zoom_minus() {
  1331. set_zoom(zoom / zoom_step);
  1332. }
  1333. void GraphEdit::_zoom_reset() {
  1334. set_zoom(1);
  1335. }
  1336. void GraphEdit::_zoom_plus() {
  1337. set_zoom(zoom * zoom_step);
  1338. }
  1339. void GraphEdit::_update_zoom_label() {
  1340. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1341. String zoom_text = itos(zoom_percent) + "%";
  1342. zoom_label->set_text(zoom_text);
  1343. }
  1344. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1345. ConnType ct(p_type, p_with_type);
  1346. valid_connection_types.insert(ct);
  1347. }
  1348. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1349. ConnType ct(p_type, p_with_type);
  1350. valid_connection_types.erase(ct);
  1351. }
  1352. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1353. ConnType ct(p_type, p_with_type);
  1354. return valid_connection_types.has(ct);
  1355. }
  1356. void GraphEdit::set_use_snap(bool p_enable) {
  1357. if (snap_button->is_pressed() == p_enable) {
  1358. return;
  1359. }
  1360. snap_button->set_pressed(p_enable);
  1361. queue_redraw();
  1362. }
  1363. bool GraphEdit::is_using_snap() const {
  1364. return snap_button->is_pressed();
  1365. }
  1366. int GraphEdit::get_snap() const {
  1367. return snap_amount->get_value();
  1368. }
  1369. void GraphEdit::set_snap(int p_snap) {
  1370. ERR_FAIL_COND(p_snap < 5);
  1371. snap_amount->set_value(p_snap);
  1372. queue_redraw();
  1373. }
  1374. void GraphEdit::_snap_toggled() {
  1375. queue_redraw();
  1376. }
  1377. void GraphEdit::_snap_value_changed(double) {
  1378. queue_redraw();
  1379. }
  1380. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1381. minimap->set_size(p_size);
  1382. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1383. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1384. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1385. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1386. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1387. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1388. minimap->queue_redraw();
  1389. }
  1390. Vector2 GraphEdit::get_minimap_size() const {
  1391. return minimap->get_size();
  1392. }
  1393. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1394. if (minimap->get_modulate().a == p_opacity) {
  1395. return;
  1396. }
  1397. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1398. minimap->queue_redraw();
  1399. }
  1400. float GraphEdit::get_minimap_opacity() const {
  1401. Color minimap_modulate = minimap->get_modulate();
  1402. return minimap_modulate.a;
  1403. }
  1404. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1405. if (minimap_button->is_pressed() == p_enable) {
  1406. return;
  1407. }
  1408. minimap_button->set_pressed(p_enable);
  1409. _minimap_toggled();
  1410. minimap->queue_redraw();
  1411. }
  1412. bool GraphEdit::is_minimap_enabled() const {
  1413. return minimap_button->is_pressed();
  1414. }
  1415. void GraphEdit::set_arrange_nodes_button_hidden(bool p_enable) {
  1416. arrange_nodes_button_hidden = p_enable;
  1417. if (arrange_nodes_button_hidden) {
  1418. layout_button->hide();
  1419. } else {
  1420. layout_button->show();
  1421. }
  1422. }
  1423. bool GraphEdit::is_arrange_nodes_button_hidden() const {
  1424. return arrange_nodes_button_hidden;
  1425. }
  1426. void GraphEdit::_minimap_toggled() {
  1427. if (is_minimap_enabled()) {
  1428. minimap->set_visible(true);
  1429. minimap->queue_redraw();
  1430. } else {
  1431. minimap->set_visible(false);
  1432. }
  1433. }
  1434. void GraphEdit::set_connection_lines_curvature(float p_curvature) {
  1435. lines_curvature = p_curvature;
  1436. queue_redraw();
  1437. }
  1438. float GraphEdit::get_connection_lines_curvature() const {
  1439. return lines_curvature;
  1440. }
  1441. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  1442. if (lines_thickness == p_thickness) {
  1443. return;
  1444. }
  1445. lines_thickness = p_thickness;
  1446. queue_redraw();
  1447. }
  1448. float GraphEdit::get_connection_lines_thickness() const {
  1449. return lines_thickness;
  1450. }
  1451. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  1452. if (lines_antialiased == p_antialiased) {
  1453. return;
  1454. }
  1455. lines_antialiased = p_antialiased;
  1456. queue_redraw();
  1457. }
  1458. bool GraphEdit::is_connection_lines_antialiased() const {
  1459. return lines_antialiased;
  1460. }
  1461. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1462. return zoom_hb;
  1463. }
  1464. Ref<ViewPanner> GraphEdit::get_panner() {
  1465. return panner;
  1466. }
  1467. void GraphEdit::set_warped_panning(bool p_warped) {
  1468. warped_panning = p_warped;
  1469. }
  1470. int GraphEdit::_set_operations(SET_OPERATIONS p_operation, HashSet<StringName> &r_u, const HashSet<StringName> &r_v) {
  1471. switch (p_operation) {
  1472. case GraphEdit::IS_EQUAL: {
  1473. for (const StringName &E : r_u) {
  1474. if (!r_v.has(E)) {
  1475. return 0;
  1476. }
  1477. }
  1478. return r_u.size() == r_v.size();
  1479. } break;
  1480. case GraphEdit::IS_SUBSET: {
  1481. if (r_u.size() == r_v.size() && !r_u.size()) {
  1482. return 1;
  1483. }
  1484. for (const StringName &E : r_u) {
  1485. if (!r_v.has(E)) {
  1486. return 0;
  1487. }
  1488. }
  1489. return 1;
  1490. } break;
  1491. case GraphEdit::DIFFERENCE: {
  1492. for (HashSet<StringName>::Iterator E = r_u.begin(); E;) {
  1493. HashSet<StringName>::Iterator N = E;
  1494. ++N;
  1495. if (r_v.has(*E)) {
  1496. r_u.remove(E);
  1497. }
  1498. E = N;
  1499. }
  1500. return r_u.size();
  1501. } break;
  1502. case GraphEdit::UNION: {
  1503. for (const StringName &E : r_v) {
  1504. if (!r_u.has(E)) {
  1505. r_u.insert(E);
  1506. }
  1507. }
  1508. return r_u.size();
  1509. } break;
  1510. default:
  1511. break;
  1512. }
  1513. return -1;
  1514. }
  1515. HashMap<int, Vector<StringName>> GraphEdit::_layering(const HashSet<StringName> &r_selected_nodes, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours) {
  1516. HashMap<int, Vector<StringName>> l;
  1517. HashSet<StringName> p = r_selected_nodes, q = r_selected_nodes, u, z;
  1518. int current_layer = 0;
  1519. bool selected = false;
  1520. while (!_set_operations(GraphEdit::IS_EQUAL, q, u)) {
  1521. _set_operations(GraphEdit::DIFFERENCE, p, u);
  1522. for (const StringName &E : p) {
  1523. HashSet<StringName> n = r_upper_neighbours[E];
  1524. if (_set_operations(GraphEdit::IS_SUBSET, n, z)) {
  1525. Vector<StringName> t;
  1526. t.push_back(E);
  1527. if (!l.has(current_layer)) {
  1528. l.insert(current_layer, Vector<StringName>{});
  1529. }
  1530. selected = true;
  1531. t.append_array(l[current_layer]);
  1532. l.insert(current_layer, t);
  1533. HashSet<StringName> V;
  1534. V.insert(E);
  1535. _set_operations(GraphEdit::UNION, u, V);
  1536. }
  1537. }
  1538. if (!selected) {
  1539. current_layer++;
  1540. uint32_t previous_size_z = z.size();
  1541. _set_operations(GraphEdit::UNION, z, u);
  1542. if (z.size() == previous_size_z) {
  1543. WARN_PRINT("Graph contains cycle(s). The cycle(s) will not be rearranged accurately.");
  1544. Vector<StringName> t;
  1545. if (l.has(0)) {
  1546. t.append_array(l[0]);
  1547. }
  1548. for (const StringName &E : p) {
  1549. t.push_back(E);
  1550. }
  1551. l.insert(0, t);
  1552. break;
  1553. }
  1554. }
  1555. selected = false;
  1556. }
  1557. return l;
  1558. }
  1559. Vector<StringName> GraphEdit::_split(const Vector<StringName> &r_layer, const HashMap<StringName, Dictionary> &r_crossings) {
  1560. if (!r_layer.size()) {
  1561. return Vector<StringName>();
  1562. }
  1563. StringName p = r_layer[Math::random(0, r_layer.size() - 1)];
  1564. Vector<StringName> left;
  1565. Vector<StringName> right;
  1566. for (int i = 0; i < r_layer.size(); i++) {
  1567. if (p != r_layer[i]) {
  1568. StringName q = r_layer[i];
  1569. int cross_pq = r_crossings[p][q];
  1570. int cross_qp = r_crossings[q][p];
  1571. if (cross_pq > cross_qp) {
  1572. left.push_back(q);
  1573. } else {
  1574. right.push_back(q);
  1575. }
  1576. }
  1577. }
  1578. left.push_back(p);
  1579. left.append_array(right);
  1580. return left;
  1581. }
  1582. void GraphEdit::_horizontal_alignment(Dictionary &r_root, Dictionary &r_align, const HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours, const HashSet<StringName> &r_selected_nodes) {
  1583. for (const StringName &E : r_selected_nodes) {
  1584. r_root[E] = E;
  1585. r_align[E] = E;
  1586. }
  1587. if (r_layers.size() == 1) {
  1588. return;
  1589. }
  1590. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1591. Vector<StringName> lower_layer = r_layers[i];
  1592. Vector<StringName> upper_layer = r_layers[i - 1];
  1593. int r = -1;
  1594. for (int j = 0; j < lower_layer.size(); j++) {
  1595. Vector<Pair<int, StringName>> up;
  1596. StringName current_node = lower_layer[j];
  1597. for (int k = 0; k < upper_layer.size(); k++) {
  1598. StringName adjacent_neighbour = upper_layer[k];
  1599. if (r_upper_neighbours[current_node].has(adjacent_neighbour)) {
  1600. up.push_back(Pair<int, StringName>(k, adjacent_neighbour));
  1601. }
  1602. }
  1603. int start = (up.size() - 1) / 2;
  1604. int end = (up.size() - 1) % 2 ? start + 1 : start;
  1605. for (int p = start; p <= end; p++) {
  1606. StringName Align = r_align[current_node];
  1607. if (Align == current_node && r < up[p].first) {
  1608. r_align[up[p].second] = lower_layer[j];
  1609. r_root[current_node] = r_root[up[p].second];
  1610. r_align[current_node] = r_root[up[p].second];
  1611. r = up[p].first;
  1612. }
  1613. }
  1614. }
  1615. }
  1616. }
  1617. void GraphEdit::_crossing_minimisation(HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, HashSet<StringName>> &r_upper_neighbours) {
  1618. if (r_layers.size() == 1) {
  1619. return;
  1620. }
  1621. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1622. Vector<StringName> upper_layer = r_layers[i - 1];
  1623. Vector<StringName> lower_layer = r_layers[i];
  1624. HashMap<StringName, Dictionary> c;
  1625. for (int j = 0; j < lower_layer.size(); j++) {
  1626. StringName p = lower_layer[j];
  1627. Dictionary d;
  1628. for (int k = 0; k < lower_layer.size(); k++) {
  1629. unsigned int crossings = 0;
  1630. StringName q = lower_layer[k];
  1631. if (j != k) {
  1632. for (int h = 1; h < upper_layer.size(); h++) {
  1633. if (r_upper_neighbours[p].has(upper_layer[h])) {
  1634. for (int g = 0; g < h; g++) {
  1635. if (r_upper_neighbours[q].has(upper_layer[g])) {
  1636. crossings++;
  1637. }
  1638. }
  1639. }
  1640. }
  1641. }
  1642. d[q] = crossings;
  1643. }
  1644. c.insert(p, d);
  1645. }
  1646. r_layers.insert(i, _split(lower_layer, c));
  1647. }
  1648. }
  1649. void GraphEdit::_calculate_inner_shifts(Dictionary &r_inner_shifts, const Dictionary &r_root, const Dictionary &r_node_names, const Dictionary &r_align, const HashSet<StringName> &r_block_heads, const HashMap<StringName, Pair<int, int>> &r_port_info) {
  1650. for (const StringName &E : r_block_heads) {
  1651. real_t left = 0;
  1652. StringName u = E;
  1653. StringName v = r_align[u];
  1654. while (u != v && (StringName)r_root[u] != v) {
  1655. String _connection = String(u) + " " + String(v);
  1656. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[u]);
  1657. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[v]);
  1658. Pair<int, int> ports = r_port_info[_connection];
  1659. int pfrom = ports.first;
  1660. int pto = ports.second;
  1661. Vector2 frompos = gfrom->get_connection_output_position(pfrom);
  1662. Vector2 topos = gto->get_connection_input_position(pto);
  1663. real_t s = (real_t)r_inner_shifts[u] + (frompos.y - topos.y) / zoom;
  1664. r_inner_shifts[v] = s;
  1665. left = MIN(left, s);
  1666. u = v;
  1667. v = (StringName)r_align[v];
  1668. }
  1669. u = E;
  1670. do {
  1671. r_inner_shifts[u] = (real_t)r_inner_shifts[u] - left;
  1672. u = (StringName)r_align[u];
  1673. } while (u != E);
  1674. }
  1675. }
  1676. float GraphEdit::_calculate_threshold(StringName p_v, StringName p_w, const Dictionary &r_node_names, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_inner_shift, real_t p_current_threshold, const HashMap<StringName, Vector2> &r_node_positions) {
  1677. #define MAX_ORDER 2147483647
  1678. #define ORDER(node, layers) \
  1679. for (unsigned int i = 0; i < layers.size(); i++) { \
  1680. int index = layers[i].find(node); \
  1681. if (index > 0) { \
  1682. order = index; \
  1683. break; \
  1684. } \
  1685. order = MAX_ORDER; \
  1686. }
  1687. int order = MAX_ORDER;
  1688. float threshold = p_current_threshold;
  1689. if (p_v == p_w) {
  1690. int min_order = MAX_ORDER;
  1691. Connection incoming;
  1692. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1693. if (E->get().to == p_w) {
  1694. ORDER(E->get().from, r_layers);
  1695. if (min_order > order) {
  1696. min_order = order;
  1697. incoming = E->get();
  1698. }
  1699. }
  1700. }
  1701. if (incoming.from != StringName()) {
  1702. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[incoming.from]);
  1703. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1704. Vector2 frompos = gfrom->get_connection_output_position(incoming.from_port);
  1705. Vector2 topos = gto->get_connection_input_position(incoming.to_port);
  1706. //If connected block node is selected, calculate thershold or add current block to list
  1707. if (gfrom->is_selected()) {
  1708. Vector2 connected_block_pos = r_node_positions[r_root[incoming.from]];
  1709. if (connected_block_pos.y != FLT_MAX) {
  1710. //Connected block is placed. Calculate threshold
  1711. threshold = connected_block_pos.y + (real_t)r_inner_shift[incoming.from] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1712. }
  1713. }
  1714. }
  1715. }
  1716. if (threshold == FLT_MIN && (StringName)r_align[p_w] == p_v) {
  1717. //This time, pick an outgoing edge and repeat as above!
  1718. int min_order = MAX_ORDER;
  1719. Connection outgoing;
  1720. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1721. if (E->get().from == p_w) {
  1722. ORDER(E->get().to, r_layers);
  1723. if (min_order > order) {
  1724. min_order = order;
  1725. outgoing = E->get();
  1726. }
  1727. }
  1728. }
  1729. if (outgoing.to != StringName()) {
  1730. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1731. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[outgoing.to]);
  1732. Vector2 frompos = gfrom->get_connection_output_position(outgoing.from_port);
  1733. Vector2 topos = gto->get_connection_input_position(outgoing.to_port);
  1734. //If connected block node is selected, calculate thershold or add current block to list
  1735. if (gto->is_selected()) {
  1736. Vector2 connected_block_pos = r_node_positions[r_root[outgoing.to]];
  1737. if (connected_block_pos.y != FLT_MAX) {
  1738. //Connected block is placed. Calculate threshold
  1739. threshold = connected_block_pos.y + (real_t)r_inner_shift[outgoing.to] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1740. }
  1741. }
  1742. }
  1743. }
  1744. #undef MAX_ORDER
  1745. #undef ORDER
  1746. return threshold;
  1747. }
  1748. void GraphEdit::_place_block(StringName p_v, float p_delta, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_node_name, const Dictionary &r_inner_shift, Dictionary &r_sink, Dictionary &r_shift, HashMap<StringName, Vector2> &r_node_positions) {
  1749. #define PRED(node, layers) \
  1750. for (unsigned int i = 0; i < layers.size(); i++) { \
  1751. int index = layers[i].find(node); \
  1752. if (index > 0) { \
  1753. predecessor = layers[i][index - 1]; \
  1754. break; \
  1755. } \
  1756. predecessor = StringName(); \
  1757. }
  1758. StringName predecessor;
  1759. StringName successor;
  1760. Vector2 pos = r_node_positions[p_v];
  1761. if (pos.y == FLT_MAX) {
  1762. pos.y = 0;
  1763. bool initial = false;
  1764. StringName w = p_v;
  1765. real_t threshold = FLT_MIN;
  1766. do {
  1767. PRED(w, r_layers);
  1768. if (predecessor != StringName()) {
  1769. StringName u = r_root[predecessor];
  1770. _place_block(u, p_delta, r_layers, r_root, r_align, r_node_name, r_inner_shift, r_sink, r_shift, r_node_positions);
  1771. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1772. if ((StringName)r_sink[p_v] == p_v) {
  1773. r_sink[p_v] = r_sink[u];
  1774. }
  1775. Vector2 predecessor_root_pos = r_node_positions[u];
  1776. Vector2 predecessor_node_size = Object::cast_to<GraphNode>(r_node_name[predecessor])->get_size();
  1777. if (r_sink[p_v] != r_sink[u]) {
  1778. real_t sc = pos.y + (real_t)r_inner_shift[w] - predecessor_root_pos.y - (real_t)r_inner_shift[predecessor] - predecessor_node_size.y - p_delta;
  1779. r_shift[r_sink[u]] = MIN(sc, (real_t)r_shift[r_sink[u]]);
  1780. } else {
  1781. real_t sb = predecessor_root_pos.y + (real_t)r_inner_shift[predecessor] + predecessor_node_size.y - (real_t)r_inner_shift[w] + p_delta;
  1782. sb = MAX(sb, threshold);
  1783. if (initial) {
  1784. pos.y = sb;
  1785. } else {
  1786. pos.y = MAX(pos.y, sb);
  1787. }
  1788. initial = false;
  1789. }
  1790. }
  1791. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1792. w = r_align[w];
  1793. } while (w != p_v);
  1794. r_node_positions.insert(p_v, pos);
  1795. }
  1796. #undef PRED
  1797. }
  1798. void GraphEdit::arrange_nodes() {
  1799. if (!arranging_graph) {
  1800. arranging_graph = true;
  1801. } else {
  1802. return;
  1803. }
  1804. Dictionary node_names;
  1805. HashSet<StringName> selected_nodes;
  1806. bool arrange_entire_graph = true;
  1807. for (int i = get_child_count() - 1; i >= 0; i--) {
  1808. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1809. if (!gn) {
  1810. continue;
  1811. }
  1812. node_names[gn->get_name()] = gn;
  1813. if (gn->is_selected()) {
  1814. arrange_entire_graph = false;
  1815. }
  1816. }
  1817. HashMap<StringName, HashSet<StringName>> upper_neighbours;
  1818. HashMap<StringName, Pair<int, int>> port_info;
  1819. Vector2 origin(FLT_MAX, FLT_MAX);
  1820. float gap_v = 100.0f;
  1821. float gap_h = 100.0f;
  1822. for (int i = get_child_count() - 1; i >= 0; i--) {
  1823. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1824. if (!gn) {
  1825. continue;
  1826. }
  1827. if (gn->is_selected() || arrange_entire_graph) {
  1828. selected_nodes.insert(gn->get_name());
  1829. HashSet<StringName> s;
  1830. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1831. GraphNode *p_from = Object::cast_to<GraphNode>(node_names[E->get().from]);
  1832. if (E->get().to == gn->get_name() && (p_from->is_selected() || arrange_entire_graph) && E->get().to != E->get().from) {
  1833. if (!s.has(p_from->get_name())) {
  1834. s.insert(p_from->get_name());
  1835. }
  1836. String s_connection = String(p_from->get_name()) + " " + String(E->get().to);
  1837. StringName _connection(s_connection);
  1838. Pair<int, int> ports(E->get().from_port, E->get().to_port);
  1839. if (port_info.has(_connection)) {
  1840. Pair<int, int> p_ports = port_info[_connection];
  1841. if (p_ports.first < ports.first) {
  1842. ports = p_ports;
  1843. }
  1844. }
  1845. port_info.insert(_connection, ports);
  1846. }
  1847. }
  1848. upper_neighbours.insert(gn->get_name(), s);
  1849. }
  1850. }
  1851. if (!selected_nodes.size()) {
  1852. arranging_graph = false;
  1853. return;
  1854. }
  1855. HashMap<int, Vector<StringName>> layers = _layering(selected_nodes, upper_neighbours);
  1856. _crossing_minimisation(layers, upper_neighbours);
  1857. Dictionary root, align, sink, shift;
  1858. _horizontal_alignment(root, align, layers, upper_neighbours, selected_nodes);
  1859. HashMap<StringName, Vector2> new_positions;
  1860. Vector2 default_position(FLT_MAX, FLT_MAX);
  1861. Dictionary inner_shift;
  1862. HashSet<StringName> block_heads;
  1863. for (const StringName &E : selected_nodes) {
  1864. inner_shift[E] = 0.0f;
  1865. sink[E] = E;
  1866. shift[E] = FLT_MAX;
  1867. new_positions.insert(E, default_position);
  1868. if ((StringName)root[E] == E) {
  1869. block_heads.insert(E);
  1870. }
  1871. }
  1872. _calculate_inner_shifts(inner_shift, root, node_names, align, block_heads, port_info);
  1873. for (const StringName &E : block_heads) {
  1874. _place_block(E, gap_v, layers, root, align, node_names, inner_shift, sink, shift, new_positions);
  1875. }
  1876. origin.y = Object::cast_to<GraphNode>(node_names[layers[0][0]])->get_position_offset().y - (new_positions[layers[0][0]].y + (float)inner_shift[layers[0][0]]);
  1877. origin.x = Object::cast_to<GraphNode>(node_names[layers[0][0]])->get_position_offset().x;
  1878. for (const StringName &E : block_heads) {
  1879. StringName u = E;
  1880. float start_from = origin.y + new_positions[E].y;
  1881. do {
  1882. Vector2 cal_pos;
  1883. cal_pos.y = start_from + (real_t)inner_shift[u];
  1884. new_positions.insert(u, cal_pos);
  1885. u = align[u];
  1886. } while (u != E);
  1887. }
  1888. // Compute horizontal coordinates individually for layers to get uniform gap.
  1889. float start_from = origin.x;
  1890. float largest_node_size = 0.0f;
  1891. for (unsigned int i = 0; i < layers.size(); i++) {
  1892. Vector<StringName> layer = layers[i];
  1893. for (int j = 0; j < layer.size(); j++) {
  1894. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1895. largest_node_size = MAX(largest_node_size, current_node_size);
  1896. }
  1897. for (int j = 0; j < layer.size(); j++) {
  1898. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1899. Vector2 cal_pos = new_positions[layer[j]];
  1900. if (current_node_size == largest_node_size) {
  1901. cal_pos.x = start_from;
  1902. } else {
  1903. float current_node_start_pos = start_from;
  1904. if (current_node_size < largest_node_size / 2) {
  1905. if (!(i || j)) {
  1906. start_from -= (largest_node_size - current_node_size);
  1907. }
  1908. current_node_start_pos = start_from + largest_node_size - current_node_size;
  1909. }
  1910. cal_pos.x = current_node_start_pos;
  1911. }
  1912. new_positions.insert(layer[j], cal_pos);
  1913. }
  1914. start_from += largest_node_size + gap_h;
  1915. largest_node_size = 0.0f;
  1916. }
  1917. emit_signal(SNAME("begin_node_move"));
  1918. for (const StringName &E : selected_nodes) {
  1919. GraphNode *gn = Object::cast_to<GraphNode>(node_names[E]);
  1920. gn->set_drag(true);
  1921. Vector2 pos = (new_positions[E]);
  1922. if (is_using_snap()) {
  1923. const int snap = get_snap();
  1924. pos = pos.snapped(Vector2(snap, snap));
  1925. }
  1926. gn->set_position_offset(pos);
  1927. gn->set_drag(false);
  1928. }
  1929. emit_signal(SNAME("end_node_move"));
  1930. arranging_graph = false;
  1931. }
  1932. void GraphEdit::_bind_methods() {
  1933. ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::connect_node);
  1934. ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
  1935. ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
  1936. ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1937. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1938. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1939. ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end);
  1940. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1941. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "offset"), &GraphEdit::set_scroll_ofs);
  1942. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1943. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1944. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1945. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1946. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1947. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1948. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1949. ClassDB::bind_method(D_METHOD("get_connection_line", "from_node", "to_node"), &GraphEdit::get_connection_line);
  1950. ClassDB::bind_method(D_METHOD("set_panning_scheme", "scheme"), &GraphEdit::set_panning_scheme);
  1951. ClassDB::bind_method(D_METHOD("get_panning_scheme"), &GraphEdit::get_panning_scheme);
  1952. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  1953. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1954. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1955. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1956. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1957. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1958. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1959. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1960. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1961. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1962. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1963. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1964. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1965. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1966. ClassDB::bind_method(D_METHOD("set_connection_lines_curvature", "curvature"), &GraphEdit::set_connection_lines_curvature);
  1967. ClassDB::bind_method(D_METHOD("get_connection_lines_curvature"), &GraphEdit::get_connection_lines_curvature);
  1968. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  1969. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  1970. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  1971. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  1972. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  1973. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1974. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  1975. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1976. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1977. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1978. ClassDB::bind_method(D_METHOD("set_arrange_nodes_button_hidden", "enable"), &GraphEdit::set_arrange_nodes_button_hidden);
  1979. ClassDB::bind_method(D_METHOD("is_arrange_nodes_button_hidden"), &GraphEdit::is_arrange_nodes_button_hidden);
  1980. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1981. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1982. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1983. GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node", "in_port", "mouse_position");
  1984. GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node", "in_port", "mouse_position");
  1985. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1986. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  1987. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1988. GDVIRTUAL_BIND(_get_connection_line, "from_position", "to_position")
  1989. GDVIRTUAL_BIND(_is_node_hover_valid, "from_node", "from_port", "to_node", "to_port");
  1990. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1991. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_ofs", "get_scroll_ofs");
  1992. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snap", "get_snap");
  1993. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1994. ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
  1995. ADD_GROUP("Connection Lines", "connection_lines");
  1996. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
  1997. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness", PROPERTY_HINT_NONE, "suffix:px"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  1998. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  1999. ADD_GROUP("Zoom", "");
  2000. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  2001. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  2002. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  2003. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  2004. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  2005. ADD_GROUP("Minimap", "minimap_");
  2006. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  2007. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_size", "get_minimap_size");
  2008. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  2009. ADD_GROUP("UI", "");
  2010. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arrange_nodes_button_hidden"), "set_arrange_nodes_button_hidden", "is_arrange_nodes_button_hidden");
  2011. 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")));
  2012. 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")));
  2013. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  2014. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  2015. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  2016. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  2017. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2018. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  2019. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2020. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  2021. ADD_SIGNAL(MethodInfo("delete_nodes_request", PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_ARRAY_TYPE, "StringName")));
  2022. ADD_SIGNAL(MethodInfo("begin_node_move"));
  2023. ADD_SIGNAL(MethodInfo("end_node_move"));
  2024. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "offset")));
  2025. ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output")));
  2026. ADD_SIGNAL(MethodInfo("connection_drag_ended"));
  2027. BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
  2028. BIND_ENUM_CONSTANT(SCROLL_PANS);
  2029. }
  2030. GraphEdit::GraphEdit() {
  2031. set_focus_mode(FOCUS_ALL);
  2032. // Allow dezooming 8 times from the default zoom level.
  2033. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  2034. // but this is still useful for previewing and navigation.
  2035. zoom_min = (1 / Math::pow(zoom_step, 8));
  2036. // Allow zooming 4 times from the default zoom level.
  2037. zoom_max = (1 * Math::pow(zoom_step, 4));
  2038. panner.instantiate();
  2039. panner->set_callbacks(callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback));
  2040. top_layer = memnew(GraphEditFilter(this));
  2041. add_child(top_layer, false, INTERNAL_MODE_BACK);
  2042. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  2043. top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2044. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  2045. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  2046. top_layer->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  2047. connections_layer = memnew(Control);
  2048. add_child(connections_layer, false, INTERNAL_MODE_FRONT);
  2049. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  2050. connections_layer->set_name("CLAYER");
  2051. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  2052. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  2053. h_scroll = memnew(HScrollBar);
  2054. h_scroll->set_name("_h_scroll");
  2055. top_layer->add_child(h_scroll);
  2056. v_scroll = memnew(VScrollBar);
  2057. v_scroll->set_name("_v_scroll");
  2058. top_layer->add_child(v_scroll);
  2059. //set large minmax so it can scroll even if not resized yet
  2060. h_scroll->set_min(-10000);
  2061. h_scroll->set_max(10000);
  2062. v_scroll->set_min(-10000);
  2063. v_scroll->set_max(10000);
  2064. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2065. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  2066. zoom_hb = memnew(HBoxContainer);
  2067. top_layer->add_child(zoom_hb);
  2068. zoom_hb->set_position(Vector2(10, 10));
  2069. zoom_label = memnew(Label);
  2070. zoom_hb->add_child(zoom_label);
  2071. zoom_label->set_visible(false);
  2072. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  2073. zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  2074. zoom_label->set_custom_minimum_size(Size2(48, 0));
  2075. _update_zoom_label();
  2076. zoom_minus = memnew(Button);
  2077. zoom_minus->set_flat(true);
  2078. zoom_hb->add_child(zoom_minus);
  2079. zoom_minus->set_tooltip_text(RTR("Zoom Out"));
  2080. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  2081. zoom_minus->set_focus_mode(FOCUS_NONE);
  2082. zoom_reset = memnew(Button);
  2083. zoom_reset->set_flat(true);
  2084. zoom_hb->add_child(zoom_reset);
  2085. zoom_reset->set_tooltip_text(RTR("Zoom Reset"));
  2086. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  2087. zoom_reset->set_focus_mode(FOCUS_NONE);
  2088. zoom_plus = memnew(Button);
  2089. zoom_plus->set_flat(true);
  2090. zoom_hb->add_child(zoom_plus);
  2091. zoom_plus->set_tooltip_text(RTR("Zoom In"));
  2092. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  2093. zoom_plus->set_focus_mode(FOCUS_NONE);
  2094. snap_button = memnew(Button);
  2095. snap_button->set_flat(true);
  2096. snap_button->set_toggle_mode(true);
  2097. snap_button->set_tooltip_text(RTR("Enable snap and show grid."));
  2098. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  2099. snap_button->set_pressed(true);
  2100. snap_button->set_focus_mode(FOCUS_NONE);
  2101. zoom_hb->add_child(snap_button);
  2102. snap_amount = memnew(SpinBox);
  2103. snap_amount->set_min(5);
  2104. snap_amount->set_max(100);
  2105. snap_amount->set_step(1);
  2106. snap_amount->set_value(20);
  2107. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  2108. zoom_hb->add_child(snap_amount);
  2109. minimap_button = memnew(Button);
  2110. minimap_button->set_flat(true);
  2111. minimap_button->set_toggle_mode(true);
  2112. minimap_button->set_tooltip_text(RTR("Enable grid minimap."));
  2113. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  2114. minimap_button->set_pressed(true);
  2115. minimap_button->set_focus_mode(FOCUS_NONE);
  2116. zoom_hb->add_child(minimap_button);
  2117. layout_button = memnew(Button);
  2118. layout_button->set_flat(true);
  2119. zoom_hb->add_child(layout_button);
  2120. layout_button->set_tooltip_text(RTR("Arrange nodes."));
  2121. layout_button->connect("pressed", callable_mp(this, &GraphEdit::arrange_nodes));
  2122. layout_button->set_focus_mode(FOCUS_NONE);
  2123. Vector2 minimap_size = Vector2(240, 160);
  2124. float minimap_opacity = 0.65;
  2125. minimap = memnew(GraphEditMinimap(this));
  2126. top_layer->add_child(minimap);
  2127. minimap->set_name("_minimap");
  2128. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  2129. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  2130. minimap->set_custom_minimum_size(Vector2(50, 50));
  2131. minimap->set_size(minimap_size);
  2132. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  2133. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  2134. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  2135. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2136. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2137. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  2138. set_clip_contents(true);
  2139. }