Added Dragger::s/getActivationModKeyMask(..) and Dragger::s/getActivationKeyEvent(...) methods to make it possible to have draggers that only respond when you press a specified modified key or standard key.

Changed the optional dragger in osgvolume to require the shift key to be pressed for the dragger to become active.
This commit is contained in:
Robert Osfield 2009-07-03 19:16:53 +00:00
parent cb1b874167
commit 6e6a7c960e
8 changed files with 287 additions and 164 deletions

View File

@ -1,12 +1,12 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications,
* as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commericial and non commericial applications,
* as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <osgDB/ReadFile>
@ -15,6 +15,7 @@
#include <osg/Switch>
#include <osgText/Text>
#include <osg/Group>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
@ -26,10 +27,16 @@
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>
#include <iostream>
#ifdef _WINDOWS
#include <Windows.h>
#include <GL/glext.h>
#include <GL/wglext.h>
#include "vsynctoggle_custom.h"
#endif
int main(int argc, char** argv)
{
// use an ArgumentParser object to manage the program arguments.
@ -85,10 +92,9 @@ int main(int argc, char** argv)
keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() );
keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() );
keyswitchManipulator->addMatrixManipulator( '5', "Spherical", new osgGA::SphericalManipulator() );
std::string pathfile;
char keyForAnimationPath = '6';
char keyForAnimationPath = '5';
while (arguments.read("-p",pathfile))
{
osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
@ -151,7 +157,34 @@ int main(int argc, char** argv)
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
viewer.setSceneData( loadedModel.get() );
osg::ref_ptr<osg::Group> group = new osg::Group();
group->addChild(loadedModel.get());
viewer.setSceneData( group.get() );
{
char tmp[256];
tmp[sizeof(tmp) - 1] = 0;
getcwd(tmp, sizeof(tmp) - 1);
std::string path(tmp);
path += "\\system\\cache\\planet";
tmp[sizeof(tmp) - 1] = 0;
snprintf(tmp, sizeof(tmp) - 1, "%s", path.c_str());
//_putenv(tmp);
// osgDB::Registry::instance()->setFileCache(new osgDB::FileCache( tmp ) );
viewer.getDatabasePager()->setTargetMaximumNumberOfPageLOD(500);
#ifdef _WINDOWS
osg::ref_ptr<baronvsync::CVsyncDrawable> vsync = new baronvsync::CVsyncDrawable();
osg::ref_ptr<osg::Geode> vsyncGeode = new osg::Geode();
vsyncGeode->addDrawable(vsync.get());
vsync->SetVSync(false);
group->addChild(vsyncGeode.get());
#endif
}
viewer.realize();

View File

@ -98,7 +98,6 @@ osg::Node* addDraggerToScene(osg::Node* scene, const std::string& name)
osgManipulator::Dragger* dragger = createDragger(name);
dragger->setHandleEvents(true);
osg::Group* root = new osg::Group;
root->addChild(dragger);
@ -110,6 +109,17 @@ osg::Node* addDraggerToScene(osg::Node* scene, const std::string& name)
dragger->addTransformUpdating(selection);
// we want the dragger to handle it's own events automatically
dragger->setHandleEvents(true);
// if we don't set an activation key or mod mask then any mouse click on
// the dragger will activate it, however if do define either of ActivationModKeyMask or
// and ActivationKeyEvent then you'll have to press either than mod key or the specified key to
// be able to activate the dragger when you mouse click on it. Please note the follow allows
// activation if either the ctrl key or the 'a' key is pressed and held down.
dragger->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_CTRL);
dragger->setActivationKeyEvent('a');
return root;
}

View File

@ -1477,6 +1477,7 @@ int main( int argc, char **argv )
#endif
dragger->setupDefaultGeometry();
dragger->setHandleEvents(true);
dragger->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_SHIFT);
dragger->addDraggerCallback(new DraggerVolumeTileCallback(tile.get(), tile->getLocator()));
dragger->setMatrix(osg::Matrix::translate(0.5,0.5,0.5)*tile->getLocator()->getTransform());

View File

@ -217,8 +217,11 @@ class OSGMANIPULATOR_EXPORT Dragger : public osg::MatrixTransform
void setHandleEvents(bool flag);
bool getHandleEvents() const { return _handleEvents; }
void setDraggerActive(bool active) { _draggerActive = active; }
bool getDraggerActive() const { return _draggerActive; }
void setActivationModKeyMask(unsigned int mask) { _activationModKeyMask = mask; }
unsigned int getActivationModKeyMask() const { return _activationModKeyMask; }
void setActivationKeyEvent(int key) { _activationKeyEvent = key; }
int getActivationKeyEvent() const { return _activationKeyEvent; }
virtual void traverse(osg::NodeVisitor& nv);
@ -260,8 +263,17 @@ class OSGMANIPULATOR_EXPORT Dragger : public osg::MatrixTransform
virtual bool receive(const MotionCommand& command);
void dispatch(MotionCommand& command);
void setDraggerActive(bool active) { _draggerActive = active; }
bool getDraggerActive() const { return _draggerActive; }
bool _handleEvents;
bool _draggerActive;
unsigned int _activationModKeyMask;
int _activationKeyEvent;
bool _activationPermittedByModKeyMask;
bool _activationPermittedByKeyEvent;
osgManipulator::PointerInfo _pointer;
Dragger* _parentDragger;

View File

