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

Revision 1748, 12.4 kB (checked in by robert, 12 years ago)

Added futher keys to complete the keyboard.

  • 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
53void KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height)
54{
55
56    osg::Geode* geodeUp = new osg::Geode;
57    {
58        osgText::Text* textUp = new osgText::Text;
59        textUp->setFont("fonts/arial.ttf");
60        textUp->setColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
61        textUp->setCharacterSize(height);
62        textUp->setPosition(pos);
63        textUp->setDrawMode(osgText::Text::TEXT/*|osgText::Text::BOUNDINGBOX*/);
64        textUp->setAlignment(osgText::Text::LEFT_CENTER);
65        textUp->setAxisAlignment(osgText::Text::XZ_PLANE);
66        textUp->setText(text);
67       
68        geodeUp->addDrawable(textUp);
69    }
70   
71    osg::Geode* geodeDown = new osg::Geode;
72    {
73        osgText::Text* textDown = new osgText::Text;
74        textDown->setFont("fonts/arial.ttf");
75        textDown->setColor(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
76        textDown->setCharacterSize(height);
77        textDown->setPosition(pos);
78        textDown->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
79        textDown->setAlignment(osgText::Text::LEFT_CENTER);
80        textDown->setAxisAlignment(osgText::Text::XZ_PLANE);
81        textDown->setText(text);
82       
83        geodeDown->addDrawable(textDown);
84    }
85
86    osg::Switch* model = new osg::Switch;
87    model->addChild(geodeUp,true);
88    model->addChild(geodeDown,false);
89   
90    _scene->addChild(model);
91
92    _keyModelMap[key] = model;
93   
94    pos.x() += width;
95   
96}
97
98void KeyboardModel::createKeyboard()
99{
100    _scene = new osg::Group;
101   
102    osg::Vec3 pos(0.0f,0.0f,0.0f);
103   
104    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_L,"Ctrl",2.0f,0.5f);
105    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_L,"Super",2.0f,0.5f);
106    addKey(pos,osgGA::GUIEventAdapter::KEY_Alt_L,"Alt",2.0f,0.5f);
107    addKey(pos,osgGA::GUIEventAdapter::KEY_Space,"Space",3.0f,1.0f);
108    addKey(pos,osgGA::GUIEventAdapter::KEY_Mode_switch,"Switch",2.0f,0.5f);
109    addKey(pos,osgGA::GUIEventAdapter::KEY_Super_R,"Super",2.0f,0.5f);
110    addKey(pos,osgGA::GUIEventAdapter::KEY_Menu,"Menu",2.0f,0.5f);
111    addKey(pos,osgGA::GUIEventAdapter::KEY_Control_R,"Ctrl",2.0f,0.5f);
112
113    osg::Vec3 middle(pos.x()+1.0f,0.0f,0.0f);
114   
115    pos.x() = 0.0f;
116    pos.z() += 1.0f;
117
118    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_L,"Shift",2.0f,0.5f);
119    addKey(pos,'\\',"\\",1.0f,1.0f);
120    addKey(pos,'z',"Z",1.0f,1.0f);
121    addKey(pos,'x',"X",1.0f,1.0f);
122    addKey(pos,'c',"C",1.0f,1.0f);
123    addKey(pos,'v',"V",1.0f,1.0f);
124    addKey(pos,'b',"B",1.0f,1.0f);
125    addKey(pos,'n',"N",1.0f,1.0f);
126    addKey(pos,'m',"M",1.0f,1.0f);
127    addKey(pos,',',",",1.0f,1.0f);
128    addKey(pos,'.',".",1.0f,1.0f);
129    addKey(pos,'/',"/",1.0f,1.0f);
130    addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_R,"Shift",2.0f,0.5f);
131
132    pos.x() = 0.0f;
133    pos.z() += 1.0f;
134   
135    addKey(pos,osgGA::GUIEventAdapter::KEY_Caps_Lock,"Caps",2.0f,0.5f);
136    addKey(pos,'a',"A",1.0f,1.0f);
137    addKey(pos,'s',"S",1.0f,1.0f);
138    addKey(pos,'d',"D",1.0f,1.0f);
139    addKey(pos,'f',"F",1.0f,1.0f);
140    addKey(pos,'g',"G",1.0f,1.0f);
141    addKey(pos,'h',"H",1.0f,1.0f);
142    addKey(pos,'j',"J",1.0f,1.0f);
143    addKey(pos,'k',"K",1.0f,1.0f);
144    addKey(pos,'l',"L",1.0f,1.0f);
145    addKey(pos,';',";",1.0f,1.0f);
146    addKey(pos,'\'',"'",1.0f,1.0f);
147    addKey(pos,'#',"#",1.0f,1.0f);
148    addKey(pos,osgGA::GUIEventAdapter::KEY_Return,"Return",4.0f,0.5f);
149
150    pos.x() = 0.0f;
151    pos.z() += 1.0f;
152   
153    addKey(pos,osgGA::GUIEventAdapter::KEY_Tab,"Tab",2.0f,0.5f);
154    addKey(pos,'q',"Q",1.0f,1.0f);
155    addKey(pos,'w',"W",1.0f,1.0f);
156    addKey(pos,'e',"E",1.0f,1.0f);
157    addKey(pos,'r',"R",1.0f,1.0f);
158    addKey(pos,'t',"T",1.0f,1.0f);
159    addKey(pos,'y',"Y",1.0f,1.0f);
160    addKey(pos,'u',"U",1.0f,1.0f);
161    addKey(pos,'i',"I",1.0f,1.0f);
162    addKey(pos,'o',"O",1.0f,1.0f);
163    addKey(pos,'p',"P",1.0f,1.0f);
164    addKey(pos,'[',"[",1.0f,1.0f);
165    addKey(pos,']',"]",1.0f,1.0f);
166
167    pos.x() = 0.0f;
168    pos.z() += 1.0f;
169   
170    addKey(pos,'`',"`",1.0f,1.0f);
171    addKey(pos,'1',"1",1.0f,1.0f);
172    addKey(pos,'2',"2",1.0f,1.0f);
173    addKey(pos,'3',"3",1.0f,1.0f);
174    addKey(pos,'4',"4",1.0f,1.0f);
175    addKey(pos,'5',"5",1.0f,1.0f);
176    addKey(pos,'6',"6",1.0f,1.0f);
177    addKey(pos,'7',"7",1.0f,1.0f);
178    addKey(pos,'8',"8",1.0f,1.0f);
179    addKey(pos,'9',"9",1.0f,1.0f);
180    addKey(pos,'0',"0",1.0f,1.0f);
181    addKey(pos,'-',"-",1.0f,1.0f);
182    addKey(pos,'=',"=",1.0f,1.0f);
183    addKey(pos,osgGA::GUIEventAdapter::KEY_BackSpace,"Backspace",3.0f,0.5f);
184
185    pos.x() = 0.0f;
186    pos.z() += 1.5f;
187   
188    float F_height = 0.5f;
189    addKey(pos,osgGA::GUIEventAdapter::KEY_Escape,"Esc",2.0f,F_height);
190    addKey(pos,osgGA::GUIEventAdapter::KEY_F1,"F1",1.0f,F_height);
191    addKey(pos,osgGA::GUIEventAdapter::KEY_F2,"F2",1.0f,F_height);
192    addKey(pos,osgGA::GUIEventAdapter::KEY_F3,"F3",1.0f,F_height);
193    addKey(pos,osgGA::GUIEventAdapter::KEY_F4,"F4",1.0f,F_height);
194    addKey(pos,osgGA::GUIEventAdapter::KEY_F5,"F5",1.0f,F_height);
195    addKey(pos,osgGA::GUIEventAdapter::KEY_F6,"F6",1.0f,F_height);
196    addKey(pos,osgGA::GUIEventAdapter::KEY_F7,"F7",1.0f,F_height);
197    addKey(pos,osgGA::GUIEventAdapter::KEY_F8,"F8",1.0f,F_height);
198    addKey(pos,osgGA::GUIEventAdapter::KEY_F9,"F9",1.0f,F_height);
199    addKey(pos,osgGA::GUIEventAdapter::KEY_F10,"F10",1.0f,F_height);
200    addKey(pos,osgGA::GUIEventAdapter::KEY_F11,"F11",1.0f,F_height);
201    addKey(pos,osgGA::GUIEventAdapter::KEY_F12,"F12",1.0f,F_height);
202
203
204   
205    float cursorMoveHeight=0.35f;
206
207    pos = middle;   
208    addKey(pos,osgGA::GUIEventAdapter::KEY_Left,"Left",1.0f,cursorMoveHeight);
209    osg::Vec3 down = pos;
210    addKey(pos,osgGA::GUIEventAdapter::KEY_Down,"Down",1.0f,cursorMoveHeight);
211    addKey(pos,osgGA::GUIEventAdapter::KEY_Right,"Right",1.0f,cursorMoveHeight);
212   
213    osg::Vec3 keypad = pos;
214    keypad.x()+=1.0f;
215
216    pos = down;
217    pos.z() += 1.0f;
218   
219    addKey(pos,osgGA::GUIEventAdapter::KEY_Up,"Up",1.0f,cursorMoveHeight);
220   
221
222    float homeHeight = 0.35f;
223    pos = middle;
224    pos.z() += 3.0;   
225    addKey(pos,osgGA::GUIEventAdapter::KEY_Delete,"Delete",1.0f,homeHeight);
226    addKey(pos,osgGA::GUIEventAdapter::KEY_End,"End",1.0f,homeHeight);
227    addKey(pos,osgGA::GUIEventAdapter::KEY_Page_Down,"Page\nDown",1.0f,homeHeight);
228   
229    pos = middle;
230    pos.z() += 4.0;   
231    addKey(pos,osgGA::GUIEventAdapter::KEY_Insert,"Insert",1.0f,homeHeight);
232    addKey(pos,osgGA::GUIEventAdapter::KEY_Home,"Home",1.0f,homeHeight);
233    addKey(pos,osgGA::GUIEventAdapter::KEY_Page_Up,"Page\nUp",1.0f,homeHeight);
234
235    pos = middle;
236    pos.z() += 5.5;   
237    addKey(pos,osgGA::GUIEventAdapter::KEY_Print,"PrtScrn\nSysRq",1.0f,homeHeight);
238    addKey(pos,osgGA::GUIEventAdapter::KEY_Scroll_Lock,"ScrLk",1.0f,homeHeight);
239    addKey(pos,osgGA::GUIEventAdapter::KEY_Pause,"Pause\nBreak",1.0f,homeHeight);
240   
241   
242
243    pos = keypad;
244    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Insert,"0",2.0f,1.0f);
245    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Delete,".",1.0f,1.0f);
246    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Enter,"Enter",1.0f,homeHeight);
247   
248    pos = keypad;
249    pos.z() += 1.0f;
250    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_End,"1",1.0f,1.0f);
251    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Down,"2",1.0f,1.0f);
252    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Page_Down,"3",1.0f,1.0f);
253
254    pos = keypad;
255    pos.z() += 2.0f;
256    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Left,"4",1.0f,1.0f);
257    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Begin,"5",1.0f,1.0f);
258    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Right,"6",1.0f,1.0f);
259    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Add,"+",1.0f,1.0f);
260
261    pos = keypad;
262    pos.z() += 3.0f;
263    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Home,"7",1.0f,1.0f);
264    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Up,"8",1.0f,1.0f);
265    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Page_Up,"9",1.0f,1.0f);
266
267    pos = keypad;
268    pos.z() += 4.0f;
269    addKey(pos,osgGA::GUIEventAdapter::KEY_Num_Lock,"Num\nLock",1.0f,0.3f);
270    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Divide,"/",1.0f,1.0f);
271    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Multiply,"*",1.0f,1.0f);
272    addKey(pos,osgGA::GUIEventAdapter::KEY_KP_Subtract,"-",1.0f,1.0f);
273
274}
275
276
277
278
279
280class KeyboardEventHandler : public osgGA::GUIEventHandler
281{
282public:
283   
284        KeyboardEventHandler(KeyboardModel* keyboardModel):
285            _keyboardModel(keyboardModel) {}
286   
287        virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
288        {
289            switch(ea.getEventType())
290            {
291                case(osgGA::GUIEventAdapter::KEYDOWN):
292                {
293                    _keyboardModel->keyChange(ea.getKey(),1);
294                    return true;
295                }
296                case(osgGA::GUIEventAdapter::KEYUP):
297                {
298                    _keyboardModel->keyChange(ea.getKey(),0);
299                    return true;
300                }
301
302                default:
303                    return false;
304            }
305        }
306
307        virtual void accept(osgGA::GUIEventHandlerVisitor& v)
308        {
309            v.visit(*this);
310        }
311       
312        osg::ref_ptr<KeyboardModel> _keyboardModel;
313       
314};
315
316int main( int argc, char **argv )
317{
318
319    // use an ArgumentParser object to manage the program arguments.
320    osg::ArgumentParser arguments(&argc,argv);
321
322    // set up the usage document, in case we need to print out how to use this program.
323    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
324    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
325    arguments.getApplicationUsage()->addCommandLineOption("-c","Mannually create occluders");
326   
327    // initialize the viewer.
328    osgProducer::Viewer viewer(arguments);
329
330    // set up the value with sensible default event handlers.
331    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
332
333
334    osg::ref_ptr<KeyboardModel> keyboardModel = new KeyboardModel;
335
336    KeyboardEventHandler* keh = new KeyboardEventHandler(keyboardModel.get());
337    viewer.getEventHandlerList().push_front(keh);
338
339    // get details on keyboard and mouse bindings used by the viewer.
340    viewer.getUsage(*arguments.getApplicationUsage());
341
342    // if user request help write it out to cout.
343    if (arguments.read("-h") || arguments.read("--help"))
344    {
345        arguments.getApplicationUsage()->write(std::cout);
346        return 1;
347    }
348
349    // any option left unread are converted into errors to write out later.
350    arguments.reportRemainingOptionsAsUnrecognized();
351
352    // report any errors if they have occured when parsing the program aguments.
353    if (arguments.errors())
354    {
355        arguments.writeErrorMessages(std::cout);
356        return 1;
357    }
358
359     
360    // attach the scene graph.
361    viewer.setSceneData( keyboardModel->getScene() );
362
363    osgDB::writeNodeFile(*keyboardModel->getScene(),"test.osg");
364
365
366    // create the windows and run the threads.
367    viewer.realize(Producer::CameraGroup::ThreadPerCamera);
368
369
370    while( !viewer.done() )
371    {
372        // wait for all cull and draw threads to complete.
373        viewer.sync();
374
375        // update the scene by traversing it with the the update visitor which will
376        // call all node update callbacks and animations.
377        viewer.update();
378         
379        // fire off the cull and draw traversals of the scene.
380        viewer.frame();
381       
382    }
383   
384    // wait for all cull and draw threads to complete before exit.
385    viewer.sync();
386
387    return 0;
388}
Note: See TracBrowser for help on using the browser.