spacebox/src/Carousel.hpp

91 lines
2.3 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#pragma once
#include <cstdint>
#include <iterator>
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/common.hpp"
#include "glm/gtx/integer.hpp"
namespace sb
{
class Carousel
{
private:
std::uint32_t offset = 0;
public:
template<typename Container>
auto current(Container& container) const
{
auto location = container.begin();
if (location != container.end())
{
std::advance(location, offset);
return location;
}
else
{
throw std::out_of_range("Container is empty");
}
}
template<typename Container>
void next(const Container& container)
{
offset = ++offset % container.size();
}
template<typename Container>
void previous(const Container& container)
{
offset = glm::mod(--offset, container.size());
}
template<typename Container>
void increment(const Container& container, int amount)
{
offset = glm::mod(offset + amount, container.size());
}
void beginning()
{
offset = 0;
}
template<typename Container>
void end(const Container& container)
{
offset = container.size() - 1;
}
/* Return true if the carousel currently points to the location at the beginning of the container. */
bool at_beginning() const
{
return offset == 0;
}
/* Return true if the carousel currently points to the location at the end of the container. */
template<typename Container>
bool at_end(const Container& container)
{
return offset >= container.size() - 1;
}
};
}