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

Revision 8588, 3.6 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: osgwidgetbox.cpp 59 2008-05-15 20:55:31Z cubicool $
3
4// NOTE: You'll find this example very similar to osgwidgetwindow. However, here we
5// demonstrate a bit of subclassing of Widget so that we can respond to events
6// such as mouseEnter and mouseLeave. We also demonstrate the use of padding, though
7// fill and alignment should be working too.
8
9#include <osg/io_utils>
10#include <osgDB/ReadFile>
11#include <osgWidget/Util>
12#include <osgWidget/WindowManager>
13#include <osgWidget/Box>
14
15const unsigned int MASK_2D = 0xF0000000;
16const unsigned int MASK_3D = 0x0F000000;
17
18struct ColorWidget: public osgWidget::Widget {
19        ColorWidget():
20        osgWidget::Widget("", 256.0f, 256.0f) {
21        }
22
23        bool mouseEnter(double, double, osgWidget::WindowManager*) {
24                addColor(-osgWidget::Color(0.4f, 0.4f, 0.4f, 0.0f));
25               
26                // osgWidget::warn() << "enter: " << getColor() << std::endl;
27
28                return true;
29        }
30
31        bool mouseLeave(double, double, osgWidget::WindowManager*) {
32                addColor(osgWidget::Color(0.4f, 0.4f, 0.4f, 0.0f));
33               
34                // osgWidget::warn() << "leave: " << getColor() << std::endl;
35               
36                return true;
37        }
38
39        bool mouseOver(double x, double y, osgWidget::WindowManager*) {
40                osgWidget::Color c = getImageColorAtPointerXY(x, y);
41
42                if(c.a() < 0.001f) {
43                        // osgWidget::warn() << "Transparent Pixel: " << x << " " << y << std::endl;
44
45                        return false;
46                }
47
48                return true;
49        }
50
51        bool keyUp(int key, int keyMask, osgWidget::WindowManager*) {
52                // osgWidget::warn() << "..." << key << " - " << keyMask << std::endl;
53
54                return true;
55        }
56};
57
58osgWidget::Box* createBox(const std::string& name, osgWidget::Box::BOX_TYPE bt) {
59        osgWidget::Box*    box     = new osgWidget::Box(name, bt, true);
60        osgWidget::Widget* widget1 = new osgWidget::Widget(name + "_widget1", 100.0f, 100.0f);
61        osgWidget::Widget* widget2 = new osgWidget::Widget(name + "_widget2", 100.0f, 100.0f);
62        osgWidget::Widget* widget3 = new ColorWidget();
63
64        widget1->setColor(0.3f, 0.3f, 0.3f, 1.0f);
65        widget2->setColor(0.6f, 0.6f, 0.6f, 1.0f);
66
67        widget3->setImage("osgWidget/natascha.png");
68        widget3->setTexCoord(0.0f, 0.0f, osgWidget::Widget::LOWER_LEFT);
69        widget3->setTexCoord(1.0f, 0.0f, osgWidget::Widget::LOWER_RIGHT);
70        widget3->setTexCoord(1.0f, 1.0f, osgWidget::Widget::UPPER_RIGHT);
71        widget3->setTexCoord(0.0f, 1.0f, osgWidget::Widget::UPPER_LEFT);
72
73        box->addWidget(widget1);
74        box->addWidget(widget2);
75        box->addWidget(widget3);
76
77        return box;
78}
79
80int main(int argc, char** argv) {
81        osgViewer::CompositeViewer viewer;
82
83        osgViewer::View* view = new osgViewer::View();
84
85        osgWidget::WindowManager* wm = new osgWidget::WindowManager(
86                view,
87                1280.0f,
88                1024.0f,
89                MASK_2D,
90                osgWidget::WindowManager::WM_PICK_DEBUG |
91                osgWidget::WindowManager::WM_NO_INVERT_Y
92        );
93       
94        wm->setPointerFocusMode(osgWidget::WindowManager::PFM_SLOPPY);
95
96        osgWidget::Window* box1 = createBox("HBOX", osgWidget::Box::HORIZONTAL);
97        osgWidget::Window* box2 = createBox("VBOX", osgWidget::Box::VERTICAL);
98        osgWidget::Window* box3 = createBox("HBOX2", osgWidget::Box::HORIZONTAL);
99        osgWidget::Window* box4 = createBox("VBOX2", osgWidget::Box::VERTICAL);
100
101        box1->getBackground()->setColor(1.0f, 0.0f, 0.0f, 0.8f);
102        box1->attachMoveCallback();
103
104        box2->getBackground()->setColor(0.0f, 1.0f, 0.0f, 0.8f);
105        box2->attachMoveCallback();
106
107        box3->getBackground()->setColor(0.0f, 0.0f, 1.0f, 0.8f);
108        box3->attachMoveCallback();
109
110        wm->addChild(box1);
111        wm->addChild(box2);
112        wm->addChild(box3);
113        wm->addChild(box4);
114
115        box4->hide();
116
117        osg::Node* model = osgDB::readNodeFile("spaceship.osg");
118
119        model->setNodeMask(MASK_3D);
120
121        return osgWidget::createCompositeExample(viewer, view, wm, model);
122}
Note: See TracBrowser for help on using the browser.