Widget 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef OSGUI_WIDGET
  14. #define OSGUI_WIDGET
  15. #include <osg/Group>
  16. #include <osg/BoundingBox>
  17. #include <osg/ScriptEngine>
  18. #include <osgGA/Event>
  19. #include <osgGA/EventVisitor>
  20. #include <osgUI/Style>
  21. namespace osgUI
  22. {
  23. class OSGUI_EXPORT Widget : public osg::Group
  24. {
  25. public:
  26. Widget();
  27. Widget(const Widget& widget, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  28. META_Node(osgUI, Widget);
  29. virtual void traverse(osg::NodeVisitor& nv);
  30. virtual void traverseImplementation(osg::NodeVisitor& nv);
  31. virtual bool handle(osgGA::EventVisitor* ev, osgGA::Event* event);
  32. virtual bool handleImplementation(osgGA::EventVisitor* ev, osgGA::Event* event);
  33. typedef std::vector<osgUtil::LineSegmentIntersector::Intersection> Intersections;
  34. virtual bool computeIntersections(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, Intersections& intersections, osg::Node::NodeMask traversalMask = 0xffffffff) const;
  35. virtual bool computePositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3d& localPosition) const;
  36. virtual bool computeExtentsPositionInLocalCoordinates(osgGA::EventVisitor* ev, osgGA::GUIEventAdapter* event, osg::Vec3d& localPosition, bool withinExtents=true) const;
  37. virtual void dirty();
  38. typedef std::map<int, osg::ref_ptr<osg::Node> > GraphicsSubgraphMap;
  39. /** Set the subgraph to be used to render the widget.*/
  40. void setGraphicsSubgraph(int orderNum, osg::Node* node) { _graphicsSubgraphMap[orderNum] = node; _graphicsInitialized = true; }
  41. /** Get the subgraph to be used to render the widget.*/
  42. osg::Node* getGraphicsSubgraph(int orderNum) { GraphicsSubgraphMap::iterator itr = _graphicsSubgraphMap.find(orderNum); return (itr!=_graphicsSubgraphMap.end()) ? itr->second.get() : 0; }
  43. /** Get the const subgraph to be used to render the widget.*/
  44. const osg::Node* getGraphicsSubgraph(int orderNum) const { GraphicsSubgraphMap::const_iterator itr = _graphicsSubgraphMap.find(orderNum); return (itr!=_graphicsSubgraphMap.end()) ? itr->second.get() : 0; }
  45. void setGraphicsSubgraphMap(const GraphicsSubgraphMap& gsm) { _graphicsSubgraphMap = gsm; _graphicsInitialized = true; }
  46. GraphicsSubgraphMap& getGraphicsSubgraphMap() { return _graphicsSubgraphMap; }
  47. const GraphicsSubgraphMap& getGraphicsSubgraphMap() const { return _graphicsSubgraphMap; }
  48. /** Set the WidgetStateSet is used internally by Widgets to manage state that decorates the subgraph.
  49. * WidgetStateSet is not serialized and is typically populated by the Widget::createGraphics() implementation,
  50. * end users will not normally touoch the WidgetStateSet, use the normal Node::setStateSet() if you want to apply
  51. * your own state to the Widget and it's subgraphs.*/
  52. void setWidgetStateSet(osg::StateSet* stateset) { _widgetStateSet = stateset; }
  53. osg::StateSet* getWidgetStateSet() { return _widgetStateSet.get(); }
  54. const osg::StateSet* getWidgetStateSet() const { return _widgetStateSet.get(); }
  55. osg::StateSet* getOrCreateWidgetStateSet() { if (!_widgetStateSet) _widgetStateSet = new osg::StateSet; return _widgetStateSet.get(); }
  56. /** createGraphics entry method, calls either callback object named "createGraphics" or the createGraphicsImplementation() method.*/
  57. virtual void createGraphics();
  58. /** createGraphicsImplementation method that creates the subgraph that will render the widget and assigns it to the Widget via the Widet::setGraphicsSubgraph() method.*/
  59. virtual void createGraphicsImplementation();
  60. virtual void setExtents(const osg::BoundingBoxf& bb);
  61. const osg::BoundingBoxf& getExtents() const { return _extents; }
  62. void setStyle(Style* style) { _style = style; }
  63. Style* getStyle() { return _style.get(); }
  64. const Style* getStyle() const { return _style.get(); }
  65. void setAlignmentSettings(AlignmentSettings* alignmentSettings) { _alignmentSettings = alignmentSettings; }
  66. AlignmentSettings* getAlignmentSettings() { return _alignmentSettings.get(); }
  67. const AlignmentSettings* getAlignmentSettings() const { return _alignmentSettings.get(); }
  68. void setFrameSettings(FrameSettings* textSettings) { _frameSettings = textSettings; }
  69. FrameSettings* getFrameSettings() { return _frameSettings.get(); }
  70. const FrameSettings* getFrameSettings() const { return _frameSettings.get(); }
  71. void setTextSettings(TextSettings* textSettings) { _textSettings = textSettings; }
  72. TextSettings* getTextSettings() { return _textSettings.get(); }
  73. const TextSettings* getTextSettings() const { return _textSettings.get(); }
  74. /** set whether the widget should fill the extents of its background.*/
  75. virtual void setAutoFillBackground(bool enabled) { _autoFillBackground = enabled; }
  76. /** get whether the widget should fill the extents of its background.*/
  77. virtual bool getAutoFillBackground() const { return _autoFillBackground; }
  78. /** set the visibility of the widget.*/
  79. virtual void setVisible(bool visible) { _visible = visible; }
  80. /** get the visibility of the widget.*/
  81. virtual bool getVisible() const { return _visible; }
  82. /** set whether the widget is enabled for user interaction.*/
  83. virtual void setEnabled(bool enabled) { _enabled = enabled; }
  84. /** get whether the widget is enabled for user interaction.*/
  85. virtual bool getEnabled() const { return _enabled; }
  86. enum FocusBehaviour
  87. {
  88. CLICK_TO_FOCUS,
  89. FOCUS_FOLLOWS_POINTER,
  90. EVENT_DRIVEN_FOCUS_DISABLED
  91. };
  92. void setFocusBehaviour(FocusBehaviour behaviour) { _focusBehaviour = behaviour; }
  93. FocusBehaviour getFocusBehaviour() const { return _focusBehaviour; }
  94. /** update the focus according to events.*/
  95. virtual void updateFocus(osg::NodeVisitor& nv);
  96. /** set whether the widget has focus or not.*/
  97. virtual void setHasEventFocus(bool focus);
  98. /** get whether the widget has focus or not.*/
  99. virtual bool getHasEventFocus() const;
  100. /** invoke all callbacks with specified names providing input and output parameters.*/
  101. bool runCallbacks(const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters) { return osg::runNamedCallbackObjects(this, name, inputParameters, outputParameters); }
  102. /** invoke all callbacks with specified names without any specified input or output parameters.*/
  103. bool runCallbacks(const std::string& name) { osg::Parameters inputParameters, outputParameters; return osg::runNamedCallbackObjects(this, name, inputParameters, outputParameters); }
  104. /** Compute the bounding sphere of the widget.*/
  105. virtual osg::BoundingSphere computeBound() const;
  106. /** update any focus related graphics+state to the focused state.*/
  107. virtual void enter();
  108. virtual void enterImplementation();
  109. /** update any focus related graphics+state to the unfocused state.*/
  110. virtual void leave();
  111. virtual void leaveImplementation();
  112. /** resize all GLObjectBuffers.*/
  113. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  114. /** resize all GLObjectBuffers.*/
  115. virtual void releaseGLObjects(osg::State* = 0) const;
  116. protected:
  117. virtual ~Widget() {}
  118. FocusBehaviour _focusBehaviour;
  119. bool _hasEventFocus;
  120. bool _graphicsInitialized;
  121. GraphicsSubgraphMap _graphicsSubgraphMap;
  122. osg::ref_ptr<osg::StateSet> _widgetStateSet;
  123. osg::BoundingBoxf _extents;
  124. osg::ref_ptr<Style> _style;
  125. osg::ref_ptr<AlignmentSettings> _alignmentSettings;
  126. osg::ref_ptr<FrameSettings> _frameSettings;
  127. osg::ref_ptr<TextSettings> _textSettings;
  128. bool _autoFillBackground;
  129. bool _visible;
  130. bool _enabled;
  131. };
  132. }
  133. #endif