graph_node.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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() || !c->get_owner())
  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() || !c->get_owner())
  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. }
  111. void GraphNode::_notification(int p_what) {
  112. if (p_what==NOTIFICATION_DRAW) {
  113. Ref<StyleBox> sb=get_stylebox("frame");
  114. Ref<Texture> port =get_icon("port");
  115. Point2i icofs = -port->get_size()*0.5;
  116. int edgeofs=3;
  117. icofs.y+=sb->get_margin(MARGIN_TOP);
  118. draw_style_box(sb,Rect2(Point2(),get_size()));
  119. for (Map<int,Slot>::Element *E=slot_info.front();E;E=E->next()) {
  120. if (E->key()>cache_y.size())
  121. continue;
  122. if (!slot_info.has(E->key()))
  123. continue;
  124. const Slot &s=slot_info[E->key()];
  125. //left
  126. if (s.enable_left)
  127. port->draw(get_canvas_item(),icofs+Point2(edgeofs,cache_y[E->key()]),s.color_left);
  128. if (s.enable_right)
  129. port->draw(get_canvas_item(),icofs+Point2(get_size().x-edgeofs,cache_y[E->key()]),s.color_right);
  130. }
  131. }
  132. if (p_what==NOTIFICATION_SORT_CHILDREN) {
  133. _resort();
  134. }
  135. }
  136. void GraphNode::set_title(const String& p_title) {
  137. title=p_title;
  138. update();
  139. }
  140. String GraphNode::get_title() const {
  141. return title;
  142. }
  143. 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) {
  144. ERR_FAIL_COND(p_idx<0);
  145. 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)) {
  146. slot_info.erase(p_idx);
  147. return;
  148. }
  149. Slot s;
  150. s.enable_left=p_enable_left;
  151. s.type_left=p_type_left;
  152. s.color_left=p_color_left;
  153. s.enable_right=p_enable_right;
  154. s.type_right=p_type_right;
  155. s.color_right=p_color_right;
  156. slot_info[p_idx]=s;
  157. update();
  158. }
  159. void GraphNode::clear_slot(int p_idx){
  160. slot_info.erase(p_idx);
  161. update();
  162. }
  163. void GraphNode::clear_all_slots(){
  164. slot_info.clear();
  165. update();
  166. }
  167. bool GraphNode::is_slot_enabled_left(int p_idx) const{
  168. if (!slot_info.has(p_idx))
  169. return false;
  170. return slot_info[p_idx].enable_left;
  171. }
  172. int GraphNode::get_slot_type_left(int p_idx) const{
  173. if (!slot_info.has(p_idx))
  174. return 0;
  175. return slot_info[p_idx].type_left;
  176. }
  177. Color GraphNode::get_slot_color_left(int p_idx) const{
  178. if (!slot_info.has(p_idx))
  179. return Color(1,1,1,1);
  180. return slot_info[p_idx].color_left;
  181. }
  182. bool GraphNode::is_slot_enabled_right(int p_idx) const{
  183. if (!slot_info.has(p_idx))
  184. return false;
  185. return slot_info[p_idx].enable_right;
  186. }
  187. int GraphNode::get_slot_type_right(int p_idx) const{
  188. if (!slot_info.has(p_idx))
  189. return 0;
  190. return slot_info[p_idx].type_right;
  191. }
  192. Color GraphNode::get_slot_color_right(int p_idx) const{
  193. if (!slot_info.has(p_idx))
  194. return Color(1,1,1,1);
  195. return slot_info[p_idx].color_right;
  196. }
  197. Size2 GraphNode::get_minimum_size() const {
  198. int sep=get_constant("separation");
  199. Ref<StyleBox> sb=get_stylebox("frame");
  200. bool first=true;
  201. Size2 minsize;
  202. for(int i=0;i<get_child_count();i++) {
  203. Control *c=get_child(i)->cast_to<Control>();
  204. if (!c)
  205. continue;
  206. if (c->is_set_as_toplevel() || !c->get_owner())
  207. continue;
  208. Size2i size=c->get_combined_minimum_size();
  209. minsize.y+=size.y;
  210. minsize.x=MAX(minsize.x,size.x);
  211. if (first)
  212. first=false;
  213. else
  214. minsize.y+=sep;
  215. }
  216. return minsize+sb->get_minimum_size();
  217. }
  218. void GraphNode::_bind_methods() {
  219. }
  220. GraphNode::GraphNode() {
  221. }