graph_node.cpp 25 KB

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