Images.h 6.3 KB

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