More work on Canvas event handling/adapt for simgear changes
This commit is contained in:
parent
b8a3d6902e
commit
e5286f1217
7 changed files with 73 additions and 36 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 );
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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<WindowUserData*>(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);
|
||||
}
|
||||
|
|
|
@ -55,7 +55,8 @@ class GUIMgr:
|
|||
simgear::PropertyObject<int> _width,
|
||||
_height;
|
||||
|
||||
canvas::WindowWeakPtr _last_push;
|
||||
canvas::WindowWeakPtr _last_push,
|
||||
_last_mouse_over;
|
||||
float _last_x,
|
||||
_last_y;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
#include <simgear/canvas/elements/CanvasElement.hxx>
|
||||
#include <simgear/canvas/MouseEvent.hxx>
|
||||
|
||||
#include <simgear/nasal/cppbind/from_nasal.hxx>
|
||||
#include <simgear/nasal/cppbind/to_nasal.hxx>
|
||||
|
@ -49,6 +50,8 @@ naRef elementGetNode(naContext c, Element& element)
|
|||
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::ElementPtr> NasalElement;
|
||||
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);
|
||||
}
|
||||
|
||||
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<std::string>(c, args[0]);
|
||||
if( ctx.argc > 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)
|
||||
{
|
||||
NasalEvent::init("canvas.Event");
|
||||
NasalMouseEvent::init("canvas.MouseEvent")
|
||||
.bases<NasalEvent>();
|
||||
NasalCanvas::init("Canvas")
|
||||
.member("_node_ghost", &elementGetNode<sc::Canvas>)
|
||||
.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<sc::Element>);
|
||||
.member("_node_ghost", &elementGetNode<sc::Element>)
|
||||
.method<&sc::Element::addEventListener>("addEventListener");
|
||||
NasalGroup::init("canvas.Group")
|
||||
.bases<NasalElement>();
|
||||
|
||||
|
|
Loading…
Reference in a new issue