graph_edit.cpp 85 KB

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