2014-05-16 18:38:40 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 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 <osgUI/PushButton>
|
|
|
|
#include <osgText/String>
|
|
|
|
#include <osgText/Font>
|
|
|
|
#include <osgText/Text>
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
|
|
|
using namespace osgUI;
|
|
|
|
|
|
|
|
PushButton::PushButton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-17 03:24:58 +08:00
|
|
|
PushButton::PushButton(const osgUI::PushButton& pb, const osg::CopyOp& copyop):
|
|
|
|
Widget(pb, copyop),
|
|
|
|
_text(pb._text)
|
2014-05-16 18:38:40 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PushButton::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event)
|
|
|
|
{
|
|
|
|
osgGA::GUIEventAdapter* ea = event->asGUIEventAdapter();
|
|
|
|
if (!ea) return false;
|
|
|
|
|
|
|
|
switch(ea->getEventType())
|
|
|
|
{
|
|
|
|
case(osgGA::GUIEventAdapter::PUSH):
|
2014-05-21 18:17:31 +08:00
|
|
|
if (_buttonSwitch.valid())
|
|
|
|
{
|
2014-05-22 00:15:02 +08:00
|
|
|
pressed();
|
2014-05-21 18:17:31 +08:00
|
|
|
}
|
2014-05-16 18:38:40 +08:00
|
|
|
break;
|
|
|
|
case(osgGA::GUIEventAdapter::RELEASE):
|
2014-05-21 18:17:31 +08:00
|
|
|
if (_buttonSwitch.valid())
|
|
|
|
{
|
2014-05-22 00:15:02 +08:00
|
|
|
released();
|
2014-05-21 18:17:31 +08:00
|
|
|
}
|
2014-05-16 18:38:40 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::enterImplementation()
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"PushButton enter"<<std::endl;
|
|
|
|
if (_buttonSwitch.valid()) _buttonSwitch->setSingleChildOn(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PushButton::leaveImplementation()
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"PushButton leave"<<std::endl;
|
|
|
|
if (_buttonSwitch.valid()) _buttonSwitch->setSingleChildOn(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::createGraphicsImplementation()
|
|
|
|
{
|
2014-05-22 18:02:35 +08:00
|
|
|
osg::ref_ptr<osg::Group> group = new osg::Group;
|
2014-05-16 18:38:40 +08:00
|
|
|
|
2014-05-22 18:02:35 +08:00
|
|
|
Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get();
|
2014-05-16 18:38:40 +08:00
|
|
|
|
|
|
|
|
2014-07-24 21:25:49 +08:00
|
|
|
float unFocused = 0.92;
|
|
|
|
float withFocus = 0.97;
|
2014-05-16 18:38:40 +08:00
|
|
|
|
2014-07-24 21:25:49 +08:00
|
|
|
osg::Vec4 frameColor(unFocused,unFocused,unFocused,1.0f);
|
2014-05-31 00:44:36 +08:00
|
|
|
|
|
|
|
osg::BoundingBox extents(_extents);
|
|
|
|
|
|
|
|
bool requiresFrame = (getFrameSettings() && getFrameSettings()->getShape()!=osgUI::FrameSettings::NO_FRAME);
|
|
|
|
if (requiresFrame)
|
|
|
|
{
|
|
|
|
group->addChild(style->createFrame(_extents, getFrameSettings(), frameColor));
|
|
|
|
extents.xMin() += getFrameSettings()->getLineWidth();
|
|
|
|
extents.xMax() -= getFrameSettings()->getLineWidth();
|
|
|
|
extents.yMin() += getFrameSettings()->getLineWidth();
|
|
|
|
extents.yMax() -= getFrameSettings()->getLineWidth();
|
|
|
|
}
|
|
|
|
|
2014-07-24 22:14:35 +08:00
|
|
|
_buttonSwitch = new osg::Switch;
|
2014-05-31 00:44:36 +08:00
|
|
|
_buttonSwitch->addChild(style->createPanel(extents, osg::Vec4(unFocused, unFocused,unFocused, 1.0)));
|
|
|
|
_buttonSwitch->addChild(style->createPanel(extents, osg::Vec4(withFocus,withFocus,withFocus,1.0)));
|
2014-05-22 18:02:35 +08:00
|
|
|
_buttonSwitch->setSingleChildOn(0);
|
2014-05-16 18:38:40 +08:00
|
|
|
|
2014-05-22 18:02:35 +08:00
|
|
|
group->addChild(_buttonSwitch.get());
|
2014-05-16 18:38:40 +08:00
|
|
|
|
2014-05-23 23:00:49 +08:00
|
|
|
// create label.
|
2014-05-31 00:44:36 +08:00
|
|
|
osg::ref_ptr<Node> node = style->createText(extents, getAlignmentSettings(), getTextSettings(), _text);
|
2014-05-23 23:00:49 +08:00
|
|
|
_textDrawable = dynamic_cast<osgText::Text*>(node.get());
|
|
|
|
_textDrawable->setDataVariance(osg::Object::DYNAMIC);
|
|
|
|
|
|
|
|
group->addChild(_textDrawable.get());
|
|
|
|
|
2014-05-24 02:59:16 +08:00
|
|
|
style->setupClipStateSet(_extents, getOrCreateStateSet());
|
|
|
|
|
2014-05-28 18:06:14 +08:00
|
|
|
setGraphicsSubgraph(0, group.get());
|
2014-05-16 18:38:40 +08:00
|
|
|
}
|
2014-05-22 00:15:02 +08:00
|
|
|
|
|
|
|
void PushButton::pressedImplementation()
|
|
|
|
{
|
|
|
|
_buttonSwitch->setSingleChildOn(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PushButton::releasedImplementation()
|
|
|
|
{
|
|
|
|
_buttonSwitch->setSingleChildOn(1);
|
|
|
|
}
|