2
0

CWindowObject.cpp 5.6 KB

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