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

Revision 6941, 13.6 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

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