@ -125,7 +125,11 @@ bool PointerInfo::projectWindowXYIntoObject(const osg::Vec2d& windowCoord, osg::
//
Dragger::Dragger() :
_handleEvents(false),
_draggerActive(false)
_draggerActive(false),
_activationModKeyMask(0),
_activationKeyEvent(0),
_activationPermittedByModKeyMask(false),
_activationPermittedByKeyEvent(false)
{
_parentDragger = this;
getOrCreateStateSet()->setDataVariance(osg::Object::DYNAMIC);
@ -135,7 +139,13 @@ Dragger::Dragger() :
}
Dragger::Dragger(const Dragger& rhs, const osg::CopyOp& copyop):
osg::MatrixTransform(rhs, copyop)
osg::MatrixTransform(rhs, copyop),
_handleEvents(rhs._handleEvents),
_draggerActive(false),
_activationModKeyMask(rhs._activationModKeyMask),
_activationKeyEvent(rhs._activationKeyEvent),
_activationPermittedByModKeyMask(false),
_activationPermittedByKeyEvent(false)
{
osg::notify(osg::NOTICE)<<"CompositeDragger::CompositeDragger(const CompositeDragger& rhs, const osg::CopyOp& copyop) not Implemented yet."<<std::endl;
}
@ -250,6 +260,38 @@ bool Dragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&
bool handled = false;
bool activationPermitted = true;
if (_activationModKeyMask!=0 || _activationKeyEvent!=0)
{
_activationPermittedByModKeyMask = (_activationModKeyMask!=0) ?
((ea.getModKeyMask() & _activationModKeyMask)!=0) :
false;
if (_activationKeyEvent!=0)
{
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::KEYDOWN:
{
if (ea.getKey()==_activationKeyEvent) _activationPermittedByKeyEvent = true;
break;
}
case osgGA::GUIEventAdapter::KEYUP:
{
if (ea.getKey()==_activationKeyEvent) _activationPermittedByKeyEvent = false;
break;
}
default:
break;
}
}
activationPermitted = _activationPermittedByModKeyMask || _activationPermittedByKeyEvent;
}
if (activationPermitted || _draggerActive)
{
switch (ea.getEventType())
{
case osgGA::GUIEventAdapter::PUSH:
@ -310,6 +352,7 @@ bool Dragger::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&
setDraggerActive(false);
_pointer.reset();
}
}
return handled;
}

View File

@ -45,7 +45,6 @@ SET(TARGET_H
IF(CMAKE_COMPILER_IS_GNUCXX)
# Remove -pedantic flag as it barfs on ffmoeg headers
STRING(REGEX REPLACE "-pedantic" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
ENDIF()

View File

@ -459,7 +459,7 @@ void RayTracedTechnique::init()
osg::ref_ptr<TexGenLocatorCallback> locatorCallback = new TexGenLocatorCallback(texgen, masterLocator, layerLocator);
masterLocator->addCallback(locatorCallback.get());
layerLocator->addCallback(locatorCallback.get());
if (masterLocator != layerLocator) layerLocator->addCallback(locatorCallback.get());
stateset->setTextureAttributeAndModes(0, texgen, osg::StateAttribute::ON);

View File

@ -215,14 +215,24 @@ BEGIN_OBJECT_REFLECTOR(osgManipulator::Dragger)
__bool__getHandleEvents,
"",
"");
I_Method1(void, setDraggerActive, IN, bool, active,
I_Method1(void, setActivationModKeyMask, IN, unsigned int, mask,
Properties::NON_VIRTUAL,
__void__setDraggerActive__bool,
__void__setActivationModKeyMask__unsigned_int,
"",
"");
I_Method0(bool, getDraggerActive,
I_Method0(unsigned int, getActivationModKeyMask,
Properties::NON_VIRTUAL,
__bool__getDraggerActive,
__unsigned_int__getActivationModKeyMask,
"",
"");
I_Method1(void, setActivationKeyEvent, IN, int, key,
Properties::NON_VIRTUAL,
__void__setActivationKeyEvent__int,
"",
"");
I_Method0(int, getActivationKeyEvent,
Properties::NON_VIRTUAL,
__int__getActivationKeyEvent,
"",
"");
I_Method1(void, traverse, IN, osg::NodeVisitor &, x,
@ -314,15 +324,30 @@ BEGIN_OBJECT_REFLECTOR(osgManipulator::Dragger)
__void__dispatch__MotionCommand_R1,
"",
"");
I_ProtectedMethod1(void, setDraggerActive, IN, bool, active,
Properties::NON_VIRTUAL,
Properties::NON_CONST,
__void__setDraggerActive__bool,
"",
"");
I_ProtectedMethod0(bool, getDraggerActive,
Properties::NON_VIRTUAL,
Properties::CONST,
__bool__getDraggerActive,
"",
"");
I_SimpleProperty(int, ActivationKeyEvent,
__int__getActivationKeyEvent,
__void__setActivationKeyEvent__int);
I_SimpleProperty(unsigned int, ActivationModKeyMask,
__unsigned_int__getActivationModKeyMask,
__void__setActivationModKeyMask__unsigned_int);
I_SimpleProperty(osgManipulator::CompositeDragger *, Composite,
__CompositeDragger_P1__getComposite,
0);
I_SimpleProperty(osgManipulator::Dragger::Constraints &, Constraints,
__Constraints_R1__getConstraints,
0);
I_SimpleProperty(bool, DraggerActive,
__bool__getDraggerActive,
__void__setDraggerActive__bool);
I_SimpleProperty(osgManipulator::Dragger::DraggerCallbacks &, DraggerCallbacks,
__DraggerCallbacks_R1__getDraggerCallbacks,
0);