| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #include <osgWidget/Util> |
|---|
| 5 | #include <osgWidget/WindowManager> |
|---|
| 6 | #include <osgWidget/Frame> |
|---|
| 7 | #include <osgWidget/Box> |
|---|
| 8 | #include <osgWidget/Widget> |
|---|
| 9 | #include <osgWidget/Types> |
|---|
| 10 | #include <osgDB/ReadFile> |
|---|
| 11 | #include <osgAnimation/EaseMotion> |
|---|
| 12 | #include <osg/io_utils> |
|---|
| 13 | #include <iostream> |
|---|
| 14 | |
|---|
| 15 | const unsigned int MASK_2D = 0xF0000000; |
|---|
| 16 | |
|---|
| 17 | class MessageBox |
|---|
| 18 | { |
|---|
| 19 | protected: |
|---|
| 20 | |
|---|
| 21 | osgWidget::Frame* createButtonOk(const std::string& theme, const std::string& text, const std::string& font, int fontSize); |
|---|
| 22 | osgWidget::Label* createLabel(const std::string& string, const std::string& font, int size, const osgWidget::Color& color); |
|---|
| 23 | |
|---|
| 24 | osg::ref_ptr<osgWidget::Frame> _window; |
|---|
| 25 | osg::ref_ptr<osgWidget::Frame> _button; |
|---|
| 26 | |
|---|
| 27 | public: |
|---|
| 28 | |
|---|
| 29 | osgWidget::Frame* getButton(); |
|---|
| 30 | osgWidget::Frame* getWindow(); |
|---|
| 31 | |
|---|
| 32 | bool create(const std::string& themeMessage, |
|---|
| 33 | const std::string& themeButton, |
|---|
| 34 | const std::string& titleText, |
|---|
| 35 | const std::string& messageText, |
|---|
| 36 | const std::string& buttonText, |
|---|
| 37 | const std::string& font, |
|---|
| 38 | int fontSize); |
|---|
| 39 | |
|---|
| 40 | }; |
|---|
| 41 | osgWidget::Frame* MessageBox::getButton() { return _button.get(); } |
|---|
| 42 | osgWidget::Frame* MessageBox::getWindow() { return _window.get(); } |
|---|
| 43 | |
|---|
| 44 | struct AlphaSetterVisitor : public osg::NodeVisitor |
|---|
| 45 | { |
|---|
| 46 | float _alpha; |
|---|
| 47 | AlphaSetterVisitor( float alpha = 1.0):osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) { _alpha = alpha;} |
|---|
| 48 | |
|---|
| 49 | void apply(osg::MatrixTransform& node) |
|---|
| 50 | { |
|---|
| 51 | osgWidget::Window* win = dynamic_cast<osgWidget::Window*>(&node); |
|---|
| 52 | |
|---|
| 53 | if (win) { |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | for (osgWidget::Window::Iterator it = win->begin(); it != win->end(); it++) |
|---|
| 57 | { |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | osgWidget::Color color = it->get()->getColor(); |
|---|
| 61 | color[3] = color[3] *_alpha; |
|---|
| 62 | it->get()->setColor(color); |
|---|
| 63 | } |
|---|
| 64 | { |
|---|
| 65 | osgWidget::Color color = win->getBackground()->getColor(); |
|---|
| 66 | color[3] = color[3] *_alpha; |
|---|
| 67 | win->getBackground()->setColor(color); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | traverse(node); |
|---|
| 71 | } |
|---|
| 72 | }; |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | struct ColorSetterVisitor : public osg::NodeVisitor |
|---|
| 76 | { |
|---|
| 77 | osgWidget::Color _color; |
|---|
| 78 | ColorSetterVisitor( const osgWidget::Color& color):osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) { _color = color;} |
|---|
| 79 | |
|---|
| 80 | void apply(osg::MatrixTransform& node) |
|---|
| 81 | { |
|---|
| 82 | osgWidget::Window* win = dynamic_cast<osgWidget::Window*>(&node); |
|---|
| 83 | |
|---|
| 84 | if (win) { |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | for (osgWidget::Window::Iterator it = win->begin(); it != win->end(); it++) |
|---|
| 88 | { |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | it->get()->setColor(_color); |
|---|
| 94 | } |
|---|
| 95 | { |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | win->getBackground()->setColor(osgWidget::Color(0,0,0,0)); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | traverse(node); |
|---|
| 102 | } |
|---|
| 103 | }; |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | struct EventOK : public osgWidget::Callback, osg::NodeCallback |
|---|
| 108 | { |
|---|
| 109 | typedef osgAnimation::OutCubicMotion WidgetMotion; |
|---|
| 110 | |
|---|
| 111 | WidgetMotion _motionOver; |
|---|
| 112 | WidgetMotion _motionLeave; |
|---|
| 113 | |
|---|
| 114 | double _lastUpdate; |
|---|
| 115 | osgWidget::Color _defaultColor; |
|---|
| 116 | osgWidget::Color _overColor; |
|---|
| 117 | bool _over; |
|---|
| 118 | osg::ref_ptr<osgWidget::Frame> _frame; |
|---|
| 119 | float _width; |
|---|
| 120 | float _height; |
|---|
| 121 | EventOK(osgWidget::Frame* frame) : osgWidget::Callback(osgWidget::EVENT_ALL), _frame(frame) |
|---|
| 122 | { |
|---|
| 123 | _motionOver = WidgetMotion(0.0, 0.4); |
|---|
| 124 | _motionLeave = WidgetMotion(0.0, 0.5); |
|---|
| 125 | _defaultColor = _frame->getEmbeddedWindow()->getColor(); |
|---|
| 126 | _overColor = osgWidget::Color(229.0/255.0, |
|---|
| 127 | 103.0/255.0, |
|---|
| 128 | 17.0/255, |
|---|
| 129 | _defaultColor[3]); |
|---|
| 130 | _over = false; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | bool operator()(osgWidget::Event& ev) |
|---|
| 134 | { |
|---|
| 135 | if (ev.type == osgWidget::EVENT_MOUSE_ENTER) |
|---|
| 136 | { |
|---|
| 137 | _over = true; |
|---|
| 138 | _width = _frame->getWidth(); |
|---|
| 139 | _height = _frame->getHeight(); |
|---|
| 140 | _motionOver.reset(); |
|---|
| 141 | std::cout << "enter" << std::endl; |
|---|
| 142 | return true; |
|---|
| 143 | } |
|---|
| 144 | else if (ev.type == osgWidget::EVENT_MOUSE_LEAVE) |
|---|
| 145 | { |
|---|
| 146 | _over = false; |
|---|
| 147 | _motionLeave.reset(); |
|---|
| 148 | std::cout << "leave" << std::endl; |
|---|
| 149 | return true; |
|---|
| 150 | } |
|---|
| 151 | return false; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 155 | { |
|---|
| 156 | if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| 157 | { |
|---|
| 158 | const osg::FrameStamp* fs = nv->getFrameStamp(); |
|---|
| 159 | double dt = fs->getSimulationTime() - _lastUpdate; |
|---|
| 160 | _lastUpdate = fs->getSimulationTime(); |
|---|
| 161 | |
|---|
| 162 | if (_frame.valid()) |
|---|
| 163 | { |
|---|
| 164 | float value; |
|---|
| 165 | if (_over) |
|---|
| 166 | { |
|---|
| 167 | _motionOver.update(dt); |
|---|
| 168 | value = _motionOver.getValue(); |
|---|
| 169 | } |
|---|
| 170 | else |
|---|
| 171 | { |
|---|
| 172 | _motionLeave.update(dt); |
|---|
| 173 | value = 1.0 - _motionLeave.getValue(); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | osgWidget::Color c = _defaultColor + ((_overColor - _defaultColor) * value); |
|---|
| 177 | ColorSetterVisitor colorSetter(c); |
|---|
| 178 | _frame->accept(colorSetter); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | node->traverse(*nv); |
|---|
| 182 | } |
|---|
| 183 | }; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | osgWidget::Label* MessageBox::createLabel(const std::string& string, const std::string& font, int size, const osgWidget::Color& color) |
|---|
| 188 | { |
|---|
| 189 | osgWidget::Label* label = new osgWidget::Label("", ""); |
|---|
| 190 | label->setFont(font); |
|---|
| 191 | label->setFontSize(size); |
|---|
| 192 | label->setFontColor(color); |
|---|
| 193 | label->setColor(osgWidget::Color(0,0,0,0)); |
|---|
| 194 | label->setLabel(string); |
|---|
| 195 | label->setCanFill(true); |
|---|
| 196 | return label; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | osgWidget::Frame* MessageBox::createButtonOk(const std::string& theme, |
|---|
| 200 | const std::string& text, |
|---|
| 201 | const std::string& font, |
|---|
| 202 | int fontSize) |
|---|
| 203 | { |
|---|
| 204 | osg::ref_ptr<osgWidget::Frame> frame = osgWidget::Frame::createSimpleFrameFromTheme( |
|---|
| 205 | "ButtonOK", |
|---|
| 206 | osgDB::readImageFile(theme), |
|---|
| 207 | 300.0f, |
|---|
| 208 | 50.0f, |
|---|
| 209 | osgWidget::Frame::FRAME_TEXTURE |
|---|
| 210 | ); |
|---|
| 211 | frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 212 | |
|---|
| 213 | osgWidget::Label* label = createLabel(text, font, fontSize, osgWidget::Color(0,0,0,1)); |
|---|
| 214 | |
|---|
| 215 | osgWidget::Box* box = new osgWidget::Box("HBOX", osgWidget::Box::HORIZONTAL); |
|---|
| 216 | box->addWidget(label); |
|---|
| 217 | box->resize(); |
|---|
| 218 | osgWidget::Color colorBack = frame->getEmbeddedWindow()->getColor(); |
|---|
| 219 | box->getBackground()->setColor(colorBack); |
|---|
| 220 | frame->getEmbeddedWindow()->setWindow(box); |
|---|
| 221 | box->setEventMask(osgWidget::EVENT_NONE); |
|---|
| 222 | |
|---|
| 223 | frame->resizeFrame(box->getWidth(), box->getHeight()); |
|---|
| 224 | frame->resizeAdd(0, 0); |
|---|
| 225 | |
|---|
| 226 | EventOK* event = new EventOK(frame.get()); |
|---|
| 227 | frame->setUpdateCallback(event); |
|---|
| 228 | frame->addCallback(event); |
|---|
| 229 | |
|---|
| 230 | return frame.release(); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | bool MessageBox::create(const std::string& themeMessage, |
|---|
| 234 | const std::string& themeButton, |
|---|
| 235 | const std::string& titleText, |
|---|
| 236 | const std::string& messageText, |
|---|
| 237 | const std::string& buttonText, |
|---|
| 238 | const std::string& font, |
|---|
| 239 | int fontSize) |
|---|
| 240 | { |
|---|
| 241 | |
|---|
| 242 | osg::ref_ptr<osgWidget::Frame> frame = osgWidget::Frame::createSimpleFrameFromTheme( |
|---|
| 243 | "error", |
|---|
| 244 | osgDB::readImageFile(themeMessage), |
|---|
| 245 | 300.0f, |
|---|
| 246 | 50.0f, |
|---|
| 247 | osgWidget::Frame::FRAME_ALL |
|---|
| 248 | ); |
|---|
| 249 | frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 250 | |
|---|
| 251 | osgWidget::Label* labelText = createLabel(messageText, font, fontSize, osgWidget::Color(0,0,0,1)); |
|---|
| 252 | osgWidget::Label* labelTitle = createLabel(titleText, font, fontSize+5, osgWidget::Color(0.4,0,0,1)); |
|---|
| 253 | |
|---|
| 254 | osgWidget::Box* box = new osgWidget::Box("VBOX", osgWidget::Box::VERTICAL); |
|---|
| 255 | |
|---|
| 256 | _button = createButtonOk(themeButton, buttonText, font, fontSize); |
|---|
| 257 | osgWidget::Widget* buttonOK = _button->embed(); |
|---|
| 258 | buttonOK->setColor(osgWidget::Color(0,0,0,0)); |
|---|
| 259 | buttonOK->setCanFill(false); |
|---|
| 260 | |
|---|
| 261 | box->addWidget(buttonOK); |
|---|
| 262 | box->addWidget(labelText); |
|---|
| 263 | box->addWidget(labelTitle); |
|---|
| 264 | |
|---|
| 265 | osgWidget::Color colorBack = frame->getEmbeddedWindow()->getColor(); |
|---|
| 266 | box->getBackground()->setColor(colorBack); |
|---|
| 267 | |
|---|
| 268 | frame->setWindow(box); |
|---|
| 269 | |
|---|
| 270 | box->resize(); |
|---|
| 271 | frame->resizeFrame(box->getWidth(), box->getHeight()); |
|---|
| 272 | _window = frame; |
|---|
| 273 | return true; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | const char* LABEL1 = |
|---|
| 283 | "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\n" |
|---|
| 284 | "do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" |
|---|
| 285 | "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\n" |
|---|
| 286 | "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in..." |
|---|
| 287 | ; |
|---|
| 288 | |
|---|
| 289 | int main(int argc, char** argv) |
|---|
| 290 | { |
|---|
| 291 | |
|---|
| 292 | osgViewer::Viewer viewer; |
|---|
| 293 | |
|---|
| 294 | osgWidget::WindowManager* wm = new osgWidget::WindowManager( |
|---|
| 295 | &viewer, |
|---|
| 296 | 1280.0f, |
|---|
| 297 | 1024.0f, |
|---|
| 298 | MASK_2D, |
|---|
| 299 | osgWidget::WindowManager::WM_PICK_DEBUG |
|---|
| 300 | ); |
|---|
| 301 | |
|---|
| 302 | int fontSize = 20; |
|---|
| 303 | std::string font="fonts/arial.ttf"; |
|---|
| 304 | std::string buttonTheme = "osgWidget/theme-8-shadow.png"; |
|---|
| 305 | std::string borderTheme = "osgWidget/theme-8.png"; |
|---|
| 306 | |
|---|
| 307 | MessageBox message; |
|---|
| 308 | message.create(borderTheme, |
|---|
| 309 | buttonTheme, |
|---|
| 310 | "Error - Critical", |
|---|
| 311 | LABEL1, |
|---|
| 312 | "Quit", |
|---|
| 313 | font, |
|---|
| 314 | fontSize); |
|---|
| 315 | |
|---|
| 316 | AlphaSetterVisitor alpha(.8f); |
|---|
| 317 | message.getWindow()->accept(alpha); |
|---|
| 318 | |
|---|
| 319 | wm->addChild(message.getWindow()); |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | osgWidget::point_type w = wm->getWidth(); |
|---|
| 323 | osgWidget::point_type h = wm->getHeight(); |
|---|
| 324 | osgWidget::point_type ww = message.getWindow()->getWidth(); |
|---|
| 325 | osgWidget::point_type hw = message.getWindow()->getHeight(); |
|---|
| 326 | osgWidget::point_type ox = (w - ww) / 2; |
|---|
| 327 | osgWidget::point_type oy = (h - hw) / 2; |
|---|
| 328 | message.getWindow()->setPosition(osgWidget::Point(ox, oy, message.getWindow()->getPosition()[2] )); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | return osgWidget::createExample(viewer, wm, osgDB::readNodeFile("cow.osg")); |
|---|
| 334 | |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | #if 0 |
|---|
| 394 | struct AlphaSetterVisitor : public osg::NodeVisitor |
|---|
| 395 | { |
|---|
| 396 | float _alpha; |
|---|
| 397 | AlphaSetterVisitor( float alpha = 1.0):osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) { _alpha = alpha;} |
|---|
| 398 | |
|---|
| 399 | void apply(osg::MatrixTransform& node) |
|---|
| 400 | { |
|---|
| 401 | osgWidget::Window* win = dynamic_cast<osgWidget::Window*>(&node); |
|---|
| 402 | |
|---|
| 403 | if (win) { |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | for (osgWidget::Window::Iterator it = win->begin(); it != win->end(); it++) |
|---|
| 407 | { |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | osgWidget::Color color = it->get()->getColor(); |
|---|
| 411 | color[3] = color[3] *_alpha; |
|---|
| 412 | it->get()->setColor(color); |
|---|
| 413 | } |
|---|
| 414 | { |
|---|
| 415 | osgWidget::Color color = win->getBackground()->getColor(); |
|---|
| 416 | color[3] = color[3] *_alpha; |
|---|
| 417 | win->getBackground()->setColor(color); |
|---|
| 418 | } |
|---|
| 419 | } |
|---|
| 420 | traverse(node); |
|---|
| 421 | } |
|---|
| 422 | }; |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | struct ColorSetterVisitor : public osg::NodeVisitor |
|---|
| 426 | { |
|---|
| 427 | osgWidget::Color _color; |
|---|
| 428 | ColorSetterVisitor( const osgWidget::Color& color):osg::NodeVisitor(TRAVERSE_ALL_CHILDREN) { _color = color;} |
|---|
| 429 | |
|---|
| 430 | void apply(osg::MatrixTransform& node) |
|---|
| 431 | { |
|---|
| 432 | osgWidget::Window* win = dynamic_cast<osgWidget::Window*>(&node); |
|---|
| 433 | |
|---|
| 434 | if (win) { |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | for (osgWidget::Window::Iterator it = win->begin(); it != win->end(); it++) |
|---|
| 438 | { |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | it->get()->setColor(_color); |
|---|
| 444 | } |
|---|
| 445 | { |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | win->getBackground()->setColor(osgWidget::Color(0,0,0,0)); |
|---|
| 449 | } |
|---|
| 450 | } |
|---|
| 451 | traverse(node); |
|---|
| 452 | } |
|---|
| 453 | }; |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | struct EventOK : public osgWidget::Callback, osg::NodeCallback |
|---|
| 457 | { |
|---|
| 458 | typedef osgAnimation::OutQuartMotion WidgetMotion; |
|---|
| 459 | WidgetMotion _motionOver; |
|---|
| 460 | WidgetMotion _motionLeave; |
|---|
| 461 | |
|---|
| 462 | double _lastUpdate; |
|---|
| 463 | osgWidget::Color _defaultColor; |
|---|
| 464 | osgWidget::Color _overColor; |
|---|
| 465 | bool _over; |
|---|
| 466 | osg::ref_ptr<osgWidget::Frame> _frame; |
|---|
| 467 | float _width; |
|---|
| 468 | float _height; |
|---|
| 469 | EventOK(osgWidget::Frame* frame) : osgWidget::Callback(osgWidget::EVENT_ALL), _frame(frame) |
|---|
| 470 | { |
|---|
| 471 | _motionOver = WidgetMotion(0.0, 0.4); |
|---|
| 472 | _motionLeave = WidgetMotion(0.0, 0.5); |
|---|
| 473 | _defaultColor = _frame->getEmbeddedWindow()->getColor(); |
|---|
| 474 | _overColor = osgWidget::Color(229.0/255.0, |
|---|
| 475 | 103.0/255.0, |
|---|
| 476 | 17.0/255, |
|---|
| 477 | _defaultColor[3]); |
|---|
| 478 | _over = false; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | bool operator()(osgWidget::Event& ev) |
|---|
| 482 | { |
|---|
| 483 | if (ev.type == osgWidget::EVENT_MOUSE_ENTER) |
|---|
| 484 | { |
|---|
| 485 | _over = true; |
|---|
| 486 | |
|---|
| 487 | _width = _frame->getWidth(); |
|---|
| 488 | _height = _frame->getHeight(); |
|---|
| 489 | _motionOver.reset(); |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | return true; |
|---|
| 493 | } |
|---|
| 494 | else if (ev.type == osgWidget::EVENT_MOUSE_LEAVE) |
|---|
| 495 | { |
|---|
| 496 | _over = false; |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | _motionLeave.reset(); |
|---|
| 500 | return true; |
|---|
| 501 | } |
|---|
| 502 | return false; |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 506 | { |
|---|
| 507 | if (nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR) |
|---|
| 508 | { |
|---|
| 509 | const osg::FrameStamp* fs = nv->getFrameStamp(); |
|---|
| 510 | double dt = fs->getSimulationTime() - _lastUpdate; |
|---|
| 511 | _lastUpdate = fs->getSimulationTime(); |
|---|
| 512 | |
|---|
| 513 | if (_frame.valid()) |
|---|
| 514 | { |
|---|
| 515 | float value; |
|---|
| 516 | if (_over) |
|---|
| 517 | { |
|---|
| 518 | _motionOver.update(dt); |
|---|
| 519 | value = _motionOver.getValue(); |
|---|
| 520 | } |
|---|
| 521 | else |
|---|
| 522 | { |
|---|
| 523 | _motionLeave.update(dt); |
|---|
| 524 | value = 1.0 - _motionLeave.getValue(); |
|---|
| 525 | } |
|---|
| 526 | |
|---|
| 527 | osgWidget::Color c = _defaultColor + ((_overColor - _defaultColor) * value); |
|---|
| 528 | ColorSetterVisitor colorSetter(c); |
|---|
| 529 | _frame->accept(colorSetter); |
|---|
| 530 | } |
|---|
| 531 | } |
|---|
| 532 | node->traverse(*nv); |
|---|
| 533 | } |
|---|
| 534 | }; |
|---|
| 535 | |
|---|
| 536 | |
|---|
| 537 | |
|---|
| 538 | osgWidget::Label* createLabel(const std::string& string, const std::string& font, int size, const osgWidget::Color& color) |
|---|
| 539 | { |
|---|
| 540 | osgWidget::Label* label = new osgWidget::Label("", ""); |
|---|
| 541 | label->setFont(font); |
|---|
| 542 | label->setFontSize(size); |
|---|
| 543 | label->setFontColor(color); |
|---|
| 544 | label->setColor(osgWidget::Color(0,0,0,0)); |
|---|
| 545 | label->setLabel(string); |
|---|
| 546 | label->setCanFill(true); |
|---|
| 547 | return label; |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | osgWidget::Window* createButtonOk(const std::string& theme, const std::string& text, int fontSize) |
|---|
| 551 | { |
|---|
| 552 | osg::ref_ptr<osgWidget::Frame> frame = osgWidget::Frame::createSimpleFrameFromTheme( |
|---|
| 553 | "ButtonOK", |
|---|
| 554 | osgDB::readImageFile(theme), |
|---|
| 555 | 300.0f, |
|---|
| 556 | 50.0f, |
|---|
| 557 | osgWidget::Frame::FRAME_TEXTURE |
|---|
| 558 | ); |
|---|
| 559 | frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 560 | |
|---|
| 561 | osgWidget::Label* label = createLabel(text, "fonts/Vera.ttf", fontSize, osgWidget::Color(0,0,0,1)); |
|---|
| 562 | |
|---|
| 563 | osgWidget::Box* box = new osgWidget::Box("HBOX", osgWidget::Box::HORIZONTAL); |
|---|
| 564 | box->addWidget(label); |
|---|
| 565 | box->resize(); |
|---|
| 566 | osgWidget::Color colorBack = frame->getEmbeddedWindow()->getColor(); |
|---|
| 567 | box->getBackground()->setColor(colorBack); |
|---|
| 568 | frame->getEmbeddedWindow()->setWindow(box); |
|---|
| 569 | box->setEventMask(osgWidget::EVENT_NONE); |
|---|
| 570 | |
|---|
| 571 | frame->resizeFrame(box->getWidth(), box->getHeight()); |
|---|
| 572 | frame->resizeAdd(0, 0); |
|---|
| 573 | |
|---|
| 574 | EventOK* event = new EventOK(frame); |
|---|
| 575 | frame->setUpdateCallback(event); |
|---|
| 576 | frame->addCallback(event); |
|---|
| 577 | |
|---|
| 578 | |
|---|
| 579 | return frame.release(); |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | osgWidget::Frame* createErrorMessage(const std::string& themeMessage, |
|---|
| 583 | const std::string& themeButton, |
|---|
| 584 | const std::string& titleText, |
|---|
| 585 | const std::string& messageText, |
|---|
| 586 | const std::string& buttonText, |
|---|
| 587 | const std::string& font, |
|---|
| 588 | int fontSize) |
|---|
| 589 | { |
|---|
| 590 | |
|---|
| 591 | osg::ref_ptr<osgWidget::Frame> frame = osgWidget::Frame::createSimpleFrameFromTheme( |
|---|
| 592 | "error", |
|---|
| 593 | osgDB::readImageFile(themeMessage), |
|---|
| 594 | 300.0f, |
|---|
| 595 | 50.0f, |
|---|
| 596 | osgWidget::Frame::FRAME_ALL |
|---|
| 597 | ); |
|---|
| 598 | frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 599 | |
|---|
| 600 | osgWidget::Label* labelText = createLabel(messageText, font, fontSize, osgWidget::Color(0,0,0,1)); |
|---|
| 601 | osgWidget::Label* labelTitle = createLabel(titleText, font, fontSize+5, osgWidget::Color(0.4,0,0,1)); |
|---|
| 602 | |
|---|
| 603 | osgWidget::Box* box = new osgWidget::Box("VBOX", osgWidget::Box::VERTICAL); |
|---|
| 604 | |
|---|
| 605 | osgWidget::Widget* buttonOK = createButtonOk(themeButton, buttonText, fontSize)->embed(); |
|---|
| 606 | buttonOK->setColor(osgWidget::Color(0,0,0,0)); |
|---|
| 607 | buttonOK->setCanFill(false); |
|---|
| 608 | |
|---|
| 609 | box->addWidget(buttonOK); |
|---|
| 610 | box->addWidget(labelText); |
|---|
| 611 | box->addWidget(labelTitle); |
|---|
| 612 | |
|---|
| 613 | osgWidget::Color colorBack = frame->getEmbeddedWindow()->getColor(); |
|---|
| 614 | box->getBackground()->setColor(colorBack); |
|---|
| 615 | |
|---|
| 616 | frame->setWindow(box); |
|---|
| 617 | |
|---|
| 618 | box->resize(); |
|---|
| 619 | frame->resizeFrame(box->getWidth(), box->getHeight()); |
|---|
| 620 | return frame.release(); |
|---|
| 621 | } |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | const char* LABEL1 = |
|---|
| 625 | "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed\n" |
|---|
| 626 | "do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" |
|---|
| 627 | "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris\n" |
|---|
| 628 | "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in..." |
|---|
| 629 | ; |
|---|
| 630 | |
|---|
| 631 | int main(int argc, char** argv) |
|---|
| 632 | { |
|---|
| 633 | std::string theme = "osgWidget/theme-1.png"; |
|---|
| 634 | if (argc > 1) |
|---|
| 635 | theme = std::string(argv[1]); |
|---|
| 636 | |
|---|
| 637 | osgViewer::Viewer viewer; |
|---|
| 638 | |
|---|
| 639 | osgWidget::WindowManager* wm = new osgWidget::WindowManager( |
|---|
| 640 | &viewer, |
|---|
| 641 | 1280.0f, |
|---|
| 642 | 1024.0f, |
|---|
| 643 | MASK_2D, |
|---|
| 644 | osgWidget::WindowManager::WM_PICK_DEBUG |
|---|
| 645 | ); |
|---|
| 646 | |
|---|
| 647 | osgWidget::Frame* frame = createErrorMessage(theme, |
|---|
| 648 | "osgWidget/theme-8-shadow.png", |
|---|
| 649 | "Error - Critical", |
|---|
| 650 | LABEL1, |
|---|
| 651 | "Ok", |
|---|
| 652 | "fonts/Vera.ttf", |
|---|
| 653 | 20); |
|---|
| 654 | |
|---|
| 655 | wm->addChild(frame); |
|---|
| 656 | frame->resizeAdd(30, 30); |
|---|
| 657 | |
|---|
| 658 | AlphaSetterVisitor alpha(.8f); |
|---|
| 659 | frame->accept(alpha); |
|---|
| 660 | return osgWidget::createExample(viewer, wm, osgDB::readNodeFile("cow.osg")); |
|---|
| 661 | } |
|---|
| 662 | #endif |
|---|