Added new osgkeyboad example.

This commit is contained in:
Robert Osfield 2003-03-24 21:17:51 +00:00
parent c2167be67b
commit a03675495a
8 changed files with 495 additions and 2 deletions

View File

@ -97,6 +97,7 @@ EXAMPLE_DIRS = \
osghangglide\ osghangglide\
osghud\ osghud\
osgimpostor\ osgimpostor\
osgkeyboard\
osglight\ osglight\
osglightpoint\ osglightpoint\
osglogo\ osglogo\

View File

@ -492,6 +492,33 @@ Package=<4>
############################################################################### ###############################################################################
Project: "Example osgkeyboard"=.\examples\osgkeyboard\osgkeyboard.dsp - Package Owner=<4>
Package=<5>
{{{
}}}
Package=<4>
{{{
Begin Project Dependency
Project_Dep_Name Core osg
End Project Dependency
Begin Project Dependency
Project_Dep_Name Core osgDB
End Project Dependency
Begin Project Dependency
Project_Dep_Name Core osgGA
End Project Dependency
Begin Project Dependency
Project_Dep_Name Core osgProducer
End Project Dependency
Begin Project Dependency
Project_Dep_Name Core osgUtil
End Project Dependency
}}}
###############################################################################
Project: "Example osglight"=.\examples\osglight\osglight.dsp - Package Owner=<4> Project: "Example osglight"=.\examples\osglight\osglight.dsp - Package Owner=<4>
Package=<5> Package=<5>

View File

@ -0,0 +1,95 @@
# Microsoft Developer Studio Project File - Name="Example osgkeyboard" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=Example osgkeyboard - Win32 Release
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "osgkeyboard.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "osgkeyboard.mak" CFG="Example osgkeyboard - Win32 Release"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "Example osgkeyboard - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "Example osgkeyboard - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "Example osgkeyboard - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MD /W3 /GR /GX /O2 /I "../../../include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 /nologo /subsystem:console /pdb:none /machine:I386 /out:"../../../bin/osgkeyboard.exe" /libpath:"../../../lib"
!ELSEIF "$(CFG)" == "Example osgkeyboard - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /MDd /W3 /Gm /vd0 /GR /GX /Zi /Od /I "../../../include" /D "_CONSOLE" /D "_MBCS" /D "FL_DLL" /D "WIN32" /D "_DEBUG" /FR /YX /FD /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 glut32.lib glu32.lib opengl32.lib /nologo /subsystem:console /debug /machine:I386 /nodefaultlib:"libcmt" /out:"../../../bin/osgkeyboardd.exe" /pdbtype:sept /libpath:"../../../lib"
# SUBTRACT LINK32 /incremental:no
!ENDIF
# Begin Target
# Name "Example osgkeyboard - Win32 Release"
# Name "Example osgkeyboard - Win32 Debug"
# Begin Source File
SOURCE=..\..\..\examples\osgkeyboard\osgkeyboard.cpp
# End Source File
# End Target
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Project

View File

