1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <basegfx/range/b2drange.hxx>
22 #include <basegfx/range/b1drange.hxx>
23 #include <basegfx/range/b2dpolyrange.hxx>
24 #include <basegfx/polygon/b2dpolypolygon.hxx>
25 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
27 #include <osl/diagnose.h>
31 using namespace ::com::sun::star
;
33 namespace slideshow::internal
35 Layer::Layer( Dummy
) :
40 mbBackgroundLayer(true),
50 mbBackgroundLayer(false),
55 ViewLayerSharedPtr
Layer::addView( const ViewSharedPtr
& rNewView
)
57 OSL_ASSERT( rNewView
);
59 ViewEntryVector::iterator aIter
;
60 const ViewEntryVector::iterator
aEnd( maViewEntries
.end() );
61 if( (aIter
=std::find_if( maViewEntries
.begin(),
63 [&rNewView
]( const ViewEntry
& rViewEntry
)
64 { return rViewEntry
.getView() == rNewView
; } ) ) != aEnd
)
66 // already added - just return existing layer
67 return aIter
->mpViewLayer
;
71 // not yet added - create new view layer
72 ViewLayerSharedPtr pNewLayer
;
73 if( mbBackgroundLayer
)
76 pNewLayer
= rNewView
->createViewLayer(maBounds
);
79 maViewEntries
.emplace_back( rNewView
,
82 return maViewEntries
.back().mpViewLayer
;
85 ViewLayerSharedPtr
Layer::removeView( const ViewSharedPtr
& rView
)
89 ViewEntryVector::iterator aIter
;
90 const ViewEntryVector::iterator
aEnd( maViewEntries
.end() );
91 if( (aIter
=std::find_if( maViewEntries
.begin(),
93 [&rView
]( const ViewEntry
& rViewEntry
)
94 { return rViewEntry
.getView() == rView
; } ) ) == aEnd
)
96 // View was not added/is already removed
97 return ViewLayerSharedPtr();
100 OSL_ENSURE( std::count_if( maViewEntries
.begin(),
102 [&rView
]( const ViewEntry
& rViewEntry
)
103 { return rViewEntry
.getView() == rView
; } ) == 1,
104 "Layer::removeView(): view added multiple times" );
106 ViewLayerSharedPtr
pRet( aIter
->mpViewLayer
);
107 maViewEntries
.erase(aIter
);
112 void Layer::setShapeViews( ShapeSharedPtr
const& rShape
) const
114 rShape
->clearAllViewLayers();
116 for( const auto& rViewEntry
: maViewEntries
)
117 rShape
->addViewLayer( rViewEntry
.getViewLayer(), false );
120 void Layer::setPriority( const ::basegfx::B1DRange
& rPrioRange
)
122 if( !mbBackgroundLayer
)
124 for( const auto& rViewEntry
: maViewEntries
)
125 rViewEntry
.getViewLayer()->setPriority( rPrioRange
);
129 void Layer::addUpdateRange( ::basegfx::B2DRange
const& rUpdateRange
)
131 // TODO(Q1): move this to B2DMultiRange
132 if( !rUpdateRange
.isEmpty() )
133 maUpdateAreas
.appendElement( rUpdateRange
,
134 basegfx::B2VectorOrientation::Positive
);
137 void Layer::updateBounds( ShapeSharedPtr
const& rShape
)
139 if( !mbBackgroundLayer
)
144 maNewBounds
.expand( rShape
->getUpdateArea() );
147 mbBoundsDirty
= true;
150 bool Layer::commitBounds()
152 mbBoundsDirty
= false;
154 if( mbBackgroundLayer
)
157 if( maNewBounds
== maBounds
)
160 maBounds
= maNewBounds
;
161 if( std::count_if( maViewEntries
.begin(),
163 [this]( const ViewEntry
& rViewEntry
)
164 { return rViewEntry
.getViewLayer()->resize( this->maBounds
); }
170 // layer content invalid, update areas have wrong
171 // coordinates/not sensible anymore.
177 void Layer::clearUpdateRanges()
179 maUpdateAreas
.clear();
182 void Layer::clearContent()
184 // clear content on all view layers
185 for( const auto& rViewEntry
: maViewEntries
)
186 rViewEntry
.getViewLayer()->clearAll();
188 // layer content cleared, update areas are not sensible
196 LayerEndUpdate( const LayerEndUpdate
& ) = delete;
197 LayerEndUpdate
& operator=( const LayerEndUpdate
& ) = delete;
198 explicit LayerEndUpdate( LayerSharedPtr xLayer
) :
199 mpLayer(std::move( xLayer
))
202 ~LayerEndUpdate() { if(mpLayer
) mpLayer
->endUpdate(); }
205 LayerSharedPtr mpLayer
;
208 Layer::EndUpdater
Layer::beginUpdate()
210 if( maUpdateAreas
.count() )
212 // perform proper layer update. That means, setup proper
213 // clipping, and render each shape that intersects with
214 // the calculated update area
215 ::basegfx::B2DPolyPolygon
aClip( maUpdateAreas
.solveCrossovers() );
216 aClip
= ::basegfx::utils::stripNeutralPolygons(aClip
);
217 aClip
= ::basegfx::utils::stripDispensablePolygons(aClip
);
219 // actually, if there happen to be shapes with zero
220 // update area in the maUpdateAreas vector, the
221 // resulting clip polygon will be empty.
224 for( const auto& rViewEntry
: maViewEntries
)
226 const ViewLayerSharedPtr
& pViewLayer
= rViewEntry
.getViewLayer();
228 // set clip to all view layers and
229 pViewLayer
->setClip( aClip
);
231 // clear update area on all view layers
239 return std::make_shared
<LayerEndUpdate
>(shared_from_this());
242 void Layer::endUpdate()
248 basegfx::B2DPolyPolygon aEmptyClip
;
249 for( const auto& rViewEntry
: maViewEntries
)
250 rViewEntry
.getViewLayer()->setClip( aEmptyClip
);
256 bool Layer::isInsideUpdateArea( ShapeSharedPtr
const& rShape
) const
258 return maUpdateAreas
.overlaps( rShape
->getUpdateArea() );
261 LayerSharedPtr
Layer::createBackgroundLayer()
263 return LayerSharedPtr(new Layer( BackgroundLayer
));
266 LayerSharedPtr
Layer::createLayer( )
268 return LayerSharedPtr( new Layer
);
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */