Images.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. VCMI_LIB_NAMESPACE_BEGIN
  14. class Rect;
  15. VCMI_LIB_NAMESPACE_END
  16. class CAnimImage;
  17. class CLabel;
  18. class CAnimation;
  19. class IImage;
  20. // Image class
  21. class CPicture : public CIntObject
  22. {
  23. std::shared_ptr<IImage> bg;
  24. public:
  25. /// if set, only specified section of internal image will be rendered
  26. std::optional<Rect> srcRect;
  27. /// If set to true, iamge will be redrawn on each frame
  28. bool needRefresh;
  29. /// If set to false, image will not be rendered
  30. /// Deprecated, use CIntObject::disable()/enable() instead
  31. bool visible;
  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. /// Loads image from specified file name
  41. CPicture(const std::string & bmpname);
  42. CPicture(const std::string & bmpname, const Point & position);
  43. CPicture(const std::string & 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(int value);
  47. void scaleTo(Point size);
  48. void colorize(PlayerColor player);
  49. void show(Canvas & to) override;
  50. void showAll(Canvas & to) override;
  51. };
  52. /// area filled with specific texture
  53. class CFilledTexture : public CIntObject
  54. {
  55. protected:
  56. std::shared_ptr<IImage> texture;
  57. Rect imageArea;
  58. public:
  59. CFilledTexture(std::string imageName, Rect position);
  60. CFilledTexture(std::shared_ptr<IImage> image, Rect position, Rect imageArea);
  61. void showAll(Canvas & to) override;
  62. };
  63. class FilledTexturePlayerColored : public CFilledTexture
  64. {
  65. public:
  66. FilledTexturePlayerColored(std::string imageName, Rect position);
  67. void playerColored(PlayerColor player);
  68. };
  69. /// Class for displaying one image from animation
  70. class CAnimImage: public CIntObject
  71. {
  72. private:
  73. std::shared_ptr<CAnimation> anim;
  74. //displayed frame/group
  75. size_t frame;
  76. size_t group;
  77. ui8 flags;
  78. const Point scaledSize;
  79. /// If set, then image is colored using player-specific palette
  80. std::optional<PlayerColor> player;
  81. bool isScaled() const;
  82. void setSizeFromImage(const IImage &img);
  83. void init();
  84. public:
  85. bool visible;
  86. CAnimImage(const std::string & name, size_t Frame, size_t Group=0, int x=0, int y=0, ui8 Flags=0);
  87. CAnimImage(std::shared_ptr<CAnimation> Anim, 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, Rect targetPos, size_t Group=0, ui8 Flags=0);
  89. ~CAnimImage();
  90. /// size of animation
  91. size_t size();
  92. /// change displayed frame on this one
  93. void setFrame(size_t Frame, size_t Group=0);
  94. /// makes image player-colored to specific player
  95. void playerColored(PlayerColor player);
  96. /// returns true if image has player-colored effect applied
  97. bool isPlayerColored() const;
  98. void showAll(Canvas & to) override;
  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, std::string 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, std::string name, ui8 flags = 0, ECreatureAnimType = ECreatureAnimType::HOLDING);
  165. };