openscenegraph
osgUI/Widget
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2014 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSGUI_WIDGET
15#define OSGUI_WIDGET
16
17#include <osg/Group>
18#include <osg/BoundingBox>
19#include <osg/ScriptEngine>
20#include <osgGA/Event>
21#include <osgGA/EventVisitor>
22
23#include <osgUI/Style>
24
25namespace osgUI
26{
27
28class OSGUI_EXPORT Widget : public osg::Group
29{
30public:
31 Widget();
32 Widget(const Widget& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
33 META_Node(osgUI, Widget);
34
35 virtual void traverse(osg::NodeVisitor& nv);
36 virtual void traverseImplementation(osg::NodeVisitor& nv);
37
38 virtual bool handle(osgGA::EventVisitor* ev, osgGA::Event* event);
39 virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event);
40
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;
45
46 virtual void dirty();
47
48 typedef std::map<int, osg::ref_ptr<osg::Node> > GraphicsSubgraphMap;
49
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; }
56
57 void setGraphicsSubgraphMap(const GraphicsSubgraphMap& gsm) { _graphicsSubgraphMap = gsm; _graphicsInitialized = true; }
58 GraphicsSubgraphMap& getGraphicsSubgraphMap() { return _graphicsSubgraphMap; }
59 const GraphicsSubgraphMap& getGraphicsSubgraphMap() const { return _graphicsSubgraphMap; }
60
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(); }
69
70
71 /** createGraphics entry method, calls either callback object named "createGraphics" or the createGraphicsImplementation() method.*/
72 virtual void createGraphics();
73
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();
76
77
78 virtual void setExtents(const osg::BoundingBoxf& bb);
79 const osg::BoundingBoxf& getExtents() const { return _extents; }
80
81 void setStyle(Style* style) { _style = style; }
82 Style* getStyle() { return _style.get(); }
83 const Style* getStyle() const { return _style.get(); }
84
85 void setAlignmentSettings(AlignmentSettings* alignmentSettings) { _alignmentSettings = alignmentSettings; }
86 AlignmentSettings* getAlignmentSettings() { return _alignmentSettings.get(); }
87 const AlignmentSettings* getAlignmentSettings() const { return _alignmentSettings.get(); }
88
89 void setFrameSettings(FrameSettings* textSettings) { _frameSettings = textSettings; }
90 FrameSettings* getFrameSettings() { return _frameSettings.get(); }
91 const FrameSettings* getFrameSettings() const { return _frameSettings.get(); }
92
93 void setTextSettings(TextSettings* textSettings) { _textSettings = textSettings; }
94 TextSettings* getTextSettings() { return _textSettings.get(); }
95 const TextSettings* getTextSettings() const { return _textSettings.get(); }
96
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; }
101
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; }
106
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; }
111
112 enum FocusBehaviour
113 {
114 CLICK_TO_FOCUS,
115 FOCUS_FOLLOWS_POINTER,
116 EVENT_DRIVEN_FOCUS_DISABLED
117 };
118
119 void setFocusBehaviour(FocusBehaviour behaviour) { _focusBehaviour = behaviour; }
120 FocusBehaviour getFocusBehaviour() const { return _focusBehaviour; }
121
122 /** update the focus according to events.*/
123 virtual void updateFocus(osg::NodeVisitor& nv);
124
125 /** set whether the widget has focus or not.*/
126 virtual void setHasEventFocus(bool focus);
127
128 /** get whether the widget has focus or not.*/
129 virtual bool getHasEventFocus() const;
130
131
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); }
134
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); }
137
138
139 /** Compute the bounding sphere of the widget.*/
140 virtual osg::BoundingSphere computeBound() const;
141
142 /** update any focus related graphics+state to the focused state.*/
143 virtual void enter();
144 virtual void enterImplementation();
145
146 /** update any focus related graphics+state to the unfocused state.*/
147 virtual void leave();
148 virtual void leaveImplementation();
149
150 /** resize all GLObjectBuffers.*/
151 virtual void resizeGLObjectBuffers(unsigned int maxSize);
152 /** resize all GLObjectBuffers.*/
153 virtual void releaseGLObjects(osg::State* = 0) const;
154
155protected:
156 virtual ~Widget() {}
157
158 FocusBehaviour _focusBehaviour;
159 bool _hasEventFocus;
160 bool _graphicsInitialized;
161
162 GraphicsSubgraphMap _graphicsSubgraphMap;
163 osg::ref_ptr<osg::StateSet> _widgetStateSet;
164
165 osg::BoundingBoxf _extents;
166
167 osg::ref_ptr<Style> _style;
168
169 osg::ref_ptr<AlignmentSettings> _alignmentSettings;
170 osg::ref_ptr<FrameSettings> _frameSettings;
171 osg::ref_ptr<TextSettings> _textSettings;
172 bool _autoFillBackground;
173
174 bool _visible;
175 bool _enabled;
176};
177
178}
179
180#endif