/* 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 .
*/
namespace dk {
template
struct LayeredViewport::ViewportItem {
ViewportItem ( const TileInfo& parTInfo, Tyler& parTyler );
ViewportItem ( ViewportItem&& ) = default;
ViewportItem ( const ViewportItem& ) = delete;
TileInfo tile_info;
Viewport viewport;
int ref_count;
bool operator== ( const TileInfo& parOther ) const a_pure;
ViewportItem& operator= ( const ViewportItem& ) = default;
ViewportItem& operator= ( ViewportItem&& );
};
template
LayeredViewport::ViewportItem::ViewportItem (const TileInfo& parTInfo, Tyler& parTyler) :
tile_info(parTInfo),
viewport(parTyler, &tile_info),
ref_count(1)
{
}
template
bool LayeredViewport::ViewportItem::operator== (const TileInfo& parOther) const {
return tile_info == parOther;
}
template
typename LayeredViewport::ViewportItem& LayeredViewport::ViewportItem::operator= (ViewportItem&& parOther) {
tile_info = std::move(parOther.tile_info);
viewport = std::move(parOther.viewport);
ref_count = parOther.ref_count;
return *this;
}
template
LayeredViewport::LayeredViewport (LayeredViewport&& parOther) :
m_views(std::move(parOther.m_views)),
m_tyler(parOther.m_tyler),
m_tyler_id(std::move(m_tyler_id))
{
}
template
LayeredViewport::LayeredViewport (Tyler& parTyler) :
m_tyler(&parTyler)
{
DK_ASSERT(m_tyler);
for (const auto& layer : *m_tyler) {
this->on_layer(layer, true);
}
using std::placeholders::_1;
using std::placeholders::_2;
m_tyler_id = m_tyler->register_for_layeradd(std::bind(&LayeredViewport::on_layer, this, _1, _2));
}
template
LayeredViewport::~LayeredViewport() noexcept {
m_tyler->unregister_for_layeradd(m_tyler_id);
}
template
LayeredViewport& LayeredViewport::operator+= (const coords_f& parValue) {
for (auto& view : m_views) {
view.viewport += parValue;
}
return *this;
}
template
LayeredViewport& LayeredViewport::operator-= (const coords_f& parValue) {
for (auto& view : m_views) {
view.viewport -= parValue;
}
return *this;
}
template
void LayeredViewport::on_layer (const LayerBase& parLayer, bool parAdded) {
const TileInfo tinfo(parLayer);
auto it_found = std::find(m_views.begin(), m_views.end(), tinfo);
if (parAdded) {
if (m_views.end() == it_found) {
m_views.emplace_back(ViewportItem(tinfo, *m_tyler));
}
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);
}
}
}
template
auto LayeredViewport::begin() -> iterator {
IterableViewport (LayeredViewport::*vpfunc)(const LayerBase&) = &LayeredViewport::viewport;
boost::function(const LayerBase&)> functor = boost::bind(vpfunc, this, _1);
auto it_beg = m_tyler->begin();
return boost::make_transform_iterator(it_beg, functor);
}
template
auto LayeredViewport::begin() const -> const_iterator {
const IterableViewport (LayeredViewport::*vpfunc)(const LayerBase&) const = &LayeredViewport::viewport;
return const_iterator(m_tyler->begin(), boost::bind(&vpfunc, this, _1));
}
template
auto LayeredViewport::end() -> iterator {
IterableViewport (LayeredViewport::*vpfunc)(const LayerBase&) = &LayeredViewport::viewport;
boost::function(const LayerBase&)> functor = boost::bind(vpfunc, this, _1);
return boost::make_transform_iterator(m_tyler->end(), functor);
}
template
auto LayeredViewport::end() const -> const_iterator {
const IterableViewport (LayeredViewport::*vpfunc)(const LayerBase&) const = &LayeredViewport::viewport;
return const_iterator(m_tyler->end(), boost::bind(&vpfunc, this, _1));
}
template
IterableViewport LayeredViewport::viewport (const LayerBase& parLayer) {
return const_cast*>(this)->viewport(parLayer);
}
template
const IterableViewport LayeredViewport::viewport (const LayerBase& parLayer) const {
const TileInfo tinfo(parLayer);
auto it_found = std::find(m_views.begin(), m_views.end(), tinfo);
DK_ASSERT(m_views.end() != it_found);
return IterableViewport(&it_found->viewport, &parLayer);
}
} //namespace dk