Add ViewportRect to UICanvas for UI placement

This commit is contained in:
2026-07-24 18:35:49 +02:00
parent 8f53373ad2
commit 14ffa300cc
3 changed files with 57 additions and 0 deletions
@@ -197,6 +197,14 @@ namespace FlaxEngine.GUI
&& (_canvas.TestCanvasIntersection == null || _canvas.TestCanvasIntersection(ref location));
}
/// <inheritdoc />
public override void GetDesireClientArea(out Rectangle rect)
{
var size = Size;
var viewportRect = _canvas.ViewportRect;
rect = new Rectangle(size * new Float2(viewportRect.X, viewportRect.Y), size * new Float2(viewportRect.Z, viewportRect.W));
}
/// <inheritdoc />
public override void Update(float deltaTime)
{
+17
View File
@@ -708,5 +708,22 @@ namespace FlaxEngine.GUI
}
}
}
/// <summary>
/// Sets the anchors for the control.
/// </summary>
/// <param name="anchorMin">The minimum anchors point to use.</param>
/// <param name="anchorMax">The maximum anchors point to use.</param>
/// <param name="preserveBounds">True if preserve current control bounds, otherwise will align control position accordingly to the anchor location.</param>
public void SetAnchors(Float2 anchorMin, Float2 anchorMax, bool preserveBounds)
{
var bounds = _bounds;
_anchorMin = anchorMin;
_anchorMax = anchorMax;
if (preserveBounds)
Bounds = bounds;
else
UpdateBounds();
}
}
}
+32
View File
@@ -1,6 +1,7 @@
// Copyright (c) Wojciech Figat. All rights reserved.
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Text;
@@ -114,6 +115,7 @@ namespace FlaxEngine
private readonly CanvasRootControl _guiRoot;
private CanvasRenderer _renderer;
private bool _isLoading, _isRegisteredForTick, _isRegisteredForOnDraw;
private Float4 _viewportRect = new Float4(0, 0, 1, 1);
/// <summary>
/// Gets or sets the canvas rendering mode.
@@ -222,6 +224,21 @@ namespace FlaxEngine
[EditorOrder(70), NoSerialize, EditorDisplay("Canvas"), VisibleIf("Editor_IsGPUTexture")]
public GPUTexture OutputTexture { get; set; }
/// <summary>
/// Four values that define where on the screen this canvas should be drawn. Measured in normalized coordinates (range 0-1). Order: X, Y, Width, Height. For example (0, 0, 1, 1) means full screen, (0, 0, 0.5, 1) means left half of the screen and (0.5, 0, 0.5, 1) means right half of the screen.
/// </summary>
[EditorOrder(80), EditorDisplay("Canvas"), Limit(0, 1, 0.001f)]
[DefaultValue(typeof(Float4), "0,0,1,1")]
public Float4 ViewportRect
{
get => _viewportRect;
set
{
_viewportRect = value;
_guiRoot.PerformLayout();
}
}
/// <summary>
/// Gets the canvas GUI root control.
/// </summary>
@@ -688,6 +705,21 @@ namespace FlaxEngine
jsonWriter.WriteValue(Distance);
}
if (noOther || _viewportRect != other._viewportRect)
{
jsonWriter.WritePropertyName("ViewportRect");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("X");
jsonWriter.WriteValue(_viewportRect.X);
jsonWriter.WritePropertyName("Y");
jsonWriter.WriteValue(_viewportRect.Y);
jsonWriter.WritePropertyName("Z");
jsonWriter.WriteValue(_viewportRect.Z);
jsonWriter.WritePropertyName("W");
jsonWriter.WriteValue(_viewportRect.W);
jsonWriter.WriteEndObject();
}
bool saveSize = RenderMode == CanvasRenderMode.WorldSpace || RenderMode == CanvasRenderMode.WorldSpaceFaceCamera;
if (!noOther)
saveSize = (saveSize || other.RenderMode == CanvasRenderMode.WorldSpace || other.RenderMode == CanvasRenderMode.WorldSpaceFaceCamera) && Size != other.Size;