2
0

graph_edit.cpp 57 KB

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