2013-11-18 20:46:00 +08:00
|
|
|
/* -*-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 <osg/Geode>
|
2014-02-07 01:04:40 +08:00
|
|
|
#include <osg/ScriptEngine>
|
2013-11-18 20:46:00 +08:00
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/MatrixTransform>
|
2014-03-12 19:01:09 +08:00
|
|
|
#include <osg/ValueObject>
|
2013-11-18 20:46:00 +08:00
|
|
|
#include <osg/io_utils>
|
|
|
|
|
2014-02-07 01:32:41 +08:00
|
|
|
#include <osgGA/Widget>
|
2013-11-18 20:46:00 +08:00
|
|
|
#include <osgGA/EventVisitor>
|
2014-02-07 01:32:41 +08:00
|
|
|
#include <osgGA/GUIActionAdapter>
|
2013-11-18 20:46:00 +08:00
|
|
|
|
2014-02-07 01:32:41 +08:00
|
|
|
using namespace osgGA;
|
2013-11-18 20:46:00 +08:00
|
|
|
|
|
|
|
Widget::Widget():
|
|
|
|
_focusBehaviour(FOCUS_FOLLOWS_POINTER),
|
|
|
|
_hasEventFocus(false),
|
|
|
|
_graphicsInitialized(false)
|
|
|
|
|
|
|
|
{
|
|
|
|
setNumChildrenRequiringEventTraversal(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::Widget(const Widget& widget, const osg::CopyOp& copyop):
|
2016-06-08 17:18:23 +08:00
|
|
|
osg::Group(widget, copyop),
|
2013-11-18 20:46:00 +08:00
|
|
|
_focusBehaviour(widget._focusBehaviour),
|
|
|
|
_hasEventFocus(false),
|
|
|
|
_graphicsInitialized(false)
|
|
|
|
{
|
|
|
|
setNumChildrenRequiringEventTraversal(1);
|
|
|
|
}
|
|
|
|
|
2014-02-11 00:44:13 +08:00
|
|
|
void Widget::setExtents(const osg::BoundingBoxf& bb)
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
_extents = bb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::updateFocus(osg::NodeVisitor& nv)
|
|
|
|
{
|
2016-01-21 01:49:10 +08:00
|
|
|
osgGA::EventVisitor* ev = nv.asEventVisitor();
|
2014-02-07 01:32:41 +08:00
|
|
|
osgGA::GUIActionAdapter* aa = ev ? ev->getActionAdapter() : 0;
|
|
|
|
if (ev && aa)
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
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;
|
2014-02-07 01:32:41 +08:00
|
|
|
bool withinWidget = aa->computeIntersections(*ea, nv.getNodePath(), intersections);
|
2013-11-18 20:46:00 +08:00
|
|
|
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;
|
2014-02-07 01:32:41 +08:00
|
|
|
bool withinWidget = aa->computeIntersections(*ea, nv.getNodePath(), intersections);
|
2013-11-18 20:46:00 +08:00
|
|
|
|
|
|
|
_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()
|
2014-02-07 01:04:40 +08:00
|
|
|
{
|
|
|
|
osg::CallbackObject* co = osg::getCallbackObject(this, "enter");
|
|
|
|
if (co)
|
|
|
|
{
|
|
|
|
co->run(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enterImplementation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::enterImplementation()
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
OSG_NOTICE<<"enter()"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::leave()
|
2014-02-07 01:04:40 +08:00
|
|
|
{
|
|
|
|
osg::CallbackObject* co = osg::getCallbackObject(this, "leave");
|
|
|
|
if (co)
|
|
|
|
{
|
|
|
|
co->run(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
leaveImplementation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::leaveImplementation()
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
OSG_NOTICE<<"leave()"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::traverse(osg::NodeVisitor& nv)
|
2014-02-07 01:04:40 +08:00
|
|
|
{
|
|
|
|
osg::CallbackObject* co = osg::getCallbackObject(this, "traverse");
|
|
|
|
if (co)
|
|
|
|
{
|
2014-02-11 02:07:26 +08:00
|
|
|
// currently lua scripting takes a ref count so messes up handling of NodeVisitor's created on stack,
|
|
|
|
// so don't attempt to call the sctipt.
|
|
|
|
if (nv.referenceCount()==0)
|
|
|
|
{
|
|
|
|
traverseImplementation(nv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 01:04:40 +08:00
|
|
|
osg::Parameters inputParameters, outputParameters;
|
|
|
|
inputParameters.push_back(&nv);
|
|
|
|
co->run(this, inputParameters, outputParameters);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
traverseImplementation(nv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::traverseImplementation(osg::NodeVisitor& nv)
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
if (!_graphicsInitialized && nv.getVisitorType()!=osg::NodeVisitor::CULL_VISITOR) createGraphics();
|
|
|
|
|
2014-02-11 02:07:26 +08:00
|
|
|
|
2016-01-21 01:49:10 +08:00
|
|
|
osgGA::EventVisitor* ev = nv.asEventVisitor();
|
2014-02-07 01:32:41 +08:00
|
|
|
if (ev)
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2014-03-12 19:01:09 +08:00
|
|
|
if (handle(ev, itr->get()))
|
|
|
|
{
|
|
|
|
(*itr)->setHandled(true);
|
|
|
|
}
|
2013-11-18 20:46:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::Group::traverse(nv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 01:04:40 +08:00
|
|
|
bool Widget::handle(osgGA::EventVisitor* ev, osgGA::Event* event)
|
|
|
|
{
|
|
|
|
osg::CallbackObject* co = osg::getCallbackObject(this, "handle");
|
|
|
|
if (co)
|
|
|
|
{
|
2014-02-11 02:07:26 +08:00
|
|
|
// currently lua scripting takes a ref count so messes up handling of NodeVisitor's created on stack,
|
|
|
|
// so don't attempt to call the sctipt.
|
|
|
|
if (ev->referenceCount()==0)
|
2014-02-07 01:04:40 +08:00
|
|
|
{
|
2014-02-11 02:07:26 +08:00
|
|
|
return handleImplementation(ev, event);
|
2014-02-07 01:04:40 +08:00
|
|
|
}
|
|
|
|
|
2014-02-11 02:07:26 +08:00
|
|
|
osg::Parameters inputParameters, outputParameters;
|
|
|
|
inputParameters.push_back(ev);
|
|
|
|
inputParameters.push_back(event);
|
2014-03-12 19:01:09 +08:00
|
|
|
if (co->run(this, inputParameters, outputParameters))
|
|
|
|
{
|
|
|
|
if (outputParameters.size()>=1)
|
|
|
|
{
|
|
|
|
osg::BoolValueObject* bvo = dynamic_cast<osg::BoolValueObject*>(outputParameters[0].get());
|
|
|
|
if (bvo)
|
|
|
|
{
|
|
|
|
return bvo->getValue();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-02-07 01:04:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return handleImplementation(ev, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 17:18:23 +08:00
|
|
|
bool Widget::handleImplementation(osgGA::EventVisitor* /*ev*/, osgGA::Event* /*event*/)
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::computePositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3& localPosition) const
|
|
|
|
{
|
2014-02-07 01:32:41 +08:00
|
|
|
osgGA::GUIActionAdapter* aa = ev ? ev->getActionAdapter() : 0;
|
2013-11-18 20:46:00 +08:00
|
|
|
osgUtil::LineSegmentIntersector::Intersections intersections;
|
2014-02-07 01:32:41 +08:00
|
|
|
if (aa && aa->computeIntersections(*event, ev->getNodePath(), intersections))
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
localPosition = intersections.begin()->getLocalIntersectPoint();
|
|
|
|
|
|
|
|
return (_extents.contains(localPosition, 1e-6));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Widget::createGraphics()
|
2014-02-07 01:04:40 +08:00
|
|
|
{
|
|
|
|
osg::CallbackObject* co = osg::getCallbackObject(this, "createGraphics");
|
|
|
|
if (co)
|
|
|
|
{
|
|
|
|
co->run(this);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
createGraphicsImplementation();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::createGraphicsImplementation()
|
2013-11-18 20:46:00 +08:00
|
|
|
{
|
|
|
|
_graphicsInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::BoundingSphere Widget::computeBound() const
|
|
|
|
{
|
2014-03-07 19:09:25 +08:00
|
|
|
if (_extents.valid()) return osg::BoundingSphere(_extents);
|
|
|
|
else return osg::Group::computeBound();
|
2013-11-18 20:46:00 +08:00
|
|
|
}
|