spacebox/src/Box.cpp

283 lines
4.3 KiB
C++

#include "Box.hpp"
Box::Box(const glm::vec2& nw, const glm::vec2& size) : rect({nw.x, nw.y, size.x, size.y}) {}
float Box::get_x() const
{
return rect.x;
}
float Box::get_y() const
{
return rect.y;
}
float Box::get_w() const
{
return rect.w;
}
float Box::get_h() const
{
return rect.h;
}
void Box::set_x(float x)
{
rect.x = x;
}
void Box::set_y(float y)
{
rect.y = y;
}
void Box::set_w(float w)
{
rect.w = w;
}
void Box::set_h(float h)
{
rect.h = h;
}
glm::vec2 Box::get_size() const
{
return glm::vec2(get_w(), get_h());
}
float Box::get_area() const
{
return get_w() * get_h();
}
void Box::set_size(const glm::vec2& size, bool preserve_center)
{
glm::vec2 center = get_center();
set_w(size.x);
set_h(size.y);
if (preserve_center)
{
set_center(center);
}
}
float Box::get_top() const
{
return get_y();
}
float Box::get_right() const
{
return get_x() + get_w();
}
float Box::get_bottom() const
{
return get_y() + get_h();
}
float Box::get_left() const
{
return get_x();
}
float Box::get_center_x() const
{
return get_left() + get_w() / 2;
}
float Box::get_center_y() const
{
return get_top() + get_h() / 2;
}
void Box::set_top(float top)
{
set_y(top);
}
void Box::set_right(float right, bool drag)
{
float delta = right - get_right();
if (!drag)
{
move({delta, 0});
}
else
{
drag_right(delta);
}
}
void Box::drag_right(float delta)
{
float previous_right = get_right();
set_right(get_right() + delta);
set_w(get_w() + previous_right - get_right());
}
void Box::set_bottom(float bottom)
{
move({0, bottom - get_bottom()});
}
void Box::set_left(float left, bool drag)
{
if (!drag)
{
set_x(left);
}
else
{
drag_left(left - get_left());
}
}
void Box::drag_left(float delta)
{
float previous_left = get_left();
set_left(get_left() + delta);
set_w(get_w() + previous_left - get_left());
}
void Box::set_center_x(float x)
{
move({x - get_center_x(), 0});
}
void Box::set_center_y(float y)
{
move({0, y - get_center_y()});
}
glm::vec2 Box::get_nw() const
{
return {get_x(), get_y()};
}
glm::vec2 Box::get_north() const
{
return glm::vec2(get_x() + get_w() / 2, get_y());
}
glm::vec2 Box::get_ne() const
{
return glm::vec2(get_right(), get_y());
}
glm::vec2 Box::get_east() const
{
return glm::vec2(get_right(), get_top() + get_h() / 2);
}
glm::vec2 Box::get_se() const
{
return glm::vec2(get_right(), get_bottom());
}
glm::vec2 Box::get_south() const
{
return glm::vec2(get_left() + get_w() / 2, get_bottom());
}
glm::vec2 Box::get_sw() const
{
return glm::vec2(get_left(), get_bottom());
}
glm::vec2 Box::get_west() const
{
return glm::vec2(get_x(), get_y() + get_h() / 2);
}
glm::vec2 Box::get_center() const
{
return glm::vec2(get_x() + get_w() / 2, get_y() + get_h() / 2);
}
void Box::set_nw(const glm::vec2& nw)
{
move(nw - get_nw());
}
void Box::set_north(const glm::vec2& n)
{
move(n - get_north());
}
void Box::set_ne(const glm::vec2& ne)
{
move(ne - get_ne());
}
void Box::set_east(const glm::vec2& e)
{
move(e - get_east());
}
void Box::set_se(const glm::vec2& se)
{
move(se - get_se());
}
void Box::set_south(const glm::vec2& s)
{
move(s - get_south());
}
void Box::set_west(const glm::vec2& w)
{
move(w - get_west());
}
void Box::set_center(const glm::vec2& center)
{
move(center - get_center());
}
SDL_FRect* Box::get_rect()
{
return ▭
}
SDL_Rect Box::get_int_rect() const
{
return {static_cast<int>(rect.x), static_cast<int>(rect.y), static_cast<int>(rect.w), static_cast<int>(rect.h)};
}
void Box::clear()
{
set_nw(glm::vec2(0, 0));
set_size(glm::vec2(0, 0));
}
void Box::scale(float amount, bool preserve_center)
{
glm::vec2 center = get_center();
set_w(get_w() * amount);
set_h(get_h() * amount);
if (preserve_center)
{
set_center(center);
}
}
void Box::move(const glm::vec2& delta)
{
set_x(get_x() + delta.x);
set_y(get_y() + delta.y);
}
std::ostream& Box::to_string (std::ostream& out) const
{
out << "{(" << rect.x << ", " << rect.y << "), (" << rect.w << ", " << rect.h << ")}";
return out;
}
std::ostream& operator<< (std::ostream& out, const Box& box)
{
return box.to_string(out);
}