WallpaperState.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include "WallpaperState.h"
  2. #include "TextureProvider.h"
  3. #include "WallpaperEngine/Logging/Log.h"
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace WallpaperEngine::Render;
  7. WallpaperState::WallpaperState (const TextureUVsScaling& textureUVsMode, const uint32_t& clampMode) :
  8. m_textureUVsMode (textureUVsMode), m_clampingMode (clampMode) { }
  9. std::optional<WallpaperState::TextureUVsScaling> WallpaperState::parseScalingMode (const std::string& value) {
  10. if (value == "stretch") {
  11. return TextureUVsScaling::StretchUVs;
  12. }
  13. if (value == "fit") {
  14. return TextureUVsScaling::ZoomFitUVs;
  15. }
  16. if (value == "fill") {
  17. return TextureUVsScaling::ZoomFillUVs;
  18. }
  19. if (value == "center") {
  20. return TextureUVsScaling::CenterUVs;
  21. }
  22. if (value == "free") {
  23. return TextureUVsScaling::FreeUVs;
  24. }
  25. if (value == "default") {
  26. return TextureUVsScaling::DefaultUVs;
  27. }
  28. return std::nullopt;
  29. }
  30. bool WallpaperState::hasChanged (
  31. const glm::ivec4& viewport, const bool& vflip, const int& projectionWidth, const int& projectionHeight
  32. ) const {
  33. return this->m_uvsDirty || this->m_viewport.width != viewport.z || this->m_viewport.height != viewport.w
  34. || this->m_projection.width != projectionWidth || this->m_projection.height != projectionHeight
  35. || this->m_vflip != vflip;
  36. }
  37. void WallpaperState::resetUVs () {
  38. this->m_UVs.ustart = 0;
  39. this->m_UVs.uend = 1;
  40. if (m_vflip) {
  41. this->m_UVs.vstart = 0.0f;
  42. this->m_UVs.vend = 1.0f;
  43. } else {
  44. this->m_UVs.vstart = 1.0f;
  45. this->m_UVs.vend = 0.0f;
  46. }
  47. }
  48. glm::vec4 WallpaperState::alignmentMargins (const int mode) const {
  49. const float width = static_cast<float> (this->getProjectionWidth ());
  50. const float height = static_cast<float> (this->getProjectionHeight ());
  51. const float outputWidth = static_cast<float> (this->getViewportWidth ());
  52. const float outputHeight = static_cast<float> (this->getViewportHeight ());
  53. const float sceneAspect = width / height;
  54. const float outputAspect = outputWidth / outputHeight;
  55. const float position = this->m_alignment.position;
  56. const float x = this->m_alignment.x;
  57. const float y = this->m_alignment.y;
  58. float left = 0.0f;
  59. float right = 0.0f;
  60. float bottom = 0.0f;
  61. float top = 0.0f;
  62. switch (mode) {
  63. case 0:
  64. // cover: the axis that sticks out is cut, position 0 keeps the right or the top edge
  65. if (sceneAspect > outputAspect) {
  66. const float half = (width - height / outputHeight * outputWidth) * 0.5f;
  67. left = 2.0f * (1.0f - position) * half;
  68. right = 2.0f * position * half;
  69. } else {
  70. const float half = (height - width / outputWidth * outputHeight) * 0.5f;
  71. top = 2.0f * position * half;
  72. bottom = 2.0f * (1.0f - position) * half;
  73. }
  74. break;
  75. case 1:
  76. // fit: the other axis gets bars, position 0 puts the scene on the left or the top
  77. if (outputAspect > sceneAspect) {
  78. const float half = (width - height / outputHeight * outputWidth) * 0.5f;
  79. left = 2.0f * position * half;
  80. right = 2.0f * (1.0f - position) * half;
  81. } else {
  82. const float half = (height - width / outputWidth * outputHeight) * 0.5f;
  83. top = 2.0f * position * half;
  84. bottom = 2.0f * (1.0f - position) * half;
  85. }
  86. break;
  87. case 3:
  88. // center: native size, x/y 0 keep the left and top edges
  89. left = x * (width - outputWidth);
  90. right = (1.0f - x) * (width - outputWidth);
  91. top = y * (height - outputHeight);
  92. bottom = (1.0f - y) * (height - outputHeight);
  93. break;
  94. case 4:
  95. {
  96. // free: x/y slide the native size scene from past one edge to past the other, the zoom scales the shown
  97. // region by (2 - zoom)^4, no smaller than 1% of it
  98. const float baseLeft = width - x * (outputWidth + width);
  99. const float baseTop = height - y * (outputHeight + height);
  100. const float baseRight = width - (1.0f - x) * (outputWidth + width);
  101. const float baseBottom = height - (1.0f - y) * (outputHeight + height);
  102. const float scale = std::max (-0.99f, std::pow (2.0f - this->m_alignment.zoom, 4.0f) - 1.0f);
  103. const float growY = (height - baseTop - baseBottom) * scale;
  104. const float growX = (width - baseRight - baseLeft) * scale;
  105. left = baseLeft - growX * x;
  106. right = baseRight - growX * (1.0f - x);
  107. top = baseTop - growY * y;
  108. bottom = baseBottom - growY * (1.0f - y);
  109. break;
  110. }
  111. default:
  112. break;
  113. }
  114. return { left, right, bottom, top };
  115. }
  116. void WallpaperState::setMargins (const glm::vec4& margins) {
  117. const float width = static_cast<float> (this->getProjectionWidth ());
  118. const float height = static_cast<float> (this->getProjectionHeight ());
  119. this->m_UVs.ustart = margins.x / width;
  120. this->m_UVs.uend = 1.0f - margins.y / width;
  121. // v runs top down in a flipped state, bottom up otherwise. The scene is laid out y down here, so WE's top margin
  122. // (y up) is the one at the v origin of the canvas
  123. if (this->m_vflip) {
  124. this->m_UVs.vstart = margins.z / height;
  125. this->m_UVs.vend = 1.0f - margins.w / height;
  126. } else {
  127. this->m_UVs.vstart = 1.0f - margins.z / height;
  128. this->m_UVs.vend = margins.w / height;
  129. }
  130. }
  131. template <> void WallpaperState::updateTextureUVs<WallpaperState::TextureUVsScaling::StretchUVs> () {
  132. this->resetUVs ();
  133. }
  134. template <> void WallpaperState::updateTextureUVs<WallpaperState::TextureUVsScaling::ZoomFillUVs> () {
  135. this->setMargins (this->alignmentMargins (0));
  136. }
  137. template <> void WallpaperState::updateTextureUVs<WallpaperState::TextureUVsScaling::ZoomFitUVs> () {
  138. this->setMargins (this->alignmentMargins (1));
  139. }
  140. template <> void WallpaperState::updateTextureUVs<WallpaperState::TextureUVsScaling::DefaultUVs> () {
  141. // WE's default scaling (mode 0 in sub_140183A70) crops whichever axis overflows so the scene covers the output,
  142. // the same as fill: 2977423343's 5760x2610 scene showed clamped edges letterboxed into 16:9
  143. this->updateTextureUVs<TextureUVsScaling::ZoomFillUVs> ();
  144. }
  145. template <> void WallpaperState::updateTextureUVs<WallpaperState::TextureUVsScaling::CenterUVs> () {
  146. // no scale factor at all: crops to native size if the wallpaper is bigger than the viewport, overflows past [0,1]
  147. // (relying on border clamping) to letterbox it if it's smaller
  148. this->setMargins (this->alignmentMargins (3));
  149. }
  150. template <> void WallpaperState::updateTextureUVs<WallpaperState::TextureUVsScaling::FreeUVs> () {
  151. this->setMargins (this->alignmentMargins (4));
  152. }
  153. template <WallpaperState::TextureUVsScaling T> void WallpaperState::updateTextureUVs () {
  154. sLog.exception (
  155. "Using generic template for scaling is not allowed. Write specialization template for your scaling mode.\
  156. This message is for developers, if you are just user it's a bug."
  157. );
  158. }
  159. WallpaperState::TextureUVsScaling WallpaperState::getTextureUVsScaling () const { return this->m_textureUVsMode; }
  160. uint32_t WallpaperState::getClampingMode () const { return this->m_clampingMode; }
  161. void WallpaperState::setTextureUVsStrategy (WallpaperState::TextureUVsScaling strategy) {
  162. if (this->m_textureUVsMode == strategy) {
  163. return;
  164. }
  165. this->m_textureUVsMode = strategy;
  166. this->m_uvsDirty = true;
  167. }
  168. float WallpaperState::getZoom () const { return this->m_zoom; }
  169. void WallpaperState::setZoom (float zoom) {
  170. zoom = std::clamp (zoom, 0.1f, 5.0f);
  171. if (this->m_zoom == zoom) {
  172. return;
  173. }
  174. this->m_zoom = zoom;
  175. this->m_uvsDirty = true;
  176. }
  177. // zoom > 1 shrinks, < 1 grows the UV window around its center, whichever way vflip orders start/end
  178. namespace {
  179. void applyZoomToAxis (float& start, float& end, float zoom) {
  180. const float center = (start + end) / 2.0f;
  181. const float half = (end - start) / 2.0f;
  182. start = center - half / zoom;
  183. end = center + half / zoom;
  184. }
  185. // Slides the UV window toward one edge without changing its size, offset in [-1, 1]; no-op when nothing is cropped on
  186. // the axis
  187. void applyOffsetToAxis (float& start, float& end, float offset) {
  188. if (offset == 0.0f) {
  189. return;
  190. }
  191. const bool inverted = start > end;
  192. float lo = inverted ? end : start;
  193. float hi = inverted ? start : end;
  194. const float size = hi - lo;
  195. const float slack = std::max (0.0f, 1.0f - size);
  196. if (slack <= 0.0f) {
  197. return;
  198. }
  199. const float shift = std::clamp (offset, -1.0f, 1.0f) * slack / 2.0f;
  200. lo = std::clamp (lo + shift, 0.0f, 1.0f - size);
  201. hi = lo + size;
  202. if (inverted) {
  203. start = hi;
  204. end = lo;
  205. } else {
  206. start = lo;
  207. end = hi;
  208. }
  209. }
  210. }
  211. float WallpaperState::getOffsetX () const { return this->m_offsetX; }
  212. float WallpaperState::getOffsetY () const { return this->m_offsetY; }
  213. void WallpaperState::setOffset (float offsetX, float offsetY) {
  214. offsetX = std::clamp (offsetX, -1.0f, 1.0f);
  215. offsetY = std::clamp (offsetY, -1.0f, 1.0f);
  216. if (this->m_offsetX == offsetX && this->m_offsetY == offsetY) {
  217. return;
  218. }
  219. this->m_offsetX = offsetX;
  220. this->m_offsetY = offsetY;
  221. this->m_uvsDirty = true;
  222. }
  223. const WallpaperState::Alignment& WallpaperState::getAlignment () const { return this->m_alignment; }
  224. void WallpaperState::setAlignment (const Alignment& alignment) {
  225. if (this->m_alignment == alignment) {
  226. return;
  227. }
  228. this->m_alignment = alignment;
  229. this->m_uvsDirty = true;
  230. }
  231. int WallpaperState::getViewportWidth () const { return this->m_viewport.width; }
  232. int WallpaperState::getViewportHeight () const { return this->m_viewport.height; }
  233. int WallpaperState::getProjectionWidth () const { return this->m_projection.width; }
  234. int WallpaperState::getProjectionHeight () const { return this->m_projection.height; }
  235. void WallpaperState::updateState (
  236. const glm::ivec4& viewport, const bool& vflip, const int& projectionWidth, const int& projectionHeight
  237. ) {
  238. this->m_viewport.width = viewport.z;
  239. this->m_viewport.height = viewport.w;
  240. this->m_vflip = vflip;
  241. this->m_projection.width = projectionWidth;
  242. this->m_projection.height = projectionHeight;
  243. this->m_uvsDirty = false;
  244. switch (this->getTextureUVsScaling ()) {
  245. case WallpaperState::TextureUVsScaling::StretchUVs:
  246. this->updateTextureUVs<WallpaperState::TextureUVsScaling::StretchUVs> ();
  247. break;
  248. case WallpaperState::TextureUVsScaling::ZoomFillUVs:
  249. this->updateTextureUVs<WallpaperState::TextureUVsScaling::ZoomFillUVs> ();
  250. break;
  251. case WallpaperState::TextureUVsScaling::ZoomFitUVs:
  252. this->updateTextureUVs<WallpaperState::TextureUVsScaling::ZoomFitUVs> ();
  253. break;
  254. case WallpaperState::TextureUVsScaling::CenterUVs:
  255. this->updateTextureUVs<WallpaperState::TextureUVsScaling::CenterUVs> ();
  256. break;
  257. case WallpaperState::TextureUVsScaling::FreeUVs:
  258. this->updateTextureUVs<WallpaperState::TextureUVsScaling::FreeUVs> ();
  259. break;
  260. case WallpaperState::TextureUVsScaling::DefaultUVs:
  261. this->updateTextureUVs<WallpaperState::TextureUVsScaling::DefaultUVs> ();
  262. break;
  263. default:
  264. sLog.exception (
  265. "Switch case for specified scaling mode doesn't exist. Add your realisation in switch statement.\
  266. This message is for developers, if you are just user it's a bug."
  267. );
  268. break;
  269. }
  270. if (this->m_zoom != 1.0f) {
  271. applyZoomToAxis (this->m_UVs.ustart, this->m_UVs.uend, this->m_zoom);
  272. applyZoomToAxis (this->m_UVs.vstart, this->m_UVs.vend, this->m_zoom);
  273. }
  274. // applied after zoom, a more zoomed-in view has more room to pan
  275. applyOffsetToAxis (this->m_UVs.ustart, this->m_UVs.uend, this->m_offsetX);
  276. applyOffsetToAxis (this->m_UVs.vstart, this->m_UVs.vend, this->m_offsetY);
  277. }