graph_node.cpp 14 KB

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