Images.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Images.cpp, 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. #include "StdInc.h"
  11. #include "Images.h"
  12. #include "MiscWidgets.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../render/IImage.h"
  15. #include "../render/IRenderHandler.h"
  16. #include "../render/CAnimation.h"
  17. #include "../render/Canvas.h"
  18. #include "../render/ColorFilter.h"
  19. #include "../render/Colors.h"
  20. #include "../battle/BattleInterface.h"
  21. #include "../battle/BattleInterfaceClasses.h"
  22. #include "../CGameInfo.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/texts/CGeneralTextHandler.h" //for Unicode related stuff
  27. #include "../../lib/CRandomGenerator.h"
  28. CPicture::CPicture(std::shared_ptr<IImage> image, const Point & position)
  29. : bg(image)
  30. , needRefresh(false)
  31. {
  32. pos += position;
  33. pos.w = bg->width();
  34. pos.h = bg->height();
  35. addUsedEvents(SHOW_POPUP);
  36. }
  37. CPicture::CPicture( const ImagePath &bmpname, int x, int y )
  38. : CPicture(bmpname, Point(x,y))
  39. {}
  40. CPicture::CPicture( const ImagePath & bmpname )
  41. : CPicture(bmpname, Point(0,0))
  42. {}
  43. CPicture::CPicture( const ImagePath & bmpname, const Point & position, EImageBlitMode mode )
  44. : bg(GH.renderHandler().loadImage(bmpname, mode))
  45. , needRefresh(false)
  46. {
  47. pos.x += position.x;
  48. pos.y += position.y;
  49. assert(bg);
  50. if(bg)
  51. {
  52. pos.w = bg->width();
  53. pos.h = bg->height();
  54. }
  55. else
  56. {
  57. pos.w = pos.h = 0;
  58. }
  59. addUsedEvents(SHOW_POPUP);
  60. }
  61. CPicture::CPicture( const ImagePath & bmpname, const Point & position )
  62. :CPicture(bmpname, position, EImageBlitMode::COLORKEY)
  63. {}
  64. CPicture::CPicture(const ImagePath & bmpname, const Rect &SrcRect, int x, int y)
  65. : CPicture(bmpname, Point(x,y))
  66. {
  67. srcRect = SrcRect;
  68. pos.w = srcRect->w;
  69. pos.h = srcRect->h;
  70. addUsedEvents(SHOW_POPUP);
  71. }
  72. CPicture::CPicture(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
  73. : CPicture(image, Point(x,y))
  74. {
  75. srcRect = SrcRect;
  76. pos.w = srcRect->w;
  77. pos.h = srcRect->h;
  78. addUsedEvents(SHOW_POPUP);
  79. }
  80. void CPicture::show(Canvas & to)
  81. {
  82. if (needRefresh)
  83. showAll(to);
  84. }
  85. void CPicture::showAll(Canvas & to)
  86. {
  87. if(bg)
  88. {
  89. if (srcRect.has_value())
  90. to.draw(bg, pos.topLeft(), *srcRect);
  91. else
  92. to.draw(bg, pos.topLeft());
  93. }
  94. }
  95. void CPicture::setAlpha(uint8_t value)
  96. {
  97. bg->setAlpha(value);
  98. }
  99. void CPicture::scaleTo(Point size)
  100. {
  101. bg->scaleTo(size, EScalingAlgorithm::BILINEAR);
  102. pos.w = bg->width();
  103. pos.h = bg->height();
  104. }
  105. void CPicture::setPlayerColor(PlayerColor player)
  106. {
  107. bg->playerColored(player);
  108. }
  109. void CPicture::addRClickCallback(const std::function<void()> & callback)
  110. {
  111. rCallback = callback;
  112. }
  113. void CPicture::showPopupWindow(const Point & cursorPosition)
  114. {
  115. if(rCallback)
  116. rCallback();
  117. }
  118. CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position)
  119. : CIntObject(0, position.topLeft())
  120. , texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
  121. {
  122. pos.w = position.w;
  123. pos.h = position.h;
  124. imageArea = Rect(Point(), texture->dimensions());
  125. }
  126. CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position, Rect imageArea)
  127. : CIntObject(0, position.topLeft())
  128. , texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
  129. , imageArea(imageArea)
  130. {
  131. pos.w = position.w;
  132. pos.h = position.h;
  133. }
  134. void CFilledTexture::showAll(Canvas & to)
  135. {
  136. CanvasClipRectGuard guard(to, pos);
  137. for (int y=pos.top(); y < pos.bottom(); y+= imageArea.h)
  138. {
  139. for (int x=pos.left(); x < pos.right(); x+= imageArea.w)
  140. to.draw(texture, Point(x,y), imageArea);
  141. }
  142. }
  143. void FilledTexturePlayerIndexed::setPlayerColor(PlayerColor player)
  144. {
  145. texture->playerColored(player);
  146. }
  147. FilledTexturePlayerColored::FilledTexturePlayerColored(Rect position)
  148. :CFilledTexture(ImagePath::builtin("DiBoxBck"), position)
  149. {
  150. }
  151. void FilledTexturePlayerColored::setPlayerColor(PlayerColor player)
  152. {
  153. ImagePath imagePath = ImagePath::builtin("DialogBoxBackground_" + player.toString() + ".bmp");
  154. texture = GH.renderHandler().loadImage(imagePath, EImageBlitMode::COLORKEY);
  155. }
  156. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  157. frame(Frame),
  158. group(Group),
  159. flags(Flags)
  160. {
  161. pos.x += x;
  162. pos.y += y;
  163. anim = GH.renderHandler().loadAnimation(name, (Flags & CCreatureAnim::CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_SELECTION: EImageBlitMode::COLORKEY);
  164. init();
  165. }
  166. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
  167. anim(GH.renderHandler().loadAnimation(name, (Flags & CCreatureAnim::CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_SELECTION : EImageBlitMode::COLORKEY)),
  168. frame(Frame),
  169. group(Group),
  170. flags(Flags),
  171. scaledSize(targetPos.w, targetPos.h)
  172. {
  173. pos.x += targetPos.x;
  174. pos.y += targetPos.y;
  175. init();
  176. }
  177. size_t CAnimImage::size()
  178. {
  179. return anim->size(group);
  180. }
  181. bool CAnimImage::isScaled() const
  182. {
  183. return (scaledSize.x != 0);
  184. }
  185. void CAnimImage::setSizeFromImage(const IImage &img)
  186. {
  187. if (isScaled())
  188. {
  189. // At the time of writing this, IImage had no method to scale to different aspect ratio
  190. // Therefore, have to ignore the target height and preserve original aspect ratio
  191. pos.w = scaledSize.x;
  192. pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
  193. }
  194. else
  195. {
  196. pos.w = img.width();
  197. pos.h = img.height();
  198. }
  199. }
  200. void CAnimImage::init()
  201. {
  202. visible = true;
  203. auto img = anim->getImage(frame, group);
  204. if (img)
  205. setSizeFromImage(*img);
  206. }
  207. CAnimImage::~CAnimImage()
  208. {
  209. }
  210. void CAnimImage::showAll(Canvas & to)
  211. {
  212. if(!visible)
  213. return;
  214. std::vector<size_t> frames = {frame};
  215. if((flags & CShowableAnim::BASE) && frame != 0)
  216. {
  217. frames.insert(frames.begin(), 0);
  218. }
  219. for(auto targetFrame : frames)
  220. {
  221. if(auto img = anim->getImage(targetFrame, group))
  222. {
  223. if(isScaled())
  224. img->scaleTo(scaledSize, EScalingAlgorithm::BILINEAR);
  225. to.draw(img, pos.topLeft());
  226. }
  227. }
  228. }
  229. void CAnimImage::setAnimationPath(const AnimationPath & name, size_t frame)
  230. {
  231. this->frame = frame;
  232. anim = GH.renderHandler().loadAnimation(name, EImageBlitMode::COLORKEY);
  233. init();
  234. }
  235. void CAnimImage::setScale(Point scale)
  236. {
  237. scaledSize = scale;
  238. }
  239. void CAnimImage::setFrame(size_t Frame, size_t Group)
  240. {
  241. if (frame == Frame && group==Group)
  242. return;
  243. if (anim->size(Group) > Frame)
  244. {
  245. frame = Frame;
  246. group = Group;
  247. if(auto img = anim->getImage(frame, group))
  248. {
  249. if (player.has_value())
  250. img->playerColored(*player);
  251. setSizeFromImage(*img);
  252. }
  253. }
  254. else
  255. logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
  256. }
  257. void CAnimImage::setPlayerColor(PlayerColor currPlayer)
  258. {
  259. player = currPlayer;
  260. anim->getImage(frame, group)->playerColored(*player);
  261. if (flags & CShowableAnim::BASE)
  262. anim->getImage(0, group)->playerColored(*player);
  263. }
  264. bool CAnimImage::isPlayerColored() const
  265. {
  266. return player.has_value();
  267. }
  268. CShowableAnim::CShowableAnim(int x, int y, const AnimationPath & name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
  269. anim(GH.renderHandler().loadAnimation(name, (Flags & CREATURE_MODE) ? EImageBlitMode::WITH_SHADOW_AND_SELECTION : EImageBlitMode::COLORKEY)),
  270. group(Group),
  271. frame(0),
  272. first(0),
  273. frameTimeTotal(frameTime),
  274. frameTimePassed(0),
  275. flags(Flags),
  276. xOffset(0),
  277. yOffset(0),
  278. alpha(alpha)
  279. {
  280. last = anim->size(group);
  281. auto image = anim->getImage(0, group);
  282. if (!image)
  283. throw std::runtime_error("Failed to load group " + std::to_string(group) + " of animation file " + name.getOriginalName());
  284. pos.w = image->width();
  285. pos.h = image->height();
  286. pos.x+= x;
  287. pos.y+= y;
  288. addUsedEvents(TIME);
  289. }
  290. void CShowableAnim::setAlpha(ui32 alphaValue)
  291. {
  292. alpha = std::min<ui32>(alphaValue, 255);
  293. }
  294. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  295. {
  296. size_t max = anim->size(Group);
  297. if (to < max)
  298. max = to;
  299. if (max < from || max == 0)
  300. return false;
  301. group = Group;
  302. frame = first = from;
  303. last = max;
  304. frameTimePassed = 0;
  305. return true;
  306. }
  307. bool CShowableAnim::set(size_t Group)
  308. {
  309. if (anim->size(Group)== 0)
  310. return false;
  311. if (group != Group)
  312. {
  313. first = 0;
  314. group = Group;
  315. last = anim->size(Group);
  316. }
  317. frame = 0;
  318. frameTimePassed = 0;
  319. return true;
  320. }
  321. void CShowableAnim::reset()
  322. {
  323. frame = first;
  324. if (callback)
  325. callback();
  326. }
  327. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  328. {
  329. xOffset = posX;
  330. yOffset = posY;
  331. pos.w = width;
  332. pos.h = height;
  333. }
  334. void CShowableAnim::show(Canvas & to)
  335. {
  336. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  337. blitImage(first, group, to);
  338. blitImage(frame, group, to);
  339. }
  340. void CShowableAnim::tick(uint32_t msPassed)
  341. {
  342. if ((flags & PLAY_ONCE) && frame + 1 == last)
  343. return;
  344. frameTimePassed += msPassed;
  345. if(frameTimePassed >= frameTimeTotal)
  346. {
  347. frameTimePassed -= frameTimeTotal;
  348. if ( ++frame >= last)
  349. reset();
  350. }
  351. }
  352. void CShowableAnim::showAll(Canvas & to)
  353. {
  354. if ( flags & BASE )// && frame != first)
  355. blitImage(first, group, to);
  356. blitImage(frame, group, to);
  357. }
  358. void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
  359. {
  360. Rect src( xOffset, yOffset, pos.w, pos.h);
  361. auto img = anim->getImage(frame, group);
  362. if(img)
  363. {
  364. img->setAlpha(alpha);
  365. img->setOverlayColor(Colors::TRANSPARENCY);
  366. to.draw(img, pos.topLeft(), src);
  367. }
  368. }
  369. void CShowableAnim::rotate(bool on, bool vertical)
  370. {
  371. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  372. if (on)
  373. flags |= flag;
  374. else
  375. flags &= ~flag;
  376. }
  377. void CShowableAnim::setDuration(int durationMs)
  378. {
  379. frameTimeTotal = durationMs/(last - first);
  380. }
  381. CCreatureAnim::CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags, ECreatureAnimType type):
  382. CShowableAnim(x, y, name, flags | CREATURE_MODE, 100, size_t(type)) // H3 uses 100 ms per frame, irregardless of battle speed settings
  383. {
  384. xOffset = 0;
  385. yOffset = 0;
  386. }
  387. void CCreatureAnim::loopPreview(bool warMachine)
  388. {
  389. std::vector<ECreatureAnimType> available;
  390. static const ECreatureAnimType creaPreviewList[] = {
  391. ECreatureAnimType::HOLDING,
  392. ECreatureAnimType::HITTED,
  393. ECreatureAnimType::DEFENCE,
  394. ECreatureAnimType::ATTACK_FRONT,
  395. ECreatureAnimType::SPECIAL_FRONT
  396. };
  397. static const ECreatureAnimType machPreviewList[] = {
  398. ECreatureAnimType::HOLDING,
  399. ECreatureAnimType::MOVING,
  400. ECreatureAnimType::SHOOT_UP,
  401. ECreatureAnimType::SHOOT_FRONT,
  402. ECreatureAnimType::SHOOT_DOWN
  403. };
  404. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  405. for (auto & elem : previewList)
  406. if (anim->size(size_t(elem)))
  407. available.push_back(elem);
  408. size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
  409. if (rnd >= available.size())
  410. {
  411. ECreatureAnimType type;
  412. if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
  413. type = ECreatureAnimType::HOLDING;
  414. else
  415. type = ECreatureAnimType::MOVING;
  416. //display this anim for ~1 second (time is random, but it looks good)
  417. for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
  418. addLast(type);
  419. }
  420. else
  421. addLast(available[rnd]);
  422. }
  423. void CCreatureAnim::addLast(ECreatureAnimType newType)
  424. {
  425. auto currType = ECreatureAnimType(group);
  426. if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
  427. {
  428. queue.push( ECreatureAnimType::MOVE_START );
  429. }
  430. else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
  431. {
  432. queue.push( ECreatureAnimType::MOVE_END );
  433. }
  434. if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
  435. queue.push(newType);
  436. queue.push(newType);
  437. }
  438. void CCreatureAnim::reset()
  439. {
  440. //if we are in the middle of rotation - set flag
  441. if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
  442. rotate(true);
  443. if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
  444. rotate(false);
  445. while (!queue.empty())
  446. {
  447. ECreatureAnimType at = queue.front();
  448. queue.pop();
  449. if (set(size_t(at)))
  450. return;
  451. }
  452. if (callback)
  453. callback();
  454. while (!queue.empty())
  455. {
  456. ECreatureAnimType at = queue.front();
  457. queue.pop();
  458. if (set(size_t(at)))
  459. return;
  460. }
  461. set(size_t(ECreatureAnimType::HOLDING));
  462. }
  463. void CCreatureAnim::startPreview(bool warMachine)
  464. {
  465. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  466. }
  467. void CCreatureAnim::clearAndSet(ECreatureAnimType type)
  468. {
  469. while (!queue.empty())
  470. queue.pop();
  471. set(size_t(type));
  472. }