CWindowObject.cpp 6.6 KB

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