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

Revision 12292, 4.1 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: osgwidgetcanvas.cpp 33 2008-04-04 19:03:12Z cubicool $
3
4#include <osgWidget/Util>
5#include <osgWidget/WindowManager>
6#include <osgWidget/Canvas>
7
8const unsigned int MASK_2D = 0xF0000000;
9
10bool colorWidgetEnter(osgWidget::Event& event) {
11    event.getWidget()->addColor(0.5f, 0.2f, 0.3f, 0.0f);
12
13    // osgWidget::warn() << "WIDGET mouseEnter " << event.getWidget()->getName() << std::endl;
14   
15    return false;
16}
17
18bool colorWidgetLeave(osgWidget::Event& event) {
19    event.getWidget()->addColor(-0.5f, -0.2f, -0.3f, 0.0f);
20
21    // osgWidget::warn() << "WIDGET mouseLeave" << std::endl;
22   
23    return true;
24}
25
26bool windowMouseOver(osgWidget::Event& event) {
27    osgWidget::XYCoord xy = event.getWindow()->localXY(event.x, event.y);
28
29    // osgWidget::warn() << "WINDOW " << xy.x() << " - " << xy.y() << std::endl;
30
31    return true;
32}
33
34bool widgetMouseOver(osgWidget::Event& event) {
35    osgWidget::XYCoord xy = event.getWidget()->localXY(event.x, event.y);
36
37    // osgWidget::warn() << "WIDGET mouseOver " << xy.x() << " - " << xy.y() << std::endl;
38
39    return true;
40}
41
42osgWidget::Widget* createWidget(
43    const std::string&       name,
44    osgWidget::color_type    col,
45    osgWidget::Widget::Layer layer
46) {
47    osgWidget::Widget* widget = new osgWidget::Widget(name, 200.0f, 200.0f);
48
49    widget->setEventMask(osgWidget::EVENT_ALL);
50    widget->addCallback(new osgWidget::Callback(&colorWidgetEnter, osgWidget::EVENT_MOUSE_PUSH));
51    widget->addCallback(new osgWidget::Callback(&colorWidgetLeave, osgWidget::EVENT_MOUSE_RELEASE));
52    widget->addCallback(new osgWidget::Callback(&colorWidgetEnter, osgWidget::EVENT_MOUSE_ENTER));
53    widget->addCallback(new osgWidget::Callback(&colorWidgetLeave, osgWidget::EVENT_MOUSE_LEAVE));
54    widget->addCallback(new osgWidget::Callback(&widgetMouseOver, osgWidget::EVENT_MOUSE_OVER));
55    widget->setColor(col, col, col, 0.5f);
56    widget->setLayer(layer);
57   
58    return widget;
59}
60
61int main(int argc, char** argv) {
62    osgViewer::Viewer viewer;
63
64    osgWidget::WindowManager* wm = new osgWidget::WindowManager(
65        &viewer,
66        1280.0f,
67        1024.0f,
68        MASK_2D,
69        osgWidget::WindowManager::WM_PICK_DEBUG
70    );
71   
72    osgWidget::Canvas* canvas = new osgWidget::Canvas("canvas");
73
74    canvas->addCallback(new osgWidget::Callback(&windowMouseOver, osgWidget::EVENT_MOUSE_OVER));
75    canvas->attachMoveCallback();
76    canvas->attachRotateCallback();
77    canvas->attachScaleCallback();
78
79    canvas->addWidget(
80        createWidget("w1", 0.2f, osgWidget::Widget::LAYER_LOW),
81        0.0f,
82        0.0f
83    );
84   
85    canvas->addWidget(
86        createWidget("w2", 0.4f, osgWidget::Widget::LAYER_MIDDLE),
87        200.0f,
88        0.0f
89    );
90
91    canvas->addWidget(
92        createWidget("w3", 0.6f, osgWidget::Widget::LAYER_HIGH),
93        400.0f,
94        0.0f
95    );
96
97    // Add a child and then resize it relatively to the size of the parent Window.
98    osgWidget::Widget* relWidget = new osgWidget::Widget("relative");
99
100    relWidget->setLayer(osgWidget::Widget::LAYER_LOW, 1);
101    relWidget->setCoordinateMode(osgWidget::Widget::CM_RELATIVE);
102    relWidget->setSize(0.2f, 0.2f);
103    relWidget->setColor(0.5f, 0.5f, 0.1f, 0.9f);
104
105    osgWidget::warn() << canvas->getWidth() << std::endl;
106
107    canvas->addWidget(relWidget, 0.4f, 0.4f);
108   
109    relWidget->addOrigin(0.1f, 0.1f);
110    relWidget->addSize(0.2f, 0.2f);
111
112    canvas->resize();
113
114    // Finally, add the whole thing to the WindowManager.
115    wm->addChild(canvas);
116
117    return osgWidget::createExample(viewer, wm);
118}
119
120/*
121int main(int argc, char** argv) {
122    osgViewer::Viewer viewer;
123
124    osgWidget::WindowManager* wm = new osgWidget::WindowManager(
125        &viewer,
126        1280.0f,
127        1024.0f,
128        MASK_2D,
129        osgWidget::WindowManager::WM_PICK_DEBUG
130    );
131   
132    osgWidget::Canvas* canvas = new osgWidget::Canvas("canvas");
133
134    canvas->addWidget(new osgWidget::Widget("spacer", 2.0f, 300.0f), 1280.0f, 0.0f);
135
136    canvas->setOrigin(0.0f, 300.0f);
137
138    wm->addChild(canvas);
139
140    return osgWidget::createExample(viewer, wm);
141}
142*/
Note: See TracBrowser for help on using the browser.