VR-Vantage
Loading...
Searching...
No Matches
exampleEventProcessor

Overview

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

Expected Result

Before your press 'f'
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)
{
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;
}
  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) 2024 MAK Technologies, Inc.
// ** All rights reserved.
// ******************************************************************************/
#include <boost/bind/bind.hpp>
#include <vrvCore/DtDe.h>
// Use the VR-Vantage namespace. All classes in VR-Vantage are in this namespace.
using namespace makVrv;
static vrvSignalsLib::connection postInitializeConnection;
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, boost::placeholders::_1, boost::placeholders::_2, boost::placeholders::_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.
postInitializeConnection = de.signal_postInitialize.connect(boost::bind(
&installEventProcessor, std::ref(de)));
}
}
{
// Setup the plug-in. Normally, the init function and functionality
// should be in its own library.
init(*de);
return true;
}
Contains makVrv::DtDe class.
#define DT_DE_INIT_ONCE(de)
This macro is called by modules in their init() function. This macro ensures that the module is only ...
Definition DtDe.h:30
Contains makVrv::DtDeSharedState class.
Contains makVrv:DtMasterScene class.
Interface for an event processor for a channel.
Contains makVrv::DtInputDriver class.
Contains makVrv::DtKeyMap and related classes.
Contains makVrv::DtKeyMapManager class.
Contains makVrv::DtVrvApplication implementation.
The abstract Channel Class.
Definition DtChannel.h:36
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer,...
Definition DtDe.h:77
virtual bool isInMasterMode() const
Returns true if the DtDe is in master mode.
virtual DtDriverManager & driverManager()
The DtMasterScene accessor. The function will throw before the initialize function is called.
boost::signalslib::signal< void()> signal_postInitialize
Definition DtDe.h:535
The initialization configuration for the makVrv::DtDe.
Definition DtDeInitializer.h:40
DtInputDriver & inputDriver()
Return the DtInputDriver associated with the master scene.
The DtEventProcessorInterface defines the generic interface for classes which handle input events....
Definition DtEventProcessorInterface.h:35
This class's main responsibility is to handle keyboard and mouse events from OSG (Qt events are route...
Definition DtInputDriver.h:46
virtual void addEventProcessorToFront(DtEventProcessorInterface *epi)
This class encapsulates a Key event.
Definition DtKeyEvent.h:17
const DtKeyState & keyState() const
Get the state object associated with this event.
Definition DtKeyEvent.h:44
DtKeyEvent::KeyEventType eventType() const
Get the event type this event.
Definition DtKeyEvent.h:59
@ KEY_DOWN
Definition DtKeyEvent.h:22
@ KEY_F
Definition DtKeyState.h:61
virtual KeyModifier modifiers() const
get the entire modifier vector instead of using the boolean accessors
@ NO_MODIFIER
Definition DtKeyState.h:137
virtual KeyType keyType() const
Get the type of key.
A configuration of a menu layout. A menu configuration manages an ordered collection of menu paths th...
Definition DtMenuConfiguration.h:44
This class encapsulates a mouse button event.
Definition DtMouseEvent.h:20
A configuration of the pages in the gui.
Definition DtPageConfiguration.h:26
A configuration of the toolbar layout.
Definition DtToolbarConfiguration.h:26
This owns a list of display engine configurations.
Definition DtVrvApplicationConfiguration.h:33
const UIConfiguration & masterModeUIConfiguration() const
void setMasterModePageConfiguration(const DtPageConfiguration &pageConfig)
void setMasterModeToolbarConfiguration(const DtToolbarConfiguration &toolbarConfig)
void setMasterModeMenuConfiguration(const DtMenuConfiguration &menuConfig)
The Root IG Application object. To run the IG simply create one, initialize it and call run.
Definition DtVrvApplication.h:58
static DtVrvApplication * findFromDe(DtDe &)
virtual void installPostPluginTransform(const ApplicationTransform &transform, const std::string &id)
Add a transformer that runs after plug-ins are loaded.
boost::function< void(DtDeInitializer &, DtDe &, DtVrvApplicationConfiguration &)> ApplicationTransform
Definition DtVrvApplication.h:147
DT_DLL_EXAMPLEACCESSORY void init(makVrv::DtDe &de)
Contains DLL export macros.
DT_DE_PLUGIN_EXPORT_MACRO bool initDeModule(makVrv::DtDe *de)
Contains DtCreateSensorFieldOfViewCommand and DtCreateSensorFieldOfViewCommandCreator class declarati...
Definition DtGridLinesSettingsRecord.h:16
bool myShowStartupScreen
Definition DtVrvApplicationConfiguration.h:42