Images.h 5.7 KB

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