graph_edit.cpp 85 KB

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