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

Table of Contents

Overview

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

Expected Result

exampleEventProcessor1.png
Before your press 'f'
exampleEventProcessor2.png
After your press 'f'

Example details

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)
{
// print a message for the user to see in the console.
DtWarn("processKeyboardEvent handled\n");
// handled return true
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(de);
DtInputDriver& inputDriver = 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 a plug-in. You can run it by running vrvStealth.exe –plugin ../examples/plugins/release/exampleEventProcessor.dll –showConsole (on Windows) or vrvStealth.exe –plugin ../examples/plugins/release/libexampleEventProcessor.so (on Linux) from the command prompt. Once exampleEventProcessor is started press the "f" key to trigger the "processKeyboardEvent" and have a message print out to the console.

For more information about running examples, please see Running Applications and Examples.

Example Source Files


exampleEventProcessor.cxx

// /******************************************************************************
// ** Copyright (c) 2019 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#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());
// print a message for the user to see in the console.
DtWarn("processKeyboardEvent handled\n");
// handled return true
return true;
}
return false;
}
virtual const std::string& className()
{
return myClassName;
}
protected:
DtDe& myDe;
std::string myClassName;
};
void installEventProcessor(DtDe& de)
{
MyEventProcessor* captureF = new MyEventProcessor(de);
DtInputDriver& inputDriver = de.driverManager().inputDriver();
inputDriver.addEventProcessorToFront(captureF);
}
void initializeMinimalUI(DtDeInitializer& deInit, DtDe& de,
{
// Tell the De not to show the startup screen. This must be done here
// because it will be overwritten if set with the rest of the configs.
}
void init(DtDe& de)
{
// Ensure that init only gets called once
// (not strictly necessary here, but this is good practice in general)
// We only want to create the driver if we are running in master mode.
if (de.isInMasterMode())
{
if (app)
{
DtVrvApplication::ApplicationTransform functor = boost::bind(&initializeMinimalUI, _1, _2, _3);
app->installPostPluginTransform(functor, "MinimalUIPostPluginTransform");
}
// The accessory must be created after the DE's initialization is
// finished, to make sure all the objects it uses exist.
&installEventProcessor, boost::ref(de)));
}
}
{
// Setup the plug-in. Normally, the init function and functionality
// should be in its own library.
init(*de);
return true;
}

[<< Examples] [Home] [Top of Page]


Document ID: Generated on Tue Sep 21 17:42:52 EDT 2021 from SVN revision 234861
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)