| 1 | |
|---|
| 2 | |
|---|
| 3 | #include <sstream> |
|---|
| 4 | #include <osg/io_utils> |
|---|
| 5 | #include <osgWidget/StyleManager> |
|---|
| 6 | |
|---|
| 7 | namespace osgWidget { |
|---|
| 8 | |
|---|
| 9 | Style::Style(const std::string& name, const std::string& style): |
|---|
| 10 | _style(style) { |
|---|
| 11 | setName(name); |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | Style::Style(const Style& style, const osg::CopyOp& co): |
|---|
| 15 | osg::Object (style, co), |
|---|
| 16 | _style (style._style) { |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | bool Style::applyStyle(Widget* widget, Reader r) { |
|---|
| 20 | std::string str; |
|---|
| 21 | osg::Vec2 vec2; |
|---|
| 22 | osg::Vec3 vec3; |
|---|
| 23 | osg::Vec4 vec4; |
|---|
| 24 | float f; |
|---|
| 25 | |
|---|
| 26 | if(_match("pos %i %i", r) || _match("pos %f %f", r)) { |
|---|
| 27 | r.readSequence(vec2); |
|---|
| 28 | |
|---|
| 29 | widget->setOrigin(vec2); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | else if(_match("pos-x %i", r) || _match("pos-x %f", r)) { |
|---|
| 33 | r.readSequence(f); |
|---|
| 34 | |
|---|
| 35 | widget->setX(f); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | else if(_match("pos-y %i", r) || _match("pos-y %f", r)) { |
|---|
| 39 | r.readSequence(f); |
|---|
| 40 | |
|---|
| 41 | widget->setY(f); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | else if(_match("size %i %i", r) || _match("size %f %f", r)) { |
|---|
| 45 | r.readSequence(vec2); |
|---|
| 46 | |
|---|
| 47 | widget->setSize(vec2); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | else if(_match("width %i", r) || _match("width %f", r)) { |
|---|
| 51 | r.readSequence(f); |
|---|
| 52 | |
|---|
| 53 | widget->setWidth(f); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | else if(_match("height %i", r) || _match("height %f", r)) { |
|---|
| 57 | r.readSequence(f); |
|---|
| 58 | |
|---|
| 59 | widget->setHeight(f); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | else if(_match("color %i %i %i %i", r)) { |
|---|
| 64 | r.readSequence(vec4); |
|---|
| 65 | |
|---|
| 66 | widget->setColor(vec4 / 255.0f); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | else if(_match("color %i %i %i", r)) { |
|---|
| 71 | r.readSequence(vec3); |
|---|
| 72 | |
|---|
| 73 | widget->setColor(osg::Vec4(vec3[0], vec3[1], vec3[2], 255.0f) / 255.0f); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | else if(_match("color %f %f %f %f", r)) { |
|---|
| 78 | r.readSequence(vec4); |
|---|
| 79 | |
|---|
| 80 | widget->setColor(vec4); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | else if(_match("color %f %f %f", r)) { |
|---|
| 85 | r.readSequence(vec3); |
|---|
| 86 | |
|---|
| 87 | widget->setColor(osg::Vec4(vec3[0], vec3[1], vec3[2], 1.0f)); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | else if(_match("padding %i", r)) { |
|---|
| 92 | r.readSequence(f); |
|---|
| 93 | |
|---|
| 94 | widget->setPadding(f); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | else if(_match("padding-left %i", r)) { |
|---|
| 99 | r.readSequence(f); |
|---|
| 100 | |
|---|
| 101 | widget->setPadLeft(f); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | else if(_match("padding-right %i", r)) { |
|---|
| 106 | r.readSequence(f); |
|---|
| 107 | |
|---|
| 108 | widget->setPadRight(f); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | else if(_match("padding-top %i", r)) { |
|---|
| 113 | r.readSequence(f); |
|---|
| 114 | |
|---|
| 115 | widget->setPadTop(f); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | else if(_match("padding-bottom %i", r)) { |
|---|
| 120 | r.readSequence(f); |
|---|
| 121 | |
|---|
| 122 | widget->setPadBottom(f); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | else if(_match("layer %w", r)) { |
|---|
| 126 | r.readSequence(str); |
|---|
| 127 | |
|---|
| 128 | widget->setLayer(strToLayer(str)); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | else if(_match("valign %w", r)) { |
|---|
| 132 | r.readSequence(str); |
|---|
| 133 | |
|---|
| 134 | widget->setAlignVertical(strToVAlign(str)); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | else if(_match("halign %w", r)) { |
|---|
| 138 | r.readSequence(str); |
|---|
| 139 | |
|---|
| 140 | widget->setAlignHorizontal(strToHAlign(str)); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | else if(_match("coordmode %w", r)) { |
|---|
| 144 | r.readSequence(str); |
|---|
| 145 | |
|---|
| 146 | widget->setCoordinateMode(strToCoordMode(str)); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | else if(_match("fill %w", r)) { |
|---|
| 150 | r.readSequence(str); |
|---|
| 151 | |
|---|
| 152 | widget->setCanFill(strToFill(str)); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | else if(_match("image %s", r)) { |
|---|
| 156 | r.readSequence(str); |
|---|
| 157 | |
|---|
| 158 | widget->setImage(str, true); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | else return false; |
|---|
| 163 | |
|---|
| 164 | return true; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | bool Style::applyStyle(Label* label, Reader r) { |
|---|
| 168 | return false; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | bool Style::applyStyle(Input* input, Reader r) { |
|---|
| 172 | return false; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | bool Style::applyStyle(Window* window, Reader r) { |
|---|
| 176 | osg::Vec2 vec2; |
|---|
| 177 | float f; |
|---|
| 178 | |
|---|
| 179 | if(_match("pos %i %i", r) || _match("pos %f %f", r)) { |
|---|
| 180 | r.readSequence(vec2); |
|---|
| 181 | |
|---|
| 182 | window->setOrigin(vec2.x(), vec2.y()); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | else if(_match("pos-x %i", r) || _match("pos-x %f", r)) { |
|---|
| 186 | r.readSequence(f); |
|---|
| 187 | |
|---|
| 188 | window->setX(f); |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | else if(_match("pos-y %i", r) || _match("pos-y %f", r)) { |
|---|
| 192 | r.readSequence(f); |
|---|
| 193 | |
|---|
| 194 | window->setY(f); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | else if(_match("size %i %i", r) || _match("size %f %f", r)) { |
|---|
| 198 | r.readSequence(vec2); |
|---|
| 199 | |
|---|
| 200 | window->resize(vec2.x(), vec2.y()); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | else if(_match("width %i", r) || _match("width %f", r)) { |
|---|
| 204 | r.readSequence(f); |
|---|
| 205 | |
|---|
| 206 | window->resize(f); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | else if(_match("height %i", r) || _match("height %f", r)) { |
|---|
| 210 | r.readSequence(f); |
|---|
| 211 | |
|---|
| 212 | window->resize(0.0f, f); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | else return false; |
|---|
| 216 | |
|---|
| 217 | return true; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | bool Style::applyStyle(Canvas* label, Reader r) { |
|---|
| 221 | return false; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | bool Style::applyStyle(Window::EmbeddedWindow*, Reader r) { |
|---|
| 226 | return false; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | bool Style::applyStyle(Box* box, Reader r) { |
|---|
| 230 | if(applyStyle(static_cast<Window*>(box), r)) return true; |
|---|
| 231 | |
|---|
| 232 | return false; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | bool Style::applyStyle(Frame::Corner*, Reader r) { |
|---|
| 236 | return false; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | bool Style::applyStyle(Frame::Border*, Reader r) { |
|---|
| 240 | return false; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | Widget::Layer Style::strToLayer(const std::string& layer) |
|---|
| 244 | { |
|---|
| 245 | std::string l = lowerCase(layer); |
|---|
| 246 | |
|---|
| 247 | if(l == "top") return Widget::LAYER_TOP; |
|---|
| 248 | |
|---|
| 249 | else if(l == "high") return Widget::LAYER_HIGH; |
|---|
| 250 | |
|---|
| 251 | else if(l == "middle") return Widget::LAYER_MIDDLE; |
|---|
| 252 | |
|---|
| 253 | else if(l == "low") return Widget::LAYER_LOW; |
|---|
| 254 | |
|---|
| 255 | else if(l == "bg") return Widget::LAYER_BG; |
|---|
| 256 | |
|---|
| 257 | else { |
|---|
| 258 | warn() << "Unkown Layer name [" << layer << "]; using LAYER_MIDDLE." << std::endl; |
|---|
| 259 | |
|---|
| 260 | return Widget::LAYER_MIDDLE; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | Widget::VerticalAlignment Style::strToVAlign(const std::string& valign) { |
|---|
| 265 | std::string va = lowerCase(valign); |
|---|
| 266 | |
|---|
| 267 | if(va == "center") return Widget::VA_CENTER; |
|---|
| 268 | |
|---|
| 269 | else if(va == "top") return Widget::VA_TOP; |
|---|
| 270 | |
|---|
| 271 | else if(va == "bottom") return Widget::VA_BOTTOM; |
|---|
| 272 | |
|---|
| 273 | else { |
|---|
| 274 | warn() << "Unkown VAlign name [" << valign << "]; using VA_CENTER." << std::endl; |
|---|
| 275 | |
|---|
| 276 | return Widget::VA_CENTER; |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | Widget::HorizontalAlignment Style::strToHAlign(const std::string& halign) { |
|---|
| 281 | std::string ha = lowerCase(halign); |
|---|
| 282 | |
|---|
| 283 | if(ha == "center") return Widget::HA_CENTER; |
|---|
| 284 | |
|---|
| 285 | else if(ha == "left") return Widget::HA_LEFT; |
|---|
| 286 | |
|---|
| 287 | else if(ha == "right") return Widget::HA_RIGHT; |
|---|
| 288 | |
|---|
| 289 | else { |
|---|
| 290 | warn() << "Unkown HAlign name [" << halign << "]; using HA_CENTER." << std::endl; |
|---|
| 291 | |
|---|
| 292 | return Widget::HA_CENTER; |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | Widget::CoordinateMode Style::strToCoordMode(const std::string& coordmode) { |
|---|
| 297 | std::string cm = lowerCase(coordmode); |
|---|
| 298 | |
|---|
| 299 | if(cm == "absolute") return Widget::CM_ABSOLUTE; |
|---|
| 300 | |
|---|
| 301 | else if(cm == "relative") return Widget::CM_RELATIVE; |
|---|
| 302 | |
|---|
| 303 | else { |
|---|
| 304 | warn() |
|---|
| 305 | << "Unkown CoordMode name [" << coordmode |
|---|
| 306 | << "]; using CM_ABSOLUTE." << std::endl |
|---|
| 307 | ; |
|---|
| 308 | |
|---|
| 309 | return Widget::CM_ABSOLUTE; |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | bool Style::strToFill(const std::string& fill) { |
|---|
| 314 | std::string cm = lowerCase(fill); |
|---|
| 315 | |
|---|
| 316 | if(cm == "true") return true; |
|---|
| 317 | |
|---|
| 318 | else if(cm == "false") return false; |
|---|
| 319 | |
|---|
| 320 | else { |
|---|
| 321 | warn() |
|---|
| 322 | << "Unkown Fill name [" << fill |
|---|
| 323 | << "]; using false." << std::endl |
|---|
| 324 | ; |
|---|
| 325 | |
|---|
| 326 | return false; |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | StyleManager::StyleManager() { |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | StyleManager::StyleManager(const StyleManager& manager, const osg::CopyOp& co): |
|---|
| 334 | osg::Object(manager, co) { |
|---|
| 335 | for(ConstIterator i = _styles.begin(); i != _styles.end(); i++) if(i->second.valid()) { |
|---|
| 336 | _styles[i->first] = new Style(*i->second.get(), osg::CopyOp::DEEP_COPY_ALL); |
|---|
| 337 | } |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | bool StyleManager::_applyStyleToObject(osg::Object* obj, const std::string& style) { |
|---|
| 341 | std::string c = obj->className(); |
|---|
| 342 | |
|---|
| 343 | if(!std::string("Widget").compare(c)) return _coerceAndApply<Widget>( |
|---|
| 344 | obj, |
|---|
| 345 | style, |
|---|
| 346 | c |
|---|
| 347 | ); |
|---|
| 348 | |
|---|
| 349 | else if(!std::string("Label").compare(c)) return _coerceAndApply<Label>( |
|---|
| 350 | obj, |
|---|
| 351 | style, |
|---|
| 352 | c |
|---|
| 353 | ); |
|---|
| 354 | |
|---|
| 355 | else if(!std::string("Box").compare(c)) return _coerceAndApply<Box>( |
|---|
| 356 | obj, |
|---|
| 357 | style, |
|---|
| 358 | c |
|---|
| 359 | ); |
|---|
| 360 | else if(!std::string("Canvas").compare(c)) return _coerceAndApply<Canvas>( |
|---|
| 361 | obj, |
|---|
| 362 | style, |
|---|
| 363 | c |
|---|
| 364 | ); |
|---|
| 365 | else if(!std::string("Window").compare(c)) return _coerceAndApply<Window>( |
|---|
| 366 | obj, |
|---|
| 367 | style, |
|---|
| 368 | c |
|---|
| 369 | ); |
|---|
| 370 | else if(!std::string("Input").compare(c)) return _coerceAndApply<Input>( |
|---|
| 371 | obj, |
|---|
| 372 | style, |
|---|
| 373 | c |
|---|
| 374 | ); |
|---|
| 375 | else if(!std::string("Corner").compare(c)) return _coerceAndApply<Frame::Corner>( |
|---|
| 376 | obj, |
|---|
| 377 | style, |
|---|
| 378 | c |
|---|
| 379 | ); |
|---|
| 380 | else if(!std::string("Border").compare(c)) return _coerceAndApply<Frame::Border>( |
|---|
| 381 | obj, |
|---|
| 382 | style, |
|---|
| 383 | c |
|---|
| 384 | ); |
|---|
| 385 | else if(!std::string("EmbeddedWindow").compare(c)) return _coerceAndApply<Window::EmbeddedWindow>( |
|---|
| 386 | obj, |
|---|
| 387 | style, |
|---|
| 388 | c |
|---|
| 389 | ); |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | else warn() |
|---|
| 393 | << "StyleManager does not support coercion of objects of type " |
|---|
| 394 | << c << "." << std::endl |
|---|
| 395 | ; |
|---|
| 396 | |
|---|
| 397 | return false; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | bool StyleManager::addStyle(Style* style) { |
|---|
| 401 | if(!style || style->getName().empty()) { |
|---|
| 402 | warn() << "Cannot add a NULL or nameless Style object." << std::endl; |
|---|
| 403 | |
|---|
| 404 | return false; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | _styles[style->getName()] = style; |
|---|
| 408 | |
|---|
| 409 | return true; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | } |
|---|