Images.h 6.5 KB

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