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