DoorKeeper/include/doorkeeper/implem/layeredviewport.inl

160 lines
5.1 KiB
Text
Raw Normal View History

2015-08-26 16:08:05 +00:00
/* Copyright 2015, Michele Santullo
* This file is part of DoorKeeper.
*
* DoorKeeper is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DoorKeeper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DoorKeeper. If not, see <http://www.gnu.org/licenses/>.
*/
namespace dk {
2015-08-31 15:11:11 +00:00
template <uint32_t D>
struct LayeredViewport<D>::ViewportItem {
ViewportItem ( const TileInfo<D>& parTInfo, Tyler<D>& parTyler );
ViewportItem ( ViewportItem&& ) = default;
2015-09-01 15:39:39 +00:00
ViewportItem ( const ViewportItem& ) = delete;
2015-08-31 15:11:11 +00:00
TileInfo<D> tile_info;
Viewport<D> viewport;
int ref_count;
bool operator== ( const TileInfo<D>& parOther ) const a_pure;
2015-09-01 15:39:39 +00:00
ViewportItem& operator= ( const ViewportItem& ) = default;
2015-09-02 15:52:13 +00:00
ViewportItem& operator= ( ViewportItem&& );
2015-08-31 15:11:11 +00:00
};
template <uint32_t D>
LayeredViewport<D>::ViewportItem::ViewportItem (const TileInfo<D>& parTInfo, Tyler<D>& parTyler) :
tile_info(parTInfo),
viewport(parTyler, &tile_info),
ref_count(1)
{
}
template <uint32_t D>
bool LayeredViewport<D>::ViewportItem::operator== (const TileInfo<D>& parOther) const {
return tile_info == parOther;
}
2015-09-02 15:52:13 +00:00
template <uint32_t D>
typename LayeredViewport<D>::ViewportItem& LayeredViewport<D>::ViewportItem::operator= (ViewportItem&& parOther) {
tile_info = std::move(parOther.tile_info);
viewport = std::move(parOther.viewport);
ref_count = parOther.ref_count;
return *this;
}
2015-09-01 15:39:39 +00:00
template <uint32_t D>
LayeredViewport<D>::LayeredViewport (LayeredViewport<D>&& parOther) :
m_views(std::move(parOther.m_views)),
m_tyler(parOther.m_tyler),
m_tyler_id(std::move(m_tyler_id))
{
}
2015-08-26 16:08:05 +00:00
template <uint32_t D>
LayeredViewport<D>::LayeredViewport (Tyler<D>& parTyler) :
2015-09-02 15:52:13 +00:00
m_tyler(&parTyler)
2015-08-26 16:08:05 +00:00
{
2015-09-02 15:52:13 +00:00
DK_ASSERT(m_tyler);
for (const auto& layer : *m_tyler) {
2015-08-31 15:11:11 +00:00
this->on_layer(layer, true);
2015-08-28 15:31:05 +00:00
}
2015-08-31 15:11:11 +00:00
using std::placeholders::_1;
using std::placeholders::_2;
2015-09-02 15:52:13 +00:00
m_tyler_id = m_tyler->register_for_layeradd(std::bind(&LayeredViewport<D>::on_layer, this, _1, _2));
2015-08-26 16:08:05 +00:00
}
template <uint32_t D>
2015-08-28 15:31:05 +00:00
LayeredViewport<D>::~LayeredViewport() noexcept {
2015-09-02 15:52:13 +00:00
m_tyler->unregister_for_layeradd(m_tyler_id);
2015-08-26 16:08:05 +00:00
}
template <uint32_t D>
LayeredViewport<D>& LayeredViewport<D>::operator+= (const coords_f& parValue) {
2015-08-27 15:12:55 +00:00
for (auto& view : m_views) {
2015-08-31 15:11:11 +00:00
view.viewport += parValue;
2015-08-27 15:12:55 +00:00
}
2015-08-26 16:08:05 +00:00
return *this;
}
template <uint32_t D>
LayeredViewport<D>& LayeredViewport<D>::operator-= (const coords_f& parValue) {
2015-08-27 15:12:55 +00:00
for (auto& view : m_views) {
2015-08-31 15:11:11 +00:00
view.viewport -= parValue;
2015-08-27 15:12:55 +00:00
}
2015-08-26 16:08:05 +00:00
return *this;
}
template <uint32_t D>
2015-08-31 15:11:11 +00:00
void LayeredViewport<D>::on_layer (const LayerBase<D>& parLayer, bool parAdded) {
const TileInfo<D> tinfo(parLayer);
auto it_found = std::find(m_views.begin(), m_views.end(), tinfo);
2015-08-26 16:08:05 +00:00
2015-08-31 15:11:11 +00:00
if (parAdded) {
if (m_views.end() == it_found) {
2015-09-02 15:52:13 +00:00
m_views.emplace_back(ViewportItem(tinfo, *m_tyler));
2015-08-31 15:11:11 +00:00
}
else {
++it_found->ref_count;
}
}
else {
DK_ASSERT(m_views.end() != it_found);
--it_found->ref_count;
if (0 == it_found->ref_count) {
m_views.erase(it_found);
}
}
2015-08-26 16:08:05 +00:00
}
2015-08-27 15:12:55 +00:00
template <uint32_t D>
auto LayeredViewport<D>::begin() -> iterator {
2015-09-01 15:39:39 +00:00
IterableViewport<D> (LayeredViewport<D>::*vpfunc)(const LayerBase<D>&) = &LayeredViewport<D>::viewport;
boost::function<IterableViewport<D>(const LayerBase<D>&)> functor = boost::bind(vpfunc, this, _1);
2015-09-02 15:52:13 +00:00
auto it_beg = m_tyler->begin();
2015-09-01 15:39:39 +00:00
return boost::make_transform_iterator(it_beg, functor);
2015-08-31 15:11:11 +00:00
}
2015-08-27 15:12:55 +00:00
template <uint32_t D>
auto LayeredViewport<D>::begin() const -> const_iterator {
2015-09-01 15:39:39 +00:00
const IterableViewport<D> (LayeredViewport<D>::*vpfunc)(const LayerBase<D>&) const = &LayeredViewport<D>::viewport;
2015-09-02 15:52:13 +00:00
return const_iterator(m_tyler->begin(), boost::bind(&vpfunc, this, _1));
2015-08-27 15:12:55 +00:00
}
template <uint32_t D>
auto LayeredViewport<D>::end() -> iterator {
2015-09-01 15:39:39 +00:00
IterableViewport<D> (LayeredViewport<D>::*vpfunc)(const LayerBase<D>&) = &LayeredViewport<D>::viewport;
boost::function<IterableViewport<D>(const LayerBase<D>&)> functor = boost::bind(vpfunc, this, _1);
2015-09-02 15:52:13 +00:00
return boost::make_transform_iterator(m_tyler->end(), functor);
2015-08-31 15:11:11 +00:00
}
2015-08-27 15:12:55 +00:00
template <uint32_t D>
auto LayeredViewport<D>::end() const -> const_iterator {
2015-09-01 15:39:39 +00:00
const IterableViewport<D> (LayeredViewport<D>::*vpfunc)(const LayerBase<D>&) const = &LayeredViewport<D>::viewport;
2015-09-02 15:52:13 +00:00
return const_iterator(m_tyler->end(), boost::bind(&vpfunc, this, _1));
2015-08-27 15:12:55 +00:00
}
template <uint32_t D>
2015-08-31 15:11:11 +00:00
IterableViewport<D> LayeredViewport<D>::viewport (const LayerBase<D>& parLayer) {
2015-09-02 15:52:13 +00:00
return const_cast<const LayeredViewport<D>*>(this)->viewport(parLayer);
2015-08-31 15:11:11 +00:00
}
2015-08-27 15:12:55 +00:00
template <uint32_t D>
2015-08-31 15:11:11 +00:00
const IterableViewport<D> LayeredViewport<D>::viewport (const LayerBase<D>& parLayer) const {
2015-09-02 15:52:13 +00:00
const TileInfo<D> tinfo(parLayer);
auto it_found = std::find(m_views.begin(), m_views.end(), tinfo);
DK_ASSERT(m_views.end() != it_found);
return IterableViewport<D>(&it_found->viewport, &parLayer);
2015-08-27 15:12:55 +00:00
}
2015-08-26 16:08:05 +00:00
} //namespace dk