graph_edit.cpp 57 KB

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