Images.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  95. CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group=0, ui8 Flags=0);
  96. ~CAnimImage();
  97. /// size of animation
  98. size_t size();
  99. /// change displayed frame on this one
  100. void setFrame(size_t Frame, size_t Group=0);
  101. /// makes image player-colored to specific player
  102. void setPlayerColor(PlayerColor player);
  103. /// returns true if image has player-colored effect applied
  104. bool isPlayerColored() const;
  105. void showAll(Canvas & to) override;
  106. void setAnimationPath(const AnimationPath & name, size_t frame);
  107. void setScale(Point scale);
  108. };
  109. /// Base class for displaying animation, used as superclass for different animations
  110. class CShowableAnim: public CIntObject
  111. {
  112. public:
  113. enum EFlags
  114. {
  115. BASE=1, //base frame will be blitted before current one
  116. HORIZONTAL_FLIP=2, //TODO: will be displayed rotated
  117. VERTICAL_FLIP=4, //TODO: will be displayed rotated
  118. PALETTE_ALPHA=8, // use alpha channel for images with palette. Required for creatures in battle and map objects
  119. PLAY_ONCE=32 //play animation only once and stop at last frame
  120. };
  121. protected:
  122. std::shared_ptr<CAnimation> anim;
  123. size_t group, frame;//current frame
  124. size_t first, last; //animation range
  125. /// total time on scren for each frame in animation
  126. ui32 frameTimeTotal;
  127. /// how long was current frame visible on screen
  128. ui32 frameTimePassed;
  129. ui8 flags;//Flags from EFlags enum
  130. //blit image with optional rotation, fitting into rect, etc
  131. void blitImage(size_t frame, size_t group, Canvas & to);
  132. //For clipping in rect, offsets of picture coordinates
  133. int xOffset, yOffset;
  134. ui8 alpha;
  135. public:
  136. //called when next animation sequence is required
  137. std::function<void()> callback;
  138. //Set per-surface alpha, 0 = transparent, 255 = opaque
  139. void setAlpha(ui32 alphaValue);
  140. CShowableAnim(int x, int y, const AnimationPath & name, ui8 flags, ui32 frameTime, size_t Group=0, uint8_t alpha = UINT8_MAX);
  141. //set animation to group or part of group
  142. bool set(size_t Group);
  143. bool set(size_t Group, size_t from, size_t to=-1);
  144. //set rotation flags
  145. void rotate(bool on, bool vertical=false);
  146. //move displayed part of picture (if picture is clipped to rect)
  147. void clipRect(int posX, int posY, int width, int height);
  148. //set frame to first, call callback
  149. virtual void reset();
  150. //set animation duration
  151. void setDuration(int durationMs);
  152. //show current frame and increase counter
  153. void show(Canvas & to) override;
  154. void showAll(Canvas & to) override;
  155. void tick(uint32_t msPassed) override;
  156. };
  157. /// Creature-dependend animations like attacking, moving,...
  158. class CCreatureAnim: public CShowableAnim
  159. {
  160. private:
  161. //queue of animations waiting to be displayed
  162. std::queue<ECreatureAnimType> queue;
  163. //this function is used as callback if preview flag was set during construction
  164. void loopPreview(bool warMachine);
  165. public:
  166. //change anim to next if queue is not empty, call callback othervice
  167. void reset() override;
  168. //add sequence to the end of queue
  169. void addLast(ECreatureAnimType newType);
  170. void startPreview(bool warMachine);
  171. //clear queue and set animation to this sequence
  172. void clearAndSet(ECreatureAnimType type);
  173. CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
  174. };