Skip to main content
GameDev.net gamedev.net
🔒 Locked

keyboardhandler class help

Started by nerazzuri_4 Apr 18, 2010 at 9:42 AM 0 replies 950+ views
Original Post
nerazzuri_4
nerazzuri_4
Hi, I am trying to write a program where I load some CAD objects and then try to move them and manipulate parts of them using a keyboard handler. I found the example on the osg tutorials page but I'm having the following problem: How I can modify the function that is in keyboardhandler.h which is currently: typedef void (*functionType) (); //change this to the one below: typedef void (*functionType) (osg::PositionAttitudeTransform* PAT, double& value); The reason I want to do this is because for example a function like: void moveForward(osg::PositionAttitudeTransform* PAT, double& value); //in my main() I want to pass it a PAT node and the value of either x/y/z coordinate and modify it in the function. Currently the way I have it setup doesnt allow me to pass paramters so I have to keep these values as global variables so they can be accessed by any function. But I want to avoid this. ---------------------------------------------------------------------------------------------------------------------------------------------------------------- For reference I will copy the keyboard handler class below: class keyboardEventHandler : public osgGA::GUIEventHandler { public: typedef void (*functionType) (); enum keyStatusType { KEY_UP, KEY_DOWN }; struct functionStatusType { functionStatusType(){keyState = KEY_UP; keyFunction = NULL;} functionType keyFunction; keyStatusType keyState; }; bool addFunction(int whatKey, functionType newFunction); bool addFunction(int whatKey, keyStatusType keyPressStatus, functionType newFunction); virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); }; protected: typedef std::map keyFunctionMap; keyFunctionMap keyFuncMap; keyFunctionMap keyUPFuncMap; };
nerazzuri_4
nerazzuri_4
I forgot to post the keyboardhandler.cpp file so:

bool keyboardEventHandler::addFunction(int whatKey, functionType newFunction)
{
if ( keyFuncMap.end() != keyFuncMap.find( whatKey ))
{
std::cout << "duplicate key '" << whatKey << "' ignored." << std::endl;
return false;
}
else
{
keyFuncMap[whatKey].keyFunction = newFunction;
return true;
}
}

bool keyboardEventHandler::addFunction (int whatKey, keyStatusType keyPressStatus, functionType newFunction)
{
if (keyPressStatus == KEY_DOWN)
{
return addFunction(whatKey,newFunction);
}
else
{
if ( keyUPFuncMap.end() != keyUPFuncMap.find( whatKey ))
{
std::cout << "duplicate key '" << whatKey << "' ignored." << std::endl;
return false;
}
else
{
keyUPFuncMap[whatKey].keyFunction = newFunction;
return true;
}
} // KEY_UP
}

bool keyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa)
{
bool newKeyDownEvent = false;
bool newKeyUpEvent = false;

switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
keyFunctionMap::iterator itr = keyFuncMap.find(ea.getKey());
if (itr != keyFuncMap.end())
{
if ( (*itr).second.keyState == KEY_UP )
{
(*itr).second.keyState = KEY_DOWN;
newKeyDownEvent = true;
}
if (newKeyDownEvent)
{
(*itr).second.keyFunction;
newKeyDownEvent = false;
}
return true;
}
return false;
}
case(osgGA::GUIEventAdapter::KEYUP):
{
keyFunctionMap::iterator itr = keyFuncMap.find(ea.getKey());
if (itr != keyFuncMap.end() )
{
(*itr).second.keyState = KEY_UP;
}
itr = keyUPFuncMap.find(ea.getKey());
if (itr != keyUPFuncMap.end())
{
(*itr).second.keyFunction;
return true;
}
return false;
}
default:
return false;
}
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.