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

Revision 8588, 4.9 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: osgwidgetinput.cpp 50 2008-05-06 05:06:36Z cubicool $
3
4#include <osgDB/WriteFile>
5
6#include <osgWidget/Util>
7#include <osgWidget/WindowManager>
8#include <osgWidget/Box>
9#include <osgWidget/Table>
10#include <osgWidget/Frame>
11#include <osgWidget/Label>
12#include <osgWidget/Input>
13
14const unsigned int MASK_2D = 0xF0000000;
15
16const char* INFO =
17        "Use the Input Wigets below to enter the X, Y, and Z position of a\n"
18        "sphere to be inserted into the scene. Once you've done this, use\n"
19        "the button below to add it!"
20;
21
22void setupLabel(osgWidget::Label* label) {
23        label->setFontSize(16);
24        label->setFontColor(1.0f, 1.0f, 1.0f, 1.0f);
25        // label->setFont("fonts/monospace.ttf");
26        label->setFont("fonts/Calibri1.ttf");
27        label->setPadding(2.0f);
28        label->setHeight(18.0f);
29        label->setCanFill(true);
30}
31
32osgWidget::Input* createTableRow(
33        osgWidget::Table*  table,
34        unsigned int       rowNum,
35        const std::string& valName
36) {
37        std::stringstream ssLabel;
38        std::stringstream ssInput;
39
40        ssLabel << "Label_Row" << rowNum;
41        ssInput << "Input_Row" << rowNum;
42
43        osgWidget::Label* label = new osgWidget::Label(ssLabel.str(), valName);
44        osgWidget::Input* input = new osgWidget::Input(ssInput.str(), "", 20);
45
46        setupLabel(label);
47        setupLabel(input);
48
49        label->setWidth(50.0f);
50        label->setColor(0.1f, 0.1f, 0.1f, 1.0f);
51
52        input->setWidth(150.0f);
53        input->setColor(0.4f, 0.4f, 0.4f, 1.0f);
54
55        table->addWidget(label, rowNum, 0);
56        table->addWidget(input, rowNum, 1);
57
58        return input;
59}
60
61osgWidget::Label* createLabel(const std::string& text) {
62        osgWidget::Label* label = new osgWidget::Label("", text);
63
64        setupLabel(label);
65
66        return label;
67}
68
69class Button: public osgWidget::Label {
70public:
71        typedef std::vector<osgWidget::Input*> Inputs;
72
73private:
74        Inputs _xyz;
75
76public:
77        Button(const std::string& text, const Inputs& inputs):
78        osgWidget::Label("", text),
79        _xyz(inputs) {
80                setupLabel(this);
81
82                setEventMask(osgWidget::EVENT_MASK_MOUSE_CLICK);
83                setShadow(0.1f);
84                addHeight(4.0f);
85        }
86
87        bool mousePush(double, double, osgWidget::WindowManager*) {
88                osgWidget::warn()
89                        << "x: " << _xyz[0]->getLabel() << std::endl
90                        << "y: " << _xyz[1]->getLabel() << std::endl
91                        << "z: " << _xyz[2]->getLabel() << std::endl
92                ;
93
94                return false;
95        }
96};
97
98// TODO: Testing our _parent/EmbeddedWindow stuff.
99bool info(osgWidget::Event& ev) {
100        osgWidget::warn() << "MousePush @ Window: " << ev.getWindow()->getName() << std::endl;
101
102        return true;
103}
104
105int main(int argc, char** argv) {
106        osgViewer::Viewer viewer;
107
108        osgWidget::WindowManager* wm = new osgWidget::WindowManager(
109                &viewer,
110                1280.0f,
111                1024.0f,
112                MASK_2D,
113                osgWidget::WindowManager::WM_PICK_DEBUG
114        );
115       
116        osgWidget::Box*   box   = new osgWidget::Box("vbox", osgWidget::Box::VERTICAL);
117        osgWidget::Table* table = new osgWidget::Table("table", 3, 2);
118        osgWidget::Box*   lbox1 = new osgWidget::Box("lbox1", osgWidget::Box::HORIZONTAL);
119        osgWidget::Box*   lbox2 = new osgWidget::Box("lbox2", osgWidget::Box::HORIZONTAL);
120        osgWidget::Frame* frame = osgWidget::Frame::createSimpleFrameWithSingleTexture(
121                "frame",
122                "osgWidget/theme.png",
123                64.0f,
124                64.0f,
125                16.0f,
126                16.0f,
127                100.0f,
128                100.0f
129        );
130
131        osgWidget::Input* x = createTableRow(table, 0, "X Position");
132        osgWidget::Input* y = createTableRow(table, 1, "Y Position");
133        osgWidget::Input* z = createTableRow(table, 2, "Z Position");
134       
135        Button::Inputs inputs;
136
137        inputs.push_back(x);
138        inputs.push_back(y);
139        inputs.push_back(z);
140
141        table->addCallback(osgWidget::Callback(&info, osgWidget::EVENT_MOUSE_PUSH));
142
143        lbox1->addWidget(createLabel(INFO));
144        lbox2->addWidget(new Button("Add To Scene...", inputs));
145
146        box->addWidget(lbox1->embed());
147        box->addWidget(table->embed());
148        box->addWidget(lbox2->embed());
149        box->addCallback(osgWidget::Callback(&info, osgWidget::EVENT_MOUSE_PUSH));
150
151        frame->setWindow(box);
152        frame->getEmbeddedWindow()->setSize(box->getWidth(), box->getHeight());
153        frame->getBackground()->setColor(0.0f, 0.0f, 0.0f, 0.0f);
154        frame->attachTabFocusCallback();
155
156        for(osgWidget::Frame::Iterator i = frame->begin(); i != frame->end(); i++) {
157                if(i->valid()) i->get()->setColor(0.5f, 0.7f, 1.0f, 1.0f);
158        }
159
160        wm->addChild(frame);
161
162        /*
163        // Print out our focus list, it should just have 3 widgets.
164        osgWidget::WidgetList wl;
165
166        box->getFocusList(wl);
167
168        for(osgWidget::WidgetList::iterator i = wl.begin(); i != wl.end(); i++) {
169                osgWidget::warn() << i->get()->getName() << std::endl;
170        }
171        */
172       
173        lbox1->getBackground()->setColor(1.0f, 0.0f, 0.0f, 1.0f, osgWidget::Widget::UPPER_LEFT);
174        lbox1->getBackground()->setColor(0.0f, 1.0f, 0.0f, 1.0f, osgWidget::Widget::LOWER_LEFT);
175        lbox1->getBackground()->setColor(0.0f, 0.0f, 1.0f, 1.0f, osgWidget::Widget::LOWER_RIGHT);
176        lbox1->getBackground()->setColor(1.0f, 1.0f, 1.0f, 1.0f, osgWidget::Widget::UPPER_RIGHT);
177        lbox1->setVisibilityMode(osgWidget::Window::VM_ENTIRE);
178        lbox1->update();
179
180        int r = osgWidget::createExample(viewer, wm);
181
182        // osgWidget::writeWindowManagerNode(wm);
183        // osgDB::writeNodeFile(*box, "osgWidget.osg");
184
185        return r;
186}
Note: See TracBrowser for help on using the browser.