Initial phase of UI Canvas Editor rendering with Atom (#164)
* Move Draw2d.h to Include folder * Initial phase of UI Canvas Editor rendering with Atom * Simplify Draw2d by removing BeginDraw2d/EndDraw2d which is no longer needed * Fix compile errors for non-unity buildsmain
parent
88d0853cfd
commit
c66e2f4bcf
@ -1,79 +0,0 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <LyShine/IDraw2d.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Interface used by UI components to render to the canvas
|
||||
//
|
||||
//! The IUiRenderer provides helper functions for UI rendering and also manages state that
|
||||
//! persists between UI elements when rendering a UI canvas.
|
||||
//! For example one UI component can turn on stencil test and that affects all UI rendering
|
||||
//! until it is turned off.
|
||||
//!
|
||||
//! This is a singleton class that is accessed via IUiRenderer::Get() which is a shortcut for
|
||||
//! gEnv->pLyShine()->GetUiRenderer();
|
||||
class IUiRenderer
|
||||
{
|
||||
public: // types
|
||||
|
||||
|
||||
public: // member functions
|
||||
|
||||
//! Implement virtual destructor for safety.
|
||||
virtual ~IUiRenderer() {}
|
||||
|
||||
//! Start the rendering of a UI canvas
|
||||
virtual void BeginCanvasRender(AZ::Vector2 viewportSize) = 0;
|
||||
|
||||
//! End the rendering of a UI canvas
|
||||
virtual void EndCanvasRender() = 0;
|
||||
|
||||
//! Get the current base state
|
||||
virtual int GetBaseState() = 0;
|
||||
|
||||
//! Set the base state
|
||||
virtual void SetBaseState(int state) = 0;
|
||||
|
||||
//! Get the current stencil test reference value
|
||||
virtual uint32 GetStencilRef() = 0;
|
||||
|
||||
//! Set the stencil test reference value
|
||||
virtual void SetStencilRef(uint32) = 0;
|
||||
|
||||
//! Increment the current stencil reference value
|
||||
virtual void IncrementStencilRef() = 0;
|
||||
|
||||
//! Decrement the current stencil reference value
|
||||
virtual void DecrementStencilRef() = 0;
|
||||
|
||||
//! Get flag that indicates we are rendering into a mask. Used to avoid masks on child mask elements.
|
||||
virtual bool IsRenderingToMask() = 0;
|
||||
|
||||
//! Set flag that we are rendering into a mask. Used to avoid masks on child mask elements.
|
||||
virtual void SetIsRenderingToMask(bool isRenderingToMask) = 0;
|
||||
|
||||
//! Push an alpha fade, this is multiplied with any existing alpha fade from parents
|
||||
virtual void PushAlphaFade(float alphaFadeValue) = 0;
|
||||
|
||||
//! Pop an alpha fade off the stack
|
||||
virtual void PopAlphaFade() = 0;
|
||||
|
||||
//! Get the current alpha fade value
|
||||
virtual float GetAlphaFade() const = 0;
|
||||
|
||||
public: // static member functions
|
||||
|
||||
//! Helper function to get the singleton UiRenderer
|
||||
static IUiRenderer* Get() { return gEnv->pLyShine->GetUiRenderer(); }
|
||||
};
|
||||
@ -0,0 +1,517 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <LyShine/IDraw2d.h>
|
||||
#include <LyShine/ILyShine.h>
|
||||
|
||||
#include <Atom/Bootstrap/BootstrapNotificationBus.h>
|
||||
#include <Atom/RPI.Public/DynamicDraw/DynamicDrawInterface.h>
|
||||
#include <Atom/RPI.Reflect/Image/Image.h>
|
||||
#include <Atom/RPI.Public/ViewportContext.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Implementation of IDraw2d interface for 2D drawing in screen space
|
||||
//
|
||||
//! The CDraw2d class implements the IDraw2d interface for drawing 2D images, shapes and text.
|
||||
//! Positions and sizes are specified in pixels in the associated 2D viewport.
|
||||
class CDraw2d
|
||||
: public IDraw2d // LYSHINE_ATOM_TODO - keep around until gEnv->pLyShine is replaced by bus interface
|
||||
, public AZ::Render::Bootstrap::NotificationBus::Handler
|
||||
{
|
||||
public: // member functions
|
||||
|
||||
//! Constructor, constructed by the LyShine class
|
||||
CDraw2d(AZ::RPI::ViewportContextPtr viewportContext = nullptr);
|
||||
|
||||
// IDraw2d
|
||||
|
||||
~CDraw2d() override;
|
||||
|
||||
// ~IDraw2d
|
||||
|
||||
//! Draw a textured quad with the top left corner at the given position.
|
||||
//
|
||||
//! The image is drawn with the color specified by SetShapeColor and the opacity
|
||||
//! passed as an argument.
|
||||
//! If rotation is non-zero then the quad is rotated. If the pivot point is
|
||||
//! provided then the points of the quad are rotated about that point, otherwise
|
||||
//! they are rotated about the top left corner of the quad.
|
||||
//! \param texId The texture ID returned by ITexture::GetTextureID()
|
||||
//! \param position Position of the top left corner of the quad (before rotation) in pixels
|
||||
//! \param size The width and height of the quad. Use texture width and height to avoid minification,
|
||||
//! magnification or stretching (assuming the minMaxTexCoords are left to the default)
|
||||
//! \param opacity The alpha value used when blending
|
||||
//! \param rotation Angle of rotation in degrees counter-clockwise
|
||||
//! \param pivotPoint The point about which the quad is rotated
|
||||
//! \param minMaxTexCoords An optional two component array. The first component is the UV coord for the top left
|
||||
//! point of the quad and the second is the UV coord of the bottom right point of the quad
|
||||
//! \param imageOptions Optional struct specifying options that tend to be the same from call to call
|
||||
void DrawImage(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vector2 position, AZ::Vector2 size, float opacity = 1.0f,
|
||||
float rotation = 0.0f, const AZ::Vector2* pivotPoint = nullptr, const AZ::Vector2* minMaxTexCoords = nullptr,
|
||||
ImageOptions* imageOptions = nullptr);
|
||||
|
||||
//! Draw a textured quad where the position specifies the point specified by the alignment.
|
||||
//
|
||||
//! Rotation is always around the position.
|
||||
//! \param texId The texture ID returned by ITexture::GetTextureID()
|
||||
//! \param position Position align point of the quad (before rotation) in pixels
|
||||
//! \param size The width and height of the quad. Use texture width and height to avoid minification,
|
||||
//! magnification or stretching (assuming the minMaxTexCoords are left to the default)
|
||||
//! \param horizontalAlignment Specifies how the quad is horizontally aligned to the given position
|
||||
//! \param verticalAlignment Specifies how the quad is vertically aligned to the given position
|
||||
//! \param opacity The alpha value used when blending
|
||||
//! \param rotation Angle of rotation in degrees counter-clockwise
|
||||
//! \param minMaxTexCoords An optional two component array. The first component is the UV coord for the top left
|
||||
//! point of the quad and the second is the UV coord of the bottom right point of the quad
|
||||
//! \param imageOptions Optional struct specifying options that tend to be the same from call to call
|
||||
void DrawImageAligned(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vector2 position, AZ::Vector2 size,
|
||||
HAlign horizontalAlignment, VAlign verticalAlignment,
|
||||
float opacity = 1.0f, float rotation = 0.0f, const AZ::Vector2* minMaxTexCoords = nullptr,
|
||||
ImageOptions* imageOptions = nullptr);
|
||||
|
||||
//! Draw a textured quad where the position, color and uv of each point is specified explicitly
|
||||
//
|
||||
//! \param texId The texture ID returned by ITexture::GetTextureID()
|
||||
//! \param verts An array of 4 vertices, in clockwise order (e.g. top left, top right, bottom right, bottom left)
|
||||
//! \param blendMode UseDefault means default blend mode (currently GS_BLSRC_SRCALPHA | GS_BLDST_ONEMINUSSRCALPHA)
|
||||
//! \param pixelRounding Whether and how to round pixel coordinates
|
||||
//! \param baseState Additional render state to pass to or into value passed to renderer SetState
|
||||
virtual void DrawQuad(AZ::Data::Instance<AZ::RPI::Image> image,
|
||||
VertexPosColUV* verts,
|
||||
int blendMode = UseDefault,
|
||||
Rounding pixelRounding = Rounding::Nearest,
|
||||
int baseState = UseDefault);
|
||||
|
||||
//! Draw a line
|
||||
//
|
||||
//! \param start The start position
|
||||
//! \param end The end position
|
||||
//! \param color The color of the line
|
||||
//! \param blendMode UseDefault means default blend mode (currently GS_BLSRC_SRCALPHA | GS_BLDST_ONEMINUSSRCALPHA)
|
||||
//! \param pixelRounding Whether and how to round pixel coordinates
|
||||
//! \param baseState Additional render state to pass to or into value passed to renderer SetState
|
||||
virtual void DrawLine(AZ::Vector2 start, AZ::Vector2 end, AZ::Color color,
|
||||
int blendMode = UseDefault,
|
||||
IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
|
||||
int baseState = UseDefault);
|
||||
|
||||
//! Draw a line with a texture so it can be dotted or dashed
|
||||
//
|
||||
//! \param texId The texture ID returned by ITexture::GetTextureID()
|
||||
//! \param verts An array of 2 vertices for the start and end points of the line
|
||||
//! \param blendMode UseDefault means default blend mode (currently GS_BLSRC_SRCALPHA | GS_BLDST_ONEMINUSSRCALPHA)
|
||||
//! \param pixelRounding Whether and how to round pixel coordinates
|
||||
//! \param baseState Additional render state to pass to or into value passed to renderer SetState
|
||||
virtual void DrawLineTextured(AZ::Data::Instance<AZ::RPI::Image> image,
|
||||
VertexPosColUV* verts,
|
||||
int blendMode = UseDefault,
|
||||
IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
|
||||
int baseState = UseDefault);
|
||||
//! Draw a text string. Only supports ASCII text.
|
||||
//
|
||||
//! The font and effect used to render the text are specified in the textOptions structure
|
||||
//! \param textString A null terminated ASCII text string. May contain \n characters
|
||||
//! \param position Position of the text in pixels. Alignment values in textOptions affect actual position
|
||||
//! \param pointSize The size of the font to use
|
||||
//! \param opacity The opacity (alpha value) to use to draw the text
|
||||
//! \param textOptions Pointer to an options struct. If null the default options are used
|
||||
void DrawText(const char* textString, AZ::Vector2 position, float pointSize,
|
||||
float opacity = 1.0f, TextOptions* textOptions = nullptr);
|
||||
|
||||
//! Get the width and height (in pixels) that would be used to draw the given text string.
|
||||
//
|
||||
//! Pass the same parameter values that would be used to draw the string
|
||||
AZ::Vector2 GetTextSize(const char* textString, float pointSize, TextOptions* textOptions = nullptr);
|
||||
|
||||
//! Get the width of the rendering viewport (in pixels).
|
||||
float GetViewportWidth() const;
|
||||
|
||||
//! Get the height of the rendering viewport (in pixels).
|
||||
float GetViewportHeight() const;
|
||||
|
||||
//! Get the default values that would be used if no image options were passed in
|
||||
//
|
||||
//! This is a convenient way to initialize the imageOptions struct
|
||||
virtual const ImageOptions& GetDefaultImageOptions() const;
|
||||
|
||||
//! Get the default values that would be used if no text options were passed in
|
||||
//
|
||||
//! This is a convenient way to initialize the textOptions struct
|
||||
virtual const TextOptions& GetDefaultTextOptions() const;
|
||||
|
||||
//! Render the primitives that have been deferred
|
||||
void RenderDeferredPrimitives();
|
||||
|
||||
//! Specify whether to defer future primitives or render them right away
|
||||
void SetDeferPrimitives(bool deferPrimitives);
|
||||
|
||||
//! Return whether future primitives will be deferred or rendered right away
|
||||
bool GetDeferPrimitives();
|
||||
|
||||
private:
|
||||
|
||||
AZ_DISABLE_COPY_MOVE(CDraw2d);
|
||||
|
||||
// AZ::Render::Bootstrap::NotificationBus overrides
|
||||
void OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene) override;
|
||||
|
||||
public: // static member functions
|
||||
|
||||
//! Given a position and size and an alignment return the top left corner of the aligned quad
|
||||
static AZ::Vector2 Align(AZ::Vector2 position, AZ::Vector2 size, HAlign horizontalAlignment, VAlign verticalAlignment);
|
||||
|
||||
//! Helper to load a texture
|
||||
static AZ::Data::Instance<AZ::RPI::Image> LoadTexture(const AZStd::string& pathName);
|
||||
|
||||
protected: // types and constants
|
||||
|
||||
enum
|
||||
{
|
||||
MAX_VERTICES_IN_PRIM = 6
|
||||
};
|
||||
|
||||
// Cached shader data
|
||||
struct Draw2dShaderData
|
||||
{
|
||||
AZ::RHI::ShaderInputImageIndex m_imageInputIndex;
|
||||
AZ::RHI::ShaderInputConstantIndex m_viewProjInputIndex;
|
||||
};
|
||||
|
||||
class DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
virtual ~DeferredPrimitive() {};
|
||||
virtual void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
|
||||
const Draw2dShaderData& shaderData,
|
||||
AZ::RPI::ViewportContextPtr viewportContext) const = 0;
|
||||
};
|
||||
|
||||
class DeferredQuad
|
||||
: public DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
~DeferredQuad() override {};
|
||||
void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
|
||||
const Draw2dShaderData& shaderData,
|
||||
AZ::RPI::ViewportContextPtr viewportContext) const override;
|
||||
|
||||
AZ::Vector2 m_points[4];
|
||||
AZ::Vector2 m_texCoords[4];
|
||||
uint32 m_packedColors[4];
|
||||
AZ::Data::Instance<AZ::RPI::Image> m_image;
|
||||
int m_state;
|
||||
};
|
||||
|
||||
class DeferredLine
|
||||
: public DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
~DeferredLine() override {};
|
||||
void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
|
||||
const Draw2dShaderData& shaderData,
|
||||
AZ::RPI::ViewportContextPtr viewportContext) const override;
|
||||
|
||||
AZ::Data::Instance<AZ::RPI::Image> m_image;
|
||||
AZ::Vector2 m_points[2];
|
||||
AZ::Vector2 m_texCoords[2];
|
||||
uint32 m_packedColors[2];
|
||||
int m_state;
|
||||
};
|
||||
|
||||
class DeferredText
|
||||
: public DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
~DeferredText() override {};
|
||||
void Draw(AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> dynamicDraw,
|
||||
const Draw2dShaderData& shaderData,
|
||||
AZ::RPI::ViewportContextPtr viewportContext) const override;
|
||||
|
||||
STextDrawContext m_fontContext;
|
||||
IFFont* m_font;
|
||||
AZ::Vector2 m_position;
|
||||
std::string m_string;
|
||||
};
|
||||
|
||||
protected: // member functions
|
||||
|
||||
//! Rotate an array of points around the z-axis at the pivot point.
|
||||
//
|
||||
//! Angle is in degrees counter-clockwise
|
||||
void RotatePointsAboutPivot(AZ::Vector2* points, int numPoints, AZ::Vector2 pivot, float angle) const;
|
||||
|
||||
//! Helper function to render a text string
|
||||
void DrawTextInternal(const char* textString, IFFont* font, unsigned int effectIndex,
|
||||
AZ::Vector2 position, float pointSize, AZ::Color color, float rotation,
|
||||
HAlign horizontalAlignment, VAlign verticalAlignment, int baseState);
|
||||
|
||||
//! Draw or defer a quad
|
||||
void DrawOrDeferQuad(const DeferredQuad* quad);
|
||||
|
||||
//! Draw or defer a line
|
||||
void DrawOrDeferLine(const DeferredLine* line);
|
||||
|
||||
//! Get specified viewport context or default viewport context if not specified
|
||||
AZ::RPI::ViewportContextPtr GetViewportContext() const;
|
||||
|
||||
protected: // attributes
|
||||
|
||||
ImageOptions m_defaultImageOptions; //!< The default image options used if nullptr is passed
|
||||
TextOptions m_defaultTextOptions; //!< The default text options used if nullptr is passed
|
||||
|
||||
//! True if the actual render of the primitives should be deferred to a RenderDeferredPrimitives call
|
||||
bool m_deferCalls;
|
||||
|
||||
std::vector<DeferredPrimitive*> m_deferredPrimitives;
|
||||
|
||||
AZ::RPI::ViewportContextPtr m_viewportContext;
|
||||
AZ::RHI::Ptr<AZ::RPI::DynamicDrawContext> m_dynamicDraw;
|
||||
Draw2dShaderData m_shaderData;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Helper class for using the IDraw2d interface
|
||||
//!
|
||||
//! The Draw2dHelper class is an inline wrapper that provides the convenience feature of
|
||||
//! automatically setting member options structures to their defaults and providing set functions.
|
||||
class Draw2dHelper
|
||||
{
|
||||
public: // member functions
|
||||
|
||||
//! Start a section of 2D drawing function calls that will render to the default viewport
|
||||
Draw2dHelper(bool deferCalls = false)
|
||||
{
|
||||
InitCommon(nullptr, deferCalls);
|
||||
}
|
||||
|
||||
//! Start a section of 2D drawing function calls that will render to the viewport
|
||||
//! associated with the specified Draw2d object
|
||||
Draw2dHelper(CDraw2d* draw2d, bool deferCalls = false)
|
||||
{
|
||||
InitCommon(draw2d, deferCalls);
|
||||
}
|
||||
|
||||
void InitCommon(CDraw2d* draw2d, bool deferCalls)
|
||||
{
|
||||
m_draw2d = draw2d;
|
||||
|
||||
if (!m_draw2d)
|
||||
{
|
||||
// Set to default which is the game's draw 2d object
|
||||
m_draw2d = GetDefaultDraw2d();
|
||||
}
|
||||
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_previousDeferCalls = m_draw2d->GetDeferPrimitives();
|
||||
m_draw2d->SetDeferPrimitives(deferCalls);
|
||||
m_imageOptions = m_draw2d->GetDefaultImageOptions();
|
||||
m_textOptions = m_draw2d->GetDefaultTextOptions();
|
||||
}
|
||||
}
|
||||
|
||||
//! End a section of 2D drawing function calls.
|
||||
~Draw2dHelper()
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_draw2d->SetDeferPrimitives(m_previousDeferCalls);
|
||||
}
|
||||
}
|
||||
|
||||
//! Draw a textured quad, optional rotation is counter-clockwise in degrees.
|
||||
//
|
||||
//! See IDraw2d:DrawImage for parameter descriptions
|
||||
void DrawImage(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vector2 position, AZ::Vector2 size, float opacity = 1.0f,
|
||||
float rotation = 0.0f, const AZ::Vector2* pivotPoint = nullptr, const AZ::Vector2* minMaxTexCoords = nullptr)
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_draw2d->DrawImage(image, position, size, opacity, rotation, pivotPoint, minMaxTexCoords, &m_imageOptions);
|
||||
}
|
||||
}
|
||||
|
||||
//! Draw a textured quad where the position specifies the point specified by the alignment.
|
||||
//
|
||||
//! See IDraw2d:DrawImageAligned for parameter descriptions
|
||||
void DrawImageAligned(AZ::Data::Instance<AZ::RPI::Image> image, AZ::Vector2 position, AZ::Vector2 size,
|
||||
IDraw2d::HAlign horizontalAlignment, IDraw2d::VAlign verticalAlignment,
|
||||
float opacity = 1.0f, float rotation = 0.0f, const AZ::Vector2* minMaxTexCoords = nullptr)
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_draw2d->DrawImageAligned(image, position, size, horizontalAlignment, verticalAlignment,
|
||||
opacity, rotation, minMaxTexCoords, &m_imageOptions);
|
||||
}
|
||||
}
|
||||
|
||||
//! Draw a textured quad where the position, color and uv of each point is specified explicitly
|
||||
//
|
||||
//! See IDraw2d:DrawQuad for parameter descriptions
|
||||
void DrawQuad(AZ::Data::Instance<AZ::RPI::Image> image, IDraw2d::VertexPosColUV* verts, int blendMode = IDraw2d::UseDefault,
|
||||
IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
|
||||
int baseState = IDraw2d::UseDefault)
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_draw2d->DrawQuad(image, verts, blendMode, pixelRounding, baseState);
|
||||
}
|
||||
}
|
||||
|
||||
//! Draw a line
|
||||
//
|
||||
//! See IDraw2d:DrawLine for parameter descriptions
|
||||
void DrawLine(AZ::Vector2 start, AZ::Vector2 end, AZ::Color color, int blendMode = IDraw2d::UseDefault,
|
||||
IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
|
||||
int baseState = IDraw2d::UseDefault)
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_draw2d->DrawLine(start, end, color, blendMode, pixelRounding, baseState);
|
||||
}
|
||||
}
|
||||
|
||||
//! Draw a line with a texture so it can be dotted or dashed
|
||||
//
|
||||
//! See IDraw2d:DrawLineTextured for parameter descriptions
|
||||
void DrawLineTextured(AZ::Data::Instance<AZ::RPI::Image> image, IDraw2d::VertexPosColUV* verts, int blendMode = IDraw2d::UseDefault,
|
||||
IDraw2d::Rounding pixelRounding = IDraw2d::Rounding::Nearest,
|
||||
int baseState = IDraw2d::UseDefault)
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_draw2d->DrawLineTextured(image, verts, blendMode, pixelRounding, baseState);
|
||||
}
|
||||
}
|
||||
|
||||
//! Draw a text string. Only supports ASCII text.
|
||||
//
|
||||
//! See IDraw2d:DrawText for parameter descriptions
|
||||
void DrawText(const char* textString, AZ::Vector2 position, float pointSize, float opacity = 1.0f)
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
m_draw2d->DrawText(textString, position, pointSize, opacity, &m_textOptions);
|
||||
}
|
||||
}
|
||||
|
||||
//! Get the width and height (in pixels) that would be used to draw the given text string.
|
||||
//
|
||||
//! See IDraw2d:GetTextSize for parameter descriptions
|
||||
AZ::Vector2 GetTextSize(const char* textString, float pointSize)
|
||||
{
|
||||
if (m_draw2d)
|
||||
{
|
||||
return m_draw2d->GetTextSize(textString, pointSize, &m_textOptions);
|
||||
}
|
||||
else
|
||||
{
|
||||
return AZ::Vector2(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// State management
|
||||
|
||||
//! Set the blend mode used for images, default is GS_BLSRC_SRCALPHA|GS_BLDST_ONEMINUSSRCALPHA.
|
||||
void SetImageBlendMode(int mode) { m_imageOptions.blendMode = mode; }
|
||||
|
||||
//! Set the color used for DrawImage and other image drawing.
|
||||
void SetImageColor(AZ::Vector3 color) { m_imageOptions.color = color; }
|
||||
|
||||
//! Set whether images are rounded to have the points on exact pixel boundaries.
|
||||
void SetImagePixelRounding(IDraw2d::Rounding round) { m_imageOptions.pixelRounding = round; }
|
||||
|
||||
//! Set the base state (that blend mode etc is combined with) used for images, default is GS_NODEPTHTEST.
|
||||
void SetImageBaseState(int state) { m_imageOptions.baseState = state; }
|
||||
|
||||
//! Set the text font.
|
||||
void SetTextFont(IFFont* font) { m_textOptions.font = font; }
|
||||
|
||||
//! Set the text font effect index.
|
||||
void SetTextEffectIndex(unsigned int effectIndex) { m_textOptions.effectIndex = effectIndex; }
|
||||
|
||||
//! Set the text color.
|
||||
void SetTextColor(AZ::Vector3 color) { m_textOptions.color = color; }
|
||||
|
||||
//! Set the text alignment.
|
||||
void SetTextAlignment(IDraw2d::HAlign horizontalAlignment, IDraw2d::VAlign verticalAlignment)
|
||||
{
|
||||
m_textOptions.horizontalAlignment = horizontalAlignment;
|
||||
m_textOptions.verticalAlignment = verticalAlignment;
|
||||
}
|
||||
|
||||
//! Set a drop shadow for text drawing. An alpha of zero disables drop shadow.
|
||||
void SetTextDropShadow(AZ::Vector2 offset, AZ::Color color)
|
||||
{
|
||||
m_textOptions.dropShadowOffset = offset;
|
||||
m_textOptions.dropShadowColor = color;
|
||||
}
|
||||
|
||||
//! Set a rotation for the text. The text rotates around its position (taking into account alignment).
|
||||
void SetTextRotation(float rotation)
|
||||
{
|
||||
m_textOptions.rotation = rotation;
|
||||
}
|
||||
|
||||
//! Set the base state (that blend mode etc is combined with) used for text, default is GS_NODEPTHTEST.
|
||||
void SetTextBaseState(int state) { m_textOptions.baseState = state; }
|
||||
|
||||
public: // static member functions
|
||||
|
||||
//! Helper to get the default IDraw2d interface
|
||||
static CDraw2d* GetDefaultDraw2d()
|
||||
{
|
||||
if (gEnv && gEnv->pLyShine) // LYSHINE_ATOM_TODO - remove pLyShine and use bus interface
|
||||
{
|
||||
IDraw2d* draw2d = gEnv->pLyShine->GetDraw2d();
|
||||
return reinterpret_cast<CDraw2d*>(draw2d);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//! Round the X and Y coordinates of a point using the given rounding policy
|
||||
template<typename T>
|
||||
static T RoundXY(T value, IDraw2d::Rounding roundingType)
|
||||
{
|
||||
T result = value;
|
||||
|
||||
switch (roundingType)
|
||||
{
|
||||
case IDraw2d::Rounding::None:
|
||||
// nothing to do
|
||||
break;
|
||||
case IDraw2d::Rounding::Nearest:
|
||||
result.SetX(floor(value.GetX() + 0.5f));
|
||||
result.SetY(floor(value.GetY() + 0.5f));
|
||||
break;
|
||||
case IDraw2d::Rounding::Down:
|
||||
result.SetX(floor(value.GetX()));
|
||||
result.SetY(floor(value.GetY()));
|
||||
break;
|
||||
case IDraw2d::Rounding::Up:
|
||||
result.SetX(ceil(value.GetX()));
|
||||
result.SetY(ceil(value.GetY()));
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected: // attributes
|
||||
|
||||
IDraw2d::ImageOptions m_imageOptions; //!< image options are stored locally and updated by member functions
|
||||
IDraw2d::TextOptions m_textOptions; //!< text options are stored locally and updated by member functions
|
||||
CDraw2d* m_draw2d;
|
||||
bool m_previousDeferCalls;
|
||||
};
|
||||
@ -1,189 +0,0 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <LyShine/IDraw2d.h>
|
||||
#include <IRenderer.h>
|
||||
#include <stack>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//! Implementation of IDraw2d interface for 2D drawing in screen space
|
||||
//
|
||||
//! The CDraw2d class implements the IDraw2d interface for drawing 2D images, shapes and text.
|
||||
//! Positions and sizes are specified in pixels in the current 2D viewport.
|
||||
class CDraw2d
|
||||
: public IDraw2d
|
||||
{
|
||||
public: // member functions
|
||||
|
||||
//! Constructor, constructed by the LyShine class
|
||||
CDraw2d();
|
||||
|
||||
// IDraw2d
|
||||
|
||||
~CDraw2d() override;
|
||||
|
||||
//! Start a section of 2D drawing function calls. This will set appropriate render state.
|
||||
void BeginDraw2d(bool deferCalls = false) override;
|
||||
|
||||
//! Start a section of 2D drawing function calls. This will set appropriate render state.
|
||||
//! This variant allows the viewport size to be specified
|
||||
void BeginDraw2d(AZ::Vector2 viewportSize, bool deferCalls = false) override;
|
||||
|
||||
//! End a section of 2D drawing function calls. This will reset some render state.
|
||||
void EndDraw2d() override;
|
||||
|
||||
//! Draw a textured quad, optional rotation is counter-clockwise in degrees.
|
||||
void DrawImage(int texId, AZ::Vector2 position, AZ::Vector2 size, float opacity = 1.0f,
|
||||
float rotation = 0.0f, const AZ::Vector2* pivotPoint = nullptr, const AZ::Vector2* minMaxTexCoords = nullptr,
|
||||
ImageOptions* imageOptions = nullptr) override;
|
||||
|
||||
//! Draw a textured quad where the position specifies the point specified by the alignment. Rotation is around that point.
|
||||
void DrawImageAligned(int texId, AZ::Vector2 position, AZ::Vector2 size,
|
||||
HAlign horizontalAlignment, VAlign verticalAlignment,
|
||||
float opacity = 1.0f, float rotation = 0.0f, const AZ::Vector2* minMaxTexCoords = nullptr,
|
||||
ImageOptions* imageOptions = nullptr) override;
|
||||
|
||||
//! Draw a textured quad where the position, color and uv of each point is specified explicitly
|
||||
void DrawQuad(int texId, VertexPosColUV* verts, int blendMode, Rounding pixelRounding, int baseState) override;
|
||||
|
||||
//! Draw a line
|
||||
void DrawLine(AZ::Vector2 start, AZ::Vector2 end, AZ::Color color, int blendMode, Rounding pixelRounding, int baseState) override;
|
||||
|
||||
//! Draw a line textured
|
||||
void DrawLineTextured(int texId, VertexPosColUV* verts, int blendMode, Rounding pixelRounding, int baseState) override;
|
||||
|
||||
//! Draw a text string. Only supports ASCII text.
|
||||
void DrawText(const char* textString, AZ::Vector2 position, float pointSize,
|
||||
float opacity = 1.0f, TextOptions* textOptions = nullptr) override;
|
||||
|
||||
//! Get the width and height (in pixels) that would be used to draw the given text string.
|
||||
AZ::Vector2 GetTextSize(const char* textString, float pointSize, TextOptions* textOptions = nullptr) override;
|
||||
|
||||
//! Get the width of the rendering viewport (in pixels).
|
||||
float GetViewportWidth() const override;
|
||||
|
||||
//! Get the height of the rendering viewport (in pixels).
|
||||
float GetViewportHeight() const override;
|
||||
|
||||
//! Get the default values that would be used if no image options were passed in
|
||||
const ImageOptions& GetDefaultImageOptions() const override;
|
||||
|
||||
//! Get the default values that would be used if no text options were passed in
|
||||
const TextOptions& GetDefaultTextOptions() const override;
|
||||
|
||||
// ~IDraw2d
|
||||
|
||||
//! Render the primitives that have been deferred
|
||||
void RenderDeferredPrimitives();
|
||||
|
||||
private:
|
||||
|
||||
AZ_DISABLE_COPY_MOVE(CDraw2d);
|
||||
|
||||
public: // static member functions
|
||||
|
||||
//! Given a position and size and an alignment return the top left corner of the aligned quad
|
||||
static AZ::Vector2 Align(AZ::Vector2 position, AZ::Vector2 size, HAlign horizontalAlignment, VAlign verticalAlignment);
|
||||
|
||||
protected: // types and constants
|
||||
|
||||
enum
|
||||
{
|
||||
MAX_VERTICES_IN_PRIM = 6
|
||||
};
|
||||
|
||||
class DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
virtual ~DeferredPrimitive() {};
|
||||
virtual void Draw() const = 0;
|
||||
};
|
||||
|
||||
class DeferredQuad
|
||||
: public DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
~DeferredQuad() override {};
|
||||
void Draw() const override;
|
||||
|
||||
AZ::Vector2 m_points[4];
|
||||
AZ::Vector2 m_texCoords[4];
|
||||
uint32 m_packedColors[4];
|
||||
int m_texId;
|
||||
int m_state;
|
||||
};
|
||||
|
||||
class DeferredLine
|
||||
: public DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
~DeferredLine() override {};
|
||||
void Draw() const override;
|
||||
|
||||
int m_texId;
|
||||
AZ::Vector2 m_points[2];
|
||||
AZ::Vector2 m_texCoords[2];
|
||||
uint32 m_packedColors[2];
|
||||
int m_state;
|
||||
};
|
||||
|
||||
class DeferredText
|
||||
: public DeferredPrimitive
|
||||
{
|
||||
public:
|
||||
~DeferredText() override {};
|
||||
void Draw() const override;
|
||||
|
||||
STextDrawContext m_fontContext;
|
||||
IFFont* m_font;
|
||||
AZ::Vector2 m_position;
|
||||
std::string m_string;
|
||||
};
|
||||
|
||||
protected: // member functions
|
||||
|
||||
//! Rotate an array of points around the z-axis at the pivot point.
|
||||
//
|
||||
//! Angle is in degrees counter-clockwise
|
||||
void RotatePointsAboutPivot(AZ::Vector2* points, int numPoints, AZ::Vector2 pivot, float angle) const;
|
||||
|
||||
//! Helper function to render a text string
|
||||
void DrawTextInternal(const char* textString, IFFont* font, unsigned int effectIndex,
|
||||
AZ::Vector2 position, float pointSize, AZ::Color color, float rotation,
|
||||
HAlign horizontalAlignment, VAlign verticalAlignment, int baseState);
|
||||
|
||||
//! Draw or defer a quad
|
||||
void DrawOrDeferQuad(const DeferredQuad* quad);
|
||||
|
||||
//! Draw or defer a line
|
||||
void DrawOrDeferLine(const DeferredLine* line);
|
||||
|
||||
protected: // attributes
|
||||
|
||||
ImageOptions m_defaultImageOptions; //!< The default image options used if nullptr is passed
|
||||
TextOptions m_defaultTextOptions; //!< The default text options used if nullptr is passed
|
||||
|
||||
bool m_deferCalls; //!< True if the actual render of the primitives should be deferred until end of frame
|
||||
|
||||
std::vector<DeferredPrimitive*> m_deferredPrimitives;
|
||||
|
||||
//! These two data members allows nested calls to BeginDraw2d/EndDraw2d. We will begin 2D mode only on the
|
||||
//! outermost call to BeginDraw2d with deferCalls set to false and will end 2D mode on the corresposnding
|
||||
//! call to EndDraw2d. The stack is used to detect that corresponding call and we need the level it occurred
|
||||
//! to know when to end 2D mode.
|
||||
int m_nestLevelAtWhichStarted2dMode;
|
||||
std::stack<bool> m_deferCallsFlagStack;
|
||||
|
||||
private:
|
||||
TransformationMatrices m_backupSceneMatrices;
|
||||
};
|
||||
Loading…
Reference in New Issue