graph_edit.cpp 80 KB

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