root/OpenSceneGraph/trunk/examples/osgwidgetprogress/osgwidgetprogress.cpp @ 12529

Revision 12529, 2.7 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
RevLine 
[9052]1// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
2// $Id$
3
4#include <osgDB/ReadFile>
5#include <osgWidget/Util>
6#include <osgWidget/WindowManager>
7#include <osgWidget/Canvas>
8
9const unsigned int MASK_2D = 0xF0000000;
10
11struct UpdateProgressNode: public osg::NodeCallback {
[9370]12    float start;
13    float done;
[9052]14
[9370]15    UpdateProgressNode():
16    start (0.0f),
17    done  (5.0f) {
18    }
[9052]19
[9370]20    virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
21        const osg::FrameStamp* fs = nv->getFrameStamp();
[9052]22
[9370]23        float t = fs->getSimulationTime();
[9052]24
[9370]25        if(start == 0.0f) start = t;
[9052]26
[9370]27        float width   = ((t - start) / done) * 512.0f;
28        float percent = (width / 512.0f) * 100.0f;
29   
30        if(width < 1.0f || width > 512.0f) return;
[9052]31
[9370]32        osgWidget::Window* window = dynamic_cast<osgWidget::Window*>(node);
[9052]33
[9370]34        if(!window) return;
[9052]35
[9370]36        osgWidget::Widget* w = window->getByName("pMeter");
37        osgWidget::Label*  l = dynamic_cast<osgWidget::Label*>(window->getByName("pLabel"));
[9052]38
[9370]39        if(!w || !l) return;
[9052]40
[9370]41        w->setWidth(width);
42        w->setTexCoordRegion(0.0f, 0.0f, width, 64.0f);
[9052]43
[9370]44        std::ostringstream ss;
[9052]45
[9370]46        ss << osg::round(percent) << "% Done" << std::endl;
[9052]47
[9370]48        l->setLabel(ss.str());
49    }
[9052]50};
51
52int main(int argc, char** argv) {
[9370]53    osgViewer::Viewer viewer;
[9052]54
[9370]55    osgWidget::WindowManager* wm = new osgWidget::WindowManager(
56        &viewer,
57        1280.0f,
58        1024.0f,
59        MASK_2D,
60        osgWidget::WindowManager::WM_PICK_DEBUG
61    );
62   
63    osgWidget::Canvas* canvas   = new osgWidget::Canvas("canvas");
64    osgWidget::Widget* pOutline = new osgWidget::Widget("pOutline", 512.0f, 64.0f);
65    osgWidget::Widget* pMeter   = new osgWidget::Widget("pMeter", 0.0f, 64.0f);
66    osgWidget::Label*  pLabel   = new osgWidget::Label("pLabel", "0% Done");
[9052]67
[9370]68    pOutline->setImage("osgWidget/progress-outline.png", true);
69    pOutline->setLayer(osgWidget::Widget::LAYER_MIDDLE, 2);
70   
71    pMeter->setImage("osgWidget/progress-meter.png");
72    pMeter->setColor(0.7f, 0.1f, 0.1f, 0.7f);
73    pMeter->setLayer(osgWidget::Widget::LAYER_MIDDLE, 1);
[9052]74
[9370]75    pLabel->setFont("fonts/VeraMono.ttf");
76    pLabel->setFontSize(20);
77    pLabel->setFontColor(1.0f, 1.0f, 1.0f, 1.0f);
78    pLabel->setSize(512.0f, 64.0f);
79    pLabel->setLayer(osgWidget::Widget::LAYER_MIDDLE, 3);
[9052]80
[9370]81    canvas->setOrigin(300.0f, 300.0f);
82    canvas->addWidget(pMeter, 0.0f, 0.0f);
83    canvas->addWidget(pOutline, 0.0f, 0.0f);
84    canvas->addWidget(pLabel, 0.0f, 0.0f);
85    canvas->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f);
86    canvas->setUpdateCallback(new UpdateProgressNode());
[9052]87
[9370]88    wm->addChild(canvas);
[9052]89
[12529]90    return osgWidget::createExample(viewer, wm, osgDB::readNodeFile("cow.osgt"));
[9052]91}
Note: See TracBrowser for help on using the browser.