graph_node.cpp 35 KB

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