graph_edit.cpp 77 KB

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