graph_edit.cpp 57 KB

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