graph_edit.cpp 80 KB

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