#include KeyboardEventHandler::KeyboardEventHandler( int nTimesteps, float tps) { _paused = DEF_PAUSED_START; _direction = 1; _ntimesteps = nTimesteps; _tps = tps; _tpsorig = tps; _timestepchanged = false; _timestep = 0; _prevtime = 0; _togglewireframe = false; _toggleculling = false; } bool KeyboardEventHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&) { bool handled = false; if( ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN ) { switch( ea.getKey() ) { case osgGA::GUIEventAdapter::KEY_Space: _paused = _paused ? false : true; handled = true; break; case osgGA::GUIEventAdapter::KEY_Up: if( !_paused ) _tps *= 1.5; handled = true; break; case osgGA::GUIEventAdapter::KEY_Down: if( !_paused ) _tps /= 1.5; handled = true; break; case 'r': _paused = DEF_PAUSED_START; _tps = _tpsorig; _timestep = 0; _timestepchanged = true; handled = true; break; case osgGA::GUIEventAdapter::KEY_Right: if( _paused ) { _timestep = (_timestep+1) % _ntimesteps; _timestepchanged = true; } else _direction = +1; handled = true; break; case osgGA::GUIEventAdapter::KEY_Left: if( _paused ) { _timestep = _timestep-1; if( _timestep < 0 ) _timestep = _ntimesteps-1; _timestepchanged = true; } else _direction = -1; handled = true; break; case 'w': _togglewireframe = true; handled = true; break; case 'c': _toggleculling = true; handled = true; break; } } return handled; } void KeyboardEventHandler::setTime( float time) { if( !isPaused() && time - _prevtime > 1.0/_tps ) { _prevtime = time; _timestep = _timestep + _direction; if( _timestep == _ntimesteps ) _timestep = 0; else if( _timestep < 0 ) _timestep = _ntimesteps-1; _timestepchanged = true; } } bool KeyboardEventHandler::timestepChanged() { if( _timestepchanged ) { _timestepchanged = false; return true; } return false; } bool KeyboardEventHandler::toggleWireframe() { if( _togglewireframe ) { _togglewireframe = false; return true; } return false; } bool KeyboardEventHandler::toggleCulling() { if( _toggleculling ) { _toggleculling = false; return true; } return false; }