graph_edit.cpp 84 KB

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