graph_node.cpp 25 KB

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