graph_node.cpp 16 KB

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