graph_edit.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  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. constexpr int MINIMAP_OFFSET = 12;
  40. constexpr int MINIMAP_PADDING = 5;
  41. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  42. return ge->_filter_input(p_point);
  43. }
  44. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  45. ge = p_edit;
  46. }
  47. void GraphEditMinimap::_bind_methods() {
  48. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEditMinimap::_gui_input);
  49. }
  50. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  51. ge = p_edit;
  52. graph_proportions = Vector2(1, 1);
  53. graph_padding = Vector2(0, 0);
  54. camera_position = Vector2(100, 50);
  55. camera_size = Vector2(200, 200);
  56. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  57. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  58. is_pressing = false;
  59. is_resizing = false;
  60. }
  61. void GraphEditMinimap::update_minimap() {
  62. Vector2 graph_offset = _get_graph_offset();
  63. Vector2 graph_size = _get_graph_size();
  64. camera_position = ge->get_scroll_ofs() - graph_offset;
  65. camera_size = ge->get_size();
  66. Vector2 render_size = _get_render_size();
  67. float target_ratio = render_size.x / render_size.y;
  68. float graph_ratio = graph_size.x / graph_size.y;
  69. graph_proportions = graph_size;
  70. graph_padding = Vector2(0, 0);
  71. if (graph_ratio > target_ratio) {
  72. graph_proportions.x = graph_size.x;
  73. graph_proportions.y = graph_size.x / target_ratio;
  74. graph_padding.y = Math::abs(graph_size.y - graph_proportions.y) / 2;
  75. } else {
  76. graph_proportions.x = graph_size.y * target_ratio;
  77. graph_proportions.y = graph_size.y;
  78. graph_padding.x = Math::abs(graph_size.x - graph_proportions.x) / 2;
  79. }
  80. // This centers minimap inside the minimap rectangle.
  81. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  82. }
  83. Rect2 GraphEditMinimap::get_camera_rect() {
  84. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  85. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  86. Vector2 camera_position = (camera_center - camera_viewport / 2);
  87. return Rect2(camera_position, camera_viewport);
  88. }
  89. Vector2 GraphEditMinimap::_get_render_size() {
  90. if (!is_inside_tree()) {
  91. return Vector2(0, 0);
  92. }
  93. return get_size() - 2 * minimap_padding;
  94. }
  95. Vector2 GraphEditMinimap::_get_graph_offset() {
  96. return Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  97. }
  98. Vector2 GraphEditMinimap::_get_graph_size() {
  99. 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());
  100. if (graph_size.x == 0) {
  101. graph_size.x = 1;
  102. }
  103. if (graph_size.y == 0) {
  104. graph_size.y = 1;
  105. }
  106. return graph_size;
  107. }
  108. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  109. Vector2 map_position = Vector2(0, 0);
  110. Vector2 render_size = _get_render_size();
  111. map_position.x = p_position.x * render_size.x / graph_proportions.x;
  112. map_position.y = p_position.y * render_size.y / graph_proportions.y;
  113. return map_position;
  114. }
  115. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  116. Vector2 graph_position = Vector2(0, 0);
  117. Vector2 render_size = _get_render_size();
  118. graph_position.x = p_position.x * graph_proportions.x / render_size.x;
  119. graph_position.y = p_position.y * graph_proportions.y / render_size.y;
  120. return graph_position;
  121. }
  122. void GraphEditMinimap::_gui_input(const Ref<InputEvent> &p_ev) {
  123. ERR_FAIL_COND(p_ev.is_null());
  124. if (!ge->is_minimap_enabled()) {
  125. return;
  126. }
  127. Ref<InputEventMouseButton> mb = p_ev;
  128. Ref<InputEventMouseMotion> mm = p_ev;
  129. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  130. if (mb->is_pressed()) {
  131. is_pressing = true;
  132. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  133. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  134. if (resizer_hitbox.has_point(mb->get_position())) {
  135. is_resizing = true;
  136. } else {
  137. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  138. _adjust_graph_scroll(click_position);
  139. }
  140. } else {
  141. is_pressing = false;
  142. is_resizing = false;
  143. }
  144. accept_event();
  145. } else if (mm.is_valid() && is_pressing) {
  146. if (is_resizing) {
  147. // Prevent setting minimap wider than GraphEdit
  148. Vector2 new_minimap_size;
  149. new_minimap_size.x = MIN(get_size().x - mm->get_relative().x, ge->get_size().x - 2.0 * minimap_padding.x);
  150. new_minimap_size.y = MIN(get_size().y - mm->get_relative().y, ge->get_size().y - 2.0 * minimap_padding.y);
  151. ge->set_minimap_size(new_minimap_size);
  152. update();
  153. } else {
  154. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  155. _adjust_graph_scroll(click_position);
  156. }
  157. accept_event();
  158. }
  159. }
  160. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  161. Vector2 graph_offset = _get_graph_offset();
  162. ge->set_scroll_ofs(p_offset + graph_offset - camera_size / 2);
  163. }
  164. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  165. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  166. return OK;
  167. }
  168. Connection c;
  169. c.from = p_from;
  170. c.from_port = p_from_port;
  171. c.to = p_to;
  172. c.to_port = p_to_port;
  173. c.activity = 0;
  174. connections.push_back(c);
  175. top_layer->update();
  176. minimap->update();
  177. update();
  178. connections_layer->update();
  179. return OK;
  180. }
  181. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  182. for (const Connection &E : connections) {
  183. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  190. for (const List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  191. 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) {
  192. connections.erase(E);
  193. top_layer->update();
  194. minimap->update();
  195. update();
  196. connections_layer->update();
  197. return;
  198. }
  199. }
  200. }
  201. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  202. *r_connections = connections;
  203. }
  204. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  205. setting_scroll_ofs = true;
  206. h_scroll->set_value(p_ofs.x);
  207. v_scroll->set_value(p_ofs.y);
  208. _update_scroll();
  209. setting_scroll_ofs = false;
  210. }
  211. Vector2 GraphEdit::get_scroll_ofs() const {
  212. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  213. }
  214. void GraphEdit::_scroll_moved(double) {
  215. if (!awaiting_scroll_offset_update) {
  216. call_deferred(SNAME("_update_scroll_offset"));
  217. awaiting_scroll_offset_update = true;
  218. }
  219. top_layer->update();
  220. minimap->update();
  221. update();
  222. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  223. emit_signal(SNAME("scroll_offset_changed"), get_scroll_ofs());
  224. }
  225. }
  226. void GraphEdit::_update_scroll_offset() {
  227. set_block_minimum_size_adjust(true);
  228. for (int i = 0; i < get_child_count(); i++) {
  229. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  230. if (!gn) {
  231. continue;
  232. }
  233. Point2 pos = gn->get_position_offset() * zoom;
  234. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  235. gn->set_position(pos);
  236. if (gn->get_scale() != Vector2(zoom, zoom)) {
  237. gn->set_scale(Vector2(zoom, zoom));
  238. }
  239. }
  240. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  241. set_block_minimum_size_adjust(false);
  242. awaiting_scroll_offset_update = false;
  243. }
  244. void GraphEdit::_update_scroll() {
  245. if (updating) {
  246. return;
  247. }
  248. updating = true;
  249. set_block_minimum_size_adjust(true);
  250. Rect2 screen;
  251. for (int i = 0; i < get_child_count(); i++) {
  252. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  253. if (!gn) {
  254. continue;
  255. }
  256. Rect2 r;
  257. r.position = gn->get_position_offset() * zoom;
  258. r.size = gn->get_size() * zoom;
  259. screen = screen.merge(r);
  260. }
  261. screen.position -= get_size();
  262. screen.size += get_size() * 2.0;
  263. h_scroll->set_min(screen.position.x);
  264. h_scroll->set_max(screen.position.x + screen.size.x);
  265. h_scroll->set_page(get_size().x);
  266. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  267. h_scroll->hide();
  268. } else {
  269. h_scroll->show();
  270. }
  271. v_scroll->set_min(screen.position.y);
  272. v_scroll->set_max(screen.position.y + screen.size.y);
  273. v_scroll->set_page(get_size().y);
  274. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  275. v_scroll->hide();
  276. } else {
  277. v_scroll->show();
  278. }
  279. Size2 hmin = h_scroll->get_combined_minimum_size();
  280. Size2 vmin = v_scroll->get_combined_minimum_size();
  281. // Avoid scrollbar overlapping.
  282. h_scroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  283. v_scroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  284. set_block_minimum_size_adjust(false);
  285. if (!awaiting_scroll_offset_update) {
  286. call_deferred(SNAME("_update_scroll_offset"));
  287. awaiting_scroll_offset_update = true;
  288. }
  289. updating = false;
  290. }
  291. void GraphEdit::_graph_node_raised(Node *p_gn) {
  292. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  293. ERR_FAIL_COND(!gn);
  294. if (gn->is_comment()) {
  295. move_child(gn, 0);
  296. } else {
  297. gn->raise();
  298. }
  299. int first_not_comment = 0;
  300. for (int i = 0; i < get_child_count(); i++) {
  301. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  302. if (gn2 && !gn2->is_comment()) {
  303. first_not_comment = i;
  304. break;
  305. }
  306. }
  307. move_child(connections_layer, first_not_comment);
  308. top_layer->raise();
  309. emit_signal(SNAME("node_selected"), p_gn);
  310. }
  311. void GraphEdit::_graph_node_moved(Node *p_gn) {
  312. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  313. ERR_FAIL_COND(!gn);
  314. top_layer->update();
  315. minimap->update();
  316. update();
  317. connections_layer->update();
  318. }
  319. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_gn) {
  320. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  321. ERR_FAIL_COND(!gn);
  322. top_layer->update();
  323. minimap->update();
  324. update();
  325. connections_layer->update();
  326. }
  327. void GraphEdit::add_child_notify(Node *p_child) {
  328. Control::add_child_notify(p_child);
  329. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  330. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  331. if (gn) {
  332. gn->set_scale(Vector2(zoom, zoom));
  333. gn->connect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved), varray(gn));
  334. gn->connect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated), varray(gn));
  335. gn->connect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised), varray(gn));
  336. gn->connect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  337. gn->connect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::update));
  338. _graph_node_moved(gn);
  339. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  340. }
  341. }
  342. void GraphEdit::remove_child_notify(Node *p_child) {
  343. Control::remove_child_notify(p_child);
  344. if (p_child == top_layer) {
  345. top_layer = nullptr;
  346. minimap = nullptr;
  347. } else if (p_child == connections_layer) {
  348. connections_layer = nullptr;
  349. }
  350. if (top_layer != nullptr && is_inside_tree()) {
  351. top_layer->call_deferred(SNAME("raise")); // Top layer always on top!
  352. }
  353. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  354. if (gn) {
  355. gn->disconnect("position_offset_changed", callable_mp(this, &GraphEdit::_graph_node_moved));
  356. gn->disconnect("slot_updated", callable_mp(this, &GraphEdit::_graph_node_slot_updated));
  357. gn->disconnect("raise_request", callable_mp(this, &GraphEdit::_graph_node_raised));
  358. // In case of the whole GraphEdit being destroyed these references can already be freed.
  359. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  360. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)connections_layer, &CanvasItem::update));
  361. }
  362. if (minimap != nullptr && minimap->is_inside_tree()) {
  363. gn->disconnect("item_rect_changed", callable_mp((CanvasItem *)minimap, &GraphEditMinimap::update));
  364. }
  365. }
  366. }
  367. void GraphEdit::_notification(int p_what) {
  368. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  369. port_grab_distance_horizontal = get_theme_constant(SNAME("port_grab_distance_horizontal"));
  370. port_grab_distance_vertical = get_theme_constant(SNAME("port_grab_distance_vertical"));
  371. zoom_minus->set_icon(get_theme_icon(SNAME("minus")));
  372. zoom_reset->set_icon(get_theme_icon(SNAME("reset")));
  373. zoom_plus->set_icon(get_theme_icon(SNAME("more")));
  374. snap_button->set_icon(get_theme_icon(SNAME("snap")));
  375. minimap_button->set_icon(get_theme_icon(SNAME("minimap")));
  376. layout_button->set_icon(get_theme_icon(SNAME("layout")));
  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(SNAME("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(SNAME("grid_minor"));
  400. Color grid_major = get_theme_color(SNAME("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(SNAME("port"), SNAME("GraphNode"));
  431. Vector2i port_size = Vector2i(port->get_width(), port->get_height());
  432. for (int i = get_child_count() - 1; i >= 0; i--) {
  433. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  434. if (!gn) {
  435. continue;
  436. }
  437. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  438. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  439. if (is_in_hot_zone(pos / zoom, p_point / zoom, port_size, false)) {
  440. return true;
  441. }
  442. }
  443. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  444. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  445. if (is_in_hot_zone(pos / zoom, p_point / zoom, port_size, true)) {
  446. return true;
  447. }
  448. }
  449. }
  450. return false;
  451. }
  452. void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
  453. Ref<InputEventMouseButton> mb = p_ev;
  454. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT && mb->is_pressed()) {
  455. Ref<Texture2D> port = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  456. Vector2i port_size = Vector2i(port->get_width(), port->get_height());
  457. connecting_valid = false;
  458. click_pos = mb->get_position() / zoom;
  459. for (int i = get_child_count() - 1; i >= 0; i--) {
  460. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  461. if (!gn) {
  462. continue;
  463. }
  464. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  465. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  466. if (is_in_hot_zone(pos / zoom, click_pos, port_size, false)) {
  467. if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
  468. //check disconnect
  469. for (const Connection &E : connections) {
  470. if (E.from == gn->get_name() && E.from_port == j) {
  471. Node *to = get_node(String(E.to));
  472. if (Object::cast_to<GraphNode>(to)) {
  473. connecting_from = E.to;
  474. connecting_index = E.to_port;
  475. connecting_out = false;
  476. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E.to_port);
  477. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E.to_port);
  478. connecting_target = false;
  479. connecting_to = pos;
  480. just_disconnected = true;
  481. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  482. to = get_node(String(connecting_from)); //maybe it was erased
  483. if (Object::cast_to<GraphNode>(to)) {
  484. connecting = true;
  485. }
  486. return;
  487. }
  488. }
  489. }
  490. }
  491. connecting = true;
  492. connecting_from = gn->get_name();
  493. connecting_index = j;
  494. connecting_out = true;
  495. connecting_type = gn->get_connection_output_type(j);
  496. connecting_color = gn->get_connection_output_color(j);
  497. connecting_target = false;
  498. connecting_to = pos;
  499. just_disconnected = false;
  500. return;
  501. }
  502. }
  503. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  504. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  505. if (is_in_hot_zone(pos / zoom, click_pos, port_size, true)) {
  506. if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
  507. //check disconnect
  508. for (const Connection &E : connections) {
  509. if (E.to == gn->get_name() && E.to_port == j) {
  510. Node *fr = get_node(String(E.from));
  511. if (Object::cast_to<GraphNode>(fr)) {
  512. connecting_from = E.from;
  513. connecting_index = E.from_port;
  514. connecting_out = true;
  515. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E.from_port);
  516. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E.from_port);
  517. connecting_target = false;
  518. connecting_to = pos;
  519. just_disconnected = true;
  520. emit_signal(SNAME("disconnection_request"), E.from, E.from_port, E.to, E.to_port);
  521. fr = get_node(String(connecting_from)); //maybe it was erased
  522. if (Object::cast_to<GraphNode>(fr)) {
  523. connecting = true;
  524. }
  525. return;
  526. }
  527. }
  528. }
  529. }
  530. connecting = true;
  531. connecting_from = gn->get_name();
  532. connecting_index = j;
  533. connecting_out = false;
  534. connecting_type = gn->get_connection_input_type(j);
  535. connecting_color = gn->get_connection_input_color(j);
  536. connecting_target = false;
  537. connecting_to = pos;
  538. just_disconnected = false;
  539. return;
  540. }
  541. }
  542. }
  543. }
  544. Ref<InputEventMouseMotion> mm = p_ev;
  545. if (mm.is_valid() && connecting) {
  546. connecting_to = mm->get_position();
  547. connecting_target = false;
  548. top_layer->update();
  549. minimap->update();
  550. connecting_valid = just_disconnected || click_pos.distance_to(connecting_to / zoom) > 20.0;
  551. if (connecting_valid) {
  552. Ref<Texture2D> port = get_theme_icon(SNAME("port"), SNAME("GraphNode"));
  553. Vector2i port_size = Vector2i(port->get_width(), port->get_height());
  554. Vector2 mpos = mm->get_position() / zoom;
  555. for (int i = get_child_count() - 1; i >= 0; i--) {
  556. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  557. if (!gn) {
  558. continue;
  559. }
  560. if (!connecting_out) {
  561. for (int j = 0; j < gn->get_connection_output_count(); j++) {
  562. Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
  563. int type = gn->get_connection_output_type(j);
  564. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos / zoom, mpos, port_size, false)) {
  565. connecting_target = true;
  566. connecting_to = pos;
  567. connecting_target_to = gn->get_name();
  568. connecting_target_index = j;
  569. return;
  570. }
  571. }
  572. } else {
  573. for (int j = 0; j < gn->get_connection_input_count(); j++) {
  574. Vector2 pos = gn->get_connection_input_position(j) + gn->get_position();
  575. int type = gn->get_connection_input_type(j);
  576. if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos / zoom, mpos, port_size, true)) {
  577. connecting_target = true;
  578. connecting_to = pos;
  579. connecting_target_to = gn->get_name();
  580. connecting_target_index = j;
  581. return;
  582. }
  583. }
  584. }
  585. }
  586. }
  587. }
  588. if (mb.is_valid() && mb->get_button_index() == MOUSE_BUTTON_LEFT && !mb->is_pressed()) {
  589. if (connecting_valid) {
  590. if (connecting && connecting_target) {
  591. String from = connecting_from;
  592. int from_slot = connecting_index;
  593. String to = connecting_target_to;
  594. int to_slot = connecting_target_index;
  595. if (!connecting_out) {
  596. SWAP(from, to);
  597. SWAP(from_slot, to_slot);
  598. }
  599. emit_signal(SNAME("connection_request"), from, from_slot, to, to_slot);
  600. } else if (!just_disconnected) {
  601. String from = connecting_from;
  602. int from_slot = connecting_index;
  603. Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
  604. if (!connecting_out) {
  605. emit_signal(SNAME("connection_from_empty"), from, from_slot, ofs);
  606. } else {
  607. emit_signal(SNAME("connection_to_empty"), from, from_slot, ofs);
  608. }
  609. }
  610. }
  611. connecting = false;
  612. top_layer->update();
  613. minimap->update();
  614. update();
  615. connections_layer->update();
  616. }
  617. }
  618. bool GraphEdit::_check_clickable_control(Control *p_control, const Vector2 &pos) {
  619. if (p_control->is_set_as_top_level() || !p_control->is_visible()) {
  620. return false;
  621. }
  622. if (!p_control->has_point(pos) || p_control->get_mouse_filter() == MOUSE_FILTER_IGNORE) {
  623. //test children
  624. for (int i = 0; i < p_control->get_child_count(); i++) {
  625. Control *subchild = Object::cast_to<Control>(p_control->get_child(i));
  626. if (!subchild) {
  627. continue;
  628. }
  629. if (_check_clickable_control(subchild, pos - subchild->get_position())) {
  630. return true;
  631. }
  632. }
  633. return false;
  634. } else {
  635. return true;
  636. }
  637. }
  638. bool GraphEdit::is_in_hot_zone(const Vector2 &pos, const Vector2 &p_mouse_pos, const Vector2i &p_port_size, bool p_left) {
  639. if (p_left) {
  640. if (!Rect2(
  641. pos.x - p_port_size.x / 2 - port_grab_distance_horizontal,
  642. pos.y - p_port_size.y / 2 - port_grab_distance_vertical / 2,
  643. p_port_size.x + port_grab_distance_horizontal,
  644. p_port_size.y + port_grab_distance_vertical)
  645. .has_point(p_mouse_pos)) {
  646. return false;
  647. }
  648. } else {
  649. if (!Rect2(
  650. pos.x - p_port_size.x / 2,
  651. pos.y - p_port_size.y / 2 - port_grab_distance_vertical / 2,
  652. p_port_size.x + port_grab_distance_horizontal,
  653. p_port_size.y + port_grab_distance_vertical)
  654. .has_point(p_mouse_pos)) {
  655. return false;
  656. }
  657. }
  658. for (int i = 0; i < get_child_count(); i++) {
  659. Control *child = Object::cast_to<Control>(get_child(i));
  660. if (!child) {
  661. continue;
  662. }
  663. Rect2 rect = child->get_rect();
  664. // To prevent intersections with other nodes.
  665. rect.position *= zoom;
  666. rect.size *= zoom;
  667. if (rect.has_point(p_mouse_pos)) {
  668. //check sub-controls
  669. Vector2 subpos = p_mouse_pos - rect.position;
  670. for (int j = 0; j < child->get_child_count(); j++) {
  671. Control *subchild = Object::cast_to<Control>(child->get_child(j));
  672. if (!subchild) {
  673. continue;
  674. }
  675. if (_check_clickable_control(subchild, subpos - subchild->get_position())) {
  676. return false;
  677. }
  678. }
  679. }
  680. }
  681. return true;
  682. }
  683. PackedVector2Array GraphEdit::get_connection_line(const Vector2 &p_from, const Vector2 &p_to) {
  684. if (get_script_instance() && get_script_instance()->get_script().is_valid() && get_script_instance()->has_method("_get_connection_line")) {
  685. return get_script_instance()->call("_get_connection_line", p_from, p_to);
  686. }
  687. Curve2D curve;
  688. Vector<Color> colors;
  689. curve.add_point(p_from);
  690. curve.set_point_out(0, Vector2(60, 0));
  691. curve.add_point(p_to);
  692. curve.set_point_in(1, Vector2(-60, 0));
  693. return curve.tessellate();
  694. }
  695. 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) {
  696. Vector<Vector2> points = get_connection_line(p_from / p_zoom, p_to / p_zoom);
  697. Vector<Vector2> scaled_points;
  698. Vector<Color> colors;
  699. float length = p_from.distance_to(p_to);
  700. for (int i = 0; i < points.size(); i++) {
  701. float d = p_from.distance_to(points[i]) / length;
  702. colors.push_back(p_color.lerp(p_to_color, d));
  703. scaled_points.push_back(points[i] * p_zoom);
  704. }
  705. #ifdef TOOLS_ENABLED
  706. p_where->draw_polyline_colors(scaled_points, colors, Math::floor(p_width * EDSCALE), lines_antialiased);
  707. #else
  708. p_where->draw_polyline_colors(scaled_points, colors, p_width, lines_antialiased);
  709. #endif
  710. }
  711. void GraphEdit::_connections_layer_draw() {
  712. Color activity_color = get_theme_color(SNAME("activity"));
  713. //draw connections
  714. List<List<Connection>::Element *> to_erase;
  715. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  716. NodePath fromnp(E->get().from);
  717. Node *from = get_node(fromnp);
  718. if (!from) {
  719. to_erase.push_back(E);
  720. continue;
  721. }
  722. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  723. if (!gfrom) {
  724. to_erase.push_back(E);
  725. continue;
  726. }
  727. NodePath tonp(E->get().to);
  728. Node *to = get_node(tonp);
  729. if (!to) {
  730. to_erase.push_back(E);
  731. continue;
  732. }
  733. GraphNode *gto = Object::cast_to<GraphNode>(to);
  734. if (!gto) {
  735. to_erase.push_back(E);
  736. continue;
  737. }
  738. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_position_offset() * zoom;
  739. Color color = gfrom->get_connection_output_color(E->get().from_port);
  740. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_position_offset() * zoom;
  741. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  742. if (E->get().activity > 0) {
  743. color = color.lerp(activity_color, E->get().activity);
  744. tocolor = tocolor.lerp(activity_color, E->get().activity);
  745. }
  746. _draw_connection_line(connections_layer, frompos, topos, color, tocolor, lines_thickness, zoom);
  747. }
  748. while (to_erase.size()) {
  749. connections.erase(to_erase.front()->get());
  750. to_erase.pop_front();
  751. }
  752. }
  753. void GraphEdit::_top_layer_draw() {
  754. _update_scroll();
  755. if (connecting) {
  756. Node *fromn = get_node(connecting_from);
  757. ERR_FAIL_COND(!fromn);
  758. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  759. ERR_FAIL_COND(!from);
  760. Vector2 pos;
  761. if (connecting_out) {
  762. pos = from->get_connection_output_position(connecting_index);
  763. } else {
  764. pos = from->get_connection_input_position(connecting_index);
  765. }
  766. pos += from->get_position();
  767. Vector2 topos;
  768. topos = connecting_to;
  769. Color col = connecting_color;
  770. if (connecting_target) {
  771. col.r += 0.4;
  772. col.g += 0.4;
  773. col.b += 0.4;
  774. }
  775. if (!connecting_out) {
  776. SWAP(pos, topos);
  777. }
  778. _draw_connection_line(top_layer, pos, topos, col, col, lines_thickness, zoom);
  779. }
  780. if (box_selecting) {
  781. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_fill")));
  782. top_layer->draw_rect(box_selecting_rect, get_theme_color(SNAME("selection_stroke")), false);
  783. }
  784. }
  785. void GraphEdit::_minimap_draw() {
  786. if (!is_minimap_enabled()) {
  787. return;
  788. }
  789. minimap->update_minimap();
  790. // Draw the minimap background.
  791. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  792. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("bg")), minimap_rect);
  793. Vector2 graph_offset = minimap->_get_graph_offset();
  794. Vector2 minimap_offset = minimap->minimap_offset;
  795. // Draw comment graph nodes.
  796. for (int i = get_child_count() - 1; i >= 0; i--) {
  797. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  798. if (!gn || !gn->is_comment()) {
  799. continue;
  800. }
  801. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  802. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  803. Rect2 node_rect = Rect2(node_position, node_size);
  804. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  805. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  806. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "commentfocus" : "comment");
  807. if (sbf.is_valid()) {
  808. Color node_color = sbf->get_bg_color();
  809. sb_minimap->set_bg_color(node_color);
  810. }
  811. minimap->draw_style_box(sb_minimap, node_rect);
  812. }
  813. // Draw regular graph nodes.
  814. for (int i = get_child_count() - 1; i >= 0; i--) {
  815. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  816. if (!gn || gn->is_comment()) {
  817. continue;
  818. }
  819. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_position_offset() * zoom - graph_offset) + minimap_offset;
  820. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  821. Rect2 node_rect = Rect2(node_position, node_size);
  822. Ref<StyleBoxFlat> sb_minimap = minimap->get_theme_stylebox(SNAME("node"))->duplicate();
  823. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  824. Ref<StyleBoxFlat> sbf = gn->get_theme_stylebox(gn->is_selected() ? "selectedframe" : "frame");
  825. if (sbf.is_valid()) {
  826. Color node_color = sbf->get_border_color();
  827. sb_minimap->set_bg_color(node_color);
  828. }
  829. minimap->draw_style_box(sb_minimap, node_rect);
  830. }
  831. // Draw node connections.
  832. Color activity_color = get_theme_color(SNAME("activity"));
  833. for (const Connection &E : connections) {
  834. NodePath fromnp(E.from);
  835. Node *from = get_node(fromnp);
  836. if (!from) {
  837. continue;
  838. }
  839. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  840. if (!gfrom) {
  841. continue;
  842. }
  843. NodePath tonp(E.to);
  844. Node *to = get_node(tonp);
  845. if (!to) {
  846. continue;
  847. }
  848. GraphNode *gto = Object::cast_to<GraphNode>(to);
  849. if (!gto) {
  850. continue;
  851. }
  852. Vector2 from_slot_position = gfrom->get_position_offset() * zoom + gfrom->get_connection_output_position(E.from_port);
  853. Vector2 from_position = minimap->_convert_from_graph_position(from_slot_position - graph_offset) + minimap_offset;
  854. Color from_color = gfrom->get_connection_output_color(E.from_port);
  855. Vector2 to_slot_position = gto->get_position_offset() * zoom + gto->get_connection_input_position(E.to_port);
  856. Vector2 to_position = minimap->_convert_from_graph_position(to_slot_position - graph_offset) + minimap_offset;
  857. Color to_color = gto->get_connection_input_color(E.to_port);
  858. if (E.activity > 0) {
  859. from_color = from_color.lerp(activity_color, E.activity);
  860. to_color = to_color.lerp(activity_color, E.activity);
  861. }
  862. _draw_connection_line(minimap, from_position, to_position, from_color, to_color, 0.1, minimap->_convert_from_graph_position(Vector2(zoom, zoom)).length());
  863. }
  864. // Draw the "camera" viewport.
  865. Rect2 camera_rect = minimap->get_camera_rect();
  866. minimap->draw_style_box(minimap->get_theme_stylebox(SNAME("camera")), camera_rect);
  867. // Draw the resizer control.
  868. Ref<Texture2D> resizer = minimap->get_theme_icon(SNAME("resizer"));
  869. Color resizer_color = minimap->get_theme_color(SNAME("resizer_color"));
  870. minimap->draw_texture(resizer, Point2(), resizer_color);
  871. }
  872. void GraphEdit::set_selected(Node *p_child) {
  873. for (int i = get_child_count() - 1; i >= 0; i--) {
  874. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  875. if (!gn) {
  876. continue;
  877. }
  878. gn->set_selected(gn == p_child);
  879. }
  880. }
  881. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  882. ERR_FAIL_COND(p_ev.is_null());
  883. Ref<InputEventMouseMotion> mm = p_ev;
  884. if (mm.is_valid() && (mm->get_button_mask() & MOUSE_BUTTON_MASK_MIDDLE || (mm->get_button_mask() & MOUSE_BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
  885. Vector2i relative = Input::get_singleton()->warp_mouse_motion(mm, get_global_rect());
  886. h_scroll->set_value(h_scroll->get_value() - relative.x);
  887. v_scroll->set_value(v_scroll->get_value() - relative.y);
  888. }
  889. if (mm.is_valid() && dragging) {
  890. if (!moving_selection) {
  891. emit_signal(SNAME("begin_node_move"));
  892. moving_selection = true;
  893. }
  894. just_selected = true;
  895. drag_accum += mm->get_relative();
  896. for (int i = get_child_count() - 1; i >= 0; i--) {
  897. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  898. if (gn && gn->is_selected()) {
  899. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  900. // Snapping can be toggled temporarily by holding down Ctrl.
  901. // This is done here as to not toggle the grid when holding down Ctrl.
  902. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  903. const int snap = get_snap();
  904. pos = pos.snapped(Vector2(snap, snap));
  905. }
  906. gn->set_position_offset(pos);
  907. }
  908. }
  909. }
  910. if (mm.is_valid() && box_selecting) {
  911. box_selecting_to = mm->get_position();
  912. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  913. MIN(box_selecting_from.y, box_selecting_to.y),
  914. ABS(box_selecting_from.x - box_selecting_to.x),
  915. ABS(box_selecting_from.y - box_selecting_to.y));
  916. for (int i = get_child_count() - 1; i >= 0; i--) {
  917. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  918. if (!gn) {
  919. continue;
  920. }
  921. Rect2 r = gn->get_rect();
  922. r.size *= zoom;
  923. bool in_box = r.intersects(box_selecting_rect);
  924. if (in_box) {
  925. if (!gn->is_selected() && box_selection_mode_additive) {
  926. emit_signal(SNAME("node_selected"), gn);
  927. } else if (gn->is_selected() && !box_selection_mode_additive) {
  928. emit_signal(SNAME("node_deselected"), gn);
  929. }
  930. gn->set_selected(box_selection_mode_additive);
  931. } else {
  932. bool select = (previous_selected.find(gn) != nullptr);
  933. if (gn->is_selected() && !select) {
  934. emit_signal(SNAME("node_deselected"), gn);
  935. } else if (!gn->is_selected() && select) {
  936. emit_signal(SNAME("node_selected"), gn);
  937. }
  938. gn->set_selected(select);
  939. }
  940. }
  941. top_layer->update();
  942. minimap->update();
  943. }
  944. Ref<InputEventMouseButton> b = p_ev;
  945. if (b.is_valid()) {
  946. if (b->get_button_index() == MOUSE_BUTTON_RIGHT && b->is_pressed()) {
  947. if (box_selecting) {
  948. box_selecting = false;
  949. for (int i = get_child_count() - 1; i >= 0; i--) {
  950. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  951. if (!gn) {
  952. continue;
  953. }
  954. bool select = (previous_selected.find(gn) != nullptr);
  955. if (gn->is_selected() && !select) {
  956. emit_signal(SNAME("node_deselected"), gn);
  957. } else if (!gn->is_selected() && select) {
  958. emit_signal(SNAME("node_selected"), gn);
  959. }
  960. gn->set_selected(select);
  961. }
  962. top_layer->update();
  963. minimap->update();
  964. } else {
  965. if (connecting) {
  966. connecting = false;
  967. top_layer->update();
  968. minimap->update();
  969. } else {
  970. emit_signal(SNAME("popup_request"), b->get_global_position());
  971. }
  972. }
  973. }
  974. if (b->get_button_index() == MOUSE_BUTTON_LEFT && !b->is_pressed() && dragging) {
  975. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  976. //deselect current node
  977. for (int i = get_child_count() - 1; i >= 0; i--) {
  978. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  979. if (gn) {
  980. Rect2 r = gn->get_rect();
  981. r.size *= zoom;
  982. if (r.has_point(b->get_position())) {
  983. emit_signal(SNAME("node_deselected"), gn);
  984. gn->set_selected(false);
  985. }
  986. }
  987. }
  988. }
  989. if (drag_accum != Vector2()) {
  990. for (int i = get_child_count() - 1; i >= 0; i--) {
  991. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  992. if (gn && gn->is_selected()) {
  993. gn->set_drag(false);
  994. }
  995. }
  996. }
  997. if (moving_selection) {
  998. emit_signal(SNAME("end_node_move"));
  999. moving_selection = false;
  1000. }
  1001. dragging = false;
  1002. top_layer->update();
  1003. minimap->update();
  1004. update();
  1005. connections_layer->update();
  1006. }
  1007. if (b->get_button_index() == MOUSE_BUTTON_LEFT && b->is_pressed()) {
  1008. GraphNode *gn = nullptr;
  1009. for (int i = get_child_count() - 1; i >= 0; i--) {
  1010. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  1011. if (gn_selected) {
  1012. if (gn_selected->is_resizing()) {
  1013. continue;
  1014. }
  1015. if (gn_selected->has_point((b->get_position() - gn_selected->get_position()) / zoom)) {
  1016. gn = gn_selected;
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. if (gn) {
  1022. if (_filter_input(b->get_position())) {
  1023. return;
  1024. }
  1025. dragging = true;
  1026. drag_accum = Vector2();
  1027. just_selected = !gn->is_selected();
  1028. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CTRL)) {
  1029. for (int i = 0; i < get_child_count(); i++) {
  1030. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1031. if (o_gn) {
  1032. if (o_gn == gn) {
  1033. o_gn->set_selected(true);
  1034. } else {
  1035. if (o_gn->is_selected()) {
  1036. emit_signal(SNAME("node_deselected"), o_gn);
  1037. }
  1038. o_gn->set_selected(false);
  1039. }
  1040. }
  1041. }
  1042. }
  1043. gn->set_selected(true);
  1044. for (int i = 0; i < get_child_count(); i++) {
  1045. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1046. if (!o_gn) {
  1047. continue;
  1048. }
  1049. if (o_gn->is_selected()) {
  1050. o_gn->set_drag(true);
  1051. }
  1052. }
  1053. } else {
  1054. if (_filter_input(b->get_position())) {
  1055. return;
  1056. }
  1057. if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  1058. return;
  1059. }
  1060. box_selecting = true;
  1061. box_selecting_from = b->get_position();
  1062. if (b->is_ctrl_pressed()) {
  1063. box_selection_mode_additive = true;
  1064. previous_selected.clear();
  1065. for (int i = get_child_count() - 1; i >= 0; i--) {
  1066. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1067. if (!gn2 || !gn2->is_selected()) {
  1068. continue;
  1069. }
  1070. previous_selected.push_back(gn2);
  1071. }
  1072. } else if (b->is_shift_pressed()) {
  1073. box_selection_mode_additive = false;
  1074. previous_selected.clear();
  1075. for (int i = get_child_count() - 1; i >= 0; i--) {
  1076. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1077. if (!gn2 || !gn2->is_selected()) {
  1078. continue;
  1079. }
  1080. previous_selected.push_back(gn2);
  1081. }
  1082. } else {
  1083. box_selection_mode_additive = true;
  1084. previous_selected.clear();
  1085. for (int i = get_child_count() - 1; i >= 0; i--) {
  1086. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1087. if (!gn2) {
  1088. continue;
  1089. }
  1090. if (gn2->is_selected()) {
  1091. emit_signal(SNAME("node_deselected"), gn2);
  1092. }
  1093. gn2->set_selected(false);
  1094. }
  1095. }
  1096. }
  1097. }
  1098. if (b->get_button_index() == MOUSE_BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  1099. box_selecting = false;
  1100. box_selecting_rect = Rect2();
  1101. previous_selected.clear();
  1102. top_layer->update();
  1103. minimap->update();
  1104. }
  1105. int scroll_direction = (b->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN) - (b->get_button_index() == MOUSE_BUTTON_WHEEL_UP);
  1106. if (scroll_direction != 0) {
  1107. if (b->is_ctrl_pressed()) {
  1108. if (b->is_shift_pressed()) {
  1109. // Horizontal scrolling.
  1110. h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1111. } else {
  1112. // Vertical scrolling.
  1113. v_scroll->set_value(v_scroll->get_value() + (v_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1114. }
  1115. } else {
  1116. // Zooming.
  1117. set_zoom_custom(scroll_direction < 0 ? zoom * zoom_step : zoom / zoom_step, b->get_position());
  1118. }
  1119. }
  1120. }
  1121. if (p_ev->is_pressed()) {
  1122. if (p_ev->is_action("ui_graph_duplicate")) {
  1123. emit_signal(SNAME("duplicate_nodes_request"));
  1124. accept_event();
  1125. } else if (p_ev->is_action("ui_copy")) {
  1126. emit_signal(SNAME("copy_nodes_request"));
  1127. accept_event();
  1128. } else if (p_ev->is_action("ui_paste")) {
  1129. emit_signal(SNAME("paste_nodes_request"));
  1130. accept_event();
  1131. } else if (p_ev->is_action("ui_graph_delete")) {
  1132. emit_signal(SNAME("delete_nodes_request"));
  1133. accept_event();
  1134. }
  1135. }
  1136. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  1137. if (magnify_gesture.is_valid()) {
  1138. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  1139. }
  1140. Ref<InputEventPanGesture> pan_gesture = p_ev;
  1141. if (pan_gesture.is_valid()) {
  1142. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  1143. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  1144. }
  1145. }
  1146. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1147. for (Connection &E : connections) {
  1148. if (E.from == p_from && E.from_port == p_from_port && E.to == p_to && E.to_port == p_to_port) {
  1149. if (Math::is_equal_approx(E.activity, p_activity)) {
  1150. //update only if changed
  1151. top_layer->update();
  1152. minimap->update();
  1153. connections_layer->update();
  1154. }
  1155. E.activity = p_activity;
  1156. return;
  1157. }
  1158. }
  1159. }
  1160. void GraphEdit::clear_connections() {
  1161. connections.clear();
  1162. minimap->update();
  1163. update();
  1164. connections_layer->update();
  1165. }
  1166. void GraphEdit::set_zoom(float p_zoom) {
  1167. set_zoom_custom(p_zoom, get_size() / 2);
  1168. }
  1169. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1170. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1171. if (zoom == p_zoom) {
  1172. return;
  1173. }
  1174. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  1175. zoom = p_zoom;
  1176. top_layer->update();
  1177. zoom_minus->set_disabled(zoom == zoom_min);
  1178. zoom_plus->set_disabled(zoom == zoom_max);
  1179. _update_scroll();
  1180. minimap->update();
  1181. connections_layer->update();
  1182. if (is_visible_in_tree()) {
  1183. Vector2 ofs = sbofs * zoom - p_center;
  1184. h_scroll->set_value(ofs.x);
  1185. v_scroll->set_value(ofs.y);
  1186. }
  1187. _update_zoom_label();
  1188. update();
  1189. }
  1190. float GraphEdit::get_zoom() const {
  1191. return zoom;
  1192. }
  1193. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1194. p_zoom_step = abs(p_zoom_step);
  1195. if (zoom_step == p_zoom_step) {
  1196. return;
  1197. }
  1198. zoom_step = p_zoom_step;
  1199. }
  1200. float GraphEdit::get_zoom_step() const {
  1201. return zoom_step;
  1202. }
  1203. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1204. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1205. if (zoom_min == p_zoom_min) {
  1206. return;
  1207. }
  1208. zoom_min = p_zoom_min;
  1209. set_zoom(zoom);
  1210. }
  1211. float GraphEdit::get_zoom_min() const {
  1212. return zoom_min;
  1213. }
  1214. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1215. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1216. if (zoom_max == p_zoom_max) {
  1217. return;
  1218. }
  1219. zoom_max = p_zoom_max;
  1220. set_zoom(zoom);
  1221. }
  1222. float GraphEdit::get_zoom_max() const {
  1223. return zoom_max;
  1224. }
  1225. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1226. if (zoom_label->is_visible() == p_enable) {
  1227. return;
  1228. }
  1229. zoom_label->set_visible(p_enable);
  1230. }
  1231. bool GraphEdit::is_showing_zoom_label() const {
  1232. return zoom_label->is_visible();
  1233. }
  1234. void GraphEdit::set_right_disconnects(bool p_enable) {
  1235. right_disconnects = p_enable;
  1236. }
  1237. bool GraphEdit::is_right_disconnects_enabled() const {
  1238. return right_disconnects;
  1239. }
  1240. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1241. valid_right_disconnect_types.insert(p_type);
  1242. }
  1243. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1244. valid_right_disconnect_types.erase(p_type);
  1245. }
  1246. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1247. valid_left_disconnect_types.insert(p_type);
  1248. }
  1249. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1250. valid_left_disconnect_types.erase(p_type);
  1251. }
  1252. Array GraphEdit::_get_connection_list() const {
  1253. List<Connection> conns;
  1254. get_connection_list(&conns);
  1255. Array arr;
  1256. for (const Connection &E : conns) {
  1257. Dictionary d;
  1258. d["from"] = E.from;
  1259. d["from_port"] = E.from_port;
  1260. d["to"] = E.to;
  1261. d["to_port"] = E.to_port;
  1262. arr.push_back(d);
  1263. }
  1264. return arr;
  1265. }
  1266. void GraphEdit::_zoom_minus() {
  1267. set_zoom(zoom / zoom_step);
  1268. }
  1269. void GraphEdit::_zoom_reset() {
  1270. set_zoom(1);
  1271. }
  1272. void GraphEdit::_zoom_plus() {
  1273. set_zoom(zoom * zoom_step);
  1274. }
  1275. void GraphEdit::_update_zoom_label() {
  1276. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1277. String zoom_text = itos(zoom_percent) + "%";
  1278. zoom_label->set_text(zoom_text);
  1279. }
  1280. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1281. ConnType ct;
  1282. ct.type_a = p_type;
  1283. ct.type_b = p_with_type;
  1284. valid_connection_types.insert(ct);
  1285. }
  1286. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1287. ConnType ct;
  1288. ct.type_a = p_type;
  1289. ct.type_b = p_with_type;
  1290. valid_connection_types.erase(ct);
  1291. }
  1292. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1293. ConnType ct;
  1294. ct.type_a = p_type;
  1295. ct.type_b = p_with_type;
  1296. return valid_connection_types.has(ct);
  1297. }
  1298. void GraphEdit::set_use_snap(bool p_enable) {
  1299. snap_button->set_pressed(p_enable);
  1300. update();
  1301. }
  1302. bool GraphEdit::is_using_snap() const {
  1303. return snap_button->is_pressed();
  1304. }
  1305. int GraphEdit::get_snap() const {
  1306. return snap_amount->get_value();
  1307. }
  1308. void GraphEdit::set_snap(int p_snap) {
  1309. ERR_FAIL_COND(p_snap < 5);
  1310. snap_amount->set_value(p_snap);
  1311. update();
  1312. }
  1313. void GraphEdit::_snap_toggled() {
  1314. update();
  1315. }
  1316. void GraphEdit::_snap_value_changed(double) {
  1317. update();
  1318. }
  1319. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1320. minimap->set_size(p_size);
  1321. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1322. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1323. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1324. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1325. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  1326. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  1327. minimap->update();
  1328. }
  1329. Vector2 GraphEdit::get_minimap_size() const {
  1330. return minimap->get_size();
  1331. }
  1332. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1333. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1334. minimap->update();
  1335. }
  1336. float GraphEdit::get_minimap_opacity() const {
  1337. Color minimap_modulate = minimap->get_modulate();
  1338. return minimap_modulate.a;
  1339. }
  1340. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1341. minimap_button->set_pressed(p_enable);
  1342. minimap->update();
  1343. }
  1344. bool GraphEdit::is_minimap_enabled() const {
  1345. return minimap_button->is_pressed();
  1346. }
  1347. void GraphEdit::_minimap_toggled() {
  1348. if (is_minimap_enabled()) {
  1349. minimap->set_visible(true);
  1350. minimap->update();
  1351. } else {
  1352. minimap->set_visible(false);
  1353. }
  1354. }
  1355. void GraphEdit::set_connection_lines_thickness(float p_thickness) {
  1356. lines_thickness = p_thickness;
  1357. update();
  1358. }
  1359. float GraphEdit::get_connection_lines_thickness() const {
  1360. return lines_thickness;
  1361. }
  1362. void GraphEdit::set_connection_lines_antialiased(bool p_antialiased) {
  1363. lines_antialiased = p_antialiased;
  1364. update();
  1365. }
  1366. bool GraphEdit::is_connection_lines_antialiased() const {
  1367. return lines_antialiased;
  1368. }
  1369. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1370. return zoom_hb;
  1371. }
  1372. int GraphEdit::_set_operations(SET_OPERATIONS p_operation, Set<StringName> &r_u, const Set<StringName> &r_v) {
  1373. switch (p_operation) {
  1374. case GraphEdit::IS_EQUAL: {
  1375. for (Set<StringName>::Element *E = r_u.front(); E; E = E->next()) {
  1376. if (!r_v.has(E->get()))
  1377. return 0;
  1378. }
  1379. return r_u.size() == r_v.size();
  1380. } break;
  1381. case GraphEdit::IS_SUBSET: {
  1382. if (r_u.size() == r_v.size() && !r_u.size()) {
  1383. return 1;
  1384. }
  1385. for (Set<StringName>::Element *E = r_u.front(); E; E = E->next()) {
  1386. if (!r_v.has(E->get()))
  1387. return 0;
  1388. }
  1389. return 1;
  1390. } break;
  1391. case GraphEdit::DIFFERENCE: {
  1392. for (Set<StringName>::Element *E = r_u.front(); E; E = E->next()) {
  1393. if (r_v.has(E->get())) {
  1394. r_u.erase(E->get());
  1395. }
  1396. }
  1397. return r_u.size();
  1398. } break;
  1399. case GraphEdit::UNION: {
  1400. for (Set<StringName>::Element *E = r_v.front(); E; E = E->next()) {
  1401. if (!r_u.has(E->get())) {
  1402. r_u.insert(E->get());
  1403. }
  1404. }
  1405. return r_v.size();
  1406. } break;
  1407. default:
  1408. break;
  1409. }
  1410. return -1;
  1411. }
  1412. HashMap<int, Vector<StringName>> GraphEdit::_layering(const Set<StringName> &r_selected_nodes, const HashMap<StringName, Set<StringName>> &r_upper_neighbours) {
  1413. HashMap<int, Vector<StringName>> l;
  1414. Set<StringName> p = r_selected_nodes, q = r_selected_nodes, u, z;
  1415. int current_layer = 0;
  1416. bool selected = false;
  1417. while (!_set_operations(GraphEdit::IS_EQUAL, q, u)) {
  1418. _set_operations(GraphEdit::DIFFERENCE, p, u);
  1419. for (const Set<StringName>::Element *E = p.front(); E; E = E->next()) {
  1420. Set<StringName> n = r_upper_neighbours[E->get()];
  1421. if (_set_operations(GraphEdit::IS_SUBSET, n, z)) {
  1422. Vector<StringName> t;
  1423. t.push_back(E->get());
  1424. if (!l.has(current_layer)) {
  1425. l.set(current_layer, Vector<StringName>{});
  1426. }
  1427. selected = true;
  1428. t.append_array(l[current_layer]);
  1429. l.set(current_layer, t);
  1430. Set<StringName> V;
  1431. V.insert(E->get());
  1432. _set_operations(GraphEdit::UNION, u, V);
  1433. }
  1434. }
  1435. if (!selected) {
  1436. current_layer++;
  1437. _set_operations(GraphEdit::UNION, z, u);
  1438. }
  1439. selected = false;
  1440. }
  1441. return l;
  1442. }
  1443. Vector<StringName> GraphEdit::_split(const Vector<StringName> &r_layer, const HashMap<StringName, Dictionary> &r_crossings) {
  1444. if (!r_layer.size()) {
  1445. return Vector<StringName>();
  1446. }
  1447. StringName p = r_layer[Math::random(0, r_layer.size() - 1)];
  1448. Vector<StringName> left;
  1449. Vector<StringName> right;
  1450. for (int i = 0; i < r_layer.size(); i++) {
  1451. if (p != r_layer[i]) {
  1452. StringName q = r_layer[i];
  1453. int cross_pq = r_crossings[p][q];
  1454. int cross_qp = r_crossings[q][p];
  1455. if (cross_pq > cross_qp) {
  1456. left.push_back(q);
  1457. } else {
  1458. right.push_back(q);
  1459. }
  1460. }
  1461. }
  1462. left.push_back(p);
  1463. left.append_array(right);
  1464. return left;
  1465. }
  1466. void GraphEdit::_horizontal_alignment(Dictionary &r_root, Dictionary &r_align, const HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, Set<StringName>> &r_upper_neighbours, const Set<StringName> &r_selected_nodes) {
  1467. for (const Set<StringName>::Element *E = r_selected_nodes.front(); E; E = E->next()) {
  1468. r_root[E->get()] = E->get();
  1469. r_align[E->get()] = E->get();
  1470. }
  1471. if (r_layers.size() == 1) {
  1472. return;
  1473. }
  1474. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1475. Vector<StringName> lower_layer = r_layers[i];
  1476. Vector<StringName> upper_layer = r_layers[i - 1];
  1477. int r = -1;
  1478. for (int j = 0; j < lower_layer.size(); j++) {
  1479. Vector<Pair<int, StringName>> up;
  1480. StringName current_node = lower_layer[j];
  1481. for (int k = 0; k < upper_layer.size(); k++) {
  1482. StringName adjacent_neighbour = upper_layer[k];
  1483. if (r_upper_neighbours[current_node].has(adjacent_neighbour)) {
  1484. up.push_back(Pair<int, StringName>(k, adjacent_neighbour));
  1485. }
  1486. }
  1487. int start = up.size() / 2;
  1488. int end = up.size() % 2 ? start : start + 1;
  1489. for (int p = start; p <= end; p++) {
  1490. StringName Align = r_align[current_node];
  1491. if (Align == current_node && r < up[p].first) {
  1492. r_align[up[p].second] = lower_layer[j];
  1493. r_root[current_node] = r_root[up[p].second];
  1494. r_align[current_node] = r_root[up[p].second];
  1495. r = up[p].first;
  1496. }
  1497. }
  1498. }
  1499. }
  1500. }
  1501. void GraphEdit::_crossing_minimisation(HashMap<int, Vector<StringName>> &r_layers, const HashMap<StringName, Set<StringName>> &r_upper_neighbours) {
  1502. if (r_layers.size() == 1) {
  1503. return;
  1504. }
  1505. for (unsigned int i = 1; i < r_layers.size(); i++) {
  1506. Vector<StringName> upper_layer = r_layers[i - 1];
  1507. Vector<StringName> lower_layer = r_layers[i];
  1508. HashMap<StringName, Dictionary> c;
  1509. for (int j = 0; j < lower_layer.size(); j++) {
  1510. StringName p = lower_layer[j];
  1511. Dictionary d;
  1512. for (int k = 0; k < lower_layer.size(); k++) {
  1513. unsigned int crossings = 0;
  1514. StringName q = lower_layer[k];
  1515. if (j != k) {
  1516. for (int h = 1; h < upper_layer.size(); h++) {
  1517. if (r_upper_neighbours[p].has(upper_layer[h])) {
  1518. for (int g = 0; g < h; g++) {
  1519. if (r_upper_neighbours[q].has(upper_layer[g])) {
  1520. crossings++;
  1521. }
  1522. }
  1523. }
  1524. }
  1525. }
  1526. d[q] = crossings;
  1527. }
  1528. c.set(p, d);
  1529. }
  1530. r_layers.set(i, _split(lower_layer, c));
  1531. }
  1532. }
  1533. void GraphEdit::_calculate_inner_shifts(Dictionary &r_inner_shifts, const Dictionary &r_root, const Dictionary &r_node_names, const Dictionary &r_align, const Set<StringName> &r_block_heads, const HashMap<StringName, Pair<int, int>> &r_port_info) {
  1534. for (const Set<StringName>::Element *E = r_block_heads.front(); E; E = E->next()) {
  1535. real_t left = 0;
  1536. StringName u = E->get();
  1537. StringName v = r_align[u];
  1538. while (u != v && (StringName)r_root[u] != v) {
  1539. String _connection = String(u) + " " + String(v);
  1540. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[u]);
  1541. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[v]);
  1542. Pair<int, int> ports = r_port_info[_connection];
  1543. int pfrom = ports.first;
  1544. int pto = ports.second;
  1545. Vector2 frompos = gfrom->get_connection_output_position(pfrom);
  1546. Vector2 topos = gto->get_connection_input_position(pto);
  1547. real_t s = (real_t)r_inner_shifts[u] + (frompos.y - topos.y) / zoom;
  1548. r_inner_shifts[v] = s;
  1549. left = MIN(left, s);
  1550. u = v;
  1551. v = (StringName)r_align[v];
  1552. }
  1553. u = E->get();
  1554. do {
  1555. r_inner_shifts[u] = (real_t)r_inner_shifts[u] - left;
  1556. u = (StringName)r_align[u];
  1557. } while (u != E->get());
  1558. }
  1559. }
  1560. float GraphEdit::_calculate_threshold(StringName p_v, StringName p_w, const Dictionary &r_node_names, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_inner_shift, real_t p_current_threshold, const HashMap<StringName, Vector2> &r_node_positions) {
  1561. #define MAX_ORDER 2147483647
  1562. #define ORDER(node, layers) \
  1563. for (unsigned int i = 0; i < layers.size(); i++) { \
  1564. int index = layers[i].find(node); \
  1565. if (index > 0) { \
  1566. order = index; \
  1567. break; \
  1568. } \
  1569. order = MAX_ORDER; \
  1570. }
  1571. int order = MAX_ORDER;
  1572. float threshold = p_current_threshold;
  1573. if (p_v == p_w) {
  1574. int min_order = MAX_ORDER;
  1575. Connection incoming;
  1576. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1577. if (E->get().to == p_w) {
  1578. ORDER(E->get().from, r_layers);
  1579. if (min_order > order) {
  1580. min_order = order;
  1581. incoming = E->get();
  1582. }
  1583. }
  1584. }
  1585. if (incoming.from != StringName()) {
  1586. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[incoming.from]);
  1587. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1588. Vector2 frompos = gfrom->get_connection_output_position(incoming.from_port);
  1589. Vector2 topos = gto->get_connection_input_position(incoming.to_port);
  1590. //If connected block node is selected, calculate thershold or add current block to list
  1591. if (gfrom->is_selected()) {
  1592. Vector2 connected_block_pos = r_node_positions[r_root[incoming.from]];
  1593. if (connected_block_pos.y != FLT_MAX) {
  1594. //Connected block is placed. Calculate threshold
  1595. threshold = connected_block_pos.y + (real_t)r_inner_shift[incoming.from] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1596. }
  1597. }
  1598. }
  1599. }
  1600. if (threshold == FLT_MIN && (StringName)r_align[p_w] == p_v) {
  1601. //This time, pick an outgoing edge and repeat as above!
  1602. int min_order = MAX_ORDER;
  1603. Connection outgoing;
  1604. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1605. if (E->get().from == p_w) {
  1606. ORDER(E->get().to, r_layers);
  1607. if (min_order > order) {
  1608. min_order = order;
  1609. outgoing = E->get();
  1610. }
  1611. }
  1612. }
  1613. if (outgoing.to != StringName()) {
  1614. GraphNode *gfrom = Object::cast_to<GraphNode>(r_node_names[p_w]);
  1615. GraphNode *gto = Object::cast_to<GraphNode>(r_node_names[outgoing.to]);
  1616. Vector2 frompos = gfrom->get_connection_output_position(outgoing.from_port);
  1617. Vector2 topos = gto->get_connection_input_position(outgoing.to_port);
  1618. //If connected block node is selected, calculate thershold or add current block to list
  1619. if (gto->is_selected()) {
  1620. Vector2 connected_block_pos = r_node_positions[r_root[outgoing.to]];
  1621. if (connected_block_pos.y != FLT_MAX) {
  1622. //Connected block is placed. Calculate threshold
  1623. threshold = connected_block_pos.y + (real_t)r_inner_shift[outgoing.to] - (real_t)r_inner_shift[p_w] + frompos.y - topos.y;
  1624. }
  1625. }
  1626. }
  1627. }
  1628. #undef MAX_ORDER
  1629. #undef ORDER
  1630. return threshold;
  1631. }
  1632. void GraphEdit::_place_block(StringName p_v, float p_delta, const HashMap<int, Vector<StringName>> &r_layers, const Dictionary &r_root, const Dictionary &r_align, const Dictionary &r_node_name, const Dictionary &r_inner_shift, Dictionary &r_sink, Dictionary &r_shift, HashMap<StringName, Vector2> &r_node_positions) {
  1633. #define PRED(node, layers) \
  1634. for (unsigned int i = 0; i < layers.size(); i++) { \
  1635. int index = layers[i].find(node); \
  1636. if (index > 0) { \
  1637. predecessor = layers[i][index - 1]; \
  1638. break; \
  1639. } \
  1640. predecessor = StringName(); \
  1641. }
  1642. StringName predecessor;
  1643. StringName successor;
  1644. Vector2 pos = r_node_positions[p_v];
  1645. if (pos.y == FLT_MAX) {
  1646. pos.y = 0;
  1647. bool initial = false;
  1648. StringName w = p_v;
  1649. real_t threshold = FLT_MIN;
  1650. do {
  1651. PRED(w, r_layers);
  1652. if (predecessor != StringName()) {
  1653. StringName u = r_root[predecessor];
  1654. _place_block(u, p_delta, r_layers, r_root, r_align, r_node_name, r_inner_shift, r_sink, r_shift, r_node_positions);
  1655. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1656. if ((StringName)r_sink[p_v] == p_v) {
  1657. r_sink[p_v] = r_sink[u];
  1658. }
  1659. Vector2 predecessor_root_pos = r_node_positions[u];
  1660. Vector2 predecessor_node_size = Object::cast_to<GraphNode>(r_node_name[predecessor])->get_size();
  1661. if (r_sink[p_v] != r_sink[u]) {
  1662. real_t sc = pos.y + (real_t)r_inner_shift[w] - predecessor_root_pos.y - (real_t)r_inner_shift[predecessor] - predecessor_node_size.y - p_delta;
  1663. r_shift[r_sink[u]] = MIN(sc, (real_t)r_shift[r_sink[u]]);
  1664. } else {
  1665. real_t sb = predecessor_root_pos.y + (real_t)r_inner_shift[predecessor] + predecessor_node_size.y - (real_t)r_inner_shift[w] + p_delta;
  1666. sb = MAX(sb, threshold);
  1667. if (initial) {
  1668. pos.y = sb;
  1669. } else {
  1670. pos.y = MAX(pos.y, sb);
  1671. }
  1672. initial = false;
  1673. }
  1674. }
  1675. threshold = _calculate_threshold(p_v, w, r_node_name, r_layers, r_root, r_align, r_inner_shift, threshold, r_node_positions);
  1676. w = r_align[w];
  1677. } while (w != p_v);
  1678. r_node_positions.set(p_v, pos);
  1679. }
  1680. #undef PRED
  1681. }
  1682. void GraphEdit::arrange_nodes() {
  1683. if (!arranging_graph) {
  1684. arranging_graph = true;
  1685. } else {
  1686. return;
  1687. }
  1688. Dictionary node_names;
  1689. Set<StringName> selected_nodes;
  1690. for (int i = get_child_count() - 1; i >= 0; i--) {
  1691. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1692. if (!gn) {
  1693. continue;
  1694. }
  1695. node_names[gn->get_name()] = gn;
  1696. }
  1697. HashMap<StringName, Set<StringName>> upper_neighbours;
  1698. HashMap<StringName, Pair<int, int>> port_info;
  1699. Vector2 origin(FLT_MAX, FLT_MAX);
  1700. float gap_v = 100.0f;
  1701. float gap_h = 100.0f;
  1702. for (int i = get_child_count() - 1; i >= 0; i--) {
  1703. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1704. if (!gn) {
  1705. continue;
  1706. }
  1707. if (gn->is_selected()) {
  1708. selected_nodes.insert(gn->get_name());
  1709. Set<StringName> s;
  1710. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1711. GraphNode *p_from = Object::cast_to<GraphNode>(node_names[E->get().from]);
  1712. if (E->get().to == gn->get_name() && p_from->is_selected()) {
  1713. if (!s.has(p_from->get_name())) {
  1714. s.insert(p_from->get_name());
  1715. }
  1716. String s_connection = String(p_from->get_name()) + " " + String(E->get().to);
  1717. StringName _connection(s_connection);
  1718. Pair<int, int> ports(E->get().from_port, E->get().to_port);
  1719. if (port_info.has(_connection)) {
  1720. Pair<int, int> p_ports = port_info[_connection];
  1721. if (p_ports.first < ports.first) {
  1722. ports = p_ports;
  1723. }
  1724. }
  1725. port_info.set(_connection, ports);
  1726. }
  1727. }
  1728. upper_neighbours.set(gn->get_name(), s);
  1729. }
  1730. }
  1731. if (!selected_nodes.size()) {
  1732. arranging_graph = false;
  1733. return;
  1734. }
  1735. HashMap<int, Vector<StringName>> layers = _layering(selected_nodes, upper_neighbours);
  1736. _crossing_minimisation(layers, upper_neighbours);
  1737. Dictionary root, align, sink, shift;
  1738. _horizontal_alignment(root, align, layers, upper_neighbours, selected_nodes);
  1739. HashMap<StringName, Vector2> new_positions;
  1740. Vector2 default_position(FLT_MAX, FLT_MAX);
  1741. Dictionary inner_shift;
  1742. Set<StringName> block_heads;
  1743. for (const Set<StringName>::Element *E = selected_nodes.front(); E; E = E->next()) {
  1744. inner_shift[E->get()] = 0.0f;
  1745. sink[E->get()] = E->get();
  1746. shift[E->get()] = FLT_MAX;
  1747. new_positions.set(E->get(), default_position);
  1748. if ((StringName)root[E->get()] == E->get()) {
  1749. block_heads.insert(E->get());
  1750. }
  1751. }
  1752. _calculate_inner_shifts(inner_shift, root, node_names, align, block_heads, port_info);
  1753. for (const Set<StringName>::Element *E = block_heads.front(); E; E = E->next()) {
  1754. _place_block(E->get(), gap_v, layers, root, align, node_names, inner_shift, sink, shift, new_positions);
  1755. }
  1756. origin.y = Object::cast_to<GraphNode>(node_names[layers[0][0]])->get_position_offset().y - (new_positions[layers[0][0]].y + (float)inner_shift[layers[0][0]]);
  1757. origin.x = Object::cast_to<GraphNode>(node_names[layers[0][0]])->get_position_offset().x;
  1758. for (const Set<StringName>::Element *E = block_heads.front(); E; E = E->next()) {
  1759. StringName u = E->get();
  1760. float start_from = origin.y + new_positions[E->get()].y;
  1761. do {
  1762. Vector2 cal_pos;
  1763. cal_pos.y = start_from + (real_t)inner_shift[u];
  1764. new_positions.set(u, cal_pos);
  1765. u = align[u];
  1766. } while (u != E->get());
  1767. }
  1768. //Compute horizontal co-ordinates individually for layers to get uniform gap
  1769. float start_from = origin.x;
  1770. float largest_node_size = 0.0f;
  1771. for (unsigned int i = 0; i < layers.size(); i++) {
  1772. Vector<StringName> layer = layers[i];
  1773. for (int j = 0; j < layer.size(); j++) {
  1774. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1775. largest_node_size = MAX(largest_node_size, current_node_size);
  1776. }
  1777. for (int j = 0; j < layer.size(); j++) {
  1778. float current_node_size = Object::cast_to<GraphNode>(node_names[layer[j]])->get_size().x;
  1779. Vector2 cal_pos = new_positions[layer[j]];
  1780. if (current_node_size == largest_node_size) {
  1781. cal_pos.x = start_from;
  1782. } else {
  1783. float current_node_start_pos = start_from;
  1784. if (current_node_size < largest_node_size / 2) {
  1785. if (!(i || j)) {
  1786. start_from -= (largest_node_size - current_node_size);
  1787. }
  1788. current_node_start_pos = start_from + largest_node_size - current_node_size;
  1789. }
  1790. cal_pos.x = current_node_start_pos;
  1791. }
  1792. new_positions.set(layer[j], cal_pos);
  1793. }
  1794. start_from += largest_node_size + gap_h;
  1795. largest_node_size = 0.0f;
  1796. }
  1797. emit_signal("begin_node_move");
  1798. for (const Set<StringName>::Element *E = selected_nodes.front(); E; E = E->next()) {
  1799. GraphNode *gn = Object::cast_to<GraphNode>(node_names[E->get()]);
  1800. gn->set_drag(true);
  1801. Vector2 pos = (new_positions[E->get()]);
  1802. if (is_using_snap()) {
  1803. const int snap = get_snap();
  1804. pos = pos.snapped(Vector2(snap, snap));
  1805. }
  1806. gn->set_position_offset(pos);
  1807. gn->set_drag(false);
  1808. }
  1809. emit_signal("end_node_move");
  1810. arranging_graph = false;
  1811. }
  1812. void GraphEdit::_bind_methods() {
  1813. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  1814. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  1815. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  1816. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1817. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1818. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1819. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1820. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  1821. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1822. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1823. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1824. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1825. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1826. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1827. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1828. ClassDB::bind_method(D_METHOD("get_connection_line", "from", "to"), &GraphEdit::get_connection_line);
  1829. ClassDB::bind_method(D_METHOD("set_zoom", "zoom"), &GraphEdit::set_zoom);
  1830. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1831. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1832. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1833. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1834. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1835. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1836. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1837. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1838. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1839. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1840. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1841. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1842. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1843. ClassDB::bind_method(D_METHOD("set_connection_lines_thickness", "pixels"), &GraphEdit::set_connection_lines_thickness);
  1844. ClassDB::bind_method(D_METHOD("get_connection_lines_thickness"), &GraphEdit::get_connection_lines_thickness);
  1845. ClassDB::bind_method(D_METHOD("set_connection_lines_antialiased", "pixels"), &GraphEdit::set_connection_lines_antialiased);
  1846. ClassDB::bind_method(D_METHOD("is_connection_lines_antialiased"), &GraphEdit::is_connection_lines_antialiased);
  1847. ClassDB::bind_method(D_METHOD("set_minimap_size", "size"), &GraphEdit::set_minimap_size);
  1848. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1849. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "opacity"), &GraphEdit::set_minimap_opacity);
  1850. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1851. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1852. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1853. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1854. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1855. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  1856. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1857. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1858. ClassDB::bind_method(D_METHOD("arrange_nodes"), &GraphEdit::arrange_nodes);
  1859. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1860. BIND_VMETHOD(MethodInfo(Variant::PACKED_VECTOR2_ARRAY, "_get_connection_line", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  1861. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1862. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  1863. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  1864. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1865. ADD_GROUP("Connection Lines", "connection_lines");
  1866. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "connection_lines_thickness"), "set_connection_lines_thickness", "get_connection_lines_thickness");
  1867. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_lines_antialiased"), "set_connection_lines_antialiased", "is_connection_lines_antialiased");
  1868. ADD_GROUP("Zoom", "");
  1869. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom"), "set_zoom", "get_zoom");
  1870. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_min"), "set_zoom_min", "get_zoom_min");
  1871. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_max"), "set_zoom_max", "get_zoom_max");
  1872. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "zoom_step"), "set_zoom_step", "get_zoom_step");
  1873. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  1874. ADD_GROUP("Minimap", "minimap");
  1875. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  1876. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size"), "set_minimap_size", "get_minimap_size");
  1877. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  1878. 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")));
  1879. 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")));
  1880. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1881. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1882. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1883. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1884. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1885. ADD_SIGNAL(MethodInfo("node_deselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1886. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING_NAME, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1887. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING_NAME, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1888. ADD_SIGNAL(MethodInfo("delete_nodes_request"));
  1889. ADD_SIGNAL(MethodInfo("begin_node_move"));
  1890. ADD_SIGNAL(MethodInfo("end_node_move"));
  1891. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  1892. }
  1893. GraphEdit::GraphEdit() {
  1894. set_focus_mode(FOCUS_ALL);
  1895. // Allow dezooming 8 times from the default zoom level.
  1896. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  1897. // but this is still useful for previewing and navigation.
  1898. zoom_min = (1 / Math::pow(zoom_step, 8));
  1899. // Allow zooming 4 times from the default zoom level.
  1900. zoom_max = (1 * Math::pow(zoom_step, 4));
  1901. top_layer = memnew(GraphEditFilter(this));
  1902. add_child(top_layer);
  1903. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1904. top_layer->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  1905. top_layer->connect("draw", callable_mp(this, &GraphEdit::_top_layer_draw));
  1906. top_layer->connect("gui_input", callable_mp(this, &GraphEdit::_top_layer_input));
  1907. connections_layer = memnew(Control);
  1908. add_child(connections_layer);
  1909. connections_layer->connect("draw", callable_mp(this, &GraphEdit::_connections_layer_draw));
  1910. connections_layer->set_name("CLAYER");
  1911. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1912. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1913. h_scroll = memnew(HScrollBar);
  1914. h_scroll->set_name("_h_scroll");
  1915. top_layer->add_child(h_scroll);
  1916. v_scroll = memnew(VScrollBar);
  1917. v_scroll->set_name("_v_scroll");
  1918. top_layer->add_child(v_scroll);
  1919. //set large minmax so it can scroll even if not resized yet
  1920. h_scroll->set_min(-10000);
  1921. h_scroll->set_max(10000);
  1922. v_scroll->set_min(-10000);
  1923. v_scroll->set_max(10000);
  1924. h_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1925. v_scroll->connect("value_changed", callable_mp(this, &GraphEdit::_scroll_moved));
  1926. zoom_hb = memnew(HBoxContainer);
  1927. top_layer->add_child(zoom_hb);
  1928. zoom_hb->set_position(Vector2(10, 10));
  1929. zoom_label = memnew(Label);
  1930. zoom_hb->add_child(zoom_label);
  1931. zoom_label->set_visible(false);
  1932. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1933. zoom_label->set_align(Label::ALIGN_CENTER);
  1934. #ifdef TOOLS_ENABLED
  1935. zoom_label->set_custom_minimum_size(Size2(48, 0) * EDSCALE);
  1936. #else
  1937. zoom_label->set_custom_minimum_size(Size2(48, 0));
  1938. #endif
  1939. _update_zoom_label();
  1940. zoom_minus = memnew(Button);
  1941. zoom_minus->set_flat(true);
  1942. zoom_hb->add_child(zoom_minus);
  1943. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1944. zoom_minus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_minus));
  1945. zoom_minus->set_focus_mode(FOCUS_NONE);
  1946. zoom_reset = memnew(Button);
  1947. zoom_reset->set_flat(true);
  1948. zoom_hb->add_child(zoom_reset);
  1949. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1950. zoom_reset->connect("pressed", callable_mp(this, &GraphEdit::_zoom_reset));
  1951. zoom_reset->set_focus_mode(FOCUS_NONE);
  1952. zoom_plus = memnew(Button);
  1953. zoom_plus->set_flat(true);
  1954. zoom_hb->add_child(zoom_plus);
  1955. zoom_plus->set_tooltip(RTR("Zoom In"));
  1956. zoom_plus->connect("pressed", callable_mp(this, &GraphEdit::_zoom_plus));
  1957. zoom_plus->set_focus_mode(FOCUS_NONE);
  1958. snap_button = memnew(Button);
  1959. snap_button->set_flat(true);
  1960. snap_button->set_toggle_mode(true);
  1961. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1962. snap_button->connect("pressed", callable_mp(this, &GraphEdit::_snap_toggled));
  1963. snap_button->set_pressed(true);
  1964. snap_button->set_focus_mode(FOCUS_NONE);
  1965. zoom_hb->add_child(snap_button);
  1966. snap_amount = memnew(SpinBox);
  1967. snap_amount->set_min(5);
  1968. snap_amount->set_max(100);
  1969. snap_amount->set_step(1);
  1970. snap_amount->set_value(20);
  1971. snap_amount->connect("value_changed", callable_mp(this, &GraphEdit::_snap_value_changed));
  1972. zoom_hb->add_child(snap_amount);
  1973. minimap_button = memnew(Button);
  1974. minimap_button->set_flat(true);
  1975. minimap_button->set_toggle_mode(true);
  1976. minimap_button->set_tooltip(RTR("Enable grid minimap."));
  1977. minimap_button->connect("pressed", callable_mp(this, &GraphEdit::_minimap_toggled));
  1978. minimap_button->set_pressed(true);
  1979. minimap_button->set_focus_mode(FOCUS_NONE);
  1980. zoom_hb->add_child(minimap_button);
  1981. layout_button = memnew(Button);
  1982. layout_button->set_flat(true);
  1983. zoom_hb->add_child(layout_button);
  1984. layout_button->set_tooltip(RTR("Arrange nodes."));
  1985. layout_button->connect("pressed", callable_mp(this, &GraphEdit::arrange_nodes));
  1986. layout_button->set_focus_mode(FOCUS_NONE);
  1987. Vector2 minimap_size = Vector2(240, 160);
  1988. float minimap_opacity = 0.65;
  1989. minimap = memnew(GraphEditMinimap(this));
  1990. top_layer->add_child(minimap);
  1991. minimap->set_name("_minimap");
  1992. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  1993. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  1994. minimap->set_custom_minimum_size(Vector2(50, 50));
  1995. minimap->set_size(minimap_size);
  1996. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1997. minimap->set_offset(Side::SIDE_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1998. minimap->set_offset(Side::SIDE_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1999. minimap->set_offset(Side::SIDE_RIGHT, -MINIMAP_OFFSET);
  2000. minimap->set_offset(Side::SIDE_BOTTOM, -MINIMAP_OFFSET);
  2001. minimap->connect("draw", callable_mp(this, &GraphEdit::_minimap_draw));
  2002. set_clip_contents(true);
  2003. }