CWindowObject.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/IScreenHandler.h"
  23. #include "../render/IRenderHandler.h"
  24. #include "../render/Canvas.h"
  25. #include "../render/CanvasImage.h"
  26. #include "../CGameInfo.h"
  27. #include "../CPlayerInterface.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CConfigHandler.h"
  30. #include "../../lib/texts/CGeneralTextHandler.h" //for Unicode related stuff
  31. #include <SDL_surface.h>
  32. CWindowObject::CWindowObject(int options_, const ImagePath & imageName, Point centerAt):
  33. WindowBase(0, Point()),
  34. options(options_),
  35. background(createBg(imageName, options & PLAYER_COLORED))
  36. {
  37. if(!(options & NEEDS_ANIMATED_BACKGROUND)) //currently workaround for highscores (currently uses window as normal control, because otherwise videos are not played in background yet)
  38. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  39. if (options & RCLICK_POPUP)
  40. CCS->curh->hide();
  41. if (background)
  42. pos = background->center(centerAt);
  43. else
  44. center(centerAt);
  45. if (!(options & SHADOW_DISABLED))
  46. setShadow(true);
  47. }
  48. CWindowObject::CWindowObject(int options_, const ImagePath & imageName):
  49. WindowBase(0, Point()),
  50. options(options_),
  51. background(createBg(imageName, options_ & PLAYER_COLORED))
  52. {
  53. if(!(options & NEEDS_ANIMATED_BACKGROUND)) //currently workaround for highscores (currently uses window as normal control, because otherwise videos are not played in background yet)
  54. assert(parent == nullptr); //Safe to remove, but windows should not have parent
  55. if(options & RCLICK_POPUP)
  56. CCS->curh->hide();
  57. if(background)
  58. pos = background->center();
  59. else
  60. center(GH.screenDimensions() / 2);
  61. if(!(options & SHADOW_DISABLED))
  62. setShadow(true);
  63. }
  64. CWindowObject::~CWindowObject()
  65. {
  66. if(options & RCLICK_POPUP)
  67. CCS->curh->show();
  68. }
  69. std::shared_ptr<CPicture> CWindowObject::createBg(const ImagePath & imageName, bool playerColored)
  70. {
  71. OBJECT_CONSTRUCTION;
  72. if(imageName.empty())
  73. return nullptr;
  74. auto image = std::make_shared<CPicture>(imageName, Point(0,0), EImageBlitMode::OPAQUE);
  75. if(playerColored)
  76. image->setPlayerColor(LOCPLINT->playerID);
  77. return image;
  78. }
  79. void CWindowObject::setBackground(const ImagePath & filename)
  80. {
  81. OBJECT_CONSTRUCTION;
  82. background = createBg(filename, options & PLAYER_COLORED);
  83. if(background)
  84. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  85. updateShadow();
  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. int size = 8;
  97. if(on == !shadowParts.empty())
  98. return;
  99. shadowParts.clear();
  100. //object too small to cast shadow
  101. if(pos.h <= size || pos.w <= size)
  102. return;
  103. if(on)
  104. {
  105. //FIXME: do something with this points
  106. Point shadowStart;
  107. if (options & BORDERED)
  108. shadowStart = Point(size - 14, size - 14);
  109. else
  110. shadowStart = Point(size, size);
  111. Point shadowPos;
  112. if (options & BORDERED)
  113. shadowPos = Point(pos.w + 14, pos.h + 14);
  114. else
  115. shadowPos = Point(pos.w, pos.h);
  116. Point fullsize;
  117. if (options & BORDERED)
  118. fullsize = Point(pos.w + 28, pos.h + 29);
  119. else
  120. fullsize = Point(pos.w, pos.h);
  121. Point sizeCorner(size, size);
  122. Point sizeRight(fullsize.x - size, size);
  123. Point sizeBottom(size, fullsize.y - size);
  124. //create base 8x8 piece of shadow
  125. auto imageCorner = GH.renderHandler().createImage(sizeCorner, CanvasScalingPolicy::AUTO);
  126. auto imageRight = GH.renderHandler().createImage(sizeRight, CanvasScalingPolicy::AUTO);
  127. auto imageBottom = GH.renderHandler().createImage(sizeBottom, CanvasScalingPolicy::AUTO);
  128. Canvas canvasCorner = imageCorner->getCanvas();
  129. Canvas canvasRight = imageRight->getCanvas();
  130. Canvas canvasBottom = imageBottom->getCanvas();
  131. canvasCorner.drawColor(Rect(Point(0,0), sizeCorner), { 0, 0, 0, 128 });
  132. canvasRight.drawColor(Rect(Point(0,0), sizeRight), { 0, 0, 0, 128 });
  133. canvasBottom.drawColor(Rect(Point(0,0), sizeBottom), { 0, 0, 0, 128 });
  134. canvasCorner.drawColor(Rect(Point(0,0), sizeCorner - Point(1,1)), { 0, 0, 0, 192 });
  135. canvasRight.drawColor(Rect(Point(0,0), sizeRight - Point(0,1)), { 0, 0, 0, 192 });
  136. canvasBottom.drawColor(Rect(Point(0,0), sizeBottom - Point(1,0)), { 0, 0, 0, 192 });
  137. //generate "shadow" object with these 3 pieces in it
  138. {
  139. OBJECT_CONSTRUCTION;
  140. shadowParts.push_back(std::make_shared<CPicture>( imageCorner, Point(shadowPos.x, shadowPos.y)));
  141. shadowParts.push_back(std::make_shared<CPicture>( imageRight, Point(shadowStart.x, shadowPos.y)));
  142. shadowParts.push_back(std::make_shared<CPicture>( imageBottom, Point(shadowPos.x, shadowStart.y)));
  143. }
  144. }
  145. }
  146. void CWindowObject::showAll(Canvas & to)
  147. {
  148. auto color = LOCPLINT ? LOCPLINT->playerID : PlayerColor(1);
  149. if(settings["session"]["spectate"].Bool())
  150. color = PlayerColor(1); // TODO: Spectator shouldn't need special code for UI colors
  151. CIntObject::showAll(to);
  152. if ((options & BORDERED) && (pos.dimensions() != GH.screenDimensions()))
  153. CMessage::drawBorder(color, to, pos.w+28, pos.h+29, pos.x-14, pos.y-15);
  154. }
  155. bool CWindowObject::isPopupWindow() const
  156. {
  157. return options & RCLICK_POPUP;
  158. }