Warning, /include/Geant4/tools/sg/back_area is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_sg_back_area
0005 #define tools_sg_back_area
0006
0007 #include "node"
0008 #include "atb_vertices"
0009 #include "rgba"
0010 #include "draw_style"
0011 #include "../mathf"
0012
0013 #include "separator"
0014 #include "normal"
0015
0016 namespace tools {
0017 namespace sg {
0018
0019 class back_area : public node {
0020 TOOLS_NODE(back_area,tools::sg::back_area,node)
0021 public:
0022 static unsigned int corner_top_right() {return 1<<0;}
0023 static unsigned int corner_top_left() {return 1<<1;}
0024 static unsigned int corner_bottom_right() {return 1<<2;}
0025 static unsigned int corner_bottom_left() {return 1<<3;}
0026 static unsigned int corner_all() {
0027 return corner_top_right()|corner_top_left()|corner_bottom_right()|corner_bottom_left();
0028 }
0029 public:
0030 sf<float> width;
0031 sf<float> height;
0032
0033 sf_vec<colorf,float> color;
0034
0035 sf<bool> gradient;
0036 sf_vec<colorf,float> color_top;
0037
0038 sf<bool> border_visible;
0039 sf_vec<colorf,float> border_color;
0040 sf<float> border_line_width;
0041
0042 sf<float> shadow; //in percent of width.
0043
0044 sf<float> corner_radius; //percent of height.
0045 sf<unsigned int> corner_steps;
0046 sf<unsigned int> corner_mask;
0047 public:
0048 virtual const desc_fields& node_desc_fields() const {
0049 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::back_area)
0050 static const desc_fields s_v(parent::node_desc_fields(),12, //WARNING : take care of count.
0051 TOOLS_ARG_FIELD_DESC(width),
0052 TOOLS_ARG_FIELD_DESC(height),
0053 TOOLS_ARG_FIELD_DESC(color),
0054 TOOLS_ARG_FIELD_DESC(gradient),
0055 TOOLS_ARG_FIELD_DESC(color_top),
0056 TOOLS_ARG_FIELD_DESC(border_visible),
0057 TOOLS_ARG_FIELD_DESC(border_color),
0058 TOOLS_ARG_FIELD_DESC(border_line_width),
0059 TOOLS_ARG_FIELD_DESC(shadow),
0060 TOOLS_ARG_FIELD_DESC(corner_radius),
0061 TOOLS_ARG_FIELD_DESC(corner_steps),
0062 TOOLS_ARG_FIELD_DESC(corner_mask)
0063 );
0064 return s_v;
0065 }
0066 private:
0067 void add_fields(){
0068 add_field(&width);
0069 add_field(&height);
0070
0071 add_field(&color);
0072 add_field(&gradient);
0073 add_field(&color_top);
0074 add_field(&border_visible);
0075 add_field(&border_color);
0076 add_field(&border_line_width);
0077
0078 add_field(&shadow);
0079
0080 add_field(&corner_radius);
0081 add_field(&corner_steps);
0082 add_field(&corner_mask);
0083 }
0084 public:
0085 virtual void render(render_action& a_action) {
0086 if(touched()) {
0087 update_sg();
0088 reset_touched();
0089 }
0090 m_back_sep.render(a_action);
0091 }
0092 virtual void search(search_action& a_action) {
0093 if(touched()) {
0094 update_sg();
0095 reset_touched();
0096 }
0097 parent::search(a_action);
0098 if(a_action.done()) return;
0099 m_back_sep.search(a_action);
0100 if(a_action.done()) return;
0101 }
0102 virtual void pick(pick_action& a_action) {
0103 if(touched()) {
0104 update_sg();
0105 reset_touched();
0106 }
0107 m_back_sep.pick(a_action);
0108 }
0109 public:
0110 back_area()
0111 :parent()
0112 ,width(1)
0113 ,height(1)
0114 ,color(colorf_white())
0115 ,gradient(false)
0116 ,color_top(colorf_white())
0117 ,border_visible(true)
0118 ,border_color(colorf_black())
0119 ,border_line_width(1)
0120 ,shadow(0)
0121 ,corner_radius(0) //in percent of the height.
0122 ,corner_steps(12)
0123 ,corner_mask(corner_all())
0124 {
0125 add_fields();
0126 }
0127 virtual ~back_area(){}
0128 public:
0129 back_area(const back_area& a_from)
0130 :parent(a_from)
0131 ,width(a_from.width)
0132 ,height(a_from.height)
0133 ,color(a_from.color)
0134 ,gradient(a_from.gradient)
0135 ,color_top(a_from.color_top)
0136 ,border_visible(a_from.border_visible)
0137 ,border_color(a_from.border_color)
0138 ,border_line_width(a_from.border_line_width)
0139 ,shadow(a_from.shadow)
0140 ,corner_radius(a_from.corner_radius)
0141 ,corner_steps(a_from.corner_steps)
0142 ,corner_mask(a_from.corner_mask)
0143 {
0144 add_fields();
0145 }
0146 back_area& operator=(const back_area& a_from){
0147 parent::operator=(a_from);
0148 width = a_from.width;
0149 height = a_from.height;
0150 color = a_from.color;
0151 gradient = a_from.gradient;
0152 color_top = a_from.color_top;
0153 border_visible = a_from.border_visible;
0154 border_color = a_from.border_color;
0155 border_line_width = a_from.border_line_width;
0156 shadow = a_from.shadow;
0157 corner_radius = a_from.corner_radius;
0158 corner_steps = a_from.corner_steps;
0159 corner_mask = a_from.corner_mask;
0160 return *this;
0161 }
0162 protected:
0163 void update_sg() {
0164 m_back_sep.clear();
0165
0166 if(width.value()<=0) return;
0167 if(height.value()<=0) return;
0168
0169 float xb = -width*0.5f;
0170 float xe = width*0.5f;
0171 float yb = -height*0.5f;
0172
0173 //float zshadow = -0.05f;
0174 float zshadow = -0.005f; //ok with gopaw and ROOT_default.
0175 float zback = 0;
0176 float zborder = 0.01f;
0177
0178 if(shadow.value()) {
0179 float zz = zback+zshadow;
0180 float ye = height*0.5f;
0181
0182 sg::rgba* mat = new sg::rgba();
0183 mat->color = colorf_black();
0184 m_back_sep.add(mat);
0185
0186 normal* nm = new normal;
0187 //nm->vec.value(); //default is z. ok.
0188 m_back_sep.add(nm);
0189
0190 vertices* vtxs = new vertices;
0191 vtxs->mode = gl::triangle_fan();
0192 m_back_sep.add(vtxs);
0193
0194 float dx = width*shadow;
0195 float dy = -dx;
0196 vtxs->add(xb+dx,yb+dy,zz);
0197 vtxs->add(xe+dx,yb+dy,zz);
0198 vtxs->add(xe+dx,ye+dy,zz);
0199 vtxs->add(xb+dx,ye+dy,zz);
0200 }
0201
0202 {//background :
0203 normal* nm = new normal;
0204 //nm->vec.value(); //default is z. ok.
0205 m_back_sep.add(nm);
0206
0207 if(gradient.value()) {
0208 // if(true) {
0209 //color gradient from (bottom,color) to (top,color_top)
0210
0211 atb_vertices* vtxs = new atb_vertices;
0212 vtxs->mode = gl::triangle_strip();
0213 m_back_sep.add(vtxs);
0214
0215 float zz = zback;
0216
0217 unsigned int ncol = 50;
0218 float dy = height/ncol;
0219 float ye = yb+dy;
0220
0221 colorf col_beg = color.value();
0222 colorf col_end = color_top.value();
0223
0224 float dr = (col_end.r()-col_beg.r())/ncol;
0225 float dg = (col_end.g()-col_beg.g())/ncol;
0226 float db = (col_end.b()-col_beg.b())/ncol;
0227 float da = (col_end.a()-col_beg.a())/ncol;
0228 vec4f dcol(dr,dg,db,da);
0229
0230 colorf col = col_beg;
0231
0232 vtxs->add(xb,yb,zz);
0233 vtxs->add_color(col);
0234
0235 vtxs->add(xe,yb,zz);
0236 vtxs->add_color(col);
0237
0238 for(unsigned int index=0;index<ncol;index++) {
0239 vtxs->add(xb,ye,zz);
0240 vtxs->add(xe,ye,zz);
0241
0242 vtxs->add_color(col);
0243 vtxs->add_color(col);
0244
0245 ye += dy;
0246 col += dcol;
0247 }
0248
0249 } else {
0250
0251 float zz = zback;
0252 float ye = height*0.5f;
0253
0254 sg::rgba* mat = new sg::rgba();
0255 mat->color = color;
0256 m_back_sep.add(mat);
0257
0258 vertices* vtxs = new vertices;
0259 vtxs->mode = gl::triangle_fan();
0260 m_back_sep.add(vtxs);
0261
0262 float r = height*corner_radius;
0263 if((r>(0.5f*height.value()))||(r>(0.5f*width.value()))) r = 0;
0264
0265 if((r>0) && corner_steps.value()) {
0266 float dangle = fhalf_pi()/float(corner_steps);
0267 unsigned int nslice = corner_steps;
0268
0269 vtxs->add(0,0,zz);
0270 vtxs->add(xe,yb+r,zz);
0271 vtxs->add(xe,ye-r,zz);
0272
0273 // top-right :
0274 float angle = dangle;
0275 if(corner_mask.value() & corner_top_right()) {
0276 float xc = xe-r;
0277 float yc = ye-r;
0278 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0279 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0280 }
0281 } else {
0282 angle += fhalf_pi();
0283 vtxs->add(xe,ye,zz);
0284 vtxs->add(xe-r,ye,zz);
0285 }
0286
0287 vtxs->add(xb+r,ye,zz);
0288
0289 // top-left :
0290 if(corner_mask.value() & corner_top_left()) {
0291 float xc = xb+r;
0292 float yc = ye-r;
0293 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0294 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0295 }
0296 } else {
0297 angle += fhalf_pi();
0298 vtxs->add(xb,ye,zz);
0299 vtxs->add(xb,ye-r,zz);
0300 }
0301
0302 vtxs->add(xb,yb+r,zz);
0303
0304 // bottom-left :
0305 if(corner_mask.value() & corner_bottom_left()) {
0306 float xc = xb+r;
0307 float yc = yb+r;
0308 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0309 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0310 }
0311 } else {
0312 angle += fhalf_pi();
0313 vtxs->add(xb,yb,zz);
0314 vtxs->add(xb+r,yb,zz);
0315 }
0316
0317 vtxs->add(xe-r,yb,zz);
0318
0319 // bottom-right :
0320 if(corner_mask.value() & corner_bottom_right()) {
0321 float xc = xe-r;
0322 float yc = yb+r;
0323 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0324 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0325 }
0326 } else {
0327 angle += fhalf_pi();
0328 vtxs->add(xe,yb,zz);
0329 vtxs->add(xe,yb+r,zz);
0330 }
0331
0332 } else {
0333 vtxs->add(xb,yb,zz);
0334 vtxs->add(xe,yb,zz);
0335 vtxs->add(xe,ye,zz);
0336 vtxs->add(xb,ye,zz);
0337 }
0338
0339 }}
0340
0341 if(border_visible.value()){
0342 float zz = zborder;
0343 float ye = height*0.5f;
0344
0345 sg::rgba* mat = new sg::rgba();
0346 mat->color = border_color;
0347 m_back_sep.add(mat);
0348
0349 draw_style* ds = new draw_style;
0350 ds->style = draw_lines;
0351 ds->line_width = border_line_width;
0352 m_back_sep.add(ds);
0353
0354 vertices* vtxs = new vertices;
0355 vtxs->mode = gl::line_strip();
0356 m_back_sep.add(vtxs);
0357
0358 float r = height*corner_radius;
0359 if((r>(0.5f*height.value()))||(r>(0.5f*width.value()))) r = 0;
0360
0361 if((r>0) && corner_steps.value()) {
0362 float dangle = fhalf_pi()/float(corner_steps);
0363 unsigned int nslice = corner_steps;
0364
0365 vtxs->add(xe,yb+r,zz);
0366 vtxs->add(xe,ye-r,zz);
0367
0368 // top-right :
0369 float angle = dangle;
0370 if(corner_mask.value() & corner_top_right()) {
0371 float xc = xe-r;
0372 float yc = ye-r;
0373 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0374 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0375 }
0376 } else {
0377 angle += fhalf_pi();
0378 vtxs->add(xe,ye,zz);
0379 vtxs->add(xe-r,ye,zz);
0380 }
0381
0382 vtxs->add(xb+r,ye,zz);
0383
0384 // top-left :
0385 if(corner_mask.value() & corner_top_left()) {
0386 float xc = xb+r;
0387 float yc = ye-r;
0388 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0389 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0390 }
0391 } else {
0392 angle += fhalf_pi();
0393 vtxs->add(xb,ye,zz);
0394 vtxs->add(xb,ye-r,zz);
0395 }
0396
0397 vtxs->add(xb,yb+r,zz);
0398
0399 // bottom-left :
0400 if(corner_mask.value() & corner_bottom_left()) {
0401 float xc = xb+r;
0402 float yc = yb+r;
0403 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0404 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0405 }
0406 } else {
0407 angle += fhalf_pi();
0408 vtxs->add(xb,yb,zz);
0409 vtxs->add(xb+r,yb,zz);
0410 }
0411
0412 vtxs->add(xe-r,yb,zz);
0413
0414 // bottom-right :
0415 if(corner_mask.value() & corner_bottom_right()) {
0416 float xc = xe-r;
0417 float yc = yb+r;
0418 for(unsigned int i=0;i<nslice;i++,angle+=dangle) {
0419 vtxs->add(r*fcos(angle)+xc,r*fsin(angle)+yc,zz);
0420 }
0421 } else {
0422 angle += fhalf_pi();
0423 vtxs->add(xe,yb,zz);
0424 vtxs->add(xe,yb+r,zz);
0425 }
0426
0427 } else {
0428 vtxs->add(xb,yb,zz);
0429 vtxs->add(xe,yb,zz);
0430 vtxs->add(xe,ye,zz);
0431 vtxs->add(xb,ye,zz);
0432 vtxs->add(xb,yb,zz);
0433 }
0434 }
0435
0436 }
0437 protected:
0438 separator m_back_sep;
0439 };
0440
0441 }}
0442
0443 #endif