VR-Forces 4.3 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exampleEventProcessor

This example shows how to create and register an event processor.

This event processor bypasses the key mapping system. It toggles wireframe mode on or off whenever the F key is pressed, regardless of any key mapping to the contrary.

Note: Any functions mapped to the F key in a key map will not be executed, because the event processor returns true, indicating that the event has been handled.

  1. Create the new event processor class derived from DtEventProcessorInterface. We provide a reference to DtDisplayEngine, since we will need this to toggle wireframe mode.

class MyEventProcessor : public DtEventProcessorInterface
{
public:
MyEventProcessor( DtDe& de )
: myDe( de )
, myClassName( "MyEventProcessor" )
{
}

  1. The destructor is trivial, as is the processMouseEvent(), since we do not care about those. It is important that processMouseEvent return false, otherwise our processor will block all mouse messages. classname() simply returns the name of our class; this can be used to remove a processor from the event processor queue if desired.

virtual ~MyEventProcessor()
{
}
virtual bool processMouseEvent( const DtMouseEvent& ev, DtChannel& channel)
{
return false;
}

  1. The processKeyboardEvent() method does the work for this event processor. It checks to see if the event is a KEY_DOWN event, and if so, if the key pressed was KEY_R. It also makes sure that the modifiers are NO_MODIFIER, so that it will not mistakenly process SHIFT-F, CTRL-F, etc. Note that if we didn't check for the KEY_DOWN event explicitly, our processor would process the KEY_DOWN and KEY_UP events, which would provide us with a momentary key; pressing F would turn on wireframe mode, and releasing it would turn it back off.

virtual bool processKeyboardEvent( const DtKeyEvent& ev, DtChannel& channel)
{
if ( ev.eventType() == DtKeyEvent::KEY_DOWN &&
ev.keyState().keyType() == DtKeyState::KEY_F &&
ev.keyState().modifiers() == DtKeyState::NO_MODIFIER )
{
// handled
return true;
}
return false;
}

  1. Declare the member variables, and the class is complete.

DtDe& myDe;
std::string myClassName;

  1. Now we need to register our new event processor with the input driver. To do so, we add the following code after initialization. Note that we create the processor on the heap (using 'new') rather than on the stack, since the input processor takes control of the resource and deletes it when it is destroyed.

MyEventProcessor* captureF = new MyEventProcessor( igApp.de() );
DtInputDriver& inputDriver =
igApp.de().driverManager().inputDriver();
inputDriver.addEventProcessorToFront( captureF );

Building the Example

VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.

Running the Example

This example is an application. You can run it by running ./bin/exampleEventProcessor.exe (on Windows) or ./bin/exampleEventProcessor (on Linux). For more information about running examples, please see Running Applications and Examples.

Example Source Files


exampleEventProcessor.cxx

// /******************************************************************************
// ** Copyright (c) 2008 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
// /******************************************************************************
// ** $RCSfile: exampleEventProcessor.cxx,v $ $Revision: 1.14 $ $State: Exp $
// ******************************************************************************/
#include <boost/bind.hpp>
#include <vrvCore/DtDe.h>
// Use the VR-Vantage namespace. All classes in VR-Vantage are in this namespace.
using namespace makVrv;
class MyEventProcessor : public DtEventProcessorInterface
{
public:
MyEventProcessor( DtDe& de )
: myDe( de )
, myClassName( "MyEventProcessor" )
{
}
virtual ~MyEventProcessor()
{
}
virtual bool processMouseEvent( const DtMouseEvent& ev, DtChannel& channel)
{
return false;
}
virtual bool processKeyboardEvent( const DtKeyEvent& ev, DtChannel& channel)
{
{
myDe.sharedState().setWireframeMode(!myDe.sharedState().wireframeMode());
// handled
return true;
}
return false;
}
virtual const std::string& className()
{
return myClassName;
}
protected:
DtDe& myDe;
std::string myClassName;
};
int main(int argc,char** argv)
{
// A DtVrvApplication wraps up all the components required to build a complete
// VR-Vantage application (DtDe, DtPluginManager, DtEventLoop, and a
// DtVrvApplicationConfiguration).
// This causes the plugin manager to load plugins, cause a Display Configuration
// to be realized, creates the default environment, etc.
igApp.initialize(argc, argv);
MyEventProcessor* captureF = new MyEventProcessor( igApp.de() );
DtInputDriver& inputDriver =
inputDriver.addEventProcessorToFront( captureF );
// Creates an event loop and begins running it, causing the creation of frames.
igApp.run();
}

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)