1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Added constructor with target size argument for CAnimImage

The image will be scaled to the given width (height is ignored)
This commit is contained in:
Arseniy Lartsev 2020-03-31 19:52:22 +02:00 committed by Andrii Danylchenko
parent 9e3c4b69c4
commit c502cfc223
2 changed files with 59 additions and 9 deletions

View File

@ -228,11 +228,45 @@ CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Gr
init();
}
CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
anim(Anim),
frame(Frame),
group(Group),
player(-1),
flags(Flags),
scaledSize(targetPos.w, targetPos.h)
{
pos.x += targetPos.x;
pos.y += targetPos.y;
init();
}
size_t CAnimImage::size()
{
return anim->size(group);
}
bool CAnimImage::isScaled() const
{
return (scaledSize.x != 0);
}
void CAnimImage::setSizeFromImage(const IImage &img)
{
if (isScaled())
{
// At the time of writing this, IImage had no method to scale to different aspect ratio
// Therefore, have to ignore the target height and preserve original aspect ratio
pos.w = scaledSize.x;
pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
}
else
{
pos.w = img.width();
pos.h = img.height();
}
}
void CAnimImage::init()
{
visible = true;
@ -242,10 +276,7 @@ void CAnimImage::init()
auto img = anim->getImage(frame, group);
if (img)
{
pos.w = img->width();
pos.h = img->height();
}
setSizeFromImage(*img);
}
CAnimImage::~CAnimImage()
@ -259,10 +290,26 @@ void CAnimImage::showAll(SDL_Surface * to)
if(flags & CShowableAnim::BASE && frame != 0)
if(auto img = anim->getImage(0, group))
img->draw(to, pos.x, pos.y);
{
if (isScaled())
{
auto scaled = img->scaleFast(float(scaledSize.x) / img->width());
scaled->draw(to, pos.x, pos.y);
}
else
img->draw(to, pos.x, pos.y);
}
if(auto img = anim->getImage(frame, group))
img->draw(to, pos.x, pos.y);
{
if (isScaled())
{
auto scaled = img->scaleFast(float(scaledSize.x) / img->width());
scaled->draw(to, pos.x, pos.y);
}
else
img->draw(to, pos.x, pos.y);
}
}
void CAnimImage::setFrame(size_t Frame, size_t Group)
@ -278,8 +325,7 @@ void CAnimImage::setFrame(size_t Frame, size_t Group)
{
if (flags & CShowableAnim::PLAYER_COLORED)
img->playerColored(player);
pos.w = img->width();
pos.h = img->height();
setSizeFromImage(*img);
}
}
else

View File

@ -17,6 +17,7 @@ struct Rect;
class CAnimImage;
class CLabel;
class CAnimation;
class IImage;
// Image class
class CPicture : public CIntObject
@ -74,14 +75,17 @@ private:
size_t group;
PlayerColor player;
ui8 flags;
const Point scaledSize;
bool isScaled() const;
void setSizeFromImage(const IImage &img);
void init();
public:
bool visible;
CAnimImage(const std::string & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
~CAnimImage();
//size of animation