mirror of
https://github.com/kevinporetti/UnrealImGui.git
synced 2025-01-18 08:20:32 +00:00
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:
parent
2ccd3252ec
commit
f8e689561f
@ -6,9 +6,9 @@
|
||||
|
||||
|
||||
#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
|
||||
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
|
||||
{
|
||||
// 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[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
|
||||
// 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;
|
||||
#else
|
||||
SlateVertex.Position = Transform.TransformPoint(ImGuiInterops::ToVector2D(ImGuiVertex.pos));
|
||||
#endif // WITH_OBSOLETE_CLIPPING_API
|
||||
|
||||
// Unpack ImU32 color.
|
||||
|
@ -32,24 +32,26 @@ public:
|
||||
|
||||
// Get the draw command by number.
|
||||
// @param CommandNb - Number of draw command
|
||||
// @param Transform - Transform to apply to clipping rectangle
|
||||
// @returns Draw command data
|
||||
FORCEINLINE FImGuiDrawCommand GetCommand(int CommandNb) const
|
||||
FImGuiDrawCommand GetCommand(int CommandNb, const FTransform2D& Transform) const
|
||||
{
|
||||
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
|
||||
// Transform and copy vertex data to target buffer (old data in the target buffer are replaced).
|
||||
// @param OutVertexBuffer - Destination buffer
|
||||
// @param VertexPositionOffset - Position offset added to every vertex to transform it to different space
|
||||
// @param VertexClippingRect - Clipping rectangle for Slate vertices
|
||||
void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FVector2D VertexPositionOffset, const FSlateRotatedRect& VertexClippingRect) const;
|
||||
// @param Transform - Transform to apply to all vertices
|
||||
// @param VertexClippingRect - Clipping rectangle for transformed Slate vertices
|
||||
void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FTransform2D& Transform, const FSlateRotatedRect& VertexClippingRect) const;
|
||||
#else
|
||||
// Transform and copy vertex data to target buffer (old data in the target buffer are replaced).
|
||||
// @param OutVertexBuffer - Destination buffer
|
||||
// @param VertexPositionOffset - Position offset added to every vertex to transform it to different space
|
||||
void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FVector2D VertexPositionOffset) const;
|
||||
// @param Transform - Transform to apply to all vertices
|
||||
void CopyVertexData(TArray<FSlateVertex>& OutVertexBuffer, const FTransform2D& Transform) const;
|
||||
#endif // WITH_OBSOLETE_CLIPPING_API
|
||||
|
||||
// Transform and copy index data to target buffer (old data in the target buffer are replaced).
|
||||
|
@ -85,6 +85,12 @@ namespace ImGuiInterops
|
||||
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.
|
||||
FORCEINLINE TextureIndex ToTextureIndex(ImTextureID Index)
|
||||
{
|
||||
|
@ -443,22 +443,25 @@ int32 SImGuiWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeo
|
||||
// Convert clipping rectangle to format required by Slate vertex.
|
||||
const FSlateRotatedRect VertexClippingRect{ MyClippingRect };
|
||||
|
||||
// Scale -> CanvasOffset in Screen Space
|
||||
const FTransform2D Transform{ VertexPositionOffset };
|
||||
|
||||
for (const auto& DrawList : ContextProxy->GetDrawData())
|
||||
{
|
||||
#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.
|
||||
extern SLATECORE_API TOptional<FShortRect> GSlateScissorRect;
|
||||
auto GSlateScissorRectSaver = ScopeGuards::MakeStateSaver(GSlateScissorRect);
|
||||
#else
|
||||
DrawList.CopyVertexData(VertexBuffer, VertexPositionOffset);
|
||||
DrawList.CopyVertexData(VertexBuffer, Transform);
|
||||
#endif // WITH_OBSOLETE_CLIPPING_API
|
||||
|
||||
int IndexBufferOffset = 0;
|
||||
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);
|
||||
|
||||
@ -469,7 +472,7 @@ int32 SImGuiWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeo
|
||||
const FSlateResourceHandle& Handle = ModuleManager->GetTextureManager().GetTextureHandle(DrawCommand.TextureId);
|
||||
|
||||
// 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
|
||||
GSlateScissorRect = FShortRect{ ClippingRect };
|
||||
@ -534,7 +537,7 @@ static TArray<FKey> GetImGuiMappedKeys()
|
||||
return Keys;
|
||||
}
|
||||
|
||||
// Column layout unitlities.
|
||||
// Column layout utilities.
|
||||
namespace Columns
|
||||
{
|
||||
template<typename FunctorType>
|
||||
|
Loading…
Reference in New Issue
Block a user