graph_edit.cpp 77 KB

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