graph_edit.cpp 80 KB

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