graph_node.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 "method_bind_ext.gen.inc"
  32. bool GraphNode::_set(const StringName &p_name, const Variant &p_value) {
  33. if (!p_name.operator String().begins_with("slot/"))
  34. return false;
  35. int idx = p_name.operator String().get_slice("/", 1).to_int();
  36. String what = p_name.operator String().get_slice("/", 2);
  37. Slot si;
  38. if (slot_info.has(idx))
  39. si = slot_info[idx];
  40. if (what == "left_enabled")
  41. si.enable_left = p_value;
  42. else if (what == "left_type")
  43. si.type_left = p_value;
  44. else if (what == "left_color")
  45. si.color_left = p_value;
  46. else if (what == "right_enabled")
  47. si.enable_right = p_value;
  48. else if (what == "right_type")
  49. si.type_right = p_value;
  50. else if (what == "right_color")
  51. si.color_right = p_value;
  52. else
  53. return false;
  54. set_slot(idx, si.enable_left, si.type_left, si.color_left, si.enable_right, si.type_right, si.color_right);
  55. update();
  56. return true;
  57. }
  58. bool GraphNode::_get(const StringName &p_name, Variant &r_ret) const {
  59. if (!p_name.operator String().begins_with("slot/")) {
  60. return false;
  61. }
  62. int idx = p_name.operator String().get_slice("/", 1).to_int();
  63. String what = p_name.operator String().get_slice("/", 2);
  64. Slot si;
  65. if (slot_info.has(idx))
  66. si = slot_info[idx];
  67. if (what == "left_enabled")
  68. r_ret = si.enable_left;
  69. else if (what == "left_type")
  70. r_ret = si.type_left;
  71. else if (what == "left_color")
  72. r_ret = si.color_left;
  73. else if (what == "right_enabled")
  74. r_ret = si.enable_right;
  75. else if (what == "right_type")
  76. r_ret = si.type_right;
  77. else if (what == "right_color")
  78. r_ret = si.color_right;
  79. else
  80. return false;
  81. return true;
  82. }
  83. void GraphNode::_get_property_list(List<PropertyInfo> *p_list) const {
  84. int idx = 0;
  85. for (int i = 0; i < get_child_count(); i++) {
  86. Control *c = get_child(i)->cast_to<Control>();
  87. if (!c || c->is_set_as_toplevel())
  88. continue;
  89. String base = "slot/" + itos(idx) + "/";
  90. p_list->push_back(PropertyInfo(Variant::BOOL, base + "left_enabled"));
  91. p_list->push_back(PropertyInfo(Variant::INT, base + "left_type"));
  92. p_list->push_back(PropertyInfo(Variant::COLOR, base + "left_color"));
  93. p_list->push_back(PropertyInfo(Variant::BOOL, base + "right_enabled"));
  94. p_list->push_back(PropertyInfo(Variant::INT, base + "right_type"));
  95. p_list->push_back(PropertyInfo(Variant::COLOR, base + "right_color"));
  96. idx++;
  97. }
  98. }
  99. void GraphNode::_resort() {
  100. int sep = get_constant("separation");
  101. Ref<StyleBox> sb = get_stylebox("frame");
  102. bool first = true;
  103. Size2 minsize;
  104. for (int i = 0; i < get_child_count(); i++) {
  105. Control *c = get_child(i)->cast_to<Control>();
  106. if (!c)
  107. continue;
  108. if (c->is_set_as_toplevel())
  109. continue;
  110. Size2i size = c->get_combined_minimum_size();
  111. minsize.y += size.y;
  112. minsize.x = MAX(minsize.x, size.x);
  113. if (first)
  114. first = false;
  115. else
  116. minsize.y += sep;
  117. }
  118. int vofs = 0;
  119. int w = get_size().x - sb->get_minimum_size().x;
  120. cache_y.clear();
  121. for (int i = 0; i < get_child_count(); i++) {
  122. Control *c = get_child(i)->cast_to<Control>();
  123. if (!c)
  124. continue;
  125. if (c->is_set_as_toplevel())
  126. continue;
  127. Size2i size = c->get_combined_minimum_size();
  128. Rect2 r(sb->get_margin(MARGIN_LEFT), sb->get_margin(MARGIN_TOP) + vofs, w, size.y);
  129. fit_child_in_rect(c, r);
  130. cache_y.push_back(vofs + size.y * 0.5);
  131. if (vofs > 0)
  132. vofs += sep;
  133. vofs += size.y;
  134. }
  135. _change_notify();
  136. update();
  137. connpos_dirty = true;
  138. }
  139. void GraphNode::_notification(int p_what) {
  140. if (p_what == NOTIFICATION_DRAW) {
  141. Ref<StyleBox> sb = get_stylebox(selected ? "selectedframe" : "frame");
  142. Ref<Texture> port = get_icon("port");
  143. Ref<Texture> close = get_icon("close");
  144. int close_offset = get_constant("close_offset");
  145. Ref<Font> title_font = get_font("title_font");
  146. int title_offset = get_constant("title_offset");
  147. Color title_color = get_color("title_color");
  148. Point2i icofs = -port->get_size() * 0.5;
  149. int edgeofs = get_constant("port_offset");
  150. icofs.y += sb->get_margin(MARGIN_TOP);
  151. draw_style_box(sb, Rect2(Point2(), get_size()));
  152. int w = get_size().width - sb->get_minimum_size().x;
  153. if (show_close)
  154. w -= close->get_width();
  155. draw_string(title_font, Point2(sb->get_margin(MARGIN_LEFT), -title_font->get_height() + title_font->get_ascent() + title_offset), title, title_color, w);
  156. if (show_close) {
  157. Vector2 cpos = Point2(w + sb->get_margin(MARGIN_LEFT), -close->get_height() + close_offset);
  158. draw_texture(close, cpos);
  159. close_rect.pos = cpos;
  160. close_rect.size = close->get_size();
  161. } else {
  162. close_rect = Rect2();
  163. }
  164. for (Map<int, Slot>::Element *E = slot_info.front(); E; E = E->next()) {
  165. if (E->key() < 0 || E->key() >= cache_y.size())
  166. continue;
  167. if (!slot_info.has(E->key()))
  168. continue;
  169. const Slot &s = slot_info[E->key()];
  170. //left
  171. if (s.enable_left)
  172. port->draw(get_canvas_item(), icofs + Point2(edgeofs, cache_y[E->key()]), s.color_left);
  173. if (s.enable_right)
  174. port->draw(get_canvas_item(), icofs + Point2(get_size().x - edgeofs, cache_y[E->key()]), s.color_right);
  175. }
  176. }
  177. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  178. _resort();
  179. }
  180. }
  181. 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) {
  182. ERR_FAIL_COND(p_idx < 0);
  183. 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)) {
  184. slot_info.erase(p_idx);
  185. return;
  186. }
  187. Slot s;
  188. s.enable_left = p_enable_left;
  189. s.type_left = p_type_left;
  190. s.color_left = p_color_left;
  191. s.enable_right = p_enable_right;
  192. s.type_right = p_type_right;
  193. s.color_right = p_color_right;
  194. slot_info[p_idx] = s;
  195. update();
  196. connpos_dirty = true;
  197. }
  198. void GraphNode::clear_slot(int p_idx) {
  199. slot_info.erase(p_idx);
  200. update();
  201. connpos_dirty = true;
  202. }
  203. void GraphNode::clear_all_slots() {
  204. slot_info.clear();
  205. update();
  206. connpos_dirty = true;
  207. }
  208. bool GraphNode::is_slot_enabled_left(int p_idx) const {
  209. if (!slot_info.has(p_idx))
  210. return false;
  211. return slot_info[p_idx].enable_left;
  212. }
  213. int GraphNode::get_slot_type_left(int p_idx) const {
  214. if (!slot_info.has(p_idx))
  215. return 0;
  216. return slot_info[p_idx].type_left;
  217. }
  218. Color GraphNode::get_slot_color_left(int p_idx) const {
  219. if (!slot_info.has(p_idx))
  220. return Color(1, 1, 1, 1);
  221. return slot_info[p_idx].color_left;
  222. }
  223. bool GraphNode::is_slot_enabled_right(int p_idx) const {
  224. if (!slot_info.has(p_idx))
  225. return false;
  226. return slot_info[p_idx].enable_right;
  227. }
  228. int GraphNode::get_slot_type_right(int p_idx) const {
  229. if (!slot_info.has(p_idx))
  230. return 0;
  231. return slot_info[p_idx].type_right;
  232. }
  233. Color GraphNode::get_slot_color_right(int p_idx) const {
  234. if (!slot_info.has(p_idx))
  235. return Color(1, 1, 1, 1);
  236. return slot_info[p_idx].color_right;
  237. }
  238. Size2 GraphNode::get_minimum_size() const {
  239. Ref<Font> title_font = get_font("title_font");
  240. int sep = get_constant("separation");
  241. Ref<StyleBox> sb = get_stylebox("frame");
  242. bool first = true;
  243. Size2 minsize;
  244. minsize.x = title_font->get_string_size(title).x;
  245. if (show_close) {
  246. Ref<Texture> close = get_icon("close");
  247. minsize.x += sep + close->get_width();
  248. }
  249. for (int i = 0; i < get_child_count(); i++) {
  250. Control *c = get_child(i)->cast_to<Control>();
  251. if (!c)
  252. continue;
  253. if (c->is_set_as_toplevel())
  254. continue;
  255. Size2i size = c->get_combined_minimum_size();
  256. minsize.y += size.y;
  257. minsize.x = MAX(minsize.x, size.x);
  258. if (first)
  259. first = false;
  260. else
  261. minsize.y += sep;
  262. }
  263. return minsize + sb->get_minimum_size();
  264. }
  265. void GraphNode::set_title(const String &p_title) {
  266. title = p_title;
  267. minimum_size_changed();
  268. update();
  269. }
  270. String GraphNode::get_title() const {
  271. return title;
  272. }
  273. void GraphNode::set_offset(const Vector2 &p_offset) {
  274. offset = p_offset;
  275. emit_signal("offset_changed");
  276. update();
  277. }
  278. Vector2 GraphNode::get_offset() const {
  279. return offset;
  280. }
  281. void GraphNode::set_selected(bool p_selected) {
  282. selected = p_selected;
  283. update();
  284. }
  285. bool GraphNode::is_selected() {
  286. return selected;
  287. }
  288. void GraphNode::set_drag(bool p_drag) {
  289. if (p_drag)
  290. drag_from = get_offset();
  291. else
  292. emit_signal("dragged", drag_from, get_offset()); //useful for undo/redo
  293. }
  294. Vector2 GraphNode::get_drag_from() {
  295. return drag_from;
  296. }
  297. void GraphNode::set_show_close_button(bool p_enable) {
  298. show_close = p_enable;
  299. update();
  300. }
  301. bool GraphNode::is_close_button_visible() const {
  302. return show_close;
  303. }
  304. void GraphNode::_connpos_update() {
  305. int edgeofs = get_constant("port_offset");
  306. int sep = get_constant("separation");
  307. Ref<StyleBox> sb = get_stylebox("frame");
  308. conn_input_cache.clear();
  309. conn_output_cache.clear();
  310. int vofs = 0;
  311. int idx = 0;
  312. for (int i = 0; i < get_child_count(); i++) {
  313. Control *c = get_child(i)->cast_to<Control>();
  314. if (!c)
  315. continue;
  316. if (c->is_set_as_toplevel())
  317. continue;
  318. Size2i size = c->get_combined_minimum_size();
  319. int y = sb->get_margin(MARGIN_TOP) + vofs;
  320. int h = size.y;
  321. if (slot_info.has(idx)) {
  322. if (slot_info[idx].enable_left) {
  323. ConnCache cc;
  324. cc.pos = Point2i(edgeofs, y + h / 2);
  325. cc.type = slot_info[idx].type_left;
  326. cc.color = slot_info[idx].color_left;
  327. conn_input_cache.push_back(cc);
  328. }
  329. if (slot_info[idx].enable_right) {
  330. ConnCache cc;
  331. cc.pos = Point2i(get_size().width - edgeofs, y + h / 2);
  332. cc.type = slot_info[idx].type_right;
  333. cc.color = slot_info[idx].color_right;
  334. conn_output_cache.push_back(cc);
  335. }
  336. }
  337. if (vofs > 0)
  338. vofs += sep;
  339. vofs += size.y;
  340. idx++;
  341. }
  342. connpos_dirty = false;
  343. }
  344. int GraphNode::get_connection_input_count() {
  345. if (connpos_dirty)
  346. _connpos_update();
  347. return conn_input_cache.size();
  348. }
  349. int GraphNode::get_connection_output_count() {
  350. if (connpos_dirty)
  351. _connpos_update();
  352. return conn_output_cache.size();
  353. }
  354. Vector2 GraphNode::get_connection_input_pos(int p_idx) {
  355. if (connpos_dirty)
  356. _connpos_update();
  357. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Vector2());
  358. Vector2 pos = conn_input_cache[p_idx].pos;
  359. pos.x *= get_scale().x;
  360. pos.y *= get_scale().y;
  361. return pos;
  362. }
  363. int GraphNode::get_connection_input_type(int p_idx) {
  364. if (connpos_dirty)
  365. _connpos_update();
  366. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), 0);
  367. return conn_input_cache[p_idx].type;
  368. }
  369. Color GraphNode::get_connection_input_color(int p_idx) {
  370. if (connpos_dirty)
  371. _connpos_update();
  372. ERR_FAIL_INDEX_V(p_idx, conn_input_cache.size(), Color());
  373. return conn_input_cache[p_idx].color;
  374. }
  375. Vector2 GraphNode::get_connection_output_pos(int p_idx) {
  376. if (connpos_dirty)
  377. _connpos_update();
  378. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Vector2());
  379. Vector2 pos = conn_output_cache[p_idx].pos;
  380. pos.x *= get_scale().x;
  381. pos.y *= get_scale().y;
  382. return pos;
  383. }
  384. int GraphNode::get_connection_output_type(int p_idx) {
  385. if (connpos_dirty)
  386. _connpos_update();
  387. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), 0);
  388. return conn_output_cache[p_idx].type;
  389. }
  390. Color GraphNode::get_connection_output_color(int p_idx) {
  391. if (connpos_dirty)
  392. _connpos_update();
  393. ERR_FAIL_INDEX_V(p_idx, conn_output_cache.size(), Color());
  394. return conn_output_cache[p_idx].color;
  395. }
  396. void GraphNode::_input_event(const InputEvent &p_ev) {
  397. if (p_ev.type == InputEvent::MOUSE_BUTTON) {
  398. ERR_EXPLAIN("GraphNode must be the child of a GraphEdit node.");
  399. ERR_FAIL_COND(get_parent_control() == NULL);
  400. get_parent_control()->grab_focus();
  401. if (p_ev.mouse_button.pressed && p_ev.mouse_button.button_index == BUTTON_LEFT) {
  402. Vector2 mpos = Vector2(p_ev.mouse_button.x, p_ev.mouse_button.y);
  403. if (close_rect.size != Size2() && close_rect.has_point(mpos)) {
  404. emit_signal("close_request");
  405. return;
  406. }
  407. emit_signal("raise_request");
  408. }
  409. }
  410. }
  411. void GraphNode::_bind_methods() {
  412. ObjectTypeDB::bind_method(_MD("set_title", "title"), &GraphNode::set_title);
  413. ObjectTypeDB::bind_method(_MD("get_title"), &GraphNode::get_title);
  414. ObjectTypeDB::bind_method(_MD("_input_event"), &GraphNode::_input_event);
  415. ObjectTypeDB::bind_method(_MD("set_slot", "idx", "enable_left", "type_left", "color_left", "enable_right", "type_right", "color_right"), &GraphNode::set_slot);
  416. ObjectTypeDB::bind_method(_MD("clear_slot", "idx"), &GraphNode::clear_slot);
  417. ObjectTypeDB::bind_method(_MD("clear_all_slots", "idx"), &GraphNode::clear_all_slots);
  418. ObjectTypeDB::bind_method(_MD("is_slot_enabled_left", "idx"), &GraphNode::is_slot_enabled_left);
  419. ObjectTypeDB::bind_method(_MD("get_slot_type_left", "idx"), &GraphNode::get_slot_type_left);
  420. ObjectTypeDB::bind_method(_MD("get_slot_color_left", "idx"), &GraphNode::get_slot_color_left);
  421. ObjectTypeDB::bind_method(_MD("is_slot_enabled_right", "idx"), &GraphNode::is_slot_enabled_right);
  422. ObjectTypeDB::bind_method(_MD("get_slot_type_right", "idx"), &GraphNode::get_slot_type_right);
  423. ObjectTypeDB::bind_method(_MD("get_slot_color_right", "idx"), &GraphNode::get_slot_color_right);
  424. ObjectTypeDB::bind_method(_MD("set_offset", "offset"), &GraphNode::set_offset);
  425. ObjectTypeDB::bind_method(_MD("get_offset"), &GraphNode::get_offset);
  426. ObjectTypeDB::bind_method(_MD("get_connection_output_count"), &GraphNode::get_connection_output_count);
  427. ObjectTypeDB::bind_method(_MD("get_connection_input_count"), &GraphNode::get_connection_input_count);
  428. ObjectTypeDB::bind_method(_MD("get_connection_output_pos", "idx"), &GraphNode::get_connection_output_pos);
  429. ObjectTypeDB::bind_method(_MD("get_connection_output_type", "idx"), &GraphNode::get_connection_output_type);
  430. ObjectTypeDB::bind_method(_MD("get_connection_output_color", "idx"), &GraphNode::get_connection_output_color);
  431. ObjectTypeDB::bind_method(_MD("get_connection_input_pos", "idx"), &GraphNode::get_connection_input_pos);
  432. ObjectTypeDB::bind_method(_MD("get_connection_input_type", "idx"), &GraphNode::get_connection_input_type);
  433. ObjectTypeDB::bind_method(_MD("get_connection_input_color", "idx"), &GraphNode::get_connection_input_color);
  434. ObjectTypeDB::bind_method(_MD("set_show_close_button", "show"), &GraphNode::set_show_close_button);
  435. ObjectTypeDB::bind_method(_MD("is_close_button_visible"), &GraphNode::is_close_button_visible);
  436. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), _SCS("set_title"), _SCS("get_title"));
  437. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_close"), _SCS("set_show_close_button"), _SCS("is_close_button_visible"));
  438. ADD_SIGNAL(MethodInfo("offset_changed"));
  439. ADD_SIGNAL(MethodInfo("dragged", PropertyInfo(Variant::VECTOR2, "from"), PropertyInfo(Variant::VECTOR2, "to")));
  440. ADD_SIGNAL(MethodInfo("raise_request"));
  441. ADD_SIGNAL(MethodInfo("close_request"));
  442. }
  443. GraphNode::GraphNode() {
  444. show_close = false;
  445. connpos_dirty = true;
  446. set_stop_mouse(false);
  447. }