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

Revision 8588, 4.0 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: osgwidgetframe.cpp 34 2008-04-07 03:12:41Z cubicool $
3
4#include <osgWidget/Util>
5#include <osgWidget/WindowManager>
6#include <osgWidget/Frame>
7#include <osgWidget/Box>
8
9const unsigned int MASK_2D = 0xF0000000;
10
11// NOTE: THIS IS JUST A TEMPORARY HACK! :) This functionality will all eventually be
12// encapsulate into another class in osgWidget proper.
13bool scrollWindow(osgWidget::Event& ev) {
14        // The first thing we need to do is make sure we have a Frame object...
15        osgWidget::Frame* frame = dynamic_cast<osgWidget::Frame*>(ev.getWindow());
16
17        if(!frame) return false;
18
19        // And now we need to make sure our Frame has a valid internal EmbeddedWindow widget.
20        osgWidget::Window::EmbeddedWindow* ew =
21                dynamic_cast<osgWidget::Window::EmbeddedWindow*>(frame->getEmbeddedWindow())
22        ;
23               
24        if(!ew) return false;
25       
26        // Lets get the visible area so that we can use it to make sure our scrolling action
27        // is necessary in the first place.
28        const osgWidget::Quad& va = ew->getWindow()->getVisibleArea();
29
30        // The user wants to scroll up; make sure that the visible area's Y origin isn't already
31        // at 0.0f, 0.0f.
32        if(ev.getWindowManager()->isMouseScrollingUp() && va[1] != 0.0f)
33                ew->getWindow()->addVisibleArea(0, -20)
34        ;
35       
36        else if(va[1] <= (ew->getWindow()->getHeight() - ew->getHeight()))
37                ew->getWindow()->addVisibleArea(0, 20)
38        ;
39
40        // We need to manually call update to make sure the visible area scissoring is done
41        // properly.
42        frame->update();
43
44        return true;
45}
46
47bool changeTheme(osgWidget::Event& ev) {
48        std::string theme;
49
50        if(ev.key == osgGA::GUIEventAdapter::KEY_Right)
51                theme = "osgWidget/theme-1.png"
52        ;
53
54        else if(ev.key == osgGA::GUIEventAdapter::KEY_Left)
55                theme = "osgWidget/theme-2.png"
56        ;
57
58        else return false;
59
60        osgWidget::Frame* frame = dynamic_cast<osgWidget::Frame*>(ev.getWindow());
61
62        if(!frame) return false;
63
64        // This is just one way to access all our Widgets; we could just as well have used:
65        //
66        // for(osgWidget::Frame::Iterator i = frame.begin(); i != frame.end() i++) {}
67        //
68        // ...and it have worked, too.
69        for(unsigned int row = 0; row < 3; row++) {
70                for(unsigned int col = 0; col < 3; col++) {
71                        frame->getByRowCol(row, col)->setImage(theme);
72                }
73        }
74
75        return true;
76}
77
78int main(int argc, char** argv) {
79        osgViewer::Viewer viewer;
80
81        osgWidget::WindowManager* wm = new osgWidget::WindowManager(
82                &viewer,
83                1280.0f,
84                1024.0f,
85                MASK_2D,
86                osgWidget::WindowManager::WM_PICK_DEBUG
87        );
88       
89        osgWidget::Frame* frame = osgWidget::Frame::createSimpleFrameWithSingleTexture(
90                "frame",
91                "../examples/osgwidgetscrolled/theme-2.png",
92                64.0f,
93                64.0f,
94                16.0f,
95                16.0f,
96                100.0f,
97                100.0f
98        );
99
100        frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f);
101
102        // This is our Transformers box. :)
103        osgWidget::Box*    box  = new osgWidget::Box("images", osgWidget::Box::VERTICAL);
104        osgWidget::Widget* img1 = new osgWidget::Widget("im1", 256.0f, 256.0f);
105        osgWidget::Widget* img2 = new osgWidget::Widget("im2", 256.0f, 256.0f);
106        osgWidget::Widget* img3 = new osgWidget::Widget("im3", 256.0f, 256.0f);
107        osgWidget::Widget* img4 = new osgWidget::Widget("im4", 256.0f, 256.0f);
108
109        img1->setImage("../examples/osgwidgetscrolled/images/starscream.jpg", true);
110        img2->setImage("../examples/osgwidgetscrolled/images/optimus.jpg", true);
111        img3->setImage("../examples/osgwidgetscrolled/images/megatron.jpg", true);
112        img4->setImage("../examples/osgwidgetscrolled/images/bumblebee.jpg", true);
113
114        img1->setMinimumSize(10.0f, 10.0f);
115        img2->setMinimumSize(10.0f, 10.0f);
116        img3->setMinimumSize(10.0f, 10.0f);
117        img4->setMinimumSize(10.0f, 10.0f);
118
119        box->addWidget(img1);
120        box->addWidget(img2);
121        box->addWidget(img3);
122        box->addWidget(img4);
123        box->setEventMask(osgWidget::EVENT_NONE);
124
125        frame->getEmbeddedWindow()->setWindow(box);
126        frame->getEmbeddedWindow()->setColor(1.0f, 1.0f, 1.0f, 1.0f);
127        frame->resize(300.0f, 300.0f);
128        frame->addCallback(osgWidget::Callback(&scrollWindow, osgWidget::EVENT_MOUSE_SCROLL));
129        frame->addCallback(osgWidget::Callback(&changeTheme, osgWidget::EVENT_KEY_DOWN));
130
131        wm->addChild(frame);
132
133        return osgWidget::createExample(viewer, wm);
134}
Note: See TracBrowser for help on using the browser.