1
0
Fork 0

More work on Canvas event handling/adapt for simgear changes

This commit is contained in:
Thomas Geymayer 2012-11-27 13:56:39 +01:00
parent b8a3d6902e
commit e5286f1217
7 changed files with 73 additions and 36 deletions

View file

@ -99,4 +99,14 @@ namespace canvas
getNasalSys()->gcRelease(key); getNasalSys()->gcRelease(key);
} }
//------------------------------------------------------------------------------
naRef FGCanvasSystemAdapter::callMethod( naRef code,
naRef self,
int argc,
naRef* args,
naRef locals )
{
return getNasalSys()->callMethod(code, self, argc, args, locals);
}
} }

View file

@ -23,6 +23,11 @@ namespace canvas
virtual int gcSave(naRef r); virtual int gcSave(naRef r);
virtual void gcRelease(int key); virtual void gcRelease(int key);
virtual naRef callMethod( naRef code,
naRef self,
int argc,
naRef* args,
naRef locals );
}; };
} }

View file

@ -209,9 +209,6 @@ bool GUIMgr::handleEvent(const osgGA::GUIEventAdapter& ea)
case osgGA::GUIEventAdapter::MOVE: case osgGA::GUIEventAdapter::MOVE:
case osgGA::GUIEventAdapter::SCROLL: case osgGA::GUIEventAdapter::SCROLL:
return handleMouse(ea); return handleMouse(ea);
// case osgGA::GUIEventAdapter::MOVE:
// std::cout << "MOVE" << std::endl;
// break;
case osgGA::GUIEventAdapter::RESIZE: case osgGA::GUIEventAdapter::RESIZE:
handleResize( ea.getWindowX(), handleResize( ea.getWindowX(),
ea.getWindowY(), ea.getWindowY(),
@ -260,18 +257,25 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea)
if( !_transform->getNumChildren() ) if( !_transform->getNumChildren() )
return false; 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->pos.x() = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5;
event.y = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5; event->pos.y() = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5;
if( ea.getMouseYOrientation() if( ea.getMouseYOrientation()
!= osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS ) != osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS )
event.y = _height - event.y; event->pos.y() = _height - event->pos.y();
event.button = ea.getButton(); event->delta.x() = event->pos.x() - _last_x;
event.state = ea.getButtonMask(); event->delta.y() = event->pos.y() - _last_y;
event.mod = ea.getModKeyMask();
event.scroll = ea.getScrollingMotion(); _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; canvas::WindowPtr window_at_cursor;
for( int i = _transform->getNumChildren() - 1; i >= 0; --i ) for( int i = _transform->getNumChildren() - 1; i >= 0; --i )
@ -287,7 +291,7 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea)
canvas::WindowPtr window = canvas::WindowPtr window =
static_cast<WindowUserData*>(layer->getChild(j)->getUserData()) static_cast<WindowUserData*>(layer->getChild(j)->getUserData())
->window.lock(); ->window.lock();
if( window->getRegion().contains(event.x, event.y) ) if( window->getRegion().contains(event->pos.x(), event->pos.y()) )
{ {
window_at_cursor = window; window_at_cursor = window;
break; break;
@ -303,19 +307,38 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea)
{ {
case osgGA::GUIEventAdapter::PUSH: case osgGA::GUIEventAdapter::PUSH:
_last_push = window_at_cursor; _last_push = window_at_cursor;
event->type = sc::Event::MOUSE_DOWN;
break; break;
case osgGA::GUIEventAdapter::SCROLL: // case osgGA::GUIEventAdapter::SCROLL:
// event->type = sc::Event::SCROLL;
// break;
case osgGA::GUIEventAdapter::MOVE: 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: case osgGA::GUIEventAdapter::RELEASE:
target_window = _last_push.lock(); target_window = _last_push.lock();
_last_push.reset(); _last_push.reset();
event->type = sc::Event::MOUSE_UP;
break; break;
case osgGA::GUIEventAdapter::DRAG: // case osgGA::GUIEventAdapter::DRAG:
target_window = _last_push.lock(); // target_window = _last_push.lock();
break; // break;
default: default:
return false; return false;
@ -323,15 +346,9 @@ bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea)
if( target_window ) 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 // Let the event position be always relative to the top left window corner
event.x -= target_window->getRegion().x(); event->pos.x() -= target_window->getRegion().x();
event.y -= target_window->getRegion().y(); event->pos.y() -= target_window->getRegion().y();
return target_window->handleMouseEvent(event); return target_window->handleMouseEvent(event);
} }

