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>
26 #include <osl/diagnose.h>
30 using namespace ::com::sun::star
;
32 namespace slideshow::internal
34 Layer::Layer( Dummy
) :
39 mbBackgroundLayer(true),
49 mbBackgroundLayer(false),
54 ViewLayerSharedPtr
Layer::addView( const ViewSharedPtr
& rNewView
)
56 OSL_ASSERT( rNewView
);
58 ViewEntryVector::iterator aIter
;
59 const ViewEntryVector::iterator
aEnd( maViewEntries
.end() );
60 if( (aIter
=std::find_if( maViewEntries
.begin(),
62 [&rNewView
]( const ViewEntry
& rViewEntry
)
63 { return rViewEntry
.getView() == rNewView
; } ) ) != aEnd
)
65 // already added - just return existing layer
66 return aIter
->mpViewLayer
;
70 // not yet added - create new view layer
71 ViewLayerSharedPtr pNewLayer
;
72 if( mbBackgroundLayer
)
75 pNewLayer
= rNewView
->createViewLayer(maBounds
);
78 maViewEntries
.emplace_back( rNewView
,
81 return maViewEntries
.back().mpViewLayer
;
84 ViewLayerSharedPtr
Layer::removeView( const ViewSharedPtr
& rView
)
88 ViewEntryVector::iterator aIter
;
89 const ViewEntryVector::iterator
aEnd( maViewEntries
.end() );
90 if( (aIter
=std::find_if( maViewEntries
.begin(),
92 [&rView
]( const ViewEntry
& rViewEntry
)
93 { return rViewEntry
.getView() == rView
; } ) ) == aEnd
)
95 // View was not added/is already removed
96 return ViewLayerSharedPtr();
99 OSL_ENSURE( std::count_if( maViewEntries
.begin(),
101 [&rView
]( const ViewEntry
& rViewEntry
)
102 { return rViewEntry
.getView() == rView
; } ) == 1,
103 "Layer::removeView(): view added multiple times" );
105 ViewLayerSharedPtr
pRet( aIter
->mpViewLayer
);
106 maViewEntries
.erase(aIter
);
111 void Layer::setShapeViews( ShapeSharedPtr
const& rShape
) const
113 rShape
->clearAllViewLayers();
115 for( const auto& rViewEntry
: maViewEntries
)
116 rShape
->addViewLayer( rViewEntry
.getViewLayer(), false );
119 void Layer::setPriority( const ::basegfx::B1DRange
& rPrioRange
)
121 if( !mbBackgroundLayer
)
123 for( const auto& rViewEntry
: maViewEntries
)
124 rViewEntry
.getViewLayer()->setPriority( rPrioRange
);
128 void Layer::addUpdateRange( ::basegfx::B2DRange
const& rUpdateRange
)
130 // TODO(Q1): move this to B2DMultiRange
131 if( !rUpdateRange
.isEmpty() )
132 maUpdateAreas
.appendElement( rUpdateRange
,
133 basegfx::B2VectorOrientation::Positive
);
136 void Layer::updateBounds( ShapeSharedPtr
const& rShape
)
138 if( !mbBackgroundLayer
)
143 maNewBounds
.expand( rShape
->getUpdateArea() );
146 mbBoundsDirty
= true;
149 bool Layer::commitBounds()
151 mbBoundsDirty
= false;
153 if( mbBackgroundLayer
)
156 if( maNewBounds
== maBounds
)
159 maBounds
= maNewBounds
;
160 if( std::count_if( maViewEntries
.begin(),
162 [this]( const ViewEntry
& rViewEntry
)
163 { return rViewEntry
.getViewLayer()->resize( this->maBounds
); }
169 // layer content invalid, update areas have wrong
170 // coordinates/not sensible anymore.
176 void Layer::clearUpdateRanges()
178 maUpdateAreas
.clear();
181 void Layer::clearContent()
183 // clear content on all view layers
184 for( const auto& rViewEntry
: maViewEntries
)
185 rViewEntry
.getViewLayer()->clearAll();
187 // layer content cleared, update areas are not sensible
195 LayerEndUpdate( const LayerEndUpdate
& ) = delete;
196 LayerEndUpdate
& operator=( const LayerEndUpdate
& ) = delete;
197 explicit LayerEndUpdate( LayerSharedPtr
const& rLayer
) :
201 ~LayerEndUpdate() { if(mpLayer
) mpLayer
->endUpdate(); }
204 LayerSharedPtr mpLayer
;
207 Layer::EndUpdater
Layer::beginUpdate()
209 if( maUpdateAreas
.count() )
211 // perform proper layer update. That means, setup proper
212 // clipping, and render each shape that intersects with
213 // the calculated update area
214 ::basegfx::B2DPolyPolygon
aClip( maUpdateAreas
.solveCrossovers() );
215 aClip
= ::basegfx::utils::stripNeutralPolygons(aClip
);
216 aClip
= ::basegfx::utils::stripDispensablePolygons(aClip
);
218 // actually, if there happen to be shapes with zero
219 // update area in the maUpdateAreas vector, the
220 // resulting clip polygon will be empty.
223 for( const auto& rViewEntry
: maViewEntries
)
225 const ViewLayerSharedPtr
& pViewLayer
= rViewEntry
.getViewLayer();
227 // set clip to all view layers and
228 pViewLayer
->setClip( aClip
);
230 // clear update area on all view layers
238 return std::make_shared
<LayerEndUpdate
>(shared_from_this());
241 void Layer::endUpdate()
247 basegfx::B2DPolyPolygon aEmptyClip
;
248 for( const auto& rViewEntry
: maViewEntries
)
249 rViewEntry
.getViewLayer()->setClip( aEmptyClip
);
255 bool Layer::isInsideUpdateArea( ShapeSharedPtr
const& rShape
) const
257 return maUpdateAreas
.overlaps( rShape
->getUpdateArea() );
260 LayerSharedPtr
Layer::createBackgroundLayer()
262 return LayerSharedPtr(new Layer( BackgroundLayer
));
265 LayerSharedPtr
Layer::createLayer( )
267 return LayerSharedPtr( new Layer
);
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */