// Copyright (C) 2010, Guy Barrand. All rights reserved. // See the file tools.license for terms. #ifndef tools_sg_legend #define tools_sg_legend // To handle a colored "rep" (line or marker) with a text at right. #include "back_area" #include "matrix" #include "text_hershey" #include "base_freetype" #include "enums" #include "../colorf" namespace tools { namespace sg { class legend : public back_area { TOOLS_NODE(legend,tools::sg::legend,back_area) public: mf_string strings; sf_vec color; //color of the "rep" // text atbs : //sf transparency; sf_string font; sf_string encoding; //sf line_width; // for text_hershey. sf back_visible; sf wmargin_factor; sf hmargin_factor; sf_enum lhjust; sf_enum rhjust; //sf confine; public: virtual const std::vector& node_fields() const { TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::legend) static std::vector s_v; if(s_v.empty()) { s_v = parent::node_fields(); TOOLS_ADD_FIELD_DESC(strings) TOOLS_ADD_FIELD_DESC(color) TOOLS_ADD_FIELD_DESC(font) TOOLS_ADD_FIELD_DESC(encoding); TOOLS_ADD_FIELD_DESC(back_visible) TOOLS_ADD_FIELD_DESC(wmargin_factor) TOOLS_ADD_FIELD_DESC(hmargin_factor) TOOLS_ADD_FIELD_DESC(lhjust) TOOLS_ADD_FIELD_DESC(rhjust) } return s_v; } private: void add_fields(){ add_field(&strings); add_field(&color); add_field(&font); add_field(&encoding); add_field(&back_visible); add_field(&wmargin_factor); add_field(&hmargin_factor); add_field(&lhjust); add_field(&rhjust); } public: virtual void render(render_action& a_action) { if(touched()) { update_sg(); reset_touched(); } if(back_visible.value()) m_back_sep.render(a_action); m_sep.render(a_action); } virtual void pick(pick_action& a_action) { if(touched()) { update_sg(); reset_touched(); } if(back_visible.value()) { m_back_sep.pick(a_action); if(a_action.done()) return; } } virtual void search(search_action& a_action) { node::search(a_action); if(a_action.done()) return; if(touched()) { update_sg(); reset_touched(); } if(back_visible.value()) { m_back_sep.search(a_action); if(a_action.done()) return; } m_sep.search(a_action); if(a_action.done()) return; } virtual void bbox(bbox_action& a_action) { if(touched()) { update_sg(); reset_touched(); } if(back_visible.value()) m_back_sep.bbox(a_action); m_sep.bbox(a_action); } public: legend(const base_freetype& a_ttf) :parent() ,strings() ,color(colorf_black()) //,transparency(0) ,font(font_hershey()) ,encoding(encoding_PAW()) //,line_width(1) ,back_visible(true) ,wmargin_factor(0.9f) ,hmargin_factor(0.9f) ,lhjust(left) ,rhjust(right) //,confine(false) ,m_ttf(a_ttf) { add_fields(); } virtual ~legend(){} public: legend(const legend& a_from) :parent(a_from) ,strings(a_from.strings) ,color(a_from.color) //,transparency(a_from.transparency) ,font(a_from.font) ,encoding(a_from.encoding) //,line_width(a_from.line_width) ,back_visible(a_from.back_visible) ,wmargin_factor(a_from.wmargin_factor) ,hmargin_factor(a_from.hmargin_factor) ,lhjust(a_from.lhjust) ,rhjust(a_from.rhjust) //,confine(a_from.confine) ,m_ttf(a_from.m_ttf) { add_fields(); } legend& operator=(const legend& a_from){ parent::operator=(a_from); strings = a_from.strings; color = a_from.color; //transparency = a_from.transparency; font = a_from.font; encoding = a_from.encoding; //line_width = a_from.line_width; back_visible = a_from.back_visible; wmargin_factor = a_from.wmargin_factor; hmargin_factor = a_from.hmargin_factor; lhjust = a_from.lhjust; rhjust = a_from.rhjust; //confine = a_from.confine; return *this; } public: void update_sg() { // have this method public in order to use it in plotter. // This is so because legend::height is an output field // needed in plotter to place the box. m_back_sep.clear(); //back_area::update_sg done last. m_sep.clear(); if(width.value()<=0) return; //if(confine) { // if(height.value()<=0) return; //} {bool empty = true; std::vector::const_iterator it; for(it=strings.values().begin();it!=strings.values().end();++it) { if((*it).size()) {empty = false;break;} } if(empty) return;} //sf zfront ? float zz = back_visible.value()?0.01f:0; float fw = width * wmargin_factor; //////////////////////////////////////////////////////////// /// rep at left //////////////////////////////////////////// //////////////////////////////////////////////////////////// matrix* ltsf = 0; float lw = fw*0.5f*0.95f; {separator* _sep = new separator; m_sep.add(_sep); ltsf = new matrix; _sep->add(ltsf); rgba* mat = new rgba(); mat->color = color; _sep->add(mat); draw_style* ds = new draw_style; ds->style = draw_style::lines; _sep->add(ds); vertices* vtxs = new vertices; vtxs->mode = gl::line_strip(); _sep->add(vtxs); vtxs->add( 0,0,zz); vtxs->add(lw,0,zz);} //////////////////////////////////////////////////////////// /// right text ///////////////////////////////////////////// //////////////////////////////////////////////////////////// base_text* rtext = 0; matrix* rtsf = 0; {separator* _sep = new separator; m_sep.add(_sep); rgba* mat = new rgba(); mat->color = colorf_black(); _sep->add(mat); if(font==font_hershey()) { draw_style* ds = new draw_style; ds->style.value(draw_style::lines); _sep->add(ds); } rtsf = new matrix; _sep->add(rtsf); if(font==font_hershey()) { text_hershey* text = new text_hershey; rtext = text; text->encoding = encoding; _sep->add(text); } else { base_freetype* text = base_freetype::create(m_ttf); rtext = text; text->font = font; _sep->add(text); } rtext->strings = strings; rtext->hjust = rhjust;} //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// //if(confine) { float fh = height * hmargin_factor; float rw = fw*0.5f; // adjust width : float th = fh; {float mn_x,mn_y,mn_z; float mx_x,mx_y,mx_z; rtext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z); float bxw = mx_x-mn_x; // adjust box width : // fh -> bxw then to have rw : if(!bxw) { m_sep.clear(); parent::update_sg(); return; } th = rw*fh/bxw;} rtext->height = th; {float xx = -fw*0.5F; //left justified. ltsf->set_translate(xx,0,zz);} {float mn_x,mn_y,mn_z; float mx_x,mx_y,mx_z; rtext->get_bounds(th,mn_x,mn_y,mn_z,mx_x,mx_y,mx_z); rtext->hjust = right; float xx = fw*0.5f; float yy = -(mn_y+mx_y)*0.5F; rtsf->set_translate(xx,yy,zz);} parent::update_sg(); //} else { //} } protected: separator m_sep; const base_freetype& m_ttf; }; }} #endif