2020-11-22 20:58:37 +00:00
|
|
|
#include <cmath>
|
2020-11-21 19:57:40 +00:00
|
|
|
#include "sdlpp_rectrenderer.hpp"
|
|
|
|
|
|
|
|
namespace SDLPP {
|
|
|
|
RectangleRender::RectangleRender( double x, double y, double w, double h,
|
|
|
|
const std::shared_ptr< Renderer > &r )
|
2021-03-12 21:33:46 +00:00
|
|
|
: RectangleRender( { x, y }, { w, h }, r ) {}
|
|
|
|
RectangleRender::RectangleRender( double x, double y, double w, double h,
|
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::shared_ptr< Texture > &t,
|
|
|
|
int source_x, int source_y, int source_width,
|
|
|
|
int source_height )
|
|
|
|
: RectangleRender( { x, y }, { w, h }, r, t, source_x, source_y,
|
|
|
|
source_width, source_height ) {}
|
|
|
|
RectangleRender::RectangleRender( double x, double y, double w, double h,
|
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::shared_ptr< Texture > &t,
|
|
|
|
const SDL_Rect &source_rect )
|
|
|
|
: RectangleRender( { x, y }, { w, h }, r, t, source_rect ) {}
|
|
|
|
RectangleRender::RectangleRender( double x, double y, double w, double h,
|
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::string &img_or_color,
|
|
|
|
bool is_polygon )
|
|
|
|
: RectangleRender( { x, y }, { w, h }, r, img_or_color, is_polygon ) {}
|
|
|
|
RectangleRender::RectangleRender( double x, double y, double w, double h,
|
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::string &img, int source_x,
|
|
|
|
int source_y, int source_width,
|
|
|
|
int source_height )
|
|
|
|
: RectangleRender( { x, y }, { w, h }, r, img, source_x, source_y,
|
|
|
|
source_width, source_height ) {}
|
|
|
|
RectangleRender::RectangleRender( double x, double y, double w, double h,
|
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::string &img,
|
|
|
|
const SDL_Rect &source_rect )
|
|
|
|
: RectangleRender( { x, y }, { w, h }, r, img, source_rect ) {}
|
|
|
|
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
|
|
|
|
const Vec2D< double > &size,
|
|
|
|
const std::shared_ptr< Renderer > &r )
|
2020-11-21 19:57:40 +00:00
|
|
|
: RenderObject( r ) {
|
2021-03-12 21:33:46 +00:00
|
|
|
original = top_left;
|
|
|
|
original_size = size;
|
2020-11-21 19:57:40 +00:00
|
|
|
updateSizeAndPosition();
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
|
|
|
|
const Vec2D< double > &size,
|
2020-11-21 19:57:40 +00:00
|
|
|
const std::shared_ptr< Renderer > &r,
|
2021-03-07 12:06:08 +00:00
|
|
|
const std::shared_ptr< Texture > &t,
|
|
|
|
int source_x, int source_y, int source_width,
|
|
|
|
int source_height )
|
2021-03-12 21:33:46 +00:00
|
|
|
: RectangleRender( top_left, size, r ) {
|
2021-03-07 12:06:08 +00:00
|
|
|
setTexture( t, source_x, source_y, source_width, source_height );
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
|
|
|
|
const Vec2D< double > &size,
|
2021-03-07 12:06:08 +00:00
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::shared_ptr< Texture > &t,
|
2021-03-07 13:06:55 +00:00
|
|
|
const SDL_Rect &source_rect )
|
2021-03-12 21:33:46 +00:00
|
|
|
: RectangleRender( top_left, size, r ) {
|
2021-03-07 12:06:08 +00:00
|
|
|
setTexture( t, source_rect );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
|
|
|
|
const Vec2D< double > &size,
|
2020-11-21 19:57:40 +00:00
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::string &img_or_color,
|
|
|
|
bool is_polygon )
|
2021-03-12 21:33:46 +00:00
|
|
|
: RectangleRender( top_left, size, r ) {
|
2020-11-21 19:57:40 +00:00
|
|
|
if ( !is_polygon ) {
|
|
|
|
setTexture( img_or_color );
|
|
|
|
} else {
|
|
|
|
setColor( img_or_color );
|
|
|
|
color = img_or_color;
|
|
|
|
}
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
|
|
|
|
const Vec2D< double > &size,
|
2021-03-07 12:06:08 +00:00
|
|
|
const std::shared_ptr< Renderer > &r,
|
|
|
|
const std::string &img, int source_x,
|
|
|
|
int source_y, int source_width,
|
|
|
|
int source_height )
|
2021-03-12 21:33:46 +00:00
|
|
|
: RectangleRender( top_left, size, r ) {
|
2021-03-07 12:06:08 +00:00
|
|
|
setTexture( img, source_x, source_y, source_width, source_height );
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
RectangleRender::RectangleRender( const Vec2D< double > &top_left,
|
|
|
|
const Vec2D< double > &size,
|
2021-03-07 12:06:08 +00:00
|
|
|
const std::shared_ptr< Renderer > &r,
|
2021-03-07 13:06:55 +00:00
|
|
|
const std::string &img,
|
|
|
|
const SDL_Rect &source_rect )
|
2021-03-12 21:33:46 +00:00
|
|
|
: RectangleRender( top_left, size, r ) {
|
2021-03-07 12:06:08 +00:00
|
|
|
setTexture( img, source_rect );
|
|
|
|
}
|
2020-11-21 19:57:40 +00:00
|
|
|
void RectangleRender::setColor( const std::string &color ) {
|
|
|
|
if ( !polygon ) {
|
|
|
|
polygon = std::make_shared< RectColider >( 0, 0, 1, 1 );
|
|
|
|
polygon->updateCollision( collisionPushX(), collisionPushY(),
|
2021-04-26 19:57:31 +00:00
|
|
|
collisionWidth(), collisionHeight(),
|
|
|
|
getId() );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
polygon->setColor( color );
|
|
|
|
}
|
|
|
|
void RectangleRender::setOutlineColor( const std::string &color ) {
|
|
|
|
if ( !polygon ) {
|
|
|
|
polygon = std::make_shared< RectColider >( 0, 0, 1, 1 );
|
|
|
|
polygon->updateCollision( collisionPushX(), collisionPushY(),
|
2021-04-26 19:57:31 +00:00
|
|
|
collisionWidth(), collisionHeight(),
|
|
|
|
getId() );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
polygon->setOutlineColor( color );
|
|
|
|
}
|
2021-03-12 21:33:46 +00:00
|
|
|
std::pair< Vec2D< double >, Vec2D< double > >
|
2020-11-21 19:57:40 +00:00
|
|
|
RectangleRender::getDoubleRect() const {
|
2021-03-12 21:33:46 +00:00
|
|
|
return { original, original_size };
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
int RectangleRender::leftmost() {
|
|
|
|
return rect.x;
|
|
|
|
}
|
|
|
|
int RectangleRender::topmost() {
|
|
|
|
return rect.y;
|
|
|
|
}
|
|
|
|
int RectangleRender::rightmost() {
|
|
|
|
return rect.x + rect.w;
|
|
|
|
}
|
|
|
|
int RectangleRender::bottommost() {
|
|
|
|
return rect.y + rect.h;
|
|
|
|
}
|
|
|
|
int RectangleRender::collisionPushX() {
|
|
|
|
return rect.x;
|
|
|
|
}
|
|
|
|
int RectangleRender::collisionPushY() {
|
|
|
|
return rect.y;
|
|
|
|
}
|
|
|
|
int RectangleRender::collisionWidth() {
|
|
|
|
return rect.w;
|
|
|
|
}
|
|
|
|
int RectangleRender::collisionHeight() {
|
|
|
|
return rect.h;
|
|
|
|
}
|
|
|
|
void RectangleRender::updateSizeAndPosition() {
|
|
|
|
updateXY();
|
|
|
|
auto dimension = renderer->getSmallerSide();
|
2021-03-12 21:33:46 +00:00
|
|
|
rect.x = std::round( current.getX() * dimension );
|
|
|
|
rect.y = std::round( current.getY() * dimension );
|
|
|
|
rect.w =
|
2021-03-13 17:36:29 +00:00
|
|
|
std::round( ( current.getX() + original_size.getX() ) * dimension ) -
|
|
|
|
rect.x;
|
2021-03-12 21:33:46 +00:00
|
|
|
rect.h =
|
2021-03-13 17:36:29 +00:00
|
|
|
std::round( ( current.getY() + original_size.getY() ) * dimension ) -
|
|
|
|
rect.y;
|
2021-05-31 12:30:28 +00:00
|
|
|
if(static_cast<uint64_t>(rect.w) < min_size.getX())
|
2021-05-02 12:03:26 +00:00
|
|
|
rect.w = min_size.getX();
|
2021-05-31 12:30:28 +00:00
|
|
|
if(static_cast<uint64_t>(rect.h) < min_size.getY())
|
2021-05-02 12:03:26 +00:00
|
|
|
rect.h = min_size.getY();
|
2020-11-21 19:57:40 +00:00
|
|
|
if ( polygon )
|
|
|
|
polygon->updateCollision( collisionPushX(), collisionPushY(),
|
2021-04-26 19:57:31 +00:00
|
|
|
collisionWidth(), collisionHeight(),
|
|
|
|
getId() );
|
2020-11-21 19:57:40 +00:00
|
|
|
for ( auto &x : collisions ) {
|
|
|
|
x->updateCollision( collisionPushX(), collisionPushY(),
|
2021-04-26 19:57:31 +00:00
|
|
|
collisionWidth(), collisionHeight(), getId() );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SDL_Rect RectangleRender::getRect() {
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
std::shared_ptr< RenderObject > RectangleRender::copySelf() {
|
2020-12-18 15:02:02 +00:00
|
|
|
auto ret = std::make_shared< RectangleRender >( *this );
|
2021-03-07 12:06:08 +00:00
|
|
|
copyTo( ret );
|
2020-12-18 15:02:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2021-03-07 12:06:08 +00:00
|
|
|
void RectangleRender::copyTo( std::shared_ptr< RenderObject > other ) {
|
|
|
|
RenderObject::copyTo( other );
|
2020-11-21 19:57:40 +00:00
|
|
|
}
|
|
|
|
std::string RectangleRender::getColor() const {
|
|
|
|
return color;
|
|
|
|
}
|
2021-05-02 12:03:26 +00:00
|
|
|
void RectangleRender::setMinWidth( uint64_t width ) {
|
|
|
|
min_size = {width, min_size.getY()};
|
|
|
|
}
|
|
|
|
void RectangleRender::setMinHeight( uint64_t height ) {
|
|
|
|
min_size = {min_size.getX(), height};
|
|
|
|
}
|
|
|
|
void RectangleRender::setSize( Vec2D< double > size ) {
|
|
|
|
original_size = size;
|
|
|
|
updateSizeAndPosition();
|
|
|
|
}
|
2020-11-21 19:57:40 +00:00
|
|
|
} // namespace SDLPP
|