WindowManager
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 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.
*/
// Code by: Jeremy Moles (cubicool) 2007-2008
#ifndef OSGWIDGET_WINDOW_MANAGER
#define OSGWIDGET_WINDOW_MANAGER
#include <osg/Switch>
#include <osg/Uniform>
#include <osg/Drawable>
#include <osgGA/GUIEventAdapter>
#include <osgUtil/LineSegmentIntersector>
#include <osgViewer/View>
#include <osgWidget/ScriptEngine>
#include <osgWidget/StyleManager>
#include <osgWidget/Window>
namespace osgWidget {
// TODO: It should be possible to use something other than osgWidget/ViewerEventHandlers
// to handle all of these events. In fact, I need to create an SDL example showing this.
// A private typedef that we use for pickAtXY() below.
typedef osgUtil::LineSegmentIntersector::Intersections Intersections;
// A WindowManager performs pointer interaction with the topmost (highest Z) Widget,
// and performs keyboard input on the currently focused Window->Widget.
class OSGWIDGET_EXPORT WindowManager: public osg::Switch, public UIObjectParent<Window> {
public:
enum WmFlags {
WM_USE_LUA = 0x00000001,
WM_USE_PYTHON = 0x00000002,
WM_USE_RENDERBINS = 0x00000004,
WM_PICK_DEBUG = 0x00000008
};
enum PointerDirection {
PD_NONE = 0x00000000,
PD_LEFT = 0x00000001,
PD_RIGHT = 0x00000002,
PD_UP = 0x00000004,
PD_DOWN = 0x00000008
};
enum PointerFocusMode {
PFM_FOCUS = 0x00000000,
PFM_UNFOCUS = 0x00000001,
PFM_SLOPPY = 0x00000002
};
public:
META_Object(osgWidget, WindowManager);
WindowManager(
osgViewer::View* = 0,
point_type = 0.0f,
point_type = 0.0f,
unsigned int = 0,
unsigned int = 0
);
WindowManager(const WindowManager&, const osg::CopyOp&);
virtual ~WindowManager();
// A static method that will set both the _widget and _window data of an Event
// reference from a passed-in Interface.
static void setEventFromInterface(Event&, EventInterface*);
// A static template method that will iterate over a container and return a
// properly formed EventInterface*.
template<typename T>
static EventInterface* getFirstEventInterface(T&, Event&);
bool pickAtXY (float, float, WidgetList&);
bool setFocused (Window*);
void setPointerXY (float, float);
void setStyleManager (StyleManager*);
void resizeAllWindows (bool = true);
XYCoord windowXY (double, double) const;
XYCoord localXY (double, double) const;
// Methods all called by the ViewerEventHandlers::MouseHandler object, or
// by some customer caller of your own. Examples of this to come...
bool pointerMove (float, float);
bool pointerDrag (float, float);
bool mouseScroll (float, float);
osg::Camera* createParentOrthoCamera();
unsigned int getNodeMask() const {
return _nodeMask;
}
point_type getWidth() const {
return _width;
}
point_type getHeight() const {
return _height;
}
bool isUsingLua() const {
return (_flags & WM_USE_LUA) != 0;
}
bool isUsingPython() const {
return (_flags & WM_USE_PYTHON) != 0;
}
bool isUsingRenderBins() const {
return (_flags & WM_USE_RENDERBINS) != 0;
}
int getMouseKeysDown() const {
int flag = 0;
flag |= _leftDown ? osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON : 0;
flag |= _middleDown ? osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON: 0;
flag |= _rightDown ? osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON : 0;
return flag;
}
ScriptEngine* getLuaEngine() {
return _lua.get();
}
const ScriptEngine* getLuaEngine() const {
return _lua.get();
}
ScriptEngine* getPythonEngine() {
return _python.get();
}
const ScriptEngine* getPythonEngine() const {
return _python.get();
}
StyleManager* getStyleManager() {
return _styleManager.get();
}
const StyleManager* getStyleManager() const {
return _styleManager.get();
}
PointerDirection getPointerVerticalDirection() const {
return _lastVertical;
}
PointerDirection getPointerHorizontalDirection() const {
return _lastHorizontal;
}
PointerFocusMode getPointerFocusMode() const {
return _focusMode;
}
int getPointerDirectionVector() const {
return _lastVertical | _lastHorizontal;
}
bool isPointerMovingUp() const {
return _lastVertical == PD_UP;
}
bool isPointerMovingDown() const {
return _lastVertical == PD_DOWN;
}
bool isPointerMovingLeft() const {
return _lastHorizontal == PD_LEFT;
}
bool isPointerMovingRight() const {
return _lastHorizontal == PD_RIGHT;
}
bool isPointerMovingVertically() const {
return _lastVertical != PD_NONE;
}
bool isPointerMovingHorizontally() const {
return _lastHorizontal != PD_NONE;
}
bool isLeftMouseButtonDown() const {
return _leftDown;
}
bool isMiddleMouseButtonDown() const {
return _middleDown;
}
bool isRightMouseButtonDown() const {
return _rightDown;
}
bool isMouseScrollingUp() const {
return _scrolling == osgGA::GUIEventAdapter::SCROLL_UP;
}
bool isMouseScrollingDown() const {
return _scrolling == osgGA::GUIEventAdapter::SCROLL_DOWN;
}
bool setFocusedByName(const std::string& name) {
return setFocused(getByName(name));
}
void setScrollingMotion(osgGA::GUIEventAdapter::ScrollingMotion sm) {
_scrolling = sm;
}
void setPointerFocusMode(PointerFocusMode pfm) {
_focusMode = pfm;
}
void setWidth(point_type w) {
_width = w;
}
void setHeight(point_type h) {
_height = h;
}
void setSize(point_type w, point_type h) {
_width = w;
_height = h;
}
void setWindowSize(point_type w, point_type h) {
_windowWidth = w;
_windowHeight = h;
}
// Wrappers around the real calls. These only pertains to mouse buttons,
// particularly 3-button mice, although there are other more generic
// "pointer" API methods.
bool mousePushedLeft(float x, float y) {
return _handleMousePushed(x, y, _leftDown);
}
bool mousePushedMiddle(float x, float y) {
return _handleMousePushed(x, y, _middleDown);
}
bool mousePushedRight(float x, float y) {
return _handleMousePushed(x, y, _rightDown);
}
bool mouseReleasedLeft(float x, float y) {
return _handleMouseReleased(x, y, _leftDown);
}
bool mouseReleasedMiddle(float x, float y) {
return _handleMouseReleased(x, y, _middleDown);
}
bool mouseReleasedRight(float x, float y) {
return _handleMouseReleased(x, y, _rightDown);
}
// Keyboards wrappers, as above; takes the key and key's mask code, which
// can be compared to osgGA::GUIEventAdapter::{KeySymbol,KeyModMask}.
bool keyDown (int, int);
bool keyUp (int, int);
osgViewer::View* getView() { return _view; }
const osgViewer::View* getView() const { return _view; }
private:
// A functor used to sort the Windows by their Z component in descending order.
struct WindowZCompare
{
bool operator()(const ptr_type& x, const ptr_type& y) {
return x.get()->getZ() > y.get()->getZ();
}
};
// A functor used to sort the Windows by their BinNum component in descending order.
struct WindowBinNumberCompare
{
bool operator()(const ptr_type& x, const ptr_type& y) {
return
x.get()->getOrCreateStateSet()->getBinNumber() >
y.get()->getOrCreateStateSet()->getBinNumber()
;
}
};
point_type _width;
point_type _height;
point_type _windowWidth;
point_type _windowHeight;
unsigned int _flags;
unsigned int _nodeMask;
osgViewer::View* _view;
float _lastX;
float _lastY;
EventInterface* _lastEvent;
EventInterface* _lastPush;
PointerDirection _lastVertical;
PointerDirection _lastHorizontal;
PointerFocusMode _focusMode;
bool _leftDown;
bool _middleDown;
bool _rightDown;
osgGA::GUIEventAdapter::ScrollingMotion _scrolling;
osg::ref_ptr<ScriptEngine> _lua;
osg::ref_ptr<ScriptEngine> _python;
osg::ref_ptr<StyleManager> _styleManager;
osg::observer_ptr<Widget> _widget;
osg::observer_ptr<Window> _focused;
osg::observer_ptr<Window> _pickWindow;
void childInserted (unsigned int);
void childRemoved (unsigned int, unsigned int);
bool _handleMousePushed (float, float, bool&);
bool _handleMouseReleased (float, float, bool&);
bool _handleMouseScrolled (float, float, bool = false);
void _getPointerXYDiff (float&, float&);
void _updatePickWindow (const WidgetList*, point_type, point_type);
};
// We use a template here because the container could be a list or a vector; or something
// else that supports iteration!
template<typename T>
EventInterface* WindowManager::getFirstEventInterface(T& container, Event& ev) {
if(!container.size()) return 0;
// See if we can find a Widget that responds to this event...
for(typename T::iterator i = container.begin(); i != container.end(); i++) {
Widget* widget = i->get();
// If so, set the _widget/_window members and return it.
if(widget->getEventMask() & ev.type) {
ev._window = widget->getParent();
ev._widget = widget;
return widget;
}
}
// If we can't find a Widget that will accept this event, try and recurse all
// of the parent Windows and find one that can.
WindowList windowList;
Window* parent = container.back()->getParent();
if(parent) {
parent->getParentList(windowList);
// A WindowList from getParentList includes the Window the method was called
// on, and the entire tree of parentage.
for(WindowList::iterator i = windowList.begin(); i != windowList.end(); i++) {
Window* window = i->get();
if(window->getEventMask() & ev.type) {
ev._window = window;
return window;
}
}
}
return 0;
}
}
#endif
Drawable
GUIEventAdapter
LineSegmentIntersector
StyleManager
Switch
Uniform
Window
osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON
@ RIGHT_MOUSE_BUTTON
Definition GUIEventAdapter:88
osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON
@ MIDDLE_MOUSE_BUTTON
Definition GUIEventAdapter:87
osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON
@ LEFT_MOUSE_BUTTON
Definition GUIEventAdapter:86
osgGA::GUIEventAdapter::ScrollingMotion
ScrollingMotion
Definition GUIEventAdapter:360
osgGA::GUIEventAdapter::SCROLL_DOWN
@ SCROLL_DOWN
Definition GUIEventAdapter:365
osgGA::GUIEventAdapter::SCROLL_UP
@ SCROLL_UP
Definition GUIEventAdapter:364
osgUtil::LineSegmentIntersector::Intersections
std::multiset< Intersection > Intersections
Definition LineSegmentIntersector:74
osgViewer::View
Definition osgViewer/View:76
osgWidget::EventInterface
Definition EventInterface:268
osgWidget::EventInterface::getEventMask
unsigned int getEventMask() const
Definition EventInterface:313
osgWidget::Event
Definition EventInterface:61
osgWidget::Event::_window
Window * _window
Definition EventInterface:145
osgWidget::Event::type
EventType type
Definition EventInterface:63
osgWidget::Event::_widget
Widget * _widget
Definition EventInterface:146
osgWidget::ScriptEngine
Definition osgWidget/ScriptEngine:27
osgWidget::StyleManager
Definition StyleManager:85
osgWidget::UIObjectParent
Definition UIObjectParent:26
osgWidget::UIObjectParent< Window >::getByName
object_type * getByName(const std::string &name)
Definition UIObjectParent:55
osgWidget::Widget
Definition osgWidget/Widget:35
osgWidget::Widget::getParent
Window * getParent()
Definition osgWidget/Widget:297
osgWidget::WindowManager::windowXY
XYCoord windowXY(double, double) const
osgWidget::WindowManager::pickAtXY
bool pickAtXY(float, float, WidgetList &)
osgWidget::WindowManager::mouseReleasedMiddle
bool mouseReleasedMiddle(float x, float y)
Definition WindowManager:270
osgWidget::WindowManager::setWindowSize
void setWindowSize(point_type w, point_type h)
Definition WindowManager:246
osgWidget::WindowManager::getPointerFocusMode
PointerFocusMode getPointerFocusMode() const
Definition WindowManager:169
osgWidget::WindowManager::isPointerMovingVertically
bool isPointerMovingVertically() const
Definition WindowManager:193
osgWidget::WindowManager::getPythonEngine
ScriptEngine * getPythonEngine()
Definition WindowManager:145
osgWidget::WindowManager::getPointerVerticalDirection
PointerDirection getPointerVerticalDirection() const
Definition WindowManager:161
osgWidget::WindowManager::setFocused
bool setFocused(Window *)
osgWidget::WindowManager::isPointerMovingLeft
bool isPointerMovingLeft() const
Definition WindowManager:185
osgWidget::WindowManager::resizeAllWindows
void resizeAllWindows(bool=true)
osgWidget::WindowManager::mousePushedLeft
bool mousePushedLeft(float x, float y)
Definition WindowManager:254
osgWidget::WindowManager::getPythonEngine
const ScriptEngine * getPythonEngine() const
Definition WindowManager:149
osgWidget::WindowManager::PointerDirection
PointerDirection
Definition WindowManager:48
osgWidget::WindowManager::PD_LEFT
@ PD_LEFT
Definition WindowManager:50
osgWidget::WindowManager::PD_RIGHT
@ PD_RIGHT
Definition WindowManager:51
osgWidget::WindowManager::PD_DOWN
@ PD_DOWN
Definition WindowManager:53
osgWidget::WindowManager::PD_UP
@ PD_UP
Definition WindowManager:52
osgWidget::WindowManager::PD_NONE
@ PD_NONE
Definition WindowManager:49
osgWidget::WindowManager::getLuaEngine
ScriptEngine * getLuaEngine()
Definition WindowManager:137
osgWidget::WindowManager::isMouseScrollingUp
bool isMouseScrollingUp() const
Definition WindowManager:213
osgWidget::WindowManager::mousePushedRight
bool mousePushedRight(float x, float y)
Definition WindowManager:262
osgWidget::WindowManager::isUsingRenderBins
bool isUsingRenderBins() const
Definition WindowManager:123
osgWidget::WindowManager::getFirstEventInterface
static EventInterface * getFirstEventInterface(T &, Event &)
Definition WindowManager:348
osgWidget::WindowManager::createParentOrthoCamera
osg::Camera * createParentOrthoCamera()
osgWidget::WindowManager::isRightMouseButtonDown
bool isRightMouseButtonDown() const
Definition WindowManager:209
osgWidget::WindowManager::getView
osgViewer::View * getView()
Definition WindowManager:283
osgWidget::WindowManager::isMiddleMouseButtonDown
bool isMiddleMouseButtonDown() const
Definition WindowManager:205
osgWidget::WindowManager::WindowManager
WindowManager(osgViewer::View *=0, point_type=0.0f, point_type=0.0f, unsigned int=0, unsigned int=0)
osgWidget::WindowManager::getView
const osgViewer::View * getView() const
Definition WindowManager:284
osgWidget::WindowManager::setPointerXY
void setPointerXY(float, float)
osgWidget::WindowManager::pointerMove
bool pointerMove(float, float)
osgWidget::WindowManager::setStyleManager
void setStyleManager(StyleManager *)
osgWidget::WindowManager::isLeftMouseButtonDown
bool isLeftMouseButtonDown() const
Definition WindowManager:201
osgWidget::WindowManager::setEventFromInterface
static void setEventFromInterface(Event &, EventInterface *)
osgWidget::WindowManager::setWidth
void setWidth(point_type w)
Definition WindowManager:233
osgWidget::WindowManager::setFocusedByName
bool setFocusedByName(const std::string &name)
Definition WindowManager:221
osgWidget::WindowManager::PointerFocusMode
PointerFocusMode
Definition WindowManager:56
osgWidget::WindowManager::PFM_UNFOCUS
@ PFM_UNFOCUS
Definition WindowManager:58
osgWidget::WindowManager::PFM_FOCUS
@ PFM_FOCUS
Definition WindowManager:57
osgWidget::WindowManager::PFM_SLOPPY
@ PFM_SLOPPY
Definition WindowManager:59
osgWidget::WindowManager::localXY
XYCoord localXY(double, double) const
osgWidget::WindowManager::~WindowManager
virtual ~WindowManager()
osgWidget::WindowManager::getMouseKeysDown
int getMouseKeysDown() const
Definition WindowManager:127
osgWidget::WindowManager::isUsingLua
bool isUsingLua() const
Definition WindowManager:115
osgWidget::WindowManager::keyUp
bool keyUp(int, int)
osgWidget::WindowManager::isUsingPython
bool isUsingPython() const
Definition WindowManager:119
osgWidget::WindowManager::setScrollingMotion
void setScrollingMotion(osgGA::GUIEventAdapter::ScrollingMotion sm)
Definition WindowManager:225
osgWidget::WindowManager::META_Object
META_Object(osgWidget, WindowManager)
osgWidget::WindowManager::setSize
void setSize(point_type w, point_type h)
Definition WindowManager:241
osgWidget::WindowManager::isPointerMovingRight
bool isPointerMovingRight() const
Definition WindowManager:189
osgWidget::WindowManager::getStyleManager
const StyleManager * getStyleManager() const
Definition WindowManager:157
osgWidget::WindowManager::isMouseScrollingDown
bool isMouseScrollingDown() const
Definition WindowManager:217
osgWidget::WindowManager::pointerDrag
bool pointerDrag(float, float)
osgWidget::WindowManager::getPointerDirectionVector
int getPointerDirectionVector() const
Definition WindowManager:173
osgWidget::WindowManager::mouseReleasedLeft
bool mouseReleasedLeft(float x, float y)
Definition WindowManager:266
osgWidget::WindowManager::mousePushedMiddle
bool mousePushedMiddle(float x, float y)
Definition WindowManager:258
osgWidget::WindowManager::getStyleManager
StyleManager * getStyleManager()
Definition WindowManager:153
osgWidget::WindowManager::isPointerMovingDown
bool isPointerMovingDown() const
Definition WindowManager:181
osgWidget::WindowManager::mouseReleasedRight
bool mouseReleasedRight(float x, float y)
Definition WindowManager:274
osgWidget::WindowManager::keyDown
bool keyDown(int, int)
osgWidget::WindowManager::WmFlags
WmFlags
Definition WindowManager:41
osgWidget::WindowManager::WM_USE_RENDERBINS
@ WM_USE_RENDERBINS
Definition WindowManager:44
osgWidget::WindowManager::WM_USE_LUA
@ WM_USE_LUA
Definition WindowManager:42
osgWidget::WindowManager::WM_PICK_DEBUG
@ WM_PICK_DEBUG
Definition WindowManager:45
osgWidget::WindowManager::WM_USE_PYTHON
@ WM_USE_PYTHON
Definition WindowManager:43
osgWidget::WindowManager::setHeight
void setHeight(point_type h)
Definition WindowManager:237
osgWidget::WindowManager::isPointerMovingUp
bool isPointerMovingUp() const
Definition WindowManager:177
osgWidget::WindowManager::getLuaEngine
const ScriptEngine * getLuaEngine() const
Definition WindowManager:141
osgWidget::WindowManager::setPointerFocusMode
void setPointerFocusMode(PointerFocusMode pfm)
Definition WindowManager:229
osgWidget::WindowManager::WindowManager
WindowManager(const WindowManager &, const osg::CopyOp &)
osgWidget::WindowManager::getHeight
point_type getHeight() const
Definition WindowManager:111
osgWidget::WindowManager::mouseScroll
bool mouseScroll(float, float)
osgWidget::WindowManager::getNodeMask
unsigned int getNodeMask() const
Definition WindowManager:103
osgWidget::WindowManager::getPointerHorizontalDirection
PointerDirection getPointerHorizontalDirection() const
Definition WindowManager:165
osgWidget::WindowManager::isPointerMovingHorizontally
bool isPointerMovingHorizontally() const
Definition WindowManager:197
osgWidget::WindowManager::getWidth
point_type getWidth() const
Definition WindowManager:107
osgWidget::Window
Definition Window:44
osgWidget::Window::getParentList
void getParentList(WindowList &) const
osg::Camera
Definition Camera:45
osg::CopyOp
Definition CopyOp:41
osg::Switch
Definition Switch:27
osgWidget
Definition Box:21
osgWidget::WindowList
Window::WindowList WindowList
Definition Window:658
osgWidget::point_type
Point::value_type point_type
Definition osgWidget/Types:33
osgWidget::XYCoord
osg::Vec2 XYCoord
Definition osgWidget/Types:36
osgWidget::WidgetList
std::list< osg::observer_ptr< Widget > > WidgetList
Definition osgWidget/Widget:513
osgWidget::Intersections
osgUtil::LineSegmentIntersector::Intersections Intersections
Definition WindowManager:35
View
OSGWIDGET_EXPORT
#define OSGWIDGET_EXPORT
Definition osgWidget/Export:42
ScriptEngine