graph_edit.cpp 41 KB

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