CWindowObject.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/IScreenHandler.h"
  23. #include "../render/IRenderHandler.h"
  24. #include "../render/Canvas.h"
  25. #include "../CGameInfo.h"
  26. #include "../CPlayerInterface.h"
  27. #include "../../CCallback.h"
  28. #include "../../lib/CConfigHandler.h"
  29. #include "../../lib/texts/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. 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. if(options & RCLICK_POPUP)
  55. CCS->curh->hide();
  56. if(background)
  57. pos = background->center();
  58. else
  59. center(GH.screenDimensions() / 2);
  60. if(!(options & SHADOW_DISABLED))
  61. setShadow(true);
  62. }
  63. CWindowObject::~CWindowObject()
  64. {
  65. if(options & RCLICK_POPUP)
  66. CCS->curh->show();
  67. }
  68. std::shared_ptr<CPicture> CWindowObject::createBg(const ImagePath & imageName, bool playerColored)
  69. {
  70. OBJECT_CONSTRUCTION;
  71. if(imageName.empty())
  72. return nullptr;
  73. auto image = std::make_shared<CPicture>(imageName);
  74. image->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  75. if(playerColored)
  76. image->setPlayerColor(LOCPLINT->playerID);
  77. return image;
  78. }
  79. void CWindowObject::setBackground(const ImagePath & filename)
  80. {
  81. OBJECT_CONSTRUCTION;
  82. background = createBg(filename, options & PLAYER_COLORED);
  83. if(background)
  84. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  85. updateShadow();
  86. }
  87. void CWindowObject::updateShadow()
  88. {
  89. setShadow(false);
  90. if (!(options & SHADOW_DISABLED))
  91. setShadow(true);
  92. }
  93. void CWindowObject::setShadow(bool on)
  94. {
  95. //size of shadow
  96. int sizeOriginal = 8;
  97. int size = sizeOriginal * GH.screenHandler().getScalingFactor();
  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(sizeOriginal - 14, sizeOriginal - 14);
  150. else
  151. shadowStart = Point(sizeOriginal, sizeOriginal);
  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::scaleSurface(shadowBottomTempl, (fullsize.x - sizeOriginal) * GH.screenHandler().getScalingFactor(), size);
  165. SDL_Surface * shadowRight = CSDL_Ext::scaleSurface(shadowRightTempl, size, (fullsize.y - sizeOriginal) * GH.screenHandler().getScalingFactor());
  166. blitAlphaCol(shadowBottom, 0);
  167. blitAlphaRow(shadowRight, 0);
  168. //generate "shadow" object with these 3 pieces in it
  169. {
  170. OBJECT_CONSTRUCTION;
  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. }