Images.h 6.1 KB

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