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

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

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

  • Property svn:eol-style set to native
RevLine 
[8588]1// -*-c++-*- osgWidget - Code by: Jeremy Moles (cubicool) 2007-2008
2// $Id: osgwidgetframe.cpp 34 2008-04-07 03:12:41Z cubicool $
3
[9289]4#include <osgDB/ReadFile>
5
[8588]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) {
[8600]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());
[8588]18
[8600]19    if(!frame) return false;
[8588]20
[8600]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();
[8588]31
[8600]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    ;
[8588]41
[8600]42    // We need to manually call update to make sure the visible area scissoring is done
43    // properly.
44    frame->update();
[8588]45
[8600]46    return true;
[8588]47}
48
49bool changeTheme(osgWidget::Event& ev) {
[8600]50    std::string theme;
[8588]51
[8600]52    if(ev.key == osgGA::GUIEventAdapter::KEY_Right)
53        theme = "osgWidget/theme-1.png"
54    ;
[8588]55
[8600]56    else if(ev.key == osgGA::GUIEventAdapter::KEY_Left)
57        theme = "osgWidget/theme-2.png"
58    ;
[8588]59
[8600]60    else return false;
[8588]61
[8600]62    osgWidget::Frame* frame = dynamic_cast<osgWidget::Frame*>(ev.getWindow());
[8588]63
[8600]64    if(!frame) return false;
[8588]65
[8600]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    }
[8588]76
[8600]77    return true;
[8588]78}
79
80int main(int argc, char** argv) {
[8600]81    osgViewer::Viewer viewer;
[8588]82
[8600]83    osgWidget::WindowManager* wm = new osgWidget::WindowManager(
84        &viewer,
85        1280.0f,
86        1024.0f,
87        MASK_2D,
88        osgWidget::WindowManager::WM_PICK_DEBUG
[9289]89        //osgWidget::WindowManager::WM_NO_INVERT_Y
[8600]90    );
91   
[9289]92    osgWidget::Frame* frame = osgWidget::Frame::createSimpleFrameFromTheme(
[8600]93        "frame",
[9289]94        osgDB::readImageFile("osgWidget/theme.png"),
95        40.0f,
96        40.0f,
97        osgWidget::Frame::FRAME_ALL
[8600]98    );
[8588]99
[8600]100    frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f);
[8588]101
[8600]102    // This is our Transformers box. :)
103    osgWidget::Box*    box  = new osgWidget::Box("images", osgWidget::Box::VERTICAL);
[8602]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);
[8588]108
[8602]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);
[8588]113
[8600]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);
[8588]118
[8600]119    box->addWidget(img1);
120    box->addWidget(img2);
121    box->addWidget(img3);
122    box->addWidget(img4);
123    box->setEventMask(osgWidget::EVENT_NONE);
[8588]124
[9289]125    //frame->getEmbeddedWindow()->setWindow(box);
126    frame->setWindow(box);
[8600]127    frame->getEmbeddedWindow()->setColor(1.0f, 1.0f, 1.0f, 1.0f);
128    frame->resize(300.0f, 300.0f);
[9289]129    frame->addCallback(new osgWidget::Callback(&scrollWindow, osgWidget::EVENT_MOUSE_SCROLL));
130    frame->addCallback(new osgWidget::Callback(&changeTheme, osgWidget::EVENT_KEY_DOWN));
[8588]131
[8600]132    wm->addChild(frame);
[8588]133
[8600]134    return osgWidget::createExample(viewer, wm);
[8588]135}
Note: See TracBrowser for help on using the browser.