CWindowObject.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. 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. CIntObject(getUsedEvents(options_), Point()),
  47. shadow(nullptr),
  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. 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()
  62. {
  63. setShadow(false);
  64. }
  65. CPicture * CWindowObject::createBg(std::string imageName, bool playerColored)
  66. {
  67. OBJ_CONSTRUCTION_CAPTURING_ALL;
  68. if (imageName.empty())
  69. return nullptr;
  70. auto image = new CPicture(imageName);
  71. if (playerColored)
  72. image->colorize(LOCPLINT->playerID);
  73. return image;
  74. }
  75. void CWindowObject::setBackground(std::string filename)
  76. {
  77. OBJ_CONSTRUCTION_CAPTURING_ALL;
  78. delete background;
  79. background = createBg(filename, options & PLAYER_COLORED);
  80. if (background)
  81. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  82. updateShadow();
  83. }
  84. int CWindowObject::getUsedEvents(int options)
  85. {
  86. if (options & RCLICK_POPUP)
  87. return RCLICK;
  88. return 0;
  89. }
  90. void CWindowObject::updateShadow()
  91. {
  92. setShadow(false);
  93. if (!(options & SHADOW_DISABLED))
  94. setShadow(true);
  95. }
  96. void CWindowObject::setShadow(bool on)
  97. {
  98. //size of shadow
  99. static const int size = 8;
  100. if (on == bool(shadow))
  101. return;
  102. vstd::clear_pointer(shadow);
  103. //object too small to cast shadow
  104. if (pos.h <= size || pos.w <= size)
  105. return;
  106. if (on)
  107. {
  108. //helper to set last row
  109. auto blitAlphaRow = [](SDL_Surface *surf, size_t row)
  110. {
  111. Uint8 * ptr = (Uint8*)surf->pixels + surf->pitch * (row);
  112. for (size_t i=0; i< surf->w; i++)
  113. {
  114. Channels::px<4>::a.set(ptr, 128);
  115. ptr+=4;
  116. }
  117. };
  118. // helper to set last column
  119. auto blitAlphaCol = [](SDL_Surface *surf, size_t col)
  120. {
  121. Uint8 * ptr = (Uint8*)surf->pixels + 4 * (col);
  122. for (size_t i=0; i< surf->h; i++)
  123. {
  124. Channels::px<4>::a.set(ptr, 128);
  125. ptr+= surf->pitch;
  126. }
  127. };
  128. static SDL_Surface * shadowCornerTempl = nullptr;
  129. static SDL_Surface * shadowBottomTempl = nullptr;
  130. static SDL_Surface * shadowRightTempl = nullptr;
  131. //one-time initialization
  132. if (!shadowCornerTempl)
  133. {
  134. //create "template" surfaces
  135. shadowCornerTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, size);
  136. shadowBottomTempl = CSDL_Ext::createSurfaceWithBpp<4>(1, size);
  137. shadowRightTempl = CSDL_Ext::createSurfaceWithBpp<4>(size, 1);
  138. Uint32 shadowColor = SDL_MapRGBA(shadowCornerTempl->format, 0, 0, 0, 192);
  139. //fill with shadow body color
  140. SDL_FillRect(shadowCornerTempl, nullptr, shadowColor);
  141. SDL_FillRect(shadowBottomTempl, nullptr, shadowColor);
  142. SDL_FillRect(shadowRightTempl, nullptr, shadowColor);
  143. //fill last row and column with more transparent color
  144. blitAlphaCol(shadowRightTempl , size-1);
  145. blitAlphaCol(shadowCornerTempl, size-1);
  146. blitAlphaRow(shadowBottomTempl, size-1);
  147. blitAlphaRow(shadowCornerTempl, size-1);
  148. }
  149. OBJ_CONSTRUCTION_CAPTURING_ALL;
  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. shadow = new CIntObject();
  174. shadow->addChild(new CPicture(shadowCorner, shadowPos.x, shadowPos.y));
  175. shadow->addChild(new CPicture(shadowRight, shadowPos.x, shadowStart.y));
  176. shadow->addChild(new CPicture(shadowBottom, shadowStart.x, shadowPos.y));
  177. }
  178. }
  179. void CWindowObject::showAll(SDL_Surface *to)
  180. {
  181. auto color = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1);
  182. if(settings["session"]["spectate"].Bool())
  183. color = PlayerColor(1); // TODO: Spectator shouldn't need special code for UI colors
  184. CIntObject::showAll(to);
  185. if ((options & BORDERED) && (pos.h != to->h || pos.w != to->w))
  186. CMessage::drawBorder(color, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  187. }
  188. void CWindowObject::close()
  189. {
  190. GH.popIntTotally(this);
  191. }
  192. void CWindowObject::clickRight(tribool down, bool previousState)
  193. {
  194. close();
  195. CCS->curh->show();
  196. }