@ -0,0 +1,19 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgkeyboard.cpp\
LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
GNUmakefile.inst=GNUmakefile
EXEC = osgkeyboard
INC += $(PRODUCER_INCLUDE_DIR)
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@ -0,0 +1,15 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
osgkeyboard.cpp\
LIBS += -losgProducer -lProducer -losgText -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = osgkeyboard
INC += $(PRODUCER_INCLUDE_DIR)
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@ -0,0 +1,334 @@
#include <osgProducer/Viewer>
#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Group>
#include <osg/Switch>
#include <osg/Notify>
#include <osg/Geometry>
#include <osgText/Text>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
class KeyboardModel : public osg::Referenced
{
public:
KeyboardModel() { createKeyboard(); }
osg::Group* getScene() { return _scene.get(); }
void keyChange(int key,int value);
protected:
~KeyboardModel() {}
void addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height);
void createKeyboard();
typedef std::map<int, osg::ref_ptr<osg::Switch> > KeyModelMap;
osg::ref_ptr<osg::Group> _scene;
KeyModelMap _keyModelMap;
};
void KeyboardModel::keyChange(int key,int value)
{
std::cout << std::hex << key << "\t"<< value << std::dec << std::endl;
KeyModelMap::iterator itr = _keyModelMap.find(key);
if (itr!=_keyModelMap.end())
{
itr->second->setSingleChildOn(value);
}
}
void KeyboardModel::addKey(osg::Vec3& pos, int key,const std::string& text,float width, float height)
{
osg::Geode* geodeUp = new osg::Geode;
{
osgText::Text* textUp = new osgText::Text;
textUp->setFont("fonts/arial.ttf");
textUp->setColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
textUp->setCharacterSize(height);
textUp->setPosition(pos);
textUp->setDrawMode(osgText::Text::TEXT/*|osgText::Text::BOUNDINGBOX*/);
textUp->setAlignment(osgText::Text::LEFT_CENTER);
textUp->setAxisAlignment(osgText::Text::XZ_PLANE);
textUp->setText(text);
geodeUp->addDrawable(textUp);
}
osg::Geode* geodeDown = new osg::Geode;
{
osgText::Text* textDown = new osgText::Text;
textDown->setFont("fonts/arial.ttf");
textDown->setColor(osg::Vec4(1.0f,0.0f,1.0f,1.0f));
textDown->setCharacterSize(height);
textDown->setPosition(pos);
textDown->setDrawMode(osgText::Text::TEXT/*||osgText::Text::BOUNDINGBOX*/);
textDown->setAlignment(osgText::Text::LEFT_CENTER);
textDown->setAxisAlignment(osgText::Text::XZ_PLANE);
textDown->setText(text);
geodeDown->addDrawable(textDown);
}
osg::Switch* model = new osg::Switch;
model->addChild(geodeUp,true);
model->addChild(geodeDown,false);
_scene->addChild(model);
_keyModelMap[key] = model;
pos.x() += width;
}
void KeyboardModel::createKeyboard()
{
_scene = new osg::Group;
osg::Vec3 pos(0.0f,0.0f,0.0f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Control_L,"Ctrl",2.0f,0.5f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Super_L,"Super",2.0f,0.5f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Alt_L,"Alt",2.0f,0.5f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Space,"Space",3.0f,1.0f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Mode_switch,"Switch",2.0f,0.5f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Super_R,"Super",2.0f,0.5f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Menu,"Menu",2.0f,0.5f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Control_R,"Ctrl",2.0f,0.5f);
osg::Vec3 middle(pos.x()+1.0f,0.0f,0.0f);
pos.x() = 0.0f;
pos.z() += 1.0f;
addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_L,"Shift",2.0f,0.5f);
addKey(pos,'\\',"\\",1.0f,1.0f);
addKey(pos,'z',"Z",1.0f,1.0f);
addKey(pos,'x',"X",1.0f,1.0f);
addKey(pos,'c',"C",1.0f,1.0f);
addKey(pos,'v',"V",1.0f,1.0f);
addKey(pos,'b',"B",1.0f,1.0f);
addKey(pos,'n',"N",1.0f,1.0f);
addKey(pos,'m',"M",1.0f,1.0f);
addKey(pos,',',",",1.0f,1.0f);
addKey(pos,'.',".",1.0f,1.0f);
addKey(pos,'/',"/",1.0f,1.0f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Shift_R,"Shift",2.0f,0.5f);
pos.x() = 0.0f;
pos.z() += 1.0f;
addKey(pos,osgGA::GUIEventAdapter::KEY_Caps_Lock,"Caps",2.0f,0.5f);
addKey(pos,'a',"A",1.0f,1.0f);
addKey(pos,'s',"S",1.0f,1.0f);
addKey(pos,'d',"D",1.0f,1.0f);
addKey(pos,'f',"F",1.0f,1.0f);
addKey(pos,'g',"G",1.0f,1.0f);
addKey(pos,'h',"H",1.0f,1.0f);
addKey(pos,'j',"J",1.0f,1.0f);
addKey(pos,'k',"K",1.0f,1.0f);
addKey(pos,'l',"L",1.0f,1.0f);
addKey(pos,';',";",1.0f,1.0f);
addKey(pos,'\'',"'",1.0f,1.0f);
addKey(pos,'#',"#",1.0f,1.0f);
addKey(pos,osgGA::GUIEventAdapter::KEY_Return,"Return",4.0f,0.5f);
pos.x() = 0.0f;
pos.z() += 1.0f;
addKey(pos,osgGA::GUIEventAdapter::KEY_Tab,"Tab",2.0f,0.5f);
addKey(pos,'q',"Q",1.0f,1.0f);
addKey(pos,'w',"W",1.0f,1.0f);
addKey(pos,'e',"E",1.0f,1.0f);
addKey(pos,'r',"R",1.0f,1.0f);
addKey(pos,'t',"T",1.0f,1.0f);
addKey(pos,'y',"Y",1.0f,1.0f);
addKey(pos,'u',"U",1.0f,1.0f);
addKey(pos,'i',"I",1.0f,1.0f);
addKey(pos,'o',"O",1.0f,1.0f);
addKey(pos,'p',"P",1.0f,1.0f);
addKey(pos,'[',"[",1.0f,1.0f);
addKey(pos,']',"]",1.0f,1.0f);
pos.x() = 0.0f;
pos.z() += 1.0f;
addKey(pos,'`',"`",1.0f,1.0f);
addKey(pos,'1',"1",1.0f,1.0f);
addKey(pos,'2',"2",1.0f,1.0f);
addKey(pos,'3',"3",1.0f,1.0f);
addKey(pos,'4',"4",1.0f,1.0f);
addKey(pos,'5',"5",1.0f,1.0f);
addKey(pos,'6',"6",1.0f,1.0f);
addKey(pos,'7',"7",1.0f,1.0f);
addKey(pos,'8',"8",1.0f,1.0f);
addKey(pos,'9',"9",1.0f,1.0f);
addKey(pos,'0',"0",1.0f,1.0f);
addKey(pos,'-',"-",1.0f,1.0f);
addKey(pos,'=',"=",1.0f,1.0f);
addKey(pos,osgGA::GUIEventAdapter::KEY_BackSpace,"Backspace",3.0f,0.5f);
pos.x() = 0.0f;
pos.z() += 1.0f;
float F_height = 0.5f;
addKey(pos,osgGA::GUIEventAdapter::KEY_Escape,"Esc",2.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F1,"F1",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F2,"F2",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F3,"F3",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F4,"F4",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F5,"F5",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F6,"F6",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F7,"F7",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F8,"F8",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F9,"F9",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F10,"F10",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F11,"F11",1.0f,F_height);
addKey(pos,osgGA::GUIEventAdapter::KEY_F12,"F12",1.0f,F_height);
float cursorMoveHeight=0.5f;
pos = middle;
addKey(pos,osgGA::GUIEventAdapter::KEY_Left,"Left",2.0f,cursorMoveHeight);
osg::Vec3 down = pos;
addKey(pos,osgGA::GUIEventAdapter::KEY_Down,"Down",2.0f,cursorMoveHeight);
addKey(pos,osgGA::GUIEventAdapter::KEY_Right,"Right",2.0f,cursorMoveHeight);
pos = down;
pos.z() += 1.0f;
addKey(pos,osgGA::GUIEventAdapter::KEY_Up,"Up",2.0f,cursorMoveHeight);
}
class KeyboardEventHandler : public osgGA::GUIEventHandler
{
public:
KeyboardEventHandler(KeyboardModel* keyboardModel):
_keyboardModel(keyboardModel) {}
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
_keyboardModel->keyChange(ea.getKey(),1);
return true;
}
case(osgGA::GUIEventAdapter::KEYUP):
{
_keyboardModel->keyChange(ea.getKey(),0);
return true;
}
default:
return false;
}
}
virtual void accept(osgGA::GUIEventHandlerVisitor& v)
{
v.visit(*this);
}
osg::ref_ptr<KeyboardModel> _keyboardModel;
};
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getProgramName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
arguments.getApplicationUsage()->addCommandLineOption("-c","Mannually create occluders");
// initialize the viewer.
osgProducer::Viewer viewer(arguments);
// set up the value with sensible default event handlers.
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
osg::ref_ptr<KeyboardModel> keyboardModel = new KeyboardModel;
KeyboardEventHandler* keh = new KeyboardEventHandler(keyboardModel.get());
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") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
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(std::cout);
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(Producer::CameraGroup::ThreadPerCamera);
while( !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();
}
//viewer.sync();
return 0;
}

