graph_edit.cpp 61 KB

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