graph_edit.cpp 58 KB

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