const EventListener& cb )
{
if( !_root_group.get() )
- throw std::runtime_error("Canvas::AddEventListener: no root group!");
+ throw std::runtime_error("Canvas::addEventListener: no root group!");
return _root_group->addEventListener(type, cb);
}
+ //----------------------------------------------------------------------------
+ bool Canvas::dispatchEvent(const EventPtr& event)
+ {
+ if( !_root_group.get() )
+ throw std::runtime_error("Canvas::dispatchEvent: no root group!");
+
+ return _root_group->dispatchEvent(event);
+ }
+
//----------------------------------------------------------------------------
void Canvas::setSizeX(int sx)
{
void update(double delta_time_sec);
bool addEventListener(const std::string& type, const EventListener& cb);
+ bool dispatchEvent(const EventPtr& event);
void setSizeX(int sx);
void setSizeY(int sy);
}
//----------------------------------------------------------------------------
- ElementPtr Element::getParent()
+ ElementPtr Element::getParent() const
{
return _parent;
}
+ //----------------------------------------------------------------------------
+ CanvasWeakPtr Element::getCanvas() const
+ {
+ return _canvas;
+ }
+
//----------------------------------------------------------------------------
void Element::update(double dt)
{
}
//----------------------------------------------------------------------------
- bool Element::handleEvent(EventPtr event)
+ bool Element::handleEvent(const EventPtr& event)
{
ListenerMap::iterator listeners = _listener.find(event->getType());
if( listeners == _listener.end() )
return false;
BOOST_FOREACH(EventListener const& listener, listeners->second)
- listener(event);
+ try
+ {
+ listener(event);
+ }
+ catch( std::exception const& ex )
+ {
+ SG_LOG(
+ SG_GENERAL,
+ SG_WARN,
+ "canvas::Element: event handler error: '" << ex.what() << "'"
+ );
+ }
return true;
}
//----------------------------------------------------------------------------
- bool Element::dispatchEvent(EventPtr event)
+ bool Element::dispatchEvent(const EventPtr& event)
{
EventPropagationPath path;
path.push_back( EventTarget(this) );
virtual ~Element() = 0;
virtual void onDestroy();
- ElementPtr getParent();
+ ElementPtr getParent() const;
+ CanvasWeakPtr getCanvas() const;
/**
* Called every frame to update internal state
virtual bool ascend(EventVisitor& visitor);
virtual bool traverse(EventVisitor& visitor);
- virtual bool handleEvent(EventPtr event);
- bool dispatchEvent(EventPtr event);
+ virtual bool handleEvent(const EventPtr& event);
+ bool dispatchEvent(const EventPtr& event);
/**
*
}
//----------------------------------------------------------------------------
- bool Image::handleEvent(EventPtr event)
+ bool Image::handleEvent(const EventPtr& event)
{
bool handled = Element::handleEvent(event);
if( mouse_event )
{
mouse_event.reset( new MouseEvent(*mouse_event) );
- event = mouse_event;
mouse_event->client_pos = mouse_event->local_pos
- toOsg(_region.getMin());
const SGRect<float>& getRegion() const;
- bool handleEvent(EventPtr event);
+ bool handleEvent(const EventPtr& event);
protected: