graph_edit.cpp 59 KB

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