graph_node.cpp 28 KB

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