View File

@ -53,6 +53,8 @@ public:
enum KeySymbol enum KeySymbol
{ {
KEY_Space = 0x20,
KEY_BackSpace = 0xFF08, /* back space, back char */ KEY_BackSpace = 0xFF08, /* back space, back char */
KEY_Tab = 0xFF09, KEY_Tab = 0xFF09,
KEY_Linefeed = 0xFF0A, /* Linefeed, LF */ KEY_Linefeed = 0xFF0A, /* Linefeed, LF */
@ -94,7 +96,7 @@ public:
KEY_Help = 0xFF6A, /* Help */ KEY_Help = 0xFF6A, /* Help */
KEY_Break = 0xFF6B, KEY_Break = 0xFF6B,
KEY_Mode_switch = 0xFF7E, /* Character set switch */ KEY_Mode_switch = 0xFF7E, /* Character set switch */
KEY_script_switch = 0xFF7E, /* Alias for mode_switch */ KEY_Script_switch = 0xFF7E, /* Alias for mode_switch */
KEY_Num_Lock = 0xFF7F, KEY_Num_Lock = 0xFF7F,
/* Keypad Functions, keypad numbers cleverly chosen to map to ascii */ /* Keypad Functions, keypad numbers cleverly chosen to map to ascii */

View File

@ -232,7 +232,7 @@ bool EventAdapter::initKeySymbolMap()
s_keySymbolMap[VK_MODECHANGE] = KEY_Mode_switch; s_keySymbolMap[VK_MODECHANGE] = KEY_Mode_switch;
s_keySymbolMap[VK_SPACE] = 0x20 s_keySymbolMap[VK_SPACE] = KEY_Space;
s_keySymbolMap[VK_PRIOR] = KEY_Prior; s_keySymbolMap[VK_PRIOR] = KEY_Prior;
s_keySymbolMap[VK_NEXT] = KEY_Next; s_keySymbolMap[VK_NEXT] = KEY_Next;
s_keySymbolMap[VK_END] = KEY_End; s_keySymbolMap[VK_END] = KEY_End;