#!/bin/env python # Copyright (C) 2002-2003 Gideon May (gideon@computer.org) # # Permission to copy, use, sell and distribute this software is granted # provided this copyright notice appears in all copies. # Permission to modify the code and to distribute modified code is granted # provided this copyright notice appears in all copies, and a notice # that the code was modified is included with the copyright notice. # # This software is provided "as is" without express or implied warranty, # and with no claim as to its suitability for any purpose. # -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield # # This application is open source and may be redistributed and/or modified # freely and without restriction, both in commericial and non commericial applications, # as long as this copyright notice is maintained. # # This application is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # from PyOSG import Producer from PyOSG import osg from PyOSG import osgDB from PyOSG import osgGA from PyOSG import osgUtil from PyOSG import osgProducer from PyOSG import osgText import sys class KeyboardModel: def __init__(self): self._scene = None self._keyModelMap = {} self._inputText = None self.createKeyboard() def getScene(self): return self._scene def keyChange(self, key, value): #-- osg.notify(osg.INFO) << "key value change, code="<',self.addKey(pos,'.',".",1.0,1.0)) self.addKey_2('?',self.addKey(pos,'/',"/",1.0,1.0)) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Shift_R,"Shift",2.0,0.5) pos._x = 0.0 pos._z += 1.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Caps_Lock,"Caps",2.0,0.5) self.addKey_2('A',self.addKey(pos,'a',"A",1.0,1.0)) self.addKey_2('S',self.addKey(pos,'s',"S",1.0,1.0)) self.addKey_2('D',self.addKey(pos,'d',"D",1.0,1.0)) self.addKey_2('F',self.addKey(pos,'f',"F",1.0,1.0)) self.addKey_2('G',self.addKey(pos,'g',"G",1.0,1.0)) self.addKey_2('H',self.addKey(pos,'h',"H",1.0,1.0)) self.addKey_2('J',self.addKey(pos,'j',"J",1.0,1.0)) self.addKey_2('K',self.addKey(pos,'k',"K",1.0,1.0)) self.addKey_2('L',self.addKey(pos,'l',"L",1.0,1.0)) self.addKey_2(':',self.addKey(pos,';',";",1.0,1.0)) self.addKey_2('@',self.addKey(pos,'\'',"'",1.0,1.0)) self.addKey_2('~',self.addKey(pos,'#',"#",1.0,1.0)) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Return,"Return",4.0,0.5) pos._x = 0.0 pos._z += 1.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Tab,"Tab",2.0,0.5) self.addKey_2('Q',self.addKey(pos,'q',"Q",1.0,1.0)) self.addKey_2('W',self.addKey(pos,'w',"W",1.0,1.0)) self.addKey_2('E',self.addKey(pos,'e',"E",1.0,1.0)) self.addKey_2('R',self.addKey(pos,'r',"R",1.0,1.0)) self.addKey_2('T',self.addKey(pos,'t',"T",1.0,1.0)) self.addKey_2('Y',self.addKey(pos,'y',"Y",1.0,1.0)) self.addKey_2('U',self.addKey(pos,'u',"U",1.0,1.0)) self.addKey_2('I',self.addKey(pos,'i',"I",1.0,1.0)) self.addKey_2('O',self.addKey(pos,'o',"O",1.0,1.0)) self.addKey_2('P',self.addKey(pos,'p',"P",1.0,1.0)) self.addKey_2('{',self.addKey(pos,'[',"[",1.0,1.0)) self.addKey_2('}',self.addKey(pos,']',"]",1.0,1.0)) pos._x = 0.0 pos._z += 1.0 self.addKey(pos,'`',"`",1.0,1.0) self.addKey(pos,'1',"1",1.0,1.0) self.addKey(pos,'2',"2",1.0,1.0) self.addKey(pos,'3',"3",1.0,1.0) self.addKey(pos,'4',"4",1.0,1.0) self.addKey(pos,'5',"5",1.0,1.0) self.addKey(pos,'6',"6",1.0,1.0) self.addKey(pos,'7',"7",1.0,1.0) self.addKey(pos,'8',"8",1.0,1.0) self.addKey(pos,'9',"9",1.0,1.0) self.addKey(pos,'0',"0",1.0,1.0) self.addKey(pos,'-',"-",1.0,1.0) self.addKey(pos,'=',"=",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_BackSpace,"Backspace",3.0,0.5) pos._x = 0.0 pos._z += 1.5 F_height = 0.5 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Escape,"Esc",2.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F1,"F1",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F2,"F2",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F3,"F3",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F4,"F4",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F5,"F5",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F6,"F6",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F7,"F7",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F8,"F8",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F9,"F9",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F10,"F10",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F11,"F11",1.0,F_height) self.addKey(pos,osgGA.GUIEventAdapter.KEY_F12,"F12",1.0,F_height) cursorMoveHeight=0.35 pos.set(middle) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Left,"Left",1.0,cursorMoveHeight) down = osg.Vec3() down.set(pos) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Down,"Down",1.0,cursorMoveHeight) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Right,"Right",1.0,cursorMoveHeight) keypad = osg.Vec3() keypad.set(pos) keypad._x += 1.0 pos.set(down) pos._z += 1.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Up,"Up",1.0,cursorMoveHeight) homeHeight = 0.35 pos.set(middle) pos._z += 3.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Delete,"Delete",1.0,homeHeight) self.addKey(pos,osgGA.GUIEventAdapter.KEY_End,"End",1.0,homeHeight) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Page_Down,"Page\nDown",1.0,homeHeight) pos.set(middle) pos._z += 4.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Insert,"Insert",1.0,homeHeight) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Home,"Home",1.0,homeHeight) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Page_Up,"Page\nUp",1.0,homeHeight) pos.set(middle) pos._z += 5.5 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Print,"PrtScrn\nSysRq",1.0,homeHeight) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Scroll_Lock,"ScrLk",1.0,homeHeight) self.addKey(pos,osgGA.GUIEventAdapter.KEY_Pause,"Pause\nBreak",1.0,homeHeight) pos.set(keypad) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Insert,"0",2.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Delete,".",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Enter,"Enter",1.0,homeHeight) pos.set(keypad) pos._z += 1.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_End,"1",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Down,"2",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Page_Down,"3",1.0,1.0) pos.set(keypad) pos._z += 2.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Left,"4",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Begin,"5",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Right,"6",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Add,"+",1.0,1.0) pos.set(keypad) pos._z += 3.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Home,"7",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Up,"8",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Page_Up,"9",1.0,1.0) pos.set(keypad) pos._z += 4.0 self.addKey(pos,osgGA.GUIEventAdapter.KEY_Num_Lock,"Num\nLock",1.0,0.3) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Divide,"/",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Multiply,"*",1.0,1.0) self.addKey(pos,osgGA.GUIEventAdapter.KEY_KP_Subtract,"-",1.0,1.0) totalWidth = pos._x-origin._x pos.set(origin) pos._z += -1.5 geodeInput = osg.Geode() if 1: self._inputText = osgText.Text() self._inputText.setFont("fonts/arial.ttf") self._inputText.setColor(osg.Vec4(1.0,1.0,0.0,1.0)) self._inputText.setCharacterSize(1.0) self._inputText.setMaximumWidth(totalWidth) self._inputText.setPosition(pos) # self._inputText.setDrawMode(osgText.Text.TEXT | osgText.Text.BOUNDINGBOX) self._inputText.setDrawMode(osgText.Text.TEXT) self._inputText.setAlignment(osgText.Text.BASE_LINE) self._inputText.setAxisAlignment(osgText.Text.XZ_PLANE) self._inputText.setText("Press some keys...") geodeInput.addDrawable(self._inputText) self._scene.addChild(geodeInput) class KeyboardEventHandler(osgGA.GUIEventHandler): def __init__(self, keyboardModel): osgGA.GUIEventHandler.__init__(self) self._keyboardModel = keyboardModel self._self = self def handle(self, ea, aa): et = ea.getEventType() if et == osgGA.GUIEventAdapter.KEYDOWN: self._keyboardModel.keyChange(ea.getKey(), 1) return True elif et == osgGA.GUIEventAdapter.KEYUP: self._keyboardModel.keyChange(ea.getKey(), 0) return True else: return False def accept(self, v): v.visit(self) def main(argv ): # use an ArgumentParser object to manage the program arguments. arguments = osg.ArgumentParser(argv) # set up the usage document, in case we need to print out how to use this program. arguments.getApplicationUsage().setDescription(arguments.getApplicationName()+" is the example which demonstrates use of handling keyboard events and text.") arguments.getApplicationUsage().setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...") arguments.getApplicationUsage().addCommandLineOption("-h or --help","Display this information") arguments.getApplicationUsage().addCommandLineOption("-c","Mannually create occluders") # initialize the viewer. viewer = osgProducer.Viewer(arguments) # set up the value with sensible default event handlers. viewer.setUpViewer(osgProducer.Viewer.STANDARD_SETTINGS) keyboardModel = KeyboardModel() keh = KeyboardEventHandler(keyboardModel) viewer.getEventHandlerList().push_front(keh) # get details on keyboard and mouse bindings used by the viewer. viewer.getUsage(arguments.getApplicationUsage()) # if user request help write it out to cout. if arguments.read("-h") or arguments.read("--help"): arguments.getApplicationUsage().write() return 1 # any option left unread are converted into errors to write out later. arguments.reportRemainingOptionsAsUnrecognized() # report any errors if they have occured when parsing the program aguments. if arguments.errors(): arguments.writeErrorMessages() return 1 # attach the scene graph. viewer.setSceneData( keyboardModel.getScene() ) #osgDB.writeNodeFile(keyboardModel.getScene(),"test.osg") # create the windows and run the threads. viewer.realize() while not viewer.done(): # wait for all cull and draw threads to complete. viewer.sync() # update the scene by traversing it with the the update visitor which will # call all node update callbacks and animations. viewer.update() # fire off the cull and draw traversals of the scene. viewer.frame() # wait for all cull and draw threads to complete before exit. viewer.sync() return 0 if __name__ == "__main__": # raw_input("Press enter:") main(sys.argv)