g4tools  5.4.0
infos_box
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sg_infos_box
5 #define tools_sg_infos_box
6 
7 //
8 // In z the scene is within [0,0.5]
9 //
10 
11 #include "back_area"
12 #include "matrix"
13 #include "text_hershey"
14 #include "base_freetype"
15 #include "enums"
16 
17 #include "../colorf"
18 
19 namespace tools {
20 namespace sg {
21 
22 class infos_box : public back_area {
24 public:
27  sf<unsigned int> num_spaces; //in "number of one line height" unit.
28 
33  sf<float> line_width; // for text_hershey.
35 
42 public:
43  virtual const desc_fields& node_desc_fields() const {
45  static const desc_fields s_v(parent::node_desc_fields(),15, //WARNING : take care of count.
50 
52  font_hershey().c_str(),
53  font_lato_regular_ttf().c_str(),
54  font_roboto_bold_ttf().c_str(),
55  font_arial_ttf().c_str(),
56  font_arialbd_ttf().c_str(),
57  font_timesbd_ttf().c_str(),
58  font_symbol_ttf().c_str(),
59  font_stixgeneral_otf().c_str(),
60  font_helvetica_ttf().c_str(),
61  font_times_roman_ttf().c_str()
63 
69 
73 
80  );
81  return s_v;
82  }
83 private:
84  void add_fields(){
88 
89  add_field(&color);
90  add_field(&font);
95 
99  add_field(&lhjust);
100  add_field(&rhjust);
101  add_field(&confine);
102  }
103 public:
104  virtual void render(render_action& a_action) {
105  if(touched()) {
106  update_sg();
107  reset_touched();
108  }
109  if(back_visible.value()) m_back_sep.render(a_action);
110  m_sep.render(a_action);
111  }
112  virtual void pick(pick_action& a_action) {
113  if(touched()) {
114  update_sg();
115  reset_touched();
116  }
117  if(back_visible.value()) {
118  m_back_sep.pick(a_action);
119  if(a_action.done()) return;
120  }
121  }
122  virtual void search(search_action& a_action) {
123  if(touched()) {
124  update_sg();
125  reset_touched();
126  }
127  node::search(a_action);
128  if(a_action.done()) return;
129  if(back_visible.value()) {
130  m_back_sep.search(a_action);
131  if(a_action.done()) return;
132  }
133  m_sep.search(a_action);
134  if(a_action.done()) return;
135  }
136  virtual bool write(write_action& a_action) {
137  if(touched()) {
138  update_sg();
139  reset_touched();
140  }
141  if(back_visible.value()) if(!m_back_sep.write(a_action)) return false;
142  return m_sep.write(a_action);
143  }
144  virtual void bbox(bbox_action& a_action) {
145  if(touched()) {
146  update_sg();
147  reset_touched();
148  }
149  if(back_visible.value()) m_back_sep.bbox(a_action);
150  m_sep.bbox(a_action);
151  }
152 public:
153  infos_box(const base_freetype& a_ttf)
154  :parent()
155  ,lstrings()
156  ,rstrings()
157  ,num_spaces(4)
158 
159  ,color(colorf_black())
160  ,font(font_hershey())
163  ,line_width(1)
165 
166  ,back_visible(true)
167  ,wmargin_factor(0.9f)
168  ,hmargin_factor(0.9f)
169  ,lhjust(left)
170  ,rhjust(right)
171 
172  ,confine(false)
173 
174  ,m_ttf(a_ttf)
175  {
176  add_fields();
177  }
178  virtual ~infos_box(){}
179 public:
180  infos_box(const infos_box& a_from)
181  :parent(a_from)
182  ,lstrings(a_from.lstrings)
183  ,rstrings(a_from.rstrings)
184  ,num_spaces(a_from.num_spaces)
185 
186  ,color(a_from.color)
187  ,font(a_from.font)
189  ,encoding(a_from.encoding)
190  ,line_width(a_from.line_width)
191  ,front_face(a_from.front_face)
192 
193  ,back_visible(a_from.back_visible)
196  ,lhjust(a_from.lhjust)
197  ,rhjust(a_from.rhjust)
198 
199  ,confine(a_from.confine)
200 
201  ,m_ttf(a_from.m_ttf)
202  {
203  add_fields();
204  }
205  infos_box& operator=(const infos_box& a_from){
206  parent::operator=(a_from);
207  lstrings = a_from.lstrings;
208  rstrings = a_from.rstrings;
209  num_spaces = a_from.num_spaces;
210 
211  color = a_from.color;
212  font = a_from.font;
213  font_modeling = a_from.font_modeling;
214  encoding = a_from.encoding;
215  line_width = a_from.line_width;
216  front_face = a_from.front_face;
217 
218  back_visible = a_from.back_visible;
221  lhjust = a_from.lhjust;
222  rhjust = a_from.rhjust;
223 
224  confine = a_from.confine;
225 
226  return *this;
227  }
228 public:
229  void update_sg() {
230  // have this method public in order to use it in plotter.
231  // This is so because infos_box::height is an output field
232  // needed in plotter to place the box.
233 
234  m_back_sep.clear(); //back_area::update_sg done last.
235 
236  m_sep.clear();
237 
238  if(width.value()<=0) return;
239  if(confine) {
240  if(height.value()<=0) return;
241  }
242 
243  {bool empty = true;
244  std::vector<std::string>::const_iterator it;
245  for(it=lstrings.values().begin();it!=lstrings.values().end();++it) {
246  if((*it).size()) {empty = false;break;}
247  }
248  if(empty){
249  for(it=rstrings.values().begin();it!=rstrings.values().end();++it) {
250  if((*it).size()) {empty = false;break;}
251  }
252  }
253  if(empty) {
254  //parent::update_sg();
255  return;
256  }}
257 
258  rgba* mat = new rgba();
259  mat->color = color;
260  m_sep.add(mat);
261 
262  if(font==font_hershey()) {
263  draw_style* ds = new draw_style;
264  ds->style.value(draw_lines);
265  //ds->line_pattern = line_pattern;
266  ds->line_width = line_width;
267  m_sep.add(ds);
268  }
269 
273  base_text* ltext = 0;
274  matrix* ltsf = 0;
275 
276  {separator* sep = new separator;
277  m_sep.add(sep);
278  ltsf = new matrix;
279  sep->add(ltsf);
280  if(font==font_hershey()) {
282  ltext = text;
284  sep->add(text);
285  } else {
287  //TTNODE* text = new TTNODE;
288  ltext = text;
289  text->font = font;
290  text->modeling = font_modeling;
291  sep->add(text);
292  }
293  ltext->strings = lstrings;
294  ltext->hjust = lhjust;}
295 
299  base_text* rtext = 0;
300  matrix* rtsf = 0;
301 
302  {separator* sep = new separator;
303  m_sep.add(sep);
304  rtsf = new matrix;
305  sep->add(rtsf);
306  if(font==font_hershey()) {
308  rtext = text;
310  sep->add(text);
311  } else {
313  //TTNODE* text = new TTNODE;
314  rtext = text;
315  text->font = font;
316  text->modeling = font_modeling;
317  sep->add(text);
318  }
319  rtext->strings = rstrings;
320  rtext->hjust = rhjust;}
321 
325  base_text* mtext = 0;
326  {std::string s(num_spaces,' ');
327  if(font==font_hershey()) {
329  mtext = text;
330  text->strings.add(s);
331  text->hjust = left;
332  } else {
334  //TTNODE* text = new TTNODE;
335  mtext = text;
336  text->strings.add(s);
337  text->hjust = left;
338  text->font = font;
339  text->modeling = font_modeling;
340  }}
341 
342  //sf<float> zfront ?
343  float zz = back_visible.value()?0.01f:0;
344 
345  float fw = width * wmargin_factor;
346 
347  if(confine) {
348  // left right texts may overlap.
349 
350  // adjust height :
351  float fh = height * hmargin_factor;
352  float th = fh;
353  {float mn_x,mn_y,mn_z;
354  float mx_x,mx_y,mx_z;
355  ltext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z);
356  float bxh = mx_y-mn_y;
357  // adjust box height :
358  // fh -> bxh then to have fh :
359  if(!bxh) {
360  m_sep.clear();
361  parent::update_sg();
362  delete mtext;
363  return;
364  }
365  th = fh*fh/bxh;}
366 
367  ltext->height = th;
368  rtext->height = th;
369 
370  {float mn_x,mn_y,mn_z;
371  float mx_x,mx_y,mx_z;
372  ltext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z);
373  float lw = mx_x-mn_x;
374  float xtrans = (fw-lw)*0.5f; //left justified
375  float xx = -(mn_x+mx_x)*0.5F-xtrans;
376  float yy = -(mn_y+mx_y)*0.5F;
377  ltsf->set_translate(xx,yy,zz);}
378 
379  {float mn_x,mn_y,mn_z;
380  float mx_x,mx_y,mx_z;
381  rtext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z);
382  rtext->hjust = right;
383  float xx = fw*0.5f;
384  float yy = -(mn_y+mx_y)*0.5F;
385  rtsf->set_translate(xx,yy,zz);}
386 
387  parent::update_sg();
388 
389  } else {
390  // height of the info box is an output of the below.
391  // The input to compute the geometry is the width of the infos
392  // box, the left/right texts and the space between
393  // left/right texts.
394 
395  // from a text height of th=1, and a left/right sep
396  // of num_spaces*th, get width of the box :
397 
398  float th = 1;
399 
400  float lmn_x,lmn_y,lmn_z;
401  float lmx_x,lmx_y,lmx_z;
402  ltext->get_bounds(th,lmn_x,lmn_y,lmn_z,lmx_x,lmx_y,lmx_z);
403  float lw = (lmx_x<lmn_x)?0:lmx_x-lmn_x;
404 
405  float rmn_x,rmn_y,rmn_z;
406  float rmx_x,rmx_y,rmx_z;
407  rtext->get_bounds(th,rmn_x,rmn_y,rmn_z,rmx_x,rmx_y,rmx_z);
408  float rw = (rmx_x<rmn_x)?0:rmx_x-rmn_x;
409 
410  float mmn_x,mmn_y,mmn_z;
411  float mmx_x,mmx_y,mmx_z;
412  mtext->get_bounds(th,mmn_x,mmn_y,mmn_z,mmx_x,mmx_y,mmx_z);
413  float mw = (mmx_x<mmn_x)?0:mmx_x-mmn_x;
414 
415  float winfos = lw+mw+rw;
416  if(!winfos) {
417  m_sep.clear();
418  height = 1;
419  parent::update_sg();
420  delete mtext;
421  return;
422  }
423 
424  // assuming that text size is linear relative
425  // to th, we get the th needed to match infos_box.width :
426  // th -> winfos then to have fw :
427  th = fw*th/winfos;
428 
429  // place the left/right texts by rescaling them to
430  // the new th :
431  ltext->height = th;
432  ltext->get_bounds(th,lmn_x,lmn_y,lmn_z,lmx_x,lmx_y,lmx_z);
433  float hinfos = (lmx_x<lmn_x)?0:lmx_y-lmn_y;
434 
435  float xx = -fw*0.5F; //left justified.
436  float ty = -ltext->ascent(th)+hinfos*0.5f;
437  ltsf->set_translate(xx,ty,zz);
438 
439  rtext->height = th;
440  if(rtext->hjust==right) {
441  xx = fw*0.5F;
442  } else if(rtext->hjust==left) {
443  rtext->get_bounds(th,rmn_x,rmn_y,rmn_z,rmx_x,rmx_y,rmx_z);
444  rw = (rmx_x<rmn_x)?0:rmx_x-rmn_x;
445  xx = fw*0.5F-rw;
446  } else { //center
447  rtext->get_bounds(th,rmn_x,rmn_y,rmn_z,rmx_x,rmx_y,rmx_z);
448  rw = (rmx_x<rmn_x)?0:rmx_x-rmn_x;
449  xx = fw*0.5F-rw*0.5F;
450  }
451  rtsf->set_translate(xx,ty,zz);
452 
453  // set infos_box.height :
454  height = hinfos/hmargin_factor;
455 
456  //done last because of the upper height setup.
457  parent::update_sg();
458 
459  }
460 
461  delete mtext;
462 
463  }
464 protected:
466 
468 };
469 
470 }}
471 
472 #endif
tools::sg::infos_box::operator=
infos_box & operator=(const infos_box &a_from)
Definition: infos_box:205
tools::sg::font_pixmap
@ font_pixmap
Definition: enums:94
tools::sg::group::search
virtual void search(search_action &a_action)
Definition: group:51
tools::sg::base_text
Definition: base_text:17
tools::sg::infos_box::lstrings
mf_string lstrings
Definition: infos_box:25
tools::sg::winding_ccw
@ winding_ccw
Definition: enums:105
tools::sg::bmf::add
void add(const T &a_value)
Definition: bmf:73
tools::sg::infos_box::encoding
sf_string encoding
Definition: infos_box:32
TOOLS_ARG_FIELD_DESC_ENUMS_BEG
#define TOOLS_ARG_FIELD_DESC_ENUMS_BEG(a__field, a__num)
Definition: field:77
tools::sg::infos_box::m_sep
separator m_sep
Definition: infos_box:467
tools::sg::draw_style::line_width
sf< float > line_width
Definition: draw_style:22
tools::sg::base_text::height
sf< float > height
Definition: base_text:30
tools::sg::text::font
sf_string font
Definition: text:32
tools::sg::base_text::hjust
sf_enum< sg::hjust > hjust
Definition: base_text:31
tools::sg::bbox_action
Definition: bbox_action:15
tools::sg::matrix::set_translate
void set_translate(float a_x, float a_y, float a_z)
Definition: matrix:90
TOOLS_NODE
#define TOOLS_NODE(a__class, a__sclass, a__parent)
Definition: node:324
tools::sg::search_action::done
bool done() const
Definition: search_action:68
tools::sg::encoding_PAW
const std::string & encoding_PAW()
Definition: strings:42
tools::sg::write_action
Definition: write_action:21
tools::sg::rgba
Definition: rgba:16
tools::sg::text
Definition: text:25
tools::sg::back_area::m_back_sep
separator m_back_sep
Definition: back_area:438
tools::sg::right
@ right
Definition: enums:76
tools::sg::base_freetype
Definition: base_freetype:16
tools::sg::infos_box::rhjust
sf_enum< hjust > rhjust
Definition: infos_box:40
tools::sg::base_text::get_bounds
virtual void get_bounds(float, float &, float &, float &, float &, float &, float &) const =0
tools::sg::draw_style
Definition: draw_style:18
tools::sg::infos_box::font_modeling
sf_enum< sg::font_modeling > font_modeling
Definition: infos_box:31
tools::sg::text::strings
mf_string strings
Definition: text:28
tools::sg::text_hershey
Definition: text_hershey:82
tools::sg::base_text::ascent
virtual float ascent(float) const =0
tools::sg::desc_fields
Definition: field_desc:148
tools::sg::color
Definition: color:16
matrix
tools::sg::bmf::values
const std::vector< T > & values() const
Definition: bmf:71
tools::sg::infos_box::wmargin_factor
sf< float > wmargin_factor
Definition: infos_box:37
tools::sg::separator::pick
virtual void pick(pick_action &a_action)
Definition: separator:31
tools::sg::infos_box::infos_box
infos_box(const infos_box &a_from)
Definition: infos_box:180
tools::sg::pick_action
Definition: pick_action:59
tools::sg::separator::bbox
virtual void bbox(bbox_action &a_action)
Definition: separator:38
tools::sg::left
@ left
Definition: enums:74
tools::sg::sf_vec
Definition: sf_vec:15
tools::sg::infos_box::line_width
sf< float > line_width
Definition: infos_box:33
base_freetype
tools::sg::infos_box::~infos_box
virtual ~infos_box()
Definition: infos_box:178
tools::sg::infos_box::infos_box
infos_box(const base_freetype &a_ttf)
Definition: infos_box:153
tools::sg::infos_box::write
virtual bool write(write_action &a_action)
Definition: infos_box:136
tools::sg::sf_enum< sg::font_modeling >
tools::sg::infos_box
Definition: infos_box:22
tools::sg::font_modeling
font_modeling
Definition: enums:91
tools::sg::group::add
void add(node *a_node)
Definition: group:96
tools::sg::font_outline
@ font_outline
Definition: enums:92
TOOLS_ARG_FIELD_DESC_OPTS_BEG
#define TOOLS_ARG_FIELD_DESC_OPTS_BEG(a__field, a__num)
Definition: field:87
tools::sg::search_action
Definition: search_action:19
tools::sg::back_area::height
sf< float > height
Definition: back_area:31
tools::sg::infos_box::update_sg
void update_sg()
Definition: infos_box:229
tools::sg::node::search
virtual void search(search_action &a_action)
Definition: node:56
tools::sg::render_action
Definition: render_action:24
tools::sg::separator
Definition: separator:15
TOOLS_ARG_FIELD_DESC_OPTS_END
#define TOOLS_ARG_FIELD_DESC_OPTS_END
Definition: field:89
tools::sg::infos_box::m_ttf
const base_freetype & m_ttf
Definition: infos_box:465
tools::sg::infos_box::font
sf_string font
Definition: infos_box:30
tools::sg::mf_string
Definition: mf:76
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::separator::render
virtual void render(render_action &a_action)
Definition: separator:18
tools::sg::node::add_field
void add_field(field *a_field)
Definition: node:128
tools::sg::base_freetype::create
static base_freetype * create(const base_freetype &a_ttf)
Definition: base_freetype:108
tools::sg::matrix
Definition: matrix:19
tools::sg::infos_box::num_spaces
sf< unsigned int > num_spaces
Definition: infos_box:27
enums
tools::sg::infos_box::back_visible
sf< bool > back_visible
Definition: infos_box:36
tools::sg::infos_box::confine
sf< bool > confine
Definition: infos_box:41
back_area
tools::sg::base_text::strings
mf_string strings
Definition: base_text:29
tools::sg::pick_action::done
bool done() const
Definition: pick_action:255
TOOLS_FIELD_DESC_NODE_CLASS
#define TOOLS_FIELD_DESC_NODE_CLASS(a__class)
Definition: field:68
tools::mat
Definition: mat:19
tools::sg::infos_box::pick
virtual void pick(pick_action &a_action)
Definition: infos_box:112
tools::sg::text::hjust
sf_enum< sg::hjust > hjust
Definition: text:48
TOOLS_ARG_ENUM
#define TOOLS_ARG_ENUM(a__value)
Definition: field:85
tools::sg::back_area
Definition: back_area:19
tools::sg::infos_box::search
virtual void search(search_action &a_action)
Definition: infos_box:122
tools::sg::group::clear
void clear()
Definition: group:235
tools::sg::sf_string
Definition: sf_string:15
tools::sg::infos_box::hmargin_factor
sf< float > hmargin_factor
Definition: infos_box:38
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::sg::infos_box::rstrings
mf_string rstrings
Definition: infos_box:26
tools::sg::font_hershey
const std::string & font_hershey()
Definition: strings:186
tools::sep
const std::string & sep()
Definition: sep:11
tools::sg::back_area::width
sf< float > width
Definition: back_area:30
tools::sg::infos_box::color
sf_vec< colorf, float > color
Definition: infos_box:29
tools::sg::font_filled
@ font_filled
Definition: enums:93
text_hershey
tools::sg::infos_box::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: infos_box:43
tools::sg::draw_style::style
sf_enum< draw_type > style
Definition: draw_style:21
tools::sg::text::encoding
sf_string encoding
Definition: text:35
tools::sg::draw_lines
@ draw_lines
Definition: enums:192
tools::sg::node::touched
virtual bool touched()
Definition: node:96
tools::sg::infos_box::bbox
virtual void bbox(bbox_action &a_action)
Definition: infos_box:144
tools::sg::infos_box::render
virtual void render(render_action &a_action)
Definition: infos_box:104
tools::sg::infos_box::front_face
sf_enum< winding_type > front_face
Definition: infos_box:34
tools::sg::sf< unsigned int >
TOOLS_ARG_FIELD_DESC
#define TOOLS_ARG_FIELD_DESC(a__field)
Definition: field:71
tools::sg::infos_box::lhjust
sf_enum< hjust > lhjust
Definition: infos_box:39
tools::sg::node::reset_touched
virtual void reset_touched()
Definition: node:102
TOOLS_ARG_FIELD_DESC_ENUMS_END
#define TOOLS_ARG_FIELD_DESC_ENUMS_END
Definition: field:83
tools::sg::separator::write
virtual bool write(write_action &a_action)
Definition: separator:59