X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FCanvas%2Fwindow.cxx;h=012915faf80bbee9d5917093b3ee212b53d3b18f;hb=2beb0f29afba98c49606a4b929a49bda4f6db91a;hp=8deed486ca09f55f0cbf2ec2d4b9f15d2f8906a5;hpb=326ee0c5cfc4c2b704db110746feac4fc5778575;p=flightgear.git diff --git a/src/Canvas/window.cxx b/src/Canvas/window.cxx index 8deed486c..012915faf 100644 --- a/src/Canvas/window.cxx +++ b/src/Canvas/window.cxx @@ -29,11 +29,18 @@ namespace canvas { + namespace sc = simgear::canvas; + + //---------------------------------------------------------------------------- + const std::string Window::TYPE_NAME = "window"; + //---------------------------------------------------------------------------- - Window::Window(SGPropertyNode* node): - PropertyBasedElement(node), + Window::Window( const simgear::canvas::CanvasWeakPtr& canvas, + const SGPropertyNode_ptr& node, + const Style& parent_style, + Element* parent ): + Image(canvas, node, parent_style, parent), _attributes_dirty(0), - _image(simgear::canvas::CanvasPtr(), node), _resizable(false), _capture_events(true), _resize_top(node, "resize-top"), @@ -42,8 +49,6 @@ namespace canvas _resize_left(node, "resize-left"), _resize_status(node, "resize-status") { - _image.removeListener(); - node->setFloatValue("source/right", 1); node->setFloatValue("source/bottom", 1); node->setBoolValue("source/normalized", true); @@ -59,52 +64,13 @@ namespace canvas //---------------------------------------------------------------------------- void Window::update(double delta_time_sec) { - _image.update(delta_time_sec); - - if( _attributes_dirty & SHADOW ) + if( _attributes_dirty & DECORATION ) { - float radius = get("shadow-radius"), - inset = get("shadow-inset"), - slice_width = radius + inset; - - if( slice_width <= 1 || _canvas_content.expired() ) - { - if( _image_shadow ) - { - getGroup()->removeChild(_image_shadow->getMatrixTransform()); - _image_shadow.reset(); - } - } - else - { - if( !_image_shadow ) - { - _image_shadow.reset(new simgear::canvas::Image( - _canvas_content, - _node->getChild("image-shadow", 0, true) - )); - _image_shadow->set("file", "gui/images/shadow.png"); - _image_shadow->set("slice", 7); - _image_shadow->set("fill", "#000000"); - getGroup()->insertChild(0, _image_shadow->getMatrixTransform()); - } - - simgear::canvas::CanvasPtr canvas = _canvas_decoration - ? _canvas_decoration - : _canvas_content.lock(); - - _image_shadow->set("slice-width", slice_width); - _image_shadow->set("x", -radius); - _image_shadow->set("y", -radius); - _image_shadow->set("size[0]", canvas->getViewWidth() + 2 * radius); - _image_shadow->set("size[1]", canvas->getViewHeight()+ 2 * radius); - } - - _attributes_dirty &= ~SHADOW; + updateDecoration(); + _attributes_dirty &= ~DECORATION; } - if( _image_shadow ) - _image_shadow->update(delta_time_sec); + Image::update(delta_time_sec); } //---------------------------------------------------------------------------- @@ -115,52 +81,35 @@ namespace canvas { handled = true; const std::string& name = node->getNameString(); - if( name == "raise-top" ) - doRaise(node); - else if( name == "resize" ) + if( name == "resize" ) _resizable = node->getBoolValue(); + else if( name == "update" ) + update(0); else if( name == "capture-events" ) _capture_events = node->getBoolValue(); else if( name == "decoration-border" ) parseDecorationBorder(node->getStringValue()); - else if( boost::starts_with(name, "shadow-") ) - _attributes_dirty |= SHADOW; + else if( boost::starts_with(name, "shadow-") + || name == "content-size" ) + _attributes_dirty |= DECORATION; else handled = false; } if( !handled ) - _image.valueChanged(node); - } - - //---------------------------------------------------------------------------- - void Window::childAdded(SGPropertyNode* parent, SGPropertyNode* child) - { - _image.childAdded(parent, child); - } - - //---------------------------------------------------------------------------- - void Window::childRemoved(SGPropertyNode* parent, SGPropertyNode* child) - { - _image.childRemoved(parent, child); + Image::valueChanged(node); } //---------------------------------------------------------------------------- osg::Group* Window::getGroup() { - return _image.getMatrixTransform(); - } - - //---------------------------------------------------------------------------- - const SGRect& Window::getRegion() const - { - return _image.getRegion(); + return getMatrixTransform(); } //---------------------------------------------------------------------------- const SGVec2 Window::getPosition() const { - const osg::Matrix& m = _image.getMatrixTransform()->getMatrix(); + const osg::Matrix& m = getMatrixTransform()->getMatrix(); return SGVec2( m(3, 0), m(3, 1) ); } @@ -171,28 +120,27 @@ namespace canvas } //---------------------------------------------------------------------------- - void Window::setCanvas(simgear::canvas::CanvasPtr canvas) + void Window::setCanvasContent(sc::CanvasPtr canvas) { _canvas_content = canvas; - _image.setSrcCanvas(canvas); - } - //---------------------------------------------------------------------------- - simgear::canvas::CanvasWeakPtr Window::getCanvas() const - { - return _image.getSrcCanvas(); + if( _image_content ) + // Placement within decoration canvas + _image_content->setSrcCanvas(canvas); + else + setSrcCanvas(canvas); } //---------------------------------------------------------------------------- - simgear::canvas::CanvasPtr Window::getCanvasDecoration() + sc::CanvasWeakPtr Window::getCanvasContent() const { - return _canvas_decoration; + return _canvas_content; } //---------------------------------------------------------------------------- - bool Window::isVisible() const + sc::CanvasPtr Window::getCanvasDecoration() { - return _image.isVisible(); + return _canvas_decoration; } //---------------------------------------------------------------------------- @@ -208,13 +156,16 @@ namespace canvas } //---------------------------------------------------------------------------- - bool Window::handleMouseEvent(const simgear::canvas::MouseEventPtr& event) + void Window::raise() { - return _image.handleEvent(event); + // on writing the z-index the window always is moved to the top of all other + // windows with the same z-index. + set("z-index", get("z-index", 0)); } //---------------------------------------------------------------------------- - void Window::handleResize(uint8_t mode, const osg::Vec2f& delta) + void Window::handleResize( uint8_t mode, + const osg::Vec2f& offset ) { if( mode == NONE ) { @@ -231,53 +182,53 @@ namespace canvas } if( mode & BOTTOM ) - _resize_bottom += delta.y(); + _resize_bottom = getRegion().b() + offset.y(); else if( mode & TOP ) - _resize_top += delta.y(); + _resize_top = getRegion().t() + offset.y(); if( mode & canvas::Window::RIGHT ) - _resize_right += delta.x(); + _resize_right = getRegion().r() + offset.x(); else if( mode & canvas::Window::LEFT ) - _resize_left += delta.x(); + _resize_left = getRegion().l() + offset.x(); } //---------------------------------------------------------------------------- - void Window::doRaise(SGPropertyNode* node_raise) + void Window::parseDecorationBorder(const std::string& str) { - if( node_raise && !node_raise->getBoolValue() ) - return; - - BOOST_FOREACH(osg::Group* parent, getGroup()->getParents()) - { - // Remove window... - parent->removeChild(getGroup()); - - // ...and add again as topmost window - parent->addChild(getGroup()); - } - - if( node_raise ) - node_raise->setBoolValue(false); + _decoration_border = simgear::CSSBorder::parse(str); + _attributes_dirty |= DECORATION; } //---------------------------------------------------------------------------- - void Window::parseDecorationBorder( const std::string& str ) + void Window::updateDecoration() { - _decoration_border = simgear::CSSBorder::parse(str); - if( _decoration_border.isNone() ) + int shadow_radius = get("shadow-radius") + 0.5; + if( shadow_radius < 2 ) + shadow_radius = 0; + + sc::CanvasPtr content = _canvas_content.lock(); + SGRect content_view + ( + 0, + 0, + get("content-size[0]", content ? content->getViewWidth() : 400), + get("content-size[1]", content ? content->getViewHeight() : 300) + ); + + if( _decoration_border.isNone() && !shadow_radius ) { - simgear::canvas::CanvasPtr canvas_content = _canvas_content.lock(); - _image.setSrcCanvas(canvas_content); - _image.set("size[0]", canvas_content->getViewWidth()); - _image.set("size[1]", canvas_content->getViewHeight()); + setSrcCanvas(content); + set("size[0]", content_view.width()); + set("size[1]", content_view.height()); _image_content.reset(); - _canvas_decoration->destroy(); + _image_shadow.reset(); + if( _canvas_decoration ) + _canvas_decoration->destroy(); _canvas_decoration.reset(); return; } - simgear::canvas::CanvasPtr content = _canvas_content.lock(); if( !_canvas_decoration ) { CanvasMgr* mgr = @@ -290,37 +241,71 @@ namespace canvas } _canvas_decoration = mgr->createCanvas("window-decoration"); - _canvas_decoration->getProps()->setStringValue("background", "rgba(0,0,0,0)"); - _image.setSrcCanvas(_canvas_decoration); + _canvas_decoration->getProps() + ->setStringValue("background", "rgba(0,0,0,0)"); + setSrcCanvas(_canvas_decoration); - // Decoration should be drawn first... - _canvas_decoration->createGroup("decoration"); - - // ...to allow drawing the actual content on top of the decoration - _image_content = - boost::dynamic_pointer_cast( - _canvas_decoration->getRootGroup()->createChild("image", "content") - ); + _image_content = _canvas_decoration->getRootGroup() + ->createChild("content"); _image_content->setSrcCanvas(content); + + // Draw content on top of decoration + _image_content->set("z-index", 1); } + sc::GroupPtr group_decoration = + _canvas_decoration->getOrCreateGroup("decoration"); + group_decoration->set("tf/t[0]", shadow_radius); + group_decoration->set("tf/t[1]", shadow_radius); + // TODO do we need clipping or shall we trust the decorator not to draw over + // the shadow? + simgear::CSSBorder::Offsets const border = - _decoration_border.getAbsOffsets(content->getViewport()); + _decoration_border.getAbsOffsets(content_view); - int outer_width = border.l + content->getViewWidth() + border.r, - outer_height = border.t + content->getViewHeight() + border.b; + int shad2 = 2 * shadow_radius, + outer_width = border.l + content_view.width() + border.r + shad2, + outer_height = border.t + content_view.height() + border.b + shad2; _canvas_decoration->setSizeX( outer_width ); _canvas_decoration->setSizeY( outer_height ); _canvas_decoration->setViewWidth( outer_width ); _canvas_decoration->setViewHeight( outer_height ); - _image.set("size[0]", outer_width); - _image.set("size[1]", outer_height); + set("size[0]", outer_width - shad2); + set("size[1]", outer_height - shad2); + set("outset", shadow_radius); assert(_image_content); - _image_content->set("x", border.l); - _image_content->set("y", border.t); + _image_content->set("x", shadow_radius + border.l); + _image_content->set("y", shadow_radius + border.t); + _image_content->set("size[0]", content_view.width()); + _image_content->set("size[1]", content_view.height()); + + if( !shadow_radius ) + { + if( _image_shadow ) + { + _image_shadow->destroy(); + _image_shadow.reset(); + } + return; + } + + int shadow_inset = std::max(get("shadow-inset") + 0.5, 0), + slice_width = shadow_radius + shadow_inset; + + _image_shadow = _canvas_decoration->getRootGroup() + ->getOrCreateChild("shadow"); + _image_shadow->set("file", "gui/images/shadow.png"); + _image_shadow->set("slice", 7); + _image_shadow->set("fill", "#000000"); + _image_shadow->set("slice-width", slice_width); + _image_shadow->set("size[0]", outer_width); + _image_shadow->set("size[1]", outer_height); + + // Draw shadow below decoration + _image_shadow->set("z-index", -1); } } // namespace canvas