Added support for assigning GraphicsContext to individual GUIEventAdapter events,

and use of this within osgViewer::View to better track the sources of events.
This commit is contained in:
Robert Osfield 2008-06-02 17:34:47 +00:00
parent 213a2d8d13
commit e09e07d45b
10 changed files with 45 additions and 31 deletions

View File

@ -59,6 +59,10 @@ class OSGGA_EXPORT EventQueue : public osg::Referenced
bool getUseFixedMouseInputRange() { return _useFixedMouseInputRange; }
/** Set the graphics context associated with this event queue.*/
void setGraphicsContext(osg::GraphicsContext* context) { getCurrentEventState()->setGraphicsContext(context); }
/** Set the mouse input range.*/
void setMouseInputRange(float xMin, float yMin, float xMax, float yMax) { getCurrentEventState()->setInputRange(xMin, yMin, xMax, yMax); }

View File

@ -16,6 +16,7 @@
#include <osg/Object>
#include <osg/Matrix>
#include <osg/GraphicsContext>
#include <osgGA/Export>
namespace osgGA{
@ -291,6 +292,9 @@ public:
double time() const { return _time; }
void setGraphicsContext(osg::GraphicsContext* context) { _context = context; }
const osg::GraphicsContext* getGraphicsContext() const { return _context.get(); }
/** set window rectangle. */
void setWindowRectangle(int x, int y, int width, int height, bool updateMouseRange = true);
@ -429,6 +433,7 @@ public:
EventType _eventType;
double _time;
osg::observer_ptr<osg::GraphicsContext> _context;
int _windowX;
int _windowY;
int _windowWidth;

View File

@ -40,7 +40,7 @@ class OSGVIEWER_EXPORT GraphicsWindow : public osg::GraphicsContext, public osgG
{
public:
GraphicsWindow() { _eventQueue = new osgGA::EventQueue; }
GraphicsWindow() { _eventQueue = new osgGA::EventQueue; _eventQueue->setGraphicsContext(this); }
virtual bool isSameKindAs(const Object* object) const { return dynamic_cast<const GraphicsWindow*>(object)!=0; }
virtual const char* libraryName() const { return "osgViewer"; }

View File

@ -32,12 +32,12 @@ GUIEventAdapter::GUIEventAdapter():
_windowHeight(1024),
_key(0),
_button(0),
_Xmin(0.0),
_Xmin(-1.0),
_Xmax(1.0),
_Ymin(0.0),
_Ymin(-1.0),
_Ymax(1.0),
_mx(0.5),
_my(0.5),
_mx(0.0),
_my(0.0),
_pressure(0.0),
_tiltX(0.0),
_tiltY(0.0),
@ -56,6 +56,7 @@ GUIEventAdapter::GUIEventAdapter(const GUIEventAdapter& rhs,const osg::CopyOp& c
_handled(rhs._handled),
_eventType(rhs._eventType),
_time(rhs._time),
_context(rhs._context),
_windowX(rhs._windowX),
_windowY(rhs._windowY),
_windowWidth(rhs._windowWidth),

View File

@ -626,6 +626,8 @@ void CompositeViewer::eventTraversal()
{
osgGA::GUIEventAdapter* event = itr->get();
//osg::notify(osg::NOTICE)<<"event->getGraphicsContext()="<<event->getGraphicsContext()<<std::endl;
bool pointerEvent = false;
float x = event->getX();
@ -668,15 +670,11 @@ void CompositeViewer::eventTraversal()
// If this camera is not a slave camera
if (camera->getView()->getCamera() == camera)
{
const osg::GraphicsContext::Traits* traits = gw ? gw->getTraits() : 0;
if (traits)
{
eventState->setInputRange( 0, 0, traits->width, traits->height);
}
else
{
eventState->setInputRange(-1.0, -1.0, 1.0, 1.0);
}
eventState->setGraphicsContext(gw);
eventState->setInputRange( viewport->x(), viewport->y(),
viewport->x()+viewport->width(),
viewport->y()+viewport->height());
}
else
{
@ -744,8 +742,6 @@ void CompositeViewer::eventTraversal()
event->setButtonMask(eventState->getButtonMask());
event->setMouseYOrientation(eventState->getMouseYOrientation());
}
//osg::notify(osg::NOTICE)<<" mouse x = "<<event->getX()<<" y="<<event->getY()<<std::endl;
// osg::notify(osg::NOTICE)<<" mouse Xmin = "<<event->getXmin()<<" Ymin="<<event->getYmin()<<" xMax="<<event->getXmax()<<" Ymax="<<event->getYmax()<<std::endl;
}
for(itr = gw_events.begin();

View File

@ -500,7 +500,7 @@ void GraphicsWindowCarbon::init()
{
if (_initialized) return;
getEventQueue()->setCurrentEventState(osgGA::GUIEventAdapter::getAccumulatedEventState().get());
// getEventQueue()->setCurrentEventState(osgGA::GUIEventAdapter::getAccumulatedEventState().get());
_lastModifierKeys = 0;
_windowTitleHeight = 0;

View File

@ -1011,7 +1011,7 @@ void GraphicsWindowWin32::init()
{
if (_initialized) return;
getEventQueue()->setCurrentEventState(osgGA::GUIEventAdapter::getAccumulatedEventState().get());
// getEventQueue()->setCurrentEventState(osgGA::GUIEventAdapter::getAccumulatedEventState().get());
WindowData *windowData = _traits.get() ? dynamic_cast<WindowData*>(_traits->inheritedWindowData.get()) : 0;
HWND windowHandle = windowData ? windowData->_hwnd : 0;

View File

@ -545,7 +545,7 @@ void GraphicsWindowX11::init()
return;
}
getEventQueue()->setCurrentEventState(osgGA::GUIEventAdapter::getAccumulatedEventState().get());
// getEventQueue()->setCurrentEventState(osgGA::GUIEventAdapter::getAccumulatedEventState().get());
WindowData* inheritedWindowData = dynamic_cast<WindowData*>(_traits->inheritedWindowData.get());
Window windowHandle = inheritedWindowData ? inheritedWindowData->_window : 0;
@ -904,6 +904,8 @@ void GraphicsWindowX11::checkEvents()
double resizeTime = eventTime;
_timeOfLastCheckEvents = getEventQueue()->getTime();
// osg::notify(osg::NOTICE)<<"GraphicsWindowX11::checkEvents() : getEventQueue()->getCurrentEventState()->getGraphicsContext()="<<getEventQueue()->getCurrentEventState()->getGraphicsContext()<<std::endl;
int windowX = _traits->x;
int windowY = _traits->y;
int windowWidth = _traits->width;

View File

@ -1703,23 +1703,28 @@ bool View::containsCamera(const osg::Camera* camera) const
const osg::Camera* View::getCameraContainingPosition(float x, float y, float& local_x, float& local_y) const
{
const osgGA::GUIEventAdapter* eventState = getEventQueue()->getCurrentEventState();
bool view_invert_y = eventState->getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS;
const osgViewer::GraphicsWindow* gw = dynamic_cast<const osgViewer::GraphicsWindow*>(eventState->getGraphicsContext());
// osg::notify(osg::NOTICE)<<"View::getCameraContainingPosition("<<x<<","<<y<<",..,..) view_invert_y="<<view_invert_y<<std::endl;
bool view_invert_y = eventState->getMouseYOrientation()==osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS;
double epsilon = 0.5;
if (_camera->getGraphicsContext() && _camera->getViewport())
if (_camera->getGraphicsContext() &&
(!gw || _camera->getGraphicsContext()==gw) &&
_camera->getViewport())
{
const osg::Viewport* viewport = _camera->getViewport();
double new_x = static_cast<double>(_camera->getGraphicsContext()->getTraits()->width) * (x - eventState->getXmin())/(eventState->getXmax()-eventState->getXmin());
double new_y = view_invert_y ?
double new_x = x;
double new_y = y;
if (!gw)
{
new_x = static_cast<double>(_camera->getGraphicsContext()->getTraits()->width) * (x - eventState->getXmin())/(eventState->getXmax()-eventState->getXmin());
new_y = view_invert_y ?
static_cast<double>(_camera->getGraphicsContext()->getTraits()->height) * (1.0 - (y- eventState->getYmin())/(eventState->getYmax()-eventState->getYmin())) :
static_cast<double>(_camera->getGraphicsContext()->getTraits()->height) * (y - eventState->getYmin())/(eventState->getYmax()-eventState->getXmin());
// osg::notify(osg::NOTICE)<<" new_x="<<new_x<<","<<new_y<<std::endl;
}
if (viewport &&
new_x >= (viewport->x()-epsilon) && new_y >= (viewport->y()-epsilon) &&
@ -1728,7 +1733,7 @@ const osg::Camera* View::getCameraContainingPosition(float x, float y, float& lo
local_x = new_x;
local_y = new_y;
// osg::notify(osg::NOTICE)<<"Returning master camera"<<std::endl;
osg::notify(osg::INFO)<<"Returning master camera"<<std::endl;
return _camera.get();
}
@ -1736,6 +1741,7 @@ const osg::Camera* View::getCameraContainingPosition(float x, float y, float& lo
osg::Matrix masterCameraVPW = getCamera()->getViewMatrix() * getCamera()->getProjectionMatrix();
// convert to non dimensional
x = (x - eventState->getXmin()) * 2.0 / (eventState->getXmax()-eventState->getXmin()) - 1.0;
y = (y - eventState->getYmin())* 2.0 / (eventState->getYmax()-eventState->getYmin()) - 1.0;
@ -1748,7 +1754,7 @@ const osg::Camera* View::getCameraContainingPosition(float x, float y, float& lo
slave._camera->getAllowEventFocus() &&
slave._camera->getRenderTargetImplementation()==osg::Camera::FRAME_BUFFER)
{
// osg::notify(osg::NOTICE)<<"Testing slave camera "<<slave._camera->getName()<<std::endl;
osg::notify(osg::INFO)<<"Testing slave camera "<<slave._camera->getName()<<std::endl;
const osg::Camera* camera = slave._camera.get();
const osg::Viewport* viewport = camera ? camera->getViewport() : 0;