graph_edit.cpp 84 KB

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