CWindowObject.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/Canvas.h"
  23. #include "../CGameInfo.h"
  24. #include "../CPlayerInterface.h"
  25. #include "../CMusicHandler.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_, std::string imageName, Point centerAt):
  31. WindowBase(getUsedEvents(options_), Point()),
  32. options(options_),
  33. background(createBg(imageName, options & PLAYER_COLORED))
  34. {
  35. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  36. defActions = 255-DISPOSE;
  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_, std::string imageName):
  47. WindowBase(getUsedEvents(options_), Point()),
  48. options(options_),
  49. background(createBg(imageName, options_ & PLAYER_COLORED))
  50. {
  51. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  52. defActions = 255-DISPOSE;
  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(std::string imageName, bool playerColored)
  68. {
  69. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  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->colorize(LOCPLINT->playerID);
  76. return image;
  77. }
  78. void CWindowObject::setBackground(std::string filename)
  79. {
  80. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  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. int CWindowObject::getUsedEvents(int options)
  87. {
  88. if (options & RCLICK_POPUP)
  89. return SHOW_POPUP;
  90. return 0;
  91. }
  92. void CWindowObject::updateShadow()
  93. {
  94. setShadow(false);
  95. if (!(options & SHADOW_DISABLED))
  96. setShadow(true);
  97. }
  98. void CWindowObject::setShadow(bool on)
  99. {
  100. //size of shadow
  101. static const int size = 8;
  102. if(on == !shadowParts.empty())
  103. return;
  104. shadowParts.clear();
  105. //object too small to cast shadow
  106. if(pos.h <= size || pos.w <= size)
  107. return;
  108. if(on)
  109. {
  110. //helper to set last row
  111. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  112. {
  113. uint8_t * ptr = (uint8_t*)surf->pixels + surf->pitch * (row);
  114. for (size_t i=0; i< surf->w; i++)
  115. {
  116. Channels::px<4>::a.set(ptr, 128);
  117. ptr+=4;
  118. }
  119. };
  120. // helper to set last column
  121. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  122. {
  123. uint8_t * ptr = (uint8_t*)surf->pixels + 4 * (col);
  124. for (size_t i=0; i< surf->h; i++)
  125. {
  126. Channels::px<4>::a.set(ptr, 128);
  127. ptr+= surf->pitch;
  128. }
  129. };
  130. static SDL_Surface * shadowCornerTempl = nullptr;
  131. static SDL_Surface * shadowBottomTempl = nullptr;
  132. static SDL_Surface * shadowRightTempl = nullptr;
  133. //one-time initialization
  134. if(!shadowCornerTempl)
  135. {
  136. //create "template" surfaces
  137. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  138. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  139. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  140. //fill with shadow body color
  141. CSDL_Ext::fillSurface(shadowCornerTempl, { 0, 0, 0, 192 } );
  142. CSDL_Ext::fillSurface(shadowBottomTempl, { 0, 0, 0, 192 } );
  143. CSDL_Ext::fillSurface(shadowRightTempl, { 0, 0, 0, 192 } );
  144. //fill last row and column with more transparent color
  145. blitAlphaCol(shadowRightTempl , size-1);
  146. blitAlphaCol(shadowCornerTempl, size-1);
  147. blitAlphaRow(shadowBottomTempl, size-1);
  148. blitAlphaRow(shadowCornerTempl, size-1);
  149. }
  150. //FIXME: do something with this points
  151. Point shadowStart;
  152. if (options & BORDERED)
  153. shadowStart = Point(size - 14, size - 14);
  154. else
  155. shadowStart = Point(size, size);
  156. Point shadowPos;
  157. if (options & BORDERED)
  158. shadowPos = Point(pos.w + 14, pos.h + 14);
  159. else
  160. shadowPos = Point(pos.w, pos.h);
  161. Point fullsize;
  162. if (options & BORDERED)
  163. fullsize = Point(pos.w + 28, pos.h + 29);
  164. else
  165. fullsize = Point(pos.w, pos.h);
  166. //create base 8x8 piece of shadow
  167. SDL_Surface * shadowCorner = CSDL_Ext::copySurface(shadowCornerTempl);
  168. SDL_Surface * shadowBottom = CSDL_Ext::scaleSurfaceFast(shadowBottomTempl, fullsize.x - size, size);
  169. SDL_Surface * shadowRight = CSDL_Ext::scaleSurfaceFast(shadowRightTempl, size, fullsize.y - size);
  170. blitAlphaCol(shadowBottom, 0);
  171. blitAlphaRow(shadowRight, 0);
  172. //generate "shadow" object with these 3 pieces in it
  173. {
  174. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  175. shadowParts.push_back(std::make_shared<CPicture>( IImage::createFromSurface(shadowCorner), Point(shadowPos.x, shadowPos.y)));
  176. shadowParts.push_back(std::make_shared<CPicture>( IImage::createFromSurface(shadowRight ), Point(shadowPos.x, shadowStart.y)));
  177. shadowParts.push_back(std::make_shared<CPicture>( IImage::createFromSurface(shadowBottom), Point(shadowStart.x, shadowPos.y)));
  178. }
  179. SDL_FreeSurface(shadowCorner);
  180. SDL_FreeSurface(shadowBottom);
  181. SDL_FreeSurface(shadowRight);
  182. }
  183. }
  184. void CWindowObject::showAll(Canvas & to)
  185. {
  186. auto color = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1);
  187. if(settings["session"]["spectate"].Bool())
  188. color = PlayerColor(1); // TODO: Spectator shouldn't need special code for UI colors
  189. CIntObject::showAll(to);
  190. if ((options & BORDERED) && (pos.dimensions() != GH.screenDimensions()))
  191. CMessage::drawBorder(color, to.getInternalSurface(), pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  192. }
  193. bool CWindowObject::isPopupWindow() const
  194. {
  195. return options & RCLICK_POPUP;
  196. }
  197. CStatusbarWindow::CStatusbarWindow(int options, std::string imageName, Point centerAt) : CWindowObject(options, imageName, centerAt)
  198. {
  199. }
  200. CStatusbarWindow::CStatusbarWindow(int options, std::string imageName) : CWindowObject(options, imageName)
  201. {
  202. }