graph_edit.cpp 80 KB

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