Moved experimental Widget class to osgGA

Add computeIntersections() to the osgGA::GUIActionAdapter base class to enable intersection tests without needing to directly link to osgViewer.
This commit is contained in:
Robert Osfield
2014-02-06 17:32:41 +00:00
parent 958a7d0ab0
commit 4016aed62d
6 changed files with 24 additions and 16 deletions

View File

@@ -1,5 +1,4 @@
SET(TARGET_SRC
Widget.cpp
TransferFunctionWidget.cpp
osgtransferfunction.cpp
)

View File

@@ -17,14 +17,14 @@
#include <osg/Group>
#include <osg/TransferFunction>
#include "Widget.h"
#include <osgGA/Widget>
#define OSGUI_EXPORT
namespace osgUI
{
class OSGUI_EXPORT TransferFunctionWidget : public osgUI::Widget
class OSGUI_EXPORT TransferFunctionWidget : public osgGA::Widget
{
public:
TransferFunctionWidget(osg::TransferFunction1D* tf=0);

View File

@@ -1,303 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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. See the
* OpenSceneGraph Public License for more details.
*/
#include "Widget.h"
#include <osg/Geode>
#include <osg/ScriptEngine>
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include <osg/io_utils>
#include <osgGA/EventVisitor>
#include <osgViewer/View>
using namespace osgUI;
Widget::Widget():
_focusBehaviour(FOCUS_FOLLOWS_POINTER),
_hasEventFocus(false),
_graphicsInitialized(false)
{
setNumChildrenRequiringEventTraversal(1);
}
Widget::Widget(const Widget& widget, const osg::CopyOp& copyop):
osg::Group(),
_focusBehaviour(widget._focusBehaviour),
_hasEventFocus(false),
_graphicsInitialized(false)
{
setNumChildrenRequiringEventTraversal(1);
}
void Widget::setExtents(const osg::BoundingBox& bb)
{
_extents = bb;
}
void Widget::updateFocus(osg::NodeVisitor& nv)
{
osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(&nv);
osgViewer::View* view = ev ? dynamic_cast<osgViewer::View*>(ev->getActionAdapter()) : 0;
if (ev && view)
{
osgGA::EventQueue::Events& events = ev->getEvents();
for(osgGA::EventQueue::Events::iterator itr = events.begin();
itr != events.end();
++itr)
{
osgGA::GUIEventAdapter* ea = (*itr)->asGUIEventAdapter();
if (ea)
{
bool previousFocus = _hasEventFocus;
if (_focusBehaviour==CLICK_TO_FOCUS)
{
if (ea->getEventType()==osgGA::GUIEventAdapter::PUSH)
{
int numButtonsPressed = 0;
if (ea->getButtonMask()&osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON) ++numButtonsPressed;
if (ea->getButtonMask()&osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON) ++numButtonsPressed;
if (ea->getButtonMask()&osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON) ++numButtonsPressed;
if (numButtonsPressed==1)
{
osgUtil::LineSegmentIntersector::Intersections intersections;
bool withinWidget = view->computeIntersections(*ea, nv.getNodePath(), intersections);
if (withinWidget) _hasEventFocus = true;
else _hasEventFocus = false;
}
}
}
else if (_focusBehaviour==FOCUS_FOLLOWS_POINTER)
{
bool checkWithinWidget = false;
if (!_hasEventFocus)
{
checkWithinWidget = (ea->getEventType()!=osgGA::GUIEventAdapter::FRAME) && ea->getButtonMask()==0;
}
else
{
// if mouse move or mouse release check to see if mouse still within widget to retain focus
if (ea->getEventType()==osgGA::GUIEventAdapter::MOVE)
{
checkWithinWidget = true;
}
else if (ea->getEventType()==osgGA::GUIEventAdapter::RELEASE)
{
// if no buttons pressed then check
if (ea->getButtonMask()==0) checkWithinWidget = true;
}
}
if (checkWithinWidget)
{
osgUtil::LineSegmentIntersector::Intersections intersections;
bool withinWidget = view->computeIntersections(*ea, nv.getNodePath(), intersections);
_hasEventFocus = withinWidget;
}
}
if (previousFocus != _hasEventFocus)
{
if (_hasEventFocus)
{
enter();
#if 0
if (view->getCameraManipulator())
{
view->getCameraManipulator()->finishAnimation();
view->requestContinuousUpdate( false );
}
#endif
}
else
{
leave();
}
}
}
}
}
}
void Widget::setHasEventFocus(bool focus)
{
if (_hasEventFocus == focus) return;
_hasEventFocus = focus;
if (_hasEventFocus) enter();
else leave();
}
bool Widget::getHasEventFocus() const
{
return _hasEventFocus;
}
void Widget::enter()
{
osg::CallbackObject* co = osg::getCallbackObject(this, "enter");
if (co)
{
co->run(this);
}
else
{
enterImplementation();
}
}
void Widget::enterImplementation()
{
OSG_NOTICE<<"enter()"<<std::endl;
}
void Widget::leave()
{
osg::CallbackObject* co = osg::getCallbackObject(this, "leave");
if (co)
{
co->run(this);
}
else
{
leaveImplementation();
}
}
void Widget::leaveImplementation()
{
OSG_NOTICE<<"leave()"<<std::endl;
}
void Widget::traverse(osg::NodeVisitor& nv)
{
osg::CallbackObject* co = osg::getCallbackObject(this, "traverse");
if (co)
{
osg::Parameters inputParameters, outputParameters;
inputParameters.push_back(&nv);
co->run(this, inputParameters, outputParameters);
}
else
{
traverseImplementation(nv);
}
}
void Widget::traverseImplementation(osg::NodeVisitor& nv)
{
if (!_graphicsInitialized && nv.getVisitorType()!=osg::NodeVisitor::CULL_VISITOR) createGraphics();
osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(&nv);
osgViewer::View* view = ev ? dynamic_cast<osgViewer::View*>(ev->getActionAdapter()) : 0;
if (ev && view)
{
updateFocus(nv);
if (getHasEventFocus())
{
// signify that event has been taken by widget with focus
ev->setEventHandled(true);
osgGA::EventQueue::Events& events = ev->getEvents();
for(osgGA::EventQueue::Events::iterator itr = events.begin();
itr != events.end();
++itr)
{
handle(ev, itr->get());
(*itr)->setHandled(true);
}
}
}
else
{
osg::Group::traverse(nv);
}
}
bool Widget::handle(osgGA::EventVisitor* ev, osgGA::Event* event)
{
osg::CallbackObject* co = osg::getCallbackObject(this, "handle");
if (co)
{
osg::Parameters inputParameters, outputParameters;
inputParameters.push_back(ev);
inputParameters.push_back(event);
if (co->run(this, inputParameters, outputParameters))
{
if (outputParameters.size()>=1)
{
return true;
}
}
return false;
}
else
{
return handleImplementation(ev, event);
}
}
bool Widget::handleImplementation(osgGA::EventVisitor* /*ev*/, osgGA::Event* /*event*/)
{
return false;
}
bool Widget::computePositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3& localPosition) const
{
osgViewer::View* view = ev ? dynamic_cast<osgViewer::View*>(ev->getActionAdapter()) : 0;
osgUtil::LineSegmentIntersector::Intersections intersections;
if (view && view->computeIntersections(*event, ev->getNodePath(), intersections))
{
localPosition = intersections.begin()->getLocalIntersectPoint();
return (_extents.contains(localPosition, 1e-6));
}
else
{
return false;
}
}
void Widget::createGraphics()
{
osg::CallbackObject* co = osg::getCallbackObject(this, "createGraphics");
if (co)
{
co->run(this);
}
else
{
createGraphicsImplementation();
}
}
void Widget::createGraphicsImplementation()
{
_graphicsInitialized = true;
}
osg::BoundingSphere Widget::computeBound() const
{
return osg::BoundingSphere(_extents);
}

View File

@@ -1,90 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGUI_WIDGET
#define OSGUI_WIDGET
#include <osg/Group>
#include <osg/BoundingBox>
#include <osgGA/Event>
#include <osgGA/EventVisitor>
#define OSGUI_EXPORT
namespace osgUI
{
class OSGUI_EXPORT Widget : public osg::Group
{
public:
Widget();
Widget(const Widget& tfw, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Node(osgUI, Widget);
virtual void traverse(osg::NodeVisitor& nv);
virtual void traverseImplementation(osg::NodeVisitor& nv);
virtual bool handle(osgGA::EventVisitor* ev, osgGA::Event* event);
virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event);
virtual bool computePositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3& localPosition) const;
virtual void createGraphics();
virtual void createGraphicsImplementation();
virtual void setExtents(const osg::BoundingBox& bb);
const osg::BoundingBox& getExtents() const { return _extents; }
enum FocusBehaviour
{
CLICK_TO_FOCUS,
FOCUS_FOLLOWS_POINTER,
EVENT_DRIVEN_FOCUS_DISABLED
};
void setFocusBehaviour(FocusBehaviour behaviour) { _focusBehaviour = behaviour; }
FocusBehaviour getFocusBehaviour() const { return _focusBehaviour; }
/** update the focus according to events.*/
virtual void updateFocus(osg::NodeVisitor& nv);
/** set whether the widget has focus or not.*/
virtual void setHasEventFocus(bool focus);
/** get whether the widget has focus or not.*/
virtual bool getHasEventFocus() const;
virtual osg::BoundingSphere computeBound() const;
/** update any focus related graphics+state to the focused state.*/
virtual void enter();
virtual void enterImplementation();
/** update any focus related graphics+state to the unfocused state.*/
virtual void leave();
virtual void leaveImplementation();
protected:
virtual ~Widget() {}
FocusBehaviour _focusBehaviour;
bool _hasEventFocus;
bool _graphicsInitialized;
osg::BoundingBox _extents;
};
}
#endif