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

Revision 9289, 2.5 kB (checked in by robert, 5 years ago)

From Jeremy Moles, updates to osgwidget examples.

Merged by Robert Osfield from OpenSceneGraph-osgWidget-dev

Line 
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 {
12        float start;
13        float done;
14
15        UpdateProgressNode():
16        start (0.0f),
17        done  (5.0f) {
18        }
19
20        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) {
21                const osg::FrameStamp* fs = nv->getFrameStamp();
22
23                float t = fs->getSimulationTime();
24
25                if(start == 0.0f) start = t;
26
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;
31
32                osgWidget::Window* window = dynamic_cast<osgWidget::Window*>(node);
33
34                if(!window) return;
35
36                osgWidget::Widget* w = window->getByName("pMeter");
37                osgWidget::Label*  l = dynamic_cast<osgWidget::Label*>(window->getByName("pLabel"));
38
39                if(!w || !l) return;
40
41                w->setWidth(width);
42                w->setTexCoordRegion(0.0f, 0.0f, width, 64.0f);
43
44                std::ostringstream ss;
45
46                ss << osg::round(percent) << "% Done" << std::endl;
47
48                l->setLabel(ss.str());
49        }
50};
51
52int main(int argc, char** argv) {
53        osgViewer::Viewer viewer;
54
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");
67
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);
74
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);
80
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());
87
88        wm->addChild(canvas);
89
90        return osgWidget::createExample(viewer, wm, osgDB::readNodeFile("cow.osg"));
91}
Note: See TracBrowser for help on using the browser.