graph_node.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*************************************************************************/
  2. /* graph_node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_node.h"
  31. #include "core/string/translation.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "graph_edit.h"
  34. #endif
  35. struct _MinSizeCache {
  36. int min_size;
  37. bool will_stretch;
  38. int final_size;
  39. };
  40. bool GraphNode::_set(const StringName &p_name, const Variant &p_value) {
  41. String str = p_name;
  42. if (!str.begins_with("slot/")) {
  43. return false;
  44. }
  45. int idx = str.get_slice("/", 1).to_int();
  46. String what = str.get_slice("/", 2);
  47. Slot si;
  48. if (slot_info.has(idx)) {
  49. si = slot_info[idx];
  50. }
  51. if (what == "left_enabled") {
  52. si.enable_left = p_value;
  53. } else if (what == "left_type") {
  54. si.type_left = p_value;
  55. } else if (what == "left_icon") {
  56. si.custom_slot_left = p_value;
  57. } else if (what == "left_color") {
  58. si.color_left = p_value;
  59. } else if (what == "right_enabled") {
  60. si.enable_right = p_value;
  61. } else if (what == "right_type") {
  62. si.type_right = p_value;
  63. } else if (what == "right_color") {
  64. si.color_right = p_value;
  65. } else if (what == "right_icon") {
  66. si.custom_slot_right = p_value;
  67. } else if (what == "draw_stylebox") {
  68. si.draw_stylebox = p_value;
  69. } else {
  70. return false;
  71. }
  72. set_slot(idx, si.enable_left, si.type_left, si.color_left, si.enable_right, si.type_right, si.color_right, si.custom_slot_left, si.custom_slot_right, si.draw_stylebox);
  73. update();
  74. return true;
  75. }
  76. bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const {
  77. String str = p_name;
  78. if (!str.begins_with("slot/")) {
  79. return false;
  80. }
  81. int idx = str.get_slice("/", 1).to_int();
  82. String what = str.get_slice("/", 2);
  83. Slot si;
  84. if (slot_info.has(idx)) {
  85. si = slot_info[idx];
  86. }
  87. if (what == "left_enabled") {
  88. r_ret = si.enable_left;
  89. } else if (what == "left_type") {
  90. r_ret = si.type_left;
  91. } else if (what == "left_color") {
  92. r_ret = si.color_left;
  93. } else if (what == "left_icon") {
  94. r_ret = si.custom_slot_left;
  95. } else if (what == "right_enabled") {
  96. r_ret = si.enable_right;
  97. } else if (what == "right_type") {
  98. r_ret = si.type_right;
  99. } else if (what == "right_color") {
  100. r_ret = si.color_right;
  101. } else if (what == "right_icon") {
  102. r_ret = si.custom_slot_right;
  103. } else if (what == "draw_stylebox") {
  104. r_ret = si.draw_stylebox;
  105. } else {
  106. return false;
  107. }
  108. return true;
  109. }
  110. void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const {
  111. int idx = 0;
  112. for (int i = 0; i < get_child_count(); i++) {
  113. Control *c = Object::cast_to<Control>(get_child(i));
  114. if (!c || c->is_set_as_top_level()) {
  115. continue;
  116. }
  117. String base = "slot/" + itos(idx) + "/";
  118. p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled"));
  119. p_list->push_back(PropertyInfo(Variant::INT, base + "left_type"));
  120. p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color"));
  121. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "left_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  122. p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled"));
  123. p_list->push_back(PropertyInfo(Variant::INT, base + "right_type"));
  124. p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color"));
  125. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  126. p_list->push_back(PropertyInfo(Variant::BOOL, base + "draw_stylebox"));
  127. idx++;
  128. }
  129. }
  130. void GraphNode::_resort() {
  131. /** First pass, determine minimum size AND amount of stretchable elements */
  132. Size2i new_size = get_size();
  133. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  134. Ref<StyleBox> sb_slot = get_theme_stylebox(SNAME("slot"));
  135. int sep = get_theme_constant(SNAME("separation"));
  136. bool first = true;
  137. int children_count = 0;
  138. int stretch_min = 0;
  139. int stretch_avail = 0;
  140. float stretch_ratio_total = 0;
  141. HashMap<Control *, _MinSizeCache> min_size_cache;
  142. for (int i = 0; i < get_child_count(); i++) {
  143. Control *c = Object::cast_to<Control>(get_child(i));
  144. if (!c || !c->is_visible_in_tree()) {
  145. continue;
  146. }
  147. if (c->is_set_as_top_level()) {
  148. continue;
  149. }
  150. Size2i size = c->get_combined_minimum_size() + (slot_info[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2());
  151. _MinSizeCache msc;
  152. stretch_min += size.height;
  153. msc.min_size = size.height;
  154. msc.will_stretch = c->get_v_size_flags() & SIZE_EXPAND;
  155. if (msc.will_stretch) {
  156. stretch_avail += msc.min_size;
  157. stretch_ratio_total += c->get_stretch_ratio();
  158. }
  159. msc.final_size = msc.min_size;
  160. min_size_cache[c] = msc;
  161. children_count++;
  162. }
  163. if (children_count == 0) {
  164. return;
  165. }
  166. int stretch_max = new_size.height - (children_count - 1) * sep;
  167. int stretch_diff = stretch_max - stretch_min;
  168. if (stretch_diff < 0) {
  169. //avoid negative stretch space
  170. stretch_diff = 0;
  171. }
  172. stretch_avail += stretch_diff - sb->get_margin(SIDE_BOTTOM) - sb->get_margin(SIDE_TOP); //available stretch space.
  173. /** Second, pass successively to discard elements that can't be stretched, this will run while stretchable
  174. elements exist */
  175. while (stretch_ratio_total > 0) { // first of all, don't even be here if no stretchable objects exist
  176. bool refit_successful = true; //assume refit-test will go well
  177. for (int i = 0; i < get_child_count(); i++) {
  178. Control *c = Object::cast_to<Control>(get_child(i));
  179. if (!c || !c->is_visible_in_tree()) {
  180. continue;
  181. }
  182. if (c->is_set_as_top_level()) {
  183. continue;
  184. }
  185. ERR_FAIL_COND(!min_size_cache.has(c));
  186. _MinSizeCache &msc = min_size_cache[c];
  187. if (msc.will_stretch) { //wants to stretch
  188. //let's see if it can really stretch
  189. int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  190. if (final_pixel_size < msc.min_size) {
  191. //if available stretching area is too small for widget,
  192. //then remove it from stretching area
  193. msc.will_stretch = false;
  194. stretch_ratio_total -= c->get_stretch_ratio();
  195. refit_successful = false;
  196. stretch_avail -= msc.min_size;
  197. msc.final_size = msc.min_size;
  198. break;
  199. } else {
  200. msc.final_size = final_pixel_size;
  201. }
  202. }
  203. }
  204. if (refit_successful) { //uf refit went well, break
  205. break;
  206. }
  207. }
  208. /** Final pass, draw and stretch elements **/
  209. int ofs = sb->get_margin(SIDE_TOP);
  210. first = true;
  211. int idx = 0;
  212. cache_y.clear();
  213. int w = new_size.width - sb->get_minimum_size().x;
  214. for (int i = 0; i < get_child_count(); i++) {
  215. Control *c = Object::cast_to<Control>(get_child(i));
  216. if (!c || !c->is_visible_in_tree()) {
  217. continue;
  218. }
  219. if (c->is_set_as_top_level()) {
  220. continue;
  221. }
  222. _MinSizeCache &msc = min_size_cache[c];
  223. if (first) {
  224. first = false;
  225. } else {
  226. ofs += sep;
  227. }
  228. int from = ofs;
  229. int to = ofs + msc.final_size;
  230. if (msc.will_stretch && idx == children_count - 1) {
  231. //adjust so the last one always fits perfect
  232. //compensating for numerical imprecision
  233. to = new_size.height - sb->get_margin(SIDE_BOTTOM);
  234. }
  235. int size = to - from;
  236. float margin = sb->get_margin(SIDE_LEFT) + (slot_info[i].draw_stylebox ? sb_slot->get_margin(SIDE_LEFT) : 0);
  237. float width = w - (slot_info[i].draw_stylebox ? sb_slot->get_minimum_size().x : 0);
  238. Rect2 rect(margin, from, width, size);
  239. fit_child_in_rect(c, rect);
  240. cache_y.push_back(from - sb->get_margin(SIDE_TOP) + size * 0.5);
  241. ofs = to;
  242. idx++;
  243. }
  244. update();
  245. connpos_dirty = true;
  246. }
  247. bool GraphNode::has_point(const Point2 &p_point) const {
  248. if (comment) {
  249. Ref<StyleBox> comment = get_theme_stylebox(SNAME("comment"));
  250. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  251. if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
  252. return true;
  253. }
  254. if (Rect2(0, 0, get_size().width, comment->get_margin(SIDE_TOP)).has_point(p_point)) {
  255. return true;
  256. }
  257. return false;
  258. } else {
  259. return Control::has_point(p_point);
  260. }
  261. }
  262. void GraphNode::_notification(int p_what) {
  263. switch (p_what) {
  264. case NOTIFICATION_DRAW: {
  265. Ref<StyleBox> sb;
  266. if (comment) {
  267. sb = get_theme_stylebox(selected ? SNAME("comment_focus") : SNAME("comment"));
  268. } else {
  269. sb = get_theme_stylebox(selected ? SNAME("selected_frame") : SNAME("frame"));
  270. }
  271. Ref<StyleBox> sb_slot = get_theme_stylebox(SNAME("slot"));
  272. Ref<Texture2D> port = get_theme_icon(SNAME("port"));
  273. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  274. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  275. int close_offset = get_theme_constant(SNAME("close_offset"));
  276. int close_h_offset = get_theme_constant(SNAME("close_h_offset"));
  277. Color close_color = get_theme_color(SNAME("close_color"));
  278. Color resizer_color = get_theme_color(SNAME("resizer_color"));
  279. int title_offset = get_theme_constant(SNAME("title_offset"));
  280. int title_h_offset = get_theme_constant(SNAME("title_h_offset"));
  281. Color title_color = get_theme_color(SNAME("title_color"));
  282. Point2i icofs = -port->get_size() * 0.5;
  283. int edgeofs = get_theme_constant(SNAME("port_offset"));
  284. icofs.y += sb->get_margin(SIDE_TOP);
  285. draw_style_box(sb, Rect2(Point2(), get_size()));
  286. switch (overlay) {
  287. case OVERLAY_DISABLED: {
  288. } break;
  289. case OVERLAY_BREAKPOINT: {
  290. draw_style_box(get_theme_stylebox(SNAME("breakpoint")), Rect2(Point2(), get_size()));
  291. } break;
  292. case OVERLAY_POSITION: {
  293. draw_style_box(get_theme_stylebox(SNAME("position")), Rect2(Point2(), get_size()));
  294. } break;
  295. }
  296. int w = get_size().width - sb->get_minimum_size().x;
  297. title_buf->draw(get_canvas_item(), Point2(sb->get_margin(SIDE_LEFT) + title_h_offset, -title_buf->get_size().y + title_offset), title_color);
  298. if (show_close) {
  299. Vector2 cpos = Point2(w + sb->get_margin(SIDE_LEFT) + close_h_offset - close->get_width(), -close->get_height() + close_offset);
  300. draw_texture(close, cpos, close_color);
  301. close_rect.position = cpos;
  302. close_rect.size = close->get_size();
  303. } else {
  304. close_rect = Rect2();
  305. }
  306. for (const KeyValue<int, Slot> &E : slot_info) {
  307. if (E.key < 0 || E.key >= cache_y.size()) {
  308. continue;
  309. }
  310. if (!slot_info.has(E.key)) {
  311. continue;
  312. }
  313. const Slot &s = slot_info[E.key];
  314. // Left port.
  315. if (s.enable_left) {
  316. Ref<Texture2D> p = port;
  317. if (s.custom_slot_left.is_valid()) {
  318. p = s.custom_slot_left;
  319. }
  320. p->draw(get_canvas_item(), icofs + Point2(edgeofs, cache_y[E.key]), s.color_left);
  321. }
  322. // Right port.
  323. if (s.enable_right) {
  324. Ref<Texture2D> p = port;
  325. if (s.custom_slot_right.is_valid()) {
  326. p = s.custom_slot_right;
  327. }
  328. p->draw(get_canvas_item(), icofs + Point2(get_size().x - edgeofs, cache_y[E.key]), s.color_right);
  329. }
  330. // Draw slot stylebox.
  331. if (s.draw_stylebox) {
  332. Control *c = Object::cast_to<Control>(get_child(E.key));
  333. Rect2 c_rect = c->get_rect();
  334. c_rect.position.x = sb->get_margin(SIDE_LEFT);
  335. c_rect.size.width = w;
  336. draw_style_box(sb_slot, c_rect);
  337. }
  338. }
  339. if (resizable) {
  340. draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
  341. }
  342. } break;
  343. case NOTIFICATION_SORT_CHILDREN: {
  344. _resort();
  345. } break;
  346. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  347. case NOTIFICATION_TRANSLATION_CHANGED:
  348. case NOTIFICATION_THEME_CHANGED: {
  349. _shape();
  350. update_minimum_size();
  351. update();
  352. } break;
  353. }
  354. }
  355. void GraphNode::_shape() {
  356. Ref<Font> font = get_theme_font(SNAME("title_font"));
  357. int font_size = get_theme_font_size(SNAME("title_font_size"));
  358. title_buf->clear();
  359. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  360. title_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  361. } else {
  362. title_buf->set_direction((TextServer::Direction)text_direction);
  363. }
  364. title_buf->add_string(title, font, font_size, language);
  365. }
  366. #ifdef TOOLS_ENABLED
  367. void GraphNode::_edit_set_position(const Point2 &p_position) {
  368. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  369. if (graph) {
  370. Point2 offset = (p_position + graph->get_scroll_ofs()) * graph->get_zoom();
  371. set_position_offset(offset);
  372. }
  373. set_position(p_position);
  374. }
  375. void GraphNode::_validate_property(PropertyInfo &p_property) const {
  376. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  377. if (graph) {
  378. if (p_property.name == "position") {
  379. p_property.usage |= PROPERTY_USAGE_READ_ONLY;
  380. }
  381. }
  382. }
  383. #endif
  384. void GraphNode::set_slot(int p_idx, bool p_enable_left, int p_type_left, const Color &p_color_left, bool p_enable_right, int p_type_right, const Color &p_color_right, const Ref<Texture2D> &p_custom_left, const Ref<Texture2D> &p_custom_right, bool p_draw_stylebox) {
  385. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set slot with p_idx (%d) lesser than zero.", p_idx));
  386. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) &&
  387. !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) &&
  388. !p_custom_left.is_valid() && !p_custom_right.is_valid()) {
  389. slot_info.erase(p_idx);
  390. return;
  391. }
  392. Slot s;
  393. s.enable_left = p_enable_left;
  394. s.type_left = p_type_left;
  395. s.color_left = p_color_left;
  396. s.enable_right = p_enable_right;
  397. s.type_right = p_type_right;
  398. s.color_right = p_color_right;
  399. s.custom_slot_left = p_custom_left;
  400. s.custom_slot_right = p_custom_right;
  401. s.draw_stylebox = p_draw_stylebox;
  402. slot_info[p_idx] = s;
  403. update();
  404. connpos_dirty = true;
  405. emit_signal(SNAME("slot_updated"), p_idx);
  406. }
  407. void GraphNode::clear_slot(int p_idx) {
  408. slot_info.erase(p_idx);
  409. update();
  410. connpos_dirty = true;
  411. }
  412. void GraphNode::clear_all_slots() {
  413. slot_info.clear();
  414. update();
  415. connpos_dirty = true;
  416. }
  417. bool GraphNode::is_slot_enabled_left(int p_idx) const {
  418. if (!slot_info.has(p_idx)) {
  419. return false;
  420. }
  421. return slot_info[p_idx].enable_left;
  422. }
  423. void GraphNode::set_slot_enabled_left(int p_idx, bool p_enable_left) {
  424. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_left for the slot with p_idx (%d) lesser than zero.", p_idx));
  425. if (slot_info[p_idx].enable_left == p_enable_left) {
  426. return;
  427. }
  428. slot_info[p_idx].enable_left = p_enable_left;
  429. update();
  430. connpos_dirty = true;
  431. emit_signal(SNAME("slot_updated"), p_idx);
  432. }
  433. void GraphNode::set_slot_type_left(int p_idx, int p_type_left) {
  434. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set type_left for the slot '%d' because it hasn't been enabled.", p_idx));
  435. if (slot_info[p_idx].type_left == p_type_left) {
  436. return;
  437. }
  438. slot_info[p_idx].type_left = p_type_left;
  439. update();
  440. connpos_dirty = true;
  441. emit_signal(SNAME("slot_updated"), p_idx);
  442. }
  443. int GraphNode::get_slot_type_left(int p_idx) const {
  444. if (!slot_info.has(p_idx)) {
  445. return 0;
  446. }
  447. return slot_info[p_idx].type_left;
  448. }
  449. void GraphNode::set_slot_color_left(int p_idx, const Color &p_color_left) {
  450. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set color_left for the slot '%d' because it hasn't been enabled.", p_idx));
  451. if (slot_info[p_idx].color_left == p_color_left) {
  452. return;
  453. }
  454. slot_info[p_idx].color_left = p_color_left;
  455. update();
  456. connpos_dirty = true;
  457. emit_signal(SNAME("slot_updated"), p_idx);
  458. }
  459. Color GraphNode::get_slot_color_left(int p_idx) const {
  460. if (!slot_info.has(p_idx)) {
  461. return Color(1, 1, 1, 1);
  462. }
  463. return slot_info[p_idx].color_left;
  464. }
  465. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  466. if (!slot_info.has(p_idx)) {
  467. return false;
  468. }
  469. return slot_info[p_idx].enable_right;
  470. }
  471. void GraphNode::set_slot_enabled_right(int p_idx, bool p_enable_right) {
  472. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_right for the slot with p_idx (%d) lesser than zero.", p_idx));
  473. if (slot_info[p_idx].enable_right == p_enable_right) {
  474. return;
  475. }
  476. slot_info[p_idx].enable_right = p_enable_right;
  477. update();
  478. connpos_dirty = true;
  479. emit_signal(SNAME("slot_updated"), p_idx);
  480. }
  481. void GraphNode::set_slot_type_right(int p_idx, int p_type_right) {
  482. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set type_right for the slot '%d' because it hasn't been enabled.", p_idx));
  483. if (slot_info[p_idx].type_right == p_type_right) {
  484. return;
  485. }
  486. slot_info[p_idx].type_right = p_type_right;
  487. update();
  488. connpos_dirty = true;
  489. emit_signal(SNAME("slot_updated"), p_idx);
  490. }
  491. int GraphNode::get_slot_type_right(int p_idx) const {
  492. if (!slot_info.has(p_idx)) {
  493. return 0;
  494. }
  495. return slot_info[p_idx].type_right;
  496. }
  497. void GraphNode::set_slot_color_right(int p_idx, const Color &p_color_right) {
  498. ERR_FAIL_COND_MSG(!slot_info.has(p_idx), vformat("Cannot set color_right for the slot '%d' because it hasn't been enabled.", p_idx));
  499. if (slot_info[p_idx].color_right == p_color_right) {
  500. return;
  501. }
  502. slot_info[p_idx].color_right = p_color_right;
  503. update();
  504. connpos_dirty = true;
  505. emit_signal(SNAME("slot_updated"), p_idx);
  506. }
  507. Color GraphNode::get_slot_color_right(int p_idx) const {
  508. if (!slot_info.has(p_idx)) {
  509. return Color(1, 1, 1, 1);
  510. }
  511. return slot_info[p_idx].color_right;
  512. }
  513. bool GraphNode::is_slot_draw_stylebox(int p_idx) const {
  514. if (!slot_info.has(p_idx)) {
  515. return false;
  516. }
  517. return slot_info[p_idx].draw_stylebox;
  518. }
  519. void GraphNode::set_slot_draw_stylebox(int p_idx, bool p_enable) {
  520. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set draw_stylebox for the slot with p_idx (%d) lesser than zero.", p_idx));
  521. slot_info[p_idx].draw_stylebox = p_enable;
  522. update();
  523. connpos_dirty = true;
  524. emit_signal(SNAME("slot_updated"), p_idx);
  525. }
  526. Size2 GraphNode::get_minimum_size() const {
  527. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  528. Ref<StyleBox> sb_slot = get_theme_stylebox(SNAME("slot"));
  529. int sep = get_theme_constant(SNAME("separation"));
  530. int title_h_offset = get_theme_constant(SNAME("title_h_offset"));
  531. bool first = true;
  532. Size2 minsize;
  533. minsize.x = title_buf->get_size().x + title_h_offset;
  534. if (show_close) {
  535. int close_h_offset = get_theme_constant(SNAME("close_h_offset"));
  536. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  537. //TODO: Remove this magic number after GraphNode rework.
  538. minsize.x += 12 + close->get_width() + close_h_offset;
  539. }
  540. for (int i = 0; i < get_child_count(); i++) {
  541. Control *c = Object::cast_to<Control>(get_child(i));
  542. if (!c) {
  543. continue;
  544. }
  545. if (c->is_set_as_top_level()) {
  546. continue;
  547. }
  548. Size2i size = c->get_combined_minimum_size();
  549. if (slot_info.has(i)) {
  550. size += slot_info[i].draw_stylebox ? sb_slot->get_minimum_size() : Size2();
  551. }
  552. minsize.y += size.y;
  553. minsize.x = MAX(minsize.x, size.x);
  554. if (first) {
  555. first = false;
  556. } else {
  557. minsize.y += sep;
  558. }
  559. }
  560. return minsize + sb->get_minimum_size();
  561. }
  562. void GraphNode::set_title(const String &p_title) {
  563. if (title == p_title) {
  564. return;
  565. }
  566. title = p_title;
  567. _shape();
  568. update();
  569. update_minimum_size();
  570. }
  571. String GraphNode::get_title() const {
  572. return title;
  573. }
  574. void GraphNode::set_text_direction(Control::TextDirection p_text_direction) {
  575. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  576. if (text_direction != p_text_direction) {
  577. text_direction = p_text_direction;
  578. _shape();
  579. update();
  580. }
  581. }
  582. Control::TextDirection GraphNode::get_text_direction() const {
  583. return text_direction;
  584. }
  585. void GraphNode::set_language(const String &p_language) {
  586. if (language != p_language) {
  587. language = p_language;
  588. _shape();
  589. update();
  590. }
  591. }
  592. String GraphNode::get_language() const {
  593. return language;
  594. }
  595. void GraphNode::set_position_offset(const Vector2 &p_offset) {
  596. if (position_offset == p_offset) {
  597. return;
  598. }
  599. position_offset = p_offset;
  600. emit_signal(SNAME("position_offset_changed"));
  601. update();
  602. }
  603. Vector2 GraphNode::get_position_offset() const {
  604. return position_offset;
  605. }
  606. void GraphNode::set_selected(bool p_selected) {
  607. if (selected == p_selected) {
  608. return;
  609. }
  610. selected = p_selected;
  611. update();
  612. }
  613. bool GraphNode::is_selected() {
  614. return selected;
  615. }
  616. void GraphNode::set_drag(bool p_drag) {
  617. if (p_drag) {
  618. drag_from = get_position_offset();
  619. } else {
  620. emit_signal(SNAME("dragged"), drag_from, get_position_offset()); //useful for undo/redo
  621. }
  622. }
  623. Vector2 GraphNode::get_drag_from() {
  624. return drag_from;
  625. }
  626. void GraphNode::set_show_close_button(bool p_enable) {
  627. if (show_close == p_enable) {
  628. return;
  629. }
  630. show_close = p_enable;
  631. update();
  632. }
  633. bool GraphNode::is_close_button_visible() const {
  634. return show_close;
  635. }
  636. void GraphNode::_connpos_update() {
  637. int edgeofs = get_theme_constant(SNAME("port_offset"));
  638. int sep = get_theme_constant(SNAME("separation"));
  639. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  640. conn_input_cache.clear();
  641. conn_output_cache.clear();
  642. int vofs = 0;
  643. int idx = 0;
  644. for (int i = 0; i < get_child_count(); i++) {
  645. Control *c = Object::cast_to<Control>(get_child(i));
  646. if (!c) {
  647. continue;
  648. }
  649. if (c->is_set_as_top_level()) {
  650. continue;
  651. }
  652. Size2i size = c->get_rect().size;
  653. int y = sb->get_margin(SIDE_TOP) + vofs;
  654. int h = size.y;
  655. if (slot_info.has(idx)) {
  656. if (slot_info[idx].enable_left) {
  657. ConnCache cc;
  658. cc.pos = Point2i(edgeofs, y + h / 2);
  659. cc.type = slot_info[idx].type_left;
  660. cc.color = slot_info[idx].color_left;
  661. cc.height = size.height;
  662. conn_input_cache.push_back(cc);
  663. }
  664. if (slot_info[idx].enable_right) {
  665. ConnCache cc;
  666. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  667. cc.type = slot_info[idx].type_right;
  668. cc.color = slot_info[idx].color_right;
  669. cc.height = size.height;
  670. conn_output_cache.push_back(cc);
  671. }
  672. }
  673. vofs += sep;
  674. vofs += size.y;
  675. idx++;
  676. }
  677. connpos_dirty = false;
  678. }
  679. int GraphNode::get_connection_input_count() {
  680. if (connpos_dirty) {
  681. _connpos_update();
  682. }
  683. return conn_input_cache.size();
  684. }
  685. int GraphNode::get_connection_input_height(int p_idx) {
  686. if (connpos_dirty) {
  687. _connpos_update();
  688. }
  689. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  690. return conn_input_cache[p_idx].height;
  691. }
  692. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  693. if (connpos_dirty) {
  694. _connpos_update();
  695. }
  696. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  697. Vector2 pos = conn_input_cache[p_idx].pos;
  698. pos.x *= get_scale().x;
  699. pos.y *= get_scale().y;
  700. return pos;
  701. }
  702. int GraphNode::get_connection_input_type(int p_idx) {
  703. if (connpos_dirty) {
  704. _connpos_update();
  705. }
  706. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  707. return conn_input_cache[p_idx].type;
  708. }
  709. Color GraphNode::get_connection_input_color(int p_idx) {
  710. if (connpos_dirty) {
  711. _connpos_update();
  712. }
  713. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  714. return conn_input_cache[p_idx].color;
  715. }
  716. int GraphNode::get_connection_output_count() {
  717. if (connpos_dirty) {
  718. _connpos_update();
  719. }
  720. return conn_output_cache.size();
  721. }
  722. int GraphNode::get_connection_output_height(int p_idx) {
  723. if (connpos_dirty) {
  724. _connpos_update();
  725. }
  726. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  727. return conn_output_cache[p_idx].height;
  728. }
  729. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  730. if (connpos_dirty) {
  731. _connpos_update();
  732. }
  733. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  734. Vector2 pos = conn_output_cache[p_idx].pos;
  735. pos.x *= get_scale().x;
  736. pos.y *= get_scale().y;
  737. return pos;
  738. }
  739. int GraphNode::get_connection_output_type(int p_idx) {
  740. if (connpos_dirty) {
  741. _connpos_update();
  742. }
  743. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  744. return conn_output_cache[p_idx].type;
  745. }
  746. Color GraphNode::get_connection_output_color(int p_idx) {
  747. if (connpos_dirty) {
  748. _connpos_update();
  749. }
  750. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  751. return conn_output_cache[p_idx].color;
  752. }
  753. void GraphNode::gui_input(const Ref<InputEvent> &p_ev) {
  754. ERR_FAIL_COND(p_ev.is_null());
  755. Ref<InputEventMouseButton> mb = p_ev;
  756. if (mb.is_valid()) {
  757. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  758. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  759. Vector2 mpos = mb->get_position();
  760. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  761. //send focus to parent
  762. get_parent_control()->grab_focus();
  763. emit_signal(SNAME("close_request"));
  764. accept_event();
  765. return;
  766. }
  767. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  768. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  769. resizing = true;
  770. resizing_from = mpos;
  771. resizing_from_size = get_size();
  772. accept_event();
  773. return;
  774. }
  775. emit_signal(SNAME("raise_request"));
  776. }
  777. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  778. resizing = false;
  779. }
  780. }
  781. Ref<InputEventMouseMotion> mm = p_ev;
  782. if (resizing && mm.is_valid()) {
  783. Vector2 mpos = mm->get_position();
  784. Vector2 diff = mpos - resizing_from;
  785. emit_signal(SNAME("resize_request"), resizing_from_size + diff);
  786. }
  787. }
  788. void GraphNode::set_overlay(Overlay p_overlay) {
  789. if (overlay == p_overlay) {
  790. return;
  791. }
  792. overlay = p_overlay;
  793. update();
  794. }
  795. GraphNode::Overlay GraphNode::get_overlay() const {
  796. return overlay;
  797. }
  798. void GraphNode::set_comment(bool p_enable) {
  799. if (comment == p_enable) {
  800. return;
  801. }
  802. comment = p_enable;
  803. update();
  804. }
  805. bool GraphNode::is_comment() const {
  806. return comment;
  807. }
  808. void GraphNode::set_resizable(bool p_enable) {
  809. if (resizable == p_enable) {
  810. return;
  811. }
  812. resizable = p_enable;
  813. update();
  814. }
  815. bool GraphNode::is_resizable() const {
  816. return resizable;
  817. }
  818. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  819. Vector<int> flags;
  820. flags.append(SIZE_FILL);
  821. flags.append(SIZE_SHRINK_BEGIN);
  822. flags.append(SIZE_SHRINK_CENTER);
  823. flags.append(SIZE_SHRINK_END);
  824. return flags;
  825. }
  826. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  827. Vector<int> flags;
  828. flags.append(SIZE_FILL);
  829. flags.append(SIZE_EXPAND);
  830. flags.append(SIZE_SHRINK_BEGIN);
  831. flags.append(SIZE_SHRINK_CENTER);
  832. flags.append(SIZE_SHRINK_END);
  833. return flags;
  834. }
  835. void GraphNode::_bind_methods() {
  836. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  837. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  838. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &GraphNode::set_text_direction);
  839. ClassDB::bind_method(D_METHOD("get_text_direction"), &GraphNode::get_text_direction);
  840. ClassDB::bind_method(D_METHOD("set_language", "language"), &GraphNode::set_language);
  841. ClassDB::bind_method(D_METHOD("get_language"), &GraphNode::get_language);
  842. ClassDB::bind_method(D_METHOD("set_slot", "idx", "enable_left", "type_left", "color_left", "enable_right", "type_right", "color_right", "custom_left", "custom_right", "enable"), &GraphNode::set_slot, DEFVAL(Ref<Texture2D>()), DEFVAL(Ref<Texture2D>()), DEFVAL(true));
  843. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  844. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  845. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  846. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "idx", "enable_left"), &GraphNode::set_slot_enabled_left);
  847. ClassDB::bind_method(D_METHOD("set_slot_type_left", "idx", "type_left"), &GraphNode::set_slot_type_left);
  848. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  849. ClassDB::bind_method(D_METHOD("set_slot_color_left", "idx", "color_left"), &GraphNode::set_slot_color_left);
  850. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  851. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  852. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "idx", "enable_right"), &GraphNode::set_slot_enabled_right);
  853. ClassDB::bind_method(D_METHOD("set_slot_type_right", "idx", "type_right"), &GraphNode::set_slot_type_right);
  854. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  855. ClassDB::bind_method(D_METHOD("set_slot_color_right", "idx", "color_right"), &GraphNode::set_slot_color_right);
  856. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  857. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "idx"), &GraphNode::is_slot_draw_stylebox);
  858. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "idx", "draw_stylebox"), &GraphNode::set_slot_draw_stylebox);
  859. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &GraphNode::set_position_offset);
  860. ClassDB::bind_method(D_METHOD("get_position_offset"), &GraphNode::get_position_offset);
  861. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  862. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  863. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  864. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  865. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  866. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  867. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  868. ClassDB::bind_method(D_METHOD("get_connection_input_height", "idx"), &GraphNode::get_connection_input_height);
  869. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  870. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  871. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  872. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  873. ClassDB::bind_method(D_METHOD("get_connection_output_height", "idx"), &GraphNode::get_connection_output_height);
  874. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  875. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  876. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  877. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  878. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  879. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  880. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  881. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  882. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_position_offset", "get_position_offset");
  883. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  884. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  885. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  886. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  887. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  888. ADD_GROUP("BiDi", "");
  889. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  890. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  891. ADD_GROUP("", "");
  892. ADD_SIGNAL(MethodInfo("position_offset_changed"));
  893. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  894. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  895. ADD_SIGNAL(MethodInfo("raise_request"));
  896. ADD_SIGNAL(MethodInfo("close_request"));
  897. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  898. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  899. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  900. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  901. }
  902. GraphNode::GraphNode() {
  903. title_buf.instantiate();
  904. set_mouse_filter(MOUSE_FILTER_STOP);
  905. }