graph_edit.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  1. /**************************************************************************/
  2. /* graph_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "scene/gui/graph_edit_arranger.h"
  37. #include "scene/gui/view_panner.h"
  38. #include "scene/resources/style_box_flat.h"
  39. #include "scene/theme/theme_db.h"
  40. constexpr int MINIMAP_OFFSET = 12;
  41. constexpr int MINIMAP_PADDING = 5;
  42. constexpr int MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION = 20;
  43. constexpr int MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES = 5;
  44. constexpr int GRID_MINOR_STEPS_PER_MAJOR_LINE = 10;
  45. constexpr int GRID_MIN_SNAPPING_DISTANCE = 2;
  46. constexpr int GRID_MAX_SNAPPING_DISTANCE = 100;
  47. constexpr float CONNECTING_TARGET_LINE_COLOR_BRIGHTENING = 0.4;
  48. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  49. return ge->_filter_input(p_point);
  50. }
  51. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  52. ge = p_edit;
  53. }
  54. Control::CursorShape GraphEditMinimap::get_cursor_shape(const Point2 &p_pos) const {
  55. if (is_resizing || (p_pos.x < theme_cache.resizer->get_width() && p_pos.y < theme_cache.resizer->get_height())) {
  56. return CURSOR_FDIAGSIZE;
  57. }
  58. return Control::get_cursor_shape(p_pos);
  59. }
  60. void GraphEditMinimap::update_minimap() {
  61. Vector2 graph_offset = _get_graph_offset();
  62. Vector2 graph_size = _get_graph_size();
  63. camera_position = ge->get_scroll_offset() - graph_offset;
  64. camera_size = ge->get_size();
  65. Vector2 render_size = _get_render_size();
  66. float target_ratio = render_size.width / render_size.height;
  67. float graph_ratio = graph_size.width / graph_size.height;
  68. graph_proportions = graph_size;
  69. graph_padding = Vector2(0, 0);
  70. if (graph_ratio > target_ratio) {
  71. graph_proportions.width = graph_size.width;
  72. graph_proportions.height = graph_size.width / target_ratio;
  73. graph_padding.y = Math::abs(graph_size.height - graph_proportions.y) / 2;
  74. } else {
  75. graph_proportions.width = graph_size.height * target_ratio;
  76. graph_proportions.height = graph_size.height;
  77. graph_padding.x = Math::abs(graph_size.width - graph_proportions.x) / 2;
  78. }
  79. // This centers minimap inside the minimap rectangle.
  80. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  81. }
  82. Rect2 GraphEditMinimap::get_camera_rect() {
  83. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  84. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  85. Vector2 camera_pos = (camera_center - camera_viewport / 2);
  86. return Rect2(camera_pos, camera_viewport);
  87. }
  88. Vector2 GraphEditMinimap::_get_render_size() {
  89. if (!is_inside_tree()) {
  90. return Vector2(0, 0);
  91. }
  92. return get_size() - 2 * minimap_padding;
  93. }
  94. Vector2 GraphEditMinimap::_get_graph_offset() {
  95. return Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  96. }
  97. Vector2 GraphEditMinimap::_get_graph_size() {
  98. Vector2 graph_size = Vector2(ge->h_scrollbar->get_max(), ge->v_scrollbar->get_max()) - Vector2(ge->h_scrollbar->get_min(), ge->v_scrollbar->get_min());
  99. if (graph_size.width == 0) {
  100. graph_size.width = 1;
  101. }
  102. if (graph_size.height == 0) {
  103. graph_size.height = 1;
  104. }
  105. return graph_size;
  106. }
  107. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  108. Vector2 map_position = Vector2(0, 0);
  109. Vector2 render_size = _get_render_size();
  110. map_position.x = p_position.x * render_size.width / graph_proportions.x;
  111. map_position.y = p_position.y * render_size.height / graph_proportions.y;
  112. return map_position;
  113. }
  114. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  115. Vector2 graph_position = Vector2(0, 0);
  116. Vector2 render_size = _get_render_size();
  117. graph_position.x = p_position.x * graph_proportions.x / render_size.width;
  118. graph_position.y = p_position.y * graph_proportions.y / render_size.height;
  119. return graph_position;
  120. }
  121. void GraphEditMinimap::gui_input(const Ref<InputEvent> &p_ev) {
  122. ERR_FAIL_COND(p_ev.is_null());
  123. if (!ge->is_minimap_enabled()) {
  124. return;
  125. }
  126. Ref<InputEventMouseButton> mb = p_ev;
  127. Ref<InputEventMouseMotion> mm = p_ev;
  128. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  129. if (mb->is_pressed()) {
  130. is_pressing = true;
  131. Rect2 resizer_hitbox = Rect2(Point2(), theme_cache.resizer->get_size());
  132. if (resizer_hitbox.has_point(mb->get_position())) {
  133. is_resizing = true;
  134. } else {
  135. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  136. _adjust_graph_scroll(click_position);
  137. }
  138. } else {
  139. is_pressing = false;
  140. is_resizing = false;
  141. }
  142. accept_event();
  143. } else if (mm.is_valid() && is_pressing) {
  144. if (is_resizing) {
  145. // Prevent setting minimap wider than GraphEdit.
  146. Vector2 new_minimap_size;
  147. new_minimap_size.width = MIN(get_size().width - mm->get_relative().x, ge->get_size().width - 2.0 * minimap_padding.x);
  148. new_minimap_size.height = MIN(get_size().height - mm->get_relative().y, ge->get_size().height - 2.0 * minimap_padding.y);
  149. ge->set_minimap_size(new_minimap_size);
  150. queue_redraw();
  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_offset(p_offset + graph_offset - camera_size / 2);
  161. }
  162. void GraphEditMinimap::_bind_methods() {
  163. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, panel);
  164. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, node_style, "node");
  165. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, GraphEditMinimap, camera_style, "camera");
  166. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEditMinimap, resizer);
  167. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEditMinimap, resizer_color);
  168. }
  169. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  170. ge = p_edit;
  171. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  172. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  173. }
  174. Control::CursorShape GraphEdit::get_cursor_shape(const Point2 &p_pos) const {
  175. if (moving_selection) {
  176. return CURSOR_MOVE;
  177. }
  178. return Control::get_cursor_shape(p_pos);
  179. }
  180. PackedStringArray GraphEdit::get_configuration_warnings() const {
  181. PackedStringArray warnings = Control::get_configuration_warnings();
  182. warnings.push_back(RTR("Please be aware that GraphEdit and GraphNode will undergo extensive refactoring in a future 4.x version involving compatibility-breaking API changes."));
  183. return warnings;
  184. }
  185. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  186. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  187. return OK;
  188. }
  189. Connection c;
  190. c.from_node = p_from;
  191. c.from_port = p_from_port;
  192. c.to_node = p_to;
  193. c.to_port = p_to_port;
  194. c.activity = 0;
  195. connections.push_back(c);
  196. top_layer->queue_redraw();
  197. minimap->queue_redraw();
  198. queue_redraw();
  199. connections_layer->queue_redraw();
  200. return OK;
  201. }
  202. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  203. for (const Connection &E : connections) {
  204. if (E.from_node == p_from && E.from_port == p_from_port && E.to_node == p_to && E.to_port == p_to_port) {
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  211. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  212. if (E->get().from_node == p_from && E->get().from_port == p_from_port && E->get().to_node == p_to && E->get().to_port == p_to_port) {
  213. connections.erase(E);
  214. top_layer->queue_redraw();
  215. minimap->queue_redraw();
  216. queue_redraw();
  217. connections_layer->queue_redraw();
  218. return;
  219. }
  220. }
  221. }
  222. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  223. *r_connections = connections;
  224. }
  225. void GraphEdit::set_scroll_offset(const Vector2 &p_offset) {
  226. setting_scroll_offset = true;
  227. h_scrollbar->set_value(p_offset.x);
  228. v_scrollbar->set_value(p_offset.y);
  229. _update_scroll();
  230. setting_scroll_offset = false;
  231. }
  232. Vector2 GraphEdit::get_scroll_offset() const {
  233. return Vector2(h_scrollbar->get_value(), v_scrollbar->get_value());
  234. }
  235. void GraphEdit::_scroll_moved(double) {
  236. if (!awaiting_scroll_offset_update) {
  237. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  238. awaiting_scroll_offset_update = true;
  239. }
  240. top_layer->queue_redraw();
  241. minimap->queue_redraw();
  242. queue_redraw();
  243. }
  244. void GraphEdit::_update_scroll_offset() {
  245. set_block_minimum_size_adjust(true);
  246. for (int i = 0; i < get_child_count(); i++) {
  247. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  248. if (!graph_element) {
  249. continue;
  250. }
  251. Point2 pos = graph_element->get_position_offset() * zoom;
  252. pos -= Point2(h_scrollbar->get_value(), v_scrollbar->get_value());
  253. graph_element->set_position(pos);
  254. if (graph_element->get_scale() != Vector2(zoom, zoom)) {
  255. graph_element->set_scale(Vector2(zoom, zoom));
  256. }
  257. }
  258. connections_layer->set_position(-Point2(h_scrollbar->get_value(), v_scrollbar->get_value()));
  259. set_block_minimum_size_adjust(false);
  260. awaiting_scroll_offset_update = false;
  261. // In Godot, signals on value change are avoided by convention.
  262. if (!setting_scroll_offset) {
  263. emit_signal(SNAME("scroll_offset_changed"), get_scroll_offset());
  264. }
  265. }
  266. void GraphEdit::_update_scroll() {
  267. if (updating) {
  268. return;
  269. }
  270. updating = true;
  271. set_block_minimum_size_adjust(true);
  272. Rect2 screen_rect;
  273. for (int i = 0; i < get_child_count(); i++) {
  274. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  275. if (!graph_element) {
  276. continue;
  277. }
  278. Rect2 node_rect;
  279. node_rect.position = graph_element->get_position_offset() * zoom;
  280. node_rect.size = graph_element->get_size() * zoom;
  281. screen_rect = screen_rect.merge(node_rect);
  282. }
  283. screen_rect.position -= get_size();
  284. screen_rect.size += get_size() * 2.0;
  285. h_scrollbar->set_min(screen_rect.position.x);
  286. h_scrollbar->set_max(screen_rect.position.x + screen_rect.size.width);
  287. h_scrollbar->set_page(get_size().x);
  288. if (h_scrollbar->get_max() - h_scrollbar->get_min() <= h_scrollbar->get_page()) {
  289. h_scrollbar->hide();
  290. } else {
  291. h_scrollbar->show();
  292. }
  293. v_scrollbar->set_min(screen_rect.position.y);
  294. v_scrollbar->set_max(screen_rect.position.y + screen_rect.size.height);
  295. v_scrollbar->set_page(get_size().height);
  296. if (v_scrollbar->get_max() - v_scrollbar->get_min() <= v_scrollbar->get_page()) {
  297. v_scrollbar->hide();
  298. } else {
  299. v_scrollbar->show();
  300. }
  301. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  302. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  303. // Avoid scrollbar overlapping.
  304. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scrollbar->is_visible() ? -vmin.width : 0);
  305. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scrollbar->is_visible() ? -hmin.height : 0);
  306. set_block_minimum_size_adjust(false);
  307. if (!awaiting_scroll_offset_update) {
  308. callable_mp(this, &GraphEdit::_update_scroll_offset).call_deferred();
  309. awaiting_scroll_offset_update = true;
  310. }
  311. updating = false;
  312. }
  313. void GraphEdit::_graph_element_moved_to_front(Node *p_node) {
  314. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  315. ERR_FAIL_NULL(graph_element);
  316. graph_element->move_to_front();
  317. }
  318. void GraphEdit::_graph_element_selected(Node *p_node) {
  319. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  320. ERR_FAIL_NULL(graph_element);
  321. emit_signal(SNAME("node_selected"), graph_element);
  322. }
  323. void GraphEdit::_graph_element_deselected(Node *p_node) {
  324. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  325. ERR_FAIL_NULL(graph_element);
  326. emit_signal(SNAME("node_deselected"), graph_element);
  327. }
  328. void GraphEdit::_graph_element_resized(Vector2 p_new_minsize, Node *p_node) {
  329. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  330. ERR_FAIL_NULL(graph_element);
  331. graph_element->set_size(p_new_minsize);
  332. }
  333. void GraphEdit::_graph_element_moved(Node *p_node) {
  334. GraphElement *graph_element = Object::cast_to<GraphElement>(p_node);
  335. ERR_FAIL_NULL(graph_element);
  336. top_layer->queue_redraw();
  337. minimap->queue_redraw();
  338. queue_redraw();
  339. connections_layer->queue_redraw();
  340. }
  341. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_node) {
  342. GraphNode *graph_node = Object::cast_to<GraphNode>(p_node);
  343. ERR_FAIL_NULL(graph_node);
  344. top_layer->queue_redraw();
  345. minimap->queue_redraw();
  346. queue_redraw();
  347. connections_layer->queue_redraw();
  348. }
  349. void GraphEdit::add_child_notify(Node *p_child) {
  350. Control::add_child_notify(p_child);
  351. // Keep the top layer always on top!
  352. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  353. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  354. if (graph_element) {
  355. graph_element->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved).bind(graph_element));
  356. graph_element->connect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected).bind(graph_element));
  357. graph_element->connect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected).bind(graph_element));
  358. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  359. if (graph_node) {
  360. graph_element->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated).bind(graph_element));
  361. }
  362. graph_element->connect("raise_request", callable_mp(this, &GraphEdit::_graph_element_moved_to_front).bind(graph_element));
  363. graph_element->connect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resized).bind(graph_element));
  364. graph_element->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  365. graph_element->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  366. graph_element->set_scale(Vector2(zoom, zoom));
  367. _graph_element_moved(graph_element);
  368. graph_element->set_mouse_filter(MOUSE_FILTER_PASS);
  369. }
  370. }
  371. void GraphEdit::remove_child_notify(Node *p_child) {
  372. Control::remove_child_notify(p_child);
  373. if (p_child == top_layer) {
  374. top_layer = nullptr;
  375. minimap = nullptr;
  376. } else if (p_child == connections_layer) {
  377. connections_layer = nullptr;
  378. }
  379. if (top_layer != nullptr && is_inside_tree()) {
  380. // Keep the top layer always on top!
  381. callable_mp((CanvasItem *)top_layer, &CanvasItem::move_to_front).call_deferred();
  382. }
  383. GraphElement *graph_element = Object::cast_to<GraphElement>(p_child);
  384. if (graph_element) {
  385. graph_element->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_element_moved));
  386. graph_element->disconnect("node_selected", callable_mp(this, &GraphEdit::_graph_element_selected));
  387. graph_element->disconnect("node_deselected", callable_mp(this, &GraphEdit::_graph_element_deselected));
  388. GraphNode *graph_node = Object::cast_to<GraphNode>(graph_element);
  389. if (graph_node) {
  390. graph_element->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  391. }
  392. graph_element->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_element_moved_to_front));
  393. graph_element->disconnect("resize_request", callable_mp(this, &GraphEdit::_graph_element_resized));
  394. // In case of the whole GraphEdit being destroyed these references can already be freed.
  395. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  396. graph_element->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::queue_redraw));
  397. }
  398. if (minimap != nullptr && minimap->is_inside_tree()) {
  399. graph_element->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::queue_redraw));
  400. }
  401. }
  402. }
  403. void GraphEdit::_update_theme_item_cache() {
  404. Control::_update_theme_item_cache();
  405. theme_cache.base_scale = get_theme_default_base_scale();
  406. }
  407. void GraphEdit::_notification(int p_what) {
  408. switch (p_what) {
  409. case NOTIFICATION_THEME_CHANGED: {
  410. zoom_minus_button->set_icon(theme_cache.zoom_out);
  411. zoom_reset_button->set_icon(theme_cache.zoom_reset);
  412. zoom_plus_button->set_icon(theme_cache.zoom_in);
  413. toggle_snapping_button->set_icon(theme_cache.snapping_toggle);
  414. show_grid_button->set_icon(theme_cache.grid_toggle);
  415. minimap_button->set_icon(theme_cache.minimap_toggle);
  416. layout_button->set_icon(theme_cache.layout);
  417. zoom_label->set_custom_minimum_size(Size2(48, 0) * theme_cache.base_scale);
  418. } break;
  419. case NOTIFICATION_READY: {
  420. Size2 hmin = h_scrollbar->get_combined_minimum_size();
  421. Size2 vmin = v_scrollbar->get_combined_minimum_size();
  422. h_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  423. h_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  424. h_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -hmin.height);
  425. h_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  426. v_scrollbar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -vmin.width);
  427. v_scrollbar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  428. v_scrollbar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
  429. v_scrollbar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  430. } break;
  431. case NOTIFICATION_DRAW: {
  432. // Draw background fill.
  433. draw_style_box(theme_cache.panel, Rect2(Point2(), get_size()));
  434. // Draw background grid.
  435. if (show_grid) {
  436. Vector2 offset = get_scroll_offset() / zoom;
  437. Size2 size = get_size() / zoom;
  438. Point2i from_pos = (offset / float(snapping_distance)).floor();
  439. Point2i len = (size / float(snapping_distance)).floor() + Vector2(1, 1);
  440. for (int i = from_pos.x; i < from_pos.x + len.x; i++) {
  441. Color color;
  442. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  443. color = theme_cache.grid_major;
  444. } else {
  445. color = theme_cache.grid_minor;
  446. }
  447. float base_offset = i * snapping_distance * zoom - offset.x * zoom;
  448. draw_line(Vector2(base_offset, 0), Vector2(base_offset, get_size().height), color);
  449. }
  450. for (int i = from_pos.y; i < from_pos.y + len.y; i++) {
  451. Color color;
  452. if (ABS(i) % GRID_MINOR_STEPS_PER_MAJOR_LINE == 0) {
  453. color = theme_cache.grid_major;
  454. } else {
  455. color = theme_cache.grid_minor;
  456. }
  457. float base_offset = i * snapping_distance * zoom - offset.y * zoom;
  458. draw_line(Vector2(0, base_offset), Vector2(get_size().width, base_offset), color);
  459. }
  460. }
  461. } break;
  462. case NOTIFICATION_RESIZED: {
  463. _update_scroll();
  464. top_layer->queue_redraw();
  465. minimap->queue_redraw();
  466. } break;
  467. }
  468. }
  469. bool GraphEdit::_filter_input(const Point2 &p_point) {
  470. for (int i = get_child_count() - 1; i >= 0; i--) {
  471. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  472. if (!graph_node || !graph_node->is_visible_in_tree()) {
  473. continue;
  474. }
  475. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  476. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  477. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  478. // Determine slot height.
  479. int slot_index = graph_node->get_input_port_slot(j);
  480. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  481. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  482. if (is_in_input_hotzone(graph_node, j, p_point / zoom, port_size)) {
  483. return true;
  484. }
  485. }
  486. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  487. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  488. // Determine slot height.
  489. int slot_index = graph_node->get_output_port_slot(j);
  490. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  491. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  492. if (is_in_output_hotzone(graph_node, j, p_point / zoom, port_size)) {
  493. return true;
  494. }
  495. }
  496. }
  497. return false;
  498. }
  499. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  500. Ref<InputEventMouseButton> mb = p_ev;
  501. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  502. connecting_valid = false;
  503. click_pos = mb->get_position() / zoom;
  504. for (int i = get_child_count() - 1; i >= 0; i--) {
  505. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  506. if (!graph_node || !graph_node->is_visible_in_tree()) {
  507. continue;
  508. }
  509. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  510. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  511. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  512. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  513. // Determine slot height.
  514. int slot_index = graph_node->get_output_port_slot(j);
  515. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  516. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  517. if (is_in_output_hotzone(graph_node, j, click_pos, port_size)) {
  518. if (valid_left_disconnect_types.has(graph_node->get_output_port_type(j))) {
  519. // Check disconnect.
  520. for (const Connection &E : connections) {
  521. if (E.from_node == graph_node->get_name() && E.from_port == j) {
  522. Node *to = get_node(NodePath(E.to_node));
  523. if (Object::cast_to<GraphNode>(to)) {
  524. connecting_from = E.to_node;
  525. connecting_index = E.to_port;
  526. connecting_out = false;
  527. connecting_type = Object::cast_to<GraphNode>(to)->get_input_port_type(E.to_port);
  528. connecting_color = Object::cast_to<GraphNode>(to)->get_input_port_color(E.to_port);
  529. connecting_target = false;
  530. connecting_to = pos;
  531. if (connecting_type >= 0) {
  532. just_disconnected = true;
  533. emit_signal(SNAME("disconnection_request"), E.from_node, E.from_port, E.to_node, E.to_port);
  534. to = get_node(NodePath(connecting_from)); // Maybe it was erased.
  535. if (Object::cast_to<GraphNode>(to)) {
  536. connecting = true;
  537. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  538. }
  539. }
  540. return;
  541. }
  542. }
  543. }
  544. }
  545. connecting_from = graph_node->get_name();
  546. connecting_index = j;
  547. connecting_out = true;
  548. connecting_type = graph_node->get_output_port_type(j);
  549. connecting_color = graph_node->get_output_port_color(j);
  550. connecting_target = false;
  551. connecting_to = pos;
  552. if (connecting_type >= 0) {
  553. connecting = true;
  554. just_disconnected = false;
  555. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  556. }
  557. return;
  558. }
  559. }
  560. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  561. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  562. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  563. // Determine slot height.
  564. int slot_index = graph_node->get_input_port_slot(j);
  565. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  566. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  567. if (is_in_input_hotzone(graph_node, j, click_pos, port_size)) {
  568. if (right_disconnects || valid_right_disconnect_types.has(graph_node->get_input_port_type(j))) {
  569. // Check disconnect.
  570. for (const Connection &E : connections) {
  571. if (E.to_node == graph_node->get_name() && E.to_port == j) {
  572. Node *fr = get_node(NodePath(E.from_node));
  573. if (Object::cast_to<GraphNode>(fr)) {
  574. connecting_from = E.from_node;
  575. connecting_index = E.from_port;
  576. connecting_out = true;
  577. connecting_type = Object::cast_to<GraphNode>(fr)->get_output_port_type(E.from_port);
  578. connecting_color = Object::cast_to<GraphNode>(fr)->get_output_port_color(E.from_port);
  579. connecting_target = false;
  580. connecting_to = pos;
  581. just_disconnected = true;
  582. if (connecting_type >= 0) {
  583. emit_signal(SNAME("disconnection_request"), E.from_node, E.from_port, E.to_node, E.to_port);
  584. fr = get_node(NodePath(connecting_from));
  585. if (Object::cast_to<GraphNode>(fr)) {
  586. connecting = true;
  587. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, true);
  588. }
  589. }
  590. return;
  591. }
  592. }
  593. }
  594. }
  595. connecting_from = graph_node->get_name();
  596. connecting_index = j;
  597. connecting_out = false;
  598. connecting_type = graph_node->get_input_port_type(j);
  599. connecting_color = graph_node->get_input_port_color(j);
  600. connecting_target = false;
  601. connecting_to = pos;
  602. if (connecting_type >= 0) {
  603. connecting = true;
  604. just_disconnected = false;
  605. emit_signal(SNAME("connection_drag_started"), connecting_from, connecting_index, false);
  606. }
  607. return;
  608. }
  609. }
  610. }
  611. }
  612. Ref<InputEventMouseMotion> mm = p_ev;
  613. if (mm.is_valid() && connecting) {
  614. connecting_to = mm->get_position();
  615. connecting_target = false;
  616. top_layer->queue_redraw();
  617. minimap->queue_redraw();
  618. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > MIN_DRAG_DISTANCE_FOR_VALID_CONNECTION;
  619. if (connecting_valid) {
  620. Vector2 mpos = mm->get_position() / zoom;
  621. for (int i = get_child_count() - 1; i >= 0; i--) {
  622. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  623. if (!graph_node || !graph_node->is_visible_in_tree()) {
  624. continue;
  625. }
  626. Ref<Texture2D> port_icon = graph_node->theme_cache.port;
  627. if (!connecting_out) {
  628. for (int j = 0; j < graph_node->get_output_port_count(); j++) {
  629. Vector2 pos = graph_node->get_output_port_position(j) * zoom + graph_node->get_position();
  630. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  631. // Determine slot height.
  632. int slot_index = graph_node->get_output_port_slot(j);
  633. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  634. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  635. int type = graph_node->get_output_port_type(j);
  636. if ((type == connecting_type ||
  637. valid_connection_types.has(ConnectionType(type, connecting_type))) &&
  638. is_in_output_hotzone(graph_node, j, mpos, port_size)) {
  639. if (!is_node_hover_valid(graph_node->get_name(), j, connecting_from, connecting_index)) {
  640. continue;
  641. }
  642. connecting_target = true;
  643. connecting_to = pos;
  644. connecting_target_to = graph_node->get_name();
  645. connecting_target_index = j;
  646. return;
  647. }
  648. }
  649. } else {
  650. for (int j = 0; j < graph_node->get_input_port_count(); j++) {
  651. Vector2 pos = graph_node->get_input_port_position(j) * zoom + graph_node->get_position();
  652. Vector2i port_size = Vector2i(port_icon->get_width(), port_icon->get_height());
  653. // Determine slot height.
  654. int slot_index = graph_node->get_input_port_slot(j);
  655. Control *child = Object::cast_to<Control>(graph_node->get_child(slot_index));
  656. port_size.height = MAX(port_size.height, child ? child->get_size().y : 0);
  657. int type = graph_node->get_input_port_type(j);
  658. if ((type == connecting_type || valid_connection_types.has(ConnectionType(connecting_type, type))) &&
  659. is_in_input_hotzone(graph_node, j, mpos, port_size)) {
  660. if (!is_node_hover_valid(connecting_from, connecting_index, graph_node->get_name(), j)) {
  661. continue;
  662. }
  663. connecting_target = true;
  664. connecting_to = pos;
  665. connecting_target_to = graph_node->get_name();
  666. connecting_target_index = j;
  667. return;
  668. }
  669. }
  670. }
  671. }
  672. }
  673. }
  674. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  675. if (connecting_valid) {
  676. if (connecting && connecting_target) {
  677. if (connecting_out) {
  678. emit_signal(SNAME("connection_request"), connecting_from, connecting_index, connecting_target_to, connecting_target_index);
  679. } else {
  680. emit_signal(SNAME("connection_request"), connecting_target_to, connecting_target_index, connecting_from, connecting_index);
  681. }
  682. } else if (!just_disconnected) {
  683. if (connecting_out) {
  684. emit_signal(SNAME("connection_to_empty"), connecting_from, connecting_index, mb->get_position());
  685. } else {
  686. emit_signal(SNAME("connection_from_empty"), connecting_from, connecting_index, mb->get_position());
  687. }
  688. }
  689. }
  690. if (connecting) {
  691. force_connection_drag_end();
  692. }
  693. }
  694. }
  695. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &mpos, const Vector2 &p_offset) {
  696. if (p_control->is_set_as_top_level() || !p_control->is_visible() || !p_control->is_inside_tree()) {
  697. return false;
  698. }
  699. Rect2 control_rect = p_control->get_rect();
  700. control_rect.position *= zoom;
  701. control_rect.size *= zoom;
  702. control_rect.position += p_offset;
  703. if (!control_rect.has_point(mpos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  704. // Test children.
  705. for (int i = 0; i < p_control->get_child_count(); i++) {
  706. Control *child_rect = Object::cast_to<Control>(p_control->get_child(i));
  707. if (!child_rect) {
  708. continue;
  709. }
  710. if (_check_clickable_control(child_rect, mpos, control_rect.position)) {
  711. return true;
  712. }
  713. }
  714. return false;
  715. } else {
  716. return true;
  717. }
  718. }
  719. bool GraphEdit::is_in_input_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  720. bool success;
  721. if (GDVIRTUAL_CALL(_is_in_input_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  722. return success;
  723. } else {
  724. Vector2 pos = p_graph_node->get_input_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  725. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, true);
  726. }
  727. }
  728. bool GraphEdit::is_in_output_hotzone(GraphNode *p_graph_node, int p_port_idx, const Vector2 &p_mouse_pos, const Vector2i &p_port_size) {
  729. if (p_graph_node->is_resizable()) {
  730. Ref<Texture2D> resizer = p_graph_node->theme_cache.resizer;
  731. Rect2 resizer_rect = Rect2(p_graph_node->get_position() / zoom + p_graph_node->get_size() - resizer->get_size(), resizer->get_size());
  732. if (resizer_rect.has_point(p_mouse_pos)) {
  733. return false;
  734. }
  735. }
  736. bool success;
  737. if (GDVIRTUAL_CALL(_is_in_output_hotzone, p_graph_node, p_port_idx, p_mouse_pos, success)) {
  738. return success;
  739. } else {
  740. Vector2 pos = p_graph_node->get_output_port_position(p_port_idx) * zoom + p_graph_node->get_position();
  741. return is_in_port_hotzone(pos / zoom, p_mouse_pos, p_port_size, false);
  742. }
  743. }
  744. bool GraphEdit::is_in_port_hotzone(const Vector2 &p_pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  745. Rect2 hotzone = Rect2(
  746. p_pos.x - (p_left ? theme_cache.port_hotzone_outer_extent : theme_cache.port_hotzone_inner_extent),
  747. p_pos.y - p_port_size.height / 2.0,
  748. theme_cache.port_hotzone_inner_extent + theme_cache.port_hotzone_outer_extent,
  749. p_port_size.height);
  750. if (!hotzone.has_point(p_mouse_pos)) {
  751. return false;
  752. }
  753. for (int i = 0; i < get_child_count(); i++) {
  754. GraphNode *child = Object::cast_to<GraphNode>(get_child(i));
  755. if (!child) {
  756. continue;
  757. }
  758. Rect2 child_rect = child->get_rect();
  759. if (child_rect.has_point(p_mouse_pos * zoom)) {
  760. for (int j = 0; j < child->get_child_count(); j++) {
  761. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  762. if (!subchild) {
  763. continue;
  764. }
  765. if (_check_clickable_control(subchild, p_mouse_pos * zoom, child_rect.position)) {
  766. return false;
  767. }
  768. }
  769. }
  770. }
  771. return true;
  772. }
  773. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) {
  774. Vector<Vector2> ret;
  775. if (GDVIRTUAL_CALL(_get_connection_line, p_from, p_to, ret)) {
  776. return ret;
  777. }
  778. float x_diff = (p_to.x - p_from.x);
  779. float cp_offset = x_diff * lines_curvature;
  780. if (x_diff < 0) {
  781. cp_offset *= -1;
  782. }
  783. Curve2D curve;
  784. curve.add_point(p_from);
  785. curve.set_point_out(0, Vector2(cp_offset, 0));
  786. curve.add_point(p_to);
  787. curve.set_point_in(1, Vector2(-cp_offset, 0));
  788. if (lines_curvature > 0) {
  789. return curve.tessellate(MAX_CONNECTION_LINE_CURVE_TESSELATION_STAGES, 2.0);
  790. } else {
  791. return curve.tessellate(1);
  792. }
  793. }
  794. void GraphEdit::_draw_connection_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_zoom) {
  795. Vector<Vector2> points = get_connection_line(p_from / p_zoom, p_to / p_zoom);
  796. Vector<Vector2> scaled_points;
  797. Vector<Color> colors;
  798. float length = (p_from / p_zoom).distance_to(p_to / p_zoom);
  799. for (int i = 0; i < points.size(); i++) {
  800. float d = (p_from / p_zoom).distance_to(points[i]) / length;
  801. colors.push_back(p_color.lerp(p_to_color, d));
  802. scaled_points.push_back(points[i] * p_zoom);
  803. }
  804. // Thickness below 0.5 doesn't look good on the graph or its minimap.
  805. p_where->draw_polyline_colors(scaled_points, colors, MAX(0.5, Math::floor(p_width * theme_cache.base_scale)), lines_antialiased);
  806. }
  807. void GraphEdit::_connections_layer_draw() {
  808. // Draw connections.
  809. List<List<Connection>::Element *> to_erase;
  810. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  811. const Connection &c = E->get();
  812. Node *from = get_node(NodePath(c.from_node));
  813. GraphNode *gnode_from = Object::cast_to<GraphNode>(from);
  814. if (!gnode_from) {
  815. to_erase.push_back(E);
  816. continue;
  817. }
  818. Node *to = get_node(NodePath(c.to_node));
  819. GraphNode *gnode_to = Object::cast_to<GraphNode>(to);
  820. if (!gnode_to) {
  821. to_erase.push_back(E);
  822. continue;
  823. }
  824. Vector2 frompos = gnode_from->get_output_port_position(c.from_port) * zoom + gnode_from->get_position_offset() * zoom;
  825. Color color = gnode_from->get_output_port_color(c.from_port);
  826. Vector2 topos = gnode_to->get_input_port_position(c.to_port) * zoom + gnode_to->get_position_offset() * zoom;
  827. Color tocolor = gnode_to->get_input_port_color(c.to_port);
  828. if (c.activity > 0) {
  829. color = color.lerp(theme_cache.activity_color, c.activity);
  830. tocolor = tocolor.lerp(theme_cache.activity_color, c.activity);
  831. }
  832. _draw_connection_line(connections_layer, frompos, topos, color, tocolor, lines_thickness, zoom);
  833. }
  834. for (List<Connection>::Element *&E : to_erase) {
  835. connections.erase(E);
  836. }
  837. }
  838. void GraphEdit::_top_layer_draw() {
  839. _update_scroll();
  840. if (connecting) {
  841. Node *node_from = get_node_or_null(NodePath(connecting_from));
  842. ERR_FAIL_NULL(node_from);
  843. GraphNode *graph_node_from = Object::cast_to<GraphNode>(node_from);
  844. ERR_FAIL_NULL(graph_node_from);
  845. Vector2 pos;
  846. if (connecting_out) {
  847. pos = graph_node_from->get_output_port_position(connecting_index) * zoom;
  848. } else {
  849. pos = graph_node_from->get_input_port_position(connecting_index) * zoom;
  850. }
  851. pos += graph_node_from->get_position();
  852. Vector2 to_pos = connecting_to;
  853. Color line_color = connecting_color;
  854. // Draw the line to the mouse cursor brighter when it's over a valid target port.
  855. if (connecting_target) {
  856. line_color.r += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING;
  857. line_color.g += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING;
  858. line_color.b += CONNECTING_TARGET_LINE_COLOR_BRIGHTENING;
  859. }
  860. if (!connecting_out) {
  861. SWAP(pos, to_pos);
  862. }
  863. _draw_connection_line(top_layer, pos, to_pos, line_color, line_color, lines_thickness, zoom);
  864. }
  865. if (box_selecting) {
  866. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_fill);
  867. top_layer->draw_rect(box_selecting_rect, theme_cache.selection_stroke, false);
  868. }
  869. }
  870. void GraphEdit::_minimap_draw() {
  871. if (!is_minimap_enabled()) {
  872. return;
  873. }
  874. minimap->update_minimap();
  875. // Draw the minimap background.
  876. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  877. minimap->draw_style_box(minimap->theme_cache.panel, minimap_rect);
  878. Vector2 graph_offset = minimap->_get_graph_offset();
  879. Vector2 minimap_offset = minimap->minimap_offset;
  880. // Draw graph nodes.
  881. for (int i = get_child_count() - 1; i >= 0; i--) {
  882. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  883. if (!graph_node || !graph_node->is_visible()) {
  884. continue;
  885. }
  886. Vector2 node_position = minimap->_convert_from_graph_position(graph_node->get_position_offset() * zoom - graph_offset) + minimap_offset;
  887. Vector2 node_size = minimap->_convert_from_graph_position(graph_node->get_size() * zoom);
  888. Rect2 node_rect = Rect2(node_position, node_size);
  889. Ref<StyleBoxFlat> sb_minimap = minimap->theme_cache.node_style->duplicate();
  890. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  891. Ref<StyleBoxFlat> sb_frame = graph_node->is_selected() ? graph_node->theme_cache.panel_selected : graph_node->theme_cache.panel;
  892. if (sb_frame.is_valid()) {
  893. Color node_color = sb_frame->get_bg_color();
  894. sb_minimap->set_bg_color(node_color);
  895. }
  896. minimap->draw_style_box(sb_minimap, node_rect);
  897. }
  898. // Draw node connections.
  899. for (const Connection &E : connections) {
  900. Node *from = get_node(NodePath(E.from_node));
  901. GraphNode *graph_node_from = Object::cast_to<GraphNode>(from);
  902. if (!graph_node_from) {
  903. continue;
  904. }
  905. Node *node_to = get_node(NodePath(E.to_node));
  906. GraphNode *graph_node_to = Object::cast_to<GraphNode>(node_to);
  907. if (!graph_node_to) {
  908. continue;
  909. }
  910. Vector2 from_port_position = graph_node_from->get_position_offset() * zoom + graph_node_from->get_output_port_position(E.from_port) * zoom;
  911. Vector2 from_position = minimap->_convert_from_graph_position(from_port_position - graph_offset) + minimap_offset;
  912. Color from_color = graph_node_from->get_output_port_color(E.from_port);
  913. Vector2 to_port_position = graph_node_to->get_position_offset() * zoom + graph_node_to->get_input_port_position(E.to_port) * zoom;
  914. Vector2 to_position = minimap->_convert_from_graph_position(to_port_position - graph_offset) + minimap_offset;
  915. Color to_color = graph_node_to->get_input_port_color(E.to_port);
  916. if (E.activity > 0) {
  917. from_color = from_color.lerp(theme_cache.activity_color, E.activity);
  918. to_color = to_color.lerp(theme_cache.activity_color, E.activity);
  919. }
  920. _draw_connection_line(minimap, from_position, to_position, from_color, to_color, 0.5, minimap->_convert_from_graph_position(Vector2(zoom, zoom)).length());
  921. }
  922. // Draw the "camera" viewport.
  923. Rect2 camera_rect = minimap->get_camera_rect();
  924. minimap->draw_style_box(minimap->theme_cache.camera_style, camera_rect);
  925. // Draw the resizer control.
  926. Ref<Texture2D> resizer = minimap->theme_cache.resizer;
  927. Color resizer_color = minimap->theme_cache.resizer_color;
  928. minimap->draw_texture(resizer, Point2(), resizer_color);
  929. }
  930. void GraphEdit::set_selected(Node *p_child) {
  931. for (int i = get_child_count() - 1; i >= 0; i--) {
  932. GraphNode *graph_node = Object::cast_to<GraphNode>(get_child(i));
  933. if (!graph_node) {
  934. continue;
  935. }
  936. graph_node->set_selected(graph_node == p_child);
  937. }
  938. }
  939. void GraphEdit::gui_input(const Ref<InputEvent> &p_ev) {
  940. ERR_FAIL_COND(p_ev.is_null());
  941. if (panner->gui_input(p_ev, warped_panning ? get_global_rect() : Rect2())) {
  942. return;
  943. }
  944. Ref<InputEventMouseMotion> mm = p_ev;
  945. if (mm.is_valid() && dragging) {
  946. if (!moving_selection) {
  947. emit_signal(SNAME("begin_node_move"));
  948. moving_selection = true;
  949. }
  950. just_selected = true;
  951. drag_accum += mm->get_relative();
  952. for (int i = get_child_count() - 1; i >= 0; i--) {
  953. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  954. if (graph_element && graph_element->is_selected() && graph_element->is_draggable()) {
  955. Vector2 pos = (graph_element->get_drag_from() * zoom + drag_accum) / zoom;
  956. // Snapping can be toggled temporarily by holding down Ctrl.
  957. // This is done here as to not toggle the grid when holding down Ctrl.
  958. if (snapping_enabled ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  959. pos = pos.snapped(Vector2(snapping_distance, snapping_distance));
  960. }
  961. graph_element->set_position_offset(pos);
  962. }
  963. }
  964. }
  965. if (mm.is_valid() && box_selecting) {
  966. box_selecting_to = mm->get_position();
  967. box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());
  968. for (int i = get_child_count() - 1; i >= 0; i--) {
  969. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  970. if (!graph_element) {
  971. continue;
  972. }
  973. Rect2 r = graph_element->get_rect();
  974. bool in_box = r.intersects(box_selecting_rect);
  975. if (in_box) {
  976. graph_element->set_selected(box_selection_mode_additive);
  977. } else {
  978. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  979. }
  980. }
  981. top_layer->queue_redraw();
  982. minimap->queue_redraw();
  983. }
  984. Ref<InputEventMouseButton> mb = p_ev;
  985. if (mb.is_valid()) {
  986. if (mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  987. if (box_selecting) {
  988. box_selecting = false;
  989. for (int i = get_child_count() - 1; i >= 0; i--) {
  990. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  991. if (!graph_element) {
  992. continue;
  993. }
  994. graph_element->set_selected(prev_selected.find(graph_element) != nullptr);
  995. }
  996. top_layer->queue_redraw();
  997. minimap->queue_redraw();
  998. } else {
  999. if (connecting) {
  1000. force_connection_drag_end();
  1001. } else {
  1002. emit_signal(SNAME("popup_request"), mb->get_position());
  1003. }
  1004. }
  1005. }
  1006. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && dragging) {
  1007. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1008. // Deselect current node.
  1009. for (int i = get_child_count() - 1; i >= 0; i--) {
  1010. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1011. if (graph_element) {
  1012. Rect2 r = graph_element->get_rect();
  1013. if (r.has_point(mb->get_position())) {
  1014. graph_element->set_selected(false);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. if (drag_accum != Vector2()) {
  1020. for (int i = get_child_count() - 1; i >= 0; i--) {
  1021. GraphElement *graph_element = Object::cast_to<GraphElement>(get_child(i));
  1022. if (graph_element && graph_element->is_selected()) {
  1023. graph_element->set_drag(false);
  1024. }
  1025. }
  1026. }
  1027. if (moving_selection) {
  1028. emit_signal(SNAME("end_node_move"));
  1029. moving_selection = false;
  1030. }
  1031. dragging = false;
  1032. top_layer->queue_redraw();
  1033. minimap->queue_redraw();
  1034. queue_redraw();
  1035. connections_layer->queue_redraw();
  1036. }
  1037. // Node selection logic.
  1038. if (mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
  1039. GraphElement *graph_element = nullptr;
  1040. // Find node which was clicked on.
  1041. for (int i = get_child_count() - 1; i >= 0; i--) {
  1042. GraphElement *selected_element = Object::cast_to<GraphElement>(get_child(i));
  1043. if (!selected_element) {
  1044. continue;
  1045. }
  1046. if (selected_element->is_resizing()) {
  1047. continue;
  1048. }
  1049. if (selected_element->has_point((mb->get_position() - selected_element->get_position()) / zoom)) {
  1050. graph_element = selected_element;
  1051. break;
  1052. }
  1053. }
  1054. if (graph_element) {
  1055. if (_filter_input(mb->get_position())) {
  1056. return;
  1057. }
  1058. // Left-clicked on a node, select it.
  1059. dragging = true;
  1060. drag_accum = Vector2();
  1061. just_selected = !graph_element->is_selected();
  1062. if (!graph_element->is_selected() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1063. for (int i = 0; i < get_child_count(); i++) {
  1064. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1065. if (!child_element) {
  1066. continue;
  1067. }
  1068. child_element->set_selected(child_element == graph_element);
  1069. }
  1070. }
  1071. graph_element->set_selected(true);
  1072. for (int i = 0; i < get_child_count(); i++) {
  1073. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1074. if (!child_element) {
  1075. continue;
  1076. }
  1077. if (child_element->is_selected()) {
  1078. child_element->set_drag(true);
  1079. }
  1080. }
  1081. } else {
  1082. if (_filter_input(mb->get_position())) {
  1083. return;
  1084. }
  1085. if (panner->is_panning()) {
  1086. return;
  1087. }
  1088. // Left-clicked on empty space, start box select.
  1089. box_selecting = true;
  1090. box_selecting_from = mb->get_position();
  1091. if (mb->is_command_or_control_pressed()) {
  1092. box_selection_mode_additive = true;
  1093. prev_selected.clear();
  1094. for (int i = get_child_count() - 1; i >= 0; i--) {
  1095. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1096. if (!child_element || !child_element->is_selected()) {
  1097. continue;
  1098. }
  1099. prev_selected.push_back(child_element);
  1100. }
  1101. } else if (mb->is_shift_pressed()) {
  1102. box_selection_mode_additive = false;
  1103. prev_selected.clear();
  1104. for (int i = get_child_count() - 1; i >= 0; i--) {
  1105. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1106. if (!child_element || !child_element->is_selected()) {
  1107. continue;
  1108. }
  1109. prev_selected.push_back(child_element);
  1110. }
  1111. } else {
  1112. box_selection_mode_additive = true;
  1113. prev_selected.clear();
  1114. for (int i = get_child_count() - 1; i >= 0; i--) {
  1115. GraphElement *child_element = Object::cast_to<GraphElement>(get_child(i));
  1116. if (!child_element) {
  1117. continue;
  1118. }
  1119. child_element->set_selected(false);
  1120. }
  1121. }
  1122. }
  1123. }
  1124. if (mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
  1125. // Box selection ended. Nodes were selected during mouse movement.
  1126. box_selecting = false;
  1127. box_selecting_rect = Rect2();
  1128. prev_selected.clear();
  1129. top_layer->queue_redraw();
  1130. minimap->queue_redraw();
  1131. }
  1132. }
  1133. if (p_ev->is_pressed()) {
  1134. if (p_ev->is_action("ui_graph_duplicate", true)) {
  1135. emit_signal(SNAME("duplicate_nodes_request"));
  1136. accept_event();
  1137. } else if (p_ev->is_action("ui_copy", true)) {
  1138. emit_signal(SNAME("copy_nodes_request"));
  1139. accept_event();
  1140. } else if (p_ev->is_action("ui_paste", true)) {
  1141. emit_signal(SNAME("paste_nodes_request"));
  1142. accept_event();
  1143. } else if (p_ev->is_action("ui_graph_delete", true)) {
  1144. TypedArray<StringName> nodes;
  1145. for (int i = 0; i < get_child_count(); i++) {
  1146. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1147. if (!gn) {
  1148. continue;
  1149. }
  1150. if (gn->is_selected()) {
  1151. nodes.push_back(gn->get_name());
  1152. }
  1153. }
  1154. emit_signal(SNAME("delete_nodes_request"), nodes);
  1155. accept_event();
  1156. }
  1157. }
  1158. }
  1159. void GraphEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1160. h_scrollbar->set_value(h_scrollbar->get_value() - p_scroll_vec.x);
  1161. v_scrollbar->set_value(v_scrollbar->get_value() - p_scroll_vec.y);
  1162. }
  1163. void GraphEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1164. set_zoom_custom(zoom * p_zoom_factor, p_origin);
  1165. }
  1166. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1167. for (Connection &E : connections) {
  1168. if (E.from_node == p_from && E.from_port == p_from_port && E.to_node == p_to && E.to_port == p_to_port) {
  1169. if (Math::is_equal_approx(E.activity, p_activity)) {
  1170. // Update only if changed.
  1171. top_layer->queue_redraw();
  1172. minimap->queue_redraw();
  1173. connections_layer->queue_redraw();
  1174. }
  1175. E.activity = p_activity;
  1176. return;
  1177. }
  1178. }
  1179. }
  1180. void GraphEdit::clear_connections() {
  1181. connections.clear();
  1182. minimap->queue_redraw();
  1183. queue_redraw();
  1184. connections_layer->queue_redraw();
  1185. }
  1186. void GraphEdit::force_connection_drag_end() {
  1187. ERR_FAIL_COND_MSG(!connecting, "Drag end requested without active drag!");
  1188. connecting = false;
  1189. connecting_valid = false;
  1190. top_layer->queue_redraw();
  1191. minimap->queue_redraw();
  1192. queue_redraw();
  1193. connections_layer->queue_redraw();
  1194. emit_signal(SNAME("connection_drag_ended"));
  1195. }
  1196. bool GraphEdit::is_node_hover_valid(const StringName &p_from, const int p_from_port, const StringName &p_to, const int p_to_port) {
  1197. bool valid = true;
  1198. GDVIRTUAL_CALL(_is_node_hover_valid, p_from, p_from_port, p_to, p_to_port, valid);
  1199. return valid;
  1200. }
  1201. void GraphEdit::set_panning_scheme(PanningScheme p_scheme) {
  1202. panning_scheme = p_scheme;
  1203. panner->set_control_scheme((ViewPanner::ControlScheme)p_scheme);
  1204. }
  1205. GraphEdit::PanningScheme GraphEdit::get_panning_scheme() const {
  1206. return panning_scheme;
  1207. }
  1208. void GraphEdit::set_zoom(float p_zoom) {
  1209. set_zoom_custom(p_zoom, get_size() / 2);
  1210. }
  1211. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1212. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1213. if (zoom == p_zoom) {
  1214. return;
  1215. }
  1216. Vector2 scrollbar_offset = (Vector2(h_scrollbar->get_value(), v_scrollbar->get_value()) + p_center) / zoom;
  1217. zoom = p_zoom;
  1218. top_layer->queue_redraw();
  1219. zoom_minus_button->set_disabled(zoom == zoom_min);
  1220. zoom_plus_button->set_disabled(zoom == zoom_max);
  1221. _update_scroll();
  1222. minimap->queue_redraw();
  1223. connections_layer->queue_redraw();
  1224. if (is_visible_in_tree()) {
  1225. Vector2 offset = scrollbar_offset * zoom - p_center;
  1226. h_scrollbar->set_value(offset.x);
  1227. v_scrollbar->set_value(offset.y);
  1228. }
  1229. _update_zoom_label();
  1230. queue_redraw();
  1231. }
  1232. float GraphEdit::get_zoom() const {
  1233. return zoom;
  1234. }
  1235. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1236. p_zoom_step = abs(p_zoom_step);
  1237. ERR_FAIL_COND(!isfinite(p_zoom_step));
  1238. if (zoom_step == p_zoom_step) {
  1239. return;
  1240. }
  1241. zoom_step = p_zoom_step;
  1242. panner->set_scroll_zoom_factor(zoom_step);
  1243. }
  1244. float GraphEdit::get_zoom_step() const {
  1245. return zoom_step;
  1246. }
  1247. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1248. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1249. if (zoom_min == p_zoom_min) {
  1250. return;
  1251. }
  1252. zoom_min = p_zoom_min;
  1253. set_zoom(zoom);
  1254. }
  1255. float GraphEdit::get_zoom_min() const {
  1256. return zoom_min;
  1257. }
  1258. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1259. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1260. if (zoom_max == p_zoom_max) {
  1261. return;
  1262. }
  1263. zoom_max = p_zoom_max;
  1264. set_zoom(zoom);
  1265. }
  1266. float GraphEdit::get_zoom_max() const {
  1267. return zoom_max;
  1268. }
  1269. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1270. if (zoom_label->is_visible() == p_enable) {
  1271. return;
  1272. }
  1273. zoom_label->set_visible(p_enable);
  1274. }
  1275. bool GraphEdit::is_showing_zoom_label() const {
  1276. return zoom_label->is_visible();
  1277. }
  1278. void GraphEdit::set_right_disconnects(bool p_enable) {
  1279. right_disconnects = p_enable;
  1280. }
  1281. bool GraphEdit::is_right_disconnects_enabled() const {
  1282. return right_disconnects;
  1283. }
  1284. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1285. valid_right_disconnect_types.insert(p_type);
  1286. }
  1287. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1288. valid_right_disconnect_types.erase(p_type);
  1289. }
  1290. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1291. valid_left_disconnect_types.insert(p_type);
  1292. }
  1293. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1294. valid_left_disconnect_types.erase(p_type);
  1295. }
  1296. TypedArray<Dictionary> GraphEdit::_get_connection_list() const {
  1297. List<Connection> conns;
  1298. get_connection_list(&conns);
  1299. TypedArray<Dictionary> arr;
  1300. for (const Connection &E : conns) {
  1301. Dictionary d;
  1302. d["from_node"] = E.from_node;
  1303. d["from_port"] = E.from_port;
  1304. d["to_node"] = E.to_node;
  1305. d["to_port"] = E.to_port;
  1306. arr.push_back(d);
  1307. }
  1308. return arr;
  1309. }
  1310. void GraphEdit::_zoom_minus() {
  1311. set_zoom(zoom / zoom_step);
  1312. }
  1313. void GraphEdit::_zoom_reset() {
  1314. set_zoom(1);
  1315. }
  1316. void GraphEdit::_zoom_plus() {
  1317. set_zoom(zoom * zoom_step);
  1318. }
  1319. void GraphEdit::_update_zoom_label() {
  1320. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1321. String zoom_text = itos(zoom_percent) + "%";
  1322. zoom_label->set_text(zoom_text);
  1323. }
  1324. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1325. ConnectionType ct(p_type, p_with_type);
  1326. valid_connection_types.insert(ct);
  1327. }
  1328. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1329. ConnectionType ct(p_type, p_with_type);
  1330. valid_connection_types.erase(ct);
  1331. }
  1332. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1333. ConnectionType ct(p_type, p_with_type);
  1334. return valid_connection_types.has(ct);
  1335. }
  1336. void GraphEdit::set_snapping_enabled(bool p_enable) {
  1337. if (snapping_enabled == p_enable) {
  1338. return;
  1339. }
  1340. snapping_enabled = p_enable;
  1341. toggle_snapping_button->set_pressed(p_enable);
  1342. queue_redraw();
  1343. }
  1344. bool GraphEdit::is_snapping_enabled() const {
  1345. return snapping_enabled;
  1346. }
  1347. void GraphEdit::set_snapping_distance(int p_snapping_distance) {
  1348. ERR_FAIL_COND_MSG(p_snapping_distance < GRID_MIN_SNAPPING_DISTANCE || p_snapping_distance > GRID_MAX_SNAPPING_DISTANCE,
  1349. vformat("GraphEdit's snapping distance must be between %d and %d (inclusive)", GRID_MIN_SNAPPING_DISTANCE, GRID_MAX_SNAPPING_DISTANCE));
  1350. snapping_distance = p_snapping_distance;
  1351. snapping_distance_spinbox->set_value(p_snapping_distance);
  1352. queue_redraw();
  1353. }
  1354. int GraphEdit::get_snapping_distance() const {
  1355. return snapping_distance;
  1356. }
  1357. void GraphEdit::set_show_grid(bool p_show) {
  1358. if (show_grid == p_show) {
  1359. return;
  1360. }
  1361. show_grid = p_show;
  1362. show_grid_button->set_pressed(p_show);
  1363. queue_redraw();
  1364. }
  1365. bool GraphEdit::is_showing_grid() const {
  1366. return show_grid;
  1367. }
  1368. void GraphEdit::_snapping_toggled() {
  1369. snapping_enabled = toggle_snapping_button->is_pressed();
  1370. }
  1371. void GraphEdit::_snapping_distance_changed(double) {
  1372. snapping_distance = snapping_distance_spinbox->get_value();
  1373. queue_redraw();
  1374. }
  1375. void GraphEdit::_show_grid_toggled() {
  1376. show_grid = show_grid_button->is_pressed();
  1377. queue_redraw();
  1378. }
  1379. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1380. minimap->set_size(p_size);
  1381. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1382. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1383. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  1384. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  1385. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1386. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1387. minimap->queue_redraw();
  1388. }
  1389. Vector2 GraphEdit::get_minimap_size() const {
  1390. return minimap->get_size();
  1391. }
  1392. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1393. if (minimap->get_modulate().a == p_opacity) {
  1394. return;
  1395. }
  1396. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1397. minimap->queue_redraw();
  1398. }
  1399. float GraphEdit::get_minimap_opacity() const {
  1400. Color minimap_modulate = minimap->get_modulate();
  1401. return minimap_modulate.a;
  1402. }
  1403. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1404. if (minimap_button->is_pressed() == p_enable) {
  1405. return;
  1406. }
  1407. minimap_button->set_pressed(p_enable);
  1408. _minimap_toggled();
  1409. minimap->queue_redraw();
  1410. }
  1411. bool GraphEdit::is_minimap_enabled() const {
  1412. return minimap_button->is_pressed();
  1413. }
  1414. void GraphEdit::set_arrange_nodes_button_hidden(bool p_enable) {
  1415. arrange_nodes_button_hidden = p_enable;
  1416. if (arrange_nodes_button_hidden) {
  1417. layout_button->hide();
  1418. } else {
  1419. layout_button->show();
  1420. }
  1421. }
  1422. bool GraphEdit::is_arrange_nodes_button_hidden() const {
  1423. return arrange_nodes_button_hidden;
  1424. }
  1425. void GraphEdit::_minimap_toggled() {
  1426. if (is_minimap_enabled()) {
  1427. minimap->set_visible(true);
  1428. minimap->queue_redraw();
  1429. } else {
  1430. minimap->set_visible(false);
  1431. }
  1432. }
  1433. void GraphEdit::set_connection_lines_curvature(float p_curvature) {
  1434. lines_curvature = p_curvature;
  1435. queue_redraw();
  1436. }
  1437. float GraphEdit::get_connection_lines_curvature() const {
  1438. return lines_curvature;
  1439. }
  1440. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  1441. if (lines_thickness == p_thickness) {
  1442. return;
  1443. }
  1444. lines_thickness = p_thickness;
  1445. queue_redraw();
  1446. }
  1447. float GraphEdit::get_connection_lines_thickness() const {
  1448. return lines_thickness;
  1449. }
  1450. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  1451. if (lines_antialiased == p_antialiased) {
  1452. return;
  1453. }
  1454. lines_antialiased = p_antialiased;
  1455. queue_redraw();
  1456. }
  1457. bool GraphEdit::is_connection_lines_antialiased() const {
  1458. return lines_antialiased;
  1459. }
  1460. HBoxContainer *GraphEdit::get_menu_hbox() {
  1461. return menu_hbox;
  1462. }
  1463. Ref<ViewPanner> GraphEdit::get_panner() {
  1464. return panner;
  1465. }
  1466. void GraphEdit::set_warped_panning(bool p_warped) {
  1467. warped_panning = p_warped;
  1468. }
  1469. void GraphEdit::arrange_nodes() {
  1470. arranger->arrange_nodes();
  1471. }
  1472. void GraphEdit::_bind_methods() {
  1473. ClassDB::bind_method(D_METHOD("connect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::connect_node);
  1474. ClassDB::bind_method(D_METHOD("is_node_connected", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::is_node_connected);
  1475. ClassDB::bind_method(D_METHOD("disconnect_node", "from_node", "from_port", "to_node", "to_port"), &GraphEdit::disconnect_node);
  1476. ClassDB::bind_method(D_METHOD("set_connection_activity", "from_node", "from_port", "to_node", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1477. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1478. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1479. ClassDB::bind_method(D_METHOD("force_connection_drag_end"), &GraphEdit::force_connection_drag_end);
  1480. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &GraphEdit::get_scroll_offset);
  1481. ClassDB::bind_method(D_METHOD("set_scroll_offset", "offset"), &GraphEdit::set_scroll_offset);
  1482. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1483. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1484. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1485. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1486. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1487. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1488. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1489. ClassDB::bind_method(D_METHOD("get_connection_line", "from_node", "to_node"), &GraphEdit::get_connection_line);
  1490. ClassDB::bind_method(D_METHOD("set_panning_scheme", "scheme"), &GraphEdit::set_panning_scheme);
  1491. ClassDB::bind_method(D_METHOD("get_panning_scheme"), &GraphEdit::get_panning_scheme);
  1492. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  1493. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1494. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1495. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1496. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1497. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1498. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1499. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1500. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1501. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1502. ClassDB::bind_method(D_METHOD("set_show_grid", "enable"), &GraphEdit::set_show_grid);
  1503. ClassDB::bind_method(D_METHOD("is_showing_grid"), &GraphEdit::is_showing_grid);
  1504. ClassDB::bind_method(D_METHOD("set_snapping_enabled", "enable"), &GraphEdit::set_snapping_enabled);
  1505. ClassDB::bind_method(D_METHOD("is_snapping_enabled"), &GraphEdit::is_snapping_enabled);
  1506. ClassDB::bind_method(D_METHOD("set_snapping_distance", "pixels"), &GraphEdit::set_snapping_distance);
  1507. ClassDB::bind_method(D_METHOD("get_snapping_distance"), &GraphEdit::get_snapping_distance);
  1508. ClassDB::bind_method(D_METHOD("set_connection_lines_curvature", "curvature"), &GraphEdit::set_connection_lines_curvature);
  1509. ClassDB::bind_method(D_METHOD("get_connection_lines_curvature"), &GraphEdit::get_connection_lines_curvature);
  1510. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  1511. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  1512. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  1513. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  1514. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  1515. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1516. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  1517. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1518. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1519. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1520. ClassDB::bind_method(D_METHOD("set_arrange_nodes_button_hidden", "enable"), &GraphEdit::set_arrange_nodes_button_hidden);
  1521. ClassDB::bind_method(D_METHOD("is_arrange_nodes_button_hidden"), &GraphEdit::is_arrange_nodes_button_hidden);
  1522. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1523. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1524. GDVIRTUAL_BIND(_is_in_input_hotzone, "in_node", "in_port", "mouse_position");
  1525. GDVIRTUAL_BIND(_is_in_output_hotzone, "in_node", "in_port", "mouse_position");
  1526. ClassDB::bind_method(D_METHOD("get_menu_hbox"), &GraphEdit::get_menu_hbox);
  1527. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  1528. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1529. GDVIRTUAL_BIND(_get_connection_line, "from_position", "to_position")
  1530. GDVIRTUAL_BIND(_is_node_hover_valid, "from_node", "from_port", "to_node", "to_port");
  1531. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1532. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_scroll_offset", "get_scroll_offset");
  1533. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_grid"), "set_show_grid", "is_showing_grid");
  1534. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "snapping_enabled"), "set_snapping_enabled", "is_snapping_enabled");
  1535. ADD_PROPERTY(PropertyInfo(Variant::INT, "snapping_distance", PROPERTY_HINT_NONE, "suffix:px"), "set_snapping_distance", "get_snapping_distance");
  1536. ADD_PROPERTY(PropertyInfo(Variant::INT, "panning_scheme", PROPERTY_HINT_ENUM, "Scroll Zooms,Scroll Pans"), "set_panning_scheme", "get_panning_scheme");
  1537. ADD_GROUP("Connection Lines", "connection_lines");
  1538. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_curvature"), "set_connection_lines_curvature", "get_connection_lines_curvature");
  1539. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness", PROPERTY_HINT_NONE, "suffix:px"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  1540. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  1541. ADD_GROUP("Zoom", "");
  1542. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1543. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  1544. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  1545. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  1546. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  1547. ADD_GROUP("Minimap", "minimap_");
  1548. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  1549. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_size", "get_minimap_size");
  1550. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  1551. ADD_GROUP("UI", "");
  1552. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "arrange_nodes_button_hidden"), "set_arrange_nodes_button_hidden", "is_arrange_nodes_button_hidden");
  1553. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  1554. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port")));
  1555. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1556. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to_node"), PropertyInfo(Variant::INT, "to_port"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1557. ADD_SIGNAL(MethodInfo("connection_drag_started", PropertyInfo(Variant::STRING_NAME, "from_node"), PropertyInfo(Variant::INT, "from_port"), PropertyInfo(Variant::BOOL, "is_output")));
  1558. ADD_SIGNAL(MethodInfo("connection_drag_ended"));
  1559. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1560. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1561. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1562. ADD_SIGNAL(MethodInfo("delete_nodes_request", PropertyInfo(Variant::ARRAY, "nodes", PROPERTY_HINT_ARRAY_TYPE, "StringName")));
  1563. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1564. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1565. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1566. ADD_SIGNAL(MethodInfo("begin_node_move"));
  1567. ADD_SIGNAL(MethodInfo("end_node_move"));
  1568. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "offset")));
  1569. BIND_ENUM_CONSTANT(SCROLL_ZOOMS);
  1570. BIND_ENUM_CONSTANT(SCROLL_PANS);
  1571. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, GraphEdit, panel);
  1572. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_major);
  1573. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, grid_minor);
  1574. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, GraphEdit, activity_color, "activity");
  1575. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_fill);
  1576. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, GraphEdit, selection_stroke);
  1577. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_in);
  1578. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_out);
  1579. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, zoom_reset);
  1580. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, snapping_toggle);
  1581. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, grid_toggle);
  1582. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, minimap_toggle);
  1583. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, GraphEdit, layout);
  1584. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_inner_extent);
  1585. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, GraphEdit, port_hotzone_outer_extent);
  1586. }
  1587. GraphEdit::GraphEdit() {
  1588. set_focus_mode(FOCUS_ALL);
  1589. // Allow dezooming 8 times from the default zoom level.
  1590. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  1591. // but this is still useful for previewing and navigation.
  1592. zoom_min = (1 / Math::pow(zoom_step, 8));
  1593. // Allow zooming 4 times from the default zoom level.
  1594. zoom_max = (1 * Math::pow(zoom_step, 4));
  1595. panner.instantiate();
  1596. panner->set_callbacks(callable_mp(this, &GraphEdit::_pan_callback), callable_mp(this, &GraphEdit::_zoom_callback));
  1597. top_layer = memnew(GraphEditFilter(this));
  1598. add_child(top_layer, false, INTERNAL_MODE_BACK);
  1599. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1600. top_layer->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1601. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1602. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1603. top_layer->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  1604. connections_layer = memnew(Control);
  1605. add_child(connections_layer, false);
  1606. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1607. connections_layer->set_name("_connection_layer");
  1608. connections_layer->set_disable_visibility_clip(true); // Necessary, so it can draw freely and be offset.
  1609. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1610. h_scrollbar = memnew(HScrollBar);
  1611. h_scrollbar->set_name("_h_scroll");
  1612. top_layer->add_child(h_scrollbar);
  1613. v_scrollbar = memnew(VScrollBar);
  1614. v_scrollbar->set_name("_v_scroll");
  1615. top_layer->add_child(v_scrollbar);
  1616. // Set large minmax so it can scroll even if not resized yet.
  1617. h_scrollbar->set_min(-10000);
  1618. h_scrollbar->set_max(10000);
  1619. v_scrollbar->set_min(-10000);
  1620. v_scrollbar->set_max(10000);
  1621. h_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1622. v_scrollbar->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1623. menu_hbox = memnew(HBoxContainer);
  1624. top_layer->add_child(menu_hbox);
  1625. menu_hbox->set_position(Vector2(10, 10));
  1626. zoom_label = memnew(Label);
  1627. menu_hbox->add_child(zoom_label);
  1628. zoom_label->set_visible(false);
  1629. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1630. zoom_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1631. zoom_label->set_custom_minimum_size(Size2(48, 0));
  1632. _update_zoom_label();
  1633. zoom_minus_button = memnew(Button);
  1634. zoom_minus_button->set_theme_type_variation("FlatButton");
  1635. menu_hbox->add_child(zoom_minus_button);
  1636. zoom_minus_button->set_tooltip_text(RTR("Zoom Out"));
  1637. zoom_minus_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1638. zoom_minus_button->set_focus_mode(FOCUS_NONE);
  1639. zoom_reset_button = memnew(Button);
  1640. zoom_reset_button->set_theme_type_variation("FlatButton");
  1641. menu_hbox->add_child(zoom_reset_button);
  1642. zoom_reset_button->set_tooltip_text(RTR("Zoom Reset"));
  1643. zoom_reset_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1644. zoom_reset_button->set_focus_mode(FOCUS_NONE);
  1645. zoom_plus_button = memnew(Button);
  1646. zoom_plus_button->set_theme_type_variation("FlatButton");
  1647. menu_hbox->add_child(zoom_plus_button);
  1648. zoom_plus_button->set_tooltip_text(RTR("Zoom In"));
  1649. zoom_plus_button->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1650. zoom_plus_button->set_focus_mode(FOCUS_NONE);
  1651. show_grid_button = memnew(Button);
  1652. show_grid_button->set_theme_type_variation("FlatButton");
  1653. show_grid_button->set_toggle_mode(true);
  1654. show_grid_button->set_tooltip_text(RTR("Toggle the visual grid."));
  1655. show_grid_button->connect("pressed", callable_mp(this, &GraphEdit::_show_grid_toggled));
  1656. show_grid_button->set_pressed(true);
  1657. show_grid_button->set_focus_mode(FOCUS_NONE);
  1658. menu_hbox->add_child(show_grid_button);
  1659. toggle_snapping_button = memnew(Button);
  1660. toggle_snapping_button->set_theme_type_variation("FlatButton");
  1661. toggle_snapping_button->set_toggle_mode(true);
  1662. toggle_snapping_button->set_tooltip_text(RTR("Toggle snapping to the grid."));
  1663. toggle_snapping_button->connect("pressed", callable_mp(this, &GraphEdit::_snapping_toggled));
  1664. toggle_snapping_button->set_pressed(snapping_enabled);
  1665. toggle_snapping_button->set_focus_mode(FOCUS_NONE);
  1666. menu_hbox->add_child(toggle_snapping_button);
  1667. snapping_distance_spinbox = memnew(SpinBox);
  1668. snapping_distance_spinbox->set_min(GRID_MIN_SNAPPING_DISTANCE);
  1669. snapping_distance_spinbox->set_max(GRID_MAX_SNAPPING_DISTANCE);
  1670. snapping_distance_spinbox->set_step(1);
  1671. snapping_distance_spinbox->set_value(snapping_distance);
  1672. snapping_distance_spinbox->set_tooltip_text(RTR("Change the snapping distance."));
  1673. snapping_distance_spinbox->connect("value_changed", callable_mp(this, &GraphEdit::_snapping_distance_changed));
  1674. menu_hbox->add_child(snapping_distance_spinbox);
  1675. minimap_button = memnew(Button);
  1676. minimap_button->set_theme_type_variation("FlatButton");
  1677. minimap_button->set_toggle_mode(true);
  1678. minimap_button->set_tooltip_text(RTR("Toggle the graph minimap."));
  1679. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  1680. minimap_button->set_pressed(show_grid);
  1681. minimap_button->set_focus_mode(FOCUS_NONE);
  1682. menu_hbox->add_child(minimap_button);
  1683. layout_button = memnew(Button);
  1684. layout_button->set_theme_type_variation("FlatButton");
  1685. menu_hbox->add_child(layout_button);
  1686. layout_button->set_tooltip_text(RTR("Automatically arrange selected nodes."));
  1687. layout_button->connect("pressed", callable_mp(this, &GraphEdit::arrange_nodes));
  1688. layout_button->set_focus_mode(FOCUS_NONE);
  1689. Vector2 minimap_size = Vector2(240, 160);
  1690. float minimap_opacity = 0.65;
  1691. minimap = memnew(GraphEditMinimap(this));
  1692. top_layer->add_child(minimap);
  1693. minimap->set_name("_minimap");
  1694. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  1695. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  1696. minimap->set_custom_minimum_size(Vector2(50, 50));
  1697. minimap->set_size(minimap_size);
  1698. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1699. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.width - MINIMAP_OFFSET);
  1700. minimap->set_offset(Side::SIDE_TOP, -minimap_size.height - MINIMAP_OFFSET);
  1701. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1702. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1703. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  1704. set_clip_contents(true);
  1705. arranger = Ref<GraphEditArranger>(memnew(GraphEditArranger(this)));
  1706. }