graph_node.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 successively 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_MSG(p_idx < 0, vformat("Cannot set slot with p_idx (%d) lesser than zero.", p_idx));
  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. void GraphNode::set_slot_enabled_left(int p_idx, bool p_enable_left) {
  426. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_left for the slot with p_idx (%d) lesser than zero.", p_idx));
  427. slot_info[p_idx].enable_left = p_enable_left;
  428. update();
  429. connpos_dirty = true;
  430. emit_signal("slot_updated", p_idx);
  431. }
  432. void GraphNode::set_slot_type_left(int p_idx, int p_type_left) {
  433. 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));
  434. slot_info[p_idx].type_left = p_type_left;
  435. update();
  436. connpos_dirty = true;
  437. emit_signal("slot_updated", p_idx);
  438. }
  439. int GraphNode::get_slot_type_left(int p_idx) const {
  440. if (!slot_info.has(p_idx)) {
  441. return 0;
  442. }
  443. return slot_info[p_idx].type_left;
  444. }
  445. void GraphNode::set_slot_color_left(int p_idx, const Color &p_color_left) {
  446. 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));
  447. slot_info[p_idx].color_left = p_color_left;
  448. update();
  449. connpos_dirty = true;
  450. emit_signal("slot_updated", p_idx);
  451. }
  452. Color GraphNode::get_slot_color_left(int p_idx) const {
  453. if (!slot_info.has(p_idx)) {
  454. return Color(1, 1, 1, 1);
  455. }
  456. return slot_info[p_idx].color_left;
  457. }
  458. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  459. if (!slot_info.has(p_idx)) {
  460. return false;
  461. }
  462. return slot_info[p_idx].enable_right;
  463. }
  464. void GraphNode::set_slot_enabled_right(int p_idx, bool p_enable_right) {
  465. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_right for the slot with p_idx (%d) lesser than zero.", p_idx));
  466. slot_info[p_idx].enable_right = p_enable_right;
  467. update();
  468. connpos_dirty = true;
  469. emit_signal("slot_updated", p_idx);
  470. }
  471. void GraphNode::set_slot_type_right(int p_idx, int p_type_right) {
  472. 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));
  473. slot_info[p_idx].type_right = p_type_right;
  474. update();
  475. connpos_dirty = true;
  476. emit_signal("slot_updated", p_idx);
  477. }
  478. int GraphNode::get_slot_type_right(int p_idx) const {
  479. if (!slot_info.has(p_idx)) {
  480. return 0;
  481. }
  482. return slot_info[p_idx].type_right;
  483. }
  484. void GraphNode::set_slot_color_right(int p_idx, const Color &p_color_right) {
  485. 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));
  486. slot_info[p_idx].color_right = p_color_right;
  487. update();
  488. connpos_dirty = true;
  489. emit_signal("slot_updated", p_idx);
  490. }
  491. Color GraphNode::get_slot_color_right(int p_idx) const {
  492. if (!slot_info.has(p_idx)) {
  493. return Color(1, 1, 1, 1);
  494. }
  495. return slot_info[p_idx].color_right;
  496. }
  497. Size2 GraphNode::get_minimum_size() const {
  498. int sep = get_theme_constant("separation");
  499. Ref<StyleBox> sb = get_theme_stylebox("frame");
  500. bool first = true;
  501. Size2 minsize;
  502. minsize.x = title_buf->get_size().x;
  503. if (show_close) {
  504. Ref<Texture2D> close = get_theme_icon("close");
  505. minsize.x += sep + close->get_width();
  506. }
  507. for (int i = 0; i < get_child_count(); i++) {
  508. Control *c = Object::cast_to<Control>(get_child(i));
  509. if (!c) {
  510. continue;
  511. }
  512. if (c->is_set_as_top_level()) {
  513. continue;
  514. }
  515. Size2i size = c->get_combined_minimum_size();
  516. minsize.y += size.y;
  517. minsize.x = MAX(minsize.x, size.x);
  518. if (first) {
  519. first = false;
  520. } else {
  521. minsize.y += sep;
  522. }
  523. }
  524. return minsize + sb->get_minimum_size();
  525. }
  526. void GraphNode::set_title(const String &p_title) {
  527. if (title == p_title) {
  528. return;
  529. }
  530. title = p_title;
  531. _shape();
  532. update();
  533. minimum_size_changed();
  534. }
  535. String GraphNode::get_title() const {
  536. return title;
  537. }
  538. void GraphNode::set_text_direction(Control::TextDirection p_text_direction) {
  539. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  540. if (text_direction != p_text_direction) {
  541. text_direction = p_text_direction;
  542. _shape();
  543. update();
  544. }
  545. }
  546. Control::TextDirection GraphNode::get_text_direction() const {
  547. return text_direction;
  548. }
  549. void GraphNode::clear_opentype_features() {
  550. opentype_features.clear();
  551. _shape();
  552. update();
  553. }
  554. void GraphNode::set_opentype_feature(const String &p_name, int p_value) {
  555. int32_t tag = TS->name_to_tag(p_name);
  556. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  557. opentype_features[tag] = p_value;
  558. _shape();
  559. update();
  560. }
  561. }
  562. int GraphNode::get_opentype_feature(const String &p_name) const {
  563. int32_t tag = TS->name_to_tag(p_name);
  564. if (!opentype_features.has(tag)) {
  565. return -1;
  566. }
  567. return opentype_features[tag];
  568. }
  569. void GraphNode::set_language(const String &p_language) {
  570. if (language != p_language) {
  571. language = p_language;
  572. _shape();
  573. update();
  574. }
  575. }
  576. String GraphNode::get_language() const {
  577. return language;
  578. }
  579. void GraphNode::set_position_offset(const Vector2 &p_offset) {
  580. position_offset = p_offset;
  581. emit_signal("position_offset_changed");
  582. update();
  583. }
  584. Vector2 GraphNode::get_position_offset() const {
  585. return position_offset;
  586. }
  587. void GraphNode::set_selected(bool p_selected) {
  588. selected = p_selected;
  589. update();
  590. }
  591. bool GraphNode::is_selected() {
  592. return selected;
  593. }
  594. void GraphNode::set_drag(bool p_drag) {
  595. if (p_drag) {
  596. drag_from = get_position_offset();
  597. } else {
  598. emit_signal("dragged", drag_from, get_position_offset()); //useful for undo/redo
  599. }
  600. }
  601. Vector2 GraphNode::get_drag_from() {
  602. return drag_from;
  603. }
  604. void GraphNode::set_show_close_button(bool p_enable) {
  605. show_close = p_enable;
  606. update();
  607. }
  608. bool GraphNode::is_close_button_visible() const {
  609. return show_close;
  610. }
  611. void GraphNode::_connpos_update() {
  612. int edgeofs = get_theme_constant("port_offset");
  613. int sep = get_theme_constant("separation");
  614. Ref<StyleBox> sb = get_theme_stylebox("frame");
  615. conn_input_cache.clear();
  616. conn_output_cache.clear();
  617. int vofs = 0;
  618. int idx = 0;
  619. for (int i = 0; i < get_child_count(); i++) {
  620. Control *c = Object::cast_to<Control>(get_child(i));
  621. if (!c) {
  622. continue;
  623. }
  624. if (c->is_set_as_top_level()) {
  625. continue;
  626. }
  627. Size2i size = c->get_rect().size;
  628. int y = sb->get_margin(SIDE_TOP) + vofs;
  629. int h = size.y;
  630. if (slot_info.has(idx)) {
  631. if (slot_info[idx].enable_left) {
  632. ConnCache cc;
  633. cc.pos = Point2i(edgeofs, y + h / 2);
  634. cc.type = slot_info[idx].type_left;
  635. cc.color = slot_info[idx].color_left;
  636. conn_input_cache.push_back(cc);
  637. }
  638. if (slot_info[idx].enable_right) {
  639. ConnCache cc;
  640. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  641. cc.type = slot_info[idx].type_right;
  642. cc.color = slot_info[idx].color_right;
  643. conn_output_cache.push_back(cc);
  644. }
  645. }
  646. vofs += sep;
  647. vofs += size.y;
  648. idx++;
  649. }
  650. connpos_dirty = false;
  651. }
  652. int GraphNode::get_connection_input_count() {
  653. if (connpos_dirty) {
  654. _connpos_update();
  655. }
  656. return conn_input_cache.size();
  657. }
  658. int GraphNode::get_connection_output_count() {
  659. if (connpos_dirty) {
  660. _connpos_update();
  661. }
  662. return conn_output_cache.size();
  663. }
  664. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  665. if (connpos_dirty) {
  666. _connpos_update();
  667. }
  668. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  669. Vector2 pos = conn_input_cache[p_idx].pos;
  670. pos.x *= get_scale().x;
  671. pos.y *= get_scale().y;
  672. return pos;
  673. }
  674. int GraphNode::get_connection_input_type(int p_idx) {
  675. if (connpos_dirty) {
  676. _connpos_update();
  677. }
  678. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  679. return conn_input_cache[p_idx].type;
  680. }
  681. Color GraphNode::get_connection_input_color(int p_idx) {
  682. if (connpos_dirty) {
  683. _connpos_update();
  684. }
  685. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  686. return conn_input_cache[p_idx].color;
  687. }
  688. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  689. if (connpos_dirty) {
  690. _connpos_update();
  691. }
  692. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  693. Vector2 pos = conn_output_cache[p_idx].pos;
  694. pos.x *= get_scale().x;
  695. pos.y *= get_scale().y;
  696. return pos;
  697. }
  698. int GraphNode::get_connection_output_type(int p_idx) {
  699. if (connpos_dirty) {
  700. _connpos_update();
  701. }
  702. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  703. return conn_output_cache[p_idx].type;
  704. }
  705. Color GraphNode::get_connection_output_color(int p_idx) {
  706. if (connpos_dirty) {
  707. _connpos_update();
  708. }
  709. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  710. return conn_output_cache[p_idx].color;
  711. }
  712. void GraphNode::_gui_input(const Ref<InputEvent> &p_ev) {
  713. ERR_FAIL_COND(p_ev.is_null());
  714. Ref<InputEventMouseButton> mb = p_ev;
  715. if (mb.is_valid()) {
  716. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  717. if (mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  718. Vector2 mpos = Vector2(mb->get_position().x, mb->get_position().y);
  719. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  720. //send focus to parent
  721. get_parent_control()->grab_focus();
  722. emit_signal("close_request");
  723. accept_event();
  724. return;
  725. }
  726. Ref<Texture2D> resizer = get_theme_icon("resizer");
  727. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  728. resizing = true;
  729. resizing_from = mpos;
  730. resizing_from_size = get_size();
  731. accept_event();
  732. return;
  733. }
  734. emit_signal("raise_request");
  735. }
  736. if (!mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_LEFT) {
  737. resizing = false;
  738. }
  739. }
  740. Ref<InputEventMouseMotion> mm = p_ev;
  741. if (resizing && mm.is_valid()) {
  742. Vector2 mpos = mm->get_position();
  743. Vector2 diff = mpos - resizing_from;
  744. emit_signal("resize_request", resizing_from_size + diff);
  745. }
  746. }
  747. void GraphNode::set_overlay(Overlay p_overlay) {
  748. overlay = p_overlay;
  749. update();
  750. }
  751. GraphNode::Overlay GraphNode::get_overlay() const {
  752. return overlay;
  753. }
  754. void GraphNode::set_comment(bool p_enable) {
  755. comment = p_enable;
  756. update();
  757. }
  758. bool GraphNode::is_comment() const {
  759. return comment;
  760. }
  761. void GraphNode::set_resizable(bool p_enable) {
  762. resizable = p_enable;
  763. update();
  764. }
  765. bool GraphNode::is_resizable() const {
  766. return resizable;
  767. }
  768. void GraphNode::_bind_methods() {
  769. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  770. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  771. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &GraphNode::set_text_direction);
  772. ClassDB::bind_method(D_METHOD("get_text_direction"), &GraphNode::get_text_direction);
  773. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &GraphNode::set_opentype_feature);
  774. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &GraphNode::get_opentype_feature);
  775. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &GraphNode::clear_opentype_features);
  776. ClassDB::bind_method(D_METHOD("set_language", "language"), &GraphNode::set_language);
  777. ClassDB::bind_method(D_METHOD("get_language"), &GraphNode::get_language);
  778. ClassDB::bind_method(D_METHOD("_gui_input"), &GraphNode::_gui_input);
  779. 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>()));
  780. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  781. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  782. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  783. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "idx", "enable_left"), &GraphNode::set_slot_enabled_left);
  784. ClassDB::bind_method(D_METHOD("set_slot_type_left", "idx", "type_left"), &GraphNode::set_slot_type_left);
  785. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  786. ClassDB::bind_method(D_METHOD("set_slot_color_left", "idx", "color_left"), &GraphNode::set_slot_color_left);
  787. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  788. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  789. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "idx", "enable_right"), &GraphNode::set_slot_enabled_right);
  790. ClassDB::bind_method(D_METHOD("set_slot_type_right", "idx", "type_right"), &GraphNode::set_slot_type_right);
  791. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  792. ClassDB::bind_method(D_METHOD("set_slot_color_right", "idx", "color_right"), &GraphNode::set_slot_color_right);
  793. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  794. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &GraphNode::set_position_offset);
  795. ClassDB::bind_method(D_METHOD("get_position_offset"), &GraphNode::get_position_offset);
  796. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  797. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  798. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  799. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  800. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  801. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  802. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  803. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  804. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  805. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  806. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  807. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  808. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  809. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  810. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  811. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  812. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  813. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  814. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  815. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  816. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language"), "set_language", "get_language");
  817. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position_offset"), "set_position_offset", "get_position_offset");
  818. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  819. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  820. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  821. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  822. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  823. ADD_SIGNAL(MethodInfo("position_offset_changed"));
  824. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  825. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  826. ADD_SIGNAL(MethodInfo("raise_request"));
  827. ADD_SIGNAL(MethodInfo("close_request"));
  828. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  829. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  830. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  831. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  832. }
  833. GraphNode::GraphNode() {
  834. title_buf.instantiate();
  835. set_mouse_filter(MOUSE_FILTER_STOP);
  836. }