graph_edit.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*************************************************************************/
  2. /* graph_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "graph_edit.h"
  31. #include "core/input/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/button.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. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  42. return ge->_filter_input(p_point);
  43. }
  44. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  45. ge = p_edit;
  46. }
  47. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  48. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  49. return OK;
  50. }
  51. Connection c;
  52. c.from = p_from;
  53. c.from_port = p_from_port;
  54. c.to = p_to;
  55. c.to_port = p_to_port;
  56. c.activity = 0;
  57. connections.push_back(c);
  58. top_layer->update();
  59. update();
  60. connections_layer->update();
  61. return OK;
  62. }
  63. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  64. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  65. 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) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  72. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  73. 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) {
  74. connections.erase(E);
  75. top_layer->update();
  76. update();
  77. connections_layer->update();
  78. return;
  79. }
  80. }
  81. }
  82. bool GraphEdit::clips_input() const {
  83. return true;
  84. }
  85. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  86. *r_connections = connections;
  87. }
  88. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  89. setting_scroll_ofs = true;
  90. h_scroll->set_value(p_ofs.x);
  91. v_scroll->set_value(p_ofs.y);
  92. _update_scroll();
  93. setting_scroll_ofs = false;
  94. }
  95. Vector2 GraphEdit::get_scroll_ofs() const {
  96. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  97. }
  98. void GraphEdit::_scroll_moved(double) {
  99. if (!awaiting_scroll_offset_update) {
  100. call_deferred("_update_scroll_offset");
  101. awaiting_scroll_offset_update = true;
  102. }
  103. top_layer->update();
  104. update();
  105. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  106. emit_signal("scroll_offset_changed", get_scroll_ofs());
  107. }
  108. }
  109. void GraphEdit::_update_scroll_offset() {
  110. set_block_minimum_size_adjust(true);
  111. for (int i = 0; i < get_child_count(); i++) {
  112. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  113. if (!gn) {
  114. continue;
  115. }
  116. Point2 pos = gn->get_offset() * zoom;
  117. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  118. gn->set_position(pos);
  119. if (gn->get_scale() != Vector2(zoom, zoom)) {
  120. gn->set_scale(Vector2(zoom, zoom));
  121. }
  122. }
  123. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  124. set_block_minimum_size_adjust(false);
  125. awaiting_scroll_offset_update = false;
  126. }
  127. void GraphEdit::_update_scroll() {
  128. if (updating) {
  129. return;
  130. }
  131. updating = true;
  132. set_block_minimum_size_adjust(true);
  133. Rect2 screen;
  134. for (int i = 0; i < get_child_count(); i++) {
  135. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  136. if (!gn) {
  137. continue;
  138. }
  139. Rect2 r;
  140. r.position = gn->get_offset() * zoom;
  141. r.size = gn->get_size() * zoom;
  142. screen = screen.merge(r);
  143. }
  144. screen.position -= get_size();
  145. screen.size += get_size() * 2.0;
  146. h_scroll->set_min(screen.position.x);
  147. h_scroll->set_max(screen.position.x + screen.size.x);
  148. h_scroll->set_page(get_size().x);
  149. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  150. h_scroll->hide();
  151. } else {
  152. h_scroll->show();
  153. }
  154. v_scroll->set_min(screen.position.y);
  155. v_scroll->set_max(screen.position.y + screen.size.y);
  156. v_scroll->set_page(get_size().y);
  157. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  158. v_scroll->hide();
  159. } else {
  160. v_scroll->show();
  161. }
  162. Size2 hmin = h_scroll->get_combined_minimum_size();
  163. Size2 vmin = v_scroll->get_combined_minimum_size();
  164. // Avoid scrollbar overlapping.
  165. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  166. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  167. set_block_minimum_size_adjust(false);
  168. if (!awaiting_scroll_offset_update) {
  169. call_deferred("_update_scroll_offset");
  170. awaiting_scroll_offset_update = true;
  171. }
  172. updating = false;
  173. }
  174. void GraphEdit::_graph_node_raised(Node *p_gn) {
  175. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  176. ERR_FAIL_COND(!gn);
  177. if (gn->is_comment()) {
  178. move_child(gn, 0);
  179. } else {
  180. gn->raise();
  181. }
  182. int first_not_comment = 0;
  183. for (int i = 0; i < get_child_count(); i++) {
  184. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  185. if (gn2 && !gn2->is_comment()) {
  186. first_not_comment = i;
  187. break;
  188. }
  189. }
  190. move_child(connections_layer, first_not_comment);
  191. top_layer->raise();
  192. emit_signal("node_selected", p_gn);
  193. }
  194. void GraphEdit::_graph_node_moved(Node *p_gn) {
  195. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  196. ERR_FAIL_COND(!gn);
  197. top_layer->update();
  198. update();
  199. connections_layer->update();
  200. }
  201. void GraphEdit::add_child_notify(Node *p_child) {
  202. Control::add_child_notify(p_child);
  203. top_layer->call_deferred("raise"); //top layer always on top!
  204. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  205. if (gn) {
  206. gn->set_scale(Vector2(zoom, zoom));
  207. gn->connect("offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved), varray(gn));
  208. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised), varray(gn));
  209. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  210. _graph_node_moved(gn);
  211. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  212. }
  213. }
  214. void GraphEdit::remove_child_notify(Node *p_child) {
  215. Control::remove_child_notify(p_child);
  216. if (is_inside_tree()) {
  217. top_layer->call_deferred("raise"); //top layer always on top!
  218. }
  219. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  220. if (gn) {
  221. gn->disconnect("offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  222. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  223. }
  224. }
  225. void GraphEdit::_notification(int p_what) {
  226. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  227. port_grab_distance_horizontal = get_theme_constant("port_grab_distance_horizontal");
  228. port_grab_distance_vertical = get_theme_constant("port_grab_distance_vertical");
  229. zoom_minus->set_icon(get_theme_icon("minus"));
  230. zoom_reset->set_icon(get_theme_icon("reset"));
  231. zoom_plus->set_icon(get_theme_icon("more"));
  232. snap_button->set_icon(get_theme_icon("snap"));
  233. }
  234. if (p_what == NOTIFICATION_READY) {
  235. Size2 hmin = h_scroll->get_combined_minimum_size();
  236. Size2 vmin = v_scroll->get_combined_minimum_size();
  237. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  238. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  239. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  240. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  241. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  242. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  243. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  244. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  245. }
  246. if (p_what == NOTIFICATION_DRAW) {
  247. draw_style_box(get_theme_stylebox("bg"), Rect2(Point2(), get_size()));
  248. if (is_using_snap()) {
  249. //draw grid
  250. int snap = get_snap();
  251. Vector2 offset = get_scroll_ofs() / zoom;
  252. Size2 size = get_size() / zoom;
  253. Point2i from = (offset / float(snap)).floor();
  254. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  255. Color grid_minor = get_theme_color("grid_minor");
  256. Color grid_major = get_theme_color("grid_major");
  257. for (int i = from.x; i < from.x + len.x; i++) {
  258. Color color;
  259. if (ABS(i) % 10 == 0) {
  260. color = grid_major;
  261. } else {
  262. color = grid_minor;
  263. }
  264. float base_ofs = i * snap * zoom - offset.x * zoom;
  265. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  266. }
  267. for (int i = from.y; i < from.y + len.y; i++) {
  268. Color color;
  269. if (ABS(i) % 10 == 0) {
  270. color = grid_major;
  271. } else {
  272. color = grid_minor;
  273. }
  274. float base_ofs = i * snap * zoom - offset.y * zoom;
  275. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  276. }
  277. }
  278. }
  279. if (p_what == NOTIFICATION_RESIZED) {
  280. _update_scroll();
  281. top_layer->update();
  282. }
  283. }
  284. bool GraphEdit::_filter_input(const Point2 &p_point) {
  285. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  286. for (int i = get_child_count() - 1; i >= 0; i--) {
  287. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  288. if (!gn) {
  289. continue;
  290. }
  291. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  292. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  293. if (is_in_hot_zone(pos, p_point)) {
  294. return true;
  295. }
  296. }
  297. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  298. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  299. if (is_in_hot_zone(pos, p_point)) {
  300. return true;
  301. }
  302. }
  303. }
  304. return false;
  305. }
  306. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  307. Ref<InputEventMouseButton> mb = p_ev;
  308. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  309. connecting_valid = false;
  310. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  311. click_pos = mb->get_position();
  312. for (int i = get_child_count() - 1; i >= 0; i--) {
  313. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  314. if (!gn) {
  315. continue;
  316. }
  317. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  318. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  319. if (is_in_hot_zone(pos, click_pos)) {
  320. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  321. //check disconnect
  322. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  323. if (E->get().from == gn->get_name() && E->get().from_port == j) {
  324. Node *to = get_node(String(E->get().to));
  325. if (Object::cast_to<GraphNode>(to)) {
  326. connecting_from = E->get().to;
  327. connecting_index = E->get().to_port;
  328. connecting_out = false;
  329. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
  330. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E->get().to_port);
  331. connecting_target = false;
  332. connecting_to = pos;
  333. just_disconnected = true;
  334. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  335. to = get_node(String(connecting_from)); //maybe it was erased
  336. if (Object::cast_to<GraphNode>(to)) {
  337. connecting = true;
  338. }
  339. return;
  340. }
  341. }
  342. }
  343. }
  344. connecting = true;
  345. connecting_from = gn->get_name();
  346. connecting_index = j;
  347. connecting_out = true;
  348. connecting_type = gn->get_connection_output_type(j);
  349. connecting_color = gn->get_connection_output_color(j);
  350. connecting_target = false;
  351. connecting_to = pos;
  352. just_disconnected = false;
  353. return;
  354. }
  355. }
  356. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  357. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  358. if (is_in_hot_zone(pos, click_pos)) {
  359. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  360. //check disconnect
  361. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  362. if (E->get().to == gn->get_name() && E->get().to_port == j) {
  363. Node *fr = get_node(String(E->get().from));
  364. if (Object::cast_to<GraphNode>(fr)) {
  365. connecting_from = E->get().from;
  366. connecting_index = E->get().from_port;
  367. connecting_out = true;
  368. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
  369. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E->get().from_port);
  370. connecting_target = false;
  371. connecting_to = pos;
  372. just_disconnected = true;
  373. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().to_port);
  374. fr = get_node(String(connecting_from)); //maybe it was erased
  375. if (Object::cast_to<GraphNode>(fr)) {
  376. connecting = true;
  377. }
  378. return;
  379. }
  380. }
  381. }
  382. }
  383. connecting = true;
  384. connecting_from = gn->get_name();
  385. connecting_index = j;
  386. connecting_out = false;
  387. connecting_type = gn->get_connection_input_type(j);
  388. connecting_color = gn->get_connection_input_color(j);
  389. connecting_target = false;
  390. connecting_to = pos;
  391. just_disconnected = false;
  392. return;
  393. }
  394. }
  395. }
  396. }
  397. Ref<InputEventMouseMotion> mm = p_ev;
  398. if (mm.is_valid() && connecting) {
  399. connecting_to = mm->get_position();
  400. connecting_target = false;
  401. top_layer->update();
  402. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to) > 20.0 * zoom;
  403. if (connecting_valid) {
  404. Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
  405. Vector2 mpos = mm->get_position();
  406. for (int i = get_child_count() - 1; i >= 0; i--) {
  407. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  408. if (!gn) {
  409. continue;
  410. }
  411. if (!connecting_out) {
  412. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  413. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  414. int type = gn->get_connection_output_type(j);
  415. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  416. connecting_target = true;
  417. connecting_to = pos;
  418. connecting_target_to = gn->get_name();
  419. connecting_target_index = j;
  420. return;
  421. }
  422. }
  423. } else {
  424. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  425. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  426. int type = gn->get_connection_input_type(j);
  427. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
  428. connecting_target = true;
  429. connecting_to = pos;
  430. connecting_target_to = gn->get_name();
  431. connecting_target_index = j;
  432. return;
  433. }
  434. }
  435. }
  436. }
  437. }
  438. }
  439. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  440. if (connecting_valid) {
  441. if (connecting && connecting_target) {
  442. String from = connecting_from;
  443. int from_slot = connecting_index;
  444. String to = connecting_target_to;
  445. int to_slot = connecting_target_index;
  446. if (!connecting_out) {
  447. SWAP(from, to);
  448. SWAP(from_slot, to_slot);
  449. }
  450. emit_signal("connection_request", from, from_slot, to, to_slot);
  451. } else if (!just_disconnected) {
  452. String from = connecting_from;
  453. int from_slot = connecting_index;
  454. Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
  455. if (!connecting_out) {
  456. emit_signal("connection_from_empty", from, from_slot, ofs);
  457. } else {
  458. emit_signal("connection_to_empty", from, from_slot, ofs);
  459. }
  460. }
  461. }
  462. connecting = false;
  463. top_layer->update();
  464. update();
  465. connections_layer->update();
  466. }
  467. }
  468. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
  469. if (p_control->is_set_as_toplevel() || !p_control->is_visible()) {
  470. return false;
  471. }
  472. if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  473. //test children
  474. for (int i = 0; i < p_control->get_child_count(); i++) {
  475. Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
  476. if (!subchild) {
  477. continue;
  478. }
  479. if (_check_clickable_control(subchild, pos - subchild->get_position())) {
  480. return true;
  481. }
  482. }
  483. return false;
  484. } else {
  485. return true;
  486. }
  487. }
  488. bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos) {
  489. 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)) {
  490. return false;
  491. }
  492. for (int i = 0; i < get_child_count(); i++) {
  493. Control *child = Object::cast_to<Control>(get_child(i));
  494. if (!child) {
  495. continue;
  496. }
  497. Rect2 rect = child->get_rect();
  498. if (rect.has_point(p_mouse_pos)) {
  499. //check sub-controls
  500. Vector2 subpos = p_mouse_pos - rect.position;
  501. for (int j = 0; j < child->get_child_count(); j++) {
  502. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  503. if (!subchild) {
  504. continue;
  505. }
  506. if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
  507. return false;
  508. }
  509. }
  510. }
  511. }
  512. return true;
  513. }
  514. template <class Vector2>
  515. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  516. /* Formula from Wikipedia article on Bezier curves. */
  517. real_t omt = (1.0 - t);
  518. real_t omt2 = omt * omt;
  519. real_t omt3 = omt2 * omt;
  520. real_t t2 = t * t;
  521. real_t t3 = t2 * t;
  522. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  523. }
  524. 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 {
  525. float mp = p_begin + (p_end - p_begin) * 0.5;
  526. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  527. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  528. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  529. Vector2 na = (mid - beg).normalized();
  530. Vector2 nb = (end - mid).normalized();
  531. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  532. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  533. points.push_back((beg + end) * 0.5);
  534. colors.push_back(p_color.lerp(p_to_color, mp));
  535. lines++;
  536. } else {
  537. _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);
  538. _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);
  539. }
  540. }
  541. 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) {
  542. //cubic bezier code
  543. float diff = p_to.x - p_from.x;
  544. float cp_offset;
  545. int cp_len = get_theme_constant("bezier_len_pos");
  546. int cp_neg_len = get_theme_constant("bezier_len_neg");
  547. if (diff > 0) {
  548. cp_offset = MIN(cp_len, diff * 0.5);
  549. } else {
  550. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  551. }
  552. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  553. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  554. int lines = 0;
  555. Vector<Point2> points;
  556. Vector<Color> colors;
  557. points.push_back(p_from);
  558. colors.push_back(p_color);
  559. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
  560. points.push_back(p_to);
  561. colors.push_back(p_to_color);
  562. #ifdef TOOLS_ENABLED
  563. p_where->draw_polyline_colors(points, colors, Math::floor(2 * EDSCALE));
  564. #else
  565. p_where->draw_polyline_colors(points, colors, 2);
  566. #endif
  567. }
  568. void GraphEdit::_connections_layer_draw() {
  569. Color activity_color = get_theme_color("activity");
  570. //draw connections
  571. List<List<Connection>::Element *> to_erase;
  572. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  573. NodePath fromnp(E->get().from);
  574. Node *from = get_node(fromnp);
  575. if (!from) {
  576. to_erase.push_back(E);
  577. continue;
  578. }
  579. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  580. if (!gfrom) {
  581. to_erase.push_back(E);
  582. continue;
  583. }
  584. NodePath tonp(E->get().to);
  585. Node *to = get_node(tonp);
  586. if (!to) {
  587. to_erase.push_back(E);
  588. continue;
  589. }
  590. GraphNode *gto = Object::cast_to<GraphNode>(to);
  591. if (!gto) {
  592. to_erase.push_back(E);
  593. continue;
  594. }
  595. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_offset() * zoom;
  596. Color color = gfrom->get_connection_output_color(E->get().from_port);
  597. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_offset() * zoom;
  598. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  599. if (E->get().activity > 0) {
  600. color = color.lerp(activity_color, E->get().activity);
  601. tocolor = tocolor.lerp(activity_color, E->get().activity);
  602. }
  603. _draw_cos_line(connections_layer, frompos, topos, color, tocolor);
  604. }
  605. while (to_erase.size()) {
  606. connections.erase(to_erase.front()->get());
  607. to_erase.pop_front();
  608. }
  609. }
  610. void GraphEdit::_top_layer_draw() {
  611. _update_scroll();
  612. if (connecting) {
  613. Node *fromn = get_node(connecting_from);
  614. ERR_FAIL_COND(!fromn);
  615. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  616. ERR_FAIL_COND(!from);
  617. Vector2 pos;
  618. if (connecting_out) {
  619. pos = from->get_connection_output_position(connecting_index);
  620. } else {
  621. pos = from->get_connection_input_position(connecting_index);
  622. }
  623. pos += from->get_position();
  624. Vector2 topos;
  625. topos = connecting_to;
  626. Color col = connecting_color;
  627. if (connecting_target) {
  628. col.r += 0.4;
  629. col.g += 0.4;
  630. col.b += 0.4;
  631. }
  632. if (!connecting_out) {
  633. SWAP(pos, topos);
  634. }
  635. _draw_cos_line(top_layer, pos, topos, col, col);
  636. }
  637. if (box_selecting) {
  638. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_fill"));
  639. top_layer->draw_rect(box_selecting_rect, get_theme_color("selection_stroke"), false);
  640. }
  641. }
  642. void GraphEdit::set_selected(Node *p_child) {
  643. for (int i = get_child_count() - 1; i >= 0; i--) {
  644. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  645. if (!gn) {
  646. continue;
  647. }
  648. gn->set_selected(gn == p_child);
  649. }
  650. }
  651. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  652. Ref<InputEventMouseMotion> mm = p_ev;
  653. 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)))) {
  654. h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
  655. v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
  656. }
  657. if (mm.is_valid() && dragging) {
  658. just_selected = true;
  659. drag_accum += mm->get_relative();
  660. for (int i = get_child_count() - 1; i >= 0; i--) {
  661. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  662. if (gn && gn->is_selected()) {
  663. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  664. // Snapping can be toggled temporarily by holding down Ctrl.
  665. // This is done here as to not toggle the grid when holding down Ctrl.
  666. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  667. const int snap = get_snap();
  668. pos = pos.snapped(Vector2(snap, snap));
  669. }
  670. gn->set_offset(pos);
  671. }
  672. }
  673. }
  674. if (mm.is_valid() && box_selecting) {
  675. box_selecting_to = mm->get_position();
  676. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  677. MIN(box_selecting_from.y, box_selecting_to.y),
  678. ABS(box_selecting_from.x - box_selecting_to.x),
  679. ABS(box_selecting_from.y - box_selecting_to.y));
  680. for (int i = get_child_count() - 1; i >= 0; i--) {
  681. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  682. if (!gn) {
  683. continue;
  684. }
  685. Rect2 r = gn->get_rect();
  686. r.size *= zoom;
  687. bool in_box = r.intersects(box_selecting_rect);
  688. if (in_box) {
  689. if (!gn->is_selected() && box_selection_mode_additive) {
  690. emit_signal("node_selected", gn);
  691. } else if (gn->is_selected() && !box_selection_mode_additive) {
  692. emit_signal("node_unselected", gn);
  693. }
  694. gn->set_selected(box_selection_mode_additive);
  695. } else {
  696. bool select = (previus_selected.find(gn) != nullptr);
  697. if (gn->is_selected() && !select) {
  698. emit_signal("node_unselected", gn);
  699. } else if (!gn->is_selected() && select) {
  700. emit_signal("node_selected", gn);
  701. }
  702. gn->set_selected(select);
  703. }
  704. }
  705. top_layer->update();
  706. }
  707. Ref<InputEventMouseButton> b = p_ev;
  708. if (b.is_valid()) {
  709. if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
  710. if (box_selecting) {
  711. box_selecting = false;
  712. for (int i = get_child_count() - 1; i >= 0; i--) {
  713. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  714. if (!gn) {
  715. continue;
  716. }
  717. bool select = (previus_selected.find(gn) != nullptr);
  718. if (gn->is_selected() && !select) {
  719. emit_signal("node_unselected", gn);
  720. } else if (!gn->is_selected() && select) {
  721. emit_signal("node_selected", gn);
  722. }
  723. gn->set_selected(select);
  724. }
  725. top_layer->update();
  726. } else {
  727. if (connecting) {
  728. connecting = false;
  729. top_layer->update();
  730. } else {
  731. emit_signal("popup_request", b->get_global_position());
  732. }
  733. }
  734. }
  735. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
  736. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  737. //deselect current node
  738. for (int i = get_child_count() - 1; i >= 0; i--) {
  739. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  740. if (gn) {
  741. Rect2 r = gn->get_rect();
  742. r.size *= zoom;
  743. if (r.has_point(b->get_position())) {
  744. emit_signal("node_unselected", gn);
  745. gn->set_selected(false);
  746. }
  747. }
  748. }
  749. }
  750. if (drag_accum != Vector2()) {
  751. emit_signal("_begin_node_move");
  752. for (int i = get_child_count() - 1; i >= 0; i--) {
  753. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  754. if (gn && gn->is_selected()) {
  755. gn->set_drag(false);
  756. }
  757. }
  758. emit_signal("_end_node_move");
  759. }
  760. dragging = false;
  761. top_layer->update();
  762. update();
  763. connections_layer->update();
  764. }
  765. if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {
  766. GraphNode *gn = nullptr;
  767. for (int i = get_child_count() - 1; i >= 0; i--) {
  768. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  769. if (gn_selected) {
  770. if (gn_selected->is_resizing()) {
  771. continue;
  772. }
  773. if (gn_selected->has_point(b->get_position() - gn_selected->get_position())) {
  774. gn = gn_selected;
  775. break;
  776. }
  777. }
  778. }
  779. if (gn) {
  780. if (_filter_input(b->get_position())) {
  781. return;
  782. }
  783. dragging = true;
  784. drag_accum = Vector2();
  785. just_selected = !gn->is_selected();
  786. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  787. for (int i = 0; i < get_child_count(); i++) {
  788. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  789. if (o_gn) {
  790. if (o_gn == gn) {
  791. o_gn->set_selected(true);
  792. } else {
  793. if (o_gn->is_selected()) {
  794. emit_signal("node_unselected", o_gn);
  795. }
  796. o_gn->set_selected(false);
  797. }
  798. }
  799. }
  800. }
  801. gn->set_selected(true);
  802. for (int i = 0; i < get_child_count(); i++) {
  803. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  804. if (!o_gn) {
  805. continue;
  806. }
  807. if (o_gn->is_selected()) {
  808. o_gn->set_drag(true);
  809. }
  810. }
  811. } else {
  812. if (_filter_input(b->get_position())) {
  813. return;
  814. }
  815. if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  816. return;
  817. }
  818. box_selecting = true;
  819. box_selecting_from = b->get_position();
  820. if (b->get_control()) {
  821. box_selection_mode_additive = true;
  822. previus_selected.clear();
  823. for (int i = get_child_count() - 1; i >= 0; i--) {
  824. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  825. if (!gn2 || !gn2->is_selected()) {
  826. continue;
  827. }
  828. previus_selected.push_back(gn2);
  829. }
  830. } else if (b->get_shift()) {
  831. box_selection_mode_additive = false;
  832. previus_selected.clear();
  833. for (int i = get_child_count() - 1; i >= 0; i--) {
  834. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  835. if (!gn2 || !gn2->is_selected()) {
  836. continue;
  837. }
  838. previus_selected.push_back(gn2);
  839. }
  840. } else {
  841. box_selection_mode_additive = true;
  842. previus_selected.clear();
  843. for (int i = get_child_count() - 1; i >= 0; i--) {
  844. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  845. if (!gn2) {
  846. continue;
  847. }
  848. if (gn2->is_selected()) {
  849. emit_signal("node_unselected", gn2);
  850. }
  851. gn2->set_selected(false);
  852. }
  853. }
  854. }
  855. }
  856. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  857. box_selecting = false;
  858. previus_selected.clear();
  859. top_layer->update();
  860. }
  861. if (b->get_button_index() == BUTTON_WHEEL_UP && b->is_pressed()) {
  862. //too difficult to get right
  863. //set_zoom(zoom*ZOOM_SCALE);
  864. }
  865. if (b->get_button_index() == BUTTON_WHEEL_DOWN && b->is_pressed()) {
  866. //too difficult to get right
  867. //set_zoom(zoom/ZOOM_SCALE);
  868. }
  869. if (b->get_button_index() == BUTTON_WHEEL_UP && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  870. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  871. }
  872. if (b->get_button_index() == BUTTON_WHEEL_DOWN && !Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  873. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  874. }
  875. if (b->get_button_index() == BUTTON_WHEEL_RIGHT || (b->get_button_index() == BUTTON_WHEEL_DOWN && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  876. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * b->get_factor() / 8);
  877. }
  878. if (b->get_button_index() == BUTTON_WHEEL_LEFT || (b->get_button_index() == BUTTON_WHEEL_UP && Input::get_singleton()->is_key_pressed(KEY_SHIFT))) {
  879. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * b->get_factor() / 8);
  880. }
  881. }
  882. Ref<InputEventKey> k = p_ev;
  883. if (k.is_valid()) {
  884. if (k->get_keycode() == KEY_D && k->is_pressed() && k->get_command()) {
  885. emit_signal("duplicate_nodes_request");
  886. accept_event();
  887. }
  888. if (k->get_keycode() == KEY_C && k->is_pressed() && k->get_command()) {
  889. emit_signal("copy_nodes_request");
  890. accept_event();
  891. }
  892. if (k->get_keycode() == KEY_V && k->is_pressed() && k->get_command()) {
  893. emit_signal("paste_nodes_request");
  894. accept_event();
  895. }
  896. if (k->get_keycode() == KEY_DELETE && k->is_pressed()) {
  897. emit_signal("delete_nodes_request");
  898. accept_event();
  899. }
  900. }
  901. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  902. if (magnify_gesture.is_valid()) {
  903. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  904. }
  905. Ref<InputEventPanGesture> pan_gesture = p_ev;
  906. if (pan_gesture.is_valid()) {
  907. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  908. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  909. }
  910. }
  911. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  912. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  913. 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) {
  914. if (Math::is_equal_approx(E->get().activity, p_activity)) {
  915. //update only if changed
  916. top_layer->update();
  917. connections_layer->update();
  918. }
  919. E->get().activity = p_activity;
  920. return;
  921. }
  922. }
  923. }
  924. void GraphEdit::clear_connections() {
  925. connections.clear();
  926. update();
  927. connections_layer->update();
  928. }
  929. void GraphEdit::set_zoom(float p_zoom) {
  930. set_zoom_custom(p_zoom, get_size() / 2);
  931. }
  932. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  933. p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
  934. if (zoom == p_zoom) {
  935. return;
  936. }
  937. zoom_minus->set_disabled(zoom == MIN_ZOOM);
  938. zoom_plus->set_disabled(zoom == MAX_ZOOM);
  939. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  940. zoom = p_zoom;
  941. top_layer->update();
  942. _update_scroll();
  943. connections_layer->update();
  944. if (is_visible_in_tree()) {
  945. Vector2 ofs = sbofs * zoom - p_center;
  946. h_scroll->set_value(ofs.x);
  947. v_scroll->set_value(ofs.y);
  948. }
  949. update();
  950. }
  951. float GraphEdit::get_zoom() const {
  952. return zoom;
  953. }
  954. void GraphEdit::set_right_disconnects(bool p_enable) {
  955. right_disconnects = p_enable;
  956. }
  957. bool GraphEdit::is_right_disconnects_enabled() const {
  958. return right_disconnects;
  959. }
  960. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  961. valid_right_disconnect_types.insert(p_type);
  962. }
  963. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  964. valid_right_disconnect_types.erase(p_type);
  965. }
  966. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  967. valid_left_disconnect_types.insert(p_type);
  968. }
  969. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  970. valid_left_disconnect_types.erase(p_type);
  971. }
  972. Array GraphEdit::_get_connection_list() const {
  973. List<Connection> conns;
  974. get_connection_list(&conns);
  975. Array arr;
  976. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  977. Dictionary d;
  978. d["from"] = E->get().from;
  979. d["from_port"] = E->get().from_port;
  980. d["to"] = E->get().to;
  981. d["to_port"] = E->get().to_port;
  982. arr.push_back(d);
  983. }
  984. return arr;
  985. }
  986. void GraphEdit::_zoom_minus() {
  987. set_zoom(zoom / ZOOM_SCALE);
  988. }
  989. void GraphEdit::_zoom_reset() {
  990. set_zoom(1);
  991. }
  992. void GraphEdit::_zoom_plus() {
  993. set_zoom(zoom * ZOOM_SCALE);
  994. }
  995. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  996. ConnType ct;
  997. ct.type_a = p_type;
  998. ct.type_b = p_with_type;
  999. valid_connection_types.insert(ct);
  1000. }
  1001. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1002. ConnType ct;
  1003. ct.type_a = p_type;
  1004. ct.type_b = p_with_type;
  1005. valid_connection_types.erase(ct);
  1006. }
  1007. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1008. ConnType ct;
  1009. ct.type_a = p_type;
  1010. ct.type_b = p_with_type;
  1011. return valid_connection_types.has(ct);
  1012. }
  1013. void GraphEdit::set_use_snap(bool p_enable) {
  1014. snap_button->set_pressed(p_enable);
  1015. update();
  1016. }
  1017. bool GraphEdit::is_using_snap() const {
  1018. return snap_button->is_pressed();
  1019. }
  1020. int GraphEdit::get_snap() const {
  1021. return snap_amount->get_value();
  1022. }
  1023. void GraphEdit::set_snap(int p_snap) {
  1024. ERR_FAIL_COND(p_snap < 5);
  1025. snap_amount->set_value(p_snap);
  1026. update();
  1027. }
  1028. void GraphEdit::_snap_toggled() {
  1029. update();
  1030. }
  1031. void GraphEdit::_snap_value_changed(double) {
  1032. update();
  1033. }
  1034. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1035. return zoom_hb;
  1036. }
  1037. void GraphEdit::_bind_methods() {
  1038. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  1039. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  1040. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  1041. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1042. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1043. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1044. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1045. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  1046. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1047. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1048. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1049. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1050. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1051. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1052. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1053. ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
  1054. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1055. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1056. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1057. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1058. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1059. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1060. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1061. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  1062. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1063. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1064. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1065. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1066. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  1067. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  1068. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1069. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1070. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1071. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1072. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1073. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1074. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1075. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1076. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1077. ADD_SIGNAL(MethodInfo("node_unselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1078. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1079. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1080. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  1081. ADD_SIGNAL(MethodInfo("_begin_node_move"));
  1082. ADD_SIGNAL(MethodInfo("_end_node_move"));
  1083. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  1084. }
  1085. GraphEdit::GraphEdit() {
  1086. set_focus_mode(FOCUS_ALL);
  1087. awaiting_scroll_offset_update = false;
  1088. top_layer = nullptr;
  1089. top_layer = memnew(GraphEditFilter(this));
  1090. add_child(top_layer);
  1091. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1092. top_layer->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1093. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1094. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1095. connections_layer = memnew(Control);
  1096. add_child(connections_layer);
  1097. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1098. connections_layer->set_name("CLAYER");
  1099. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1100. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1101. h_scroll = memnew(HScrollBar);
  1102. h_scroll->set_name("_h_scroll");
  1103. top_layer->add_child(h_scroll);
  1104. v_scroll = memnew(VScrollBar);
  1105. v_scroll->set_name("_v_scroll");
  1106. top_layer->add_child(v_scroll);
  1107. updating = false;
  1108. connecting = false;
  1109. right_disconnects = false;
  1110. box_selecting = false;
  1111. dragging = false;
  1112. //set large minmax so it can scroll even if not resized yet
  1113. h_scroll->set_min(-10000);
  1114. h_scroll->set_max(10000);
  1115. v_scroll->set_min(-10000);
  1116. v_scroll->set_max(10000);
  1117. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1118. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1119. zoom = 1;
  1120. zoom_hb = memnew(HBoxContainer);
  1121. top_layer->add_child(zoom_hb);
  1122. zoom_hb->set_position(Vector2(10, 10));
  1123. zoom_minus = memnew(Button);
  1124. zoom_minus->set_flat(true);
  1125. zoom_hb->add_child(zoom_minus);
  1126. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1127. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1128. zoom_minus->set_focus_mode(FOCUS_NONE);
  1129. zoom_reset = memnew(Button);
  1130. zoom_reset->set_flat(true);
  1131. zoom_hb->add_child(zoom_reset);
  1132. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1133. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1134. zoom_reset->set_focus_mode(FOCUS_NONE);
  1135. zoom_plus = memnew(Button);
  1136. zoom_plus->set_flat(true);
  1137. zoom_hb->add_child(zoom_plus);
  1138. zoom_plus->set_tooltip(RTR("Zoom In"));
  1139. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1140. zoom_plus->set_focus_mode(FOCUS_NONE);
  1141. snap_button = memnew(Button);
  1142. snap_button->set_flat(true);
  1143. snap_button->set_toggle_mode(true);
  1144. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1145. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  1146. snap_button->set_pressed(true);
  1147. snap_button->set_focus_mode(FOCUS_NONE);
  1148. zoom_hb->add_child(snap_button);
  1149. snap_amount = memnew(SpinBox);
  1150. snap_amount->set_min(5);
  1151. snap_amount->set_max(100);
  1152. snap_amount->set_step(1);
  1153. snap_amount->set_value(20);
  1154. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  1155. zoom_hb->add_child(snap_amount);
  1156. setting_scroll_ofs = false;
  1157. just_disconnected = false;
  1158. set_clip_contents(true);
  1159. }