ViewportIcon.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "EditorCommon.h"
  9. #include <LyShine/IDraw2d.h>
  10. #include <Atom/RPI.Public/Image/StreamingImage.h>
  11. #include <Atom/RPI.Reflect/Image/StreamingImageAsset.h>
  12. float ViewportIcon::m_dpiScaleFactor = 1.0f;
  13. ViewportIcon::ViewportIcon(const char* textureFilename)
  14. {
  15. m_image = Draw2dHelper::LoadTexture(textureFilename);
  16. }
  17. ViewportIcon::~ViewportIcon()
  18. {
  19. }
  20. AZ::Vector2 ViewportIcon::GetTextureSize() const
  21. {
  22. if (m_image)
  23. {
  24. AZ::RHI::Size size = m_image->GetDescriptor().m_size;
  25. AZ::Vector2 scaledSize(static_cast<float>(size.m_width), static_cast<float>(size.m_height));
  26. if (m_applyDpiScaleFactorToSize)
  27. {
  28. scaledSize *= m_dpiScaleFactor;
  29. }
  30. return scaledSize;
  31. }
  32. return AZ::Vector2(0.0f, 0.0f);
  33. }
  34. void ViewportIcon::DrawImageAligned(Draw2dHelper& draw2d, AZ::Vector2& pivot, float opacity)
  35. {
  36. draw2d.DrawImageAligned(m_image,
  37. pivot,
  38. GetTextureSize(),
  39. IDraw2d::HAlign::Center,
  40. IDraw2d::VAlign::Center,
  41. opacity);
  42. }
  43. void ViewportIcon::DrawImageTiled(Draw2dHelper& draw2d, IDraw2d::VertexPosColUV* verts)
  44. {
  45. // Use default blending and rounding modes
  46. IDraw2d::Rounding rounding = IDraw2d::Rounding::Nearest;
  47. draw2d.DrawQuad(m_image, verts, rounding);
  48. }
  49. void ViewportIcon::DrawAxisAlignedBoundingBox(Draw2dHelper& draw2d, AZ::Vector2 bound0, AZ::Vector2 bound1)
  50. {
  51. AZ::Color dottedColor(1.0f, 1.0f, 1.0f, 1.0f);
  52. const float pixelLengthForDottedLineTexture = (1.0f / 8.0f);
  53. float endTexCoordU = fabsf((bound1.GetX() - bound0.GetX()) * pixelLengthForDottedLineTexture);
  54. float endTexCoordV = fabsf((bound1.GetY() - bound0.GetY()) * pixelLengthForDottedLineTexture);
  55. IDraw2d::VertexPosColUV verts[2];
  56. {
  57. verts[0].color = dottedColor;
  58. verts[1].color = dottedColor;
  59. }
  60. // bound0
  61. // A----B
  62. // | |
  63. // C----D
  64. // bound1
  65. //
  66. // Draw line segment A -> B.
  67. {
  68. verts[0].position = AZ::Vector2(bound0.GetX(), bound0.GetY());
  69. verts[1].position = AZ::Vector2(bound1.GetX(), bound0.GetY());
  70. verts[0].uv = AZ::Vector2(0.0f, 0.5f);
  71. verts[1].uv = AZ::Vector2(endTexCoordU, 0.5f);
  72. draw2d.DrawLineTextured(m_image, verts);
  73. }
  74. // bound0
  75. // A----B
  76. // | |
  77. // C----D
  78. // bound1
  79. //
  80. // Draw line segment A -> C.
  81. {
  82. verts[0].position = AZ::Vector2(bound0.GetX(), bound0.GetY());
  83. verts[1].position = AZ::Vector2(bound0.GetX(), bound1.GetY());
  84. verts[0].uv = AZ::Vector2(0.0f, 0.5f);
  85. verts[1].uv = AZ::Vector2(endTexCoordV, 0.5f);
  86. draw2d.DrawLineTextured(m_image, verts);
  87. }
  88. // bound0
  89. // A----B
  90. // | |
  91. // C----D
  92. // bound1
  93. //
  94. // Draw line segment C -> D.
  95. {
  96. verts[0].position = AZ::Vector2(bound0.GetX(), bound1.GetY());
  97. verts[1].position = AZ::Vector2(bound1.GetX(), bound1.GetY());
  98. verts[0].uv = AZ::Vector2(0.0f, 0.5f);
  99. verts[1].uv = AZ::Vector2(endTexCoordU, 0.5f);
  100. draw2d.DrawLineTextured(m_image, verts);
  101. }
  102. // bound0
  103. // A----B
  104. // | |
  105. // C----D
  106. // bound1
  107. //
  108. // Draw line segment B -> D.
  109. {
  110. verts[0].position = AZ::Vector2(bound1.GetX(), bound0.GetY());
  111. verts[1].position = AZ::Vector2(bound1.GetX(), bound1.GetY());
  112. verts[0].uv = AZ::Vector2(0.0f, 0.5f);
  113. verts[1].uv = AZ::Vector2(endTexCoordV, 0.5f);
  114. draw2d.DrawLineTextured(m_image, verts);
  115. }
  116. }
  117. void ViewportIcon::Draw(Draw2dHelper& draw2d, AZ::Vector2 anchorPos, const AZ::Matrix4x4& transform, float iconRot, AZ::Color color) const
  118. {
  119. AZ::Vector2 iconSize = GetTextureSize();
  120. // the icon images are authored with the "point" of the anchor in the center for all icons currently
  121. const AZ::Vector2 originRatio(0.5f, 0.5f);
  122. float iconOriginX = iconSize.GetX() * originRatio.GetX();
  123. float iconOriginY = iconSize.GetY() * originRatio.GetY();
  124. AZ::Vector2 tl(anchorPos.GetX() - iconOriginX, anchorPos.GetY() - iconOriginY);
  125. UiTransformInterface::RectPoints iconPoints;
  126. iconPoints.TopLeft() = tl;
  127. iconPoints.TopRight() = AZ::Vector2(tl.GetX() + iconSize.GetX(), tl.GetY());
  128. iconPoints.BottomLeft() = AZ::Vector2(tl.GetX(), tl.GetY() + iconSize.GetY());
  129. iconPoints.BottomRight() = AZ::Vector2(tl.GetX() + iconSize.GetX(), tl.GetY() + iconSize.GetY());
  130. // apply the rotation that rotates the anchor icon to point in the correct direction
  131. AZ::Vector3 pivot3(anchorPos.GetX(), anchorPos.GetY(), 0);
  132. float rotRad = DEG2RAD(iconRot);
  133. AZ::Matrix4x4 moveToPivotSpaceMat = AZ::Matrix4x4::CreateTranslation(-pivot3);
  134. AZ::Matrix4x4 rotMat = AZ::Matrix4x4::CreateRotationZ(rotRad);
  135. AZ::Matrix4x4 moveFromPivotSpaceMat = AZ::Matrix4x4::CreateTranslation(pivot3);
  136. AZ::Matrix4x4 newTransform = transform * moveFromPivotSpaceMat * rotMat * moveToPivotSpaceMat;
  137. IDraw2d::VertexPosColUV verts[4];
  138. // points are a clockwise quad
  139. static const AZ::Vector2 uvs[4] = {
  140. AZ::Vector2(0.0f, 0.0f), AZ::Vector2(1.0f, 0.0f), AZ::Vector2(1.0f, 1.0f), AZ::Vector2(0.0f, 1.0f)
  141. };
  142. for (int i = 0; i < 4; ++i)
  143. {
  144. verts[i].color = color;
  145. verts[i].uv = uvs[i];
  146. AZ::Vector3 point3(iconPoints.pt[i].GetX(), iconPoints.pt[i].GetY(), 0.0f);
  147. point3 = newTransform * point3;
  148. verts[i].position = AZ::Vector2(point3.GetX(), point3.GetY());
  149. }
  150. // in order to align the anchor icon correctly we do want rotation, shearing and negative scale
  151. // in the transform to affect the icon, but we do not want its size to be affected.
  152. // So we fix up the transformed points so that it has the correct icon width and height in viewport space.
  153. if (transform.GetElement(0, 0) != 1.0f || transform.GetElement(1, 1) != 1.0f || transform.GetElement(2, 2) != 1.0f)
  154. {
  155. AZ::Vector2 widthVec = verts[1].position - verts[0].position;
  156. AZ::Vector2 heightVec = verts[3].position - verts[0].position;
  157. AZ::Vector2 originPos = verts[0].position + widthVec * originRatio.GetX() + heightVec * originRatio.GetY();
  158. // adjust both vectors to be of the desired length (iconW and iconH)
  159. // Avoid a divide by zero. We could compare with 0.0f here and that would avoid a divide
  160. // by zero. However comparing with FLT_EPSILON also avoids the rare case of an overflow.
  161. // FLT_EPSILON is small enough to be considered equivalent to zero in this application.
  162. float widthVecLength = widthVec.GetLength();
  163. float heightVecLength = heightVec.GetLength();
  164. widthVec *= (fabsf(widthVecLength) > FLT_EPSILON) ? iconSize.GetX() / widthVecLength : 0.0f;
  165. heightVec *= (fabsf(heightVecLength) > FLT_EPSILON) ? iconSize.GetY() / heightVecLength : 0.0f;
  166. verts[0].position = originPos - widthVec * originRatio.GetX() - heightVec * originRatio.GetY();
  167. verts[1].position = originPos + widthVec * (1.0f - originRatio.GetX()) - heightVec * originRatio.GetY();
  168. verts[2].position = originPos + widthVec * (1.0f - originRatio.GetX()) + heightVec * (1.0f - originRatio.GetY());
  169. verts[3].position = originPos - widthVec * originRatio.GetX() + heightVec * (1.0f - originRatio.GetY());
  170. }
  171. draw2d.DrawQuad(m_image, verts);
  172. }
  173. void ViewportIcon::DrawAnchorLines(Draw2dHelper& draw2d, AZ::Vector2 anchorPos, AZ::Vector2 targetPos, const AZ::Matrix4x4& transform,
  174. bool xFirst, bool xText, bool yText)
  175. {
  176. AZ::Vector2 cornerPos = (xFirst) ? AZ::Vector2(targetPos.GetX(), anchorPos.GetY()) : AZ::Vector2(anchorPos.GetX(), targetPos.GetY());
  177. AZ::Vector3 start3 = EntityHelpers::MakeVec3(anchorPos);
  178. AZ::Vector3 end3 = EntityHelpers::MakeVec3(targetPos);
  179. AZ::Vector3 corner3 = EntityHelpers::MakeVec3(cornerPos);
  180. start3 = transform * start3;
  181. corner3 = transform * corner3;
  182. end3 = transform * end3;
  183. AZ::Vector2 start2(start3.GetX(), start3.GetY());
  184. AZ::Vector2 corner2(corner3.GetX(), corner3.GetY());
  185. AZ::Vector2 end2(end3.GetX(), end3.GetY());
  186. AZ::Color solidColor(1.0f, 1.0f, 1.0f, 0.2f);
  187. if ((xFirst && xText) || (!xFirst && yText))
  188. {
  189. float displayDistance = (xFirst) ? cornerPos.GetX() - anchorPos.GetX() : cornerPos.GetY() - anchorPos.GetY();
  190. DrawDistanceLine(draw2d, start2, corner2, displayDistance);
  191. }
  192. else
  193. {
  194. draw2d.DrawLine(start2, corner2, solidColor);
  195. }
  196. if ((!xFirst && xText) || (xFirst && yText))
  197. {
  198. float displayDistance = (!xFirst) ? targetPos.GetX() - cornerPos.GetX() : targetPos.GetY() - cornerPos.GetY();
  199. DrawDistanceLine(draw2d, corner2, end2, displayDistance);
  200. }
  201. else
  202. {
  203. draw2d.DrawLine(corner2, end2, solidColor);
  204. }
  205. }
  206. void ViewportIcon::DrawDistanceLine(Draw2dHelper& draw2d, AZ::Vector2 start, AZ::Vector2 end, float displayDistance, const char* suffix)
  207. {
  208. // draw a dotted line with the distance displayed on it
  209. AZ::Color dottedColor(1.0f, 1.0f, 1.0f, 1.0f);
  210. float length = AZ::Vector2(end - start).GetLength();
  211. const float pixelLengthForDottedLineTexture = 8.0f;
  212. float endTexCoordU = length / pixelLengthForDottedLineTexture;
  213. IDraw2d::VertexPosColUV verts[2];
  214. verts[0].position = start;
  215. verts[0].color = dottedColor;
  216. verts[0].uv = AZ::Vector2(0.0f, 0.5f);
  217. verts[1].position = end;
  218. verts[1].color = dottedColor;
  219. verts[1].uv = AZ::Vector2(endTexCoordU, 0.5f);
  220. draw2d.DrawLineTextured(m_image, verts);
  221. // Now draw the text rotated to match the angle of the line and slightly offset from the center point
  222. // first swap the start end such that the line always goes left to right
  223. if (start.GetX() == end.GetX())
  224. {
  225. if (start.GetY() < end.GetY())
  226. {
  227. std::swap(start, end);
  228. }
  229. }
  230. else if (start.GetX() > end.GetX())
  231. {
  232. std::swap(start, end);
  233. }
  234. // get the angle of the line (will always be > (-90 < angle <= 90)
  235. float rotRad = atan2f(end.GetY() - start.GetY(), end.GetX() - start.GetX());
  236. float rotation = RAD2DEG(rotRad);
  237. // offset the bottom center of the text from the line by a fixed offset,
  238. // we rotate the offset to match line angle
  239. const float offsetDist = 2.0f;
  240. AZ::Vector2 textOffset(offsetDist * sinf(rotRad), -offsetDist * cosf(rotRad));
  241. // position for text is the midpoint of the line plus the offset
  242. AZ::Vector2 textPos = (start + end) * 0.5f + textOffset;
  243. const size_t bufSize = 32;
  244. char textBuf[bufSize];
  245. azsnprintf(textBuf, bufSize, "%.2f%s", displayDistance, suffix ? suffix : "");
  246. draw2d.SetTextAlignment(IDraw2d::HAlign::Center, IDraw2d::VAlign::Bottom);
  247. draw2d.SetTextRotation(rotation);
  248. draw2d.DrawText(textBuf, textPos, 8.0f, 1.0f);
  249. }
  250. void ViewportIcon::DrawAnchorLinesSplit(Draw2dHelper& draw2d, AZ::Vector2 anchorPos1, AZ::Vector2 anchorPos2,
  251. AZ::Vector2 targetPos, const AZ::Matrix4x4& transform, bool horizSplit, const char* suffix)
  252. {
  253. AZ::Vector2 cornerPos = (horizSplit) ? AZ::Vector2(targetPos.GetX(), anchorPos1.GetY()) : AZ::Vector2(anchorPos1.GetX(), targetPos.GetY());
  254. AZ::Vector3 start1_3 = EntityHelpers::MakeVec3(anchorPos1);
  255. AZ::Vector3 start2_3 = EntityHelpers::MakeVec3(anchorPos2);
  256. AZ::Vector3 end3 = EntityHelpers::MakeVec3(targetPos);
  257. AZ::Vector3 corner3 = EntityHelpers::MakeVec3(cornerPos);
  258. start1_3 = transform * start1_3;
  259. start2_3 = transform * start2_3;
  260. corner3 = transform * corner3;
  261. end3 = transform * end3;
  262. AZ::Vector2 start1_2(start1_3.GetX(), start1_3.GetY());
  263. AZ::Vector2 start2_2(start2_3.GetX(), start2_3.GetY());
  264. AZ::Vector2 corner2(corner3.GetX(), corner3.GetY());
  265. AZ::Vector2 end2(end3.GetX(), end3.GetY());
  266. AZ::Color solidColor(1.0f, 1.0f, 1.0f, 0.2f);
  267. draw2d.DrawLine(start1_2, corner2, solidColor);
  268. draw2d.DrawLine(corner2, start2_2, solidColor);
  269. float displayDistance = (!horizSplit) ? targetPos.GetX() - cornerPos.GetX() : targetPos.GetY() - cornerPos.GetY();
  270. DrawDistanceLine(draw2d, corner2, end2, displayDistance, suffix);
  271. }
  272. void ViewportIcon::DrawDistanceLineWithTransform(Draw2dHelper& draw2d, AZ::Vector2 sourcePos, AZ::Vector2 targetPos, const AZ::Matrix4x4& transform,
  273. float value, const char* suffix)
  274. {
  275. AZ::Vector3 start3 = EntityHelpers::MakeVec3(sourcePos);
  276. AZ::Vector3 end3 = EntityHelpers::MakeVec3(targetPos);
  277. start3 = transform * start3;
  278. end3 = transform * end3;
  279. AZ::Vector2 start2(start3.GetX(), start3.GetY());
  280. AZ::Vector2 end2(end3.GetX(), end3.GetY());
  281. AZ::Color solidColor(1.0f, 1.0f, 1.0f, 0.2f);
  282. DrawDistanceLine(draw2d, start2, end2, value, suffix);
  283. }
  284. void ViewportIcon::DrawElementRectOutline(Draw2dHelper& draw2d, AZ::EntityId entityId, AZ::Color color)
  285. {
  286. // get the transformed rect for the element
  287. UiTransformInterface::RectPoints points;
  288. UiTransformBus::Event(entityId, &UiTransformBus::Events::GetViewportSpacePoints, points);
  289. // work out if we should snap to exact pixel
  290. AZ::EntityId canvasEntityId;
  291. UiElementBus::EventResult(canvasEntityId, entityId, &UiElementBus::Events::GetCanvasEntityId);
  292. bool isPixelAligned = true;
  293. UiCanvasBus::EventResult(isPixelAligned, canvasEntityId, &UiCanvasBus::Events::GetIsPixelAligned);
  294. IDraw2d::Rounding pixelRounding = isPixelAligned ? IDraw2d::Rounding::Nearest : IDraw2d::Rounding::None;
  295. // round the points to the nearest pixel if the canvas is set to do that for elements since
  296. // we want this outline to line up with the element
  297. for (int i = 0; i < 4; ++i)
  298. {
  299. points.pt[i] = Draw2dHelper::RoundXY(points.pt[i], pixelRounding);
  300. }
  301. // since the rect is transformed we have to add the offsets by multiplying them
  302. // by unit vectors parallel with the edges of the rect. However, the rect could be
  303. // zero width and/or height so we can't use "points" to compute these unit vectors.
  304. // So we instead get the transform matrix and transform two unit vectors
  305. // and then normalize them (they have to be re-normalized since the transform can scale them)
  306. AZ::Matrix4x4 transform;
  307. UiTransformBus::Event(entityId, &UiTransformBus::Events::GetTransformToViewport, transform);
  308. AZ::Vector3 rightVec3(1.0f, 0.0f, 0.0f);
  309. AZ::Vector3 downVec3(0.0f, 1.0f, 0.0f);
  310. rightVec3 = transform.Multiply3x3(rightVec3);
  311. downVec3 = transform.Multiply3x3(downVec3);
  312. AZ::Vector2 rightVec(rightVec3.GetX(), rightVec3.GetY());
  313. AZ::Vector2 downVec(downVec3.GetX(), downVec3.GetY());
  314. rightVec.NormalizeSafe();
  315. downVec.NormalizeSafe();
  316. uint32_t lineThickness = aznumeric_cast<uint32_t>(GetTextureSize().GetY());
  317. draw2d.DrawRectOutlineTextured(m_image, points, rightVec, downVec, color, lineThickness);
  318. }