ViewportCanvasBackground.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <LyShine/UiSerializeHelpers.h>
  9. #include "EditorCommon.h"
  10. ViewportCanvasBackground::ViewportCanvasBackground()
  11. : m_canvasBackground(new ViewportIcon("Editor/Icons/Viewport/Canvas_Background.tif"))
  12. {
  13. }
  14. ViewportCanvasBackground::~ViewportCanvasBackground()
  15. {
  16. }
  17. void ViewportCanvasBackground::Draw(Draw2dHelper& draw2d, const AZ::Vector2& canvasSize, float canvasToViewportScale, const AZ::Vector3& canvasToViewportTranslation)
  18. {
  19. // Determine size of canvas on-screen by applying the current canvas-to-viewport scale
  20. const int scaledCanvasWidth = aznumeric_cast<int>(canvasSize.GetX() * canvasToViewportScale);
  21. const int scaledCanvasHeight = aznumeric_cast<int>(canvasSize.GetY() * canvasToViewportScale);
  22. // Take on-screen canvas panning/translation into account
  23. const float xCanvasPanOffset = canvasToViewportTranslation.GetX();
  24. const float yCanvasPanOffset = canvasToViewportTranslation.GetY();
  25. AZ::Vector2 topLeft(xCanvasPanOffset, yCanvasPanOffset);
  26. AZ::Vector2 topRight(xCanvasPanOffset + scaledCanvasWidth, yCanvasPanOffset);
  27. AZ::Vector2 bottomRight(xCanvasPanOffset + scaledCanvasWidth, yCanvasPanOffset + scaledCanvasHeight);
  28. AZ::Vector2 bottomLeft(xCanvasPanOffset, yCanvasPanOffset + scaledCanvasHeight);
  29. // points are a clockwise quad
  30. static const unsigned int quadVertCount = 4;
  31. AZ::Vector2 positions[quadVertCount] =
  32. {
  33. topLeft,
  34. topRight,
  35. bottomRight,
  36. bottomLeft
  37. };
  38. // scale UV's so that one texel is one pixel on screen
  39. AZ::Vector2 textureSize(m_canvasBackground->GetTextureSize());
  40. AZ::Vector2 rectSize(aznumeric_cast<float>(scaledCanvasWidth), aznumeric_cast<float>(scaledCanvasHeight));
  41. AZ::Vector2 uvScale(rectSize.GetX() / textureSize.GetX(), rectSize.GetY() / textureSize.GetY());
  42. // now draw the same as Stretched but with UV's adjusted
  43. const AZ::Vector2 uvs[4] = { AZ::Vector2(0, 0), AZ::Vector2(uvScale.GetX(), 0), AZ::Vector2(uvScale.GetX(), uvScale.GetY()), AZ::Vector2(0, uvScale.GetY()) };
  44. AZ::Color colorWhite(1.0f, 1.0f, 1.0f, 1.0f);
  45. IDraw2d::VertexPosColUV verts[4];
  46. for (int i = 0; i < 4; ++i)
  47. {
  48. verts[i].position = positions[i];
  49. verts[i].color = colorWhite;
  50. verts[i].uv = uvs[i];
  51. }
  52. m_canvasBackground->DrawImageTiled(draw2d, verts);
  53. }