2
0

Images.cpp 12 KB

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