From 3e00764649e03143fd2e9b0ce8a281a91afaa5f4 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 16 May 2014 10:38:40 +0000 Subject: [PATCH] Added initial shell of PushButton implementation --- include/osgUI/Label | 2 - include/osgUI/LineEdit | 1 - include/osgUI/PushButton | 52 +++++++++ src/osgUI/CMakeLists.txt | 2 + src/osgUI/Label.cpp | 16 ++- src/osgUI/LineEdit.cpp | 34 +++++- src/osgUI/PushButton.cpp | 106 ++++++++++++++++++ src/osgUI/Style.cpp | 26 ++++- src/osgUI/Widget.cpp | 10 +- .../serializers/osgUI/PushButton.cpp | 14 +++ 10 files changed, 243 insertions(+), 20 deletions(-) create mode 100644 include/osgUI/PushButton create mode 100644 src/osgUI/PushButton.cpp create mode 100644 src/osgWrappers/serializers/osgUI/PushButton.cpp diff --git a/include/osgUI/Label b/include/osgUI/Label index 2d5aac6bf..029d6147f 100644 --- a/include/osgUI/Label +++ b/include/osgUI/Label @@ -31,7 +31,6 @@ public: std::string& getText() { return _text; } const std::string& getText() const { return _text; } - virtual void createGraphicsImplementation(); protected: @@ -41,7 +40,6 @@ protected: // implementation detail osg::ref_ptr _textDrawable; - osg::ref_ptr _textGeode; }; } diff --git a/include/osgUI/LineEdit b/include/osgUI/LineEdit index 8591ccc3e..bb1dd8420 100644 --- a/include/osgUI/LineEdit +++ b/include/osgUI/LineEdit @@ -42,7 +42,6 @@ protected: // implementation detail osg::ref_ptr _textDrawable; - osg::ref_ptr _textGeode; }; } diff --git a/include/osgUI/PushButton b/include/osgUI/PushButton new file mode 100644 index 000000000..758698058 --- /dev/null +++ b/include/osgUI/PushButton @@ -0,0 +1,52 @@ +/* -*-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. +*/ + +#ifndef OSGUI_PUSHBUTTON +#define OSGUI_PUSHBUTTON + +#include +#include +#include + +namespace osgUI +{ + +class OSGUI_EXPORT PushButton : public osgUI::Widget +{ +public: + PushButton(); + PushButton(const PushButton& label, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); + META_Node(osgUI, PushButton); + + void setText(const std::string& text) { _text = text; dirty(); } + std::string& getText() { return _text; } + const std::string& getText() const { return _text; } + + virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event); + virtual void createGraphicsImplementation(); + virtual void enterImplementation(); + virtual void leaveImplementation(); + +protected: + virtual ~PushButton() {} + + std::string _text; + + // implementation detail + osg::ref_ptr _buttonSwitch; + osg::ref_ptr _textDrawable; +}; + +} + +#endif diff --git a/src/osgUI/CMakeLists.txt b/src/osgUI/CMakeLists.txt index a035dfe59..cb8a2dcda 100644 --- a/src/osgUI/CMakeLists.txt +++ b/src/osgUI/CMakeLists.txt @@ -12,6 +12,7 @@ SET(TARGET_H ${HEADER_PATH}/Widget ${HEADER_PATH}/Label ${HEADER_PATH}/LineEdit + ${HEADER_PATH}/PushButton ${HEADER_PATH}/Style ${HEADER_PATH}/AlignmentSettings ${HEADER_PATH}/FrameSettings @@ -22,6 +23,7 @@ SET(TARGET_SRC Widget.cpp Label.cpp LineEdit.cpp + PushButton.cpp Style.cpp AlignmentSettings.cpp FrameSettings.cpp diff --git a/src/osgUI/Label.cpp b/src/osgUI/Label.cpp index 26632d312..37c5fe05f 100644 --- a/src/osgUI/Label.cpp +++ b/src/osgUI/Label.cpp @@ -33,8 +33,18 @@ void Label::createGraphicsImplementation() { OSG_NOTICE<<"Label::createGraphicsImplementation()"<setText(_text); + _graphicsInitialized = true; + } + else + { + Widget::createGraphicsImplementation(); - Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get(); - addChild(style->createText(_extents, getAlignmentSettings(), getTextSettings(), _text)); + Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get(); + osg::ref_ptr node = style->createText(_extents, getAlignmentSettings(), getTextSettings(), _text); + _textDrawable = dynamic_cast(node.get()); + addChild(node.get()); + } } diff --git a/src/osgUI/LineEdit.cpp b/src/osgUI/LineEdit.cpp index ae69c7755..c1a635a48 100644 --- a/src/osgUI/LineEdit.cpp +++ b/src/osgUI/LineEdit.cpp @@ -16,6 +16,7 @@ #include #include #include +#include using namespace osgUI; @@ -31,6 +32,8 @@ LineEdit::LineEdit(const osgUI::LineEdit& label, const osg::CopyOp& copyop): bool LineEdit::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event) { + OSG_NOTICE<<"LineEdit::handleImplementation"<asGUIEventAdapter(); if (!ea) return false; @@ -47,6 +50,9 @@ bool LineEdit::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event _text.push_back(ea->getKey()); } dirty(); + + OSG_NOTICE<<"Key pressed : "<getKey()<setText(_text); + _graphicsInitialized = true; + } + else + { + OSG_NOTICE<<"LineEdit::createGraphicsImplementation()"<createText(_extents, getAlignmentSettings(), getTextSettings(), _text)); + Style* style = (getStyle()!=0) ? getStyle() : Style::instance().get(); + osg::ref_ptr node = style->createText(_extents, getAlignmentSettings(), getTextSettings(), _text); + _textDrawable = dynamic_cast(node.get()); + _textDrawable->setDataVariance(osg::Object::DYNAMIC); +#if 0 + osg::ref_ptr geode = new osg::Geode; + geode->addDrawable(_textDrawable.get()); + addChild(geode.get()); +#else + addChild(_textDrawable.get()); +#endif + } } diff --git a/src/osgUI/PushButton.cpp b/src/osgUI/PushButton.cpp new file mode 100644 index 000000000..c7f23cdad --- /dev/null +++ b/src/osgUI/PushButton.cpp @@ -0,0 +1,106 @@ +/* -*-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 +#include +#include +#include +#include + +using namespace osgUI; + +PushButton::PushButton() +{ +} + +PushButton::PushButton(const osgUI::PushButton& label, const osg::CopyOp& copyop): + Widget(label, copyop), + _text(label._text) +{ +} + +bool PushButton::handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event) +{ + OSG_NOTICE<<"PushButton::handleImplementation"<asGUIEventAdapter(); + if (!ea) return false; + + switch(ea->getEventType()) + { + case(osgGA::GUIEventAdapter::PUSH): + OSG_NOTICE<<"Button pressed "<setSingleChildOn(2); + break; + case(osgGA::GUIEventAdapter::RELEASE): + OSG_NOTICE<<"Button release "<setSingleChildOn(1); + break; + default: + break; + } + + return false; +} + +void PushButton::enterImplementation() +{ + OSG_NOTICE<<"PushButton enter"<setSingleChildOn(1); +} + + +void PushButton::leaveImplementation() +{ + OSG_NOTICE<<"PushButton leave"<setSingleChildOn(0); +} + +void PushButton::createGraphicsImplementation() +{ + + if (_textDrawable.valid()) + { + OSG_NOTICE<<"PushButton::createGraphicsImplementation() updating existing TextDrawable"<setText(_text); + _graphicsInitialized = true; + } + else + { + OSG_NOTICE<<"PushButton::createGraphicsImplementation()"< node = style->createText(_extents, getAlignmentSettings(), getTextSettings(), _text); + _textDrawable = dynamic_cast(node.get()); + _textDrawable->setDataVariance(osg::Object::DYNAMIC); + + addChild(_textDrawable.get()); + + _buttonSwitch = new osg::Switch; + + float unFocused = 0.7; + float withFocus = 0.8; + float pressed = 0.5; + + _buttonSwitch->addChild(style->createPanel(_extents, osg::Vec4(unFocused, unFocused,unFocused, 1.0))); + _buttonSwitch->addChild(style->createPanel(_extents, osg::Vec4(withFocus,withFocus,withFocus,1.0))); + _buttonSwitch->addChild(style->createPanel(_extents, osg::Vec4(pressed,pressed,pressed,1.0))); + _buttonSwitch->setSingleChildOn(0); + + addChild(_buttonSwitch.get()); + + } +} diff --git a/src/osgUI/Style.cpp b/src/osgUI/Style.cpp index 02341f03e..ac16cadba 100644 --- a/src/osgUI/Style.cpp +++ b/src/osgUI/Style.cpp @@ -36,7 +36,25 @@ Style::Style(const Style& style, const osg::CopyOp& copyop): osg::Node* Style::createPanel(const osg::BoundingBox& extents, const osg::Vec4& colour) { - return 0; + OSG_NOTICE<<"Creating Panel"< geometry = new osg::Geometry; + + osg::ref_ptr vertices = new osg::Vec3Array; + geometry->setVertexArray(vertices.get()); + + vertices->push_back( osg::Vec3(extents.xMin(), extents.yMin(), extents.zMin()) ); + vertices->push_back( osg::Vec3(extents.xMin(), extents.yMax(), extents.zMin()) ); + vertices->push_back( osg::Vec3(extents.xMax(), extents.yMin(), extents.zMin()) ); + vertices->push_back( osg::Vec3(extents.xMax(), extents.yMax(), extents.zMin()) ); + + osg::ref_ptr colours = new osg::Vec4Array; + geometry->setColorArray(colours, osg::Array::BIND_OVERALL); + + colours->push_back( colour ); + + geometry->addPrimitiveSet( new osg::DrawArrays(GL_TRIANGLE_STRIP, 0, 4) ); + + return geometry.release(); } osg::Node* Style::createFrame(const osg::BoundingBox& extents, const FrameSettings* frameSettings) @@ -46,12 +64,10 @@ osg::Node* Style::createFrame(const osg::BoundingBox& extents, const FrameSettin osg::Node* Style::createText(const osg::BoundingBox& extents, const AlignmentSettings* as, const TextSettings* ts, const std::string& text) { - osg::ref_ptr textGeode = new osg::Geode; osg::ref_ptr textDrawable = new osgText::Text; - textGeode->addDrawable(textDrawable.get()); - textDrawable->setText(text); + textDrawable->setPosition( osg::Vec3(extents.xMin(), extents.yMin(), extents.zMin()) ); if (ts) { @@ -65,7 +81,7 @@ osg::Node* Style::createText(const osg::BoundingBox& extents, const AlignmentSet textDrawable->setAlignment(alignmentType); } - return textGeode.release(); + return textDrawable.release(); } osg::Node* Style::createIcon(const osg::BoundingBox& extents, const std::string& filename) diff --git a/src/osgUI/Widget.cpp b/src/osgUI/Widget.cpp index 0c8df8ab0..293a7bff2 100644 --- a/src/osgUI/Widget.cpp +++ b/src/osgUI/Widget.cpp @@ -56,6 +56,8 @@ void Widget::updateFocus(osg::NodeVisitor& nv) osgGA::GUIActionAdapter* aa = ev ? ev->getActionAdapter() : 0; if (ev && aa) { + // OSG_NOTICE<<"updateFocus"<getEvents(); for(osgGA::EventQueue::Events::iterator itr = events.begin(); itr != events.end(); @@ -167,7 +169,7 @@ void Widget::enter() void Widget::enterImplementation() { - OSG_NOTICE<<"enter()"<(&nv); if (ev) { + updateFocus(nv); + // OSG_NOTICE<<"EventTraversal getHasEventFocus()="< +#include +#include +#include +#include + + +REGISTER_OBJECT_WRAPPER( PushButton, + new osgUI::PushButton, + osgUI::PushButton, + "osg::Object osg::Node osg::Group osgUI::Widget osgUI::PushButton" ) +{ + ADD_STRING_SERIALIZER( Text, std::string()); +}