graph_edit.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  1. /**************************************************************************/
  2. /* graph_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "graph_edit.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "scene/gui/box_container.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "editor/editor_scale.h"
  37. #endif
  38. constexpr int MINIMAP_OFFSET = 12;
  39. constexpr int MINIMAP_PADDING = 5;
  40. bool GraphEditFilter::has_point(const Point2 &p_point) const {
  41. return ge->_filter_input(p_point);
  42. }
  43. GraphEditFilter::GraphEditFilter(GraphEdit *p_edit) {
  44. ge = p_edit;
  45. }
  46. void GraphEditMinimap::_bind_methods() {
  47. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEditMinimap::_gui_input);
  48. }
  49. GraphEditMinimap::GraphEditMinimap(GraphEdit *p_edit) {
  50. ge = p_edit;
  51. graph_proportions = Vector2(1, 1);
  52. graph_padding = Vector2(0, 0);
  53. camera_position = Vector2(100, 50);
  54. camera_size = Vector2(200, 200);
  55. minimap_padding = Vector2(MINIMAP_PADDING, MINIMAP_PADDING);
  56. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  57. is_pressing = false;
  58. is_resizing = false;
  59. }
  60. void GraphEditMinimap::update_minimap() {
  61. Vector2 graph_offset = _get_graph_offset();
  62. Vector2 graph_size = _get_graph_size();
  63. camera_position = ge->get_scroll_ofs() - graph_offset;
  64. camera_size = ge->get_size();
  65. Vector2 render_size = _get_render_size();
  66. float target_ratio = render_size.x / render_size.y;
  67. float graph_ratio = graph_size.x / graph_size.y;
  68. graph_proportions = graph_size;
  69. graph_padding = Vector2(0, 0);
  70. if (graph_ratio > target_ratio) {
  71. graph_proportions.x = graph_size.x;
  72. graph_proportions.y = graph_size.x / target_ratio;
  73. graph_padding.y = Math::abs(graph_size.y - graph_proportions.y) / 2;
  74. } else {
  75. graph_proportions.x = graph_size.y * target_ratio;
  76. graph_proportions.y = graph_size.y;
  77. graph_padding.x = Math::abs(graph_size.x - graph_proportions.x) / 2;
  78. }
  79. // This centers minimap inside the minimap rectangle.
  80. minimap_offset = minimap_padding + _convert_from_graph_position(graph_padding);
  81. }
  82. Rect2 GraphEditMinimap::get_camera_rect() {
  83. Vector2 camera_center = _convert_from_graph_position(camera_position + camera_size / 2) + minimap_offset;
  84. Vector2 camera_viewport = _convert_from_graph_position(camera_size);
  85. Vector2 camera_position = (camera_center - camera_viewport / 2);
  86. return Rect2(camera_position, camera_viewport);
  87. }
  88. Vector2 GraphEditMinimap::_get_render_size() {
  89. if (!is_inside_tree()) {
  90. return Vector2(0, 0);
  91. }
  92. return get_size() - 2 * minimap_padding;
  93. }
  94. Vector2 GraphEditMinimap::_get_graph_offset() {
  95. return Vector2(ge->h_scroll->get_min(), ge->v_scroll->get_min());
  96. }
  97. Vector2 GraphEditMinimap::_get_graph_size() {
  98. 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());
  99. if (graph_size.x == 0) {
  100. graph_size.x = 1;
  101. }
  102. if (graph_size.y == 0) {
  103. graph_size.y = 1;
  104. }
  105. return graph_size;
  106. }
  107. Vector2 GraphEditMinimap::_convert_from_graph_position(const Vector2 &p_position) {
  108. Vector2 map_position = Vector2(0, 0);
  109. Vector2 render_size = _get_render_size();
  110. map_position.x = p_position.x * render_size.x / graph_proportions.x;
  111. map_position.y = p_position.y * render_size.y / graph_proportions.y;
  112. return map_position;
  113. }
  114. Vector2 GraphEditMinimap::_convert_to_graph_position(const Vector2 &p_position) {
  115. Vector2 graph_position = Vector2(0, 0);
  116. Vector2 render_size = _get_render_size();
  117. graph_position.x = p_position.x * graph_proportions.x / render_size.x;
  118. graph_position.y = p_position.y * graph_proportions.y / render_size.y;
  119. return graph_position;
  120. }
  121. void GraphEditMinimap::_gui_input(const Ref<InputEvent> &p_ev) {
  122. if (!ge->is_minimap_enabled()) {
  123. return;
  124. }
  125. Ref<InputEventMouseButton> mb = p_ev;
  126. Ref<InputEventMouseMotion> mm = p_ev;
  127. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  128. if (mb->is_pressed()) {
  129. is_pressing = true;
  130. Ref<Texture> resizer = get_icon("resizer");
  131. Rect2 resizer_hitbox = Rect2(Point2(), resizer->get_size());
  132. if (resizer_hitbox.has_point(mb->get_position())) {
  133. is_resizing = true;
  134. } else {
  135. Vector2 click_position = _convert_to_graph_position(mb->get_position() - minimap_padding) - graph_padding;
  136. _adjust_graph_scroll(click_position);
  137. }
  138. } else {
  139. is_pressing = false;
  140. is_resizing = false;
  141. }
  142. accept_event();
  143. } else if (mm.is_valid() && is_pressing) {
  144. if (is_resizing) {
  145. // Prevent setting minimap wider than GraphEdit
  146. Vector2 new_minimap_size;
  147. new_minimap_size.x = MIN(get_size().x - mm->get_relative().x, ge->get_size().x - 2.0 * minimap_padding.x);
  148. new_minimap_size.y = MIN(get_size().y - mm->get_relative().y, ge->get_size().y - 2.0 * minimap_padding.y);
  149. ge->set_minimap_size(new_minimap_size);
  150. update();
  151. } else {
  152. Vector2 click_position = _convert_to_graph_position(mm->get_position() - minimap_padding) - graph_padding;
  153. _adjust_graph_scroll(click_position);
  154. }
  155. accept_event();
  156. }
  157. }
  158. void GraphEditMinimap::_adjust_graph_scroll(const Vector2 &p_offset) {
  159. Vector2 graph_offset = _get_graph_offset();
  160. ge->set_scroll_ofs(p_offset + graph_offset - camera_size / 2);
  161. }
  162. Error GraphEdit::connect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  163. if (is_node_connected(p_from, p_from_port, p_to, p_to_port)) {
  164. return OK;
  165. }
  166. Connection c;
  167. c.from = p_from;
  168. c.from_port = p_from_port;
  169. c.to = p_to;
  170. c.to_port = p_to_port;
  171. c.activity = 0;
  172. connections.push_back(c);
  173. top_layer->update();
  174. minimap->update();
  175. update();
  176. connections_layer->update();
  177. return OK;
  178. }
  179. bool GraphEdit::is_node_connected(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  180. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  181. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. void GraphEdit::disconnect_node(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port) {
  188. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  189. if (E->get().from == p_from && E->get().from_port == p_from_port && E->get().to == p_to && E->get().to_port == p_to_port) {
  190. connections.erase(E);
  191. top_layer->update();
  192. minimap->update();
  193. update();
  194. connections_layer->update();
  195. return;
  196. }
  197. }
  198. }
  199. bool GraphEdit::clips_input() const {
  200. return true;
  201. }
  202. void GraphEdit::get_connection_list(List<Connection> *r_connections) const {
  203. *r_connections = connections;
  204. }
  205. void GraphEdit::set_scroll_ofs(const Vector2 &p_ofs) {
  206. setting_scroll_ofs = true;
  207. h_scroll->set_value(p_ofs.x);
  208. v_scroll->set_value(p_ofs.y);
  209. _update_scroll();
  210. setting_scroll_ofs = false;
  211. }
  212. Vector2 GraphEdit::get_scroll_ofs() const {
  213. return Vector2(h_scroll->get_value(), v_scroll->get_value());
  214. }
  215. void GraphEdit::_scroll_moved(double) {
  216. if (!awaiting_scroll_offset_update) {
  217. call_deferred("_update_scroll_offset");
  218. awaiting_scroll_offset_update = true;
  219. }
  220. top_layer->update();
  221. minimap->update();
  222. update();
  223. if (!setting_scroll_ofs) { //in godot, signals on change value are avoided as a convention
  224. emit_signal("scroll_offset_changed", get_scroll_ofs());
  225. }
  226. }
  227. void GraphEdit::_update_scroll_offset() {
  228. set_block_minimum_size_adjust(true);
  229. for (int i = 0; i < get_child_count(); i++) {
  230. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  231. if (!gn) {
  232. continue;
  233. }
  234. Point2 pos = gn->get_offset() * zoom;
  235. pos -= Point2(h_scroll->get_value(), v_scroll->get_value());
  236. gn->set_position(pos);
  237. if (gn->get_scale() != Vector2(zoom, zoom)) {
  238. gn->set_scale(Vector2(zoom, zoom));
  239. }
  240. }
  241. connections_layer->set_position(-Point2(h_scroll->get_value(), v_scroll->get_value()));
  242. set_block_minimum_size_adjust(false);
  243. awaiting_scroll_offset_update = false;
  244. }
  245. void GraphEdit::_update_scroll() {
  246. if (updating) {
  247. return;
  248. }
  249. updating = true;
  250. set_block_minimum_size_adjust(true);
  251. Rect2 screen;
  252. for (int i = 0; i < get_child_count(); i++) {
  253. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  254. if (!gn) {
  255. continue;
  256. }
  257. Rect2 r;
  258. r.position = gn->get_offset() * zoom;
  259. r.size = gn->get_size() * zoom;
  260. screen = screen.merge(r);
  261. }
  262. screen.position -= get_size();
  263. screen.size += get_size() * 2.0;
  264. h_scroll->set_min(screen.position.x);
  265. h_scroll->set_max(screen.position.x + screen.size.x);
  266. h_scroll->set_page(get_size().x);
  267. if (h_scroll->get_max() - h_scroll->get_min() <= h_scroll->get_page()) {
  268. h_scroll->hide();
  269. } else {
  270. h_scroll->show();
  271. }
  272. v_scroll->set_min(screen.position.y);
  273. v_scroll->set_max(screen.position.y + screen.size.y);
  274. v_scroll->set_page(get_size().y);
  275. if (v_scroll->get_max() - v_scroll->get_min() <= v_scroll->get_page()) {
  276. v_scroll->hide();
  277. } else {
  278. v_scroll->show();
  279. }
  280. Size2 hmin = h_scroll->get_combined_minimum_size();
  281. Size2 vmin = v_scroll->get_combined_minimum_size();
  282. // Avoid scrollbar overlapping.
  283. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, v_scroll->is_visible() ? -vmin.width : 0);
  284. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, h_scroll->is_visible() ? -hmin.height : 0);
  285. set_block_minimum_size_adjust(false);
  286. if (!awaiting_scroll_offset_update) {
  287. call_deferred("_update_scroll_offset");
  288. awaiting_scroll_offset_update = true;
  289. }
  290. updating = false;
  291. }
  292. void GraphEdit::_graph_node_raised(Node *p_gn) {
  293. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  294. ERR_FAIL_COND(!gn);
  295. if (gn->is_comment()) {
  296. move_child(gn, 0);
  297. } else {
  298. gn->raise();
  299. }
  300. int first_not_comment = 0;
  301. for (int i = 0; i < get_child_count(); i++) {
  302. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  303. if (gn2 && !gn2->is_comment()) {
  304. first_not_comment = i;
  305. break;
  306. }
  307. }
  308. move_child(connections_layer, first_not_comment);
  309. top_layer->raise();
  310. emit_signal("node_selected", p_gn);
  311. }
  312. void GraphEdit::_graph_node_moved(Node *p_gn) {
  313. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  314. ERR_FAIL_COND(!gn);
  315. top_layer->update();
  316. minimap->update();
  317. update();
  318. connections_layer->update();
  319. }
  320. void GraphEdit::_graph_node_slot_updated(int p_index, Node *p_gn) {
  321. GraphNode *gn = Object::cast_to<GraphNode>(p_gn);
  322. ERR_FAIL_COND(!gn);
  323. top_layer->update();
  324. minimap->update();
  325. update();
  326. connections_layer->update();
  327. }
  328. void GraphEdit::add_child_notify(Node *p_child) {
  329. Control::add_child_notify(p_child);
  330. top_layer->call_deferred("raise"); // Top layer always on top!
  331. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  332. if (gn) {
  333. gn->set_scale(Vector2(zoom, zoom));
  334. gn->connect("offset_changed", this, "_graph_node_moved", varray(gn));
  335. gn->connect("slot_updated", this, "_graph_node_slot_updated", varray(gn));
  336. gn->connect("raise_request", this, "_graph_node_raised", varray(gn));
  337. gn->connect("item_rect_changed", connections_layer, "update");
  338. gn->connect("item_rect_changed", minimap, "update");
  339. _graph_node_moved(gn);
  340. gn->set_mouse_filter(MOUSE_FILTER_PASS);
  341. }
  342. }
  343. void GraphEdit::remove_child_notify(Node *p_child) {
  344. Control::remove_child_notify(p_child);
  345. if (p_child == top_layer) {
  346. top_layer = nullptr;
  347. minimap = nullptr;
  348. } else if (p_child == connections_layer) {
  349. connections_layer = nullptr;
  350. }
  351. if (top_layer != nullptr && is_inside_tree()) {
  352. top_layer->call_deferred("raise"); // Top layer always on top!
  353. }
  354. GraphNode *gn = Object::cast_to<GraphNode>(p_child);
  355. if (gn) {
  356. gn->disconnect("offset_changed", this, "_graph_node_moved");
  357. gn->disconnect("slot_updated", this, "_graph_node_slot_updated");
  358. gn->disconnect("raise_request", this, "_graph_node_raised");
  359. // In case of the whole GraphEdit being destroyed these references can already be freed.
  360. if (connections_layer != nullptr && connections_layer->is_inside_tree()) {
  361. gn->disconnect("item_rect_changed", connections_layer, "update");
  362. }
  363. if (minimap != nullptr && minimap->is_inside_tree()) {
  364. gn->disconnect("item_rect_changed", minimap, "update");
  365. }
  366. }
  367. }
  368. void GraphEdit::_notification(int p_what) {
  369. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  370. port_grab_distance_horizontal = get_constant("port_grab_distance_horizontal");
  371. port_grab_distance_vertical = get_constant("port_grab_distance_vertical");
  372. zoom_minus->set_icon(get_icon("minus"));
  373. zoom_reset->set_icon(get_icon("reset"));
  374. zoom_plus->set_icon(get_icon("more"));
  375. snap_button->set_icon(get_icon("snap"));
  376. minimap_button->set_icon(get_icon("minimap"));
  377. }
  378. if (p_what == NOTIFICATION_READY) {
  379. Size2 hmin = h_scroll->get_combined_minimum_size();
  380. Size2 vmin = v_scroll->get_combined_minimum_size();
  381. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  382. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  383. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  384. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  385. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  386. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  387. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  388. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  389. }
  390. if (p_what == NOTIFICATION_DRAW) {
  391. draw_style_box(get_stylebox("bg"), Rect2(Point2(), get_size()));
  392. if (is_using_snap()) {
  393. //draw grid
  394. int snap = get_snap();
  395. Vector2 offset = get_scroll_ofs() / zoom;
  396. Size2 size = get_size() / zoom;
  397. Point2i from = (offset / float(snap)).floor();
  398. Point2i len = (size / float(snap)).floor() + Vector2(1, 1);
  399. Color grid_minor = get_color("grid_minor");
  400. Color grid_major = get_color("grid_major");
  401. for (int i = from.x; i < from.x + len.x; i++) {
  402. Color color;
  403. if (ABS(i) % 10 == 0) {
  404. color = grid_major;
  405. } else {
  406. color = grid_minor;
  407. }
  408. float base_ofs = i * snap * zoom - offset.x * zoom;
  409. draw_line(Vector2(base_ofs, 0), Vector2(base_ofs, get_size().height), color);
  410. }
  411. for (int i = from.y; i < from.y + len.y; i++) {
  412. Color color;
  413. if (ABS(i) % 10 == 0) {
  414. color = grid_major;
  415. } else {
  416. color = grid_minor;
  417. }
  418. float base_ofs = i * snap * zoom - offset.y * zoom;
  419. draw_line(Vector2(0, base_ofs), Vector2(get_size().width, base_ofs), color);
  420. }
  421. }
  422. }
  423. if (p_what == NOTIFICATION_RESIZED) {
  424. _update_scroll();
  425. top_layer->update();
  426. minimap->update();
  427. }
  428. }
  429. bool GraphEdit::_filter_input(const Point2 &p_point) {
  430. Ref<Texture> port = get_icon("port", "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() == BUTTON_LEFT && mb->is_pressed()) {
  455. Ref<Texture> port = get_icon("port", "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 (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  470. if (E->get().from == gn->get_name() && E->get().from_port == j) {
  471. Node *to = get_node(String(E->get().to));
  472. if (Object::cast_to<GraphNode>(to)) {
  473. connecting_from = E->get().to;
  474. connecting_index = E->get().to_port;
  475. connecting_out = false;
  476. connecting_type = Object::cast_to<GraphNode>(to)->get_connection_input_type(E->get().to_port);
  477. connecting_color = Object::cast_to<GraphNode>(to)->get_connection_input_color(E->get().to_port);
  478. connecting_target = false;
  479. connecting_to = pos;
  480. just_disconnected = true;
  481. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().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 (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  509. if (E->get().to == gn->get_name() && E->get().to_port == j) {
  510. Node *fr = get_node(String(E->get().from));
  511. if (Object::cast_to<GraphNode>(fr)) {
  512. connecting_from = E->get().from;
  513. connecting_index = E->get().from_port;
  514. connecting_out = true;
  515. connecting_type = Object::cast_to<GraphNode>(fr)->get_connection_output_type(E->get().from_port);
  516. connecting_color = Object::cast_to<GraphNode>(fr)->get_connection_output_color(E->get().from_port);
  517. connecting_target = false;
  518. connecting_to = pos;
  519. just_disconnected = true;
  520. emit_signal("disconnection_request", E->get().from, E->get().from_port, E->get().to, E->get().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<Texture> port = get_icon("port", "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(connecting_type, 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(connecting_type, 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() == 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("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("connection_from_empty", from, from_slot, ofs);
  606. } else {
  607. emit_signal("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_toplevel() || !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. template <class Vector2>
  684. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, Vector2 start, Vector2 control_1, Vector2 control_2, Vector2 end) {
  685. /* Formula from Wikipedia article on Bezier curves. */
  686. real_t omt = (1.0 - t);
  687. real_t omt2 = omt * omt;
  688. real_t omt3 = omt2 * omt;
  689. real_t t2 = t * t;
  690. real_t t3 = t2 * t;
  691. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  692. }
  693. void GraphEdit::_bake_segment2d(Vector<Vector2> &points, Vector<Color> &colors, float p_begin, float p_end, const Vector2 &p_a, const Vector2 &p_out, const Vector2 &p_b, const Vector2 &p_in, int p_depth, int p_min_depth, int p_max_depth, float p_tol, const Color &p_color, const Color &p_to_color, int &lines) const {
  694. float mp = p_begin + (p_end - p_begin) * 0.5;
  695. Vector2 beg = _bezier_interp(p_begin, p_a, p_a + p_out, p_b + p_in, p_b);
  696. Vector2 mid = _bezier_interp(mp, p_a, p_a + p_out, p_b + p_in, p_b);
  697. Vector2 end = _bezier_interp(p_end, p_a, p_a + p_out, p_b + p_in, p_b);
  698. Vector2 na = (mid - beg).normalized();
  699. Vector2 nb = (end - mid).normalized();
  700. float dp = Math::rad2deg(Math::acos(na.dot(nb)));
  701. if (p_depth >= p_min_depth && (dp < p_tol || p_depth >= p_max_depth)) {
  702. points.push_back((beg + end) * 0.5);
  703. colors.push_back(p_color.linear_interpolate(p_to_color, mp));
  704. lines++;
  705. } else {
  706. _bake_segment2d(points, colors, p_begin, mp, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  707. _bake_segment2d(points, colors, mp, p_end, p_a, p_out, p_b, p_in, p_depth + 1, p_min_depth, p_max_depth, p_tol, p_color, p_to_color, lines);
  708. }
  709. }
  710. void GraphEdit::_draw_cos_line(CanvasItem *p_where, const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, const Color &p_to_color, float p_width, float p_bezier_ratio) {
  711. //cubic bezier code
  712. float diff = p_to.x - p_from.x;
  713. float cp_offset;
  714. int cp_len = get_constant("bezier_len_pos") * p_bezier_ratio;
  715. int cp_neg_len = get_constant("bezier_len_neg") * p_bezier_ratio;
  716. if (diff > 0) {
  717. cp_offset = MIN(cp_len, diff * 0.5);
  718. } else {
  719. cp_offset = MAX(MIN(cp_len - diff, cp_neg_len), -diff * 0.5);
  720. }
  721. Vector2 c1 = Vector2(cp_offset * zoom, 0);
  722. Vector2 c2 = Vector2(-cp_offset * zoom, 0);
  723. int lines = 0;
  724. Vector<Point2> points;
  725. Vector<Color> colors;
  726. points.push_back(p_from);
  727. colors.push_back(p_color);
  728. _bake_segment2d(points, colors, 0, 1, p_from, c1, p_to, c2, 0, 3, 9, 3, p_color, p_to_color, lines);
  729. points.push_back(p_to);
  730. colors.push_back(p_to_color);
  731. #ifdef TOOLS_ENABLED
  732. p_where->draw_polyline_colors(points, colors, Math::floor(p_width * EDSCALE), true);
  733. #else
  734. p_where->draw_polyline_colors(points, colors, p_width, true);
  735. #endif
  736. }
  737. void GraphEdit::_connections_layer_draw() {
  738. Color activity_color = get_color("activity");
  739. //draw connections
  740. List<List<Connection>::Element *> to_erase;
  741. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  742. NodePath fromnp(E->get().from);
  743. Node *from = get_node(fromnp);
  744. if (!from) {
  745. to_erase.push_back(E);
  746. continue;
  747. }
  748. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  749. if (!gfrom) {
  750. to_erase.push_back(E);
  751. continue;
  752. }
  753. NodePath tonp(E->get().to);
  754. Node *to = get_node(tonp);
  755. if (!to) {
  756. to_erase.push_back(E);
  757. continue;
  758. }
  759. GraphNode *gto = Object::cast_to<GraphNode>(to);
  760. if (!gto) {
  761. to_erase.push_back(E);
  762. continue;
  763. }
  764. Vector2 frompos = gfrom->get_connection_output_position(E->get().from_port) + gfrom->get_offset() * zoom;
  765. Color color = gfrom->get_connection_output_color(E->get().from_port);
  766. Vector2 topos = gto->get_connection_input_position(E->get().to_port) + gto->get_offset() * zoom;
  767. Color tocolor = gto->get_connection_input_color(E->get().to_port);
  768. if (E->get().activity > 0) {
  769. color = color.linear_interpolate(activity_color, E->get().activity);
  770. tocolor = tocolor.linear_interpolate(activity_color, E->get().activity);
  771. }
  772. _draw_cos_line(connections_layer, frompos, topos, color, tocolor);
  773. }
  774. while (to_erase.size()) {
  775. connections.erase(to_erase.front()->get());
  776. to_erase.pop_front();
  777. }
  778. }
  779. void GraphEdit::_top_layer_draw() {
  780. _update_scroll();
  781. if (connecting) {
  782. Node *fromn = get_node(connecting_from);
  783. ERR_FAIL_COND(!fromn);
  784. GraphNode *from = Object::cast_to<GraphNode>(fromn);
  785. ERR_FAIL_COND(!from);
  786. Vector2 pos;
  787. if (connecting_out) {
  788. pos = from->get_connection_output_position(connecting_index);
  789. } else {
  790. pos = from->get_connection_input_position(connecting_index);
  791. }
  792. pos += from->get_position();
  793. Vector2 topos;
  794. topos = connecting_to;
  795. Color col = connecting_color;
  796. if (connecting_target) {
  797. col.r += 0.4;
  798. col.g += 0.4;
  799. col.b += 0.4;
  800. }
  801. if (!connecting_out) {
  802. SWAP(pos, topos);
  803. }
  804. _draw_cos_line(top_layer, pos, topos, col, col);
  805. }
  806. if (box_selecting) {
  807. top_layer->draw_rect(box_selecting_rect, get_color("selection_fill"));
  808. top_layer->draw_rect(box_selecting_rect, get_color("selection_stroke"), false);
  809. }
  810. }
  811. void GraphEdit::_minimap_draw() {
  812. if (!is_minimap_enabled()) {
  813. return;
  814. }
  815. minimap->update_minimap();
  816. // Draw the minimap background.
  817. Rect2 minimap_rect = Rect2(Point2(), minimap->get_size());
  818. minimap->draw_style_box(minimap->get_stylebox("bg"), minimap_rect);
  819. Vector2 graph_offset = minimap->_get_graph_offset();
  820. Vector2 minimap_offset = minimap->minimap_offset;
  821. // Draw comment graph nodes.
  822. for (int i = get_child_count() - 1; i >= 0; i--) {
  823. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  824. if (!gn || !gn->is_comment()) {
  825. continue;
  826. }
  827. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_offset() * zoom - graph_offset) + minimap_offset;
  828. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  829. Rect2 node_rect = Rect2(node_position, node_size);
  830. Ref<StyleBoxFlat> sb_minimap = minimap->get_stylebox("node")->duplicate();
  831. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  832. Ref<StyleBoxFlat> sbf = gn->get_stylebox(gn->is_selected() ? "commentfocus" : "comment");
  833. if (sbf.is_valid()) {
  834. Color node_color = sbf->get_bg_color();
  835. sb_minimap->set_bg_color(node_color);
  836. }
  837. minimap->draw_style_box(sb_minimap, node_rect);
  838. }
  839. // Draw regular graph nodes.
  840. for (int i = get_child_count() - 1; i >= 0; i--) {
  841. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  842. if (!gn || gn->is_comment()) {
  843. continue;
  844. }
  845. Vector2 node_position = minimap->_convert_from_graph_position(gn->get_offset() * zoom - graph_offset) + minimap_offset;
  846. Vector2 node_size = minimap->_convert_from_graph_position(gn->get_size() * zoom);
  847. Rect2 node_rect = Rect2(node_position, node_size);
  848. Ref<StyleBoxFlat> sb_minimap = minimap->get_stylebox("node")->duplicate();
  849. // Override default values with colors provided by the GraphNode's stylebox, if possible.
  850. Ref<StyleBoxFlat> sbf = gn->get_stylebox(gn->is_selected() ? "selectedframe" : "frame");
  851. if (sbf.is_valid()) {
  852. Color node_color = sbf->get_border_color();
  853. sb_minimap->set_bg_color(node_color);
  854. }
  855. minimap->draw_style_box(sb_minimap, node_rect);
  856. }
  857. // Draw node connections.
  858. Color activity_color = get_color("activity");
  859. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  860. NodePath fromnp(E->get().from);
  861. Node *from = get_node(fromnp);
  862. if (!from) {
  863. continue;
  864. }
  865. GraphNode *gfrom = Object::cast_to<GraphNode>(from);
  866. if (!gfrom) {
  867. continue;
  868. }
  869. NodePath tonp(E->get().to);
  870. Node *to = get_node(tonp);
  871. if (!to) {
  872. continue;
  873. }
  874. GraphNode *gto = Object::cast_to<GraphNode>(to);
  875. if (!gto) {
  876. continue;
  877. }
  878. Vector2 from_slot_position = gfrom->get_offset() * zoom + gfrom->get_connection_output_position(E->get().from_port);
  879. Vector2 from_position = minimap->_convert_from_graph_position(from_slot_position - graph_offset) + minimap_offset;
  880. Color from_color = gfrom->get_connection_output_color(E->get().from_port);
  881. Vector2 to_slot_position = gto->get_offset() * zoom + gto->get_connection_input_position(E->get().to_port);
  882. Vector2 to_position = minimap->_convert_from_graph_position(to_slot_position - graph_offset) + minimap_offset;
  883. Color to_color = gto->get_connection_input_color(E->get().to_port);
  884. if (E->get().activity > 0) {
  885. from_color = from_color.linear_interpolate(activity_color, E->get().activity);
  886. to_color = to_color.linear_interpolate(activity_color, E->get().activity);
  887. }
  888. _draw_cos_line(minimap, from_position, to_position, from_color, to_color, 1.0, 0.5);
  889. }
  890. // Draw the "camera" viewport.
  891. Rect2 camera_rect = minimap->get_camera_rect();
  892. minimap->draw_style_box(minimap->get_stylebox("camera"), camera_rect);
  893. // Draw the resizer control.
  894. Ref<Texture> resizer = minimap->get_icon("resizer");
  895. Color resizer_color = minimap->get_color("resizer_color");
  896. minimap->draw_texture(resizer, Point2(), resizer_color);
  897. }
  898. void GraphEdit::set_selected(Node *p_child) {
  899. for (int i = get_child_count() - 1; i >= 0; i--) {
  900. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  901. if (!gn) {
  902. continue;
  903. }
  904. gn->set_selected(gn == p_child);
  905. }
  906. }
  907. void GraphEdit::_gui_input(const Ref<InputEvent> &p_ev) {
  908. Ref<InputEventMouseMotion> mm = p_ev;
  909. if (mm.is_valid() && (mm->get_button_mask() & BUTTON_MASK_MIDDLE || (mm->get_button_mask() & BUTTON_MASK_LEFT && Input::get_singleton()->is_key_pressed(KEY_SPACE)))) {
  910. Vector2i relative = Input::get_singleton()->warp_mouse_motion(mm, get_global_rect());
  911. h_scroll->set_value(h_scroll->get_value() - relative.x);
  912. v_scroll->set_value(v_scroll->get_value() - relative.y);
  913. }
  914. if (mm.is_valid() && dragging) {
  915. if (!moving_selection) {
  916. emit_signal("_begin_node_move");
  917. moving_selection = true;
  918. }
  919. just_selected = true;
  920. drag_accum += mm->get_relative();
  921. for (int i = get_child_count() - 1; i >= 0; i--) {
  922. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  923. if (gn && gn->is_selected()) {
  924. Vector2 pos = (gn->get_drag_from() * zoom + drag_accum) / zoom;
  925. // Snapping can be toggled temporarily by holding down Ctrl.
  926. // This is done here as to not toggle the grid when holding down Ctrl.
  927. if (is_using_snap() ^ Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  928. const int snap = get_snap();
  929. pos = pos.snapped(Vector2(snap, snap));
  930. }
  931. gn->set_offset(pos);
  932. }
  933. }
  934. }
  935. if (mm.is_valid() && box_selecting) {
  936. box_selecting_to = mm->get_position();
  937. box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
  938. MIN(box_selecting_from.y, box_selecting_to.y),
  939. ABS(box_selecting_from.x - box_selecting_to.x),
  940. ABS(box_selecting_from.y - box_selecting_to.y));
  941. for (int i = get_child_count() - 1; i >= 0; i--) {
  942. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  943. if (!gn) {
  944. continue;
  945. }
  946. Rect2 r = gn->get_rect();
  947. r.size *= zoom;
  948. bool in_box = r.intersects(box_selecting_rect);
  949. if (in_box) {
  950. if (!gn->is_selected() && box_selection_mode_additive) {
  951. emit_signal("node_selected", gn);
  952. } else if (gn->is_selected() && !box_selection_mode_additive) {
  953. emit_signal("node_unselected", gn);
  954. }
  955. gn->set_selected(box_selection_mode_additive);
  956. } else {
  957. bool select = (previous_selected.find(gn) != nullptr);
  958. if (gn->is_selected() && !select) {
  959. emit_signal("node_unselected", gn);
  960. } else if (!gn->is_selected() && select) {
  961. emit_signal("node_selected", gn);
  962. }
  963. gn->set_selected(select);
  964. }
  965. }
  966. top_layer->update();
  967. minimap->update();
  968. }
  969. Ref<InputEventMouseButton> b = p_ev;
  970. if (b.is_valid()) {
  971. if (b->get_button_index() == BUTTON_RIGHT && b->is_pressed()) {
  972. if (box_selecting) {
  973. box_selecting = false;
  974. for (int i = get_child_count() - 1; i >= 0; i--) {
  975. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  976. if (!gn) {
  977. continue;
  978. }
  979. bool select = (previous_selected.find(gn) != nullptr);
  980. if (gn->is_selected() && !select) {
  981. emit_signal("node_unselected", gn);
  982. } else if (!gn->is_selected() && select) {
  983. emit_signal("node_selected", gn);
  984. }
  985. gn->set_selected(select);
  986. }
  987. top_layer->update();
  988. minimap->update();
  989. } else {
  990. if (connecting) {
  991. connecting = false;
  992. top_layer->update();
  993. minimap->update();
  994. } else {
  995. emit_signal("popup_request", b->get_global_position());
  996. }
  997. }
  998. }
  999. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && dragging) {
  1000. if (!just_selected && drag_accum == Vector2() && Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  1001. //deselect current node
  1002. for (int i = get_child_count() - 1; i >= 0; i--) {
  1003. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1004. if (gn) {
  1005. Rect2 r = gn->get_rect();
  1006. r.size *= zoom;
  1007. if (r.has_point(b->get_position())) {
  1008. emit_signal("node_unselected", gn);
  1009. gn->set_selected(false);
  1010. }
  1011. }
  1012. }
  1013. }
  1014. if (drag_accum != Vector2()) {
  1015. for (int i = get_child_count() - 1; i >= 0; i--) {
  1016. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1017. if (gn && gn->is_selected()) {
  1018. gn->set_drag(false);
  1019. }
  1020. }
  1021. }
  1022. if (moving_selection) {
  1023. emit_signal("_end_node_move");
  1024. moving_selection = false;
  1025. }
  1026. dragging = false;
  1027. top_layer->update();
  1028. minimap->update();
  1029. update();
  1030. connections_layer->update();
  1031. }
  1032. if (b->get_button_index() == BUTTON_LEFT && b->is_pressed()) {
  1033. GraphNode *gn = nullptr;
  1034. for (int i = get_child_count() - 1; i >= 0; i--) {
  1035. GraphNode *gn_selected = Object::cast_to<GraphNode>(get_child(i));
  1036. if (gn_selected) {
  1037. if (gn_selected->is_resizing()) {
  1038. continue;
  1039. }
  1040. if (gn_selected->has_point((b->get_position() - gn_selected->get_position()) / zoom)) {
  1041. gn = gn_selected;
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. if (gn) {
  1047. if (_filter_input(b->get_position())) {
  1048. return;
  1049. }
  1050. dragging = true;
  1051. drag_accum = Vector2();
  1052. just_selected = !gn->is_selected();
  1053. if (!gn->is_selected() && !Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  1054. for (int i = 0; i < get_child_count(); i++) {
  1055. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1056. if (o_gn) {
  1057. if (o_gn == gn) {
  1058. o_gn->set_selected(true);
  1059. } else {
  1060. if (o_gn->is_selected()) {
  1061. emit_signal("node_unselected", o_gn);
  1062. }
  1063. o_gn->set_selected(false);
  1064. }
  1065. }
  1066. }
  1067. }
  1068. gn->set_selected(true);
  1069. for (int i = 0; i < get_child_count(); i++) {
  1070. GraphNode *o_gn = Object::cast_to<GraphNode>(get_child(i));
  1071. if (!o_gn) {
  1072. continue;
  1073. }
  1074. if (o_gn->is_selected()) {
  1075. o_gn->set_drag(true);
  1076. }
  1077. }
  1078. } else {
  1079. if (_filter_input(b->get_position())) {
  1080. return;
  1081. }
  1082. if (Input::get_singleton()->is_key_pressed(KEY_SPACE)) {
  1083. return;
  1084. }
  1085. box_selecting = true;
  1086. box_selecting_from = b->get_position();
  1087. if (b->get_control()) {
  1088. box_selection_mode_additive = true;
  1089. previous_selected.clear();
  1090. for (int i = get_child_count() - 1; i >= 0; i--) {
  1091. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1092. if (!gn2 || !gn2->is_selected()) {
  1093. continue;
  1094. }
  1095. previous_selected.push_back(gn2);
  1096. }
  1097. } else if (b->get_shift()) {
  1098. box_selection_mode_additive = false;
  1099. previous_selected.clear();
  1100. for (int i = get_child_count() - 1; i >= 0; i--) {
  1101. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1102. if (!gn2 || !gn2->is_selected()) {
  1103. continue;
  1104. }
  1105. previous_selected.push_back(gn2);
  1106. }
  1107. } else {
  1108. box_selection_mode_additive = true;
  1109. previous_selected.clear();
  1110. for (int i = get_child_count() - 1; i >= 0; i--) {
  1111. GraphNode *gn2 = Object::cast_to<GraphNode>(get_child(i));
  1112. if (!gn2) {
  1113. continue;
  1114. }
  1115. if (gn2->is_selected()) {
  1116. emit_signal("node_unselected", gn2);
  1117. }
  1118. gn2->set_selected(false);
  1119. }
  1120. }
  1121. }
  1122. }
  1123. if (b->get_button_index() == BUTTON_LEFT && !b->is_pressed() && box_selecting) {
  1124. box_selecting = false;
  1125. box_selecting_rect = Rect2();
  1126. previous_selected.clear();
  1127. top_layer->update();
  1128. minimap->update();
  1129. }
  1130. int scroll_direction = (b->get_button_index() == BUTTON_WHEEL_DOWN) - (b->get_button_index() == BUTTON_WHEEL_UP);
  1131. if (scroll_direction != 0) {
  1132. if (b->get_control()) {
  1133. if (b->get_shift()) {
  1134. // Horizontal scrolling.
  1135. h_scroll->set_value(h_scroll->get_value() + (h_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1136. } else {
  1137. // Vertical scrolling.
  1138. v_scroll->set_value(v_scroll->get_value() + (v_scroll->get_page() * b->get_factor() / 8) * scroll_direction);
  1139. }
  1140. } else {
  1141. // Zooming.
  1142. set_zoom_custom(scroll_direction < 0 ? zoom * zoom_step : zoom / zoom_step, b->get_position());
  1143. }
  1144. }
  1145. }
  1146. Ref<InputEventKey> k = p_ev;
  1147. if (k.is_valid()) {
  1148. if (k->get_scancode() == KEY_D && k->is_pressed() && k->get_command()) {
  1149. emit_signal("duplicate_nodes_request");
  1150. accept_event();
  1151. }
  1152. if (k->get_scancode() == KEY_C && k->is_pressed() && k->get_command()) {
  1153. emit_signal("copy_nodes_request");
  1154. accept_event();
  1155. }
  1156. if (k->get_scancode() == KEY_V && k->is_pressed() && k->get_command()) {
  1157. emit_signal("paste_nodes_request");
  1158. accept_event();
  1159. }
  1160. if (k->get_scancode() == KEY_DELETE && k->is_pressed()) {
  1161. Array nodes;
  1162. for (int i = 0; i < get_child_count(); i++) {
  1163. GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
  1164. if (gn) {
  1165. if (gn->is_selected() && gn->is_close_button_visible()) {
  1166. nodes.push_back(gn->get_name());
  1167. }
  1168. }
  1169. }
  1170. emit_signal("delete_nodes_request", nodes);
  1171. accept_event();
  1172. }
  1173. }
  1174. Ref<InputEventMagnifyGesture> magnify_gesture = p_ev;
  1175. if (magnify_gesture.is_valid()) {
  1176. set_zoom_custom(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  1177. }
  1178. Ref<InputEventPanGesture> pan_gesture = p_ev;
  1179. if (pan_gesture.is_valid()) {
  1180. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  1181. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  1182. }
  1183. }
  1184. void GraphEdit::set_connection_activity(const StringName &p_from, int p_from_port, const StringName &p_to, int p_to_port, float p_activity) {
  1185. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  1186. 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) {
  1187. if (Math::is_equal_approx(E->get().activity, p_activity)) {
  1188. //update only if changed
  1189. top_layer->update();
  1190. minimap->update();
  1191. connections_layer->update();
  1192. }
  1193. E->get().activity = p_activity;
  1194. return;
  1195. }
  1196. }
  1197. }
  1198. void GraphEdit::clear_connections() {
  1199. connections.clear();
  1200. minimap->update();
  1201. update();
  1202. connections_layer->update();
  1203. }
  1204. void GraphEdit::set_zoom(float p_zoom) {
  1205. set_zoom_custom(p_zoom, get_size() / 2);
  1206. }
  1207. void GraphEdit::set_zoom_custom(float p_zoom, const Vector2 &p_center) {
  1208. p_zoom = CLAMP(p_zoom, zoom_min, zoom_max);
  1209. if (zoom == p_zoom) {
  1210. return;
  1211. }
  1212. Vector2 sbofs = (Vector2(h_scroll->get_value(), v_scroll->get_value()) + p_center) / zoom;
  1213. zoom = p_zoom;
  1214. top_layer->update();
  1215. zoom_minus->set_disabled(zoom == zoom_min);
  1216. zoom_plus->set_disabled(zoom == zoom_max);
  1217. _update_scroll();
  1218. minimap->update();
  1219. connections_layer->update();
  1220. if (is_visible_in_tree()) {
  1221. Vector2 ofs = sbofs * zoom - p_center;
  1222. h_scroll->set_value(ofs.x);
  1223. v_scroll->set_value(ofs.y);
  1224. }
  1225. _update_zoom_label();
  1226. update();
  1227. }
  1228. float GraphEdit::get_zoom() const {
  1229. return zoom;
  1230. }
  1231. void GraphEdit::set_zoom_step(float p_zoom_step) {
  1232. p_zoom_step = abs(p_zoom_step);
  1233. if (zoom_step == p_zoom_step) {
  1234. return;
  1235. }
  1236. zoom_step = p_zoom_step;
  1237. }
  1238. float GraphEdit::get_zoom_step() const {
  1239. return zoom_step;
  1240. }
  1241. void GraphEdit::set_zoom_min(float p_zoom_min) {
  1242. ERR_FAIL_COND_MSG(p_zoom_min > zoom_max, "Cannot set min zoom level greater than max zoom level.");
  1243. if (zoom_min == p_zoom_min) {
  1244. return;
  1245. }
  1246. zoom_min = p_zoom_min;
  1247. set_zoom(zoom);
  1248. }
  1249. float GraphEdit::get_zoom_min() const {
  1250. return zoom_min;
  1251. }
  1252. void GraphEdit::set_zoom_max(float p_zoom_max) {
  1253. ERR_FAIL_COND_MSG(p_zoom_max < zoom_min, "Cannot set max zoom level lesser than min zoom level.");
  1254. if (zoom_max == p_zoom_max) {
  1255. return;
  1256. }
  1257. zoom_max = p_zoom_max;
  1258. set_zoom(zoom);
  1259. }
  1260. float GraphEdit::get_zoom_max() const {
  1261. return zoom_max;
  1262. }
  1263. void GraphEdit::set_show_zoom_label(bool p_enable) {
  1264. if (zoom_label->is_visible() == p_enable) {
  1265. return;
  1266. }
  1267. zoom_label->set_visible(p_enable);
  1268. }
  1269. bool GraphEdit::is_showing_zoom_label() const {
  1270. return zoom_label->is_visible();
  1271. }
  1272. void GraphEdit::set_right_disconnects(bool p_enable) {
  1273. right_disconnects = p_enable;
  1274. }
  1275. bool GraphEdit::is_right_disconnects_enabled() const {
  1276. return right_disconnects;
  1277. }
  1278. void GraphEdit::add_valid_right_disconnect_type(int p_type) {
  1279. valid_right_disconnect_types.insert(p_type);
  1280. }
  1281. void GraphEdit::remove_valid_right_disconnect_type(int p_type) {
  1282. valid_right_disconnect_types.erase(p_type);
  1283. }
  1284. void GraphEdit::add_valid_left_disconnect_type(int p_type) {
  1285. valid_left_disconnect_types.insert(p_type);
  1286. }
  1287. void GraphEdit::remove_valid_left_disconnect_type(int p_type) {
  1288. valid_left_disconnect_types.erase(p_type);
  1289. }
  1290. Array GraphEdit::_get_connection_list() const {
  1291. List<Connection> conns;
  1292. get_connection_list(&conns);
  1293. Array arr;
  1294. for (List<Connection>::Element *E = conns.front(); E; E = E->next()) {
  1295. Dictionary d;
  1296. d["from"] = E->get().from;
  1297. d["from_port"] = E->get().from_port;
  1298. d["to"] = E->get().to;
  1299. d["to_port"] = E->get().to_port;
  1300. arr.push_back(d);
  1301. }
  1302. return arr;
  1303. }
  1304. void GraphEdit::_zoom_minus() {
  1305. set_zoom(zoom / zoom_step);
  1306. }
  1307. void GraphEdit::_zoom_reset() {
  1308. set_zoom(1);
  1309. }
  1310. void GraphEdit::_zoom_plus() {
  1311. set_zoom(zoom * zoom_step);
  1312. }
  1313. void GraphEdit::_update_zoom_label() {
  1314. int zoom_percent = static_cast<int>(Math::round(zoom * 100));
  1315. String zoom_text = itos(zoom_percent) + "%";
  1316. zoom_label->set_text(zoom_text);
  1317. }
  1318. void GraphEdit::add_valid_connection_type(int p_type, int p_with_type) {
  1319. ConnType ct(p_type, p_with_type);
  1320. valid_connection_types.insert(ct);
  1321. }
  1322. void GraphEdit::remove_valid_connection_type(int p_type, int p_with_type) {
  1323. ConnType ct(p_type, p_with_type);
  1324. valid_connection_types.erase(ct);
  1325. }
  1326. bool GraphEdit::is_valid_connection_type(int p_type, int p_with_type) const {
  1327. ConnType ct(p_type, p_with_type);
  1328. return valid_connection_types.has(ct);
  1329. }
  1330. void GraphEdit::set_use_snap(bool p_enable) {
  1331. snap_button->set_pressed(p_enable);
  1332. update();
  1333. }
  1334. bool GraphEdit::is_using_snap() const {
  1335. return snap_button->is_pressed();
  1336. }
  1337. int GraphEdit::get_snap() const {
  1338. return snap_amount->get_value();
  1339. }
  1340. void GraphEdit::set_snap(int p_snap) {
  1341. ERR_FAIL_COND(p_snap < 5);
  1342. snap_amount->set_value(p_snap);
  1343. update();
  1344. }
  1345. void GraphEdit::_snap_toggled() {
  1346. update();
  1347. }
  1348. void GraphEdit::_snap_value_changed(double) {
  1349. update();
  1350. }
  1351. void GraphEdit::set_minimap_size(Vector2 p_size) {
  1352. minimap->set_size(p_size);
  1353. Vector2 minimap_size = minimap->get_size(); // The size might've been adjusted by the minimum size.
  1354. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1355. minimap->set_margin(Margin::MARGIN_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1356. minimap->set_margin(Margin::MARGIN_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1357. minimap->set_margin(Margin::MARGIN_RIGHT, -MINIMAP_OFFSET);
  1358. minimap->set_margin(Margin::MARGIN_BOTTOM, -MINIMAP_OFFSET);
  1359. minimap->update();
  1360. }
  1361. Vector2 GraphEdit::get_minimap_size() const {
  1362. return minimap->get_size();
  1363. }
  1364. void GraphEdit::set_minimap_opacity(float p_opacity) {
  1365. minimap->set_modulate(Color(1, 1, 1, p_opacity));
  1366. minimap->update();
  1367. }
  1368. float GraphEdit::get_minimap_opacity() const {
  1369. Color minimap_modulate = minimap->get_modulate();
  1370. return minimap_modulate.a;
  1371. }
  1372. void GraphEdit::set_minimap_enabled(bool p_enable) {
  1373. minimap_button->set_pressed(p_enable);
  1374. _minimap_toggled();
  1375. minimap->update();
  1376. }
  1377. bool GraphEdit::is_minimap_enabled() const {
  1378. return minimap_button->is_pressed();
  1379. }
  1380. void GraphEdit::_minimap_toggled() {
  1381. if (is_minimap_enabled()) {
  1382. minimap->set_visible(true);
  1383. minimap->update();
  1384. } else {
  1385. minimap->set_visible(false);
  1386. }
  1387. }
  1388. HBoxContainer *GraphEdit::get_zoom_hbox() {
  1389. return zoom_hb;
  1390. }
  1391. void GraphEdit::_bind_methods() {
  1392. ClassDB::bind_method(D_METHOD("connect_node", "from", "from_port", "to", "to_port"), &GraphEdit::connect_node);
  1393. ClassDB::bind_method(D_METHOD("is_node_connected", "from", "from_port", "to", "to_port"), &GraphEdit::is_node_connected);
  1394. ClassDB::bind_method(D_METHOD("disconnect_node", "from", "from_port", "to", "to_port"), &GraphEdit::disconnect_node);
  1395. ClassDB::bind_method(D_METHOD("set_connection_activity", "from", "from_port", "to", "to_port", "amount"), &GraphEdit::set_connection_activity);
  1396. ClassDB::bind_method(D_METHOD("get_connection_list"), &GraphEdit::_get_connection_list);
  1397. ClassDB::bind_method(D_METHOD("clear_connections"), &GraphEdit::clear_connections);
  1398. ClassDB::bind_method(D_METHOD("get_scroll_ofs"), &GraphEdit::get_scroll_ofs);
  1399. ClassDB::bind_method(D_METHOD("set_scroll_ofs", "ofs"), &GraphEdit::set_scroll_ofs);
  1400. ClassDB::bind_method(D_METHOD("add_valid_right_disconnect_type", "type"), &GraphEdit::add_valid_right_disconnect_type);
  1401. ClassDB::bind_method(D_METHOD("remove_valid_right_disconnect_type", "type"), &GraphEdit::remove_valid_right_disconnect_type);
  1402. ClassDB::bind_method(D_METHOD("add_valid_left_disconnect_type", "type"), &GraphEdit::add_valid_left_disconnect_type);
  1403. ClassDB::bind_method(D_METHOD("remove_valid_left_disconnect_type", "type"), &GraphEdit::remove_valid_left_disconnect_type);
  1404. ClassDB::bind_method(D_METHOD("add_valid_connection_type", "from_type", "to_type"), &GraphEdit::add_valid_connection_type);
  1405. ClassDB::bind_method(D_METHOD("remove_valid_connection_type", "from_type", "to_type"), &GraphEdit::remove_valid_connection_type);
  1406. ClassDB::bind_method(D_METHOD("is_valid_connection_type", "from_type", "to_type"), &GraphEdit::is_valid_connection_type);
  1407. ClassDB::bind_method(D_METHOD("set_zoom", "p_zoom"), &GraphEdit::set_zoom);
  1408. ClassDB::bind_method(D_METHOD("get_zoom"), &GraphEdit::get_zoom);
  1409. ClassDB::bind_method(D_METHOD("set_zoom_min", "zoom_min"), &GraphEdit::set_zoom_min);
  1410. ClassDB::bind_method(D_METHOD("get_zoom_min"), &GraphEdit::get_zoom_min);
  1411. ClassDB::bind_method(D_METHOD("set_zoom_max", "zoom_max"), &GraphEdit::set_zoom_max);
  1412. ClassDB::bind_method(D_METHOD("get_zoom_max"), &GraphEdit::get_zoom_max);
  1413. ClassDB::bind_method(D_METHOD("set_zoom_step", "zoom_step"), &GraphEdit::set_zoom_step);
  1414. ClassDB::bind_method(D_METHOD("get_zoom_step"), &GraphEdit::get_zoom_step);
  1415. ClassDB::bind_method(D_METHOD("set_show_zoom_label", "enable"), &GraphEdit::set_show_zoom_label);
  1416. ClassDB::bind_method(D_METHOD("is_showing_zoom_label"), &GraphEdit::is_showing_zoom_label);
  1417. ClassDB::bind_method(D_METHOD("set_snap", "pixels"), &GraphEdit::set_snap);
  1418. ClassDB::bind_method(D_METHOD("get_snap"), &GraphEdit::get_snap);
  1419. ClassDB::bind_method(D_METHOD("set_use_snap", "enable"), &GraphEdit::set_use_snap);
  1420. ClassDB::bind_method(D_METHOD("is_using_snap"), &GraphEdit::is_using_snap);
  1421. ClassDB::bind_method(D_METHOD("set_minimap_size", "p_size"), &GraphEdit::set_minimap_size);
  1422. ClassDB::bind_method(D_METHOD("get_minimap_size"), &GraphEdit::get_minimap_size);
  1423. ClassDB::bind_method(D_METHOD("set_minimap_opacity", "p_opacity"), &GraphEdit::set_minimap_opacity);
  1424. ClassDB::bind_method(D_METHOD("get_minimap_opacity"), &GraphEdit::get_minimap_opacity);
  1425. ClassDB::bind_method(D_METHOD("set_minimap_enabled", "enable"), &GraphEdit::set_minimap_enabled);
  1426. ClassDB::bind_method(D_METHOD("is_minimap_enabled"), &GraphEdit::is_minimap_enabled);
  1427. ClassDB::bind_method(D_METHOD("_minimap_toggled"), &GraphEdit::_minimap_toggled);
  1428. ClassDB::bind_method(D_METHOD("_minimap_draw"), &GraphEdit::_minimap_draw);
  1429. ClassDB::bind_method(D_METHOD("set_right_disconnects", "enable"), &GraphEdit::set_right_disconnects);
  1430. ClassDB::bind_method(D_METHOD("is_right_disconnects_enabled"), &GraphEdit::is_right_disconnects_enabled);
  1431. ClassDB::bind_method(D_METHOD("_graph_node_moved"), &GraphEdit::_graph_node_moved);
  1432. ClassDB::bind_method(D_METHOD("_graph_node_raised"), &GraphEdit::_graph_node_raised);
  1433. ClassDB::bind_method(D_METHOD("_graph_node_slot_updated"), &GraphEdit::_graph_node_slot_updated);
  1434. ClassDB::bind_method(D_METHOD("_top_layer_input"), &GraphEdit::_top_layer_input);
  1435. ClassDB::bind_method(D_METHOD("_top_layer_draw"), &GraphEdit::_top_layer_draw);
  1436. ClassDB::bind_method(D_METHOD("_scroll_moved"), &GraphEdit::_scroll_moved);
  1437. ClassDB::bind_method(D_METHOD("_zoom_minus"), &GraphEdit::_zoom_minus);
  1438. ClassDB::bind_method(D_METHOD("_zoom_reset"), &GraphEdit::_zoom_reset);
  1439. ClassDB::bind_method(D_METHOD("_zoom_plus"), &GraphEdit::_zoom_plus);
  1440. ClassDB::bind_method(D_METHOD("_snap_toggled"), &GraphEdit::_snap_toggled);
  1441. ClassDB::bind_method(D_METHOD("_snap_value_changed"), &GraphEdit::_snap_value_changed);
  1442. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphEdit::_gui_input);
  1443. ClassDB::bind_method(D_METHOD("_update_scroll_offset"), &GraphEdit::_update_scroll_offset);
  1444. ClassDB::bind_method(D_METHOD("_connections_layer_draw"), &GraphEdit::_connections_layer_draw);
  1445. ClassDB::bind_method(D_METHOD("get_zoom_hbox"), &GraphEdit::get_zoom_hbox);
  1446. ClassDB::bind_method(D_METHOD("set_selected", "node"), &GraphEdit::set_selected);
  1447. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "right_disconnects"), "set_right_disconnects", "is_right_disconnects_enabled");
  1448. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "scroll_offset"), "set_scroll_ofs", "get_scroll_ofs");
  1449. ADD_PROPERTY(PropertyInfo(Variant::INT, "snap_distance"), "set_snap", "get_snap");
  1450. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_snap"), "set_use_snap", "is_using_snap");
  1451. ADD_GROUP("Zoom", "");
  1452. ADD_PROPERTY(PropertyInfo(Variant::REAL, "zoom"), "set_zoom", "get_zoom");
  1453. ADD_PROPERTY(PropertyInfo(Variant::REAL, "zoom_min"), "set_zoom_min", "get_zoom_min");
  1454. ADD_PROPERTY(PropertyInfo(Variant::REAL, "zoom_max"), "set_zoom_max", "get_zoom_max");
  1455. ADD_PROPERTY(PropertyInfo(Variant::REAL, "zoom_step"), "set_zoom_step", "get_zoom_step");
  1456. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_zoom_label"), "set_show_zoom_label", "is_showing_zoom_label");
  1457. ADD_GROUP("Minimap", "minimap");
  1458. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_enabled"), "set_minimap_enabled", "is_minimap_enabled");
  1459. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "minimap_size"), "set_minimap_size", "get_minimap_size");
  1460. ADD_PROPERTY(PropertyInfo(Variant::REAL, "minimap_opacity"), "set_minimap_opacity", "get_minimap_opacity");
  1461. ADD_SIGNAL(MethodInfo("connection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1462. ADD_SIGNAL(MethodInfo("disconnection_request", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot")));
  1463. ADD_SIGNAL(MethodInfo("popup_request", PropertyInfo(Variant::VECTOR2, "position")));
  1464. ADD_SIGNAL(MethodInfo("duplicate_nodes_request"));
  1465. ADD_SIGNAL(MethodInfo("copy_nodes_request"));
  1466. ADD_SIGNAL(MethodInfo("paste_nodes_request"));
  1467. ADD_SIGNAL(MethodInfo("node_selected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1468. ADD_SIGNAL(MethodInfo("node_unselected", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
  1469. ADD_SIGNAL(MethodInfo("connection_to_empty", PropertyInfo(Variant::STRING, "from"), PropertyInfo(Variant::INT, "from_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1470. ADD_SIGNAL(MethodInfo("connection_from_empty", PropertyInfo(Variant::STRING, "to"), PropertyInfo(Variant::INT, "to_slot"), PropertyInfo(Variant::VECTOR2, "release_position")));
  1471. ADD_SIGNAL(MethodInfo("delete_nodes_request", PropertyInfo(Variant::ARRAY, "nodes")));
  1472. ADD_SIGNAL(MethodInfo("_begin_node_move"));
  1473. ADD_SIGNAL(MethodInfo("_end_node_move"));
  1474. ADD_SIGNAL(MethodInfo("scroll_offset_changed", PropertyInfo(Variant::VECTOR2, "ofs")));
  1475. }
  1476. GraphEdit::GraphEdit() {
  1477. set_focus_mode(FOCUS_ALL);
  1478. // Allow dezooming 8 times from the default zoom level.
  1479. // At low zoom levels, text is unreadable due to its small size and poor filtering,
  1480. // but this is still useful for previewing and navigation.
  1481. zoom_min = (1 / Math::pow(zoom_step, 8));
  1482. // Allow zooming 4 times from the default zoom level.
  1483. zoom_max = (1 * Math::pow(zoom_step, 4));
  1484. top_layer = memnew(GraphEditFilter(this));
  1485. add_child(top_layer);
  1486. top_layer->set_mouse_filter(MOUSE_FILTER_PASS);
  1487. top_layer->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1488. top_layer->connect("draw", this, "_top_layer_draw");
  1489. top_layer->connect("gui_input", this, "_top_layer_input");
  1490. connections_layer = memnew(Control);
  1491. add_child(connections_layer);
  1492. connections_layer->connect("draw", this, "_connections_layer_draw");
  1493. connections_layer->set_name("CLAYER");
  1494. connections_layer->set_disable_visibility_clip(true); // so it can draw freely and be offset
  1495. connections_layer->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1496. h_scroll = memnew(HScrollBar);
  1497. h_scroll->set_name("_h_scroll");
  1498. top_layer->add_child(h_scroll);
  1499. v_scroll = memnew(VScrollBar);
  1500. v_scroll->set_name("_v_scroll");
  1501. top_layer->add_child(v_scroll);
  1502. //set large minmax so it can scroll even if not resized yet
  1503. h_scroll->set_min(-10000);
  1504. h_scroll->set_max(10000);
  1505. v_scroll->set_min(-10000);
  1506. v_scroll->set_max(10000);
  1507. h_scroll->connect("value_changed", this, "_scroll_moved");
  1508. v_scroll->connect("value_changed", this, "_scroll_moved");
  1509. zoom_hb = memnew(HBoxContainer);
  1510. top_layer->add_child(zoom_hb);
  1511. zoom_hb->set_position(Vector2(10, 10));
  1512. zoom_label = memnew(Label);
  1513. zoom_hb->add_child(zoom_label);
  1514. zoom_label->set_visible(false);
  1515. zoom_label->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  1516. zoom_label->set_align(Label::ALIGN_CENTER);
  1517. #ifdef TOOLS_ENABLED
  1518. zoom_label->set_custom_minimum_size(Size2(48, 0) * EDSCALE);
  1519. #else
  1520. zoom_label->set_custom_minimum_size(Size2(48, 0));
  1521. #endif
  1522. _update_zoom_label();
  1523. zoom_minus = memnew(ToolButton);
  1524. zoom_hb->add_child(zoom_minus);
  1525. zoom_minus->set_tooltip(RTR("Zoom Out"));
  1526. zoom_minus->connect("pressed", this, "_zoom_minus");
  1527. zoom_minus->set_focus_mode(FOCUS_NONE);
  1528. zoom_reset = memnew(ToolButton);
  1529. zoom_hb->add_child(zoom_reset);
  1530. zoom_reset->set_tooltip(RTR("Zoom Reset"));
  1531. zoom_reset->connect("pressed", this, "_zoom_reset");
  1532. zoom_reset->set_focus_mode(FOCUS_NONE);
  1533. zoom_plus = memnew(ToolButton);
  1534. zoom_hb->add_child(zoom_plus);
  1535. zoom_plus->set_tooltip(RTR("Zoom In"));
  1536. zoom_plus->connect("pressed", this, "_zoom_plus");
  1537. zoom_plus->set_focus_mode(FOCUS_NONE);
  1538. snap_button = memnew(ToolButton);
  1539. snap_button->set_toggle_mode(true);
  1540. snap_button->set_tooltip(RTR("Enable snap and show grid."));
  1541. snap_button->connect("pressed", this, "_snap_toggled");
  1542. snap_button->set_pressed(true);
  1543. snap_button->set_focus_mode(FOCUS_NONE);
  1544. zoom_hb->add_child(snap_button);
  1545. snap_amount = memnew(SpinBox);
  1546. snap_amount->set_min(5);
  1547. snap_amount->set_max(100);
  1548. snap_amount->set_step(1);
  1549. snap_amount->set_value(20);
  1550. snap_amount->connect("value_changed", this, "_snap_value_changed");
  1551. zoom_hb->add_child(snap_amount);
  1552. minimap_button = memnew(Button);
  1553. minimap_button->set_flat(true);
  1554. minimap_button->set_toggle_mode(true);
  1555. minimap_button->set_tooltip(RTR("Enable grid minimap."));
  1556. minimap_button->connect("pressed", this, "_minimap_toggled");
  1557. minimap_button->set_pressed(true);
  1558. minimap_button->set_focus_mode(FOCUS_NONE);
  1559. zoom_hb->add_child(minimap_button);
  1560. Vector2 minimap_size = Vector2(240, 160);
  1561. float minimap_opacity = 0.65;
  1562. minimap = memnew(GraphEditMinimap(this));
  1563. top_layer->add_child(minimap);
  1564. minimap->set_name("_minimap");
  1565. minimap->set_modulate(Color(1, 1, 1, minimap_opacity));
  1566. minimap->set_mouse_filter(MOUSE_FILTER_PASS);
  1567. minimap->set_custom_minimum_size(Vector2(50, 50));
  1568. minimap->set_size(minimap_size);
  1569. minimap->set_anchors_preset(Control::PRESET_BOTTOM_RIGHT);
  1570. minimap->set_margin(Margin::MARGIN_LEFT, -minimap_size.x - MINIMAP_OFFSET);
  1571. minimap->set_margin(Margin::MARGIN_TOP, -minimap_size.y - MINIMAP_OFFSET);
  1572. minimap->set_margin(Margin::MARGIN_RIGHT, -MINIMAP_OFFSET);
  1573. minimap->set_margin(Margin::MARGIN_BOTTOM, -MINIMAP_OFFSET);
  1574. minimap->connect("draw", this, "_minimap_draw");
  1575. set_clip_contents(true);
  1576. }