CWindowObject.cpp 5.6 KB

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