graph_edit.cpp 61 KB

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