2
0

graph_edit.cpp 76 KB

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