From d61b5827fd11aa3d01652f7d464fe62fbf8ea68a Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Mon, 13 May 2013 00:23:34 +0200 Subject: [PATCH] Canvas: proper forwarding of dirty and visible flags within nested canvases. --- simgear/canvas/Canvas.cxx | 50 ++++++++++++++++++++----- simgear/canvas/Canvas.hxx | 18 ++++++--- simgear/canvas/ODGauge.cxx | 6 +++ simgear/canvas/ODGauge.hxx | 2 + simgear/canvas/elements/CanvasImage.cxx | 9 ++++- 5 files changed, 67 insertions(+), 18 deletions(-) diff --git a/simgear/canvas/Canvas.cxx b/simgear/canvas/Canvas.cxx index cb8d6917..7651635b 100644 --- a/simgear/canvas/Canvas.cxx +++ b/simgear/canvas/Canvas.cxx @@ -107,7 +107,7 @@ namespace canvas } //---------------------------------------------------------------------------- - void Canvas::addDependentCanvas(const CanvasWeakPtr& canvas) + void Canvas::addParentCanvas(const CanvasWeakPtr& canvas) { if( canvas.expired() ) { @@ -115,19 +115,41 @@ namespace canvas ( SG_GENERAL, SG_WARN, - "Canvas::addDependentCanvas: got an expired Canvas dependent on " - << _node->getPath() + "Canvas::addParentCanvas: got an expired parent: " << _node->getPath() ); return; } - _dependent_canvases.insert(canvas); + _parent_canvases.insert(canvas); } //---------------------------------------------------------------------------- - void Canvas::removeDependentCanvas(const CanvasWeakPtr& canvas) + void Canvas::addChildCanvas(const CanvasWeakPtr& canvas) { - _dependent_canvases.erase(canvas); + if( canvas.expired() ) + { + SG_LOG + ( + SG_GENERAL, + SG_WARN, + "Canvas::addChildCanvas: got an expired child: " << _node->getPath() + ); + return; + } + + _child_canvases.insert(canvas); + } + + //---------------------------------------------------------------------------- + void Canvas::removeParentCanvas(const CanvasWeakPtr& canvas) + { + _parent_canvases.erase(canvas); + } + + //---------------------------------------------------------------------------- + void Canvas::removeChildCanvas(const CanvasWeakPtr& canvas) + { + _child_canvases.erase(canvas); } //---------------------------------------------------------------------------- @@ -197,10 +219,18 @@ namespace canvas if( _visible || _render_always ) { + BOOST_FOREACH(CanvasWeakPtr canvas, _child_canvases) + { + // TODO should we check if the image the child canvas is displayed + // within is really visible? + if( !canvas.expired() ) + canvas.lock()->_visible = true; + } + if( _render_dirty ) { - // Also mark all dependent (eg. recursively used) canvases as dirty - BOOST_FOREACH(CanvasWeakPtr canvas, _dependent_canvases) + // Also mark all canvases this canvas is displayed within as dirty + BOOST_FOREACH(CanvasWeakPtr canvas, _parent_canvases) { if( !canvas.expired() ) canvas.lock()->_render_dirty = true; @@ -340,13 +370,13 @@ namespace canvas //---------------------------------------------------------------------------- int Canvas::getViewWidth() const { - return _view_width; + return _texture.getViewSize().x(); } //---------------------------------------------------------------------------- int Canvas::getViewHeight() const { - return _view_height; + return _texture.getViewSize().y(); } //---------------------------------------------------------------------------- diff --git a/simgear/canvas/Canvas.hxx b/simgear/canvas/Canvas.hxx index efb5bdc2..c746e287 100644 --- a/simgear/canvas/Canvas.hxx +++ b/simgear/canvas/Canvas.hxx @@ -79,18 +79,24 @@ namespace canvas CanvasMgr* getCanvasMgr() const; /** - * Add a canvas which should be mared as dirty upon any change to this + * Add a canvas which should be marked as dirty upon any change to this * canvas. * * This mechanism is used to eg. redraw a canvas if it's displaying * another canvas (recursive canvases) */ - void addDependentCanvas(const CanvasWeakPtr& canvas); + void addParentCanvas(const CanvasWeakPtr& canvas); + + /** + * Add a canvas which should be marked visible if this canvas is visible. + */ + void addChildCanvas(const CanvasWeakPtr& canvas); /** * Stop notifying the given canvas upon changes */ - void removeDependentCanvas(const CanvasWeakPtr& canvas); + void removeParentCanvas(const CanvasWeakPtr& canvas); + void removeChildCanvas(const CanvasWeakPtr& canvas); GroupPtr createGroup(const std::string& name = ""); @@ -161,9 +167,9 @@ namespace canvas std::vector _dirty_placements; std::vector _placements; - std::set _dependent_canvases; // _parent_canvases, // PlacementFactoryMap; static PlacementFactoryMap _placement_factories; diff --git a/simgear/canvas/ODGauge.cxx b/simgear/canvas/ODGauge.cxx index 45edf9a9..17858db7 100644 --- a/simgear/canvas/ODGauge.cxx +++ b/simgear/canvas/ODGauge.cxx @@ -98,6 +98,12 @@ namespace canvas updateCoordinateFrame(); } + //---------------------------------------------------------------------------- + osg::Vec2s ODGauge::getViewSize() const + { + return osg::Vec2s(_view_width, _view_height); + } + //---------------------------------------------------------------------------- void ODGauge::useImageCoords(bool use) { diff --git a/simgear/canvas/ODGauge.hxx b/simgear/canvas/ODGauge.hxx index fc3d8b77..6c62823e 100644 --- a/simgear/canvas/ODGauge.hxx +++ b/simgear/canvas/ODGauge.hxx @@ -71,6 +71,8 @@ namespace canvas */ void setViewSize(int width, int height = -1); + osg::Vec2s getViewSize() const; + /** * DEPRECATED * diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index 7400833f..d4e56601 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -331,7 +331,9 @@ namespace canvas void Image::setSrcCanvas(CanvasPtr canvas) { if( !_src_canvas.expired() ) - _src_canvas.lock()->removeDependentCanvas(_canvas); + _src_canvas.lock()->removeParentCanvas(_canvas); + if( !_canvas.expired() ) + _canvas.lock()->removeChildCanvas(_src_canvas); _src_canvas = canvas; _geom->getOrCreateStateSet() @@ -341,7 +343,10 @@ namespace canvas if( !_src_canvas.expired() ) { setupDefaultDimensions(); - _src_canvas.lock()->addDependentCanvas(_canvas); + _src_canvas.lock()->addParentCanvas(_canvas); + + if( !_canvas.expired() ) + _canvas.lock()->addChildCanvas(_src_canvas); } } -- 2.39.5