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