2
0

graph_node.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*************************************************************************/
  2. /* graph_node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "graph_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. double 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 ((double)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 {
  88. return false;
  89. }
  90. 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);
  91. update();
  92. return true;
  93. }
  94. bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const {
  95. String str = p_name;
  96. if (str.begins_with("opentype_features/")) {
  97. String name = str.get_slicec('/', 1);
  98. int32_t tag = TS->name_to_tag(name);
  99. if (opentype_features.has(tag)) {
  100. r_ret = opentype_features[tag];
  101. return true;
  102. } else {
  103. r_ret = -1;
  104. return true;
  105. }
  106. }
  107. if (!str.begins_with("slot/")) {
  108. return false;
  109. }
  110. int idx = str.get_slice("/", 1).to_int();
  111. String what = str.get_slice("/", 2);
  112. Slot si;
  113. if (slot_info.has(idx)) {
  114. si = slot_info[idx];
  115. }
  116. if (what == "left_enabled") {
  117. r_ret = si.enable_left;
  118. } else if (what == "left_type") {
  119. r_ret = si.type_left;
  120. } else if (what == "left_color") {
  121. r_ret = si.color_left;
  122. } else if (what == "left_icon") {
  123. r_ret = si.custom_slot_left;
  124. } else if (what == "right_enabled") {
  125. r_ret = si.enable_right;
  126. } else if (what == "right_type") {
  127. r_ret = si.type_right;
  128. } else if (what == "right_color") {
  129. r_ret = si.color_right;
  130. } else if (what == "right_icon") {
  131. r_ret = si.custom_slot_right;
  132. } else {
  133. return false;
  134. }
  135. return true;
  136. }
  137. void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const {
  138. for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
  139. String name = TS->tag_to_name(*ftr);
  140. p_list->push_back(PropertyInfo(Variant::FLOAT, "opentype_features/" + name));
  141. }
  142. p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  143. int idx = 0;
  144. for (int i = 0; i < get_child_count(); i++) {
  145. Control *c = Object::cast_to<Control>(get_child(i));
  146. if (!c || c->is_set_as_top_level()) {
  147. continue;
  148. }
  149. String base = "slot/" + itos(idx) + "/";
  150. p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled"));
  151. p_list->push_back(PropertyInfo(Variant::INT, base + "left_type"));
  152. p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color"));
  153. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "left_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  154. p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled"));
  155. p_list->push_back(PropertyInfo(Variant::INT, base + "right_type"));
  156. p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color"));
  157. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  158. idx++;
  159. }
  160. }
  161. void GraphNode::_resort() {
  162. /** First pass, determine minimum size AND amount of stretchable elements */
  163. Size2i new_size = get_size();
  164. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  165. int sep = get_theme_constant(SNAME("separation"));
  166. bool first = true;
  167. int children_count = 0;
  168. int stretch_min = 0;
  169. int stretch_avail = 0;
  170. float stretch_ratio_total = 0;
  171. Map<Control *, _MinSizeCache> min_size_cache;
  172. for (int i = 0; i < get_child_count(); i++) {
  173. Control *c = Object::cast_to<Control>(get_child(i));
  174. if (!c || !c->is_visible_in_tree()) {
  175. continue;
  176. }
  177. if (c->is_set_as_top_level()) {
  178. continue;
  179. }
  180. Size2i size = c->get_combined_minimum_size();
  181. _MinSizeCache msc;
  182. stretch_min += size.height;
  183. msc.min_size = size.height;
  184. msc.will_stretch = c->get_v_size_flags() & SIZE_EXPAND;
  185. if (msc.will_stretch) {
  186. stretch_avail += msc.min_size;
  187. stretch_ratio_total += c->get_stretch_ratio();
  188. }
  189. msc.final_size = msc.min_size;
  190. min_size_cache[c] = msc;
  191. children_count++;
  192. }
  193. if (children_count == 0) {
  194. return;
  195. }
  196. int stretch_max = new_size.height - (children_count - 1) * sep;
  197. int stretch_diff = stretch_max - stretch_min;
  198. if (stretch_diff < 0) {
  199. //avoid negative stretch space
  200. stretch_diff = 0;
  201. }
  202. stretch_avail += stretch_diff - sb->get_margin(SIDE_BOTTOM) - sb->get_margin(SIDE_TOP); //available stretch space.
  203. /** Second, pass successively to discard elements that can't be stretched, this will run while stretchable
  204. elements exist */
  205. while (stretch_ratio_total > 0) { // first of all, don't even be here if no stretchable objects exist
  206. bool refit_successful = true; //assume refit-test will go well
  207. for (int i = 0; i < get_child_count(); i++) {
  208. Control *c = Object::cast_to<Control>(get_child(i));
  209. if (!c || !c->is_visible_in_tree()) {
  210. continue;
  211. }
  212. if (c->is_set_as_top_level()) {
  213. continue;
  214. }
  215. ERR_FAIL_COND(!min_size_cache.has(c));
  216. _MinSizeCache &msc = min_size_cache[c];
  217. if (msc.will_stretch) { //wants to stretch
  218. //let's see if it can really stretch
  219. int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  220. if (final_pixel_size < msc.min_size) {
  221. //if available stretching area is too small for widget,
  222. //then remove it from stretching area
  223. msc.will_stretch = false;
  224. stretch_ratio_total -= c->get_stretch_ratio();
  225. refit_successful = false;
  226. stretch_avail -= msc.min_size;
  227. msc.final_size = msc.min_size;
  228. break;
  229. } else {
  230. msc.final_size = final_pixel_size;
  231. }
  232. }
  233. }
  234. if (refit_successful) { //uf refit went well, break
  235. break;
  236. }
  237. }
  238. /** Final pass, draw and stretch elements **/
  239. int ofs = sb->get_margin(SIDE_TOP);
  240. first = true;
  241. int idx = 0;
  242. cache_y.clear();
  243. int w = new_size.width - sb->get_minimum_size().x;
  244. for (int i = 0; i < get_child_count(); i++) {
  245. Control *c = Object::cast_to<Control>(get_child(i));
  246. if (!c || !c->is_visible_in_tree()) {
  247. continue;
  248. }
  249. if (c->is_set_as_top_level()) {
  250. continue;
  251. }
  252. _MinSizeCache &msc = min_size_cache[c];
  253. if (first) {
  254. first = false;
  255. } else {
  256. ofs += sep;
  257. }
  258. int from = ofs;
  259. int to = ofs + msc.final_size;
  260. if (msc.will_stretch && idx == children_count - 1) {
  261. //adjust so the last one always fits perfect
  262. //compensating for numerical imprecision
  263. to = new_size.height - sb->get_margin(SIDE_BOTTOM);
  264. }
  265. int size = to - from;
  266. Rect2 rect(sb->get_margin(SIDE_LEFT), from, w, size);
  267. fit_child_in_rect(c, rect);
  268. cache_y.push_back(from - sb->get_margin(SIDE_TOP) + size * 0.5);
  269. ofs = to;
  270. idx++;
  271. }
  272. update();
  273. connpos_dirty = true;
  274. }
  275. bool GraphNode::has_point(const Point2 &p_point) const {
  276. if (comment) {
  277. Ref<StyleBox> comment = get_theme_stylebox(SNAME("comment"));
  278. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  279. if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
  280. return true;
  281. }
  282. if (Rect2(0, 0, get_size().width, comment->get_margin(SIDE_TOP)).has_point(p_point)) {
  283. return true;
  284. }
  285. return false;
  286. } else {
  287. return Control::has_point(p_point);
  288. }
  289. }
  290. void GraphNode::_notification(int p_what) {
  291. switch (p_what) {
  292. case NOTIFICATION_DRAW: {
  293. Ref<StyleBox> sb;
  294. if (comment) {
  295. sb = get_theme_stylebox(selected ? "commentfocus" : "comment");
  296. } else {
  297. sb = get_theme_stylebox(selected ? "selectedframe" : "frame");
  298. }
  299. //sb=sb->duplicate();
  300. //sb->call("set_modulate",modulate);
  301. Ref<Texture2D> port = get_theme_icon(SNAME("port"));
  302. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  303. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  304. int close_offset = get_theme_constant(SNAME("close_offset"));
  305. int close_h_offset = get_theme_constant(SNAME("close_h_offset"));
  306. Color close_color = get_theme_color(SNAME("close_color"));
  307. Color resizer_color = get_theme_color(SNAME("resizer_color"));
  308. int title_offset = get_theme_constant(SNAME("title_offset"));
  309. int title_h_offset = get_theme_constant(SNAME("title_h_offset"));
  310. Color title_color = get_theme_color(SNAME("title_color"));
  311. Point2i icofs = -port->get_size() * 0.5;
  312. int edgeofs = get_theme_constant(SNAME("port_offset"));
  313. icofs.y += sb->get_margin(SIDE_TOP);
  314. draw_style_box(sb, Rect2(Point2(), get_size()));
  315. switch (overlay) {
  316. case OVERLAY_DISABLED: {
  317. } break;
  318. case OVERLAY_BREAKPOINT: {
  319. draw_style_box(get_theme_stylebox(SNAME("breakpoint")), Rect2(Point2(), get_size()));
  320. } break;
  321. case OVERLAY_POSITION: {
  322. draw_style_box(get_theme_stylebox(SNAME("position")), Rect2(Point2(), get_size()));
  323. } break;
  324. }
  325. int w = get_size().width - sb->get_minimum_size().x;
  326. if (show_close) {
  327. w -= close->get_width();
  328. }
  329. title_buf->set_width(w);
  330. title_buf->draw(get_canvas_item(), Point2(sb->get_margin(SIDE_LEFT) + title_h_offset, -title_buf->get_size().y + title_offset), title_color);
  331. if (show_close) {
  332. Vector2 cpos = Point2(w + sb->get_margin(SIDE_LEFT) + close_h_offset, -close->get_height() + close_offset);
  333. draw_texture(close, cpos, close_color);
  334. close_rect.position = cpos;
  335. close_rect.size = close->get_size();
  336. } else {
  337. close_rect = Rect2();
  338. }
  339. for (const KeyValue<int, Slot> &E : slot_info) {
  340. if (E.key < 0 || E.key >= cache_y.size()) {
  341. continue;
  342. }
  343. if (!slot_info.has(E.key)) {
  344. continue;
  345. }
  346. const Slot &s = slot_info[E.key];
  347. //left
  348. if (s.enable_left) {
  349. Ref<Texture2D> p = port;
  350. if (s.custom_slot_left.is_valid()) {
  351. p = s.custom_slot_left;
  352. }
  353. p->draw(get_canvas_item(), icofs + Point2(edgeofs, cache_y[E.key]), s.color_left);
  354. }
  355. if (s.enable_right) {
  356. Ref<Texture2D> p = port;
  357. if (s.custom_slot_right.is_valid()) {
  358. p = s.custom_slot_right;
  359. }
  360. p->draw(get_canvas_item(), icofs + Point2(get_size().x - edgeofs, cache_y[E.key]), s.color_right);
  361. }
  362. }
  363. if (resizable) {
  364. draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
  365. }
  366. } break;
  367. case NOTIFICATION_SORT_CHILDREN: {
  368. _resort();
  369. } break;
  370. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  371. case NOTIFICATION_TRANSLATION_CHANGED:
  372. case NOTIFICATION_THEME_CHANGED: {
  373. _shape();
  374. update_minimum_size();
  375. update();
  376. } break;
  377. }
  378. }
  379. void GraphNode::_shape() {
  380. Ref<Font> font = get_theme_font(SNAME("title_font"));
  381. int font_size = get_theme_font_size(SNAME("title_font_size"));
  382. title_buf->clear();
  383. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  384. title_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  385. } else {
  386. title_buf->set_direction((TextServer::Direction)text_direction);
  387. }
  388. title_buf->add_string(title, font, font_size, opentype_features, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
  389. }
  390. #ifdef TOOLS_ENABLED
  391. void GraphNode::_edit_set_position(const Point2 &p_position) {
  392. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  393. if (graph) {
  394. Point2 offset = (p_position + graph->get_scroll_ofs()) * graph->get_zoom();
  395. set_position_offset(offset);
  396. }
  397. set_position(p_position);
  398. }
  399. void GraphNode::_validate_property(PropertyInfo &property) const {
  400. Control::_validate_property(property);
  401. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  402. if (graph) {
  403. if (property.name == "rect_position") {
  404. property.usage |= PROPERTY_USAGE_READ_ONLY;
  405. }
  406. }
  407. }
  408. #endif
  409. 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) {
  410. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set slot with p_idx (%d) lesser than zero.", p_idx));
  411. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) &&
  412. !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) &&
  413. !p_custom_left.is_valid() && !p_custom_right.is_valid()) {
  414. slot_info.erase(p_idx);
  415. return;
  416. }
  417. Slot s;
  418. s.enable_left = p_enable_left;
  419. s.type_left = p_type_left;
  420. s.color_left = p_color_left;
  421. s.enable_right = p_enable_right;
  422. s.type_right = p_type_right;
  423. s.color_right = p_color_right;
  424. s.custom_slot_left = p_custom_left;
  425. s.custom_slot_right = p_custom_right;
  426. slot_info[p_idx] = s;
  427. update();
  428. connpos_dirty = true;
  429. emit_signal(SNAME("slot_updated"), p_idx);
  430. }
  431. void GraphNode::clear_slot(int p_idx) {
  432. slot_info.erase(p_idx);
  433. update();
  434. connpos_dirty = true;
  435. }
  436. void GraphNode::clear_all_slots() {
  437. slot_info.clear();
  438. update();
  439. connpos_dirty = true;
  440. }
  441. bool GraphNode::is_slot_enabled_left(int p_idx) const {
  442. if (!slot_info.has(p_idx)) {
  443. return false;
  444. }
  445. return slot_info[p_idx].enable_left;
  446. }
  447. void GraphNode::set_slot_enabled_left(int p_idx, bool p_enable_left) {
  448. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_left for the slot with p_idx (%d) lesser than zero.", p_idx));
  449. slot_info[p_idx].enable_left = p_enable_left;
  450. update();
  451. connpos_dirty = true;
  452. emit_signal(SNAME("slot_updated"), p_idx);
  453. }
  454. void GraphNode::set_slot_type_left(int p_idx, int p_type_left) {
  455. 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));
  456. slot_info[p_idx].type_left = p_type_left;
  457. update();
  458. connpos_dirty = true;
  459. emit_signal(SNAME("slot_updated"), p_idx);
  460. }
  461. int GraphNode::get_slot_type_left(int p_idx) const {
  462. if (!slot_info.has(p_idx)) {
  463. return 0;
  464. }
  465. return slot_info[p_idx].type_left;
  466. }
  467. void GraphNode::set_slot_color_left(int p_idx, const Color &p_color_left) {
  468. 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));
  469. slot_info[p_idx].color_left = p_color_left;
  470. update();
  471. connpos_dirty = true;
  472. emit_signal(SNAME("slot_updated"), p_idx);
  473. }
  474. Color GraphNode::get_slot_color_left(int p_idx) const {
  475. if (!slot_info.has(p_idx)) {
  476. return Color(1, 1, 1, 1);
  477. }
  478. return slot_info[p_idx].color_left;
  479. }
  480. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  481. if (!slot_info.has(p_idx)) {
  482. return false;
  483. }
  484. return slot_info[p_idx].enable_right;
  485. }
  486. void GraphNode::set_slot_enabled_right(int p_idx, bool p_enable_right) {
  487. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_right for the slot with p_idx (%d) lesser than zero.", p_idx));
  488. slot_info[p_idx].enable_right = p_enable_right;
  489. update();
  490. connpos_dirty = true;
  491. emit_signal(SNAME("slot_updated"), p_idx);
  492. }
  493. void GraphNode::set_slot_type_right(int p_idx, int p_type_right) {
  494. 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));
  495. slot_info[p_idx].type_right = p_type_right;
  496. update();
  497. connpos_dirty = true;
  498. emit_signal(SNAME("slot_updated"), p_idx);
  499. }
  500. int GraphNode::get_slot_type_right(int p_idx) const {
  501. if (!slot_info.has(p_idx)) {
  502. return 0;
  503. }
  504. return slot_info[p_idx].type_right;
  505. }
  506. void GraphNode::set_slot_color_right(int p_idx, const Color &p_color_right) {
  507. 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));
  508. slot_info[p_idx].color_right = p_color_right;
  509. update();
  510. connpos_dirty = true;
  511. emit_signal(SNAME("slot_updated"), p_idx);
  512. }
  513. Color GraphNode::get_slot_color_right(int p_idx) const {
  514. if (!slot_info.has(p_idx)) {
  515. return Color(1, 1, 1, 1);
  516. }
  517. return slot_info[p_idx].color_right;
  518. }
  519. Size2 GraphNode::get_minimum_size() const {
  520. int sep = get_theme_constant(SNAME("separation"));
  521. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  522. bool first = true;
  523. Size2 minsize;
  524. minsize.x = title_buf->get_size().x;
  525. if (show_close) {
  526. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  527. minsize.x += sep + close->get_width();
  528. }
  529. for (int i = 0; i < get_child_count(); i++) {
  530. Control *c = Object::cast_to<Control>(get_child(i));
  531. if (!c) {
  532. continue;
  533. }
  534. if (c->is_set_as_top_level()) {
  535. continue;
  536. }
  537. Size2i size = c->get_combined_minimum_size();
  538. minsize.y += size.y;
  539. minsize.x = MAX(minsize.x, size.x);
  540. if (first) {
  541. first = false;
  542. } else {
  543. minsize.y += sep;
  544. }
  545. }
  546. return minsize + sb->get_minimum_size();
  547. }
  548. void GraphNode::set_title(const String &p_title) {
  549. if (title == p_title) {
  550. return;
  551. }
  552. title = p_title;
  553. _shape();
  554. update();
  555. update_minimum_size();
  556. }
  557. String GraphNode::get_title() const {
  558. return title;
  559. }
  560. void GraphNode::set_text_direction(Control::TextDirection p_text_direction) {
  561. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  562. if (text_direction != p_text_direction) {
  563. text_direction = p_text_direction;
  564. _shape();
  565. update();
  566. }
  567. }
  568. Control::TextDirection GraphNode::get_text_direction() const {
  569. return text_direction;
  570. }
  571. void GraphNode::clear_opentype_features() {
  572. opentype_features.clear();
  573. _shape();
  574. update();
  575. }
  576. void GraphNode::set_opentype_feature(const String &p_name, int p_value) {
  577. int32_t tag = TS->name_to_tag(p_name);
  578. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  579. opentype_features[tag] = p_value;
  580. _shape();
  581. update();
  582. }
  583. }
  584. int GraphNode::get_opentype_feature(const String &p_name) const {
  585. int32_t tag = TS->name_to_tag(p_name);
  586. if (!opentype_features.has(tag)) {
  587. return -1;
  588. }
  589. return opentype_features[tag];
  590. }
  591. void GraphNode::set_language(const String &p_language) {
  592. if (language != p_language) {
  593. language = p_language;
  594. _shape();
  595. update();
  596. }
  597. }
  598. String GraphNode::get_language() const {
  599. return language;
  600. }
  601. void GraphNode::set_position_offset(const Vector2 &p_offset) {
  602. position_offset = p_offset;
  603. emit_signal(SNAME("position_offset_changed"));
  604. update();
  605. }
  606. Vector2 GraphNode::get_position_offset() const {
  607. return position_offset;
  608. }
  609. void GraphNode::set_selected(bool p_selected) {
  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. show_close = p_enable;
  628. update();
  629. }
  630. bool GraphNode::is_close_button_visible() const {
  631. return show_close;
  632. }
  633. void GraphNode::_connpos_update() {
  634. int edgeofs = get_theme_constant(SNAME("port_offset"));
  635. int sep = get_theme_constant(SNAME("separation"));
  636. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  637. conn_input_cache.clear();
  638. conn_output_cache.clear();
  639. int vofs = 0;
  640. int idx = 0;
  641. for (int i = 0; i < get_child_count(); i++) {
  642. Control *c = Object::cast_to<Control>(get_child(i));
  643. if (!c) {
  644. continue;
  645. }
  646. if (c->is_set_as_top_level()) {
  647. continue;
  648. }
  649. Size2i size = c->get_rect().size;
  650. int y = sb->get_margin(SIDE_TOP) + vofs;
  651. int h = size.y;
  652. if (slot_info.has(idx)) {
  653. if (slot_info[idx].enable_left) {
  654. ConnCache cc;
  655. cc.pos = Point2i(edgeofs, y + h / 2);
  656. cc.type = slot_info[idx].type_left;
  657. cc.color = slot_info[idx].color_left;
  658. conn_input_cache.push_back(cc);
  659. }
  660. if (slot_info[idx].enable_right) {
  661. ConnCache cc;
  662. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  663. cc.type = slot_info[idx].type_right;
  664. cc.color = slot_info[idx].color_right;
  665. conn_output_cache.push_back(cc);
  666. }
  667. }
  668. vofs += sep;
  669. vofs += size.y;
  670. idx++;
  671. }
  672. connpos_dirty = false;
  673. }
  674. int GraphNode::get_connection_input_count() {
  675. if (connpos_dirty) {
  676. _connpos_update();
  677. }
  678. return conn_input_cache.size();
  679. }
  680. int GraphNode::get_connection_output_count() {
  681. if (connpos_dirty) {
  682. _connpos_update();
  683. }
  684. return conn_output_cache.size();
  685. }
  686. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  687. if (connpos_dirty) {
  688. _connpos_update();
  689. }
  690. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  691. Vector2 pos = conn_input_cache[p_idx].pos;
  692. pos.x *= get_scale().x;
  693. pos.y *= get_scale().y;
  694. return pos;
  695. }
  696. int GraphNode::get_connection_input_type(int p_idx) {
  697. if (connpos_dirty) {
  698. _connpos_update();
  699. }
  700. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  701. return conn_input_cache[p_idx].type;
  702. }
  703. Color GraphNode::get_connection_input_color(int p_idx) {
  704. if (connpos_dirty) {
  705. _connpos_update();
  706. }
  707. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  708. return conn_input_cache[p_idx].color;
  709. }
  710. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  711. if (connpos_dirty) {
  712. _connpos_update();
  713. }
  714. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  715. Vector2 pos = conn_output_cache[p_idx].pos;
  716. pos.x *= get_scale().x;
  717. pos.y *= get_scale().y;
  718. return pos;
  719. }
  720. int GraphNode::get_connection_output_type(int p_idx) {
  721. if (connpos_dirty) {
  722. _connpos_update();
  723. }
  724. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  725. return conn_output_cache[p_idx].type;
  726. }
  727. Color GraphNode::get_connection_output_color(int p_idx) {
  728. if (connpos_dirty) {
  729. _connpos_update();
  730. }
  731. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  732. return conn_output_cache[p_idx].color;
  733. }
  734. void GraphNode::gui_input(const Ref<InputEvent> &p_ev) {
  735. ERR_FAIL_COND(p_ev.is_null());
  736. Ref<InputEventMouseButton> mb = p_ev;
  737. if (mb.is_valid()) {
  738. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  739. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  740. Vector2 mpos = mb->get_position();
  741. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  742. //send focus to parent
  743. get_parent_control()->grab_focus();
  744. emit_signal(SNAME("close_request"));
  745. accept_event();
  746. return;
  747. }
  748. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  749. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  750. resizing = true;
  751. resizing_from = mpos;
  752. resizing_from_size = get_size();
  753. accept_event();
  754. return;
  755. }
  756. emit_signal(SNAME("raise_request"));
  757. }
  758. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  759. resizing = false;
  760. }
  761. }
  762. Ref<InputEventMouseMotion> mm = p_ev;
  763. if (resizing && mm.is_valid()) {
  764. Vector2 mpos = mm->get_position();
  765. Vector2 diff = mpos - resizing_from;
  766. emit_signal(SNAME("resize_request"), resizing_from_size + diff);
  767. }
  768. }
  769. void GraphNode::set_overlay(Overlay p_overlay) {
  770. overlay = p_overlay;
  771. update();
  772. }
  773. GraphNode::Overlay GraphNode::get_overlay() const {
  774. return overlay;
  775. }
  776. void GraphNode::set_comment(bool p_enable) {
  777. comment = p_enable;
  778. update();
  779. }
  780. bool GraphNode::is_comment() const {
  781. return comment;
  782. }
  783. void GraphNode::set_resizable(bool p_enable) {
  784. resizable = p_enable;
  785. update();
  786. }
  787. bool GraphNode::is_resizable() const {
  788. return resizable;
  789. }
  790. void GraphNode::_bind_methods() {
  791. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  792. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  793. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &GraphNode::set_text_direction);
  794. ClassDB::bind_method(D_METHOD("get_text_direction"), &GraphNode::get_text_direction);
  795. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &GraphNode::set_opentype_feature);
  796. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &GraphNode::get_opentype_feature);
  797. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &GraphNode::clear_opentype_features);
  798. ClassDB::bind_method(D_METHOD("set_language", "language"), &GraphNode::set_language);
  799. ClassDB::bind_method(D_METHOD("get_language"), &GraphNode::get_language);
  800. ClassDB::bind_method(D_METHOD("set_slot", "idx", "enable_left", "type_left", "color_left", "enable_right", "type_right", "color_right", "custom_left", "custom_right"), &GraphNode::set_slot, DEFVAL(Ref<Texture2D>()), DEFVAL(Ref<Texture2D>()));
  801. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  802. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  803. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  804. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "idx", "enable_left"), &GraphNode::set_slot_enabled_left);
  805. ClassDB::bind_method(D_METHOD("set_slot_type_left", "idx", "type_left"), &GraphNode::set_slot_type_left);
  806. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  807. ClassDB::bind_method(D_METHOD("set_slot_color_left", "idx", "color_left"), &GraphNode::set_slot_color_left);
  808. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  809. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  810. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "idx", "enable_right"), &GraphNode::set_slot_enabled_right);
  811. ClassDB::bind_method(D_METHOD("set_slot_type_right", "idx", "type_right"), &GraphNode::set_slot_type_right);
  812. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  813. ClassDB::bind_method(D_METHOD("set_slot_color_right", "idx", "color_right"), &GraphNode::set_slot_color_right);
  814. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  815. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &GraphNode::set_position_offset);
  816. ClassDB::bind_method(D_METHOD("get_position_offset"), &GraphNode::get_position_offset);
  817. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  818. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  819. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  820. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  821. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  822. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  823. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  824. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  825. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  826. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  827. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  828. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  829. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  830. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  831. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  832. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  833. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  834. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  835. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  836. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  837. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language"), "set_language", "get_language");
  838. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position_offset"), "set_position_offset", "get_position_offset");
  839. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  840. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  841. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  842. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  843. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  844. ADD_SIGNAL(MethodInfo("position_offset_changed"));
  845. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  846. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  847. ADD_SIGNAL(MethodInfo("raise_request"));
  848. ADD_SIGNAL(MethodInfo("close_request"));
  849. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  850. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  851. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  852. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  853. }
  854. GraphNode::GraphNode() {
  855. title_buf.instantiate();
  856. set_mouse_filter(MOUSE_FILTER_STOP);
  857. }