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

Revision 12292, 4.4 kB (checked in by robert, 4 years ago)

Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

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