Images.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Images.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../gui/CIntObject.h"
  12. #include "../battle/BattleConstants.h"
  13. #include "../../lib/filesystem/ResourcePath.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class Rect;
  16. VCMI_LIB_NAMESPACE_END
  17. class CAnimImage;
  18. class CLabel;
  19. class CAnimation;
  20. class IImage;
  21. enum class EImageBlitMode : uint8_t;
  22. // Image class
  23. class CPicture : public CIntObject
  24. {
  25. std::shared_ptr<IImage> bg;
  26. std::function<void()> lCallback;
  27. std::function<void()> rCallback;
  28. public:
  29. /// if set, only specified section of internal image will be rendered
  30. std::optional<Rect> srcRect;
  31. /// If set to true, image will be redrawn on each frame
  32. bool needRefresh;
  33. std::shared_ptr<IImage> getSurface()
  34. {
  35. return bg;
  36. }
  37. /// wrap existing image
  38. CPicture(std::shared_ptr<IImage> image, const Point & position);
  39. /// wrap section of an existing Image
  40. CPicture(std::shared_ptr<IImage> image, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
  41. CPicture(const ImagePath & bmpname, const Rect &SrcRext, int x = 0, int y = 0); //wrap subrect of given surface
  42. /// Loads image from specified file name
  43. CPicture(const ImagePath & bmpname);
  44. CPicture(const ImagePath & bmpname, const Point & position, EImageBlitMode mode);
  45. CPicture(const ImagePath & bmpname, const Point & position);
  46. CPicture(const ImagePath & bmpname, int x, int y);
  47. /// set alpha value for whole surface. Note: may be messed up if surface is shared
  48. /// 0=transparent, 255=opaque
  49. void setAlpha(uint8_t value);
  50. void scaleTo(Point size);
  51. void setPlayerColor(PlayerColor player);
  52. void addLClickCallback(const std::function<void()> & callback);
  53. void addRClickCallback(const std::function<void()> & callback);
  54. void show(Canvas & to) override;
  55. void showAll(Canvas & to) override;
  56. void clickPressed(const Point & cursorPosition) override;
  57. void showPopupWindow(const Point & cursorPosition) override;
  58. };
  59. /// area filled with specific texture
  60. class CFilledTexture : public CIntObject
  61. {
  62. protected:
  63. std::shared_ptr<IImage> texture;
  64. Rect imageArea;
  65. public:
  66. CFilledTexture(const ImagePath & imageName, Rect position, Rect imageArea);
  67. CFilledTexture(const ImagePath & imageName, Rect position);
  68. void showAll(Canvas & to) override;
  69. };
  70. /// area filled with specific texture, colorized to player color if image is indexed
  71. class FilledTexturePlayerIndexed : public CFilledTexture
  72. {
  73. public:
  74. using CFilledTexture::CFilledTexture;
  75. void setPlayerColor(PlayerColor player);
  76. };
  77. /// area filled with specific texture, with applied color filter to colorize it to specific player
  78. class FilledTexturePlayerColored : public CFilledTexture
  79. {
  80. public:
  81. FilledTexturePlayerColored(Rect position);
  82. void setPlayerColor(PlayerColor player);
  83. };
  84. /// Class for displaying one image from animation
  85. class CAnimImage: public CIntObject
  86. {
  87. private:
  88. std::shared_ptr<CAnimation> anim;
  89. //displayed frame/group
  90. size_t frame;
  91. size_t group;
  92. ui8 flags;
  93. Point scaledSize;
  94. /// If set, then image is colored using player-specific palette
  95. std::optional<PlayerColor> player;
  96. bool isScaled() const;
  97. void setSizeFromImage(const IImage &img);
  98. void init();
  99. public:
  100. bool visible;
  101. CAnimImage(const AnimationPath & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  102. CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
  103. ~CAnimImage();
  104. /// size of animation
  105. size_t size();
  106. /// change displayed frame on this one
  107. void setFrame(size_t Frame, size_t Group=0);
  108. /// makes image player-colored to specific player
  109. void setPlayerColor(PlayerColor player);
  110. /// returns true if image has player-colored effect applied
  111. bool isPlayerColored() const;
  112. void showAll(Canvas & to) override;
  113. void setAnimationPath(const AnimationPath & name, size_t frame);
  114. void setScale(Point scale);
  115. };
  116. /// Base class for displaying animation, used as superclass for different animations
  117. class CShowableAnim: public CIntObject
  118. {
  119. public:
  120. enum EFlags
  121. {
  122. BASE=1, //base frame will be blitted before current one
  123. HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
  124. VERTICAL_FLIP=4, //TODO: will be displayed rotated
  125. CREATURE_MODE=8, // use alpha channel for images with palette. Required for creatures in battle
  126. MAP_OBJECT_MODE=16, // use alpha channel for images with palette. Required for map objects
  127. PLAY_ONCE=32 //play animation only once and stop at last frame
  128. };
  129. protected:
  130. std::shared_ptr<CAnimation> anim;
  131. size_t group, frame;//current frame
  132. size_t first, last; //animation range
  133. /// total time on scren for each frame in animation
  134. ui32 frameTimeTotal;
  135. /// how long was current frame visible on screen
  136. ui32 frameTimePassed;
  137. ui8 flags;//Flags from EFlags enum
  138. //blit image with optional rotation, fitting into rect, etc
  139. void blitImage(size_t frame, size_t group, Canvas & to);
  140. //For clipping in rect, offsets of picture coordinates
  141. int xOffset, yOffset;
  142. ui8 alpha;
  143. public:
  144. //called when next animation sequence is required
  145. std::function<void()> callback;
  146. //Set per-surface alpha, 0 = transparent, 255 = opaque
  147. void setAlpha(ui32 alphaValue);
  148. CShowableAnim(int x, int y, const AnimationPath & name, ui8 flags, ui32 frameTime, size_t Group=0, uint8_t alpha = UINT8_MAX);
  149. //set animation to group or part of group
  150. bool set(size_t Group);
  151. bool set(size_t Group, size_t from, size_t to=-1);
  152. //set rotation flags
  153. void rotate(bool on, bool vertical=false);
  154. //move displayed part of picture (if picture is clipped to rect)
  155. void clipRect(int posX, int posY, int width, int height);
  156. //set frame to first, call callback
  157. virtual void reset();
  158. //set animation duration
  159. void setDuration(int durationMs);
  160. //show current frame and increase counter
  161. void show(Canvas & to) override;
  162. void showAll(Canvas & to) override;
  163. void tick(uint32_t msPassed) override;
  164. };
  165. /// Creature-dependend animations like attacking, moving,...
  166. class CCreatureAnim: public CShowableAnim
  167. {
  168. private:
  169. //queue of animations waiting to be displayed
  170. std::queue<ECreatureAnimType> queue;
  171. //this function is used as callback if preview flag was set during construction
  172. void loopPreview(bool warMachine);
  173. public:
  174. //change anim to next if queue is not empty, call callback othervice
  175. void reset() override;
  176. //add sequence to the end of queue
  177. void addLast(ECreatureAnimType newType);
  178. void startPreview(bool warMachine);
  179. //clear queue and set animation to this sequence
  180. void clearAndSet(ECreatureAnimType type);
  181. CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
  182. };