graph_edit.cpp 43 KB

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