root/OpenSceneGraph/trunk/examples/osgwidgetaddremove/osgwidgetaddremove.cpp

Revision 12310, 3.5 kB (checked in by robert, 13 months ago)

From Mattias Helsing, "Fixes two of the osgWidget examples that were broken due to changed
virtual function prototypes in osgWidget/EventInterface"

  • Property svn:eol-style set to native
Line 
1// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
2// $Id: osgwidgetaddremove.cpp 45 2008-04-23 16:46:11Z cubicool $
3
4#include <osgWidget/Util>
5#include <osgWidget/WindowManager>
6#include <osgWidget/Table>
7#include <osgWidget/Box>
8#include <osgWidget/Label>
9
10const unsigned int MASK_2D = 0xF0000000;
11
12class ABCWidget: public osgWidget::Label {
13public:
14    ABCWidget(const std::string& label):
15    osgWidget::Label("", label) {
16        setFont("fonts/Vera.ttf");
17        setFontSize(20);
18        setCanFill(true);
19        setShadow(0.08f);
20        addSize(10.0f, 10.0f);
21    }
22};
23
24class Button: public osgWidget::Label {
25public:
26    Button(const std::string& label):
27    osgWidget::Label("", label) {
28        setFont("fonts/Vera.ttf");
29        setFontSize(30);
30        setColor(0.8f, 0.2f, 0.2f, 0.8f);
31        setCanFill(true);
32        setShadow(0.1f);
33        setEventMask(osgWidget::EVENT_MASK_MOUSE_CLICK);
34        addSize(20.0f, 20.0f);
35    }
36
37    // NOTE! I need to make it clearer than Push/Release can happen so fast that
38    // the changes you make aren't visible with your refresh rate. Throttling state
39    // changes and what-have-you on mousePush/mouseRelease/etc. is going to be
40    // annoying...
41
42    virtual bool mousePush(double, double, const osgWidget::WindowManager*) {
43        addColor(0.2f, 0.2f, 0.2f, 0.0f);
44       
45        return true;
46    }
47
48    virtual bool mouseRelease(double, double, const osgWidget::WindowManager*) {
49        addColor(-0.2f, -0.2f, -0.2f, 0.0f);
50       
51        return true;
52    }
53};
54
55class AddRemove: public osgWidget::Box {
56    osg::ref_ptr<osgWidget::Window> _win1;
57
58public:
59    AddRemove():
60    osgWidget::Box ("buttons", osgWidget::Box::VERTICAL),
61    _win1          (new osgWidget::Box("win1", osgWidget::Box::VERTICAL)) {
62        addWidget(new Button("Add Widget"));
63        addWidget(new Button("Remove Widget"));
64
65        // Take special note here! Not only do the Button objects have their
66        // own overridden methods for changing the color, but they have attached
67        // callbacks for doing the work with local data.
68        getByName("Widget_1")->addCallback(new osgWidget::Callback(
69            &AddRemove::handlePressAdd,
70            this,
71            osgWidget::EVENT_MOUSE_PUSH
72        ));
73
74        getByName("Widget_2")->addCallback(new osgWidget::Callback(
75            &AddRemove::handlePressRemove,
76            this,
77            osgWidget::EVENT_MOUSE_PUSH
78        ));
79    }
80
81    virtual void managed(osgWidget::WindowManager* wm) {
82        osgWidget::Box::managed(wm);
83
84        _win1->setOrigin(250.0f, 0.0f);
85
86        wm->addChild(_win1.get());
87    }
88
89    bool handlePressAdd(osgWidget::Event& ev) {
90        static unsigned int num = 0;
91
92        std::stringstream ss;
93
94        ss << "a random widget " << num;
95
96        _win1->addWidget(new ABCWidget(ss.str()));
97
98        num++;
99
100        return true;
101    }
102
103    bool handlePressRemove(osgWidget::Event& ev) {
104        // TODO: Temporary hack!
105        const osgWidget::Box::Vector& v = _win1->getObjects();
106   
107        if(!v.size()) return false;
108
109        osgWidget::Widget* w = _win1->getObjects()[v.size() - 1].get();
110
111        _win1->removeWidget(w);
112
113        return true;
114    }
115};
116
117int main(int argc, char** argv) {
118    osgViewer::Viewer viewer;
119
120    osgWidget::WindowManager* wm = new osgWidget::WindowManager(
121        &viewer,
122        1280.0f,
123        1024.0f,
124        MASK_2D
125    );
126   
127    osgWidget::Box* buttons = new AddRemove();
128
129    wm->addChild(buttons);
130
131    return createExample(viewer, wm);
132}
Note: See TracBrowser for help on using the browser.