Shows how to create and manipulate DtWindowObjects in VR-Vantage.
The VR-Vantage Control Toolkit lets you create 2D window objects, such as lines, polygons and textboxes. This example shows how to add new window objects and manipulate them around the screen.
It allows you to create, translate (x&y), rotate, scale (x&y) multiple different objects, using both a parenting and non-parenting mode.
The box that is initially displayed in white on the screen is a visual representation of the DtWindowSurface's clipping box.
The key bindings for the example are as follows: “Space” - Creates a window object object of the selected type “1” - Sets window object type to Polygon “2” - Sets window object type to Line “3” - Sets window object type to Text Box “4” - Sets window object type to Eclipse “WASD” - Translates the object and its children around the screen “Left” & “Right” arrow keys - scale -/+ in the x direction “Up” & “Down” arrow keys - scale -/+ in the y direction "R" - Rotate object 45 degrees counterclockwise “,” & “.” - The comma and period keys traverse up and down the vector the objects are stored in for individual control. “p” - Turns off parenting for newly created objects, for individual control. Parenting is on by default, and each newly created object is attached to the previously created object. “v” - Turns the currently selected window object's visibility on / off.
This example is a plug-in. You can run it by running ./bin64/exampleWindowObjects_stealth.bat (on Windows from cmd.exe) or ./bin64/exampleWindowObjects_stealth.sh (on Linux). For more information about running examples, please see Running Applications and Examples.
#include <vrvCore/DtWindowPolygonAgent.hpp>
#include <vrvCore/DtWindowLineAgent.hpp>
#include <vrvCore/DtWindowTextAgent.hpp>
#include <vrvCore/DtWindowSurfaceAgent.hpp>
#include <vrvCore/DtWindowEllipseAgent.hpp>
#include <iostream>
namespace makVrv
{
DtAgentManager& agentManager, const std::string& instanceName )
: DtDriver( agentManager, instanceName )
, myWindowAgents()
, myActivatedObjectType( 1 )
, myActivatedObjectId( 0 )
, myParenting( true )
, mySurface( 0 )
, mySurfaceClipBoxVisualPoly( 0 )
{
setAutoStart( true );
myUserControllableFlag = false;
mySurfaceClipBoxVisualPoly = DtWindowPolygonAgent::create( myAgentManager );
if ( mySurfaceClipBoxVisualPoly )
{
mySurfaceClipBoxVisualPoly->setNumVertices( 4 );
mySurfaceClipBoxVisualPoly->setVertex( 0, 0.0, 0.0 );
mySurfaceClipBoxVisualPoly->setVertex( 1, 0.0, 0.5 );
mySurfaceClipBoxVisualPoly->setVertex( 2, 0.5, 0.5 );
mySurfaceClipBoxVisualPoly->setVertex( 3, 0.5, 0.0 );
mySurfaceClipBoxVisualPoly->setScale( 1.0 );
mySurfaceClipBoxVisualPoly->setRotation( 0.0 );
mySurfaceClipBoxVisualPoly->setFill( false );
mySurfaceClipBoxVisualPoly->setColor( 1.0, 1.0, 1.0, 1.0 );
mySurfaceClipBoxVisualPoly->setPosition( -0.25, -0.25 );
mySurface = DtWindowSurfaceAgent::create( myAgentManager );
mySurface->setClipBox( 0.25, 0.25, 0.75, 0.75 );
mySurface->setPosition( 0.5, 0.5 );
mySurfaceClipBoxVisualPoly->setParentId( mySurface->uniqueId() );
mySurface->addChild( mySurfaceClipBoxVisualPoly->uniqueId() );
}
}
DtWindowObjectsDriver::~DtWindowObjectsDriver()
{
}
const std::string& DtWindowObjectsDriver::className() const
{
static std::string name( "DtWindowObjectsDriver" );
return name;
}
void DtWindowObjectsDriver::drawObject()
{
switch ( myActivatedObjectType )
{
case 1:
drawPolygon();
break;
case 2:
drawLine();
break;
case 3:
drawText();
break;
case 4:
drawEllipse();
break;
}
}
void DtWindowObjectsDriver::drawPolygon()
{
DtWindowPolygonAgent* agent = DtWindowPolygonAgent::create( myAgentManager );
if ( agent )
{
agent->setNumVertices( 4 );
agent->setVertex( 0, 0.0, 0.0 );
agent->setVertex( 1, 0.8, 0.0 );
agent->setVertex( 2, 0.8, 0.1 );
agent->setVertex( 3, 0.0, 0.8 );
agent->setRotation( 0.0 );
agent->setFill( false );
agent->setColor( 1.0, 1.0, 1.0, 1.0 );
DtWindowObjectAgent* baseAgent = dynamic_cast<DtWindowObjectAgent*>(agent);
if ( baseAgent )
{
if ( myParenting && !myWindowAgents.empty() )
{
myWindowAgents.back()->addChild( baseAgent->uniqueId() );
baseAgent->setParentId( myWindowAgents.back()->uniqueId());
}
else if( myParenting && myWindowAgents.empty() )
{
mySurface->addChild( baseAgent->uniqueId() );
baseAgent->setParentId( mySurface->uniqueId() );
}
myWindowAgents.push_back( baseAgent );
}
}
}
void DtWindowObjectsDriver::drawLine()
{
DtWindowLineAgent* agentLine = DtWindowLineAgent::create( myAgentManager );
if ( agentLine )
{
agentLine->setVisible( true );
agentLine->setVertex( 0, 0.05, 0.05 );
agentLine->setVertex( 1, 0.1, 0.1 );
agentLine->setWidth( 4 );
agentLine->setColor( 1.0, 1.0, 1.0, 1.0 );
agentLine->setPosition( 0.1, 0.1 );
DtWindowObjectAgent* baseAgentLine = dynamic_cast<DtWindowObjectAgent*>(agentLine);
if ( baseAgentLine )
{
if ( myParenting && !myWindowAgents.empty() )
{
myWindowAgents.back()->addChild( baseAgentLine->uniqueId() );
baseAgentLine->setParentId( myWindowAgents.back()->uniqueId() );
}
else if( myParenting && myWindowAgents.empty() )
{
mySurface->addChild( baseAgentLine->uniqueId() );
baseAgentLine->setParentId( mySurface->uniqueId() );
}
myWindowAgents.push_back(baseAgentLine);
}
}
}
void DtWindowObjectsDriver::drawText()
{
DtWindowTextAgent* agentText = DtWindowTextAgent::create( myAgentManager );
if ( agentText )
{
agentText->setColor( 1.0, 1.0, 1.0, 1.0 );
agentText->setTopLeftCorner( (float)0.0, (float)0.0 );
agentText->setText( "Hello World!" );
agentText->setTextBoxWidth( 400 );
agentText->setFont( "FreeMono.otf", 14 );
agentText->setVisible( true );
DtWindowObjectAgent* baseAgentText = dynamic_cast<DtWindowObjectAgent*>(agentText);
if ( baseAgentText )
{
if ( myParenting && !myWindowAgents.empty() )
{
myWindowAgents.back()->addChild( baseAgentText->uniqueId() );
baseAgentText->setParentId( myWindowAgents.back()->uniqueId() );
}
else if( myParenting && myWindowAgents.empty() )
{
mySurface->addChild( baseAgentText->uniqueId() );
baseAgentText->setParentId( mySurface->uniqueId() );
}
myWindowAgents.push_back( baseAgentText );
}
}
}
void DtWindowObjectsDriver::drawEllipse()
{
DtWindowEllipseAgent* agent = DtWindowEllipseAgent::create( myAgentManager );
if ( agent )
{
agent->setCenter( 0.0, 0.0 );
agent->setDimensions( 0.25, 0.25 );
agent->setNumVertices( 10 );
agent->setFill( false );
agent->setScale( 1.0, 1.0 );
DtWindowObjectAgent* base = dynamic_cast<DtWindowObjectAgent*>(agent);
if ( base )
{
if ( myParenting && !myWindowAgents.empty() )
{
myWindowAgents.back()->addChild( base->uniqueId() );
base->setParentId( myWindowAgents.back()->uniqueId() );
}
else if( myParenting && myWindowAgents.empty() )
{
mySurface->addChild( base->uniqueId() );
base->setParentId( mySurface->uniqueId() );
}
myWindowAgents.push_back( base );
}
}
}
void DtWindowObjectsDriver::activateObjectType( int objectId )
{
myActivatedObjectType = objectId;
}
void DtWindowObjectsDriver::modifyActivateObjectId( bool increaseIdNumber )
{
if ( increaseIdNumber )
{
if ( myActivatedObjectId < myWindowAgents.size()-1 )
{
myActivatedObjectId++;
}
}
else
{
if ( myActivatedObjectId > 0 )
{
myActivatedObjectId--;
}
}
}
void DtWindowObjectsDriver::setParenting()
{
if ( myParenting )
{
myParenting = false;
}
else
{
myParenting = true;
}
}
void DtWindowObjectsDriver::modifyObjectVisibility()
{
DtWindowObjectAgent* temp = findWindowObjectAgent( myActivatedObjectId );
if ( temp )
{
DtWindowObject* actualObj = temp->findObject();
if ( actualObj )
{
if ( actualObj->visible() )
{
temp->setVisible( false );
}
else
{
temp->setVisible( true);
}
}
}
}
void DtWindowObjectsDriver::shiftWindowObjectPosition( DtDirection direction )
{
DtWindowObjectAgent* temp = findWindowObjectAgent( myActivatedObjectId );
if ( temp )
{
DtWindowObject* actualObj = temp->findObject();
if ( actualObj )
{
actualObj->getPosition( x, y );
switch ( direction )
{
case Up:
y = y + 0.1;
break;
case Left:
x = x - 0.1;
break;
case Down:
y = y - 0.1;
break;
case Right:
x = x + 0.1;
break;
default:
break;
}
temp->setPosition( x, y );
}
}
}
void DtWindowObjectsDriver::scaleWindowObject( DtDirection direction )
{
DtWindowObjectAgent* temp = findWindowObjectAgent( myActivatedObjectId );
if ( temp )
{
DtWindowObject* actualObj = temp->findObject();
if ( actualObj )
{
actualObj->getScale( x, y );
switch ( direction )
{
case Up:
y = y + 0.1;
break;
case Left:
x = x - 0.1;
break;
case Down:
y = y - 0.1;
break;
case Right:
x = x + 0.1;
break;
default:
break;
}
temp->setScale( x, y );
}
}
}
void DtWindowObjectsDriver::rotateWindowObject()
{
DtWindowObjectAgent* temp = findWindowObjectAgent( myActivatedObjectId );
if ( temp )
{
DtWindowObject* actualObj = temp->findObject();
if ( actualObj )
{
double rotation;
actualObj->getRotation( rotation );
temp->setRotation( rotation + 0.1 );
}
}
}
DtWindowObjectAgent* DtWindowObjectsDriver::findWindowObjectAgent( int vecLocation )
{
if ( vecLocation < myWindowAgents.size() )
{
return myWindowAgents[vecLocation];
}
return 0;
}
bool DtWindowObjectsDriver::onStart()
{
return true ;
}
bool DtWindowObjectsDriver::onStop()
{
DtWindowObjects::iterator iter = myWindowAgents.begin();
DtWindowObjects::iterator endIter = myWindowAgents.end();
for ( ; iter != endIter; ++iter )
{
DtWindowObjectAgent* toDelete = *iter;
delete toDelete;
toDelete = 0;
}
myWindowAgents.clear();
delete mySurfaceClipBoxVisualPoly;
mySurfaceClipBoxVisualPoly = 0;
delete mySurface;
mySurface = 0;
return true;
}
bool DtWindowObjectsDriver::onTick()
{
return true;
}
}