graph_edit.cpp 58 KB

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