CWindowObject.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * CWindowObject.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 "CWindowObject.h"
  12. #include "../widgets/MiscWidgets.h"
  13. #include "../widgets/Images.h"
  14. #include "../widgets/TextControls.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/CursorHandler.h"
  17. #include "../battle/BattleInterface.h"
  18. #include "../battle/BattleInterfaceClasses.h"
  19. #include "../windows/CMessage.h"
  20. #include "../renderSDL/SDL_PixelAccess.h"
  21. #include "../render/IImage.h"
  22. #include "../render/IRenderHandler.h"
  23. #include "../render/Canvas.h"
  24. #include "../CGameInfo.h"
  25. #include "../CPlayerInterface.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h" //for Unicode related stuff
  29. #include <SDL_surface.h>
  30. CWindowObject::CWindowObject(int options_, const ImagePath & imageName, Point centerAt):
  31. WindowBase(0, Point()),
  32. options(options_),
  33. background(createBg(imageName, options & PLAYER_COLORED))
  34. {
  35. if(!(options & NEEDS_ANIMATED_BACKGROUND)) //currently workaround for highscores (currently uses window as normal control, because otherwise videos are not played in background yet)
  36. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  37. if (options & RCLICK_POPUP)
  38. CCS->curh->hide();
  39. if (background)
  40. pos = background->center(centerAt);
  41. else
  42. center(centerAt);
  43. if (!(options & SHADOW_DISABLED))
  44. setShadow(true);
  45. }
  46. CWindowObject::CWindowObject(int options_, const ImagePath & imageName):
  47. WindowBase(0, Point()),
  48. options(options_),
  49. background(createBg(imageName, options_ & PLAYER_COLORED))
  50. {
  51. if(!(options & NEEDS_ANIMATED_BACKGROUND)) //currently workaround for highscores (currently uses window as normal control, because otherwise videos are not played in background yet)
  52. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  53. if(options & RCLICK_POPUP)
  54. CCS->curh->hide();
  55. if(background)
  56. pos = background->center();
  57. else
  58. center(GH.screenDimensions() / 2);
  59. if(!(options & SHADOW_DISABLED))
  60. setShadow(true);
  61. }
  62. CWindowObject::~CWindowObject()
  63. {
  64. if(options & RCLICK_POPUP)
  65. CCS->curh->show();
  66. }
  67. std::shared_ptr<CPicture> CWindowObject::createBg(const ImagePath & imageName, bool playerColored)
  68. {
  69. OBJECT_CONSTRUCTION;
  70. if(imageName.empty())
  71. return nullptr;
  72. auto image = std::make_shared<CPicture>(imageName);
  73. image->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  74. if(playerColored)
  75. image->setPlayerColor(LOCPLINT->playerID);
  76. return image;
  77. }
  78. void CWindowObject::setBackground(const ImagePath & filename)
  79. {
  80. OBJECT_CONSTRUCTION;
  81. background = createBg(filename, options & PLAYER_COLORED);
  82. if(background)
  83. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  84. updateShadow();
  85. }
  86. void CWindowObject::updateShadow()
  87. {
  88. setShadow(false);
  89. if (!(options & SHADOW_DISABLED))
  90. setShadow(true);
  91. }
  92. void CWindowObject::setShadow(bool on)
  93. {
  94. //size of shadow
  95. static const int size = 8;
  96. if(on == !shadowParts.empty())
  97. return;
  98. shadowParts.clear();
  99. //object too small to cast shadow
  100. if(pos.h <= size || pos.w <= size)
  101. return;
  102. if(on)
  103. {
  104. //helper to set last row
  105. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  106. {
  107. uint8_t * ptr = (uint8_t*)surf->pixels + surf->pitch * (row);
  108. for (size_t i=0; i< surf->w; i++)
  109. {
  110. Channels::px<4>::a.set(ptr, 128);
  111. ptr+=4;
  112. }
  113. };
  114. // helper to set last column
  115. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  116. {
  117. uint8_t * ptr = (uint8_t*)surf->pixels + 4 * (col);
  118. for (size_t i=0; i< surf->h; i++)
  119. {
  120. Channels::px<4>::a.set(ptr, 128);
  121. ptr+= surf->pitch;
  122. }
  123. };
  124. static SDL_Surface * shadowCornerTempl = nullptr;
  125. static SDL_Surface * shadowBottomTempl = nullptr;
  126. static SDL_Surface * shadowRightTempl = nullptr;
  127. //one-time initialization
  128. if(!shadowCornerTempl)
  129. {
  130. //create "template" surfaces
  131. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  132. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  133. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  134. //fill with shadow body color
  135. CSDL_Ext::fillSurface(shadowCornerTempl, { 0, 0, 0, 192 } );
  136. CSDL_Ext::fillSurface(shadowBottomTempl, { 0, 0, 0, 192 } );
  137. CSDL_Ext::fillSurface(shadowRightTempl, { 0, 0, 0, 192 } );
  138. //fill last row and column with more transparent color
  139. blitAlphaCol(shadowRightTempl , size-1);
  140. blitAlphaCol(shadowCornerTempl, size-1);
  141. blitAlphaRow(shadowBottomTempl, size-1);
  142. blitAlphaRow(shadowCornerTempl, size-1);
  143. }
  144. //FIXME: do something with this points
  145. Point shadowStart;
  146. if (options & BORDERED)
  147. shadowStart = Point(size - 14, size - 14);
  148. else
  149. shadowStart = Point(size, size);
  150. Point shadowPos;
  151. if (options & BORDERED)
  152. shadowPos = Point(pos.w + 14, pos.h + 14);
  153. else
  154. shadowPos = Point(pos.w, pos.h);
  155. Point fullsize;
  156. if (options & BORDERED)
  157. fullsize = Point(pos.w + 28, pos.h + 29);
  158. else
  159. fullsize = Point(pos.w, pos.h);
  160. //create base 8x8 piece of shadow
  161. SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
  162. SDL_Surface * shadowBottom = CSDL_Ext::scaleSurface(shadowBottomTempl, fullsize.x - size, size);
  163. SDL_Surface * shadowRight = CSDL_Ext::scaleSurface(shadowRightTempl, size, fullsize.y - size);
  164. blitAlphaCol(shadowBottom, 0);
  165. blitAlphaRow(shadowRight, 0);
  166. //generate "shadow" object with these 3 pieces in it
  167. {
  168. OBJECT_CONSTRUCTION;
  169. shadowParts.push_back(std::make_shared<CPicture>( GH.renderHandler().createImage(shadowCorner), Point(shadowPos.x, shadowPos.y)));
  170. shadowParts.push_back(std::make_shared<CPicture>( GH.renderHandler().createImage(shadowRight ), Point(shadowPos.x, shadowStart.y)));
  171. shadowParts.push_back(std::make_shared<CPicture>( GH.renderHandler().createImage(shadowBottom), Point(shadowStart.x, shadowPos.y)));
  172. }
  173. SDL_FreeSurface(shadowCorner);
  174. SDL_FreeSurface(shadowBottom);
  175. SDL_FreeSurface(shadowRight);
  176. }
  177. }
  178. void CWindowObject::showAll(Canvas & to)
  179. {
  180. auto color = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1);
  181. if(settings["session"]["spectate"].Bool())
  182. color = PlayerColor(1); // TODO: Spectator shouldn't need special code for UI colors
  183. CIntObject::showAll(to);
  184. if ((options & BORDERED) && (pos.dimensions() != GH.screenDimensions()))
  185. CMessage::drawBorder(color, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  186. }
  187. bool CWindowObject::isPopupWindow() const
  188. {
  189. return options & RCLICK_POPUP;
  190. }