CWindowObject.cpp 6.7 KB

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