CWindowObject.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "../gui/SDL_Pixels.h"
  14. #include "../gui/SDL_Extensions.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/CCursorHandler.h"
  17. #include "../battle/CBattleInterface.h"
  18. #include "../battle/CBattleInterfaceClasses.h"
  19. #include "../CBitmapHandler.h"
  20. #include "../Graphics.h"
  21. #include "../CGameInfo.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../CMessage.h"
  24. #include "../CMusicHandler.h"
  25. #include "../windows/CAdvmapInterface.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  29. CWindowObject::CWindowObject(int options_, std::string imageName, Point centerAt):
  30. CIntObject(getUsedEvents(options_), Point()),
  31. shadow(nullptr),
  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. CIntObject(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(Point(screen->w/2, screen->h/2));
  59. if(!(options & SHADOW_DISABLED))
  60. setShadow(true);
  61. }
  62. CWindowObject::~CWindowObject() = default;
  63. std::shared_ptr<CPicture> CWindowObject::createBg(std::string imageName, bool playerColored)
  64. {
  65. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  66. if(imageName.empty())
  67. return nullptr;
  68. auto image = std::make_shared<CPicture>(imageName);
  69. if(playerColored)
  70. image->colorize(LOCPLINT->playerID);
  71. return image;
  72. }
  73. void CWindowObject::setBackground(std::string filename)
  74. {
  75. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  76. background = createBg(filename, options & PLAYER_COLORED);
  77. if(background)
  78. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  79. updateShadow();
  80. }
  81. int CWindowObject::getUsedEvents(int options)
  82. {
  83. if (options & RCLICK_POPUP)
  84. return RCLICK;
  85. return 0;
  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. static const int size = 8;
  97. if(on == bool(shadow))
  98. return;
  99. shadow.reset();
  100. //object too small to cast shadow
  101. if(pos.h <= size || pos.w <= size)
  102. return;
  103. if(on)
  104. {
  105. //helper to set last row
  106. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  107. {
  108. Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
  109. for (size_t i=0; i< surf->w; i++)
  110. {
  111. Channels::px<4>::a.set(ptr, 128);
  112. ptr+=4;
  113. }
  114. };
  115. // helper to set last column
  116. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  117. {
  118. Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
  119. for (size_t i=0; i< surf->h; i++)
  120. {
  121. Channels::px<4>::a.set(ptr, 128);
  122. ptr+= surf->pitch;
  123. }
  124. };
  125. static SDL_Surface * shadowCornerTempl = nullptr;
  126. static SDL_Surface * shadowBottomTempl = nullptr;
  127. static SDL_Surface * shadowRightTempl = nullptr;
  128. //one-time initialization
  129. if(!shadowCornerTempl)
  130. {
  131. //create "template" surfaces
  132. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  133. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  134. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  135. Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
  136. //fill with shadow body color
  137. SDL_FillRect(shadowCornerTempl, nullptr, shadowColor);
  138. SDL_FillRect(shadowBottomTempl, nullptr, shadowColor);
  139. SDL_FillRect(shadowRightTempl, nullptr, shadowColor);
  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. shadow = std::make_shared<CIntObject>();
  172. }
  173. {
  174. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255);
  175. shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
  176. shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
  177. shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
  178. }
  179. }
  180. }
  181. void CWindowObject::showAll(SDL_Surface *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.h != to->h || pos.w != to->w))
  188. CMessage::drawBorder(color, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  189. }
  190. void CWindowObject::close()
  191. {
  192. GH.popIntTotally(this);
  193. }
  194. void CWindowObject::clickRight(tribool down, bool previousState)
  195. {
  196. close();
  197. CCS->curh->show();
  198. }