graph_edit.cpp 84 KB

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