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

Revision 8588, 3.1 kB (checked in by robert, 6 years ago)

From Jeremy Moles, import of the osgWidget NodeKit?, sourced from the original http://osgwidget.googlecode.com/svn/trunk

Notes from Robert Osfield, I've merged osgWidget trunk, and added/changed CMakeLists.txt file to make it suitable for inclusion in the core OSG, and moved imagery/scripts/shaders out into OpenSceneGraph-Data

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/Calibri1.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, osgWidget::WindowManager*) {
31                return true;
32        }
33
34        bool mouseEnter(double, double, osgWidget::WindowManager*) {
35                setColor(0.6f, 0.6f, 0.6f, 1.0f);
36               
37                return true;
38        }
39
40        bool mouseLeave(double, double, 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, osgWidget::WindowManager*) {
86                if(!_window->isVisible()) _window->show();
87
88                else _window->hide();
89
90                return true;
91        }
92
93        bool mouseLeave(double, double, 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                osgWidget::WindowManager::WM_NO_BETA_WARN
110        );
111
112        osgWidget::Window* menu = new osgWidget::Box("menu", osgWidget::Box::HORIZONTAL);
113
114        menu->addWidget(new ColorLabelMenu("Pick me!"));
115        menu->addWidget(new ColorLabelMenu("No, wait, pick me!"));
116        menu->addWidget(new ColorLabelMenu("Dont pick them..."));
117        menu->addWidget(new ColorLabelMenu("Grarar!?!"));
118
119        wm->addChild(menu);
120       
121        menu->getBackground()->setColor(1.0f, 1.0f, 1.0f, 0.0f);
122        menu->resizePercent(100.0f);
123
124        osg::Node* model = osgDB::readNodeFile("osgcool.osg");
125
126        model->setNodeMask(MASK_3D);
127
128        return osgWidget::createExample(viewer, wm, model);
129}
Note: See TracBrowser for help on using the browser.