CWindowObject.cpp 6.1 KB

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