graph_edit.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "graph_edit.h"
  31. #include "core/input/input.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/keyboard.h"
  34. #include "scene/gui/box_container.h"
  35. #include "scene/gui/button.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/editor_scale.h"
  38. #endif
  39. constexpr int MINIMAP_OFFSET = 12;
  40. constexpr int MINIMAP_PADDING = 5;
  41. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  42. return ge->_filter_input(p_point);
  43. }
  44. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  45. ge = p_edit;
  46. }
  47. void GraphEditMinimap::_bind_methods() {
  48. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEditMinimap::_gui_input);
  49. }
  50. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  51. ge = p_edit;
  52. graph_proportions = Vector2(1, 1);
  53. graph_padding = Vector2(0, 0);
  54. camera_position = Vector2(100, 50);
  55. camera_size = Vector2(200, 200);
  56. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  57. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  58. is_pressing = false;
  59. is_resizing = false;
  60. }
  61. void GraphEditMinimap::update_minimap() {
  62. Vector2 graph_offset = _get_graph_offset();
  63. Vector2 graph_size = _get_graph_size();
  64. camera_position = ge->get_scroll_ofs() - graph_offset;
  65. camera_size = ge->get_size();
  66. Vector2 render_size = _get_render_size();
  67. float target_ratio = render_size.x / render_size.y;
  68. float graph_ratio = graph_size.x / graph_size.y;
  69. graph_proportions = graph_size;
  70. graph_padding = Vector2(0, 0);
  71. if (graph_ratio > target_ratio) {
  72. graph_proportions.x = graph_size.x;
  73. graph_proportions.y = graph_size.x / target_ratio;
  74. graph_padding.y = Math::abs(graph_size.y - graph_proportions.y) / 2;
  75. } else {
  76. graph_proportions.x = graph_size.y * target_ratio;
  77. graph_proportions.y = graph_size.y;
  78. graph_padding.x = Math::abs(graph_size.x - graph_proportions.x) / 2;
  79. }
  80. // This centers minimap inside the minimap rectangle.
  81. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  82. }
  83. Rect2 GraphEditMinimap::get_camera_rect() {
  84. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  85. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  86. Vector2 camera_position = (camera_center - camera_viewport / 2);
  87. return Rect2(camera_position, camera_viewport);
  88. }
  89. Vector2 GraphEditMinimap::_get_render_size() {
  90. if (!is_inside_tree()) {
  91. return Vector2(0, 0);
  92. }
  93. return get_size() - 2 * minimap_padding;
  94. }
  95. Vector2 GraphEditMinimap::_get_graph_offset() {
  96. return Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  97. }
  98. Vector2 GraphEditMinimap::_get_graph_size() {
  99. Vector2 graph_size = Vector2(ge->h_scroll->get_max(), ge->v_scroll->get_max()) - Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  100. if (graph_size.x == 0) {
  101. graph_size.x = 1;
  102. }
  103. if (graph_size.y == 0) {
  104. graph_size.y = 1;
  105. }
  106. return graph_size;
  107. }
  108. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  109. Vector2 map_position = Vector2(0, 0);
  110. Vector2 render_size = _get_render_size();
  111. map_position.x = p_position.x * render_size.x / graph_proportions.x;
  112. map_position.y = p_position.y * render_size.y / graph_proportions.y;
  113. return map_position;
  114. }
  115. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  116. Vector2 graph_position = Vector2(0, 0);
  117. Vector2 render_size = _get_render_size();
  118. graph_position.x = p_position.x * graph_proportions.x / render_size.x;
  119. graph_position.y = p_position.y * graph_proportions.y / render_size.y;
  120. return graph_position;
  121. }
  122. void GraphEditMinimap::_gui_input(const Ref<InputEvent> &p_ev) {
  123. ERR_FAIL_COND(p_ev.is_null());
  124. if (!ge->is_minimap_enabled()) {
  125. return;
  126. }
  127. Ref<InputEventMouseButton> mb = p_ev;
  128. Ref<InputEventMouseMotion> mm = p_ev;
  129. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  130. if (mb->is_pressed()) {
  131. is_pressing = true;
  132. Ref<Texture2D> resizer = get_theme_icon("resizer");
  133. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  134. if (resizer_hitbox.has_point(mb->get_position())) {
  135. is_resizing = true;
  136. } else {
  137. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  138. _adjust_graph_scroll(click_position);
  139. }
  140. } else {
  141. is_pressing = false;
  142. is_resizing = false;
  143. }
  144. accept_event();
  145. } else if (mm.is_valid() && is_pressing) {
  146. if (is_resizing) {
  147. // Prevent setting minimap wider than GraphEdit
  148. Vector2 new_minimap_size;
  149. new_minimap_size.x = MIN(get_size().x - mm->get_relative().x, ge->get_size().x - 2.0 * minimap_padding.x);
  150. new_minimap_size.y = MIN(get_size().y - mm->get_relative().y, ge->get_size().y - 2.0 * minimap_padding.y);
  151. ge->set_minimap_size(new_minimap_size);
  152. update();
  153. } else {
  154. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  155. _adjust_graph_scroll(click_position);
  156. }
  157. accept_event();
  158. }
  159. }
  160. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  161. Vector2 graph_offset = _get_graph_offset();
  162. ge->set_scroll_ofs(p_offset + graph_offset - camera_size / 2);
  163. }
  164. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  165. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  166. return OK;
  167. }
  168. Connection c;
  169. c.from = p_from;
  170. c.from_port = p_from_port;
  171. c.to = p_to;
  172. c.to_port = p_to_port;
  173. c.activity = 0;
  174. connections.push_back(c);
  175. top_layer->update();
  176. minimap->update();
  177. update();
  178. connections_layer->update();
  179. return OK;
  180. }
  181. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  182. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  183. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  190. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  191. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  192. connections.erase(E);
  193. top_layer->update();
  194. minimap->update();
  195. update();
  196. connections_layer->update();
  197. return;
  198. }
  199. }
  200. }
  201. bool GraphEdit::clips_input() const {
  202. return true;
  203. }
  204. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  205. *r_connections = connections;
  206. }
  207. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  208. setting_scroll_ofs = true;
  209. h_scroll->set_value(p_ofs.x);
  210. v_scroll->set_value(p_ofs.y);
  211. _update_scroll();
  212. setting_scroll_ofs = false;
  213. }
  214. Vector2 GraphEdit::get_scroll_ofs() const {
  215. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  216. }
  217. void GraphEdit::_scroll_moved(double) {
  218. if (!awaiting_scroll_offset_update) {
  219. call_deferred("_update_scroll_offset");
  220. awaiting_scroll_offset_update = true;
  221. }
  222. top_layer->update();
  223. minimap->update();
  224. update();
  225. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  226. emit_signal("scroll_offset_changed", get_scroll_ofs());
  227. }
  228. }
  229. void GraphEdit::_update_scroll_offset() {
  230. set_block_minimum_size_adjust(true);
  231. for (int i = 0; i < get_child_count(); i++) {
  232. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  233. if (!gn) {
  234. continue;
  235. }
  236. Point2 pos = gn->get_position_offset() * zoom;
  237. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  238. gn->set_position(pos);
  239. if (gn->get_scale() != Vector2(zoom, zoom)) {
  240. gn->set_scale(Vector2(zoom, zoom));
  241. }
  242. }
  243. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  244. set_block_minimum_size_adjust(false);
  245. awaiting_scroll_offset_update = false;
  246. }
  247. void GraphEdit::_update_scroll() {
  248. if (updating) {
  249. return;
  250. }
  251. updating = true;
  252. set_block_minimum_size_adjust(true);
  253. Rect2 screen;
  254. for (int i = 0; i < get_child_count(); i++) {
  255. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  256. if (!gn) {
  257. continue;
  258. }
  259. Rect2 r;
  260. r.position = gn->get_position_offset() * zoom;
  261. r.size = gn->get_size() * zoom;
  262. screen = screen.merge(r);
  263. }
  264. screen.position -= get_size();
  265. screen.size += get_size() * 2.0;
  266. h_scroll->set_min(screen.position.x);
  267. h_scroll->set_max(screen.position.x + screen.size.x);
  268. h_scroll->set_page(get_size().x);
  269. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  270. h_scroll->hide();
  271. } else {
  272. h_scroll->show();
  273. }
  274. v_scroll->set_min(screen.position.y);
  275. v_scroll->set_max(screen.position.y + screen.size.y);
  276. v_scroll->set_page(get_size().y);
  277. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  278. v_scroll->hide();
  279. } else {
  280. v_scroll->show();
  281. }
  282. Size2 hmin = h_scroll->get_combined_minimum_size();
  283. Size2 vmin = v_scroll->get_combined_minimum_size();
  284. // Avoid scrollbar overlapping.
  285. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  286. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  287. set_block_minimum_size_adjust(false);
  288. if (!awaiting_scroll_offset_update) {
  289. call_deferred("_update_scroll_offset");
  290. awaiting_scroll_offset_update = true;
  291. }
  292. updating = false;
  293. }
  294. void GraphEdit::_graph_node_raised(Node *p_gn) {
  295. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  296. ERR_FAIL_COND(!gn);
  297. if (gn->is_comment()) {
  298. move_child(gn, 0);
  299. } else {
  300. gn->raise();
  301. }
  302. int first_not_comment = 0;
  303. for (int i = 0; i < get_child_count(); i++) {
  304. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  305. if (gn2 && !gn2->is_comment()) {
  306. first_not_comment = i;
  307. break;
  308. }
  309. }
  310. move_child(connections_layer, first_not_comment);
  311. top_layer->raise();
  312. emit_signal("node_selected", p_gn);
  313. }
  314. void GraphEdit::_graph_node_moved(Node *p_gn) {
  315. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  316. ERR_FAIL_COND(!gn);
  317. top_layer->update();
  318. minimap->update();
  319. update();
  320. connections_layer->update();
  321. }
  322. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_gn) {
  323. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  324. ERR_FAIL_COND(!gn);
  325. top_layer->update();
  326. minimap->update();
  327. update();
  328. connections_layer->update();
  329. }
  330. void GraphEdit::add_child_notify(Node *p_child) {
  331. Control::add_child_notify(p_child);
  332. top_layer->call_deferred("raise"); // Top layer always on top!
  333. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  334. if (gn) {
  335. gn->set_scale(Vector2(zoom, zoom));
  336. gn->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved), varray(gn));
  337. gn->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated), varray(gn));
  338. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised), varray(gn));
  339. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  340. gn->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::update));
  341. _graph_node_moved(gn);
  342. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  343. }
  344. }
  345. void GraphEdit::remove_child_notify(Node *p_child) {
  346. Control::remove_child_notify(p_child);
  347. if (p_child == top_layer) {
  348. top_layer = nullptr;
  349. minimap = nullptr;
  350. } else if (p_child == connections_layer) {
  351. connections_layer = nullptr;
  352. }
  353. if (top_layer != nullptr && is_inside_tree()) {
  354. top_layer->call_deferred("raise"); // Top layer always on top!
  355. }
  356. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  357. if (gn) {
  358. gn->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  359. gn->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  360. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  361. // In case of the whole GraphEdit being destroyed these references can already be freed.
  362. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  363. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  364. }
  365. if (minimap != nullptr && minimap->is_inside_tree()) {
  366. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::update));
  367. }
  368. }
  369. }
  370. void GraphEdit::_notification(int p_what) {
  371. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  372. port_grab_distance_horizontal = get_theme_constant("port_grab_distance_horizontal");
  373. port_grab_distance_vertical = get_theme_constant("port_grab_distance_vertical");
  374. zoom_minus->set_icon(get_theme_icon("minus"));
  375. zoom_reset->set_icon(get_theme_icon("reset"));
  376. zoom_plus->set_icon(get_theme_icon("more"));
  377. snap_button->set_icon(get_theme_icon("snap"));
  378. minimap_button->set_icon(get_theme_icon("minimap"));
  379. }
  380. if (p_what == NOTIFICATION_READY) {
  381. Size2 hmin = h_scroll->get_combined_minimum_size();
  382. Size2 vmin = v_scroll->get_combined_minimum_size();
  383. h_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  384. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  385. h_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  386. h_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  387. v_scroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  388. v_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  389. v_scroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  390. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  391. }
  392. if (p_what == NOTIFICATION_DRAW) {
  393. draw_style_box(get_theme_stylebox("bg"), Rect2(Point2(), get_size()));
  394. if (is_using_snap()) {
  395. //draw grid
  396. int snap = get_snap();
  397. Vector2 offset = get_scroll_ofs() / zoom;
  398. Size2 size = get_size() / zoom;
  399. Point2i from = (offset / float(snap)).floor();
  400. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  401. Color grid_minor = get_theme_color("grid_minor");
  402. Color grid_major = get_theme_color("grid_major");
  403. for (int i = from.x; i < from.x + len.x; i++) {
  404. Color color;
  405. if (ABS(i) % 10 == 0) {
  406. color = grid_major;
  407. } else {
  408. color = grid_minor;
  409. }
  410. float base_ofs = i * snap * zoom - offset.x * zoom;
  411. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  412. }
  413. for (int i = from.y; i < from.y + len.y; i++) {
  414. Color color;
  415. if (ABS(i) % 10 == 0) {
  416. color = grid_major;
  417. } else {
  418. color = grid_minor;
  419. }
  420. float base_ofs = i * snap * zoom - offset.y * zoom;
  421. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  422. }
  423. }
  424. }
  425. if (p_what == NOTIFICATION_RESIZED) {
  426. _update_scroll();
  427. top_layer->update();
  428. minimap->update();
  429. }
  430. }
  431. bool GraphEdit::_filter_input(const Point2 &p_point) {
  432. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  433. for (int i = get_child_count() - 1; i >= 0; i--) {
  434. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  435. if (!gn) {
  436. continue;
  437. }
  438. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  439. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  440. if (is_in_hot_zone(pos / zoom, p_point / zoom)) {
  441. return true;
  442. }
  443. }
  444. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  445. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  446. if (is_in_hot_zone(pos / zoom, p_point / zoom)) {
  447. return true;
  448. }
  449. }
  450. }
  451. return false;
  452. }
  453. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  454. Ref<InputEventMouseButton> mb = p_ev;
  455. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT && mb->is_pressed()) {
  456. connecting_valid = false;
  457. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  458. click_pos = mb->get_position() / zoom;
  459. for (int i = get_child_count() - 1; i >= 0; i--) {
  460. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  461. if (!gn) {
  462. continue;
  463. }
  464. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  465. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  466. if (is_in_hot_zone(pos / zoom, click_pos)) {
  467. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  468. //check disconnect
  469. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  470. if (E->get().from == gn->get_name() && E->get().from_port == j) {
  471. Node *to = get_node(String(E->get().to));
  472. if (Object::cast_to<GraphNode>(to)) {
  473. connecting_from = E->get().to;
  474. connecting_index = E->get().to_port;
  475. connecting_out = false;
  476. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
  477. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E->get().to_port);
  478. connecting_target = false;
  479. connecting_to = pos;
  480. just_disconnected = true;
  481. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  482. to = get_node(String(connecting_from)); //maybe it was erased
  483. if (Object::cast_to<GraphNode>(to)) {
  484. connecting = true;
  485. }
  486. return;
  487. }
  488. }
  489. }
  490. }
  491. connecting = true;
  492. connecting_from = gn->get_name();
  493. connecting_index = j;
  494. connecting_out = true;
  495. connecting_type = gn->get_connection_output_type(j);
  496. connecting_color = gn->get_connection_output_color(j);
  497. connecting_target = false;
  498. connecting_to = pos;
  499. just_disconnected = false;
  500. return;
  501. }
  502. }
  503. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  504. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  505. if (is_in_hot_zone(pos / zoom, click_pos)) {
  506. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  507. //check disconnect
  508. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  509. if (E->get().to == gn->get_name() && E->get().to_port == j) {
  510. Node *fr = get_node(String(E->get().from));
  511. if (Object::cast_to<GraphNode>(fr)) {
  512. connecting_from = E->get().from;
  513. connecting_index = E->get().from_port;
  514. connecting_out = true;
  515. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
  516. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E->get().from_port);
  517. connecting_target = false;
  518. connecting_to = pos;
  519. just_disconnected = true;
  520. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  521. fr = get_node(String(connecting_from)); //maybe it was erased
  522. if (Object::cast_to<GraphNode>(fr)) {
  523. connecting = true;
  524. }
  525. return;
  526. }
  527. }
  528. }
  529. }
  530. connecting = true;
  531. connecting_from = gn->get_name();
  532. connecting_index = j;
  533. connecting_out = false;
  534. connecting_type = gn->get_connection_input_type(j);
  535. connecting_color = gn->get_connection_input_color(j);
  536. connecting_target = false;
  537. connecting_to = pos;
  538. just_disconnected = false;
  539. return;
  540. }
  541. }
  542. }
  543. }
  544. Ref<InputEventMouseMotion> mm = p_ev;
  545. if (mm.is_valid() && connecting) {
  546. connecting_to = mm->get_position();
  547. connecting_target = false;
  548. top_layer->update();
  549. minimap->update();
  550. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > 20.0 * zoom;
  551. if (connecting_valid) {
  552. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  553. Vector2 mpos = mm->get_position() / zoom;
  554. for (int i = get_child_count() - 1; i >= 0; i--) {
  555. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  556. if (!gn) {
  557. continue;
  558. }
  559. if (!connecting_out) {
  560. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  561. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  562. int type = gn->get_connection_output_type(j);
  563. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos / zoom, mpos)) {
  564. connecting_target = true;
  565. connecting_to = pos;
  566. connecting_target_to = gn->get_name();
  567. connecting_target_index = j;
  568. return;
  569. }
  570. }
  571. } else {
  572. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  573. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  574. int type = gn->get_connection_input_type(j);
  575. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos / zoom, mpos)) {
  576. connecting_target = true;
  577. connecting_to = pos;
  578. connecting_target_to = gn->get_name();
  579. connecting_target_index = j;
  580. return;
  581. }
  582. }
  583. }
  584. }
  585. }
  586. }
  587. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT && !mb->is_pressed()) {
  588. if (connecting_valid) {
  589. if (connecting && connecting_target) {
  590. String from = connecting_from;
  591. int from_slot = connecting_index;
  592. String to = connecting_target_to;
  593. int to_slot = connecting_target_index;
  594. if (!connecting_out) {
  595. SWAP(from, to);
  596. SWAP(from_slot, to_slot);
  597. }
  598. emit_signal("connection_request", from, from_slot, to, to_slot);
  599. } else if (!just_disconnected) {
  600. String from = connecting_from;
  601. int from_slot = connecting_index;
  602. Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
  603. if (!connecting_out) {
  604. emit_signal("connection_from_empty", from, from_slot, ofs);
  605. } else {
  606. emit_signal("connection_to_empty", from, from_slot, ofs);
  607. }
  608. }
  609. }
  610. connecting = false;
  611. top_layer->update();
  612. minimap->update();
  613. update();
  614. connections_layer->update();
  615. }
  616. }
  617. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
  618. if (p_control->is_set_as_top_level() || !p_control->is_visible()) {
  619. return false;
  620. }
  621. if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  622. //test children
  623. for (int i = 0; i < p_control->get_child_count(); i++) {
  624. Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
  625. if (!subchild) {
  626. continue;
  627. }
  628. if (_check_clickable_control(subchild, pos - subchild->get_position())) {
  629. return true;
  630. }
  631. }
  632. return false;
  633. } else {
  634. return true;
  635. }
  636. }
  637. bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos) {
  638. if (!Rect2(pos.x - port_grab_distance_horizontal, pos.y - port_grab_distance_vertical, port_grab_distance_horizontal * 2, port_grab_distance_vertical * 2).has_point(p_mouse_pos)) {
  639. return false;
  640. }
  641. for (int i = 0; i < get_child_count(); i++) {
  642. Control *child = Object::cast_to<Control>(get_child(i));
  643. if (!child) {
  644. continue;
  645. }
  646. Rect2 rect = child->get_rect();
  647. // To prevent intersections with other nodes.
  648. rect.position *= zoom;
  649. rect.size *= zoom;
  650. if (rect.has_point(p_mouse_pos)) {
  651. //check sub-controls
  652. Vector2 subpos = p_mouse_pos - rect.position;
  653. for (int j = 0; j < child->get_child_count(); j++) {
  654. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  655. if (!subchild) {
  656. continue;
  657. }
  658. if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
  659. return false;
  660. }
  661. }
  662. }
  663. }
  664. return true;
  665. }
  666. template <class Vector2>
  667. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  668. /* Formula from Wikipedia article on Bezier curves. */
  669. real_t omt = (1.0 - t);
  670. real_t omt2 = omt * omt;
  671. real_t omt3 = omt2 * omt;
  672. real_t t2 = t * t;
  673. real_t t3 = t2 * t;
  674. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  675. }
  676. void GraphEdit::_bake_segment2d(Vector<Vector2> &points, Vector<Color> &colors, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const {
  677. float mp = p_begin + (p_end - p_begin) * 0.5;
  678. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  679. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  680. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  681. Vector2 na = (mid - beg).normalized();
  682. Vector2 nb = (end - mid).normalized();
  683. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  684. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  685. points.push_back((beg + end) * 0.5);
  686. colors.push_back(p_color.lerp(p_to_color, mp));
  687. lines++;
  688. } else {
  689. _bake_segment2d(points, colors, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  690. _bake_segment2d(points, colors, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  691. }
  692. }
  693. void GraphEdit::_draw_cos_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_bezier_ratio) {
  694. //cubic bezier code
  695. float diff = p_to.x - p_from.x;
  696. float cp_offset;
  697. int cp_len = get_theme_constant("bezier_len_pos") * p_bezier_ratio;
  698. int cp_neg_len = get_theme_constant("bezier_len_neg") * p_bezier_ratio;
  699. if (diff > 0) {
  700. cp_offset = MIN(cp_len, diff * 0.5);
  701. } else {
  702. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  703. }
  704. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  705. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  706. int lines = 0;
  707. Vector<Point2> points;
  708. Vector<Color> colors;
  709. points.push_back(p_from);
  710. colors.push_back(p_color);
  711. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
  712. points.push_back(p_to);
  713. colors.push_back(p_to_color);
  714. #ifdef TOOLS_ENABLED
  715. p_where->draw_polyline_colors(points, colors, Math::floor(p_width * EDSCALE), lines_antialiased);
  716. #else
  717. p_where->draw_polyline_colors(points, colors, p_width, lines_antialiased);
  718. #endif
  719. }
  720. void GraphEdit::_connections_layer_draw() {
  721. Color activity_color = get_theme_color("activity");
  722. //draw connections
  723. List<List<Connection>::Element *> to_erase;
  724. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  725. NodePath fromnp(E->get().from);
  726. Node *from = get_node(fromnp);
  727. if (!from) {
  728. to_erase.push_back(E);
  729. continue;
  730. }
  731. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  732. if (!gfrom) {
  733. to_erase.push_back(E);
  734. continue;
  735. }
  736. NodePath tonp(E->get().to);
  737. Node *to = get_node(tonp);
  738. if (!to) {
  739. to_erase.push_back(E);
  740. continue;
  741. }
  742. GraphNode *gto = Object::cast_to<GraphNode>(to);
  743. if (!gto) {
  744. to_erase.push_back(E);
  745. continue;
  746. }
  747. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_position_offset() * zoom;
  748. Color color = gfrom->get_connection_output_color(E->get().from_port);
  749. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_position_offset() * zoom;
  750. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  751. if (E->get().activity > 0) {
  752. color = color.lerp(activity_color, E->get().activity);
  753. tocolor = tocolor.lerp(activity_color, E->get().activity);
  754. }
  755. _draw_cos_line(connections_layer, frompos, topos, color, tocolor, lines_thickness);
  756. }
  757. while (to_erase.size()) {
  758. connections.erase(to_erase.front()->get());
  759. to_erase.pop_front();
  760. }
  761. }
  762. void GraphEdit::_top_layer_draw() {
  763. _update_scroll();
  764. if (connecting) {
  765. Node *fromn = get_node(connecting_from);
  766. ERR_FAIL_COND(!fromn);
  767. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  768. ERR_FAIL_COND(!from);
  769. Vector2 pos;
  770. if (connecting_out) {
  771. pos = from->get_connection_output_position(connecting_index);
  772. } else {
  773. pos = from->get_connection_input_position(connecting_index);
  774. }
  775. pos += from->get_position();
  776. Vector2 topos;
  777. topos = connecting_to;
  778. Color col = connecting_color;
  779. if (connecting_target) {
  780. col.r += 0.4;
  781. col.g += 0.4;
  782. col.b += 0.4;
  783. }
  784. if (!connecting_out) {
  785. SWAP(pos, topos);
  786. }
  787. _draw_cos_line(top_layer, pos, topos, col, col, lines_thickness);
  788. }
  789. if (box_selecting) {
  790. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_fill"));
  791. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_stroke"), false);
  792. }
  793. }
  794. void GraphEdit::_minimap_draw() {
  795. if (!is_minimap_enabled()) {
  796. return;
  797. }
  798. minimap->update_minimap();
  799. // Draw the minimap background.
  800. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  801. minimap->draw_style_box(minimap->get_theme_stylebox("bg"), minimap_rect);
  802. Vector2 graph_offset = minimap->_get_graph_offset();
  803. Vector2 minimap_offset = minimap->minimap_offset;
  804. // Draw comment graph nodes.
  805. for (int i = get_child_count() - 1; i >= 0; i--) {
  806. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  807. if (!gn || !gn->is_comment()) {
  808. continue;
  809. }
  810. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  811. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  812. Rect2 node_rect = Rect2(node_position, node_size);
  813. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox("node")->duplicate();
  814. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  815. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "commentfocus" : "comment");
  816. if (sbf.is_valid()) {
  817. Color node_color = sbf->get_bg_color();
  818. sb_minimap->set_bg_color(node_color);
  819. }
  820. minimap->draw_style_box(sb_minimap, node_rect);
  821. }
  822. // Draw regular graph nodes.
  823. for (int i = get_child_count() - 1; i >= 0; i--) {
  824. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  825. if (!gn || gn->is_comment()) {
  826. continue;
  827. }
  828. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  829. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  830. Rect2 node_rect = Rect2(node_position, node_size);
  831. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox("node")->duplicate();
  832. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  833. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "selectedframe" : "frame");
  834. if (sbf.is_valid()) {
  835. Color node_color = sbf->get_border_color();
  836. sb_minimap->set_bg_color(node_color);
  837. }
  838. minimap->draw_style_box(sb_minimap, node_rect);
  839. }
  840. // Draw node connections.
  841. Color activity_color = get_theme_color("activity");
  842. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  843. NodePath fromnp(E->get().from);
  844. Node *from = get_node(fromnp);
  845. if (!from) {
  846. continue;
  847. }
  848. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  849. if (!gfrom) {
  850. continue;
  851. }
  852. NodePath tonp(E->get().to);
  853. Node *to = get_node(tonp);
  854. if (!to) {
  855. continue;
  856. }
  857. GraphNode *gto = Object::cast_to<GraphNode>(to);
  858. if (!gto) {
  859. continue;
  860. }
  861. Vector2 from_slot_position = gfrom->get_position_offset() * zoom + gfrom->get_connection_output_position(E->get().from_port);
  862. Vector2 from_position = minimap->_convert_from_graph_position(from_slot_position - graph_offset) + minimap_offset;
  863. Color from_color = gfrom->get_connection_output_color(E->get().from_port);
  864. Vector2 to_slot_position = gto->get_position_offset() * zoom + gto->get_connection_input_position(E->get().to_port);
  865. Vector2 to_position = minimap->_convert_from_graph_position(to_slot_position - graph_offset) + minimap_offset;
  866. Color to_color = gto->get_connection_input_color(E->get().to_port);
  867. if (E->get().activity > 0) {
  868. from_color = from_color.lerp(activity_color, E->get().activity);
  869. to_color = to_color.lerp(activity_color, E->get().activity);
  870. }
  871. _draw_cos_line(minimap, from_position, to_position, from_color, to_color, 1.0, 0.5);
  872. }
  873. // Draw the "camera" viewport.
  874. Rect2 camera_rect = minimap->get_camera_rect();
  875. minimap->draw_style_box(minimap->get_theme_stylebox("camera"), camera_rect);
  876. // Draw the resizer control.
  877. Ref<Texture2D> resizer = minimap->get_theme_icon("resizer");
  878. Color resizer_color = minimap->get_theme_color("resizer_color");
  879. minimap->draw_texture(resizer, Point2(), resizer_color);
  880. }
  881. void GraphEdit::set_selected(Node *p_child) {
  882. for (int i = get_child_count() - 1; i >= 0; i--) {
  883. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  884. if (!gn) {
  885. continue;
  886. }
  887. gn->set_selected(gn == p_child);
  888. }
  889. }
  890. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  891. ERR_FAIL_COND(p_ev.is_null());
  892. Ref<InputEventMouseMotion> mm = p_ev;
  893. if (mm.is_valid() && (mm->get_button_mask() & MOUSE_BUTTON_MASK_MIDDLE || (mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
  894. Vector2i relative = Input::get_singleton()->warp_mouse_motion(mm, get_global_rect());
  895. h_scroll->set_value(h_scroll->get_value() - relative.x);
  896. v_scroll->set_value(v_scroll->get_value() - relative.y);
  897. }
  898. if (mm.is_valid() && dragging) {
  899. if (!moving_selection) {
  900. emit_signal("begin_node_move");
  901. moving_selection = true;
  902. }
  903. just_selected = true;
  904. drag_accum += mm->get_relative();
  905. for (int i = get_child_count() - 1; i >= 0; i--) {
  906. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  907. if (gn && gn->is_selected()) {
  908. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  909. // Snapping can be toggled temporarily by holding down Ctrl.
  910. // This is done here as to not toggle the grid when holding down Ctrl.
  911. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  912. const int snap = get_snap();
  913. pos = pos.snapped(Vector2(snap, snap));
  914. }
  915. gn->set_position_offset(pos);
  916. }
  917. }
  918. }
  919. if (mm.is_valid() && box_selecting) {
  920. box_selecting_to = mm->get_position();
  921. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  922. MIN(box_selecting_from.y, box_selecting_to.y),
  923. ABS(box_selecting_from.x - box_selecting_to.x),
  924. ABS(box_selecting_from.y - box_selecting_to.y));
  925. for (int i = get_child_count() - 1; i >= 0; i--) {
  926. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  927. if (!gn) {
  928. continue;
  929. }
  930. Rect2 r = gn->get_rect();
  931. r.size *= zoom;
  932. bool in_box = r.intersects(box_selecting_rect);
  933. if (in_box) {
  934. if (!gn->is_selected() && box_selection_mode_additive) {
  935. emit_signal("node_selected", gn);
  936. } else if (gn->is_selected() && !box_selection_mode_additive) {
  937. emit_signal("node_deselected", gn);
  938. }
  939. gn->set_selected(box_selection_mode_additive);
  940. } else {
  941. bool select = (previous_selected.find(gn) != nullptr);
  942. if (gn->is_selected() && !select) {
  943. emit_signal("node_deselected", gn);
  944. } else if (!gn->is_selected() && select) {
  945. emit_signal("node_selected", gn);
  946. }
  947. gn->set_selected(select);
  948. }
  949. }
  950. top_layer->update();
  951. minimap->update();
  952. }
  953. Ref<InputEventMouseButton> b = p_ev;
  954. if (b.is_valid()) {
  955. if (b->get_button_index() == MOUSE_BUTTON_RIGHT && b->is_pressed()) {
  956. if (box_selecting) {
  957. box_selecting = false;
  958. for (int i = get_child_count() - 1; i >= 0; i--) {
  959. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  960. if (!gn) {
  961. continue;
  962. }
  963. bool select = (previous_selected.find(gn) != nullptr);
  964. if (gn->is_selected() && !select) {
  965. emit_signal("node_deselected", gn);
  966. } else if (!gn->is_selected() && select) {
  967. emit_signal("node_selected", gn);
  968. }
  969. gn->set_selected(select);
  970. }
  971. top_layer->update();
  972. minimap->update();
  973. } else {
  974. if (connecting) {
  975. connecting = false;
  976. top_layer->update();
  977. minimap->update();
  978. } else {
  979. emit_signal("popup_request", b->get_global_position());
  980. }
  981. }
  982. }
  983. if (b->get_button_index() == MOUSE_BUTTON_LEFT && !b->is_pressed() && dragging) {
  984. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  985. //deselect current node
  986. for (int i = get_child_count() - 1; i >= 0; i--) {
  987. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  988. if (gn) {
  989. Rect2 r = gn->get_rect();
  990. r.size *= zoom;
  991. if (r.has_point(b->get_position())) {
  992. emit_signal("node_deselected", gn);
  993. gn->set_selected(false);
  994. }
  995. }
  996. }
  997. }
  998. if (drag_accum != Vector2()) {
  999. for (int i = get_child_count() - 1; i >= 0; i--) {
  1000. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1001. if (gn && gn->is_selected()) {
  1002. gn->set_drag(false);
  1003. }
  1004. }
  1005. }
  1006. if (moving_selection) {
  1007. emit_signal("end_node_move");
  1008. moving_selection = false;
  1009. }
  1010. dragging = false;
  1011. top_layer->update();
  1012. minimap->update();
  1013. update();
  1014. connections_layer->update();
  1015. }
  1016. if (b->get_button_index() == MOUSE_BUTTON_LEFT && b->is_pressed()) {
  1017. GraphNode *gn = nullptr;
  1018. for (int i = get_child_count() - 1; i >= 0; i--) {
  1019. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  1020. if (gn_selected) {
  1021. if (gn_selected->is_resizing()) {
  1022. continue;
  1023. }
  1024. if (gn_selected->has_point((b->get_position() - gn_selected->get_position()) / zoom)) {
  1025. gn = gn_selected;
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. if (gn) {
  1031. if (_filter_input(b->get_position())) {
  1032. return;
  1033. }
  1034. dragging = true;
  1035. drag_accum = Vector2();
  1036. just_selected = !gn->is_selected();
  1037. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  1038. for (int i = 0; i < get_child_count(); i++) {
  1039. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1040. if (o_gn) {
  1041. if (o_gn == gn) {
  1042. o_gn->set_selected(true);
  1043. } else {
  1044. if (o_gn->is_selected()) {
  1045. emit_signal("node_deselected", o_gn);
  1046. }
  1047. o_gn->set_selected(false);
  1048. }
  1049. }
  1050. }
  1051. }
  1052. gn->set_selected(true);
  1053. for (int i = 0; i < get_child_count(); i++) {
  1054. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1055. if (!o_gn) {
  1056. continue;
  1057. }
  1058. if (o_gn->is_selected()) {
  1059. o_gn->set_drag(true);
  1060. }
  1061. }
  1062. } else {
  1063. if (_filter_input(b->get_position())) {
  1064. return;
  1065. }
  1066. if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  1067. return;
  1068. }
  1069. box_selecting = true;
  1070. box_selecting_from = b->get_position();
  1071. if (b->is_ctrl_pressed()) {
  1072. box_selection_mode_additive = true;
  1073. previous_selected.clear();
  1074. for (int i = get_child_count() - 1; i >= 0; i--) {
  1075. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1076. if (!gn2 || !gn2->is_selected()) {
  1077. continue;
  1078. }
  1079. previous_selected.push_back(gn2);
  1080. }
  1081. } else if (b->is_shift_pressed()) {
  1082. box_selection_mode_additive = false;
  1083. previous_selected.clear();
  1084. for (int i = get_child_count() - 1; i >= 0; i--) {
  1085. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1086. if (!gn2 || !gn2->is_selected()) {
  1087. continue;
  1088. }
  1089. previous_selected.push_back(gn2);
  1090. }
  1091. } else {
  1092. box_selection_mode_additive = true;
  1093. previous_selected.clear();
  1094. for (int i = get_child_count() - 1; i >= 0; i--) {
  1095. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1096. if (!gn2) {
  1097. continue;
  1098. }
  1099. if (gn2->is_selected()) {
  1100. emit_signal("node_deselected", gn2);
  1101. }
  1102. gn2->set_selected(false);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. if (b->get_button_index() == MOUSE_BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  1108. box_selecting = false;
  1109. box_selecting_rect = Rect2();
  1110. previous_selected.clear();
  1111. top_layer->update();
  1112. minimap->update();
  1113. }
  1114. int scroll_direction = (b->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN) - (b->get_button_index() == MOUSE_BUTTON_WHEEL_UP);
  1115. if (scroll_direction != 0) {
  1116. if (b->is_ctrl_pressed()) {
  1117. if (b->is_shift_pressed()) {
  1118. // Horizontal scrolling.
  1119. h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1120. } else {
  1121. // Vertical scrolling.
  1122. v_scroll->set_value(v_scroll->get_value() + (v_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1123. }
  1124. } else {
  1125. // Zooming.
  1126. set_zoom_custom(scroll_direction < 0 ? zoom * zoom_step : zoom / zoom_step, b->get_position());
  1127. }
  1128. }
  1129. }
  1130. if (p_ev->is_pressed()) {
  1131. if (p_ev->is_action("ui_graph_duplicate")) {
  1132. emit_signal("duplicate_nodes_request");
  1133. accept_event();
  1134. } else if (p_ev->is_action("ui_copy")) {
  1135. emit_signal("copy_nodes_request");
  1136. accept_event();
  1137. } else if (p_ev->is_action("ui_paste")) {
  1138. emit_signal("paste_nodes_request");
  1139. accept_event();
  1140. } else if (p_ev->is_action("ui_graph_delete")) {
  1141. emit_signal("delete_nodes_request");
  1142. accept_event();
  1143. }
  1144. }
  1145. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  1146. if (magnify_gesture.is_valid()) {
  1147. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  1148. }
  1149. Ref<InputEventPanGesture> pan_gesture = p_ev;
  1150. if (pan_gesture.is_valid()) {
  1151. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  1152. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  1153. }
  1154. }
  1155. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1156. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1157. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  1158. if (Math::is_equal_approx(E->get().activity, p_activity)) {
  1159. //update only if changed
  1160. top_layer->update();
  1161. minimap->update();
  1162. connections_layer->update();
  1163. }
  1164. E->get().activity = p_activity;
  1165. return;
  1166. }
  1167. }
  1168. }
  1169. void GraphEdit::clear_connections() {
  1170. connections.clear();
  1171. minimap->update();
  1172. update();
  1173. connections_layer->update();
  1174. }
  1175. void GraphEdit::set_zoom(float p_zoom) {
  1176. set_zoom_custom(p_zoom, get_size() / 2);
  1177. }
  1178. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1179. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1180. if (zoom == p_zoom) {
  1181. return;
  1182. }
  1183. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  1184. zoom = p_zoom;
  1185. top_layer->update();
  1186. zoom_minus->set_disabled(zoom == zoom_min);
  1187. zoom_plus->set_disabled(zoom == zoom_max);
  1188. _update_scroll();
  1189. minimap->update();
  1190. connections_layer->update();
  1191. if (is_visible_in_tree()) {
  1192. Vector2 ofs = sbofs * zoom - p_center;
  1193. h_scroll->set_value(ofs.x);
  1194. v_scroll->set_value(ofs.y);
  1195. }
  1196. _update_zoom_label();
  1197. update();
  1198. }
  1199. float GraphEdit::get_zoom() const {
  1200. return zoom;
  1201. }
  1202. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1203. p_zoom_step = abs(p_zoom_step);
  1204. if (zoom_step == p_zoom_step) {
  1205. return;
  1206. }
  1207. zoom_step = p_zoom_step;
  1208. }
  1209. float GraphEdit::get_zoom_step() const {
  1210. return zoom_step;
  1211. }
  1212. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1213. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1214. if (zoom_min == p_zoom_min) {
  1215. return;
  1216. }
  1217. zoom_min = p_zoom_min;
  1218. set_zoom(zoom);
  1219. }
  1220. float GraphEdit::get_zoom_min() const {
  1221. return zoom_min;
  1222. }
  1223. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1224. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1225. if (zoom_max == p_zoom_max) {
  1226. return;
  1227. }
  1228. zoom_max = p_zoom_max;
  1229. set_zoom(zoom);
  1230. }
  1231. float GraphEdit::get_zoom_max() const {
  1232. return zoom_max;
  1233. }
  1234. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1235. if (zoom_label->is_visible() == p_enable) {
  1236. return;
  1237. }
  1238. zoom_label->set_visible(p_enable);
  1239. }
  1240. bool GraphEdit::is_showing_zoom_label() const {
  1241. return zoom_label->is_visible();
  1242. }
  1243. void GraphEdit::set_right_disconnects(bool p_enable) {
  1244. right_disconnects = p_enable;
  1245. }
  1246. bool GraphEdit::is_right_disconnects_enabled() const {
  1247. return right_disconnects;
  1248. }
  1249. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1250. valid_right_disconnect_types.insert(p_type);
  1251. }
  1252. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1253. valid_right_disconnect_types.erase(p_type);
  1254. }
  1255. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1256. valid_left_disconnect_types.insert(p_type);
  1257. }
  1258. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1259. valid_left_disconnect_types.erase(p_type);
  1260. }
  1261. Array GraphEdit::_get_connection_list() const {
  1262. List<Connection> conns;
  1263. get_connection_list(&conns);
  1264. Array arr;
  1265. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  1266. Dictionary d;
  1267. d["from"] = E->get().from;
  1268. d["from_port"] = E->get().from_port;
  1269. d["to"] = E->get().to;
  1270. d["to_port"] = E->get().to_port;
  1271. arr.push_back(d);
  1272. }
  1273. return arr;
  1274. }
  1275. void GraphEdit::_zoom_minus() {
  1276. set_zoom(zoom / zoom_step);
  1277. }
  1278. void GraphEdit::_zoom_reset() {
  1279. set_zoom(1);
  1280. }
  1281. void GraphEdit::_zoom_plus() {
  1282. set_zoom(zoom * zoom_step);
  1283. }
  1284. void GraphEdit::_update_zoom_label() {
  1285. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1286. String zoom_text = itos(zoom_percent) + "%";
  1287. zoom_label->set_text(zoom_text);
  1288. }
  1289. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1290. ConnType ct;
  1291. ct.type_a = p_type;
  1292. ct.type_b = p_with_type;
  1293. valid_connection_types.insert(ct);
  1294. }
  1295. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1296. ConnType ct;
  1297. ct.type_a = p_type;
  1298. ct.type_b = p_with_type;
  1299. valid_connection_types.erase(ct);
  1300. }
  1301. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1302. ConnType ct;
  1303. ct.type_a = p_type;
  1304. ct.type_b = p_with_type;
  1305. return valid_connection_types.has(ct);
  1306. }
  1307. void GraphEdit::set_use_snap(bool p_enable) {
  1308. snap_button->set_pressed(p_enable);
  1309. update();
  1310. }
  1311. bool GraphEdit::is_using_snap() const {
  1312. return snap_button->is_pressed();
  1313. }
  1314. int GraphEdit::get_snap() const {
  1315. return snap_amount->get_value();
  1316. }
  1317. void GraphEdit::set_snap(int p_snap) {
  1318. ERR_FAIL_COND(p_snap < 5);
  1319. snap_amount->set_value(p_snap);
  1320. update();
  1321. }
  1322. void GraphEdit::_snap_toggled() {
  1323. update();
  1324. }
  1325. void GraphEdit::_snap_value_changed(double) {
  1326. update();
  1327. }
  1328. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1329. minimap->set_size(p_size);
  1330. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1331. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1332. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1333. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1334. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1335. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1336. minimap->update();
  1337. }
  1338. Vector2 GraphEdit::get_minimap_size() const {
  1339. return minimap->get_size();
  1340. }
  1341. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1342. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1343. minimap->update();
  1344. }
  1345. float GraphEdit::get_minimap_opacity() const {
  1346. Color minimap_modulate = minimap->get_modulate();
  1347. return minimap_modulate.a;
  1348. }
  1349. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1350. minimap_button->set_pressed(p_enable);
  1351. minimap->update();
  1352. }
  1353. bool GraphEdit::is_minimap_enabled() const {
  1354. return minimap_button->is_pressed();
  1355. }
  1356. void GraphEdit::_minimap_toggled() {
  1357. if (is_minimap_enabled()) {
  1358. minimap->set_visible(true);
  1359. minimap->update();
  1360. } else {
  1361. minimap->set_visible(false);
  1362. }
  1363. }
  1364. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  1365. lines_thickness = p_thickness;
  1366. update();
  1367. }
  1368. float GraphEdit::get_connection_lines_thickness() const {
  1369. return lines_thickness;
  1370. }
  1371. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  1372. lines_antialiased = p_antialiased;
  1373. update();
  1374. }
  1375. bool GraphEdit::is_connection_lines_antialiased() const {
  1376. return lines_antialiased;
  1377. }
  1378. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1379. return zoom_hb;
  1380. }
  1381. void GraphEdit::_bind_methods() {
  1382. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  1383. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  1384. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  1385. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1386. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1387. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1388. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1389. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  1390. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1391. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1392. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1393. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1394. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1395. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1396. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1397. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  1398. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1399. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1400. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1401. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1402. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1403. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1404. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1405. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1406. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1407. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1408. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1409. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1410. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1411. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  1412. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  1413. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  1414. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  1415. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  1416. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1417. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  1418. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1419. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1420. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1421. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1422. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1423. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  1424. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1425. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1426. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1427. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1428. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  1429. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  1430. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1431. ADD_GROUP("Connection Lines", "connection_lines");
  1432. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  1433. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  1434. ADD_GROUP("Zoom", "");
  1435. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1436. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  1437. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  1438. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  1439. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  1440. ADD_GROUP("Minimap", "minimap");
  1441. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  1442. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size"), "set_minimap_size", "get_minimap_size");
  1443. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  1444. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1445. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1446. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1447. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1448. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1449. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1450. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1451. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1452. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1453. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1454. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  1455. ADD_SIGNAL(MethodInfo("begin_node_move"));
  1456. ADD_SIGNAL(MethodInfo("end_node_move"));
  1457. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  1458. }
  1459. GraphEdit::GraphEdit() {
  1460. set_focus_mode(FOCUS_ALL);
  1461. // Allow dezooming 8 times from the default zoom level.
  1462. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  1463. // but this is still useful for previewing and navigation.
  1464. zoom_min = (1 / Math::pow(zoom_step, 8));
  1465. // Allow zooming 4 times from the default zoom level.
  1466. zoom_max = (1 * Math::pow(zoom_step, 4));
  1467. top_layer = memnew(GraphEditFilter(this));
  1468. add_child(top_layer);
  1469. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1470. top_layer->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1471. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1472. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1473. connections_layer = memnew(Control);
  1474. add_child(connections_layer);
  1475. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1476. connections_layer->set_name("CLAYER");
  1477. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1478. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1479. h_scroll = memnew(HScrollBar);
  1480. h_scroll->set_name("_h_scroll");
  1481. top_layer->add_child(h_scroll);
  1482. v_scroll = memnew(VScrollBar);
  1483. v_scroll->set_name("_v_scroll");
  1484. top_layer->add_child(v_scroll);
  1485. //set large minmax so it can scroll even if not resized yet
  1486. h_scroll->set_min(-10000);
  1487. h_scroll->set_max(10000);
  1488. v_scroll->set_min(-10000);
  1489. v_scroll->set_max(10000);
  1490. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1491. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1492. zoom_hb = memnew(HBoxContainer);
  1493. top_layer->add_child(zoom_hb);
  1494. zoom_hb->set_position(Vector2(10, 10));
  1495. zoom_label = memnew(Label);
  1496. zoom_hb->add_child(zoom_label);
  1497. zoom_label->set_visible(false);
  1498. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1499. zoom_label->set_align(Label::ALIGN_CENTER);
  1500. #ifdef TOOLS_ENABLED
  1501. zoom_label->set_custom_minimum_size(Size2(48, 0) * EDSCALE);
  1502. #else
  1503. zoom_label->set_custom_minimum_size(Size2(48, 0));
  1504. #endif
  1505. _update_zoom_label();
  1506. zoom_minus = memnew(Button);
  1507. zoom_minus->set_flat(true);
  1508. zoom_hb->add_child(zoom_minus);
  1509. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1510. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1511. zoom_minus->set_focus_mode(FOCUS_NONE);
  1512. zoom_reset = memnew(Button);
  1513. zoom_reset->set_flat(true);
  1514. zoom_hb->add_child(zoom_reset);
  1515. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1516. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1517. zoom_reset->set_focus_mode(FOCUS_NONE);
  1518. zoom_plus = memnew(Button);
  1519. zoom_plus->set_flat(true);
  1520. zoom_hb->add_child(zoom_plus);
  1521. zoom_plus->set_tooltip(RTR("Zoom In"));
  1522. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1523. zoom_plus->set_focus_mode(FOCUS_NONE);
  1524. snap_button = memnew(Button);
  1525. snap_button->set_flat(true);
  1526. snap_button->set_toggle_mode(true);
  1527. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1528. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  1529. snap_button->set_pressed(true);
  1530. snap_button->set_focus_mode(FOCUS_NONE);
  1531. zoom_hb->add_child(snap_button);
  1532. snap_amount = memnew(SpinBox);
  1533. snap_amount->set_min(5);
  1534. snap_amount->set_max(100);
  1535. snap_amount->set_step(1);
  1536. snap_amount->set_value(20);
  1537. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  1538. zoom_hb->add_child(snap_amount);
  1539. minimap_button = memnew(Button);
  1540. minimap_button->set_flat(true);
  1541. minimap_button->set_toggle_mode(true);
  1542. minimap_button->set_tooltip(RTR("Enable grid minimap."));
  1543. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  1544. minimap_button->set_pressed(true);
  1545. minimap_button->set_focus_mode(FOCUS_NONE);
  1546. zoom_hb->add_child(minimap_button);
  1547. Vector2 minimap_size = Vector2(240, 160);
  1548. float minimap_opacity = 0.65;
  1549. minimap = memnew(GraphEditMinimap(this));
  1550. top_layer->add_child(minimap);
  1551. minimap->set_name("_minimap");
  1552. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  1553. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  1554. minimap->set_custom_minimum_size(Vector2(50, 50));
  1555. minimap->set_size(minimap_size);
  1556. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1557. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1558. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1559. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1560. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1561. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  1562. set_clip_contents(true);
  1563. }