1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
18#include <osg/BoundingBox>
19#include <osg/ScriptEngine>
21#include <osgGA/EventVisitor>
28class OSGUI_EXPORT Widget : public osg::Group
32 Widget(const Widget& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
33 META_Node(osgUI, Widget);
35 virtual void traverse(osg::NodeVisitor& nv);
36 virtual void traverseImplementation(osg::NodeVisitor& nv);
38 virtual bool handle(osgGA::EventVisitor* ev, osgGA::Event* event);
39 virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event);
41 typedef std::vector<osgUtil::LineSegmentIntersector::Intersection> Intersections;
42 virtual bool computeIntersections(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, Intersections& intersections, osg::Node::NodeMask traversalMask = 0xffffffff) const;
43 virtual bool computePositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3d& localPosition) const;
44 virtual bool computeExtentsPositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3d& localPosition, bool withinExtents=true) const;
48 typedef std::map<int, osg::ref_ptr<osg::Node> > GraphicsSubgraphMap;
50 /** Set the subgraph to be used to render the widget.*/
51 void setGraphicsSubgraph(int orderNum, osg::Node* node) { _graphicsSubgraphMap[orderNum] = node; _graphicsInitialized = true; }
52 /** Get the subgraph to be used to render the widget.*/
53 osg::Node* getGraphicsSubgraph(int orderNum) { GraphicsSubgraphMap::iterator itr = _graphicsSubgraphMap.find(orderNum); return (itr!=_graphicsSubgraphMap.end()) ? itr->second.get() : 0; }
54 /** Get the const subgraph to be used to render the widget.*/
55 const osg::Node* getGraphicsSubgraph(int orderNum) const { GraphicsSubgraphMap::const_iterator itr = _graphicsSubgraphMap.find(orderNum); return (itr!=_graphicsSubgraphMap.end()) ? itr->second.get() : 0; }
57 void setGraphicsSubgraphMap(const GraphicsSubgraphMap& gsm) { _graphicsSubgraphMap = gsm; _graphicsInitialized = true; }
58 GraphicsSubgraphMap& getGraphicsSubgraphMap() { return _graphicsSubgraphMap; }
59 const GraphicsSubgraphMap& getGraphicsSubgraphMap() const { return _graphicsSubgraphMap; }
61 /** Set the WidgetStateSet is used internally by Widgets to manage state that decorates the subgraph.
62 * WidgetStateSet is not serialized and is typically populated by the Widget::createGraphics() implementation,
63 * end users will not normally touoch the WidgetStateSet, use the normal Node::setStateSet() if you want to apply
64 * your own state to the Widget and it's subgraphs.*/
65 void setWidgetStateSet(osg::StateSet* stateset) { _widgetStateSet = stateset; }
66 osg::StateSet* getWidgetStateSet() { return _widgetStateSet.get(); }
67 const osg::StateSet* getWidgetStateSet() const { return _widgetStateSet.get(); }
68 osg::StateSet* getOrCreateWidgetStateSet() { if (!_widgetStateSet) _widgetStateSet = new osg::StateSet; return _widgetStateSet.get(); }
71 /** createGraphics entry method, calls either callback object named "createGraphics" or the createGraphicsImplementation() method.*/
72 virtual void createGraphics();
74 /** createGraphicsImplementation method that creates the subgraph that will render the widget and assigns it to the Widget via the Widet::setGraphicsSubgraph() method.*/
75 virtual void createGraphicsImplementation();
78 virtual void setExtents(const osg::BoundingBoxf& bb);
79 const osg::BoundingBoxf& getExtents() const { return _extents; }
81 void setStyle(Style* style) { _style = style; }
82 Style* getStyle() { return _style.get(); }
83 const Style* getStyle() const { return _style.get(); }
85 void setAlignmentSettings(AlignmentSettings* alignmentSettings) { _alignmentSettings = alignmentSettings; }
86 AlignmentSettings* getAlignmentSettings() { return _alignmentSettings.get(); }
87 const AlignmentSettings* getAlignmentSettings() const { return _alignmentSettings.get(); }
89 void setFrameSettings(FrameSettings* textSettings) { _frameSettings = textSettings; }
90 FrameSettings* getFrameSettings() { return _frameSettings.get(); }
91 const FrameSettings* getFrameSettings() const { return _frameSettings.get(); }
93 void setTextSettings(TextSettings* textSettings) { _textSettings = textSettings; }
94 TextSettings* getTextSettings() { return _textSettings.get(); }
95 const TextSettings* getTextSettings() const { return _textSettings.get(); }
97 /** set whether the widget should fill the extents of its background.*/
98 virtual void setAutoFillBackground(bool enabled) { _autoFillBackground = enabled; }
99 /** get whether the widget should fill the extents of its background.*/
100 virtual bool getAutoFillBackground() const { return _autoFillBackground; }
102 /** set the visibility of the widget.*/
103 virtual void setVisible(bool visible) { _visible = visible; }
104 /** get the visibility of the widget.*/
105 virtual bool getVisible() const { return _visible; }
107 /** set whether the widget is enabled for user interaction.*/
108 virtual void setEnabled(bool enabled) { _enabled = enabled; }
109 /** get whether the widget is enabled for user interaction.*/
110 virtual bool getEnabled() const { return _enabled; }
115 FOCUS_FOLLOWS_POINTER,
116 EVENT_DRIVEN_FOCUS_DISABLED
119 void setFocusBehaviour(FocusBehaviour behaviour) { _focusBehaviour = behaviour; }
120 FocusBehaviour getFocusBehaviour() const { return _focusBehaviour; }
122 /** update the focus according to events.*/
123 virtual void updateFocus(osg::NodeVisitor& nv);
125 /** set whether the widget has focus or not.*/
126 virtual void setHasEventFocus(bool focus);
128 /** get whether the widget has focus or not.*/
129 virtual bool getHasEventFocus() const;
132 /** invoke all callbacks with specified names providing input and output parameters.*/
133 bool runCallbacks(const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters) { return osg::runNamedCallbackObjects(this, name, inputParameters, outputParameters); }
135 /** invoke all callbacks with specified names without any specified input or output parameters.*/
136 bool runCallbacks(const std::string& name) { osg::Parameters inputParameters, outputParameters; return osg::runNamedCallbackObjects(this, name, inputParameters, outputParameters); }
139 /** Compute the bounding sphere of the widget.*/
140 virtual osg::BoundingSphere computeBound() const;
142 /** update any focus related graphics+state to the focused state.*/
143 virtual void enter();
144 virtual void enterImplementation();
146 /** update any focus related graphics+state to the unfocused state.*/
147 virtual void leave();
148 virtual void leaveImplementation();
150 /** resize all GLObjectBuffers.*/
151 virtual void resizeGLObjectBuffers(unsigned int maxSize);
152 /** resize all GLObjectBuffers.*/
153 virtual void releaseGLObjects(osg::State* = 0) const;
158 FocusBehaviour _focusBehaviour;
160 bool _graphicsInitialized;
162 GraphicsSubgraphMap _graphicsSubgraphMap;
163 osg::ref_ptr<osg::StateSet> _widgetStateSet;
165 osg::BoundingBoxf _extents;
167 osg::ref_ptr<Style> _style;
169 osg::ref_ptr<AlignmentSettings> _alignmentSettings;
170 osg::ref_ptr<FrameSettings> _frameSettings;
171 osg::ref_ptr<TextSettings> _textSettings;
172 bool _autoFillBackground;