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