graph_node.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /*************************************************************************/
  2. /* graph_node.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "graph_node.h"
  31. #include "core/string/translation.h"
  32. #ifdef TOOLS_ENABLED
  33. #include "graph_edit.h"
  34. #endif
  35. struct _MinSizeCache {
  36. int min_size;
  37. bool will_stretch;
  38. int final_size;
  39. };
  40. bool GraphNode::_set(const StringName &p_name, const Variant &p_value) {
  41. String str = p_name;
  42. if (str.begins_with("opentype_features/")) {
  43. String name = str.get_slicec('/', 1);
  44. int32_t tag = TS->name_to_tag(name);
  45. int value = p_value;
  46. if (value == -1) {
  47. if (opentype_features.has(tag)) {
  48. opentype_features.erase(tag);
  49. _shape();
  50. update();
  51. }
  52. } else {
  53. if (!opentype_features.has(tag) || (int)opentype_features[tag] != value) {
  54. opentype_features[tag] = value;
  55. _shape();
  56. update();
  57. }
  58. }
  59. notify_property_list_changed();
  60. return true;
  61. }
  62. if (!str.begins_with("slot/")) {
  63. return false;
  64. }
  65. int idx = str.get_slice("/", 1).to_int();
  66. String what = str.get_slice("/", 2);
  67. Slot si;
  68. if (slot_info.has(idx)) {
  69. si = slot_info[idx];
  70. }
  71. if (what == "left_enabled") {
  72. si.enable_left = p_value;
  73. } else if (what == "left_type") {
  74. si.type_left = p_value;
  75. } else if (what == "left_icon") {
  76. si.custom_slot_left = p_value;
  77. } else if (what == "left_color") {
  78. si.color_left = p_value;
  79. } else if (what == "right_enabled") {
  80. si.enable_right = p_value;
  81. } else if (what == "right_type") {
  82. si.type_right = p_value;
  83. } else if (what == "right_color") {
  84. si.color_right = p_value;
  85. } else if (what == "right_icon") {
  86. si.custom_slot_right = p_value;
  87. } else {
  88. return false;
  89. }
  90. 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);
  91. update();
  92. return true;
  93. }
  94. bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const {
  95. String str = p_name;
  96. if (str.begins_with("opentype_features/")) {
  97. String name = str.get_slicec('/', 1);
  98. int32_t tag = TS->name_to_tag(name);
  99. if (opentype_features.has(tag)) {
  100. r_ret = opentype_features[tag];
  101. return true;
  102. } else {
  103. r_ret = -1;
  104. return true;
  105. }
  106. }
  107. if (!str.begins_with("slot/")) {
  108. return false;
  109. }
  110. int idx = str.get_slice("/", 1).to_int();
  111. String what = str.get_slice("/", 2);
  112. Slot si;
  113. if (slot_info.has(idx)) {
  114. si = slot_info[idx];
  115. }
  116. if (what == "left_enabled") {
  117. r_ret = si.enable_left;
  118. } else if (what == "left_type") {
  119. r_ret = si.type_left;
  120. } else if (what == "left_color") {
  121. r_ret = si.color_left;
  122. } else if (what == "left_icon") {
  123. r_ret = si.custom_slot_left;
  124. } else if (what == "right_enabled") {
  125. r_ret = si.enable_right;
  126. } else if (what == "right_type") {
  127. r_ret = si.type_right;
  128. } else if (what == "right_color") {
  129. r_ret = si.color_right;
  130. } else if (what == "right_icon") {
  131. r_ret = si.custom_slot_right;
  132. } else {
  133. return false;
  134. }
  135. return true;
  136. }
  137. void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const {
  138. for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
  139. String name = TS->tag_to_name(*ftr);
  140. p_list->push_back(PropertyInfo(Variant::INT, "opentype_features/" + name));
  141. }
  142. p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  143. int idx = 0;
  144. for (int i = 0; i < get_child_count(); i++) {
  145. Control *c = Object::cast_to<Control>(get_child(i));
  146. if (!c || c->is_set_as_top_level()) {
  147. continue;
  148. }
  149. String base = "slot/" + itos(idx) + "/";
  150. p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled"));
  151. p_list->push_back(PropertyInfo(Variant::INT, base + "left_type"));
  152. p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color"));
  153. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "left_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  154. p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled"));
  155. p_list->push_back(PropertyInfo(Variant::INT, base + "right_type"));
  156. p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color"));
  157. p_list->push_back(PropertyInfo(Variant::OBJECT, base + "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
  158. idx++;
  159. }
  160. }
  161. void GraphNode::_resort() {
  162. /** First pass, determine minimum size AND amount of stretchable elements */
  163. Size2i new_size = get_size();
  164. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  165. int sep = get_theme_constant(SNAME("separation"));
  166. bool first = true;
  167. int children_count = 0;
  168. int stretch_min = 0;
  169. int stretch_avail = 0;
  170. float stretch_ratio_total = 0;
  171. Map<Control *, _MinSizeCache> min_size_cache;
  172. for (int i = 0; i < get_child_count(); i++) {
  173. Control *c = Object::cast_to<Control>(get_child(i));
  174. if (!c || !c->is_visible_in_tree()) {
  175. continue;
  176. }
  177. if (c->is_set_as_top_level()) {
  178. continue;
  179. }
  180. Size2i size = c->get_combined_minimum_size();
  181. _MinSizeCache msc;
  182. stretch_min += size.height;
  183. msc.min_size = size.height;
  184. msc.will_stretch = c->get_v_size_flags() & SIZE_EXPAND;
  185. if (msc.will_stretch) {
  186. stretch_avail += msc.min_size;
  187. stretch_ratio_total += c->get_stretch_ratio();
  188. }
  189. msc.final_size = msc.min_size;
  190. min_size_cache[c] = msc;
  191. children_count++;
  192. }
  193. if (children_count == 0) {
  194. return;
  195. }
  196. int stretch_max = new_size.height - (children_count - 1) * sep;
  197. int stretch_diff = stretch_max - stretch_min;
  198. if (stretch_diff < 0) {
  199. //avoid negative stretch space
  200. stretch_diff = 0;
  201. }
  202. stretch_avail += stretch_diff - sb->get_margin(SIDE_BOTTOM) - sb->get_margin(SIDE_TOP); //available stretch space.
  203. /** Second, pass successively to discard elements that can't be stretched, this will run while stretchable
  204. elements exist */
  205. while (stretch_ratio_total > 0) { // first of all, don't even be here if no stretchable objects exist
  206. bool refit_successful = true; //assume refit-test will go well
  207. for (int i = 0; i < get_child_count(); i++) {
  208. Control *c = Object::cast_to<Control>(get_child(i));
  209. if (!c || !c->is_visible_in_tree()) {
  210. continue;
  211. }
  212. if (c->is_set_as_top_level()) {
  213. continue;
  214. }
  215. ERR_FAIL_COND(!min_size_cache.has(c));
  216. _MinSizeCache &msc = min_size_cache[c];
  217. if (msc.will_stretch) { //wants to stretch
  218. //let's see if it can really stretch
  219. int final_pixel_size = stretch_avail * c->get_stretch_ratio() / stretch_ratio_total;
  220. if (final_pixel_size < msc.min_size) {
  221. //if available stretching area is too small for widget,
  222. //then remove it from stretching area
  223. msc.will_stretch = false;
  224. stretch_ratio_total -= c->get_stretch_ratio();
  225. refit_successful = false;
  226. stretch_avail -= msc.min_size;
  227. msc.final_size = msc.min_size;
  228. break;
  229. } else {
  230. msc.final_size = final_pixel_size;
  231. }
  232. }
  233. }
  234. if (refit_successful) { //uf refit went well, break
  235. break;
  236. }
  237. }
  238. /** Final pass, draw and stretch elements **/
  239. int ofs = sb->get_margin(SIDE_TOP);
  240. first = true;
  241. int idx = 0;
  242. cache_y.clear();
  243. int w = new_size.width - sb->get_minimum_size().x;
  244. for (int i = 0; i < get_child_count(); i++) {
  245. Control *c = Object::cast_to<Control>(get_child(i));
  246. if (!c || !c->is_visible_in_tree()) {
  247. continue;
  248. }
  249. if (c->is_set_as_top_level()) {
  250. continue;
  251. }
  252. _MinSizeCache &msc = min_size_cache[c];
  253. if (first) {
  254. first = false;
  255. } else {
  256. ofs += sep;
  257. }
  258. int from = ofs;
  259. int to = ofs + msc.final_size;
  260. if (msc.will_stretch && idx == children_count - 1) {
  261. //adjust so the last one always fits perfect
  262. //compensating for numerical imprecision
  263. to = new_size.height - sb->get_margin(SIDE_BOTTOM);
  264. }
  265. int size = to - from;
  266. Rect2 rect(sb->get_margin(SIDE_LEFT), from, w, size);
  267. fit_child_in_rect(c, rect);
  268. cache_y.push_back(from - sb->get_margin(SIDE_TOP) + size * 0.5);
  269. ofs = to;
  270. idx++;
  271. }
  272. update();
  273. connpos_dirty = true;
  274. }
  275. bool GraphNode::has_point(const Point2 &p_point) const {
  276. if (comment) {
  277. Ref<StyleBox> comment = get_theme_stylebox(SNAME("comment"));
  278. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  279. if (Rect2(get_size() - resizer->get_size(), resizer->get_size()).has_point(p_point)) {
  280. return true;
  281. }
  282. if (Rect2(0, 0, get_size().width, comment->get_margin(SIDE_TOP)).has_point(p_point)) {
  283. return true;
  284. }
  285. return false;
  286. } else {
  287. return Control::has_point(p_point);
  288. }
  289. }
  290. void GraphNode::_notification(int p_what) {
  291. switch (p_what) {
  292. case NOTIFICATION_DRAW: {
  293. Ref<StyleBox> sb;
  294. if (comment) {
  295. sb = get_theme_stylebox(selected ? "comment_focus" : "comment");
  296. } else {
  297. sb = get_theme_stylebox(selected ? "selected_frame" : "frame");
  298. }
  299. //sb=sb->duplicate();
  300. //sb->call("set_modulate",modulate);
  301. Ref<Texture2D> port = get_theme_icon(SNAME("port"));
  302. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  303. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  304. int close_offset = get_theme_constant(SNAME("close_offset"));
  305. int close_h_offset = get_theme_constant(SNAME("close_h_offset"));
  306. Color close_color = get_theme_color(SNAME("close_color"));
  307. Color resizer_color = get_theme_color(SNAME("resizer_color"));
  308. int title_offset = get_theme_constant(SNAME("title_offset"));
  309. int title_h_offset = get_theme_constant(SNAME("title_h_offset"));
  310. Color title_color = get_theme_color(SNAME("title_color"));
  311. Point2i icofs = -port->get_size() * 0.5;
  312. int edgeofs = get_theme_constant(SNAME("port_offset"));
  313. icofs.y += sb->get_margin(SIDE_TOP);
  314. draw_style_box(sb, Rect2(Point2(), get_size()));
  315. switch (overlay) {
  316. case OVERLAY_DISABLED: {
  317. } break;
  318. case OVERLAY_BREAKPOINT: {
  319. draw_style_box(get_theme_stylebox(SNAME("breakpoint")), Rect2(Point2(), get_size()));
  320. } break;
  321. case OVERLAY_POSITION: {
  322. draw_style_box(get_theme_stylebox(SNAME("position")), Rect2(Point2(), get_size()));
  323. } break;
  324. }
  325. int w = get_size().width - sb->get_minimum_size().x;
  326. if (show_close) {
  327. w -= close->get_width();
  328. }
  329. title_buf->draw(get_canvas_item(), Point2(sb->get_margin(SIDE_LEFT) + title_h_offset, -title_buf->get_size().y + title_offset), title_color);
  330. if (show_close) {
  331. Vector2 cpos = Point2(w + sb->get_margin(SIDE_LEFT) + close_h_offset, -close->get_height() + close_offset);
  332. draw_texture(close, cpos, close_color);
  333. close_rect.position = cpos;
  334. close_rect.size = close->get_size();
  335. } else {
  336. close_rect = Rect2();
  337. }
  338. for (const KeyValue<int, Slot> &E : slot_info) {
  339. if (E.key < 0 || E.key >= cache_y.size()) {
  340. continue;
  341. }
  342. if (!slot_info.has(E.key)) {
  343. continue;
  344. }
  345. const Slot &s = slot_info[E.key];
  346. //left
  347. if (s.enable_left) {
  348. Ref<Texture2D> p = port;
  349. if (s.custom_slot_left.is_valid()) {
  350. p = s.custom_slot_left;
  351. }
  352. p->draw(get_canvas_item(), icofs + Point2(edgeofs, cache_y[E.key]), s.color_left);
  353. }
  354. if (s.enable_right) {
  355. Ref<Texture2D> p = port;
  356. if (s.custom_slot_right.is_valid()) {
  357. p = s.custom_slot_right;
  358. }
  359. p->draw(get_canvas_item(), icofs + Point2(get_size().x - edgeofs, cache_y[E.key]), s.color_right);
  360. }
  361. }
  362. if (resizable) {
  363. draw_texture(resizer, get_size() - resizer->get_size(), resizer_color);
  364. }
  365. } break;
  366. case NOTIFICATION_SORT_CHILDREN: {
  367. _resort();
  368. } break;
  369. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  370. case NOTIFICATION_TRANSLATION_CHANGED:
  371. case NOTIFICATION_THEME_CHANGED: {
  372. _shape();
  373. update_minimum_size();
  374. update();
  375. } break;
  376. }
  377. }
  378. void GraphNode::_shape() {
  379. Ref<Font> font = get_theme_font(SNAME("title_font"));
  380. int font_size = get_theme_font_size(SNAME("title_font_size"));
  381. title_buf->clear();
  382. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  383. title_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  384. } else {
  385. title_buf->set_direction((TextServer::Direction)text_direction);
  386. }
  387. title_buf->add_string(title, font, font_size, opentype_features, (!language.is_empty()) ? language : TranslationServer::get_singleton()->get_tool_locale());
  388. }
  389. #ifdef TOOLS_ENABLED
  390. void GraphNode::_edit_set_position(const Point2 &p_position) {
  391. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  392. if (graph) {
  393. Point2 offset = (p_position + graph->get_scroll_ofs()) * graph->get_zoom();
  394. set_position_offset(offset);
  395. }
  396. set_position(p_position);
  397. }
  398. void GraphNode::_validate_property(PropertyInfo &property) const {
  399. Control::_validate_property(property);
  400. GraphEdit *graph = Object::cast_to<GraphEdit>(get_parent());
  401. if (graph) {
  402. if (property.name == "rect_position") {
  403. property.usage |= PROPERTY_USAGE_READ_ONLY;
  404. }
  405. }
  406. }
  407. #endif
  408. 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) {
  409. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set slot with p_idx (%d) lesser than zero.", p_idx));
  410. if (!p_enable_left && p_type_left == 0 && p_color_left == Color(1, 1, 1, 1) &&
  411. !p_enable_right && p_type_right == 0 && p_color_right == Color(1, 1, 1, 1) &&
  412. !p_custom_left.is_valid() && !p_custom_right.is_valid()) {
  413. slot_info.erase(p_idx);
  414. return;
  415. }
  416. Slot s;
  417. s.enable_left = p_enable_left;
  418. s.type_left = p_type_left;
  419. s.color_left = p_color_left;
  420. s.enable_right = p_enable_right;
  421. s.type_right = p_type_right;
  422. s.color_right = p_color_right;
  423. s.custom_slot_left = p_custom_left;
  424. s.custom_slot_right = p_custom_right;
  425. slot_info[p_idx] = s;
  426. update();
  427. connpos_dirty = true;
  428. emit_signal(SNAME("slot_updated"), p_idx);
  429. }
  430. void GraphNode::clear_slot(int p_idx) {
  431. slot_info.erase(p_idx);
  432. update();
  433. connpos_dirty = true;
  434. }
  435. void GraphNode::clear_all_slots() {
  436. slot_info.clear();
  437. update();
  438. connpos_dirty = true;
  439. }
  440. bool GraphNode::is_slot_enabled_left(int p_idx) const {
  441. if (!slot_info.has(p_idx)) {
  442. return false;
  443. }
  444. return slot_info[p_idx].enable_left;
  445. }
  446. void GraphNode::set_slot_enabled_left(int p_idx, bool p_enable_left) {
  447. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_left for the slot with p_idx (%d) lesser than zero.", p_idx));
  448. slot_info[p_idx].enable_left = p_enable_left;
  449. update();
  450. connpos_dirty = true;
  451. emit_signal(SNAME("slot_updated"), p_idx);
  452. }
  453. void GraphNode::set_slot_type_left(int p_idx, int p_type_left) {
  454. 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));
  455. slot_info[p_idx].type_left = p_type_left;
  456. update();
  457. connpos_dirty = true;
  458. emit_signal(SNAME("slot_updated"), p_idx);
  459. }
  460. int GraphNode::get_slot_type_left(int p_idx) const {
  461. if (!slot_info.has(p_idx)) {
  462. return 0;
  463. }
  464. return slot_info[p_idx].type_left;
  465. }
  466. void GraphNode::set_slot_color_left(int p_idx, const Color &p_color_left) {
  467. 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));
  468. slot_info[p_idx].color_left = p_color_left;
  469. update();
  470. connpos_dirty = true;
  471. emit_signal(SNAME("slot_updated"), p_idx);
  472. }
  473. Color GraphNode::get_slot_color_left(int p_idx) const {
  474. if (!slot_info.has(p_idx)) {
  475. return Color(1, 1, 1, 1);
  476. }
  477. return slot_info[p_idx].color_left;
  478. }
  479. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  480. if (!slot_info.has(p_idx)) {
  481. return false;
  482. }
  483. return slot_info[p_idx].enable_right;
  484. }
  485. void GraphNode::set_slot_enabled_right(int p_idx, bool p_enable_right) {
  486. ERR_FAIL_COND_MSG(p_idx < 0, vformat("Cannot set enable_right for the slot with p_idx (%d) lesser than zero.", p_idx));
  487. slot_info[p_idx].enable_right = p_enable_right;
  488. update();
  489. connpos_dirty = true;
  490. emit_signal(SNAME("slot_updated"), p_idx);
  491. }
  492. void GraphNode::set_slot_type_right(int p_idx, int p_type_right) {
  493. 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));
  494. slot_info[p_idx].type_right = p_type_right;
  495. update();
  496. connpos_dirty = true;
  497. emit_signal(SNAME("slot_updated"), p_idx);
  498. }
  499. int GraphNode::get_slot_type_right(int p_idx) const {
  500. if (!slot_info.has(p_idx)) {
  501. return 0;
  502. }
  503. return slot_info[p_idx].type_right;
  504. }
  505. void GraphNode::set_slot_color_right(int p_idx, const Color &p_color_right) {
  506. 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));
  507. slot_info[p_idx].color_right = p_color_right;
  508. update();
  509. connpos_dirty = true;
  510. emit_signal(SNAME("slot_updated"), p_idx);
  511. }
  512. Color GraphNode::get_slot_color_right(int p_idx) const {
  513. if (!slot_info.has(p_idx)) {
  514. return Color(1, 1, 1, 1);
  515. }
  516. return slot_info[p_idx].color_right;
  517. }
  518. Size2 GraphNode::get_minimum_size() const {
  519. int sep = get_theme_constant(SNAME("separation"));
  520. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  521. bool first = true;
  522. Size2 minsize;
  523. minsize.x = title_buf->get_size().x;
  524. if (show_close) {
  525. Ref<Texture2D> close = get_theme_icon(SNAME("close"));
  526. minsize.x += sep + close->get_width();
  527. }
  528. for (int i = 0; i < get_child_count(); i++) {
  529. Control *c = Object::cast_to<Control>(get_child(i));
  530. if (!c) {
  531. continue;
  532. }
  533. if (c->is_set_as_top_level()) {
  534. continue;
  535. }
  536. Size2i size = c->get_combined_minimum_size();
  537. minsize.y += size.y;
  538. minsize.x = MAX(minsize.x, size.x);
  539. if (first) {
  540. first = false;
  541. } else {
  542. minsize.y += sep;
  543. }
  544. }
  545. return minsize + sb->get_minimum_size();
  546. }
  547. void GraphNode::set_title(const String &p_title) {
  548. if (title == p_title) {
  549. return;
  550. }
  551. title = p_title;
  552. _shape();
  553. update();
  554. update_minimum_size();
  555. }
  556. String GraphNode::get_title() const {
  557. return title;
  558. }
  559. void GraphNode::set_text_direction(Control::TextDirection p_text_direction) {
  560. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  561. if (text_direction != p_text_direction) {
  562. text_direction = p_text_direction;
  563. _shape();
  564. update();
  565. }
  566. }
  567. Control::TextDirection GraphNode::get_text_direction() const {
  568. return text_direction;
  569. }
  570. void GraphNode::clear_opentype_features() {
  571. opentype_features.clear();
  572. _shape();
  573. update();
  574. }
  575. void GraphNode::set_opentype_feature(const String &p_name, int p_value) {
  576. int32_t tag = TS->name_to_tag(p_name);
  577. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  578. opentype_features[tag] = p_value;
  579. _shape();
  580. update();
  581. }
  582. }
  583. int GraphNode::get_opentype_feature(const String &p_name) const {
  584. int32_t tag = TS->name_to_tag(p_name);
  585. if (!opentype_features.has(tag)) {
  586. return -1;
  587. }
  588. return opentype_features[tag];
  589. }
  590. void GraphNode::set_language(const String &p_language) {
  591. if (language != p_language) {
  592. language = p_language;
  593. _shape();
  594. update();
  595. }
  596. }
  597. String GraphNode::get_language() const {
  598. return language;
  599. }
  600. void GraphNode::set_position_offset(const Vector2 &p_offset) {
  601. position_offset = p_offset;
  602. emit_signal(SNAME("position_offset_changed"));
  603. update();
  604. }
  605. Vector2 GraphNode::get_position_offset() const {
  606. return position_offset;
  607. }
  608. void GraphNode::set_selected(bool p_selected) {
  609. selected = p_selected;
  610. update();
  611. }
  612. bool GraphNode::is_selected() {
  613. return selected;
  614. }
  615. void GraphNode::set_drag(bool p_drag) {
  616. if (p_drag) {
  617. drag_from = get_position_offset();
  618. } else {
  619. emit_signal(SNAME("dragged"), drag_from, get_position_offset()); //useful for undo/redo
  620. }
  621. }
  622. Vector2 GraphNode::get_drag_from() {
  623. return drag_from;
  624. }
  625. void GraphNode::set_show_close_button(bool p_enable) {
  626. show_close = p_enable;
  627. update();
  628. }
  629. bool GraphNode::is_close_button_visible() const {
  630. return show_close;
  631. }
  632. void GraphNode::_connpos_update() {
  633. int edgeofs = get_theme_constant(SNAME("port_offset"));
  634. int sep = get_theme_constant(SNAME("separation"));
  635. Ref<StyleBox> sb = get_theme_stylebox(SNAME("frame"));
  636. conn_input_cache.clear();
  637. conn_output_cache.clear();
  638. int vofs = 0;
  639. int idx = 0;
  640. for (int i = 0; i < get_child_count(); i++) {
  641. Control *c = Object::cast_to<Control>(get_child(i));
  642. if (!c) {
  643. continue;
  644. }
  645. if (c->is_set_as_top_level()) {
  646. continue;
  647. }
  648. Size2i size = c->get_rect().size;
  649. int y = sb->get_margin(SIDE_TOP) + vofs;
  650. int h = size.y;
  651. if (slot_info.has(idx)) {
  652. if (slot_info[idx].enable_left) {
  653. ConnCache cc;
  654. cc.pos = Point2i(edgeofs, y + h / 2);
  655. cc.type = slot_info[idx].type_left;
  656. cc.color = slot_info[idx].color_left;
  657. conn_input_cache.push_back(cc);
  658. }
  659. if (slot_info[idx].enable_right) {
  660. ConnCache cc;
  661. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  662. cc.type = slot_info[idx].type_right;
  663. cc.color = slot_info[idx].color_right;
  664. conn_output_cache.push_back(cc);
  665. }
  666. }
  667. vofs += sep;
  668. vofs += size.y;
  669. idx++;
  670. }
  671. connpos_dirty = false;
  672. }
  673. int GraphNode::get_connection_input_count() {
  674. if (connpos_dirty) {
  675. _connpos_update();
  676. }
  677. return conn_input_cache.size();
  678. }
  679. int GraphNode::get_connection_output_count() {
  680. if (connpos_dirty) {
  681. _connpos_update();
  682. }
  683. return conn_output_cache.size();
  684. }
  685. Vector2 GraphNode::get_connection_input_position(int p_idx) {
  686. if (connpos_dirty) {
  687. _connpos_update();
  688. }
  689. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  690. Vector2 pos = conn_input_cache[p_idx].pos;
  691. pos.x *= get_scale().x;
  692. pos.y *= get_scale().y;
  693. return pos;
  694. }
  695. int GraphNode::get_connection_input_type(int p_idx) {
  696. if (connpos_dirty) {
  697. _connpos_update();
  698. }
  699. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  700. return conn_input_cache[p_idx].type;
  701. }
  702. Color GraphNode::get_connection_input_color(int p_idx) {
  703. if (connpos_dirty) {
  704. _connpos_update();
  705. }
  706. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  707. return conn_input_cache[p_idx].color;
  708. }
  709. Vector2 GraphNode::get_connection_output_position(int p_idx) {
  710. if (connpos_dirty) {
  711. _connpos_update();
  712. }
  713. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  714. Vector2 pos = conn_output_cache[p_idx].pos;
  715. pos.x *= get_scale().x;
  716. pos.y *= get_scale().y;
  717. return pos;
  718. }
  719. int GraphNode::get_connection_output_type(int p_idx) {
  720. if (connpos_dirty) {
  721. _connpos_update();
  722. }
  723. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  724. return conn_output_cache[p_idx].type;
  725. }
  726. Color GraphNode::get_connection_output_color(int p_idx) {
  727. if (connpos_dirty) {
  728. _connpos_update();
  729. }
  730. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  731. return conn_output_cache[p_idx].color;
  732. }
  733. void GraphNode::gui_input(const Ref<InputEvent> &p_ev) {
  734. ERR_FAIL_COND(p_ev.is_null());
  735. Ref<InputEventMouseButton> mb = p_ev;
  736. if (mb.is_valid()) {
  737. ERR_FAIL_COND_MSG(get_parent_control() == nullptr, "GraphNode must be the child of a GraphEdit node.");
  738. if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  739. Vector2 mpos = mb->get_position();
  740. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  741. //send focus to parent
  742. get_parent_control()->grab_focus();
  743. emit_signal(SNAME("close_request"));
  744. accept_event();
  745. return;
  746. }
  747. Ref<Texture2D> resizer = get_theme_icon(SNAME("resizer"));
  748. if (resizable && mpos.x > get_size().x - resizer->get_width() && mpos.y > get_size().y - resizer->get_height()) {
  749. resizing = true;
  750. resizing_from = mpos;
  751. resizing_from_size = get_size();
  752. accept_event();
  753. return;
  754. }
  755. emit_signal(SNAME("raise_request"));
  756. }
  757. if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  758. resizing = false;
  759. }
  760. }
  761. Ref<InputEventMouseMotion> mm = p_ev;
  762. if (resizing && mm.is_valid()) {
  763. Vector2 mpos = mm->get_position();
  764. Vector2 diff = mpos - resizing_from;
  765. emit_signal(SNAME("resize_request"), resizing_from_size + diff);
  766. }
  767. }
  768. void GraphNode::set_overlay(Overlay p_overlay) {
  769. overlay = p_overlay;
  770. update();
  771. }
  772. GraphNode::Overlay GraphNode::get_overlay() const {
  773. return overlay;
  774. }
  775. void GraphNode::set_comment(bool p_enable) {
  776. comment = p_enable;
  777. update();
  778. }
  779. bool GraphNode::is_comment() const {
  780. return comment;
  781. }
  782. void GraphNode::set_resizable(bool p_enable) {
  783. resizable = p_enable;
  784. update();
  785. }
  786. bool GraphNode::is_resizable() const {
  787. return resizable;
  788. }
  789. Vector<int> GraphNode::get_allowed_size_flags_horizontal() const {
  790. Vector<int> flags;
  791. flags.append(SIZE_FILL);
  792. flags.append(SIZE_SHRINK_BEGIN);
  793. flags.append(SIZE_SHRINK_CENTER);
  794. flags.append(SIZE_SHRINK_END);
  795. return flags;
  796. }
  797. Vector<int> GraphNode::get_allowed_size_flags_vertical() const {
  798. Vector<int> flags;
  799. flags.append(SIZE_FILL);
  800. flags.append(SIZE_EXPAND);
  801. flags.append(SIZE_SHRINK_BEGIN);
  802. flags.append(SIZE_SHRINK_CENTER);
  803. flags.append(SIZE_SHRINK_END);
  804. return flags;
  805. }
  806. void GraphNode::_bind_methods() {
  807. ClassDB::bind_method(D_METHOD("set_title", "title"), &GraphNode::set_title);
  808. ClassDB::bind_method(D_METHOD("get_title"), &GraphNode::get_title);
  809. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &GraphNode::set_text_direction);
  810. ClassDB::bind_method(D_METHOD("get_text_direction"), &GraphNode::get_text_direction);
  811. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &GraphNode::set_opentype_feature);
  812. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &GraphNode::get_opentype_feature);
  813. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &GraphNode::clear_opentype_features);
  814. ClassDB::bind_method(D_METHOD("set_language", "language"), &GraphNode::set_language);
  815. ClassDB::bind_method(D_METHOD("get_language"), &GraphNode::get_language);
  816. 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>()));
  817. ClassDB::bind_method(D_METHOD("clear_slot", "idx"), &GraphNode::clear_slot);
  818. ClassDB::bind_method(D_METHOD("clear_all_slots"), &GraphNode::clear_all_slots);
  819. ClassDB::bind_method(D_METHOD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  820. ClassDB::bind_method(D_METHOD("set_slot_enabled_left", "idx", "enable_left"), &GraphNode::set_slot_enabled_left);
  821. ClassDB::bind_method(D_METHOD("set_slot_type_left", "idx", "type_left"), &GraphNode::set_slot_type_left);
  822. ClassDB::bind_method(D_METHOD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  823. ClassDB::bind_method(D_METHOD("set_slot_color_left", "idx", "color_left"), &GraphNode::set_slot_color_left);
  824. ClassDB::bind_method(D_METHOD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  825. ClassDB::bind_method(D_METHOD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  826. ClassDB::bind_method(D_METHOD("set_slot_enabled_right", "idx", "enable_right"), &GraphNode::set_slot_enabled_right);
  827. ClassDB::bind_method(D_METHOD("set_slot_type_right", "idx", "type_right"), &GraphNode::set_slot_type_right);
  828. ClassDB::bind_method(D_METHOD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  829. ClassDB::bind_method(D_METHOD("set_slot_color_right", "idx", "color_right"), &GraphNode::set_slot_color_right);
  830. ClassDB::bind_method(D_METHOD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  831. ClassDB::bind_method(D_METHOD("set_position_offset", "offset"), &GraphNode::set_position_offset);
  832. ClassDB::bind_method(D_METHOD("get_position_offset"), &GraphNode::get_position_offset);
  833. ClassDB::bind_method(D_METHOD("set_comment", "comment"), &GraphNode::set_comment);
  834. ClassDB::bind_method(D_METHOD("is_comment"), &GraphNode::is_comment);
  835. ClassDB::bind_method(D_METHOD("set_resizable", "resizable"), &GraphNode::set_resizable);
  836. ClassDB::bind_method(D_METHOD("is_resizable"), &GraphNode::is_resizable);
  837. ClassDB::bind_method(D_METHOD("set_selected", "selected"), &GraphNode::set_selected);
  838. ClassDB::bind_method(D_METHOD("is_selected"), &GraphNode::is_selected);
  839. ClassDB::bind_method(D_METHOD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  840. ClassDB::bind_method(D_METHOD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  841. ClassDB::bind_method(D_METHOD("get_connection_output_position", "idx"), &GraphNode::get_connection_output_position);
  842. ClassDB::bind_method(D_METHOD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  843. ClassDB::bind_method(D_METHOD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  844. ClassDB::bind_method(D_METHOD("get_connection_input_position", "idx"), &GraphNode::get_connection_input_position);
  845. ClassDB::bind_method(D_METHOD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  846. ClassDB::bind_method(D_METHOD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  847. ClassDB::bind_method(D_METHOD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  848. ClassDB::bind_method(D_METHOD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  849. ClassDB::bind_method(D_METHOD("set_overlay", "overlay"), &GraphNode::set_overlay);
  850. ClassDB::bind_method(D_METHOD("get_overlay"), &GraphNode::get_overlay);
  851. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  852. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  853. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  854. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position_offset"), "set_position_offset", "get_position_offset");
  855. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), "set_show_close_button", "is_close_button_visible");
  856. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resizable"), "set_resizable", "is_resizable");
  857. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selected"), "set_selected", "is_selected");
  858. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "comment"), "set_comment", "is_comment");
  859. ADD_PROPERTY(PropertyInfo(Variant::INT, "overlay", PROPERTY_HINT_ENUM, "Disabled,Breakpoint,Position"), "set_overlay", "get_overlay");
  860. ADD_SIGNAL(MethodInfo("position_offset_changed"));
  861. ADD_SIGNAL(MethodInfo("slot_updated", PropertyInfo(Variant::INT, "idx")));
  862. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  863. ADD_SIGNAL(MethodInfo("raise_request"));
  864. ADD_SIGNAL(MethodInfo("close_request"));
  865. ADD_SIGNAL(MethodInfo("resize_request", PropertyInfo(Variant::VECTOR2, "new_minsize")));
  866. BIND_ENUM_CONSTANT(OVERLAY_DISABLED);
  867. BIND_ENUM_CONSTANT(OVERLAY_BREAKPOINT);
  868. BIND_ENUM_CONSTANT(OVERLAY_POSITION);
  869. }
  870. GraphNode::GraphNode() {
  871. title_buf.instantiate();
  872. set_mouse_filter(MOUSE_FILTER_STOP);
  873. }