Draw2d.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. #pragma once
  9. #include <LyShine/IDraw2d.h>
  10. #include <AzFramework/Font/FontInterface.h>
  11. #include <Atom/Bootstrap/BootstrapNotificationBus.h>
  12. #include <Atom/RPI.Public/DynamicDraw/DynamicDrawInterface.h>
  13. #include <Atom/RPI.Public/ViewportContext.h>
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. //! Implementation of IDraw2d interface for 2D drawing in screen space
  16. //
  17. //! The CDraw2d class implements the IDraw2d interface for drawing 2D images, shapes and text.
  18. //! Positions and sizes are specified in pixels in the associated 2D viewport.
  19. class CDraw2d
  20. : public IDraw2d
  21. , public AZ::Render::Bootstrap::NotificationBus::Handler
  22. {
  23. public: // types
  24. public: // member functions
  25. //! Constructor, constructed by the LyShine class
  26. CDraw2d(AZ::RPI::ViewportContextPtr viewportContext = nullptr);
  27. // IDraw2d
  28. ~CDraw2d() override;
  29. // ~IDraw2d
  30. //! Draw a textured quad with the top left corner at the given position.
  31. //
  32. //! The image is drawn with the color specified by SetShapeColor and the opacity
  33. //! passed as an argument.
  34. //! If rotation is non-zero then the quad is rotated. If the pivot point is
  35. //! provided then the points of the quad are rotated about that point, otherwise
  36. //! they are rotated about the top left corner of the quad.
  37. //! \param texId The texture ID returned by ITexture::GetTextureID()
  38. //! \param position Position of the top left corner of the quad (before rotation) in pixels
  39. //! \param size The width and height of the quad. Use texture width and height to avoid minification,
  40. //! magnification or stretching (assuming the minMaxTexCoords are left to the default)
  41. //! \param opacity The alpha value used when blending
  42. //! \param rotation Angle of rotation in degrees counter-clockwise
  43. //! \param pivotPoint The point about which the quad is rotated
  44. //! \param minMaxTexCoords An optional two component array. The first component is the UV coord for the top left
  45. //! point of the quad and the second is the UV coord of the bottom right point of the quad
  46. //! \param imageOptions Optional struct specifying options that tend to be the same from call to call
  47. void DrawImage(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vector2 position, AZ::Vector2 size, float opacity = 1.0f,
  48. float rotation = 0.0f, const AZ::Vector2* pivotPoint = nullptr, const AZ::Vector2* minMaxTexCoords = nullptr,
  49. ImageOptions* imageOptions = nullptr) override;
  50. //! Draw a textured quad where the position specifies the point specified by the alignment.
  51. //
  52. //! Rotation is always around the position.
  53. //! \param texId The texture ID returned by ITexture::GetTextureID()
  54. //! \param position Position align point of the quad (before rotation) in pixels
  55. //! \param size The width and height of the quad. Use texture width and height to avoid minification,
  56. //! magnification or stretching (assuming the minMaxTexCoords are left to the default)
  57. //! \param horizontalAlignment Specifies how the quad is horizontally aligned to the given position
  58. //! \param verticalAlignment Specifies how the quad is vertically aligned to the given position
  59. //! \param opacity The alpha value used when blending
  60. //! \param rotation Angle of rotation in degrees counter-clockwise
  61. //! \param minMaxTexCoords An optional two component array. The first component is the UV coord for the top left
  62. //! point of the quad and the second is the UV coord of the bottom right point of the quad
  63. //! \param imageOptions Optional struct specifying options that tend to be the same from call to call
  64. void DrawImageAligned(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vector2 position, AZ::Vector2 size,
  65. HAlign horizontalAlignment, VAlign verticalAlignment,
  66. float opacity = 1.0f, float rotation = 0.0f, const AZ::Vector2* minMaxTexCoords = nullptr,
  67. ImageOptions* imageOptions = nullptr) override;
  68. //! Draw a textured quad where the position, color and uv of each point is specified explicitly
  69. //
  70. //! \param texId The texture ID returned by ITexture::GetTextureID()
  71. //! \param verts An array of 4 vertices, in clockwise order (e.g. top left, top right, bottom right, bottom left)
  72. //! \param pixelRounding Whether and how to round pixel coordinates
  73. //! \param renderState Blend mode and depth state
  74. void DrawQuad(AZ::Data::Instance<AZ::RPI::Image> image,
  75. VertexPosColUV* verts,
  76. Rounding pixelRounding = Rounding::Nearest,
  77. bool clamp = false,
  78. const RenderState& renderState = RenderState{}) override;
  79. //! Draw a line
  80. //
  81. //! \param start The start position
  82. //! \param end The end position
  83. //! \param color The color of the line
  84. //! \param pixelRounding Whether and how to round pixel coordinates
  85. //! \param renderState Blend mode and depth state
  86. void DrawLine(AZ::Vector2 start, AZ::Vector2 end, AZ::Color color,
  87. IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
  88. const RenderState& renderState = RenderState{}) override;
  89. //! Draw a line with a texture so it can be dotted or dashed
  90. //
  91. //! \param texId The texture ID returned by ITexture::GetTextureID()
  92. //! \param verts An array of 2 vertices for the start and end points of the line
  93. //! \param pixelRounding Whether and how to round pixel coordinates
  94. //! \param renderState Blend mode and depth state
  95. void DrawLineTextured(AZ::Data::Instance<AZ::RPI::Image> image,
  96. VertexPosColUV* verts,
  97. IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
  98. const RenderState& renderState = RenderState{}) override;
  99. //! Draw a text string. Only supports ASCII text.
  100. //
  101. //! The font and effect used to render the text are specified in the textOptions structure
  102. //! \param textString A null terminated ASCII text string. May contain \n characters
  103. //! \param position Position of the text in pixels. Alignment values in textOptions affect actual position
  104. //! \param pointSize The size of the font to use
  105. //! \param opacity The opacity (alpha value) to use to draw the text
  106. //! \param textOptions Pointer to an options struct. If null the default options are used
  107. void DrawText(const char* textString, AZ::Vector2 position, float pointSize,
  108. float opacity = 1.0f, TextOptions* textOptions = nullptr) override;
  109. //! Draw a rectangular outline with a texture
  110. //
  111. //! \param image The texture to be used for drawing the outline
  112. //! \param points The rect's vertices (top left, top right, bottom right, bottom left)
  113. //! \param rightVec Right vector. Specified because the rect's width/height could be 0
  114. //! \param downVec Down vector. Specified because the rect's width/height could be 0
  115. //! \param color The color of the outline
  116. //! \param lineThickness The thickness in pixels of the outline. If 0, it will be based on image height
  117. void DrawRectOutlineTextured(AZ::Data::Instance<AZ::RPI::Image> image,
  118. UiTransformInterface::RectPoints points,
  119. AZ::Vector2 rightVec,
  120. AZ::Vector2 downVec,
  121. AZ::Color color,
  122. uint32_t lineThickness = 0) override;
  123. //! Get the width and height (in pixels) that would be used to draw the given text string.
  124. //
  125. //! Pass the same parameter values that would be used to draw the string
  126. AZ::Vector2 GetTextSize(const char* textString, float pointSize, TextOptions* textOptions = nullptr) override;
  127. //! Get the width of the rendering viewport (in pixels).
  128. float GetViewportWidth() const override;
  129. //! Get the height of the rendering viewport (in pixels).
  130. float GetViewportHeight() const override;
  131. //! Get dpi scale factor
  132. float GetViewportDpiScalingFactor() const override;
  133. //! Get the default values that would be used if no image options were passed in
  134. //
  135. //! This is a convenient way to initialize the imageOptions struct
  136. const ImageOptions& GetDefaultImageOptions() const override;
  137. //! Get the default values that would be used if no text options were passed in
  138. //
  139. //! This is a convenient way to initialize the textOptions struct
  140. const TextOptions& GetDefaultTextOptions() const override;
  141. //! Render the primitives that have been deferred
  142. void RenderDeferredPrimitives() override;
  143. //! Specify whether to defer future primitives or render them right away
  144. void SetDeferPrimitives(bool deferPrimitives) override;
  145. //! Return whether future primitives will be deferred or rendered right away
  146. bool GetDeferPrimitives() override;
  147. //! Set sort key offset for following draws.
  148. void SetSortKey(int64_t key) override;
  149. private:
  150. AZ_DISABLE_COPY_MOVE(CDraw2d);
  151. // AZ::Render::Bootstrap::NotificationBus overrides
  152. void OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene) override;
  153. public: // static member functions
  154. //! Helper to load a texture
  155. static AZ::Data::Instance<AZ::RPI::Image> LoadTexture(const AZStd::string& pathName);
  156. protected: // types and constants
  157. enum
  158. {
  159. MAX_VERTICES_IN_PRIM = 6
  160. };
  161. // Cached shader data
  162. struct Draw2dShaderData
  163. {
  164. AZ::RHI::ShaderInputImageIndex m_imageInputIndex;
  165. AZ::RHI::ShaderInputConstantIndex m_viewProjInputIndex;
  166. AZ::RPI::ShaderVariantId m_shaderOptionsClamp;
  167. AZ::RPI::ShaderVariantId m_shaderOptionsWrap;
  168. };
  169. class DeferredPrimitive
  170. {
  171. public:
  172. virtual ~DeferredPrimitive() {};
  173. virtual void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
  174. const Draw2dShaderData& shaderData,
  175. AZ::RPI::ViewportContextPtr viewportContext) const = 0;
  176. };
  177. class DeferredQuad
  178. : public DeferredPrimitive
  179. {
  180. public:
  181. ~DeferredQuad() override {};
  182. void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
  183. const Draw2dShaderData& shaderData,
  184. AZ::RPI::ViewportContextPtr viewportContext) const override;
  185. AZ::Vector2 m_points[4];
  186. AZ::Vector2 m_texCoords[4];
  187. uint32 m_packedColors[4];
  188. AZ::Data::Instance<AZ::RPI::Image> m_image;
  189. bool m_clamp;
  190. RenderState m_renderState;
  191. };
  192. class DeferredLine
  193. : public DeferredPrimitive
  194. {
  195. public:
  196. ~DeferredLine() override {};
  197. void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
  198. const Draw2dShaderData& shaderData,
  199. AZ::RPI::ViewportContextPtr viewportContext) const override;
  200. AZ::Data::Instance<AZ::RPI::Image> m_image;
  201. AZ::Vector2 m_points[2];
  202. AZ::Vector2 m_texCoords[2];
  203. uint32 m_packedColors[2];
  204. RenderState m_renderState;
  205. };
  206. class DeferredText
  207. : public DeferredPrimitive
  208. {
  209. public:
  210. ~DeferredText() override {};
  211. void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
  212. const Draw2dShaderData& shaderData,
  213. AZ::RPI::ViewportContextPtr viewportContext) const override;
  214. AzFramework::TextDrawParameters m_drawParameters;
  215. AzFramework::FontId m_fontId;
  216. std::string m_string;
  217. };
  218. class DeferredRectOutline
  219. : public DeferredPrimitive
  220. {
  221. public:
  222. ~DeferredRectOutline() override {};
  223. void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
  224. const Draw2dShaderData& shaderData,
  225. AZ::RPI::ViewportContextPtr viewportContext) const override;
  226. AZ::Data::Instance<AZ::RPI::Image> m_image;
  227. static constexpr int32 NUM_VERTS = 8;
  228. AZ::Vector2 m_verts2d[NUM_VERTS];
  229. AZ::Vector2 m_uvs[NUM_VERTS];
  230. AZ::Color m_color;
  231. };
  232. protected: // member functions
  233. //! Rotate an array of points around the z-axis at the pivot point.
  234. //
  235. //! Angle is in degrees counter-clockwise
  236. void RotatePointsAboutPivot(AZ::Vector2* points, int numPoints, AZ::Vector2 pivot, float angle) const;
  237. //! Helper function to render a text string
  238. void DrawTextInternal(const char* textString, AzFramework::FontId fontId, unsigned int effectIndex,
  239. AZ::Vector2 position, float pointSize, AZ::Color color, float rotation,
  240. HAlign horizontalAlignment, VAlign verticalAlignment, bool depthTestEnabled);
  241. //! Draw or defer a quad
  242. void DrawOrDeferQuad(const DeferredQuad* quad);
  243. //! Draw or defer a line
  244. void DrawOrDeferLine(const DeferredLine* line);
  245. //! Draw or defer a text string
  246. void DrawOrDeferTextString(const DeferredText* text);
  247. //! Draw or defer a rect outline
  248. void DrawOrDeferRectOutline(const DeferredRectOutline* outlineRect);
  249. //! Get specified viewport context or default viewport context if not specified
  250. AZ::RPI::ViewportContextPtr GetViewportContext() const;
  251. protected: // attributes
  252. ImageOptions m_defaultImageOptions; //!< The default image options used if nullptr is passed
  253. TextOptions m_defaultTextOptions; //!< The default text options used if nullptr is passed
  254. //! True if the actual render of the primitives should be deferred to a RenderDeferredPrimitives call
  255. bool m_deferCalls;
  256. std::vector<DeferredPrimitive*> m_deferredPrimitives;
  257. AZ::RPI::ViewportContextPtr m_viewportContext;
  258. AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> m_dynamicDraw;
  259. Draw2dShaderData m_shaderData;
  260. };