root/OpenSceneGraph/trunk/examples/osgkeyboard/osgkeyboard.cpp @ 1744

Revision 1744, 10.1 kB (checked in by robert, 12 years ago)

Added new osgkeyboad example.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgProducer/Viewer>
2
3#include <osg/MatrixTransform>
4#include <osg/Geode>
5#include <osg/Group>
6#include <osg/Switch>
7#include <osg/Notify>
8#include <osg/Geometry>
9
10#include <osgText/Text>
11
12#include <osgDB/Registry>
13#include <osgDB/ReadFile>
14#include <osgDB/WriteFile>
15
16
17class KeyboardModel : public osg::Referenced
18{
19public:
20
21        KeyboardModel() { createKeyboard(); }
22       
23        osg::Group* getScene() { return _scene.get(); }
24       
25        void keyChange(int key,int value);
26       
27protected:
28       
29        ~KeyboardModel() {}
30
31        void addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height);
32
33        void createKeyboard();
34
35        typedef std::map<int, osg::ref_ptr<osg::Switch> > KeyModelMap;
36       
37        osg::ref_ptr<osg::Group>    _scene;
38        KeyModelMap                 _keyModelMap;
39
40};
41
42void KeyboardModel::keyChange(int key,int value)
43{
44    std::cout << std::hex << key << "\t"<< value << std::dec  << std::endl;
45
46    KeyModelMap::iterator itr = _keyModelMap.find(key);
47    if (itr!=_keyModelMap.end())
48    {
49        itr->second->setSingleChildOn(value);
50       
51    }
52}
53
54void KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height)
55{
56
57    osg::Geode* geodeUp = new osg::Geode;
58    {
59        osgText::Text* textUp = new osgText::Text;
60        textUp->setFont("fonts/arial.ttf");
61        textUp->setColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
62        textUp->setCharacterSize(height);
63        textUp->setPosition(pos);
64        textUp->setDrawMode(osgText::Text::TEXT/*|osgText::Text::BOUNDINGBOX*/);
65        textUp->setAlignment(osgText::Text::LEFT_CENTER);
66        textUp->setAxisAlignment(osgText::Text::XZ_PLANE);
67        textUp->setText(text);
68       
69        geodeUp->addDrawable(textUp);
70    }
71   
72    osg::Geode* geodeDown = new osg::Geode;
73    {
74        osgText::Text* textDown = new osgText::Text;
75        textDown->setFont("fonts/arial.ttf");
76        textDown->setColor(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
77        textDown->setCharacterSize(height);
78        textDown->setPosition(pos);
79        textDown->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
80        textDown->setAlignment(osgText::Text::LEFT_CENTER);
81        textDown->setAxisAlignment(osgText::Text::XZ_PLANE);
82        textDown->setText(text);
83       
84        geodeDown->addDrawable(textDown);
85    }
86
87    osg::Switch* model = new osg::Switch;
88    model->addChild(geodeUp,true);
89    model->addChild(geodeDown,false);
90   
91    _scene->addChild(model);
92
93    _keyModelMap[key] = model;
94   
95    pos.x() += width;
96   
97}
98
99void KeyboardModel::createKeyboard()
100{
101    _scene = new osg::Group;
102   
103    osg::Vec3 pos(0.0f,0.0f,0.0f);
104   
105    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_L,"Ctrl",2.0f,0.5f);
106    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_L,"Super",2.0f,0.5f);
107    addKey(pos,osgGA::GUIEventAdapter::KEY_Alt_L,"Alt",2.0f,0.5f);
108    addKey(pos,osgGA::GUIEventAdapter::KEY_Space,"Space",3.0f,1.0f);
109    addKey(pos,osgGA::GUIEventAdapter::KEY_Mode_switch,"Switch",2.0f,0.5f);
110    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_R,"Super",2.0f,0.5f);
111    addKey(pos,osgGA::GUIEventAdapter::KEY_Menu,"Menu",2.0f,0.5f);
112    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_R,"Ctrl",2.0f,0.5f);
113
114    osg::Vec3 middle(pos.x()+1.0f,0.0f,0.0f);
115   
116    pos.x() = 0.0f;
117    pos.z() += 1.0f;
118
119    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_L,"Shift",2.0f,0.5f);
120    addKey(pos,'\\',"\\",1.0f,1.0f);
121    addKey(pos,'z',"Z",1.0f,1.0f);
122    addKey(pos,'x',"X",1.0f,1.0f);
123    addKey(pos,'c',"C",1.0f,1.0f);
124    addKey(pos,'v',"V",1.0f,1.0f);
125    addKey(pos,'b',"B",1.0f,1.0f);
126    addKey(pos,'n',"N",1.0f,1.0f);
127    addKey(pos,'m',"M",1.0f,1.0f);
128    addKey(pos,',',",",1.0f,1.0f);
129    addKey(pos,'.',".",1.0f,1.0f);
130    addKey(pos,'/',"/",1.0f,1.0f);
131    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_R,"Shift",2.0f,0.5f);
132
133    pos.x() = 0.0f;
134    pos.z() += 1.0f;
135   
136    addKey(pos,osgGA::GUIEventAdapter::KEY_Caps_Lock,"Caps",2.0f,0.5f);
137    addKey(pos,'a',"A",1.0f,1.0f);
138    addKey(pos,'s',"S",1.0f,1.0f);
139    addKey(pos,'d',"D",1.0f,1.0f);
140    addKey(pos,'f',"F",1.0f,1.0f);
141    addKey(pos,'g',"G",1.0f,1.0f);
142    addKey(pos,'h',"H",1.0f,1.0f);
143    addKey(pos,'j',"J",1.0f,1.0f);
144    addKey(pos,'k',"K",1.0f,1.0f);
145    addKey(pos,'l',"L",1.0f,1.0f);
146    addKey(pos,';',";",1.0f,1.0f);
147    addKey(pos,'\'',"'",1.0f,1.0f);
148    addKey(pos,'#',"#",1.0f,1.0f);
149    addKey(pos,osgGA::GUIEventAdapter::KEY_Return,"Return",4.0f,0.5f);
150
151    pos.x() = 0.0f;
152    pos.z() += 1.0f;
153   
154    addKey(pos,osgGA::GUIEventAdapter::KEY_Tab,"Tab",2.0f,0.5f);
155    addKey(pos,'q',"Q",1.0f,1.0f);
156    addKey(pos,'w',"W",1.0f,1.0f);
157    addKey(pos,'e',"E",1.0f,1.0f);
158    addKey(pos,'r',"R",1.0f,1.0f);
159    addKey(pos,'t',"T",1.0f,1.0f);
160    addKey(pos,'y',"Y",1.0f,1.0f);
161    addKey(pos,'u',"U",1.0f,1.0f);
162    addKey(pos,'i',"I",1.0f,1.0f);
163    addKey(pos,'o',"O",1.0f,1.0f);
164    addKey(pos,'p',"P",1.0f,1.0f);
165    addKey(pos,'[',"[",1.0f,1.0f);
166    addKey(pos,']',"]",1.0f,1.0f);
167
168    pos.x() = 0.0f;
169    pos.z() += 1.0f;
170   
171    addKey(pos,'`',"`",1.0f,1.0f);
172    addKey(pos,'1',"1",1.0f,1.0f);
173    addKey(pos,'2',"2",1.0f,1.0f);
174    addKey(pos,'3',"3",1.0f,1.0f);
175    addKey(pos,'4',"4",1.0f,1.0f);
176    addKey(pos,'5',"5",1.0f,1.0f);
177    addKey(pos,'6',"6",1.0f,1.0f);
178    addKey(pos,'7',"7",1.0f,1.0f);
179    addKey(pos,'8',"8",1.0f,1.0f);
180    addKey(pos,'9',"9",1.0f,1.0f);
181    addKey(pos,'0',"0",1.0f,1.0f);
182    addKey(pos,'-',"-",1.0f,1.0f);
183    addKey(pos,'=',"=",1.0f,1.0f);
184    addKey(pos,osgGA::GUIEventAdapter::KEY_BackSpace,"Backspace",3.0f,0.5f);
185
186    pos.x() = 0.0f;
187    pos.z() += 1.0f;
188   
189    float F_height = 0.5f;
190    addKey(pos,osgGA::GUIEventAdapter::KEY_Escape,"Esc",2.0f,F_height);
191    addKey(pos,osgGA::GUIEventAdapter::KEY_F1,"F1",1.0f,F_height);
192    addKey(pos,osgGA::GUIEventAdapter::KEY_F2,"F2",1.0f,F_height);
193    addKey(pos,osgGA::GUIEventAdapter::KEY_F3,"F3",1.0f,F_height);
194    addKey(pos,osgGA::GUIEventAdapter::KEY_F4,"F4",1.0f,F_height);
195    addKey(pos,osgGA::GUIEventAdapter::KEY_F5,"F5",1.0f,F_height);
196    addKey(pos,osgGA::GUIEventAdapter::KEY_F6,"F6",1.0f,F_height);
197    addKey(pos,osgGA::GUIEventAdapter::KEY_F7,"F7",1.0f,F_height);
198    addKey(pos,osgGA::GUIEventAdapter::KEY_F8,"F8",1.0f,F_height);
199    addKey(pos,osgGA::GUIEventAdapter::KEY_F9,"F9",1.0f,F_height);
200    addKey(pos,osgGA::GUIEventAdapter::KEY_F10,"F10",1.0f,F_height);
201    addKey(pos,osgGA::GUIEventAdapter::KEY_F11,"F11",1.0f,F_height);
202    addKey(pos,osgGA::GUIEventAdapter::KEY_F12,"F12",1.0f,F_height);
203
204
205   
206    float cursorMoveHeight=0.5f;
207
208    pos = middle;   
209    addKey(pos,osgGA::GUIEventAdapter::KEY_Left,"Left",2.0f,cursorMoveHeight);
210    osg::Vec3 down = pos;
211    addKey(pos,osgGA::GUIEventAdapter::KEY_Down,"Down",2.0f,cursorMoveHeight);
212    addKey(pos,osgGA::GUIEventAdapter::KEY_Right,"Right",2.0f,cursorMoveHeight);
213   
214    pos = down;
215    pos.z() += 1.0f;
216   
217    addKey(pos,osgGA::GUIEventAdapter::KEY_Up,"Up",2.0f,cursorMoveHeight);
218   
219
220
221}
222
223
224
225
226
227class KeyboardEventHandler : public osgGA::GUIEventHandler
228{
229public:
230   
231        KeyboardEventHandler(KeyboardModel* keyboardModel):
232            _keyboardModel(keyboardModel) {}
233   
234        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
235        {
236            switch(ea.getEventType())
237            {
238                case(osgGA::GUIEventAdapter::KEYDOWN):
239                {
240                    _keyboardModel->keyChange(ea.getKey(),1);
241                    return true;
242                }
243                case(osgGA::GUIEventAdapter::KEYUP):
244                {
245                    _keyboardModel->keyChange(ea.getKey(),0);
246                    return true;
247                }
248
249                default:
250                    return false;
251            }
252        }
253
254        virtual void accept(osgGA::GUIEventHandlerVisitor& v)
255        {
256            v.visit(*this);
257        }
258       
259        osg::ref_ptr<KeyboardModel> _keyboardModel;
260       
261};
262
263int main( int argc, char **argv )
264{
265
266    // use an ArgumentParser object to manage the program arguments.
267    osg::ArgumentParser arguments(&argc,argv);
268
269    // set up the usage document, in case we need to print out how to use this program.
270    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
271    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
272    arguments.getApplicationUsage()->addCommandLineOption("-c","Mannually create occluders");
273   
274    // initialize the viewer.
275    osgProducer::Viewer viewer(arguments);
276
277    // set up the value with sensible default event handlers.
278    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
279
280
281    osg::ref_ptr<KeyboardModel> keyboardModel = new KeyboardModel;
282
283    KeyboardEventHandler* keh = new KeyboardEventHandler(keyboardModel.get());
284    viewer.getEventHandlerList().push_front(keh);
285
286    // get details on keyboard and mouse bindings used by the viewer.
287    viewer.getUsage(*arguments.getApplicationUsage());
288
289    // if user request help write it out to cout.
290    if (arguments.read("-h") || arguments.read("--help"))
291    {
292        arguments.getApplicationUsage()->write(std::cout);
293        return 1;
294    }
295
296    // any option left unread are converted into errors to write out later.
297    arguments.reportRemainingOptionsAsUnrecognized();
298
299    // report any errors if they have occured when parsing the program aguments.
300    if (arguments.errors())
301    {
302        arguments.writeErrorMessages(std::cout);
303        return 1;
304    }
305
306     
307    // attach the scene graph.
308    viewer.setSceneData( keyboardModel->getScene() );
309
310    osgDB::writeNodeFile(*keyboardModel->getScene(),"test.osg");
311
312
313    // create the windows and run the threads.
314    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
315
316
317    while( !viewer.done() )
318    {
319        // wait for all cull and draw threads to complete.
320        viewer.sync();
321
322        // update the scene by traversing it with the the update visitor which will
323        // call all node update callbacks and animations.
324        viewer.update();
325         
326        // fire off the cull and draw traversals of the scene.
327        viewer.frame();
328       
329    }
330
331    //viewer.sync();
332
333    return 0;
334}
Note: See TracBrowser for help on using the browser.