Add operator+= and -= to Viewport to allow panning.
This commit is contained in:
parent
71a6ddccdd
commit
163d4e0e13
4 changed files with 63 additions and 2 deletions
|
@ -43,6 +43,7 @@ namespace dk {
|
||||||
|
|
||||||
typename coords::scalar_type tiles_count ( void ) const;
|
typename coords::scalar_type tiles_count ( void ) const;
|
||||||
const coords& map_size ( void ) const { return m_size; }
|
const coords& map_size ( void ) const { return m_size; }
|
||||||
|
const coords& tile_size ( void ) const { return m_tilesize; }
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
Layer<T, D>& push_layer ( BaseMapSource<D>* parTilemap, int parIndex );
|
Layer<T, D>& push_layer ( BaseMapSource<D>* parTilemap, int parIndex );
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include "doorkeeper/components/layer.hpp"
|
#include "doorkeeper/components/layer.hpp"
|
||||||
#include "doorkeeper/components/tilecoords.hpp"
|
#include "doorkeeper/components/tilecoords.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
namespace dk {
|
namespace dk {
|
||||||
template <uint32_t D>
|
template <uint32_t D>
|
||||||
|
@ -35,6 +36,7 @@ namespace dk {
|
||||||
class Viewport {
|
class Viewport {
|
||||||
public:
|
public:
|
||||||
typedef Vector<D> coords;
|
typedef Vector<D> coords;
|
||||||
|
typedef VectorT<CoordinateAccumType, D> coords_f;
|
||||||
|
|
||||||
Viewport ( const Viewport& parOther ) = default;
|
Viewport ( const Viewport& parOther ) = default;
|
||||||
explicit Viewport ( Tyler<D>& parTyler );
|
explicit Viewport ( Tyler<D>& parTyler );
|
||||||
|
@ -42,9 +44,12 @@ namespace dk {
|
||||||
~Viewport ( void ) noexcept = default;
|
~Viewport ( void ) noexcept = default;
|
||||||
|
|
||||||
Viewport& operator= ( const Viewport& ) = default;
|
Viewport& operator= ( const Viewport& ) = default;
|
||||||
|
Viewport& operator+= ( const coords_f& parValue );
|
||||||
|
Viewport& operator-= ( const coords_f& parValue );
|
||||||
|
|
||||||
void setFrom ( const coords& parFrom );
|
void setFrom ( const coords& parFrom );
|
||||||
coords size ( void ) const;
|
coords size ( void ) const;
|
||||||
|
coords pixel_offset ( void ) const;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename Layer<T, D>::iterator begin ( Layer<T, D>& parLayer ) const;
|
typename Layer<T, D>::iterator begin ( Layer<T, D>& parLayer ) const;
|
||||||
|
@ -60,8 +65,11 @@ namespace dk {
|
||||||
typename Layer<T, D>::const_iterator cend ( const Layer<T, D>& parLayer ) const;
|
typename Layer<T, D>::const_iterator cend ( const Layer<T, D>& parLayer ) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void clip_pixel_offset ( void );
|
||||||
|
|
||||||
TileCoords<D> m_view;
|
TileCoords<D> m_view;
|
||||||
Tyler<D>& m_tyler;
|
Tyler<D>& m_tyler;
|
||||||
|
coords_f m_pixel_offset;
|
||||||
};
|
};
|
||||||
} //namespace dk
|
} //namespace dk
|
||||||
|
|
||||||
|
|
|
@ -18,14 +18,16 @@
|
||||||
namespace dk {
|
namespace dk {
|
||||||
template <uint32_t D>
|
template <uint32_t D>
|
||||||
Viewport<D>::Viewport (Tyler<D>& parTyler) :
|
Viewport<D>::Viewport (Tyler<D>& parTyler) :
|
||||||
m_tyler(parTyler)
|
m_tyler(parTyler),
|
||||||
|
m_pixel_offset(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template <uint32_t D>
|
template <uint32_t D>
|
||||||
Viewport<D>::Viewport (Tyler<D>& parTyler, const coords& parSize, const coords& parPos) :
|
Viewport<D>::Viewport (Tyler<D>& parTyler, const coords& parSize, const coords& parPos) :
|
||||||
m_view(parPos, parSize - 1),
|
m_view(parPos, parSize - 1),
|
||||||
m_tyler(parTyler)
|
m_tyler(parTyler),
|
||||||
|
m_pixel_offset(0)
|
||||||
{
|
{
|
||||||
DK_ASSERT(parPos < parPos + parSize); //parSize > 0
|
DK_ASSERT(parPos < parPos + parSize); //parSize > 0
|
||||||
DK_ASSERT(parSize + parPos <= parTyler.map_size());
|
DK_ASSERT(parSize + parPos <= parTyler.map_size());
|
||||||
|
@ -83,4 +85,49 @@ namespace dk {
|
||||||
typename Viewport<D>::coords Viewport<D>::size() const {
|
typename Viewport<D>::coords Viewport<D>::size() const {
|
||||||
return (m_view.upper() + 1);
|
return (m_view.upper() + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <uint32_t D>
|
||||||
|
Viewport<D>& Viewport<D>::operator+= (const coords_f& parValue) {
|
||||||
|
m_pixel_offset += parValue;
|
||||||
|
clip_pixel_offset();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint32_t D>
|
||||||
|
Viewport<D>& Viewport<D>::operator-= (const coords_f& parValue) {
|
||||||
|
m_pixel_offset -= parValue;
|
||||||
|
clip_pixel_offset();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint32_t D>
|
||||||
|
void Viewport<D>::clip_pixel_offset() {
|
||||||
|
const auto& tile_size = m_tyler.tile_size();
|
||||||
|
for (uint32_t z = 0; z < D; ++z) {
|
||||||
|
auto offs = static_cast<CoordinateScalarType>(m_pixel_offset[z]);
|
||||||
|
if (std::abs(offs) >= tile_size[z]) {
|
||||||
|
auto curr_tile_size_f = static_cast<CoordinateAccumType>(tile_size[z]);
|
||||||
|
if (offs < 0) {
|
||||||
|
do {
|
||||||
|
offs += tile_size[z];
|
||||||
|
m_pixel_offset[z] += curr_tile_size_f;
|
||||||
|
--m_view;
|
||||||
|
} while(offs >= tile_size[z]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
DK_ASSERT(offs > 0);
|
||||||
|
do {
|
||||||
|
offs -= tile_size[z];
|
||||||
|
m_pixel_offset[z] -= curr_tile_size_f;
|
||||||
|
++m_view;
|
||||||
|
} while(offs >= tile_size[z]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint32_t D>
|
||||||
|
auto Viewport<D>::pixel_offset() const -> coords {
|
||||||
|
return static_cast<coords>(m_pixel_offset);
|
||||||
|
}
|
||||||
} //namespace dk
|
} //namespace dk
|
||||||
|
|
|
@ -105,4 +105,9 @@ TEST_F(asciimapsource, coordinates) {
|
||||||
EXPECT_EQ(expected_coords, returned_coords);
|
EXPECT_EQ(expected_coords, returned_coords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
full_view += dk::VectorT<dk::CoordinateAccumType, 2>(2.5f, -3.1f);
|
||||||
|
EXPECT_EQ(coords2(2, -3), full_view.pixel_offset());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue