2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* Images.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2014-07-15 10:14:49 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../gui/CIntObject.h"
|
2022-12-08 19:41:02 +02:00
|
|
|
#include "../battle/BattleConstants.h"
|
2023-08-23 14:07:50 +02:00
|
|
|
#include "../../lib/filesystem/ResourcePath.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-01-18 17:32:57 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
2023-01-17 22:01:35 +02:00
|
|
|
class Rect;
|
2023-01-18 17:32:57 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
class CAnimImage;
|
|
|
|
class CLabel;
|
|
|
|
class CAnimation;
|
2020-03-31 19:52:22 +02:00
|
|
|
class IImage;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
// Image class
|
|
|
|
class CPicture : public CIntObject
|
|
|
|
{
|
2023-01-30 17:18:59 +02:00
|
|
|
std::shared_ptr<IImage> bg;
|
2024-08-15 00:21:02 +02:00
|
|
|
std::function<void()> rCallback;
|
2023-01-30 13:58:13 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/// if set, only specified section of internal image will be rendered
|
2023-04-16 19:42:56 +02:00
|
|
|
std::optional<Rect> srcRect;
|
2023-01-30 13:58:13 +02:00
|
|
|
|
2024-06-24 03:23:26 +02:00
|
|
|
/// If set to true, image will be redrawn on each frame
|
2023-01-30 13:58:13 +02:00
|
|
|
bool needRefresh;
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
std::shared_ptr<IImage> getSurface()
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
|
|
|
return bg;
|
|
|
|
}
|
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
/// wrap existing image
|
|
|
|
CPicture(std::shared_ptr<IImage> image, const Point & position);
|
2023-01-30 13:58:13 +02:00
|
|
|
|
2023-01-30 17:18:59 +02:00
|
|
|
/// wrap section of an existing Image
|
|
|
|
CPicture(std::shared_ptr<IImage> image, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
|
2024-06-08 09:35:13 +02:00
|
|
|
CPicture(const ImagePath & bmpname, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
|
2023-01-30 13:58:13 +02:00
|
|
|
|
|
|
|
/// Loads image from specified file name
|
2023-08-23 14:07:50 +02:00
|
|
|
CPicture(const ImagePath & bmpname);
|
|
|
|
CPicture(const ImagePath & bmpname, const Point & position);
|
|
|
|
CPicture(const ImagePath & bmpname, int x, int y);
|
2023-01-30 13:58:13 +02:00
|
|
|
|
|
|
|
/// set alpha value for whole surface. Note: may be messed up if surface is shared
|
|
|
|
/// 0=transparent, 255=opaque
|
2023-10-28 19:34:18 +02:00
|
|
|
void setAlpha(uint8_t value);
|
2014-07-15 10:14:49 +03:00
|
|
|
void scaleTo(Point size);
|
2024-06-05 17:50:50 +02:00
|
|
|
void setPlayerColor(PlayerColor player);
|
2023-01-30 13:58:13 +02:00
|
|
|
|
2024-08-15 23:09:04 +02:00
|
|
|
void addRClickCallback(const std::function<void()> & callback);
|
2024-08-15 00:21:02 +02:00
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void show(Canvas & to) override;
|
|
|
|
void showAll(Canvas & to) override;
|
2024-08-15 00:21:02 +02:00
|
|
|
void showPopupWindow(const Point & cursorPosition) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// area filled with specific texture
|
2022-12-17 06:19:16 +02:00
|
|
|
class CFilledTexture : public CIntObject
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2023-05-06 17:28:39 +02:00
|
|
|
protected:
|
2023-01-30 18:25:47 +02:00
|
|
|
std::shared_ptr<IImage> texture;
|
2023-04-26 14:44:10 +02:00
|
|
|
Rect imageArea;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
public:
|
2024-06-02 17:43:56 +02:00
|
|
|
CFilledTexture(const ImagePath & imageName, Rect position, Rect imageArea);
|
2023-08-23 14:07:50 +02:00
|
|
|
CFilledTexture(const ImagePath & imageName, Rect position);
|
2023-01-30 18:25:47 +02:00
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void showAll(Canvas & to) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
2024-06-05 18:30:53 +02:00
|
|
|
/// area filled with specific texture, colorized to player color if image is indexed
|
|
|
|
class FilledTexturePlayerIndexed : public CFilledTexture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using CFilledTexture::CFilledTexture;
|
|
|
|
|
|
|
|
void setPlayerColor(PlayerColor player);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// area filled with specific texture, with applied color filter to colorize it to specific player
|
2023-05-06 17:28:39 +02:00
|
|
|
class FilledTexturePlayerColored : public CFilledTexture
|
|
|
|
{
|
|
|
|
public:
|
2024-06-05 18:30:53 +02:00
|
|
|
using CFilledTexture::CFilledTexture;
|
2023-05-06 17:28:39 +02:00
|
|
|
|
2024-06-05 17:50:50 +02:00
|
|
|
void setPlayerColor(PlayerColor player);
|
2023-05-06 17:28:39 +02:00
|
|
|
};
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
/// Class for displaying one image from animation
|
|
|
|
class CAnimImage: public CIntObject
|
|
|
|
{
|
|
|
|
private:
|
2016-10-16 11:58:18 +02:00
|
|
|
std::shared_ptr<CAnimation> anim;
|
2014-07-15 10:14:49 +03:00
|
|
|
//displayed frame/group
|
|
|
|
size_t frame;
|
|
|
|
size_t group;
|
|
|
|
ui8 flags;
|
2023-10-19 20:22:26 +02:00
|
|
|
Point scaledSize;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// If set, then image is colored using player-specific palette
|
|
|
|
std::optional<PlayerColor> player;
|
|
|
|
|
2020-03-31 19:52:22 +02:00
|
|
|
bool isScaled() const;
|
|
|
|
void setSizeFromImage(const IImage &img);
|
2014-07-15 10:14:49 +03:00
|
|
|
void init();
|
|
|
|
public:
|
2016-10-16 06:51:07 +02:00
|
|
|
bool visible;
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CAnimImage(const AnimationPath & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
|
2024-06-02 17:43:56 +02:00
|
|
|
CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
|
2017-07-17 23:04:00 +02:00
|
|
|
~CAnimImage();
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// size of animation
|
2014-07-15 10:14:49 +03:00
|
|
|
size_t size();
|
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// change displayed frame on this one
|
2014-07-15 10:14:49 +03:00
|
|
|
void setFrame(size_t Frame, size_t Group=0);
|
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// makes image player-colored to specific player
|
2024-06-05 17:50:50 +02:00
|
|
|
void setPlayerColor(PlayerColor player);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-05-03 18:05:07 +02:00
|
|
|
/// returns true if image has player-colored effect applied
|
|
|
|
bool isPlayerColored() const;
|
|
|
|
|
2023-06-02 15:42:18 +02:00
|
|
|
void showAll(Canvas & to) override;
|
2023-10-19 20:22:26 +02:00
|
|
|
|
|
|
|
void setAnimationPath(const AnimationPath & name, size_t frame);
|
|
|
|
|
|
|
|
void setScale(Point scale);
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Base class for displaying animation, used as superclass for different animations
|
|
|
|
class CShowableAnim: public CIntObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum EFlags
|
|
|
|
{
|
|
|
|
BASE=1, //base frame will be blitted before current one
|
|
|
|
HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
|
|
|
|
VERTICAL_FLIP=4, //TODO: will be displayed rotated
|
2024-07-25 20:22:27 +02:00
|
|
|
CREATURE_MODE=8, // use alpha channel for images with palette. Required for creatures in battle and map objects
|
2014-07-15 10:14:49 +03:00
|
|
|
PLAY_ONCE=32 //play animation only once and stop at last frame
|
|
|
|
};
|
|
|
|
protected:
|
2018-07-25 00:36:48 +02:00
|
|
|
std::shared_ptr<CAnimation> anim;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
size_t group, frame;//current frame
|
|
|
|
|
|
|
|
size_t first, last; //animation range
|
|
|
|
|
2023-01-31 15:00:46 +02:00
|
|
|
/// total time on scren for each frame in animation
|
2023-01-27 12:22:48 +02:00
|
|
|
ui32 frameTimeTotal;
|
2023-01-31 15:00:46 +02:00
|
|
|
|
|
|
|
/// how long was current frame visible on screen
|
2023-01-27 12:22:48 +02:00
|
|
|
ui32 frameTimePassed;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
ui8 flags;//Flags from EFlags enum
|
|
|
|
|
|
|
|
//blit image with optional rotation, fitting into rect, etc
|
2023-06-02 15:42:18 +02:00
|
|
|
void blitImage(size_t frame, size_t group, Canvas & to);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//For clipping in rect, offsets of picture coordinates
|
|
|
|
int xOffset, yOffset;
|
|
|
|
|
|
|
|
ui8 alpha;
|
|
|
|
|
|
|
|
public:
|
|
|
|
//called when next animation sequence is required
|
|
|
|
std::function<void()> callback;
|
|
|
|
|
|
|
|
//Set per-surface alpha, 0 = transparent, 255 = opaque
|
|
|
|
void setAlpha(ui32 alphaValue);
|
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CShowableAnim(int x, int y, const AnimationPath & name, ui8 flags, ui32 frameTime, size_t Group=0, uint8_t alpha = UINT8_MAX);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//set animation to group or part of group
|
|
|
|
bool set(size_t Group);
|
|
|
|
bool set(size_t Group, size_t from, size_t to=-1);
|
|
|
|
|
|
|
|
//set rotation flags
|
|
|
|
void rotate(bool on, bool vertical=false);
|
|
|
|
|
|
|
|
//move displayed part of picture (if picture is clipped to rect)
|
|
|
|
void clipRect(int posX, int posY, int width, int height);
|
|
|
|
|
|
|
|
//set frame to first, call callback
|
|
|
|
virtual void reset();
|
|
|
|
|
2023-03-13 00:18:55 +02:00
|
|
|
//set animation duration
|
|
|
|
void setDuration(int durationMs);
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
//show current frame and increase counter
|
2023-06-02 15:42:18 +02:00
|
|
|
void show(Canvas & to) override;
|
|
|
|
void showAll(Canvas & to) override;
|
2023-05-13 16:24:18 +02:00
|
|
|
void tick(uint32_t msPassed) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Creature-dependend animations like attacking, moving,...
|
|
|
|
class CCreatureAnim: public CShowableAnim
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
//queue of animations waiting to be displayed
|
2022-12-22 01:04:58 +02:00
|
|
|
std::queue<ECreatureAnimType> queue;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//this function is used as callback if preview flag was set during construction
|
|
|
|
void loopPreview(bool warMachine);
|
|
|
|
|
|
|
|
public:
|
|
|
|
//change anim to next if queue is not empty, call callback othervice
|
2015-10-12 15:47:10 +02:00
|
|
|
void reset() override;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
//add sequence to the end of queue
|
2022-12-22 01:04:58 +02:00
|
|
|
void addLast(ECreatureAnimType newType);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
void startPreview(bool warMachine);
|
|
|
|
|
|
|
|
//clear queue and set animation to this sequence
|
2022-12-22 01:04:58 +02:00
|
|
|
void clearAndSet(ECreatureAnimType type);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
|
|
|
};
|