g4tools  5.4.0
legend
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_legend
5 #define tools_sg_legend
6 
7 // To handle a colored "rep" (line or marker) with a text at right.
8 
9 #include "back_area"
10 #include "matrix"
11 #include "text_hershey"
12 #include "base_freetype"
13 #include "markers"
14 #include "enums"
15 
16 #include "../colorf"
17 //#include "../paw2stix"
18 #include "text_valop"
19 
20 namespace tools {
21 namespace sg {
22 
23 class legend : public back_area {
25 public:
27 
28  sf_vec<colorf,float> color; //color of the "rep"
29  // text atbs :
30  //sf<float> transparency;
34  //sf<float> line_width; // for text_hershey.
35 
41  //sf<bool> confine;
42 
45 public:
46  virtual const desc_fields& node_desc_fields() const {
48  static const desc_fields s_v(parent::node_desc_fields(),12, //WARNING : take care of count.
52 
58 
67  );
68  return s_v;
69  }
70 private:
71  void add_fields(){
73 
74  add_field(&color);
75  add_field(&font);
78 
82  add_field(&lhjust);
83  add_field(&rhjust);
86  }
87 public:
88  virtual void render(render_action& a_action) {
89  if(touched()) {
90  update_sg();
91  reset_touched();
92  }
93  if(back_visible.value()) m_back_sep.render(a_action);
94  m_sep.render(a_action);
95  }
96  virtual void pick(pick_action& a_action) {
97  if(touched()) {
98  update_sg();
99  reset_touched();
100  }
101  if(back_visible.value()) {
102  m_back_sep.pick(a_action);
103  if(a_action.done()) return;
104  }
105  }
106  virtual void search(search_action& a_action) {
107  if(touched()) {
108  update_sg();
109  reset_touched();
110  }
111  node::search(a_action);
112  if(a_action.done()) return;
113  if(back_visible.value()) {
114  m_back_sep.search(a_action);
115  if(a_action.done()) return;
116  }
117  m_sep.search(a_action);
118  if(a_action.done()) return;
119  }
120  virtual void bbox(bbox_action& a_action) {
121  if(touched()) {
122  update_sg();
123  reset_touched();
124  }
125  if(back_visible.value()) m_back_sep.bbox(a_action);
126  m_sep.bbox(a_action);
127  }
128 public:
129  legend(const base_freetype& a_ttf)
130  :parent()
131  ,strings()
132 
133  ,color(colorf_black())
134  //,transparency(0)
135  ,font(font_hershey())
138  //,line_width(1)
139 
140  ,back_visible(true)
141  ,wmargin_factor(0.9f)
142  ,hmargin_factor(0.9f)
143  ,lhjust(left)
144  ,rhjust(right)
145 
147  ,marker_size(10)
148 
149  //,confine(false)
150 
151  ,m_ttf(a_ttf)
152  {
153  add_fields();
154  }
155  virtual ~legend(){}
156 public:
157  legend(const legend& a_from)
158  :parent(a_from)
159  ,strings(a_from.strings)
160 
161  ,color(a_from.color)
162  //,transparency(a_from.transparency)
163  ,font(a_from.font)
165  ,encoding(a_from.encoding)
166  //,line_width(a_from.line_width)
167 
168  ,back_visible(a_from.back_visible)
171  ,lhjust(a_from.lhjust)
172  ,rhjust(a_from.rhjust)
173 
174  ,marker_style(a_from.marker_style)
175  ,marker_size(a_from.marker_size)
176 
177  //,confine(a_from.confine)
178 
179  ,m_ttf(a_from.m_ttf)
180  {
181  add_fields();
182  }
183  legend& operator=(const legend& a_from){
184  parent::operator=(a_from);
185  strings = a_from.strings;
186 
187  color = a_from.color;
188  //transparency = a_from.transparency;
189  font = a_from.font;
190  font_modeling = a_from.font_modeling;
191  encoding = a_from.encoding;
192  //line_width = a_from.line_width;
193 
194  back_visible = a_from.back_visible;
197  lhjust = a_from.lhjust;
198  rhjust = a_from.rhjust;
199 
200  marker_style = a_from.marker_style;
201  marker_size = a_from.marker_size;
202 
203  //confine = a_from.confine;
204 
205  return *this;
206  }
207 public:
208  void update_sg() {
209  // have this method public in order to use it in plotter.
210  // This is so because legend::height is an output field
211  // needed in plotter to place the box.
212 
213  m_back_sep.clear(); //back_area::update_sg done last.
214 
215  m_sep.clear();
216 
217  if(width.value()<=0) return;
218  //if(confine) {
219  // if(height.value()<=0) return;
220  //}
221 
222  {bool empty = true;
223  std::vector<std::string>::const_iterator it;
224  for(it=strings.values().begin();it!=strings.values().end();++it) {
225  if((*it).size()) {empty = false;break;}
226  }
227  if(empty) return;}
228 
229  //sf<float> zfront ?
230  float zz = back_visible.value()?0.01f:0;
231 
232  //bool rep_line = false;
233 
234  /*
235  float fw = 0;
236  float fh = 0;
237  float lw = 0;
238  float rw = 0;
239  if(rep_line) {
240  fw = width * wmargin_factor;
241  fh = height * hmargin_factor;
242 
243  lw = fw*0.5f*0.95f;
244  rw = fw*0.5f;
245  }*/
249 
250  {separator* _sep = new separator;
251  m_sep.add(_sep);
252 
253  rgba* mat = new rgba();
254  mat->color = color;
255  _sep->add(mat);
256 
257 /* if(rep_line) {
258  matrix* ltsf = new matrix;
259  float xx = -fw*0.5F; //left justified.
260  ltsf->set_translate(xx,0,zz);
261  _sep->add(ltsf);
262 
263  draw_style* ds = new draw_style;
264  ds->style = draw_lines;
265  _sep->add(ds);
266 
267  vertices* vtxs = new vertices;
268  vtxs->mode = gl::line_strip();
269  vtxs->add( 0,0,zz);
270  vtxs->add(lw,0,zz);
271  _sep->add(vtxs);
272 
273  } else*/ { // rep marker :
274  if(marker_style.value()==marker_dot) {
275  draw_style* ds = new draw_style;
276  ds->style = draw_points;
277  ds->point_size = marker_size;
278  _sep->add(ds);
279  vertices* vtxs = new vertices;
280  vtxs->mode = gl::points();
281  vtxs->add(-width*0.5f+height*0.5f,0,zz);
282  _sep->add(vtxs);
283  } else {
284  markers* vtxs = new markers;
285  vtxs->size = marker_size;
286  vtxs->style = marker_style;
287  vtxs->add(-width*0.5f+height*0.5f,0,zz);
288  _sep->add(vtxs);
289  }
290  }
291 
292  }
293 
297  base_text* rtext = 0;
298  matrix* rtsf = 0;
299 
300  {separator* _sep = new separator;
301  m_sep.add(_sep);
302 
303  rgba* mat = new rgba();
304  mat->color = colorf_black();
305  _sep->add(mat);
306 
307  if(font==font_hershey()) {
308  draw_style* ds = new draw_style;
309  ds->style.value(draw_lines);
310  _sep->add(ds);
311  }
312 
313  rtsf = new matrix;
314  _sep->add(rtsf);
315  if(font==font_hershey()) {
318  text->strings = strings;
319  _sep->add(text);
320  rtext = text;
321  } else {
322  if(encoding==encoding_PAW()) {
324  text->font = font;
326  text->strings = strings;
327  _sep->add(text);
328  rtext = text;
329  } else {
331  text->font = font;
332  text->modeling = font_modeling;
333  text->strings = strings;
334  _sep->add(text);
335  rtext = text;
336  }
337  }
338  rtext->hjust = rhjust;}
339 
343 
344 /* if(rep_line) {
345 
346  // adjust width :
347  float th = fh;
348  {float mn_x,mn_y,mn_z;
349  float mx_x,mx_y,mx_z;
350  rtext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z);
351  float bxw = mx_x-mn_x;
352  if(!bxw) {
353  m_sep.clear();
354  parent::update_sg();
355  return;
356  }
357  // adjust box width :
358  // fh -> bxw then to have rw :
359  float max_width = rw;
360  if(bxw>max_width) th = max_width*fh/bxw;}
361 
362  rtext->height = th;
363 
364  {float mn_x,mn_y,mn_z;
365  float mx_x,mx_y,mx_z;
366  rtext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z);
367  rtext->hjust = right;
368  float xx = fw*0.5f;
369  float yy = -(mn_y+mx_y)*0.5F;
370  rtsf->set_translate(xx,yy,zz);}
371 
372  } else*/ { //rep mark :
373 
374  // adjust width :
375  float th = height;
376  {float mn_x,mn_y,mn_z;
377  float mx_x,mx_y,mx_z;
378  rtext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z);
379  float bxw = mx_x-mn_x;
380  if(!bxw) {
381  m_sep.clear();
382  parent::update_sg();
383  return;
384  }
385  // adjust box width :
386  // height -> bxw then to have a wanted width :
387  float max_width = (width-height)*wmargin_factor;
388  if(bxw>max_width) th = max_width*height/bxw;
389  if(th<0) {
390  m_sep.clear();
391  parent::update_sg();
392  return;
393  }}
394 
395  rtext->height = th;
396  {float mn_x,mn_y,mn_z;
397  float mx_x,mx_y,mx_z;
398  rtext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z);
399  rtext->hjust = left;
400  float yy = -(mn_y+mx_y)*0.5F;
401  rtsf->set_translate(-width*0.5f+height,yy,zz);}
402 
403  }
404 
405  parent::update_sg();
406 
407  }
408 protected:
411 };
412 
413 }}
414 
415 #endif
tools::sg::markers::add
void add(const VEC &a_v)
Definition: markers:594
tools::sg::markers
Definition: markers:21
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::legend::font_modeling
sf_enum< sg::font_modeling > font_modeling
Definition: legend:32
tools::sg::base_text
Definition: base_text:17
tools::sg::vertices::mode
sf< gl::mode_t > mode
Definition: vertices:26
tools::sg::legend::encoding
sf_string encoding
Definition: legend:33
TOOLS_ARG_FIELD_DESC_ENUMS_BEG
#define TOOLS_ARG_FIELD_DESC_ENUMS_BEG(a__field, a__num)
Definition: field:77
tools::sg::legend::legend
legend(const base_freetype &a_ttf)
Definition: legend:129
tools::sg::base_text::height
sf< float > height
Definition: base_text:30
tools::sg::text::font
sf_string font
Definition: text:32
tools::sg::legend::m_ttf
const base_freetype & m_ttf
Definition: legend:410
tools::sg::marker_style
marker_style
Definition: enums:17
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::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::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::marker_dot
@ marker_dot
Definition: enums:18
tools::sg::text::strings
mf_string strings
Definition: text:28
tools::sg::draw_points
@ draw_points
Definition: enums:191
tools::sg::text_hershey
Definition: text_hershey:82
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::separator::pick
virtual void pick(pick_action &a_action)
Definition: separator:31
tools::sg::pick_action
Definition: pick_action:59
tools::sg::separator::bbox
virtual void bbox(bbox_action &a_action)
Definition: separator:38
tools::sg::legend::~legend
virtual ~legend()
Definition: legend:155
tools::sg::left
@ left
Definition: enums:74
tools::sg::sf_vec
Definition: sf_vec:15
tools::sg::legend::font
sf_string font
Definition: legend:31
base_freetype
tools::sg::legend::marker_size
sf< float > marker_size
Definition: legend:44
tools::sg::sf_enum< sg::font_modeling >
tools::sg::legend::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: legend:46
tools::sg::markers::size
sf< float > size
Definition: markers:26
tools::sg::font_modeling
font_modeling
Definition: enums:91
tools::sg::group::add
void add(node *a_node)
Definition: group:96
tools::sg::legend::back_visible
sf< bool > back_visible
Definition: legend:36
tools::sg::legend::m_sep
separator m_sep
Definition: legend:409
tools::sg::text::font_modeling
sf_enum< sg::font_modeling > font_modeling
Definition: text:33
tools::sg::font_outline
@ font_outline
Definition: enums:92
tools::gl::points
mode_t points()
Definition: glprims:16
tools::sg::search_action
Definition: search_action:19
tools::sg::back_area::height
sf< float > height
Definition: back_area:31
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::sg::mf_string
Definition: mf:76
tools::sg::legend::update_sg
void update_sg()
Definition: legend:208
tools::sg::legend::color
sf_vec< colorf, float > color
Definition: legend:28
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::legend::wmargin_factor
sf< float > wmargin_factor
Definition: legend:37
enums
tools::sg::draw_style::point_size
sf< float > point_size
Definition: draw_style:24
back_area
tools::sg::text_valop
Definition: text_valop:17
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::legend::rhjust
sf_enum< hjust > rhjust
Definition: legend:40
tools::sg::legend::lhjust
sf_enum< hjust > lhjust
Definition: legend:39
TOOLS_ARG_ENUM
#define TOOLS_ARG_ENUM(a__value)
Definition: field:85
tools::sg::legend::marker_style
sf_enum< sg::marker_style > marker_style
Definition: legend:43
tools::sg::back_area
Definition: back_area:19
tools::sg::group::clear
void clear()
Definition: group:235
tools::sg::sf_string
Definition: sf_string:15
tools::sg::legend::render
virtual void render(render_action &a_action)
Definition: legend:88
tools::sg::legend::hmargin_factor
sf< float > hmargin_factor
Definition: legend:38
tools::sg::legend::pick
virtual void pick(pick_action &a_action)
Definition: legend:96
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::sg::legend::strings
mf_string strings
Definition: legend:26
tools::sg::font_hershey
const std::string & font_hershey()
Definition: strings:186
tools::sg::legend::legend
legend(const legend &a_from)
Definition: legend:157
tools::sg::back_area::width
sf< float > width
Definition: back_area:30
markers
tools::sg::font_filled
@ font_filled
Definition: enums:93
text_hershey
tools::sg::legend::operator=
legend & operator=(const legend &a_from)
Definition: legend:183
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::legend
Definition: legend:23
tools::sg::draw_lines
@ draw_lines
Definition: enums:192
tools::sg::node::touched
virtual bool touched()
Definition: node:96
tools::sg::vertices::add
void add(const VEC &a_v)
Definition: vertices:155
tools::sg::legend::search
virtual void search(search_action &a_action)
Definition: legend:106
tools::sg::sf< bool >
tools::sg::markers::style
sf_enum< marker_style > style
Definition: markers:24
TOOLS_ARG_FIELD_DESC
#define TOOLS_ARG_FIELD_DESC(a__field)
Definition: field:71
tools::sg::node::reset_touched
virtual void reset_touched()
Definition: node:102
tools::sg::legend::bbox
virtual void bbox(bbox_action &a_action)
Definition: legend:120
text_valop
TOOLS_ARG_FIELD_DESC_ENUMS_END
#define TOOLS_ARG_FIELD_DESC_ENUMS_END
Definition: field:83
tools::sg::vertices
Definition: vertices:22