graph_node.cpp 12 KB

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