Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_text_valop 5 #define tools_sg_text_valop 6 7 #include "base_text" 8 #include "valop2sg" 9 #include "nodekit" 10 #include "base_freetype" 11 #include "mnmx" 12 #include "../nostream" 13 14 namespace tools { 15 namespace sg { 16 17 class text_valop : public base_text { 18 public: 19 TOOLS_NODE(text_valop,tools::sg::text_valop,base_text) 20 public: 21 sf_string encoding; 22 sf_string font; 23 sf_enum<sg::font_modeling> font_modeling; 24 public: 25 virtual const desc_fields& node_desc_fields() const { 26 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::text_valop) 27 static const desc_fields s_v(parent::node_desc_fields(),3, //WARNING : take care of count. 28 TOOLS_ARG_FIELD_DESC(encoding), 29 TOOLS_ARG_FIELD_DESC(font), 30 TOOLS_ARG_FIELD_DESC_ENUMS_BEG(font_modeling,3) 31 TOOLS_ARG_ENUM(font_outline), 32 TOOLS_ARG_ENUM(font_filled), 33 TOOLS_ARG_ENUM(font_pixmap) 34 TOOLS_ARG_FIELD_DESC_ENUMS_END 35 ); 36 return s_v; 37 } 38 private: 39 void add_fields(){ 40 add_field(&encoding); 41 add_field(&font); 42 add_field(&font_modeling); 43 } 44 public: 45 virtual void render(render_action& a_action) { 46 if(touched()) { 47 update_sg(a_action.out()); 48 reset_touched(); 49 } 50 m_sep.render(a_action); 51 } 52 virtual void pick(pick_action& a_action) { 53 if(touched()) { 54 update_sg(a_action.out()); 55 reset_touched(); 56 } 57 nodekit_pick(a_action,m_sep,this); 58 } 59 virtual void bbox(bbox_action& a_action) { 60 if(touched()) { 61 update_sg(a_action.out()); 62 reset_touched(); 63 } 64 m_sep.bbox(a_action); 65 } 66 virtual void search(search_action& a_action) { 67 if(touched()) { 68 update_sg(a_action.out()); 69 reset_touched(); 70 } 71 parent::search(a_action); 72 if(a_action.done()) return; 73 if(a_action.do_path()) a_action.path_push(this); 74 m_sep.search(a_action); 75 if(a_action.done()) return; 76 if(a_action.do_path()) a_action.path_pop(); 77 } 78 public: 79 text_valop(const base_freetype& a_ttf) 80 :parent() 81 ,encoding(encoding_none()) 82 ,font(font_hershey()) 83 ,font_modeling(font_filled) 84 ,m_ttf(a_ttf) 85 { 86 add_fields(); 87 } 88 virtual ~text_valop(){} 89 public: 90 text_valop(const text_valop& a_from) 91 :parent(a_from) 92 ,encoding(a_from.encoding) 93 ,font(a_from.font) 94 ,font_modeling(a_from.font_modeling) 95 ,m_ttf(a_from.m_ttf) 96 { 97 add_fields(); 98 } 99 text_valop& operator=(const text_valop& a_from){ 100 parent::operator=(a_from); 101 encoding = a_from.encoding; 102 font = a_from.font; 103 font_modeling = a_from.font_modeling; 104 return *this; 105 } 106 public: //sg::base_text : 107 virtual void get_bounds(float /*a_height*/, 108 float& a_mn_x,float& a_mn_y,float& a_mn_z, 109 float& a_mx_x,float& a_mx_y,float& a_mx_z) const { 110 text_valop& self = const_cast<text_valop&>(*this); 111 if(self.touched()) { 112 self.update_sg(self.m_out); 113 self.reset_touched(); 114 } 115 vec3f mn,mx; 116 mnmx(self.m_out,self.m_sep,mn,mx); 117 a_mn_x = mn[0];a_mn_y = mn[1];a_mn_z = mn[2]; 118 a_mx_x = mx[0];a_mx_y = mx[1];a_mx_z = mx[2]; 119 //::printf("debug : get_bounds : %g %g %g : %g %g %g\n",a_mn_x,a_mn_y,a_mn_z,a_mx_x,a_mx_y,a_mx_z); 120 } 121 virtual float ascent(float) const {return 1;} 122 virtual float y_advance(float) const {return 1;} 123 virtual float descent(float) const {return 0;} 124 virtual bool truncate(const std::string&,float,float,std::string&) const {return false;} 125 protected: 126 void update_sg(std::ostream& a_out) { 127 m_sep.clear(); 128 matrix* tsf = new matrix; 129 m_sep.add(tsf); 130 tools_vforcit(std::string,strings.values(),it) { 131 valop* _valop = new valop(valop::STRING,*it); 132 valop2sg v(a_out,m_sep,m_ttf); 133 if(!v.visit(*_valop)) { 134 a_out << "tools::sg::text_valop::upate_sg : valop2sg.visit() failed." << std::endl; 135 m_sep.clear(); 136 delete _valop; 137 return; 138 } 139 delete _valop; 140 } 141 vec3f mn,mx; 142 mnmx(a_out,m_sep,mn,mx); 143 float h = mx[1]-mn[1]; 144 if(!h) { 145 a_out << "tools::sg::text_valop::upate_sg : valop has null height." << std::endl; 146 m_sep.clear(); 147 return; 148 } 149 float scale = height.value()/h; 150 tsf->set_scale(scale,scale,1); 151 } 152 protected: 153 const base_freetype& m_ttf; 154 separator m_sep; 155 nostream m_out; 156 }; 157 158 }} 159 160 #endif