Images.h 5.9 KB

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