Switched to FTransform2D when processing ImGui output before rendering it in Slate. This allows for more flexibility when transforming that data. Changed interface of FImGuiDrawList to enforce consistent transformation of vertices and clipping rectangle.

This commit is contained in:
Sebastian 2018-04-17 20:28:29 +01:00
parent 2ccd3252ec
commit f8e689561f
4 changed files with 30 additions and 19 deletions

View File

@ -6,9 +6,9 @@
#if WITH_OBSOLETE_CLIPPING_API #if WITH_OBSOLETE_CLIPPING_API
void FImGuiDrawList::CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FVector2D VertexPositionOffset, const FSlateRotatedRect& VertexClippingRect) const void FImGuiDrawList::CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FTransform2D& Transform, const FSlateRotatedRect& VertexClippingRect) const
#else #else
void FImGuiDrawList::CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FVector2D VertexPositionOffset) const void FImGuiDrawList::CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FTransform2D& Transform) const
#endif // WITH_OBSOLETE_CLIPPING_API #endif // WITH_OBSOLETE_CLIPPING_API
{ {
// Reset and reserve space in destination buffer. // Reset and reserve space in destination buffer.
@ -25,13 +25,13 @@ void FImGuiDrawList::CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const
SlateVertex.TexCoords[1] = ImGuiVertex.uv.y; SlateVertex.TexCoords[1] = ImGuiVertex.uv.y;
SlateVertex.TexCoords[2] = SlateVertex.TexCoords[3] = 1.f; SlateVertex.TexCoords[2] = SlateVertex.TexCoords[3] = 1.f;
// Copy ImGui position and add offset.
SlateVertex.Position[0] = ImGuiVertex.pos.x + VertexPositionOffset.X;
SlateVertex.Position[1] = ImGuiVertex.pos.y + VertexPositionOffset.Y;
#if WITH_OBSOLETE_CLIPPING_API #if WITH_OBSOLETE_CLIPPING_API
// Set clipping rectangle. const FVector2D VertexPosition = Transform.TransformPoint(ImGuiInterops::ToVector2D(ImGuiVertex.pos));
SlateVertex.Position[0] = VertexPosition.X;
SlateVertex.Position[1] = VertexPosition.Y;
SlateVertex.ClipRect = VertexClippingRect; SlateVertex.ClipRect = VertexClippingRect;
#else
SlateVertex.Position = Transform.TransformPoint(ImGuiInterops::ToVector2D(ImGuiVertex.pos));
#endif // WITH_OBSOLETE_CLIPPING_API #endif // WITH_OBSOLETE_CLIPPING_API
// Unpack ImU32 color. // Unpack ImU32 color.

View File

@ -32,24 +32,26 @@ public:
// Get the draw command by number. // Get the draw command by number.
// @param CommandNb - Number of draw command // @param CommandNb - Number of draw command
// @param Transform - Transform to apply to clipping rectangle
// @returns Draw command data // @returns Draw command data
FORCEINLINE FImGuiDrawCommand GetCommand(int CommandNb) const FImGuiDrawCommand GetCommand(int CommandNb, const FTransform2D& Transform) const
{ {
const ImDrawCmd& ImGuiCommand = ImGuiCommandBuffer[CommandNb]; const ImDrawCmd& ImGuiCommand = ImGuiCommandBuffer[CommandNb];
return{ ImGuiCommand.ElemCount, ImGuiInterops::ToSlateRect(ImGuiCommand.ClipRect), ImGuiInterops::ToTextureIndex(ImGuiCommand.TextureId) }; return { ImGuiCommand.ElemCount, TransformRect(Transform, ImGuiInterops::ToSlateRect(ImGuiCommand.ClipRect)),
ImGuiInterops::ToTextureIndex(ImGuiCommand.TextureId) };
} }
#if WITH_OBSOLETE_CLIPPING_API #if WITH_OBSOLETE_CLIPPING_API
// Transform and copy vertex data to target buffer (old data in the target buffer are replaced). // Transform and copy vertex data to target buffer (old data in the target buffer are replaced).
// @param OutVertexBuffer - Destination buffer // @param OutVertexBuffer - Destination buffer
// @param VertexPositionOffset - Position offset added to every vertex to transform it to different space // @param Transform - Transform to apply to all vertices
// @param VertexClippingRect - Clipping rectangle for Slate vertices // @param VertexClippingRect - Clipping rectangle for transformed Slate vertices
void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FVector2D VertexPositionOffset, const FSlateRotatedRect& VertexClippingRect) const; void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FTransform2D& Transform, const FSlateRotatedRect& VertexClippingRect) const;
#else #else
// Transform and copy vertex data to target buffer (old data in the target buffer are replaced). // Transform and copy vertex data to target buffer (old data in the target buffer are replaced).
// @param OutVertexBuffer - Destination buffer // @param OutVertexBuffer - Destination buffer
// @param VertexPositionOffset - Position offset added to every vertex to transform it to different space // @param Transform - Transform to apply to all vertices
void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FVector2D VertexPositionOffset) const; void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FTransform2D& Transform) const;
#endif // WITH_OBSOLETE_CLIPPING_API #endif // WITH_OBSOLETE_CLIPPING_API
// Transform and copy index data to target buffer (old data in the target buffer are replaced). // Transform and copy index data to target buffer (old data in the target buffer are replaced).