View file

@ -55,7 +55,8 @@ class GUIMgr:
simgear::PropertyObject<int> _width, simgear::PropertyObject<int> _width,
_height; _height;
canvas::WindowWeakPtr _last_push; canvas::WindowWeakPtr _last_push,
_last_mouse_over;
float _last_x, float _last_x,
_last_y; _last_y;

View file

@ -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() ) if( !getCanvas().expired() )
return getCanvas().lock()->handleMouseEvent(event); return getCanvas().lock()->handleMouseEvent(event);

View file

@ -45,7 +45,7 @@ namespace canvas
void setCanvas(simgear::canvas::CanvasPtr canvas); void setCanvas(simgear::canvas::CanvasPtr canvas);
simgear::canvas::CanvasWeakPtr getCanvas() const; simgear::canvas::CanvasWeakPtr getCanvas() const;
bool handleMouseEvent(const simgear::canvas::MouseEvent& event); bool handleMouseEvent(const simgear::canvas::MouseEventPtr& event);
protected: protected:

View file

@ -33,6 +33,7 @@
#include <simgear/canvas/Canvas.hxx> #include <simgear/canvas/Canvas.hxx>
#include <simgear/canvas/elements/CanvasElement.hxx> #include <simgear/canvas/elements/CanvasElement.hxx>
#include <simgear/canvas/MouseEvent.hxx>
#include <simgear/nasal/cppbind/from_nasal.hxx> #include <simgear/nasal/cppbind/from_nasal.hxx>
#include <simgear/nasal/cppbind/to_nasal.hxx> #include <simgear/nasal/cppbind/to_nasal.hxx>
@ -49,6 +50,8 @@ naRef elementGetNode(naContext c, Element& element)
return propNodeGhostCreate(c, element.getProps()); return propNodeGhostCreate(c, element.getProps());
} }
typedef nasal::Ghost<sc::EventPtr> NasalEvent;
typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas; typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
typedef nasal::Ghost<sc::ElementPtr> NasalElement; typedef nasal::Ghost<sc::ElementPtr> NasalElement;
typedef nasal::Ghost<sc::GroupPtr> NasalGroup; typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
@ -145,27 +148,28 @@ static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
return NasalCanvas::create(c, canvas); return NasalCanvas::create(c, canvas);
} }
naRef f_canvasCreateGroup( sc::Canvas& canvas, naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
naContext c,
int argc,
naRef* args )
{ {
std::string name; std::string name;
if( argc > 0 ) if( ctx.argc > 0 )
name = nasal::from_nasal<std::string>(c, args[0]); name = nasal::from_nasal<std::string>(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) naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
{ {
NasalEvent::init("canvas.Event");
NasalMouseEvent::init("canvas.MouseEvent")
.bases<NasalEvent>();
NasalCanvas::init("Canvas") NasalCanvas::init("Canvas")
.member("_node_ghost", &elementGetNode<sc::Canvas>) .member("_node_ghost", &elementGetNode<sc::Canvas>)
.member("size_x", &sc::Canvas::getSizeX) .member("size_x", &sc::Canvas::getSizeX)
.member("size_y", &sc::Canvas::getSizeY) .member("size_y", &sc::Canvas::getSizeY)
.method_func<&f_canvasCreateGroup>("createGroup"); .method_func<&f_canvasCreateGroup>("createGroup");
NasalElement::init("canvas.Element") NasalElement::init("canvas.Element")
.member("_node_ghost", &elementGetNode<sc::Element>); .member("_node_ghost", &elementGetNode<sc::Element>)
.method<&sc::Element::addEventListener>("addEventListener");
NasalGroup::init("canvas.Group") NasalGroup::init("canvas.Group")
.bases<NasalElement>(); .bases<NasalElement>();