graph_edit.cpp 58 KB

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