View File

@ -85,6 +85,12 @@ namespace ImGuiInterops
return FSlateRect{ ImGuiRect.x, ImGuiRect.y, ImGuiRect.z, ImGuiRect.w }; return FSlateRect{ ImGuiRect.x, ImGuiRect.y, ImGuiRect.z, ImGuiRect.w };
} }
// Convert from ImVec2 rectangle to FVector2D.
FORCEINLINE FVector2D ToVector2D(const ImVec2& ImGuiVector)
{
return FVector2D{ ImGuiVector.x, ImGuiVector.y };
}
// Convert from ImGui Texture Id to Texture Index that we use for texture resources. // Convert from ImGui Texture Id to Texture Index that we use for texture resources.
FORCEINLINE TextureIndex ToTextureIndex(ImTextureID Index) FORCEINLINE TextureIndex ToTextureIndex(ImTextureID Index)
{ {

View File

@ -443,22 +443,25 @@ int32 SImGuiWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeo
// Convert clipping rectangle to format required by Slate vertex. // Convert clipping rectangle to format required by Slate vertex.
const FSlateRotatedRect VertexClippingRect{ MyClippingRect }; const FSlateRotatedRect VertexClippingRect{ MyClippingRect };
// Scale -> CanvasOffset in Screen Space
const FTransform2D Transform{ VertexPositionOffset };
for (const auto& DrawList : ContextProxy->GetDrawData()) for (const auto& DrawList : ContextProxy->GetDrawData())
{ {
#if WITH_OBSOLETE_CLIPPING_API #if WITH_OBSOLETE_CLIPPING_API
DrawList.CopyVertexData(VertexBuffer, VertexPositionOffset, VertexClippingRect); DrawList.CopyVertexData(VertexBuffer, Transform, VertexClippingRect);
// Get access to the Slate scissor rectangle defined in Slate Core API, so we can customize elements drawing. // Get access to the Slate scissor rectangle defined in Slate Core API, so we can customize elements drawing.
extern SLATECORE_API TOptional<FShortRect> GSlateScissorRect; extern SLATECORE_API TOptional<FShortRect> GSlateScissorRect;
auto GSlateScissorRectSaver = ScopeGuards::MakeStateSaver(GSlateScissorRect); auto GSlateScissorRectSaver = ScopeGuards::MakeStateSaver(GSlateScissorRect);
#else #else
DrawList.CopyVertexData(VertexBuffer, VertexPositionOffset); DrawList.CopyVertexData(VertexBuffer, Transform);
#endif // WITH_OBSOLETE_CLIPPING_API #endif // WITH_OBSOLETE_CLIPPING_API
int IndexBufferOffset = 0; int IndexBufferOffset = 0;
for (int CommandNb = 0; CommandNb < DrawList.NumCommands(); CommandNb++) for (int CommandNb = 0; CommandNb < DrawList.NumCommands(); CommandNb++)
{ {
const auto& DrawCommand = DrawList.GetCommand(CommandNb); const auto& DrawCommand = DrawList.GetCommand(CommandNb, Transform);
DrawList.CopyIndexData(IndexBuffer, IndexBufferOffset, DrawCommand.NumElements); DrawList.CopyIndexData(IndexBuffer, IndexBufferOffset, DrawCommand.NumElements);
@ -469,7 +472,7 @@ int32 SImGuiWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeo
const FSlateResourceHandle& Handle = ModuleManager->GetTextureManager().GetTextureHandle(DrawCommand.TextureId); const FSlateResourceHandle& Handle = ModuleManager->GetTextureManager().GetTextureHandle(DrawCommand.TextureId);
// Transform clipping rectangle to screen space and apply to elements that we draw. // Transform clipping rectangle to screen space and apply to elements that we draw.
const FSlateRect ClippingRect = DrawCommand.ClippingRect.OffsetBy(MyClippingRect.GetTopLeft()).IntersectionWith(MyClippingRect); const FSlateRect ClippingRect = DrawCommand.ClippingRect.IntersectionWith(MyClippingRect);
#if WITH_OBSOLETE_CLIPPING_API #if WITH_OBSOLETE_CLIPPING_API
GSlateScissorRect = FShortRect{ ClippingRect }; GSlateScissorRect = FShortRect{ ClippingRect };
@ -534,7 +537,7 @@ static TArray<FKey> GetImGuiMappedKeys()
return Keys; return Keys;
} }
// Column layout unitlities. // Column layout utilities.
namespace Columns namespace Columns
{ {
template<typename FunctorType> template<typename FunctorType>