graph_node.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. conn_input_cache.push_back(cc);
  692. }
  693. if (slot_info[idx].enable_right) {
  694. ConnCache cc;
  695. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  696. cc.type = slot_info[idx].type_right;
  697. cc.color = slot_info[idx].color_right;
  698. conn_output_cache.push_back(cc);
  699. }
  700. }
  701. vofs += sep;
  702. vofs += size.y;
  703. idx++;
  704. }
  705. connpos_dirty = false;
  706. }
  707. int GraphNode::get_connection_input_count() {
  708. if (connpos_dirty) {
  709. _connpos_update();
  710. }
  711. return conn_input_cache.size();
  712. }
  713. int GraphNode::get_connection_output_count() {
  714. if (connpos_dirty) {
  715. _connpos_update();
  716. }
  717. return conn_output_cache.size();
  718. }
  719. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  720. if (connpos_dirty) {
  721. _connpos_update();
  722. }
  723. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  724. Vector2 pos = conn_input_cache[p_idx].pos;
  725. pos.x *= get_scale().x;
  726. pos.y *= get_scale().y;
  727. return pos;
  728. }
  729. int GraphNode::get_connection_input_type(int p_idx) {
  730. if (connpos_dirty) {
  731. _connpos_update();
  732. }
  733. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  734. return conn_input_cache[p_idx].type;
  735. }
  736. Color GraphNode::get_connection_input_color(int p_idx) {
  737. if (connpos_dirty) {
  738. _connpos_update();
  739. }
  740. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  741. return conn_input_cache[p_idx].color;
  742. }
  743. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  744. if (connpos_dirty) {
  745. _connpos_update();
  746. }
  747. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  748. Vector2 pos = conn_output_cache[p_idx].pos;
  749. pos.x *= get_scale().x;
  750. pos.y *= get_scale().y;
  751. return pos;
  752. }
  753. int GraphNode::get_connection_output_type(int p_idx) {
  754. if (connpos_dirty) {
  755. _connpos_update();
  756. }
  757. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  758. return conn_output_cache[p_idx].type;
  759. }
  760. Color GraphNode::get_connection_output_color(int p_idx) {
  761. if (connpos_dirty) {
  762. _connpos_update();
  763. }
  764. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  765. return conn_output_cache[p_idx].color;
  766. }
  767. void GraphNode::gui_input(const Ref<InputEvent> &p_ev) {
  768. ERR_FAIL_COND(p_ev.is_null());
  769. Ref<InputEventMouseButton> mb = p_ev;
  770. if (mb.is_valid()) {
  771. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  772. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  773. Vector2 mpos = mb->get_position();
  774. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  775. //send focus to parent
  776. get_parent_control()->grab_focus();
  777. emit_signal(SNAME("close_request"));
  778. accept_event();
  779. return;
  780. }
  781. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  782. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  783. resizing = true;
  784. resizing_from = mpos;
  785. resizing_from_size = get_size();
  786. accept_event();
  787. return;
  788. }
  789. emit_signal(SNAME("raise_request"));
  790. }
  791. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  792. resizing = false;
  793. }
  794. }
  795. Ref<InputEventMouseMotion> mm = p_ev;
  796. if (resizing && mm.is_valid()) {
  797. Vector2 mpos = mm->get_position();
  798. Vector2 diff = mpos - resizing_from;
  799. emit_signal(SNAME("resize_request"), resizing_from_size + diff);
  800. }
  801. }
  802. void GraphNode::set_overlay(Overlay p_overlay) {
  803. overlay = p_overlay;
  804. update();
  805. }
  806. GraphNode::Overlay GraphNode::get_overlay() const {
  807. return overlay;
  808. }
  809. void GraphNode::set_comment(bool p_enable) {
  810. comment = p_enable;
  811. update();
  812. }
  813. bool GraphNode::is_comment() const {
  814. return comment;
  815. }
  816. void GraphNode::set_resizable(bool p_enable) {
  817. resizable = p_enable;
  818. update();
  819. }
  820. bool GraphNode::is_resizable() const {
  821. return resizable;
  822. }
  823. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  824. Vector<int> flags;
  825. flags.append(SIZE_FILL);
  826. flags.append(SIZE_SHRINK_BEGIN);
  827. flags.append(SIZE_SHRINK_CENTER);
  828. flags.append(SIZE_SHRINK_END);
  829. return flags;
  830. }
  831. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  832. Vector<int> flags;
  833. flags.append(SIZE_FILL);
  834. flags.append(SIZE_EXPAND);
  835. flags.append(SIZE_SHRINK_BEGIN);
  836. flags.append(SIZE_SHRINK_CENTER);
  837. flags.append(SIZE_SHRINK_END);
  838. return flags;
  839. }
  840. void GraphNode::_bind_methods() {
  841. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  842. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  843. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &GraphNode::set_text_direction);
  844. ClassDB::bind_method(D_METHOD("get_text_direction"), &GraphNode::get_text_direction);
  845. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &GraphNode::set_opentype_feature);
  846. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &GraphNode::get_opentype_feature);
  847. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &GraphNode::clear_opentype_features);
  848. ClassDB::bind_method(D_METHOD("set_language", "language"), &GraphNode::set_language);
  849. ClassDB::bind_method(D_METHOD("get_language"), &GraphNode::get_language);
  850. 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));
  851. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  852. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  853. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  854. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "idx", "enable_left"), &GraphNode::set_slot_enabled_left);
  855. ClassDB::bind_method(D_METHOD("set_slot_type_left", "idx", "type_left"), &GraphNode::set_slot_type_left);
  856. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  857. ClassDB::bind_method(D_METHOD("set_slot_color_left", "idx", "color_left"), &GraphNode::set_slot_color_left);
  858. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  859. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  860. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "idx", "enable_right"), &GraphNode::set_slot_enabled_right);
  861. ClassDB::bind_method(D_METHOD("set_slot_type_right", "idx", "type_right"), &GraphNode::set_slot_type_right);
  862. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  863. ClassDB::bind_method(D_METHOD("set_slot_color_right", "idx", "color_right"), &GraphNode::set_slot_color_right);
  864. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  865. ClassDB::bind_method(D_METHOD("is_slot_draw_stylebox", "idx"), &GraphNode::is_slot_draw_stylebox);
  866. ClassDB::bind_method(D_METHOD("set_slot_draw_stylebox", "idx", "draw_stylebox"), &GraphNode::set_slot_draw_stylebox);
  867. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &GraphNode::set_position_offset);
  868. ClassDB::bind_method(D_METHOD("get_position_offset"), &GraphNode::get_position_offset);
  869. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  870. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  871. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  872. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  873. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  874. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  875. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  876. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  877. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  878. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  879. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  880. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  881. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  882. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  883. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  884. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  885. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  886. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  887. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  888. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  889. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  890. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position_offset"), "set_position_offset", "get_position_offset");
  891. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  892. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  893. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  894. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  895. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  896. ADD_SIGNAL(MethodInfo("position_offset_changed"));
  897. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  898. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  899. ADD_SIGNAL(MethodInfo("raise_request"));
  900. ADD_SIGNAL(MethodInfo("close_request"));
  901. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  902. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  903. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  904. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  905. }
  906. GraphNode::GraphNode() {
  907. title_buf.instantiate();
  908. set_mouse_filter(MOUSE_FILTER_STOP);
  909. }