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

Revision 5927, 12.6 kB (checked in by robert, 7 years ago)

Ported following examples to osgViewer:

osggeodemo
osggeometry
osghud
osgimpostor
osgkeyboard
osglauncher
osglight
osglightpoint
osglogicop
osglogo
osgmovie
osgmultiplecameras
osgmultitexture
osgoccluder
osgparametric
osgparticle

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgViewer/Viewer>
2#include <osg/io_utils>
3
4#include <osg/MatrixTransform>
5#include <osg/Geode>
6#include <osg/Group>
7#include <osg/Switch>
8#include <osg/Notify>
9#include <osg/Geometry>
10
11#include <osgText/Text>
12
13#include <osgDB/Registry>
14#include <osgDB/ReadFile>
15#include <osgDB/WriteFile>
16
17
18class KeyboardModel : public osg::Referenced
19{
20public:
21
22        KeyboardModel() { createKeyboard(); }
23       
24        osg::Group* getScene() { return _scene.get(); }
25       
26        void keyChange(int key,int value);
27       
28protected:
29       
30        ~KeyboardModel() {}
31
32        osg::Switch* addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height);
33        osg::Switch* addKey(int key,osg::Switch* sw);
34
35        void createKeyboard();
36
37        typedef std::map<int, osg::ref_ptr<osg::Switch> > KeyModelMap;
38       
39        osg::ref_ptr<osg::Group>    _scene;
40        KeyModelMap                 _keyModelMap;
41        osg::ref_ptr<osgText::Text> _inputText;
42
43};
44
45void KeyboardModel::keyChange(int key,int value)
46{
47    osg::notify(osg::INFO) << "key value change, code="<<std::hex << key << "\t value="<< value << std::dec  << std::endl;
48
49    // toggle the keys graphical representation on or off via osg::Swithc
50    KeyModelMap::iterator itr = _keyModelMap.find(key);
51    if (itr!=_keyModelMap.end())
52    {
53        itr->second->setSingleChildOn(value);
54    }
55   
56    if (value)
57    {
58        // when a key is pressed add the new data to the text field
59       
60        if (key>0 && key<256)
61        {
62            // just add ascii characters right now...
63            _inputText->getText().push_back(key);
64            _inputText->update();
65        }
66        else if (key==osgGA::GUIEventAdapter::KEY_Return)
67        {
68            _inputText->getText().push_back('\n');
69            _inputText->update();
70        }
71        else if (key==osgGA::GUIEventAdapter::KEY_BackSpace || key==osgGA::GUIEventAdapter::KEY_Delete)
72        {
73            if (!_inputText->getText().empty())
74            {
75                _inputText->getText().pop_back();
76                _inputText->update();
77            }
78        }
79       
80    }
81   
82}
83
84osg::Switch* KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height)
85{
86
87    osg::Geode* geodeUp = new osg::Geode;
88    {
89        osgText::Text* textUp = new osgText::Text;
90        textUp->setFont("fonts/arial.ttf");
91        textUp->setColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
92        textUp->setCharacterSize(height);
93        textUp->setPosition(pos);
94        textUp->setDrawMode(osgText::Text::TEXT/*|osgText::Text::BOUNDINGBOX*/);
95        textUp->setAlignment(osgText::Text::LEFT_CENTER);
96        textUp->setAxisAlignment(osgText::Text::XZ_PLANE);
97        textUp->setText(text);
98       
99        geodeUp->addDrawable(textUp);
100    }
101   
102    osg::Geode* geodeDown = new osg::Geode;
103    {
104        osgText::Text* textDown = new osgText::Text;
105        textDown->setFont("fonts/arial.ttf");
106        textDown->setColor(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
107        textDown->setCharacterSize(height);
108        textDown->setPosition(pos);
109        textDown->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
110        textDown->setAlignment(osgText::Text::LEFT_CENTER);
111        textDown->setAxisAlignment(osgText::Text::XZ_PLANE);
112        textDown->setText(text);
113       
114        geodeDown->addDrawable(textDown);
115    }
116
117    osg::Switch* model = new osg::Switch;
118    model->addChild(geodeUp,true);
119    model->addChild(geodeDown,false);
120   
121    _scene->addChild(model);
122
123    _keyModelMap[key] = model;
124   
125    pos.x() += width;
126   
127    return model;
128   
129}
130
131osg::Switch* KeyboardModel::addKey(int key,osg::Switch* sw)
132{
133    _keyModelMap[key] = sw;
134    return sw;
135}
136
137void KeyboardModel::createKeyboard()
138{
139    _scene = new osg::Group;
140   
141    osg::Vec3 origin(0.0f,0.0f,0.0f);
142    osg::Vec3 pos=origin;
143   
144    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_L,"Ctrl",2.0f,0.5f);
145    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_L,"Super",2.0f,0.5f);
146    addKey(pos,osgGA::GUIEventAdapter::KEY_Alt_L,"Alt",2.0f,0.5f);
147    addKey(pos,osgGA::GUIEventAdapter::KEY_Space,"Space",3.0f,1.0f);
148    addKey(pos,osgGA::GUIEventAdapter::KEY_Mode_switch,"Switch",2.0f,0.5f);
149    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_R,"Super",2.0f,0.5f);
150    addKey(pos,osgGA::GUIEventAdapter::KEY_Menu,"Menu",2.0f,0.5f);
151    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_R,"Ctrl",2.0f,0.5f);
152
153    osg::Vec3 middle(pos.x()+1.0f,0.0f,0.0f);
154   
155    pos.x() = 0.0f;
156    pos.z() += 1.0f;
157
158    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_L,"Shift",2.0f,0.5f);
159    addKey(pos,'\\',"\\",1.0f,1.0f);
160    addKey('Z',addKey(pos,'z',"Z",1.0f,1.0f));
161    addKey('X',addKey(pos,'x',"X",1.0f,1.0f));
162    addKey('C',addKey(pos,'c',"C",1.0f,1.0f));
163    addKey('V',addKey(pos,'v',"V",1.0f,1.0f));
164    addKey('B',addKey(pos,'b',"B",1.0f,1.0f));
165    addKey('N',addKey(pos,'n',"N",1.0f,1.0f));
166    addKey('M',addKey(pos,'m',"M",1.0f,1.0f));
167    addKey('<',addKey(pos,',',",",1.0f,1.0f));
168    addKey('>',addKey(pos,'.',".",1.0f,1.0f));
169    addKey('?',addKey(pos,'/',"/",1.0f,1.0f));
170    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_R,"Shift",2.0f,0.5f);
171
172    pos.x() = 0.0f;
173    pos.z() += 1.0f;
174   
175    addKey(pos,osgGA::GUIEventAdapter::KEY_Caps_Lock,"Caps",2.0f,0.5f);
176    addKey('A',addKey(pos,'a',"A",1.0f,1.0f));
177    addKey('S',addKey(pos,'s',"S",1.0f,1.0f));
178    addKey('D',addKey(pos,'d',"D",1.0f,1.0f));
179    addKey('F',addKey(pos,'f',"F",1.0f,1.0f));
180    addKey('G',addKey(pos,'g',"G",1.0f,1.0f));
181    addKey('H',addKey(pos,'h',"H",1.0f,1.0f));
182    addKey('J',addKey(pos,'j',"J",1.0f,1.0f));
183    addKey('K',addKey(pos,'k',"K",1.0f,1.0f));
184    addKey('L',addKey(pos,'l',"L",1.0f,1.0f));
185    addKey(':',addKey(pos,';',";",1.0f,1.0f));
186    addKey('@',addKey(pos,'\'',"'",1.0f,1.0f));
187    addKey('~',addKey(pos,'#',"#",1.0f,1.0f));
188    addKey(pos,osgGA::GUIEventAdapter::KEY_Return,"Return",4.0f,0.5f);
189
190    pos.x() = 0.0f;
191    pos.z() += 1.0f;
192   
193    addKey(pos,osgGA::GUIEventAdapter::KEY_Tab,"Tab",2.0f,0.5f);
194    addKey('Q',addKey(pos,'q',"Q",1.0f,1.0f));
195    addKey('W',addKey(pos,'w',"W",1.0f,1.0f));
196    addKey('E',addKey(pos,'e',"E",1.0f,1.0f));
197    addKey('R',addKey(pos,'r',"R",1.0f,1.0f));
198    addKey('T',addKey(pos,'t',"T",1.0f,1.0f));
199    addKey('Y',addKey(pos,'y',"Y",1.0f,1.0f));
200    addKey('U',addKey(pos,'u',"U",1.0f,1.0f));
201    addKey('I',addKey(pos,'i',"I",1.0f,1.0f));
202    addKey('O',addKey(pos,'o',"O",1.0f,1.0f));
203    addKey('P',addKey(pos,'p',"P",1.0f,1.0f));
204    addKey('{',addKey(pos,'[',"[",1.0f,1.0f));
205    addKey('}',addKey(pos,']',"]",1.0f,1.0f));
206
207    pos.x() = 0.0f;
208    pos.z() += 1.0f;
209   
210    addKey(pos,'`',"`",1.0f,1.0f);
211    addKey(pos,'1',"1",1.0f,1.0f);
212    addKey(pos,'2',"2",1.0f,1.0f);
213    addKey(pos,'3',"3",1.0f,1.0f);
214    addKey(pos,'4',"4",1.0f,1.0f);
215    addKey(pos,'5',"5",1.0f,1.0f);
216    addKey(pos,'6',"6",1.0f,1.0f);
217    addKey(pos,'7',"7",1.0f,1.0f);
218    addKey(pos,'8',"8",1.0f,1.0f);
219    addKey(pos,'9',"9",1.0f,1.0f);
220    addKey(pos,'0',"0",1.0f,1.0f);
221    addKey(pos,'-',"-",1.0f,1.0f);
222    addKey(pos,'=',"=",1.0f,1.0f);
223    addKey(pos,osgGA::GUIEventAdapter::KEY_BackSpace,"Backspace",3.0f,0.5f);
224
225    pos.x() = 0.0f;
226    pos.z() += 1.5f;
227   
228    float F_height = 0.5f;
229    addKey(pos,osgGA::GUIEventAdapter::KEY_Escape,"Esc",2.0f,F_height);
230    addKey(pos,osgGA::GUIEventAdapter::KEY_F1,"F1",1.0f,F_height);
231    addKey(pos,osgGA::GUIEventAdapter::KEY_F2,"F2",1.0f,F_height);
232    addKey(pos,osgGA::GUIEventAdapter::KEY_F3,"F3",1.0f,F_height);
233    addKey(pos,osgGA::GUIEventAdapter::KEY_F4,"F4",1.0f,F_height);
234    addKey(pos,osgGA::GUIEventAdapter::KEY_F5,"F5",1.0f,F_height);
235    addKey(pos,osgGA::GUIEventAdapter::KEY_F6,"F6",1.0f,F_height);
236    addKey(pos,osgGA::GUIEventAdapter::KEY_F7,"F7",1.0f,F_height);
237    addKey(pos,osgGA::GUIEventAdapter::KEY_F8,"F8",1.0f,F_height);
238    addKey(pos,osgGA::GUIEventAdapter::KEY_F9,"F9",1.0f,F_height);
239    addKey(pos,osgGA::GUIEventAdapter::KEY_F10,"F10",1.0f,F_height);
240    addKey(pos,osgGA::GUIEventAdapter::KEY_F11,"F11",1.0f,F_height);
241    addKey(pos,osgGA::GUIEventAdapter::KEY_F12,"F12",1.0f,F_height);
242
243
244   
245    float cursorMoveHeight=0.35f;
246
247    pos = middle;   
248    addKey(pos,osgGA::GUIEventAdapter::KEY_Left,"Left",1.0f,cursorMoveHeight);
249    osg::Vec3 down = pos;
250    addKey(pos,osgGA::GUIEventAdapter::KEY_Down,"Down",1.0f,cursorMoveHeight);
251    addKey(pos,osgGA::GUIEventAdapter::KEY_Right,"Right",1.0f,cursorMoveHeight);
252   
253    osg::Vec3 keypad = pos;
254    keypad.x()+=1.0f;
255
256    pos = down;
257    pos.z() += 1.0f;
258   
259    addKey(pos,osgGA::GUIEventAdapter::KEY_Up,"Up",1.0f,cursorMoveHeight);
260   
261
262    float homeHeight = 0.35f;
263    pos = middle;
264    pos.z() += 3.0;   
265    addKey(pos,osgGA::GUIEventAdapter::KEY_Delete,"Delete",1.0f,homeHeight);
266    addKey(pos,osgGA::GUIEventAdapter::KEY_End,"End",1.0f,homeHeight);
267    addKey(pos,osgGA::GUIEventAdapter::KEY_Page_Down,"Page\nDown",1.0f,homeHeight);
268   
269    pos = middle;
270    pos.z() += 4.0;   
271    addKey(pos,osgGA::GUIEventAdapter::KEY_Insert,"Insert",1.0f,homeHeight);
272    addKey(pos,osgGA::GUIEventAdapter::KEY_Home,"Home",1.0f,homeHeight);
273    addKey(pos,osgGA::GUIEventAdapter::KEY_Page_Up,"Page\nUp",1.0f,homeHeight);
274
275    pos = middle;
276    pos.z() += 5.5;   
277    addKey(pos,osgGA::GUIEventAdapter::KEY_Print,"PrtScrn\nSysRq",1.0f,homeHeight);
278    addKey(pos,osgGA::GUIEventAdapter::KEY_Scroll_Lock,"ScrLk",1.0f,homeHeight);
279    addKey(pos,osgGA::GUIEventAdapter::KEY_Pause,"Pause\nBreak",1.0f,homeHeight);
280   
281   
282
283    pos = keypad;
284    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Insert,"0",2.0f,1.0f);
285    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Delete,".",1.0f,1.0f);
286    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Enter,"Enter",1.0f,homeHeight);
287   
288    pos = keypad;
289    pos.z() += 1.0f;
290    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_End,"1",1.0f,1.0f);
291    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Down,"2",1.0f,1.0f);
292    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Page_Down,"3",1.0f,1.0f);
293
294    pos = keypad;
295    pos.z() += 2.0f;
296    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Left,"4",1.0f,1.0f);
297    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Begin,"5",1.0f,1.0f);
298    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Right,"6",1.0f,1.0f);
299    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Add,"+",1.0f,1.0f);
300
301    pos = keypad;
302    pos.z() += 3.0f;
303    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Home,"7",1.0f,1.0f);
304    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Up,"8",1.0f,1.0f);
305    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Page_Up,"9",1.0f,1.0f);
306
307    pos = keypad;
308    pos.z() += 4.0f;
309    addKey(pos,osgGA::GUIEventAdapter::KEY_Num_Lock,"Num\nLock",1.0f,0.3f);
310    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Divide,"/",1.0f,1.0f);
311    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Multiply,"*",1.0f,1.0f);
312    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Subtract,"-",1.0f,1.0f);
313   
314    float totalWidth = pos.x()-origin.x();
315    pos = origin;
316    pos.z() += -1.5f;
317
318    osg::Geode* geodeInput = new osg::Geode;
319    {
320        _inputText = new osgText::Text;
321        _inputText->setFont("fonts/arial.ttf");
322        _inputText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
323        _inputText->setCharacterSize(1.0f);
324        _inputText->setMaximumWidth(totalWidth);
325        _inputText->setPosition(pos);
326        _inputText->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
327        _inputText->setAlignment(osgText::Text::BASE_LINE);
328        _inputText->setAxisAlignment(osgText::Text::XZ_PLANE);
329        _inputText->setText("Press some keys...");
330       
331        geodeInput->addDrawable(_inputText.get());
332       
333        _scene->addChild(geodeInput);
334    }
335
336}
337
338
339
340
341
342class KeyboardEventHandler : public osgGA::GUIEventHandler
343{
344public:
345   
346        KeyboardEventHandler(KeyboardModel* keyboardModel):
347            _keyboardModel(keyboardModel) {}
348   
349        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
350        {
351            switch(ea.getEventType())
352            {
353                case(osgGA::GUIEventAdapter::KEYDOWN):
354                {
355                    _keyboardModel->keyChange(ea.getKey(),1);
356                    return true;
357                }
358                case(osgGA::GUIEventAdapter::KEYUP):
359                {
360                    _keyboardModel->keyChange(ea.getKey(),0);
361                    return true;
362                }
363
364                default:
365                    return false;
366            }
367        }
368       
369        osg::ref_ptr<KeyboardModel> _keyboardModel;
370       
371};
372
373int main(int , char **)
374{
375    osgViewer::Viewer viewer;
376
377    osg::ref_ptr<KeyboardModel> keyboardModel = new KeyboardModel;
378
379    viewer.addEventHandler(new KeyboardEventHandler(keyboardModel.get()));
380    viewer.setSceneData( keyboardModel->getScene() );
381
382    return viewer.run();
383}
Note: See TracBrowser for help on using the browser.