graph_edit.cpp 58 KB

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