From e5286f12173a060bc786fef49e6a58813663f846 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Tue, 27 Nov 2012 13:56:39 +0100 Subject: [PATCH] More work on Canvas event handling/adapt for simgear changes --- src/Canvas/FGCanvasSystemAdapter.cxx | 10 +++++ src/Canvas/FGCanvasSystemAdapter.hxx | 5 +++ src/Canvas/gui_mgr.cxx | 67 +++++++++++++++++----------- src/Canvas/gui_mgr.hxx | 3 +- src/Canvas/window.cxx | 2 +- src/Canvas/window.hxx | 2 +- src/Scripting/NasalCanvas.cxx | 20 +++++---- 7 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/Canvas/FGCanvasSystemAdapter.cxx b/src/Canvas/FGCanvasSystemAdapter.cxx index 740973979..84db82d55 100644 --- a/src/Canvas/FGCanvasSystemAdapter.cxx +++ b/src/Canvas/FGCanvasSystemAdapter.cxx @@ -99,4 +99,14 @@ namespace canvas getNasalSys()->gcRelease(key); } + //------------------------------------------------------------------------------ + naRef FGCanvasSystemAdapter::callMethod( naRef code, + naRef self, + int argc, + naRef* args, + naRef locals ) + { + return getNasalSys()->callMethod(code, self, argc, args, locals); + } + } diff --git a/src/Canvas/FGCanvasSystemAdapter.hxx b/src/Canvas/FGCanvasSystemAdapter.hxx index 82896bda5..0f13057cc 100644 --- a/src/Canvas/FGCanvasSystemAdapter.hxx +++ b/src/Canvas/FGCanvasSystemAdapter.hxx @@ -23,6 +23,11 @@ namespace canvas virtual int gcSave(naRef r); virtual void gcRelease(int key); + virtual naRef callMethod( naRef code, + naRef self, + int argc, + naRef* args, + naRef locals ); }; } diff --git a/src/Canvas/gui_mgr.cxx b/src/Canvas/gui_mgr.cxx index f72cf49d6..d2a0b332f 100644 --- a/src/Canvas/gui_mgr.cxx +++ b/src/Canvas/gui_mgr.cxx @@ -209,9 +209,6 @@ bool GUIMgr::handleEvent(const osgGA::GUIEventAdapter& ea) case osgGA::GUIEventAdapter::MOVE: case osgGA::GUIEventAdapter::SCROLL: return handleMouse(ea); -// case osgGA::GUIEventAdapter::MOVE: -// std::cout << "MOVE" << std::endl; -// break; case osgGA::GUIEventAdapter::RESIZE: handleResize( ea.getWindowX(), ea.getWindowY(), @@ -260,18 +257,25 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea) if( !_transform->getNumChildren() ) return false; - simgear::canvas::MouseEvent event( ea.getEventType() ); + namespace sc = simgear::canvas; + sc::MouseEventPtr event(new sc::MouseEvent); - event.x = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5; - event.y = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5; + event->pos.x() = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5; + event->pos.y() = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5; if( ea.getMouseYOrientation() != osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS ) - event.y = _height - event.y; + event->pos.y() = _height - event->pos.y(); - event.button = ea.getButton(); - event.state = ea.getButtonMask(); - event.mod = ea.getModKeyMask(); - event.scroll = ea.getScrollingMotion(); + event->delta.x() = event->pos.x() - _last_x; + event->delta.y() = event->pos.y() - _last_y; + + _last_x = event->pos.x(); + _last_y = event->pos.y(); + + event->button = ea.getButton(); + event->state = ea.getButtonMask(); + event->mod = ea.getModKeyMask(); + //event->scroll = ea.getScrollingMotion(); canvas::WindowPtr window_at_cursor; for( int i = _transform->getNumChildren() - 1; i >= 0; --i ) @@ -287,7 +291,7 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea) canvas::WindowPtr window = static_cast(layer->getChild(j)->getUserData()) ->window.lock(); - if( window->getRegion().contains(event.x, event.y) ) + if( window->getRegion().contains(event->pos.x(), event->pos.y()) ) { window_at_cursor = window; break; @@ -303,19 +307,38 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea) { case osgGA::GUIEventAdapter::PUSH: _last_push = window_at_cursor; + event->type = sc::Event::MOUSE_DOWN; break; - case osgGA::GUIEventAdapter::SCROLL: +// case osgGA::GUIEventAdapter::SCROLL: +// event->type = sc::Event::SCROLL; +// break; case osgGA::GUIEventAdapter::MOVE: - break; + { + canvas::WindowPtr last_mouse_over = _last_mouse_over.lock(); + if( last_mouse_over != window_at_cursor && last_mouse_over ) + { + sc::MouseEventPtr move_event( new sc::MouseEvent(*event) ); + move_event->type = sc::Event::MOUSE_LEAVE; + // Let the event position be always relative to the top left window corner + move_event->pos.x() -= last_mouse_over->getRegion().x(); + move_event->pos.y() -= last_mouse_over->getRegion().y(); + + last_mouse_over->handleMouseEvent(move_event); + } + _last_mouse_over = window_at_cursor; + event->type = sc::Event::MOUSE_MOVE; + break; + } case osgGA::GUIEventAdapter::RELEASE: target_window = _last_push.lock(); _last_push.reset(); + event->type = sc::Event::MOUSE_UP; break; - case osgGA::GUIEventAdapter::DRAG: - target_window = _last_push.lock(); - break; +// case osgGA::GUIEventAdapter::DRAG: +// target_window = _last_push.lock(); +// break; default: return false; @@ -323,15 +346,9 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea) if( target_window ) { - event.dx = event.x - _last_x; - event.dy = event.y - _last_y; - - _last_x = event.x; - _last_y = event.y; - // Let the event position be always relative to the top left window corner - event.x -= target_window->getRegion().x(); - event.y -= target_window->getRegion().y(); + event->pos.x() -= target_window->getRegion().x(); + event->pos.y() -= target_window->getRegion().y(); return target_window->handleMouseEvent(event); } diff --git a/src/Canvas/gui_mgr.hxx b/src/Canvas/gui_mgr.hxx index f1eeccca4..1661011d5 100644 --- a/src/Canvas/gui_mgr.hxx +++ b/src/Canvas/gui_mgr.hxx @@ -55,7 +55,8 @@ class GUIMgr: simgear::PropertyObject _width, _height; - canvas::WindowWeakPtr _last_push; + canvas::WindowWeakPtr _last_push, + _last_mouse_over; float _last_x, _last_y; diff --git a/src/Canvas/window.cxx b/src/Canvas/window.cxx index d79e1cb7a..cda7e2931 100644 --- a/src/Canvas/window.cxx +++ b/src/Canvas/window.cxx @@ -91,7 +91,7 @@ namespace canvas } //---------------------------------------------------------------------------- - bool Window::handleMouseEvent(const simgear::canvas::MouseEvent& event) + bool Window::handleMouseEvent(const simgear::canvas::MouseEventPtr& event) { if( !getCanvas().expired() ) return getCanvas().lock()->handleMouseEvent(event); diff --git a/src/Canvas/window.hxx b/src/Canvas/window.hxx index a682fec89..28a681ba7 100644 --- a/src/Canvas/window.hxx +++ b/src/Canvas/window.hxx @@ -45,7 +45,7 @@ namespace canvas void setCanvas(simgear::canvas::CanvasPtr canvas); simgear::canvas::CanvasWeakPtr getCanvas() const; - bool handleMouseEvent(const simgear::canvas::MouseEvent& event); + bool handleMouseEvent(const simgear::canvas::MouseEventPtr& event); protected: diff --git a/src/Scripting/NasalCanvas.cxx b/src/Scripting/NasalCanvas.cxx index 204cfa682..2a9faaa1b 100644 --- a/src/Scripting/NasalCanvas.cxx +++ b/src/Scripting/NasalCanvas.cxx @@ -33,6 +33,7 @@ #include #include +#include #include #include @@ -49,6 +50,8 @@ naRef elementGetNode(naContext c, Element& element) return propNodeGhostCreate(c, element.getProps()); } +typedef nasal::Ghost NasalEvent; +typedef nasal::Ghost NasalMouseEvent; typedef nasal::Ghost NasalCanvas; typedef nasal::Ghost NasalElement; typedef nasal::Ghost NasalGroup; @@ -145,27 +148,28 @@ static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args) return NasalCanvas::create(c, canvas); } -naRef f_canvasCreateGroup( sc::Canvas& canvas, - naContext c, - int argc, - naRef* args ) +naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx) { std::string name; - if( argc > 0 ) - name = nasal::from_nasal(c, args[0]); + if( ctx.argc > 0 ) + name = nasal::from_nasal(ctx.c, ctx.args[0]); - return NasalGroup::create(c, canvas.createGroup(name)); + return NasalGroup::create(ctx.c, canvas.createGroup(name)); } naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave) { + NasalEvent::init("canvas.Event"); + NasalMouseEvent::init("canvas.MouseEvent") + .bases(); NasalCanvas::init("Canvas") .member("_node_ghost", &elementGetNode) .member("size_x", &sc::Canvas::getSizeX) .member("size_y", &sc::Canvas::getSizeY) .method_func<&f_canvasCreateGroup>("createGroup"); NasalElement::init("canvas.Element") - .member("_node_ghost", &elementGetNode); + .member("_node_ghost", &elementGetNode) + .method<&sc::Element::addEventListener>("addEventListener"); NasalGroup::init("canvas.Group") .bases(); -- 2.39.5