graph_edit.cpp 56 KB

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