graph_edit.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /**************************************************************************/
  2. /* graph_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "graph_edit.h"
  31. #include "core/input/input.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/keyboard.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/graph_edit_arranger.h"
  37. #include "scene/gui/view_panner.h"
  38. #include "scene/resources/style_box_flat.h"
  39. constexpr int MINIMAP_OFFSET = 12;
  40. constexpr int MINIMAP_PADDING = 5;
  41. constexpr int MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION = 20;
  42. constexpr int MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES = 5;
  43. constexpr int GRID_MINOR_STEPS_PER_MAJOR_LINE = 10;
  44. constexpr int GRID_MIN_SNAPPING_DISTANCE = 2;
  45. constexpr int GRID_MAX_SNAPPING_DISTANCE = 100;
  46. constexpr float CONNECTING_TARGET_LINE_COLOR_BRIGHTENING = 0.4;
  47. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  48. return ge->_filter_input(p_point);
  49. }
  50. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  51. ge = p_edit;
  52. }
  53. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  54. ge = p_edit;
  55. graph_proportions = Vector2(1, 1);
  56. graph_padding = Vector2(0, 0);
  57. camera_position = Vector2(100, 50);
  58. camera_size = Vector2(200, 200);
  59. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  60. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  61. is_pressing = false;
  62. is_resizing = false;
  63. }
  64. Control::CursorShape GraphEditMinimap::get_cursor_shape(const Point2 &p_pos) const {
  65. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  66. if (is_resizing || (p_pos.x < resizer->get_width() && p_pos.y < resizer->get_height())) {
  67. return CURSOR_FDIAGSIZE;
  68. }
  69. return Control::get_cursor_shape(p_pos);
  70. }
  71. void GraphEditMinimap::update_minimap() {
  72. Vector2 graph_offset = _get_graph_offset();
  73. Vector2 graph_size = _get_graph_size();
  74. camera_position = ge->get_scroll_offset() - graph_offset;
  75. camera_size = ge->get_size();
  76. Vector2 render_size = _get_render_size();
  77. float target_ratio = render_size.width / render_size.height;
  78. float graph_ratio = graph_size.width / graph_size.height;
  79. graph_proportions = graph_size;
  80. graph_padding = Vector2(0, 0);
  81. if (graph_ratio > target_ratio) {
  82. graph_proportions.width = graph_size.width;
  83. graph_proportions.height = graph_size.width / target_ratio;
  84. graph_padding.y = Math::abs(graph_size.height - graph_proportions.y) / 2;
  85. } else {
  86. graph_proportions.width = graph_size.height * target_ratio;
  87. graph_proportions.height = graph_size.height;
  88. graph_padding.x = Math::abs(graph_size.width - graph_proportions.x) / 2;
  89. }
  90. // This centers minimap inside the minimap rectangle.
  91. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  92. }
  93. Rect2 GraphEditMinimap::get_camera_rect() {
  94. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  95. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  96. Vector2 camera_pos = (camera_center - camera_viewport / 2);
  97. return Rect2(camera_pos, camera_viewport);
  98. }
  99. Vector2 GraphEditMinimap::_get_render_size() {
  100. if (!is_inside_tree()) {
  101. return Vector2(0, 0);
  102. }
  103. return get_size() - 2 * minimap_padding;
  104. }
  105. Vector2 GraphEditMinimap::_get_graph_offset() {
  106. return Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  107. }
  108. Vector2 GraphEditMinimap::_get_graph_size() {
  109. Vector2 graph_size = Vector2(ge->h_scrollbar->get_max(), ge->v_scrollbar->get_max()) - Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  110. if (graph_size.width == 0) {
  111. graph_size.width = 1;
  112. }
  113. if (graph_size.height == 0) {
  114. graph_size.height = 1;
  115. }
  116. return graph_size;
  117. }
  118. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  119. Vector2 map_position = Vector2(0, 0);
  120. Vector2 render_size = _get_render_size();
  121. map_position.x = p_position.x * render_size.width / graph_proportions.x;
  122. map_position.y = p_position.y * render_size.height / graph_proportions.y;
  123. return map_position;
  124. }
  125. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  126. Vector2 graph_position = Vector2(0, 0);
  127. Vector2 render_size = _get_render_size();
  128. graph_position.x = p_position.x * graph_proportions.x / render_size.width;
  129. graph_position.y = p_position.y * graph_proportions.y / render_size.height;
  130. return graph_position;
  131. }
  132. void GraphEditMinimap::gui_input(const Ref<InputEvent> &p_ev) {
  133. ERR_FAIL_COND(p_ev.is_null());
  134. if (!ge->is_minimap_enabled()) {
  135. return;
  136. }
  137. Ref<InputEventMouseButton> mb = p_ev;
  138. Ref<InputEventMouseMotion> mm = p_ev;
  139. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  140. if (mb->is_pressed()) {
  141. is_pressing = true;
  142. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  143. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  144. if (resizer_hitbox.has_point(mb->get_position())) {
  145. is_resizing = true;
  146. } else {
  147. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  148. _adjust_graph_scroll(click_position);
  149. }
  150. } else {
  151. is_pressing = false;
  152. is_resizing = false;
  153. }
  154. accept_event();
  155. } else if (mm.is_valid() && is_pressing) {
  156. if (is_resizing) {
  157. // Prevent setting minimap wider than GraphEdit.
  158. Vector2 new_minimap_size;
  159. new_minimap_size.width = MIN(get_size().width - mm->get_relative().x, ge->get_size().width - 2.0 * minimap_padding.x);
  160. new_minimap_size.height = MIN(get_size().height - mm->get_relative().y, ge->get_size().height - 2.0 * minimap_padding.y);
  161. ge->set_minimap_size(new_minimap_size);
  162. queue_redraw();
  163. } else {
  164. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  165. _adjust_graph_scroll(click_position);
  166. }
  167. accept_event();
  168. }
  169. }
  170. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  171. Vector2 graph_offset = _get_graph_offset();
  172. ge->set_scroll_offset(p_offset + graph_offset - camera_size / 2);
  173. }
  174. Control::CursorShape GraphEdit::get_cursor_shape(const Point2 &p_pos) const {
  175. if (moving_selection) {
  176. return CURSOR_MOVE;
  177. }
  178. return Control::get_cursor_shape(p_pos);
  179. }
  180. PackedStringArray GraphEdit::get_configuration_warnings() const {
  181. PackedStringArray warnings = Control::get_configuration_warnings();
  182. warnings.push_back(RTR("Please be aware that GraphEdit and GraphNode will undergo extensive refactoring in a future 4.x version involving compatibility-breaking API changes."));
  183. return warnings;
  184. }
  185. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  186. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  187. return OK;
  188. }
  189. Connection c;
  190. c.from_node = p_from;
  191. c.from_port = p_from_port;
  192. c.to_node = p_to;
  193. c.to_port = p_to_port;
  194. c.activity = 0;
  195. connections.push_back(c);
  196. top_layer->queue_redraw();
  197. minimap->queue_redraw();
  198. queue_redraw();
  199. connections_layer->queue_redraw();
  200. return OK;
  201. }
  202. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  203. for (const Connection &E : connections) {
  204. if (E.from_node == p_from && E.from_port == p_from_port && E.to_node == p_to && E.to_port == p_to_port) {
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  211. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  212. if (E->get().from_node == p_from && E->get().from_port == p_from_port && E->get().to_node == p_to && E->get().to_port == p_to_port) {
  213. connections.erase(E);
  214. top_layer->queue_redraw();
  215. minimap->queue_redraw();
  216. queue_redraw();
  217. connections_layer->queue_redraw();
  218. return;
  219. }
  220. }
  221. }
  222. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  223. *r_connections = connections;
  224. }
  225. void GraphEdit::set_scroll_offset(const Vector2 &p_offset) {
  226. setting_scroll_offset = true;
  227. h_scrollbar->set_value(p_offset.x);
  228. v_scrollbar->set_value(p_offset.y);
  229. _update_scroll();
  230. setting_scroll_offset = false;
  231. }
  232. Vector2 GraphEdit::get_scroll_offset() const {
  233. return Vector2(h_scrollbar->get_value(), v_scrollbar->get_value());
  234. }
  235. void GraphEdit::_scroll_moved(double) {
  236. if (!awaiting_scroll_offset_update) {
  237. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  238. awaiting_scroll_offset_update = true;
  239. }
  240. top_layer->queue_redraw();
  241. minimap->queue_redraw();
  242. queue_redraw();
  243. }
  244. void GraphEdit::_update_scroll_offset() {
  245. set_block_minimum_size_adjust(true);
  246. for (int i = 0; i < get_child_count(); i++) {
  247. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  248. if (!graph_element) {
  249. continue;
  250. }
  251. Point2 pos = graph_element->get_position_offset() * zoom;
  252. pos -= Point2(h_scrollbar->get_value(), v_scrollbar->get_value());
  253. graph_element->set_position(pos);
  254. if (graph_element->get_scale() != Vector2(zoom, zoom)) {
  255. graph_element->set_scale(Vector2(zoom, zoom));
  256. }
  257. }
  258. connections_layer->set_position(-Point2(h_scrollbar->get_value(), v_scrollbar->get_value()));
  259. set_block_minimum_size_adjust(false);
  260. awaiting_scroll_offset_update = false;
  261. // In Godot, signals on value change are avoided by convention.
  262. if (!setting_scroll_offset) {
  263. emit_signal(SNAME("scroll_offset_changed"), get_scroll_offset());
  264. }
  265. }
  266. void GraphEdit::_update_scroll() {
  267. if (updating) {
  268. return;
  269. }
  270. updating = true;
  271. set_block_minimum_size_adjust(true);
  272. Rect2 screen_rect;
  273. for (int i = 0; i < get_child_count(); i++) {
  274. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  275. if (!graph_element) {
  276. continue;
  277. }
  278. Rect2 node_rect;
  279. node_rect.position = graph_element->get_position_offset() * zoom;
  280. node_rect.size = graph_element->get_size() * zoom;
  281. screen_rect = screen_rect.merge(node_rect);
  282. }
  283. screen_rect.position -= get_size();
  284. screen_rect.size += get_size() * 2.0;
  285. h_scrollbar->set_min(screen_rect.position.x);
  286. h_scrollbar->set_max(screen_rect.position.x + screen_rect.size.width);
  287. h_scrollbar->set_page(get_size().x);
  288. if (h_scrollbar->get_max() - h_scrollbar->get_min() <= h_scrollbar->get_page()) {
  289. h_scrollbar->hide();
  290. } else {
  291. h_scrollbar->show();
  292. }
  293. v_scrollbar->set_min(screen_rect.position.y);
  294. v_scrollbar->set_max(screen_rect.position.y + screen_rect.size.height);
  295. v_scrollbar->set_page(get_size().height);
  296. if (v_scrollbar->get_max() - v_scrollbar->get_min() <= v_scrollbar->get_page()) {
  297. v_scrollbar->hide();
  298. } else {
  299. v_scrollbar->show();
  300. }
  301. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  302. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  303. // Avoid scrollbar overlapping.
  304. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scrollbar->is_visible() ? -vmin.width : 0);
  305. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scrollbar->is_visible() ? -hmin.height : 0);
  306. set_block_minimum_size_adjust(false);
  307. if (!awaiting_scroll_offset_update) {
  308. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  309. awaiting_scroll_offset_update = true;
  310. }
  311. updating = false;
  312. }
  313. void GraphEdit::_graph_element_moved_to_front(Node *p_node) {
  314. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  315. ERR_FAIL_NULL(graph_element);
  316. graph_element->move_to_front();
  317. }
  318. void GraphEdit::_graph_element_selected(Node *p_node) {
  319. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  320. ERR_FAIL_NULL(graph_element);
  321. emit_signal(SNAME("node_selected"), graph_element);
  322. }
  323. void GraphEdit::_graph_element_deselected(Node *p_node) {
  324. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  325. ERR_FAIL_NULL(graph_element);
  326. emit_signal(SNAME("node_deselected"), graph_element);
  327. }
  328. void GraphEdit::_graph_element_resized(Vector2 p_new_minsize, Node *p_node) {
  329. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  330. ERR_FAIL_NULL(graph_element);
  331. graph_element->set_size(p_new_minsize);
  332. }
  333. void GraphEdit::_graph_element_moved(Node *p_node) {
  334. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  335. ERR_FAIL_NULL(graph_element);
  336. top_layer->queue_redraw();
  337. minimap->queue_redraw();
  338. queue_redraw();
  339. connections_layer->queue_redraw();
  340. }
  341. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) {
  342. GraphNode *graph_node = Object::cast_to<GraphNode>(p_node);
  343. ERR_FAIL_NULL(graph_node);
  344. top_layer->queue_redraw();
  345. minimap->queue_redraw();
  346. queue_redraw();
  347. connections_layer->queue_redraw();
  348. }
  349. void GraphEdit::add_child_notify(Node *p_child) {
  350. Control::add_child_notify(p_child);
  351. // Keep the top layer always on top!
  352. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  353. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  354. if (graph_element) {
  355. graph_element->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved).bind(graph_element));
  356. graph_element->connect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected).bind(graph_element));
  357. graph_element->connect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected).bind(graph_element));
  358. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  359. if (graph_node) {
  360. graph_element->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(graph_element));
  361. }
  362. graph_element->connect("raise_request", callable_mp(this, &GraphEdit::_graph_element_moved_to_front).bind(graph_element));
  363. graph_element->connect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resized).bind(graph_element));
  364. graph_element->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  365. graph_element->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  366. graph_element->set_scale(Vector2(zoom, zoom));
  367. _graph_element_moved(graph_element);
  368. graph_element->set_mouse_filter(MOUSE_FILTER_PASS);
  369. }
  370. }
  371. void GraphEdit::remove_child_notify(Node *p_child) {
  372. Control::remove_child_notify(p_child);
  373. if (p_child == top_layer) {
  374. top_layer = nullptr;
  375. minimap = nullptr;
  376. } else if (p_child == connections_layer) {
  377. connections_layer = nullptr;
  378. }
  379. if (top_layer != nullptr && is_inside_tree()) {
  380. // Keep the top layer always on top!
  381. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  382. }
  383. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  384. if (graph_element) {
  385. graph_element->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved));
  386. graph_element->disconnect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected));
  387. graph_element->disconnect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected));
  388. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  389. if (graph_node) {
  390. graph_element->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  391. }
  392. graph_element->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_element_moved_to_front));
  393. graph_element->disconnect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resized));
  394. // In case of the whole GraphEdit being destroyed these references can already be freed.
  395. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  396. graph_element->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  397. }
  398. if (minimap != nullptr && minimap->is_inside_tree()) {
  399. graph_element->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  400. }
  401. }
  402. }
  403. void GraphEdit::_notification(int p_what) {
  404. switch (p_what) {
  405. case NOTIFICATION_THEME_CHANGED: {
  406. port_hotzone_inner_extent = get_theme_constant("port_hotzone_inner_extent");
  407. port_hotzone_outer_extent = get_theme_constant("port_hotzone_outer_extent");
  408. zoom_minus_button->set_icon(get_theme_icon(SNAME("zoom_out")));
  409. zoom_reset_button->set_icon(get_theme_icon(SNAME("zoom_reset")));
  410. zoom_plus_button->set_icon(get_theme_icon(SNAME("zoom_in")));
  411. toggle_snapping_button->set_icon(get_theme_icon(SNAME("snapping_toggle")));
  412. show_grid_button->set_icon(get_theme_icon(SNAME("grid_toggle")));
  413. minimap_button->set_icon(get_theme_icon(SNAME("minimap_toggle")));
  414. layout_button->set_icon(get_theme_icon(SNAME("layout")));
  415. zoom_label->set_custom_minimum_size(Size2(48, 0) * get_theme_default_base_scale());
  416. } break;
  417. case NOTIFICATION_READY: {
  418. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  419. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  420. h_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  421. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  422. h_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  423. h_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  424. v_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  425. v_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  426. v_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  427. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  428. } break;
  429. case NOTIFICATION_DRAW: {
  430. // Draw background fill.
  431. draw_style_box(get_theme_stylebox(SNAME("panel")), Rect2(Point2(), get_size()));
  432. // Draw background grid.
  433. if (show_grid) {
  434. Vector2 offset = get_scroll_offset() / zoom;
  435. Size2 size = get_size() / zoom;
  436. Point2i from_pos = (offset / float(snapping_distance)).floor();
  437. Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1);
  438. Color grid_minor = get_theme_color(SNAME("grid_minor"));
  439. Color grid_major = get_theme_color(SNAME("grid_major"));
  440. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  441. Color color;
  442. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  443. color = grid_major;
  444. } else {
  445. color = grid_minor;
  446. }
  447. float base_offset = i * snapping_distance * zoom - offset.x * zoom;
  448. draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color);
  449. }
  450. for (int i = from_pos.y; i < from_pos.y + len.y; i++) {
  451. Color color;
  452. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  453. color = grid_major;
  454. } else {
  455. color = grid_minor;
  456. }
  457. float base_offset = i * snapping_distance * zoom - offset.y * zoom;
  458. draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color);
  459. }
  460. }
  461. } break;
  462. case NOTIFICATION_RESIZED: {
  463. _update_scroll();
  464. top_layer->queue_redraw();
  465. minimap->queue_redraw();
  466. } break;
  467. }
  468. }
  469. bool GraphEdit::_filter_input(const Point2 &p_point) {
  470. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  471. for (int i = get_child_count() - 1; i >= 0; i--) {
  472. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  473. if (!graph_node || !graph_node->is_visible_in_tree()) {
  474. continue;
  475. }
  476. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  477. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  478. // Determine slot height.
  479. int slot_index = graph_node->get_input_port_slot(j);
  480. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  481. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  482. if (is_in_input_hotzone(graph_node, j, p_point / zoom, port_size)) {
  483. return true;
  484. }
  485. }
  486. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  487. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  488. // Determine slot height.
  489. int slot_index = graph_node->get_output_port_slot(j);
  490. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  491. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  492. if (is_in_output_hotzone(graph_node, j, p_point / zoom, port_size)) {
  493. return true;
  494. }
  495. }
  496. }
  497. return false;
  498. }
  499. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  500. Ref<InputEventMouseButton> mb = p_ev;
  501. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  502. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  503. connecting_valid = false;
  504. click_pos = mb->get_position() / zoom;
  505. for (int i = get_child_count() - 1; i >= 0; i--) {
  506. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  507. if (!graph_node || !graph_node->is_visible_in_tree()) {
  508. continue;
  509. }
  510. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  511. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  512. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  513. // Determine slot height.
  514. int slot_index = graph_node->get_output_port_slot(j);
  515. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  516. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  517. if (is_in_output_hotzone(graph_node, j, click_pos, port_size)) {
  518. if (valid_left_disconnect_types.has(graph_node->get_output_port_type(j))) {
  519. // Check disconnect.
  520. for (const Connection &E : connections) {
  521. if (E.from_node == graph_node->get_name() && E.from_port == j) {
  522. Node *to = get_node(NodePath(E.to_node));
  523. if (Object::cast_to<GraphNode>(to)) {
  524. connecting_from = E.to_node;
  525. connecting_index = E.to_port;
  526. connecting_out = false;
  527. connecting_type = Object::cast_to<GraphNode>(to)->get_input_port_type(E.to_port);
  528. connecting_color = Object::cast_to<GraphNode>(to)->get_input_port_color(E.to_port);
  529. connecting_target = false;
  530. connecting_to = pos;
  531. if (connecting_type >= 0) {
  532. just_disconnected = true;
  533. emit_signal(SNAME("disconnection_request"), E.from_node, E.from_port, E.to_node, E.to_port);
  534. to = get_node(NodePath(connecting_from)); // Maybe it was erased.
  535. if (Object::cast_to<GraphNode>(to)) {
  536. connecting = true;
  537. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  538. }
  539. }
  540. return;
  541. }
  542. }
  543. }
  544. }
  545. connecting_from = graph_node->get_name();
  546. connecting_index = j;
  547. connecting_out = true;
  548. connecting_type = graph_node->get_output_port_type(j);
  549. connecting_color = graph_node->get_output_port_color(j);
  550. connecting_target = false;
  551. connecting_to = pos;
  552. if (connecting_type >= 0) {
  553. connecting = true;
  554. just_disconnected = false;
  555. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  556. }
  557. return;
  558. }
  559. }
  560. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  561. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  562. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  563. // Determine slot height.
  564. int slot_index = graph_node->get_input_port_slot(j);
  565. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  566. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  567. if (is_in_input_hotzone(graph_node, j, click_pos, port_size)) {
  568. if (right_disconnects || valid_right_disconnect_types.has(graph_node->get_input_port_type(j))) {
  569. // Check disconnect.
  570. for (const Connection &E : connections) {
  571. if (E.to_node == graph_node->get_name() && E.to_port == j) {
  572. Node *fr = get_node(NodePath(E.from_node));
  573. if (Object::cast_to<GraphNode>(fr)) {
  574. connecting_from = E.from_node;
  575. connecting_index = E.from_port;
  576. connecting_out = true;
  577. connecting_type = Object::cast_to<GraphNode>(fr)->get_output_port_type(E.from_port);
  578. connecting_color = Object::cast_to<GraphNode>(fr)->get_output_port_color(E.from_port);
  579. connecting_target = false;
  580. connecting_to = pos;
  581. just_disconnected = true;
  582. if (connecting_type >= 0) {
  583. emit_signal(SNAME("disconnection_request"), E.from_node, E.from_port, E.to_node, E.to_port);
  584. fr = get_node(NodePath(connecting_from));
  585. if (Object::cast_to<GraphNode>(fr)) {
  586. connecting = true;
  587. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  588. }
  589. }
  590. return;
  591. }
  592. }
  593. }
  594. }
  595. connecting_from = graph_node->get_name();
  596. connecting_index = j;
  597. connecting_out = false;
  598. connecting_type = graph_node->get_input_port_type(j);
  599. connecting_color = graph_node->get_input_port_color(j);
  600. connecting_target = false;
  601. connecting_to = pos;
  602. if (connecting_type >= 0) {
  603. connecting = true;
  604. just_disconnected = false;
  605. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  606. }
  607. return;
  608. }
  609. }
  610. }
  611. }
  612. Ref<InputEventMouseMotion> mm = p_ev;
  613. if (mm.is_valid() && connecting) {
  614. connecting_to = mm->get_position();
  615. connecting_target = false;
  616. top_layer->queue_redraw();
  617. minimap->queue_redraw();
  618. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION;
  619. if (connecting_valid) {
  620. Vector2 mpos = mm->get_position() / zoom;
  621. for (int i = get_child_count() - 1; i >= 0; i--) {
  622. Ref<Texture2D> port_icon = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  623. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  624. if (!graph_node || !graph_node->is_visible_in_tree()) {
  625. continue;
  626. }
  627. if (!connecting_out) {
  628. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  629. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  630. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  631. // Determine slot height.
  632. int slot_index = graph_node->get_output_port_slot(j);
  633. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  634. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  635. int type = graph_node->get_output_port_type(j);
  636. if ((type == connecting_type ||
  637. valid_connection_types.has(ConnectionType(connecting_type, type))) &&
  638. is_in_output_hotzone(graph_node, j, mpos, port_size)) {
  639. if (!is_node_hover_valid(graph_node->get_name(), j, connecting_from, connecting_index)) {
  640. continue;
  641. }
  642. connecting_target = true;
  643. connecting_to = pos;
  644. connecting_target_to = graph_node->get_name();
  645. connecting_target_index = j;
  646. return;
  647. }
  648. }
  649. } else {
  650. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  651. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  652. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  653. // Determine slot height.
  654. int slot_index = graph_node->get_input_port_slot(j);
  655. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  656. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  657. int type = graph_node->get_input_port_type(j);
  658. if ((type == connecting_type || valid_connection_types.has(ConnectionType(connecting_type, type))) &&
  659. is_in_input_hotzone(graph_node, j, mpos, port_size)) {
  660. if (!is_node_hover_valid(connecting_from, connecting_index, graph_node->get_name(), j)) {
  661. continue;
  662. }
  663. connecting_target = true;
  664. connecting_to = pos;
  665. connecting_target_to = graph_node->get_name();
  666. connecting_target_index = j;
  667. return;
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  675. if (connecting_valid) {
  676. if (connecting && connecting_target) {
  677. if (connecting_out) {
  678. emit_signal(SNAME("connection_request"), connecting_from, connecting_index, connecting_target_to, connecting_target_index);
  679. } else {
  680. emit_signal(SNAME("connection_request"), connecting_target_to, connecting_target_index, connecting_from, connecting_index);
  681. }
  682. } else if (!just_disconnected) {
  683. if (connecting_out) {
  684. emit_signal(SNAME("connection_to_empty"), connecting_from, connecting_index, mb->get_position());
  685. } else {
  686. emit_signal(SNAME("connection_from_empty"), connecting_from, connecting_index, mb->get_position());
  687. }
  688. }
  689. }
  690. if (connecting) {
  691. force_connection_drag_end();
  692. }
  693. }
  694. }
  695. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) {
  696. if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) {
  697. return false;
  698. }
  699. Rect2 control_rect = p_control->get_rect();
  700. control_rect.position *= zoom;
  701. control_rect.size *= zoom;
  702. control_rect.position += p_offset;
  703. if (!control_rect.has_point(mpos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  704. // Test children.
  705. for (int i = 0; i < p_control->get_child_count(); i++) {
  706. Control *child_rect = Object::cast_to<Control>(p_control->get_child(i));
  707. if (!child_rect) {
  708. continue;
  709. }
  710. if (_check_clickable_control(child_rect, mpos, control_rect.position)) {
  711. return true;
  712. }
  713. }
  714. return false;
  715. } else {
  716. return true;
  717. }
  718. }
  719. bool GraphEdit::is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  720. bool success;
  721. if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  722. return success;
  723. } else {
  724. Vector2 pos = p_graph_node->get_input_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  725. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true);
  726. }
  727. }
  728. bool GraphEdit::is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  729. if (p_graph_node->is_resizable()) {
  730. Ref<Texture2D> resizer = p_graph_node->get_theme_icon(SNAME("resizer"));
  731. Rect2 resizer_rect = Rect2(p_graph_node->get_position() / zoom + p_graph_node->get_size() - resizer->get_size(), resizer->get_size());
  732. if (resizer_rect.has_point(p_mouse_pos)) {
  733. return false;
  734. }
  735. }
  736. bool success;
  737. if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  738. return success;
  739. } else {
  740. Vector2 pos = p_graph_node->get_output_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  741. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false);
  742. }
  743. }
  744. bool GraphEdit::is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  745. Rect2 hotzone = Rect2(
  746. p_pos.x - (p_left ? port_hotzone_outer_extent : port_hotzone_inner_extent),
  747. p_pos.y - p_port_size.height / 2.0,
  748. port_hotzone_inner_extent + port_hotzone_outer_extent,
  749. p_port_size.height);
  750. if (!hotzone.has_point(p_mouse_pos)) {
  751. return false;
  752. }
  753. for (int i = 0; i < get_child_count(); i++) {
  754. GraphNode *child = Object::cast_to<GraphNode>(get_child(i));
  755. if (!child) {
  756. continue;
  757. }
  758. Rect2 child_rect = child->get_rect();
  759. if (child_rect.has_point(p_mouse_pos * zoom)) {
  760. for (int j = 0; j < child->get_child_count(); j++) {
  761. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  762. if (!subchild) {
  763. continue;
  764. }
  765. if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) {
  766. return false;
  767. }
  768. }
  769. }
  770. }
  771. return true;
  772. }
  773. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) {
  774. Vector<Vector2> ret;
  775. if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) {
  776. return ret;
  777. }
  778. float x_diff = (p_to.x - p_from.x);
  779. float cp_offset = x_diff * lines_curvature;
  780. if (x_diff < 0) {
  781. cp_offset *= -1;
  782. }
  783. Curve2D curve;
  784. curve.add_point(p_from);
  785. curve.set_point_out(0, Vector2(cp_offset, 0));
  786. curve.add_point(p_to);
  787. curve.set_point_in(1, Vector2(-cp_offset, 0));
  788. if (lines_curvature > 0) {
  789. return curve.tessellate(MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES, 2.0);
  790. } else {
  791. return curve.tessellate(1);
  792. }
  793. }
  794. 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) {
  795. Vector<Vector2> points = get_connection_line(p_from / p_zoom, p_to / p_zoom);
  796. Vector<Vector2> scaled_points;
  797. Vector<Color> colors;
  798. float length = (p_from / p_zoom).distance_to(p_to / p_zoom);
  799. for (int i = 0; i < points.size(); i++) {
  800. float d = (p_from / p_zoom).distance_to(points[i]) / length;
  801. colors.push_back(p_color.lerp(p_to_color, d));
  802. scaled_points.push_back(points[i] * p_zoom);
  803. }
  804. // Thickness below 0.5 doesn't look good on the graph or its minimap.
  805. p_where->draw_polyline_colors(scaled_points, colors, MAX(0.5, Math::floor(p_width * get_theme_default_base_scale())), lines_antialiased);
  806. }
  807. void GraphEdit::_connections_layer_draw() {
  808. Color activity_color = get_theme_color(SNAME("activity"));
  809. // Draw connections.
  810. List<List<Connection>::Element *> to_erase;
  811. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  812. const Connection &c = E->get();
  813. Node *from = get_node(NodePath(c.from_node));
  814. GraphNode *gnode_from = Object::cast_to<GraphNode>(from);
  815. if (!gnode_from) {
  816. to_erase.push_back(E);
  817. continue;
  818. }
  819. Node *to = get_node(NodePath(c.to_node));
  820. GraphNode *gnode_to = Object::cast_to<GraphNode>(to);
  821. if (!gnode_to) {
  822. to_erase.push_back(E);
  823. continue;
  824. }
  825. Vector2 frompos = gnode_from->get_output_port_position(c.from_port) * zoom + gnode_from->get_position_offset() * zoom;
  826. Color color = gnode_from->get_output_port_color(c.from_port);
  827. Vector2 topos = gnode_to->get_input_port_position(c.to_port) * zoom + gnode_to->get_position_offset() * zoom;
  828. Color tocolor = gnode_to->get_input_port_color(c.to_port);
  829. if (c.activity > 0) {
  830. color = color.lerp(activity_color, c.activity);
  831. tocolor = tocolor.lerp(activity_color, c.activity);
  832. }
  833. _draw_connection_line(connections_layer, frompos, topos, color, tocolor, lines_thickness, zoom);
  834. }
  835. for (List<Connection>::Element *&E : to_erase) {
  836. connections.erase(E);
  837. }
  838. }
  839. void GraphEdit::_top_layer_draw() {
  840. _update_scroll();
  841. if (connecting) {
  842. Node *node_from = get_node_or_null(NodePath(connecting_from));
  843. ERR_FAIL_NULL(node_from);
  844. GraphNode *graph_node_from = Object::cast_to<GraphNode>(node_from);
  845. ERR_FAIL_NULL(graph_node_from);
  846. Vector2 pos;
  847. if (connecting_out) {
  848. pos = graph_node_from->get_output_port_position(connecting_index) * zoom;
  849. } else {
  850. pos = graph_node_from->get_input_port_position(connecting_index) * zoom;
  851. }
  852. pos += graph_node_from->get_position();
  853. Vector2 to_pos = connecting_to;
  854. Color line_color = connecting_color;
  855. // Draw the line to the mouse cursor brighter when it's over a valid target port.
  856. if (connecting_target) {
  857. line_color.r += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING;
  858. line_color.g += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING;
  859. line_color.b += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING;
  860. }
  861. if (!connecting_out) {
  862. SWAP(pos, to_pos);
  863. }
  864. _draw_connection_line(top_layer, pos, to_pos, line_color, line_color, lines_thickness, zoom);
  865. }
  866. if (box_selecting) {
  867. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_fill")));
  868. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_stroke")), false);
  869. }
  870. }
  871. void GraphEdit::_minimap_draw() {
  872. if (!is_minimap_enabled()) {
  873. return;
  874. }
  875. minimap->update_minimap();
  876. // Draw the minimap background.
  877. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  878. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("panel")), minimap_rect);
  879. Vector2 graph_offset = minimap->_get_graph_offset();
  880. Vector2 minimap_offset = minimap->minimap_offset;
  881. // Draw graph nodes.
  882. for (int i = get_child_count() - 1; i >= 0; i--) {
  883. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  884. if (!graph_node || !graph_node->is_visible()) {
  885. continue;
  886. }
  887. Vector2 node_position = minimap->_convert_from_graph_position(graph_node->get_position_offset() * zoom - graph_offset) + minimap_offset;
  888. Vector2 node_size = minimap->_convert_from_graph_position(graph_node->get_size() * zoom);
  889. Rect2 node_rect = Rect2(node_position, node_size);
  890. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  891. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  892. Ref<StyleBoxFlat> sb_frame = graph_node->get_theme_stylebox(graph_node->is_selected() ? "panel_selected" : "panel");
  893. if (sb_frame.is_valid()) {
  894. Color node_color = sb_frame->get_bg_color();
  895. sb_minimap->set_bg_color(node_color);
  896. }
  897. minimap->draw_style_box(sb_minimap, node_rect);
  898. }
  899. // Draw node connections.
  900. Color activity_color = get_theme_color(SNAME("activity"));
  901. for (const Connection &E : connections) {
  902. Node *from = get_node(NodePath(E.from_node));
  903. GraphNode *graph_node_from = Object::cast_to<GraphNode>(from);
  904. if (!graph_node_from) {
  905. continue;
  906. }
  907. Node *node_to = get_node(NodePath(E.to_node));
  908. GraphNode *graph_node_to = Object::cast_to<GraphNode>(node_to);
  909. if (!graph_node_to) {
  910. continue;
  911. }
  912. Vector2 from_port_position = graph_node_from->get_position_offset() * zoom + graph_node_from->get_output_port_position(E.from_port) * zoom;
  913. Vector2 from_position = minimap->_convert_from_graph_position(from_port_position - graph_offset) + minimap_offset;
  914. Color from_color = graph_node_from->get_output_port_color(E.from_port);
  915. Vector2 to_port_position = graph_node_to->get_position_offset() * zoom + graph_node_to->get_input_port_position(E.to_port) * zoom;
  916. Vector2 to_position = minimap->_convert_from_graph_position(to_port_position - graph_offset) + minimap_offset;
  917. Color to_color = graph_node_to->get_input_port_color(E.to_port);
  918. if (E.activity > 0) {
  919. from_color = from_color.lerp(activity_color, E.activity);
  920. to_color = to_color.lerp(activity_color, E.activity);
  921. }
  922. _draw_connection_line(minimap, from_position, to_position, from_color, to_color, 0.5, minimap->_convert_from_graph_position(Vector2(zoom, zoom)).length());
  923. }
  924. // Draw the "camera" viewport.
  925. Rect2 camera_rect = minimap->get_camera_rect();
  926. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("camera")), camera_rect);
  927. // Draw the resizer control.
  928. Ref<Texture2D> resizer = minimap->get_theme_icon(SNAME("resizer"));
  929. Color resizer_color = minimap->get_theme_color(SNAME("resizer_color"));
  930. minimap->draw_texture(resizer, Point2(), resizer_color);
  931. }
  932. void GraphEdit::set_selected(Node *p_child) {
  933. for (int i = get_child_count() - 1; i >= 0; i--) {
  934. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  935. if (!graph_node) {
  936. continue;
  937. }
  938. graph_node->set_selected(graph_node == p_child);
  939. }
  940. }
  941. void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
  942. ERR_FAIL_COND(p_ev.is_null());
  943. if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) {
  944. return;
  945. }
  946. Ref<InputEventMouseMotion> mm = p_ev;
  947. if (mm.is_valid() && dragging) {
  948. if (!moving_selection) {
  949. emit_signal(SNAME("begin_node_move"));
  950. moving_selection = true;
  951. }
  952. just_selected = true;
  953. drag_accum += mm->get_relative();
  954. for (int i = get_child_count() - 1; i >= 0; i--) {
  955. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  956. if (graph_element && graph_element->is_selected() && graph_element->is_draggable()) {
  957. Vector2 pos = (graph_element->get_drag_from() * zoom + drag_accum) / zoom;
  958. // Snapping can be toggled temporarily by holding down Ctrl.
  959. // This is done here as to not toggle the grid when holding down Ctrl.
  960. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  961. pos = pos.snapped(Vector2(snapping_distance, snapping_distance));
  962. }
  963. graph_element->set_position_offset(pos);
  964. }
  965. }
  966. }
  967. if (mm.is_valid() && box_selecting) {
  968. box_selecting_to = mm->get_position();
  969. box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());
  970. for (int i = get_child_count() - 1; i >= 0; i--) {
  971. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  972. if (!graph_element) {
  973. continue;
  974. }
  975. Rect2 r = graph_element->get_rect();
  976. bool in_box = r.intersects(box_selecting_rect);
  977. if (in_box) {
  978. graph_element->set_selected(box_selection_mode_additive);
  979. } else {
  980. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  981. }
  982. }
  983. top_layer->queue_redraw();
  984. minimap->queue_redraw();
  985. }
  986. Ref<InputEventMouseButton> mb = p_ev;
  987. if (mb.is_valid()) {
  988. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  989. if (box_selecting) {
  990. box_selecting = false;
  991. for (int i = get_child_count() - 1; i >= 0; i--) {
  992. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  993. if (!graph_element) {
  994. continue;
  995. }
  996. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  997. }
  998. top_layer->queue_redraw();
  999. minimap->queue_redraw();
  1000. } else {
  1001. if (connecting) {
  1002. force_connection_drag_end();
  1003. } else {
  1004. emit_signal(SNAME("popup_request"), mb->get_position());
  1005. }
  1006. }
  1007. }
  1008. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && dragging) {
  1009. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1010. // Deselect current node.
  1011. for (int i = get_child_count() - 1; i >= 0; i--) {
  1012. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1013. if (graph_element) {
  1014. Rect2 r = graph_element->get_rect();
  1015. if (r.has_point(mb->get_position())) {
  1016. graph_element->set_selected(false);
  1017. }
  1018. }
  1019. }
  1020. }
  1021. if (drag_accum != Vector2()) {
  1022. for (int i = get_child_count() - 1; i >= 0; i--) {
  1023. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1024. if (graph_element && graph_element->is_selected()) {
  1025. graph_element->set_drag(false);
  1026. }
  1027. }
  1028. }
  1029. if (moving_selection) {
  1030. emit_signal(SNAME("end_node_move"));
  1031. moving_selection = false;
  1032. }
  1033. dragging = false;
  1034. top_layer->queue_redraw();
  1035. minimap->queue_redraw();
  1036. queue_redraw();
  1037. connections_layer->queue_redraw();
  1038. }
  1039. // Node selection logic.
  1040. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  1041. GraphElement *graph_element = nullptr;
  1042. // Find node which was clicked on.
  1043. for (int i = get_child_count() - 1; i >= 0; i--) {
  1044. GraphElement *selected_element = Object::cast_to<GraphElement>(get_child(i));
  1045. if (!selected_element) {
  1046. continue;
  1047. }
  1048. if (selected_element->is_resizing()) {
  1049. continue;
  1050. }
  1051. if (selected_element->has_point((mb->get_position() - selected_element->get_position()) / zoom)) {
  1052. graph_element = selected_element;
  1053. break;
  1054. }
  1055. }
  1056. if (graph_element) {
  1057. if (_filter_input(mb->get_position())) {
  1058. return;
  1059. }
  1060. // Left-clicked on a node, select it.
  1061. dragging = true;
  1062. drag_accum = Vector2();
  1063. just_selected = !graph_element->is_selected();
  1064. if (!graph_element->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1065. for (int i = 0; i < get_child_count(); i++) {
  1066. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1067. if (!child_element) {
  1068. continue;
  1069. }
  1070. child_element->set_selected(child_element == graph_element);
  1071. }
  1072. }
  1073. graph_element->set_selected(true);
  1074. for (int i = 0; i < get_child_count(); i++) {
  1075. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1076. if (!child_element) {
  1077. continue;
  1078. }
  1079. if (child_element->is_selected()) {
  1080. child_element->set_drag(true);
  1081. }
  1082. }
  1083. } else {
  1084. if (_filter_input(mb->get_position())) {
  1085. return;
  1086. }
  1087. if (panner->is_panning()) {
  1088. return;
  1089. }
  1090. // Left-clicked on empty space, start box select.
  1091. box_selecting = true;
  1092. box_selecting_from = mb->get_position();
  1093. if (mb->is_ctrl_pressed()) {
  1094. box_selection_mode_additive = true;
  1095. prev_selected.clear();
  1096. for (int i = get_child_count() - 1; i >= 0; i--) {
  1097. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1098. if (!child_element || !child_element->is_selected()) {
  1099. continue;
  1100. }
  1101. prev_selected.push_back(child_element);
  1102. }
  1103. } else if (mb->is_shift_pressed()) {
  1104. box_selection_mode_additive = false;
  1105. prev_selected.clear();
  1106. for (int i = get_child_count() - 1; i >= 0; i--) {
  1107. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1108. if (!child_element || !child_element->is_selected()) {
  1109. continue;
  1110. }
  1111. prev_selected.push_back(child_element);
  1112. }
  1113. } else {
  1114. box_selection_mode_additive = true;
  1115. prev_selected.clear();
  1116. for (int i = get_child_count() - 1; i >= 0; i--) {
  1117. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1118. if (!child_element) {
  1119. continue;
  1120. }
  1121. child_element->set_selected(false);
  1122. }
  1123. }
  1124. }
  1125. }
  1126. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
  1127. // Box selection ended. Nodes were selected during mouse movement.
  1128. box_selecting = false;
  1129. box_selecting_rect = Rect2();
  1130. prev_selected.clear();
  1131. top_layer->queue_redraw();
  1132. minimap->queue_redraw();
  1133. }
  1134. }
  1135. if (p_ev->is_pressed()) {
  1136. if (p_ev->is_action("ui_graph_duplicate", true)) {
  1137. emit_signal(SNAME("duplicate_nodes_request"));
  1138. accept_event();
  1139. } else if (p_ev->is_action("ui_copy", true)) {
  1140. emit_signal(SNAME("copy_nodes_request"));
  1141. accept_event();
  1142. } else if (p_ev->is_action("ui_paste", true)) {
  1143. emit_signal(SNAME("paste_nodes_request"));
  1144. accept_event();
  1145. } else if (p_ev->is_action("ui_graph_delete", true)) {
  1146. TypedArray<StringName> nodes;
  1147. for (int i = 0; i < get_child_count(); i++) {
  1148. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1149. if (!gn) {
  1150. continue;
  1151. }
  1152. if (gn->is_selected()) {
  1153. nodes.push_back(gn->get_name());
  1154. }
  1155. }
  1156. emit_signal(SNAME("close_nodes_request"), nodes);
  1157. accept_event();
  1158. }
  1159. }
  1160. }
  1161. void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1162. h_scrollbar->set_value(h_scrollbar->get_value() - p_scroll_vec.x);
  1163. v_scrollbar->set_value(v_scrollbar->get_value() - p_scroll_vec.y);
  1164. }
  1165. void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1166. set_zoom_custom(zoom * p_zoom_factor, p_origin);
  1167. }
  1168. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1169. for (Connection &E : connections) {
  1170. if (E.from_node == p_from && E.from_port == p_from_port && E.to_node == p_to && E.to_port == p_to_port) {
  1171. if (Math::is_equal_approx(E.activity, p_activity)) {
  1172. // Update only if changed.
  1173. top_layer->queue_redraw();
  1174. minimap->queue_redraw();
  1175. connections_layer->queue_redraw();
  1176. }
  1177. E.activity = p_activity;
  1178. return;
  1179. }
  1180. }
  1181. }
  1182. void GraphEdit::clear_connections() {
  1183. connections.clear();
  1184. minimap->queue_redraw();
  1185. queue_redraw();
  1186. connections_layer->queue_redraw();
  1187. }
  1188. void GraphEdit::force_connection_drag_end() {
  1189. ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
  1190. connecting = false;
  1191. connecting_valid = false;
  1192. top_layer->queue_redraw();
  1193. minimap->queue_redraw();
  1194. queue_redraw();
  1195. connections_layer->queue_redraw();
  1196. emit_signal(SNAME("connection_drag_ended"));
  1197. }
  1198. bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) {
  1199. bool valid = true;
  1200. GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid);
  1201. return valid;
  1202. }
  1203. void GraphEdit::set_panning_scheme(PanningScheme p_scheme) {
  1204. panning_scheme = p_scheme;
  1205. panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme);
  1206. }
  1207. GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const {
  1208. return panning_scheme;
  1209. }
  1210. void GraphEdit::set_zoom(float p_zoom) {
  1211. set_zoom_custom(p_zoom, get_size() / 2);
  1212. }
  1213. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1214. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1215. if (zoom == p_zoom) {
  1216. return;
  1217. }
  1218. Vector2 scrollbar_offset = (Vector2(h_scrollbar->get_value(), v_scrollbar->get_value()) + p_center) / zoom;
  1219. zoom = p_zoom;
  1220. top_layer->queue_redraw();
  1221. zoom_minus_button->set_disabled(zoom == zoom_min);
  1222. zoom_plus_button->set_disabled(zoom == zoom_max);
  1223. _update_scroll();
  1224. minimap->queue_redraw();
  1225. connections_layer->queue_redraw();
  1226. if (is_visible_in_tree()) {
  1227. Vector2 offset = scrollbar_offset * zoom - p_center;
  1228. h_scrollbar->set_value(offset.x);
  1229. v_scrollbar->set_value(offset.y);
  1230. }
  1231. _update_zoom_label();
  1232. queue_redraw();
  1233. }
  1234. float GraphEdit::get_zoom() const {
  1235. return zoom;
  1236. }
  1237. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1238. p_zoom_step = abs(p_zoom_step);
  1239. ERR_FAIL_COND(!isfinite(p_zoom_step));
  1240. if (zoom_step == p_zoom_step) {
  1241. return;
  1242. }
  1243. zoom_step = p_zoom_step;
  1244. panner->set_scroll_zoom_factor(zoom_step);
  1245. }
  1246. float GraphEdit::get_zoom_step() const {
  1247. return zoom_step;
  1248. }
  1249. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1250. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1251. if (zoom_min == p_zoom_min) {
  1252. return;
  1253. }
  1254. zoom_min = p_zoom_min;
  1255. set_zoom(zoom);
  1256. }
  1257. float GraphEdit::get_zoom_min() const {
  1258. return zoom_min;
  1259. }
  1260. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1261. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1262. if (zoom_max == p_zoom_max) {
  1263. return;
  1264. }
  1265. zoom_max = p_zoom_max;
  1266. set_zoom(zoom);
  1267. }
  1268. float GraphEdit::get_zoom_max() const {
  1269. return zoom_max;
  1270. }
  1271. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1272. if (zoom_label->is_visible() == p_enable) {
  1273. return;
  1274. }
  1275. zoom_label->set_visible(p_enable);
  1276. }
  1277. bool GraphEdit::is_showing_zoom_label() const {
  1278. return zoom_label->is_visible();
  1279. }
  1280. void GraphEdit::set_right_disconnects(bool p_enable) {
  1281. right_disconnects = p_enable;
  1282. }
  1283. bool GraphEdit::is_right_disconnects_enabled() const {
  1284. return right_disconnects;
  1285. }
  1286. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1287. valid_right_disconnect_types.insert(p_type);
  1288. }
  1289. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1290. valid_right_disconnect_types.erase(p_type);
  1291. }
  1292. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1293. valid_left_disconnect_types.insert(p_type);
  1294. }
  1295. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1296. valid_left_disconnect_types.erase(p_type);
  1297. }
  1298. TypedArray<Dictionary> GraphEdit::_get_connection_list() const {
  1299. List<Connection> conns;
  1300. get_connection_list(&conns);
  1301. TypedArray<Dictionary> arr;
  1302. for (const Connection &E : conns) {
  1303. Dictionary d;
  1304. d["from_node"] = E.from_node;
  1305. d["from_port"] = E.from_port;
  1306. d["to_node"] = E.to_node;
  1307. d["to_port"] = E.to_port;
  1308. arr.push_back(d);
  1309. }
  1310. return arr;
  1311. }
  1312. void GraphEdit::_zoom_minus() {
  1313. set_zoom(zoom / zoom_step);
  1314. }
  1315. void GraphEdit::_zoom_reset() {
  1316. set_zoom(1);
  1317. }
  1318. void GraphEdit::_zoom_plus() {
  1319. set_zoom(zoom * zoom_step);
  1320. }
  1321. void GraphEdit::_update_zoom_label() {
  1322. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1323. String zoom_text = itos(zoom_percent) + "%";
  1324. zoom_label->set_text(zoom_text);
  1325. }
  1326. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1327. ConnectionType ct(p_type, p_with_type);
  1328. valid_connection_types.insert(ct);
  1329. }
  1330. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1331. ConnectionType ct(p_type, p_with_type);
  1332. valid_connection_types.erase(ct);
  1333. }
  1334. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1335. ConnectionType ct(p_type, p_with_type);
  1336. return valid_connection_types.has(ct);
  1337. }
  1338. void GraphEdit::set_snapping_enabled(bool p_enable) {
  1339. if (snapping_enabled == p_enable) {
  1340. return;
  1341. }
  1342. snapping_enabled = p_enable;
  1343. toggle_snapping_button->set_pressed(p_enable);
  1344. queue_redraw();
  1345. }
  1346. bool GraphEdit::is_snapping_enabled() const {
  1347. return snapping_enabled;
  1348. }
  1349. void GraphEdit::set_snapping_distance(int p_snapping_distance) {
  1350. ERR_FAIL_COND_MSG(p_snapping_distance < GRID_MIN_SNAPPING_DISTANCE || p_snapping_distance > GRID_MAX_SNAPPING_DISTANCE,
  1351. vformat("GraphEdit's snapping distance must be between %d and %d (inclusive)", GRID_MIN_SNAPPING_DISTANCE, GRID_MAX_SNAPPING_DISTANCE));
  1352. snapping_distance = p_snapping_distance;
  1353. snapping_distance_spinbox->set_value(p_snapping_distance);
  1354. queue_redraw();
  1355. }
  1356. int GraphEdit::get_snapping_distance() const {
  1357. return snapping_distance;
  1358. }
  1359. void GraphEdit::set_show_grid(bool p_show) {
  1360. if (show_grid == p_show) {
  1361. return;
  1362. }
  1363. show_grid = p_show;
  1364. show_grid_button->set_pressed(p_show);
  1365. queue_redraw();
  1366. }
  1367. bool GraphEdit::is_showing_grid() const {
  1368. return show_grid;
  1369. }
  1370. void GraphEdit::_snapping_toggled() {
  1371. snapping_enabled = toggle_snapping_button->is_pressed();
  1372. }
  1373. void GraphEdit::_snapping_distance_changed(double) {
  1374. snapping_distance = snapping_distance_spinbox->get_value();
  1375. queue_redraw();
  1376. }
  1377. void GraphEdit::_show_grid_toggled() {
  1378. show_grid = show_grid_button->is_pressed();
  1379. queue_redraw();
  1380. }
  1381. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1382. minimap->set_size(p_size);
  1383. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1384. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1385. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  1386. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  1387. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1388. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1389. minimap->queue_redraw();
  1390. }
  1391. Vector2 GraphEdit::get_minimap_size() const {
  1392. return minimap->get_size();
  1393. }
  1394. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1395. if (minimap->get_modulate().a == p_opacity) {
  1396. return;
  1397. }
  1398. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1399. minimap->queue_redraw();
  1400. }
  1401. float GraphEdit::get_minimap_opacity() const {
  1402. Color minimap_modulate = minimap->get_modulate();
  1403. return minimap_modulate.a;
  1404. }
  1405. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1406. if (minimap_button->is_pressed() == p_enable) {
  1407. return;
  1408. }
  1409. minimap_button->set_pressed(p_enable);
  1410. _minimap_toggled();
  1411. minimap->queue_redraw();
  1412. }
  1413. bool GraphEdit::is_minimap_enabled() const {
  1414. return minimap_button->is_pressed();
  1415. }
  1416. void GraphEdit::set_arrange_nodes_button_hidden(bool p_enable) {
  1417. arrange_nodes_button_hidden = p_enable;
  1418. if (arrange_nodes_button_hidden) {
  1419. layout_button->hide();
  1420. } else {
  1421. layout_button->show();
  1422. }
  1423. }
  1424. bool GraphEdit::is_arrange_nodes_button_hidden() const {
  1425. return arrange_nodes_button_hidden;
  1426. }
  1427. void GraphEdit::_minimap_toggled() {
  1428. if (is_minimap_enabled()) {
  1429. minimap->set_visible(true);
  1430. minimap->queue_redraw();
  1431. } else {
  1432. minimap->set_visible(false);
  1433. }
  1434. }
  1435. void GraphEdit::set_connection_lines_curvature(float p_curvature) {
  1436. lines_curvature = p_curvature;
  1437. queue_redraw();
  1438. }
  1439. float GraphEdit::get_connection_lines_curvature() const {
  1440. return lines_curvature;
  1441. }
  1442. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  1443. if (lines_thickness == p_thickness) {
  1444. return;
  1445. }
  1446. lines_thickness = p_thickness;
  1447. queue_redraw();
  1448. }
  1449. float GraphEdit::get_connection_lines_thickness() const {
  1450. return lines_thickness;
  1451. }
  1452. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  1453. if (lines_antialiased == p_antialiased) {
  1454. return;
  1455. }
  1456. lines_antialiased = p_antialiased;
  1457. queue_redraw();
  1458. }
  1459. bool GraphEdit::is_connection_lines_antialiased() const {
  1460. return lines_antialiased;
  1461. }
  1462. HBoxContainer *GraphEdit::get_menu_hbox() {
  1463. return menu_hbox;
  1464. }
  1465. Ref<ViewPanner> GraphEdit::get_panner() {
  1466. return panner;
  1467. }
  1468. void GraphEdit::set_warped_panning(bool p_warped) {
  1469. warped_panning = p_warped;
  1470. }
  1471. void GraphEdit::arrange_nodes() {
  1472. arranger->arrange_nodes();
  1473. }
  1474. void GraphEdit::_bind_methods() {
  1475. ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::connect_node);
  1476. ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
  1477. ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
  1478. ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1479. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1480. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1481. ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end);
  1482. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &GraphEdit::get_scroll_offset);
  1483. ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &GraphEdit::set_scroll_offset);
  1484. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1485. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1486. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1487. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1488. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1489. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1490. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1491. ClassDB::bind_method(D_METHOD("get_connection_line", "from_node", "to_node"), &GraphEdit::get_connection_line);
  1492. ClassDB::bind_method(D_METHOD("set_panning_scheme", "scheme"), &GraphEdit::set_panning_scheme);
  1493. ClassDB::bind_method(D_METHOD("get_panning_scheme"), &GraphEdit::get_panning_scheme);
  1494. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  1495. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1496. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1497. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1498. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1499. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1500. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1501. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1502. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1503. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1504. ClassDB::bind_method(D_METHOD("set_show_grid", "enable"), &GraphEdit::set_show_grid);
  1505. ClassDB::bind_method(D_METHOD("is_showing_grid"), &GraphEdit::is_showing_grid);
  1506. ClassDB::bind_method(D_METHOD("set_snapping_enabled", "enable"), &GraphEdit::set_snapping_enabled);
  1507. ClassDB::bind_method(D_METHOD("is_snapping_enabled"), &GraphEdit::is_snapping_enabled);
  1508. ClassDB::bind_method(D_METHOD("set_snapping_distance", "pixels"), &GraphEdit::set_snapping_distance);
  1509. ClassDB::bind_method(D_METHOD("get_snapping_distance"), &GraphEdit::get_snapping_distance);
  1510. ClassDB::bind_method(D_METHOD("set_connection_lines_curvature", "curvature"), &GraphEdit::set_connection_lines_curvature);
  1511. ClassDB::bind_method(D_METHOD("get_connection_lines_curvature"), &GraphEdit::get_connection_lines_curvature);
  1512. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  1513. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  1514. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  1515. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  1516. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  1517. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1518. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  1519. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1520. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1521. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1522. ClassDB::bind_method(D_METHOD("set_arrange_nodes_button_hidden", "enable"), &GraphEdit::set_arrange_nodes_button_hidden);
  1523. ClassDB::bind_method(D_METHOD("is_arrange_nodes_button_hidden"), &GraphEdit::is_arrange_nodes_button_hidden);
  1524. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1525. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1526. GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node", "in_port", "mouse_position");
  1527. GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node", "in_port", "mouse_position");
  1528. ClassDB::bind_method(D_METHOD("get_menu_hbox"), &GraphEdit::get_menu_hbox);
  1529. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  1530. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1531. GDVIRTUAL_BIND(_get_connection_line, "from_position", "to_position")
  1532. GDVIRTUAL_BIND(_is_node_hover_valid, "from_node", "from_port", "to_node", "to_port");
  1533. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1534. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
  1535. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid"), "set_show_grid", "is_showing_grid");
  1536. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snapping_enabled"), "set_snapping_enabled", "is_snapping_enabled");
  1537. ADD_PROPERTY(PropertyInfo(Variant::INT, "snapping_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snapping_distance", "get_snapping_distance");
  1538. ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
  1539. ADD_GROUP("Connection Lines", "connection_lines");
  1540. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
  1541. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness", PROPERTY_HINT_NONE, "suffix:px"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  1542. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  1543. ADD_GROUP("Zoom", "");
  1544. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1545. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  1546. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  1547. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  1548. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  1549. ADD_GROUP("Minimap", "minimap_");
  1550. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  1551. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_size", "get_minimap_size");
  1552. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  1553. ADD_GROUP("UI", "");
  1554. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arrange_nodes_button_hidden"), "set_arrange_nodes_button_hidden", "is_arrange_nodes_button_hidden");
  1555. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  1556. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  1557. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1558. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1559. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1560. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1561. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1562. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1563. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1564. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1565. ADD_SIGNAL(MethodInfo("close_nodes_request", PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_ARRAY_TYPE, "StringName")));
  1566. ADD_SIGNAL(MethodInfo("begin_node_move"));
  1567. ADD_SIGNAL(MethodInfo("end_node_move"));
  1568. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "offset")));
  1569. ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output")));
  1570. ADD_SIGNAL(MethodInfo("connection_drag_ended"));
  1571. BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
  1572. BIND_ENUM_CONSTANT(SCROLL_PANS);
  1573. }
  1574. GraphEdit::GraphEdit() {
  1575. set_focus_mode(FOCUS_ALL);
  1576. // Allow dezooming 8 times from the default zoom level.
  1577. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  1578. // but this is still useful for previewing and navigation.
  1579. zoom_min = (1 / Math::pow(zoom_step, 8));
  1580. // Allow zooming 4 times from the default zoom level.
  1581. zoom_max = (1 * Math::pow(zoom_step, 4));
  1582. panner.instantiate();
  1583. panner->set_callbacks(callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback));
  1584. top_layer = memnew(GraphEditFilter(this));
  1585. add_child(top_layer, false, INTERNAL_MODE_BACK);
  1586. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1587. top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1588. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1589. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1590. top_layer->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  1591. connections_layer = memnew(Control);
  1592. add_child(connections_layer, false);
  1593. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1594. connections_layer->set_name("_connection_layer");
  1595. connections_layer->set_disable_visibility_clip(true); // Necessary, so it can draw freely and be offset.
  1596. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1597. h_scrollbar = memnew(HScrollBar);
  1598. h_scrollbar->set_name("_h_scroll");
  1599. top_layer->add_child(h_scrollbar);
  1600. v_scrollbar = memnew(VScrollBar);
  1601. v_scrollbar->set_name("_v_scroll");
  1602. top_layer->add_child(v_scrollbar);
  1603. // Set large minmax so it can scroll even if not resized yet.
  1604. h_scrollbar->set_min(-10000);
  1605. h_scrollbar->set_max(10000);
  1606. v_scrollbar->set_min(-10000);
  1607. v_scrollbar->set_max(10000);
  1608. h_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1609. v_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1610. menu_hbox = memnew(HBoxContainer);
  1611. top_layer->add_child(menu_hbox);
  1612. menu_hbox->set_position(Vector2(10, 10));
  1613. zoom_label = memnew(Label);
  1614. menu_hbox->add_child(zoom_label);
  1615. zoom_label->set_visible(false);
  1616. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1617. zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1618. zoom_label->set_custom_minimum_size(Size2(48, 0));
  1619. _update_zoom_label();
  1620. zoom_minus_button = memnew(Button);
  1621. zoom_minus_button->set_flat(true);
  1622. menu_hbox->add_child(zoom_minus_button);
  1623. zoom_minus_button->set_tooltip_text(RTR("Zoom Out"));
  1624. zoom_minus_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1625. zoom_minus_button->set_focus_mode(FOCUS_NONE);
  1626. zoom_reset_button = memnew(Button);
  1627. zoom_reset_button->set_flat(true);
  1628. menu_hbox->add_child(zoom_reset_button);
  1629. zoom_reset_button->set_tooltip_text(RTR("Zoom Reset"));
  1630. zoom_reset_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1631. zoom_reset_button->set_focus_mode(FOCUS_NONE);
  1632. zoom_plus_button = memnew(Button);
  1633. zoom_plus_button->set_flat(true);
  1634. menu_hbox->add_child(zoom_plus_button);
  1635. zoom_plus_button->set_tooltip_text(RTR("Zoom In"));
  1636. zoom_plus_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1637. zoom_plus_button->set_focus_mode(FOCUS_NONE);
  1638. show_grid_button = memnew(Button);
  1639. show_grid_button->set_flat(true);
  1640. show_grid_button->set_toggle_mode(true);
  1641. show_grid_button->set_tooltip_text(RTR("Toggle the visual grid."));
  1642. show_grid_button->connect("pressed", callable_mp(this, &GraphEdit::_show_grid_toggled));
  1643. show_grid_button->set_pressed(true);
  1644. show_grid_button->set_focus_mode(FOCUS_NONE);
  1645. menu_hbox->add_child(show_grid_button);
  1646. toggle_snapping_button = memnew(Button);
  1647. toggle_snapping_button->set_flat(true);
  1648. toggle_snapping_button->set_toggle_mode(true);
  1649. toggle_snapping_button->set_tooltip_text(RTR("Toggle snapping to the grid."));
  1650. toggle_snapping_button->connect("pressed", callable_mp(this, &GraphEdit::_snapping_toggled));
  1651. toggle_snapping_button->set_pressed(snapping_enabled);
  1652. toggle_snapping_button->set_focus_mode(FOCUS_NONE);
  1653. menu_hbox->add_child(toggle_snapping_button);
  1654. snapping_distance_spinbox = memnew(SpinBox);
  1655. snapping_distance_spinbox->set_min(GRID_MIN_SNAPPING_DISTANCE);
  1656. snapping_distance_spinbox->set_max(GRID_MAX_SNAPPING_DISTANCE);
  1657. snapping_distance_spinbox->set_step(1);
  1658. snapping_distance_spinbox->set_value(snapping_distance);
  1659. snapping_distance_spinbox->set_tooltip_text(RTR("Change the snapping distance."));
  1660. snapping_distance_spinbox->connect("value_changed", callable_mp(this, &GraphEdit::_snapping_distance_changed));
  1661. menu_hbox->add_child(snapping_distance_spinbox);
  1662. minimap_button = memnew(Button);
  1663. minimap_button->set_flat(true);
  1664. minimap_button->set_toggle_mode(true);
  1665. minimap_button->set_tooltip_text(RTR("Toggle the graph minimap."));
  1666. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  1667. minimap_button->set_pressed(show_grid);
  1668. minimap_button->set_focus_mode(FOCUS_NONE);
  1669. menu_hbox->add_child(minimap_button);
  1670. layout_button = memnew(Button);
  1671. layout_button->set_flat(true);
  1672. menu_hbox->add_child(layout_button);
  1673. layout_button->set_tooltip_text(RTR("Automatically arrange selected nodes."));
  1674. layout_button->connect("pressed", callable_mp(this, &GraphEdit::arrange_nodes));
  1675. layout_button->set_focus_mode(FOCUS_NONE);
  1676. Vector2 minimap_size = Vector2(240, 160);
  1677. float minimap_opacity = 0.65;
  1678. minimap = memnew(GraphEditMinimap(this));
  1679. top_layer->add_child(minimap);
  1680. minimap->set_name("_minimap");
  1681. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  1682. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  1683. minimap->set_custom_minimum_size(Vector2(50, 50));
  1684. minimap->set_size(minimap_size);
  1685. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1686. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  1687. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  1688. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1689. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1690. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  1691. set_clip_contents(true);
  1692. arranger = Ref<GraphEditArranger>(memnew(GraphEditArranger(this)));
  1693. }