root/OpenSceneGraph/trunk/examples/osgwidgetmenu/osgwidgetmenu.cpp @ 12310

Revision 12310, 3.5 kB (checked in by robert, 3 years 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: osgwidgetmenu.cpp 66 2008-07-14 21:54:09Z cubicool $
3
4#include <iostream>
5#include <osgDB/ReadFile>
6#include <osgWidget/Util>
7#include <osgWidget/WindowManager>
8#include <osgWidget/Box>
9#include <osgWidget/Label>
10
11// For now this is just an example, but osgWidget::Menu will later be it's own Window.
12// I just wanted to get this out there so that people could see it was possible.
13
14const unsigned int MASK_2D = 0xF0000000;
15const unsigned int MASK_3D = 0x0F000000;
16
17struct ColorLabel: public osgWidget::Label {
18    ColorLabel(const char* label):
19    osgWidget::Label("", "") {
20        setFont("fonts/Vera.ttf");
21        setFontSize(14);
22        setFontColor(1.0f, 1.0f, 1.0f, 1.0f);
23        setColor(0.3f, 0.3f, 0.3f, 1.0f);
24        addHeight(18.0f);
25        setCanFill(true);
26        setLabel(label);
27        setEventMask(osgWidget::EVENT_MOUSE_PUSH | osgWidget::EVENT_MASK_MOUSE_MOVE);
28    }
29
30    bool mousePush(double, double, const osgWidget::WindowManager*) {
31        return true;
32    }
33
34    bool mouseEnter(double, double, const osgWidget::WindowManager*) {
35        setColor(0.6f, 0.6f, 0.6f, 1.0f);
36       
37        return true;
38    }
39
40    bool mouseLeave(double, double, const osgWidget::WindowManager*) {
41        setColor(0.3f, 0.3f, 0.3f, 1.0f);
42       
43        return true;
44    }
45};
46
47class ColorLabelMenu: public ColorLabel {
48    osg::ref_ptr<osgWidget::Window> _window;
49
50public:
51    ColorLabelMenu(const char* label):
52    ColorLabel(label) {
53        _window = new osgWidget::Box(
54            std::string("Menu_") + label,
55            osgWidget::Box::VERTICAL,
56            true
57        );
58
59        _window->addWidget(new ColorLabel("Open Some Stuff"));
60        _window->addWidget(new ColorLabel("Do It Now"));
61        _window->addWidget(new ColorLabel("Hello, How Are U?"));
62        _window->addWidget(new ColorLabel("Hmmm..."));
63        _window->addWidget(new ColorLabel("Option 5"));
64
65        _window->resize();
66
67        setColor(0.8f, 0.8f, 0.8f, 0.8f);
68    }
69
70    void managed(osgWidget::WindowManager* wm) {
71        osgWidget::Label::managed(wm);
72
73        wm->addChild(_window.get());
74
75        _window->hide();
76    }
77
78    void positioned() {
79        osgWidget::Label::positioned();
80
81        _window->setOrigin(getX(), getHeight());
82        _window->resize(getWidth());
83    }
84
85    bool mousePush(double, double, const osgWidget::WindowManager*) {
86        if(!_window->isVisible()) _window->show();
87
88        else _window->hide();
89
90        return true;
91    }
92
93    bool mouseLeave(double, double, const osgWidget::WindowManager*) {
94        if(!_window->isVisible()) setColor(0.8f, 0.8f, 0.8f, 0.8f);
95
96        return true;
97    }
98};
99
100int main(int argc, char** argv) {
101    osgViewer::Viewer viewer;
102
103    osgWidget::WindowManager* wm = new osgWidget::WindowManager(
104        &viewer,
105        1280.0f,
106        1024.0f,
107        MASK_2D,
108        osgWidget::WindowManager::WM_PICK_DEBUG
109    );
110
111    osgWidget::Window* menu = new osgWidget::Box("menu", osgWidget::Box::HORIZONTAL);
112
113    menu->addWidget(new ColorLabelMenu("Pick me!"));
114    menu->addWidget(new ColorLabelMenu("No, wait, pick me!"));
115    menu->addWidget(new ColorLabelMenu("Dont pick them..."));
116    menu->addWidget(new ColorLabelMenu("Grarar!?!"));
117
118    wm->addChild(menu);
119   
120    menu->getBackground()->setColor(1.0f, 1.0f, 1.0f, 0.0f);
121    menu->resizePercent(100.0f);
122
123    osg::Node* model = osgDB::readNodeFile("osgcool.osg");
124
125    model->setNodeMask(MASK_3D);
126
127    return osgWidget::createExample(viewer, wm, model);
128}
Note: See TracBrowser for help on using the browser.