Canvas: simplify code by using new nasal function conversion.

This commit is contained in:
Thomas Geymayer 2013-10-15 17:42:49 +02:00
parent 5258699f20
commit a37a254a68
9 changed files with 4 additions and 163 deletions

View File

@ -4,7 +4,6 @@ set(HEADERS
canvas_fwd.hxx
Canvas.hxx
CanvasEvent.hxx
CanvasEventListener.hxx
CanvasEventManager.hxx
CanvasEventTypes.hxx
CanvasEventVisitor.hxx
@ -20,7 +19,6 @@ set(HEADERS
set(SOURCES
Canvas.cxx
CanvasEvent.cxx
CanvasEventListener.cxx
CanvasEventManager.cxx
CanvasEventVisitor.cxx
CanvasMgr.cxx

View File

@ -329,15 +329,6 @@ namespace canvas
return _root_group->addEventListener(type, cb);
}
//----------------------------------------------------------------------------
bool Canvas::addNasalEventListener(const std::string& type, naRef code)
{
if( !_root_group.get() )
throw std::runtime_error("Canvas::AddNasalEventListener: no root group!");
return _root_group->addNasalEventListener(type, code);
}
//----------------------------------------------------------------------------
void Canvas::setSizeX(int sx)
{

View File

@ -133,7 +133,6 @@ namespace canvas
void update(double delta_time_sec);
bool addEventListener(const std::string& type, const EventListener& cb);
bool addNasalEventListener(const std::string& type, naRef code);
void setSizeX(int sx);
void setSizeY(int sy);

View File

@ -1,71 +0,0 @@
// Listener for canvas (GUI) events being passed to a Nasal function/code
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// 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 GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#include "CanvasEvent.hxx"
#include "CanvasEventListener.hxx"
#include "CanvasSystemAdapter.hxx"
#include <simgear/nasal/cppbind/Ghost.hxx>
namespace simgear
{
namespace canvas
{
//----------------------------------------------------------------------------
NasalEventListener::NasalEventListener( naRef code,
const SystemAdapterPtr& sys_adapter ):
_code(code),
_gc_key(-1),
_sys(sys_adapter)
{
assert( sys_adapter );
if( !naIsCode(code)
&& !naIsCCode(code)
&& !naIsFunc(code) )
throw std::runtime_error
(
"canvas::NasalEventListener: invalid function argument"
);
_gc_key = naGCSave(_code);
}
//----------------------------------------------------------------------------
NasalEventListener::~NasalEventListener()
{
naGCRelease(_gc_key);
}
//----------------------------------------------------------------------------
void NasalEventListener::operator()(const canvas::EventPtr& event) const
{
SystemAdapterPtr sys = _sys.lock();
if( !sys )
return;
naRef args[] = {
nasal::Ghost<EventPtr>::create(sys->getNasalContext(), event)
};
const int num_args = sizeof(args)/sizeof(args[0]);
sys->callMethod(_code, naNil(), num_args, args, naNil());
}
} // namespace canvas
} // namespace simgear

View File

@ -1,49 +0,0 @@
// Listener for canvas (GUI) events being passed to a Nasal function/code
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// 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 GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#ifndef CANVAS_EVENT_LISTENER_HXX_
#define CANVAS_EVENT_LISTENER_HXX_
#include "canvas_fwd.hxx"
#include <simgear/nasal/naref.h>
namespace simgear
{
namespace canvas
{
class NasalEventListener:
public SGReferenced
{
public:
NasalEventListener( naRef code,
const SystemAdapterPtr& sys_adapter );
~NasalEventListener();
void operator()(const canvas::EventPtr& event) const;
protected:
naRef _code;
int _gc_key;
SystemAdapterWeakPtr _sys;
};
} // namespace canvas
} // namespace simgear
#endif /* CANVAS_EVENT_LISTENER_HXX_ */

View File

@ -19,6 +19,7 @@
#include "CanvasEventManager.hxx"
#include "MouseEvent.hxx"
#include <simgear/canvas/elements/CanvasElement.hxx>
#include <cmath>
namespace simgear
{

View File

@ -20,7 +20,6 @@
#define SG_CANVAS_SYSTEM_ADAPTER_HXX_
#include "canvas_fwd.hxx"
#include <simgear/nasal/nasal.h>
namespace simgear
{
@ -36,17 +35,6 @@ namespace canvas
virtual void addCamera(osg::Camera* camera) const = 0;
virtual void removeCamera(osg::Camera* camera) const = 0;
virtual osg::Image* getImage(const std::string& path) const = 0;
virtual naContext getNasalContext() const = 0;
/**
* Call a Nasal function with the given environment and arguments.
*/
virtual naRef callMethod( naRef code,
naRef self,
int argc,
naRef* args,
naRef locals ) = 0;
};
} // namespace canvas

View File

@ -18,7 +18,6 @@
#include "CanvasElement.hxx"
#include <simgear/canvas/Canvas.hxx>
#include <simgear/canvas/CanvasEventListener.hxx>
#include <simgear/canvas/CanvasEventVisitor.hxx>
#include <simgear/canvas/MouseEvent.hxx>
#include <simgear/math/SGMisc.hxx>
@ -178,7 +177,7 @@ namespace canvas
{
SG_LOG
(
SG_NASAL,
SG_GENERAL,
SG_INFO,
"addEventListener(" << _node->getPath() << ", " << type_str << ")"
);
@ -186,7 +185,7 @@ namespace canvas
Event::Type type = Event::strToType(type_str);
if( type == Event::UNKNOWN )
{
SG_LOG( SG_NASAL,
SG_LOG( SG_GENERAL,
SG_WARN,
"addEventListener: Unknown event type " << type_str );
return false;
@ -197,19 +196,6 @@ namespace canvas
return true;
}
//----------------------------------------------------------------------------
bool Element::addNasalEventListener(const std::string& type, naRef code)
{
SGSharedPtr<NasalEventListener> listener =
new NasalEventListener(code, _canvas.lock()->getSystemAdapter());
return addEventListener
(
type,
boost::bind(&NasalEventListener::operator(), listener, _1)
);
}
//----------------------------------------------------------------------------
void Element::clearEventListener()
{

View File

@ -23,13 +23,13 @@
#include <simgear/canvas/CanvasEvent.hxx>
#include <simgear/props/PropertyBasedElement.hxx>
#include <simgear/misc/stdint.hxx> // for uint32_t
#include <simgear/nasal/cppbind/Ghost.hxx>
#include <osg/BoundingBox>
#include <osg/MatrixTransform>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/type_traits/is_base_of.hpp>
namespace osg
{
@ -93,8 +93,6 @@ namespace canvas
virtual void update(double dt);
bool addEventListener(const std::string& type, const EventListener& cb);
bool addNasalEventListener(const std::string& type, naRef code);
virtual void clearEventListener();
virtual bool accept(EventVisitor& visitor);