Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hss/Vss force anchor setup #484

Closed
wants to merge 12 commits into from
Closed
3 changes: 3 additions & 0 deletions Runtime/Scripts/Controls/ReorderableList/ReorderableList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class ReorderableList : MonoBehaviour
[Tooltip("Should items being dragged over this list have their sizes equalized?")]
public bool EqualizeSizesOnDrag = false;

[Tooltip("Should items keep the original rotation?")]
public bool KeepItemRotation = false;

[Tooltip("Maximum number of items this container can hold")]
public int maxItems = int.MaxValue;

Expand Down
20 changes: 16 additions & 4 deletions Runtime/Scripts/Controls/ReorderableList/ReorderableListElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ private void displaceElement(int targetIndex, Transform displaced)
_displacedObjectLE.preferredWidth = _draggingObjectOriginalSize.x;
_displacedObjectLE.preferredHeight = _draggingObjectOriginalSize.y;
_displacedObject.SetParent(_reorderableList.Content, false);
_displacedObject.rotation = _reorderableList.transform.rotation;
if (!_reorderableList.KeepItemRotation)
{
_displacedObject.rotation = _reorderableList.transform.rotation;
}
_displacedObject.SetSiblingIndex(_fromIndex);
// Force refreshing both lists because otherwise we get inappropriate FromList in ReorderableListEventStruct
_reorderableList.Refresh();
Expand Down Expand Up @@ -310,7 +313,10 @@ private void revertDisplacedElement()
_displacedObjectLE.preferredWidth = _displacedObjectOriginalSize.x;
_displacedObjectLE.preferredHeight = _displacedObjectOriginalSize.y;
_displacedObject.SetParent(_displacedObjectOriginList.Content, false);
_displacedObject.rotation = _displacedObjectOriginList.transform.rotation;
if (!_reorderableList.KeepItemRotation)
{
_displacedObject.rotation = _displacedObjectOriginList.transform.rotation;
}
_displacedObject.SetSiblingIndex(_displacedFromIndex);
_displacedObject.gameObject.SetActive(true);

Expand Down Expand Up @@ -382,7 +388,10 @@ public void OnEndDrag(PointerEventData eventData)

RefreshSizes();
_draggingObject.SetParent(_currentReorderableListRaycasted.Content, false);
_draggingObject.rotation = _currentReorderableListRaycasted.transform.rotation;
if (!_reorderableList.KeepItemRotation)
{
_draggingObject.rotation = _currentReorderableListRaycasted.transform.rotation;
}
_draggingObject.SetSiblingIndex(_fakeElement.GetSiblingIndex());

//If the item is transferable, it can be dragged out again
Expand Down Expand Up @@ -474,7 +483,10 @@ private void CancelDrag()
{
RefreshSizes();
_draggingObject.SetParent(_reorderableList.Content, false);
_draggingObject.rotation = _reorderableList.Content.transform.rotation;
if (!_reorderableList.KeepItemRotation)
{
_draggingObject.rotation = _reorderableList.Content.transform.rotation;
}
_draggingObject.SetSiblingIndex(_fromIndex);


Expand Down
78 changes: 66 additions & 12 deletions Runtime/Scripts/Effects/Gradient2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
/// </summary>
using System;
using System.Collections.Generic;
#if UNITY_2021_2_OR_NEWER
using System.Buffers;
#endif
#if UNITY_2021_1_OR_NEWER
using UnityEngine.Pool;
#endif

namespace UnityEngine.UI.Extensions
{
Expand Down Expand Up @@ -36,6 +42,9 @@ public class Gradient2 : BaseMeshEffect
[SerializeField]
UnityEngine.Gradient _effectGradient = new UnityEngine.Gradient() { colorKeys = new GradientColorKey[] { new GradientColorKey(Color.black, 0), new GradientColorKey(Color.white, 1) } };

private GradientColorKey[] _colorKeys;
private GradientAlphaKey[] _alphaKeys;

#region Properties
public Blend BlendMode
{
Expand Down Expand Up @@ -103,7 +112,11 @@ public override void ModifyMesh(VertexHelper helper)
if (!IsActive() || helper.currentVertCount == 0)
return;

#if UNITY_2021_1_OR_NEWER
List<UIVertex> _vertexList = ListPool<UIVertex>.Get();
#else
List<UIVertex> _vertexList = new List<UIVertex>();
#endif

helper.GetUIVertexStream(_vertexList);

Expand Down Expand Up @@ -218,7 +231,7 @@ public override void ModifyMesh(VertexHelper helper)

helper.AddVert(centralVertex);

for (int i = 1; i < steps; i++) helper.AddTriangle(i - 1, i, steps);
for (int i = 1; i < steps; i++) helper.AddTriangle(i, i-1, steps);
helper.AddTriangle(0, steps - 1, steps);
}

Expand All @@ -238,6 +251,10 @@ public override void ModifyMesh(VertexHelper helper)
}
break;
}

#if UNITY_2021_1_OR_NEWER
ListPool<UIVertex>.Release(_vertexList);
#endif
}

Rect GetBounds(List<UIVertex> vertices)
Expand All @@ -264,18 +281,35 @@ Rect GetBounds(List<UIVertex> vertices)

void SplitTrianglesAtGradientStops(List<UIVertex> _vertexList, Rect bounds, float zoomOffset, VertexHelper helper)
{
List<float> stops = FindStops(zoomOffset, bounds);
#if UNITY_2021_1_OR_NEWER
List<float> stops = FindStops(zoomOffset, bounds, ListPool<float>.Get());
#else
List<float> stops = FindStops(zoomOffset, bounds, new List<float>());
#endif
if (stops.Count > 0)
{
helper.Clear();

int nCount = _vertexList.Count;
for (int i = 0; i < nCount; i += 3)
{
float[] positions = GetPositions(_vertexList, i);
#if UNITY_2021_2_OR_NEWER
var positions = ArrayPool<float>.Shared.Rent(3);
#else
var positions = new float[3];
#endif

GetPositions(_vertexList, i, ref positions);

#if UNITY_2021_1_OR_NEWER
List<int> originIndices = ListPool<int>.Get();
List<UIVertex> starts = ListPool<UIVertex>.Get();
List<UIVertex> ends = ListPool<UIVertex>.Get();
#else
List<int> originIndices = new List<int>(3);
List<UIVertex> starts = new List<UIVertex>(3);
List<UIVertex> ends = new List<UIVertex>(2);
#endif

for (int s = 0; s < stops.Count; s++)
{
Expand Down Expand Up @@ -403,13 +437,24 @@ void SplitTrianglesAtGradientStops(List<UIVertex> _vertexList, Rect bounds, floa
int vertexCount = helper.currentVertCount;
helper.AddTriangle(vertexCount - 3, vertexCount - 2, vertexCount - 1);
}

#if UNITY_2021_2_OR_NEWER
ArrayPool<float>.Shared.Return(positions);
#endif
#if UNITY_2021_1_OR_NEWER
ListPool<int>.Release(originIndices);
ListPool<UIVertex>.Release(starts);
ListPool<UIVertex>.Release(ends);
#endif
}
}
#if UNITY_2021_1_OR_NEWER
ListPool<float>.Release(stops);
#endif
}

float[] GetPositions(List<UIVertex> _vertexList, int index)
void GetPositions(List<UIVertex> _vertexList, int index, ref float[] positions)
{
float[] positions = new float[3];
if (GradientType == Type.Horizontal)
{
positions[0] = _vertexList[index].position.x;
Expand All @@ -422,24 +467,27 @@ float[] GetPositions(List<UIVertex> _vertexList, int index)
positions[1] = _vertexList[index + 1].position.y;
positions[2] = _vertexList[index + 2].position.y;
}
return positions;
}

List<float> FindStops(float zoomOffset, Rect bounds)
List<float> FindStops(float zoomOffset, Rect bounds, List<float> stops)
{
List<float> stops = new List<float>();
var offset = Offset * (1 - zoomOffset);
var startBoundary = zoomOffset - offset;
var endBoundary = (1 - zoomOffset) - offset;

foreach (var color in EffectGradient.colorKeys)
if (_colorKeys == null) _colorKeys = EffectGradient.colorKeys;

foreach (var color in _colorKeys)
{
if (color.time >= endBoundary)
break;
if (color.time > startBoundary)
stops.Add((color.time - startBoundary) * Zoom);
}
foreach (var alpha in EffectGradient.alphaKeys)

if (_alphaKeys == null) _alphaKeys = _effectGradient.alphaKeys;

foreach (var alpha in _alphaKeys)
{
if (alpha.time >= endBoundary)
break;
Expand All @@ -455,7 +503,13 @@ List<float> FindStops(float zoomOffset, Rect bounds)
size = bounds.height;
}

stops.Sort();
stops.Sort((x, y) =>
{
if (x > y) return 1;
if (x == y) return 0;
return -1;
});

for (int i = 0; i < stops.Count; i++)
{
stops[i] = (stops[i] * size) + min;
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Scripts/Layout/ScrollSnapBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ void Awake()

_screensContainer = _scroll_rect.content;

//ScrollRect.content RT anchors has to be stretched first in order for HSS/VSS.DistributePages() to have the correct result
_screensContainer.anchorMin = Vector2.zero;
_screensContainer.anchorMax = Vector2.one;
_screensContainer.sizeDelta = Vector2.zero;

InitialiseChildObjects();

if (NextButton)
Expand Down
4 changes: 0 additions & 4 deletions Runtime/Scripts/Primitives/UILineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,6 @@ protected override void OnEnable()
{
m_points = new Vector2[1];
}
if (transform.GetComponent<RectTransform>().position != Vector3.zero)
{
Debug.LogWarning("A Line Renderer component should be on a RectTransform positioned at (0,0,0), do not use in child Objects.\nFor best results, create separate RectTransforms as children of the canvas positioned at (0,0) for a UILineRenderer and do not move.");
}
}
}
}
92 changes: 51 additions & 41 deletions Runtime/Scripts/Utilities/UILineConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,50 @@ public class UILineConnector : MonoBehaviour
// The elements between which line segments should be drawn
public RectTransform[] transforms;
private Vector3[] previousPositions;
private RectTransform canvas;
private Vector3 previousLrPos;
private Vector3 previousGlobalScale;
private RectTransform rt;
private UILineRenderer lr;

private void Awake()
{
var canvasParent = GetComponentInParent<RectTransform>().GetParentCanvas();
if (canvasParent != null)
{
canvas = canvasParent.GetComponent<RectTransform>();
}
rt = GetComponent<RectTransform>();
lr = GetComponent<UILineRenderer>();
}

// Update is called once per frame
void Update()
private void OnEnable()
{
if (transforms == null || transforms.Length < 1)
{
return;
}
//Performance check to only redraw when the child transforms move
if (previousPositions != null && previousPositions.Length == transforms.Length)

CalculateLinePoints();
}

private void Update()
{
if (lr.RelativeSize)
{
Debug.LogWarning("While using UILineConnector, UILineRenderer should not use relative size, so that even if this RectTransform has a zero-size Rect, the positions of the points can still be calculated");
lr.RelativeSize = false;
}

if (transforms == null || transforms.Length < 1)
{
return;
}

// Get world position of UILineRenderer
Vector3 lrWorldPos = rt.position;

/*Performance check to only redraw when the child transforms move,
or the world position of UILineRenderer moves */
bool updateLine = lrWorldPos != previousLrPos;
updateLine = rt.lossyScale != previousGlobalScale;

if (!updateLine && previousPositions != null && previousPositions.Length == transforms.Length)
{
bool updateLine = false;
for (int i = 0; i < transforms.Length; i++)
{
if (transforms[i] == null)
Expand All @@ -47,56 +65,48 @@ void Update()
if (!updateLine && previousPositions[i] != transforms[i].position)
{
updateLine = true;
break;
}
}
if (!updateLine) return;
}
}
if (!updateLine) return;

// Get the pivot points
Vector2 thisPivot = rt.pivot;
Vector2 canvasPivot = canvas.pivot;

// Set up some arrays of coordinates in various reference systems
Vector3[] worldSpaces = new Vector3[transforms.Length];
Vector3[] canvasSpaces = new Vector3[transforms.Length];
Vector2[] points = new Vector2[transforms.Length];
// Calculate delta from the local position
CalculateLinePoints();


// First, convert the pivot to worldspace
//save previous states
previousLrPos = lrWorldPos;
previousGlobalScale = rt.lossyScale;
previousPositions = new Vector3[transforms.Length];
for (int i = 0; i < transforms.Length; i++)
{
if (transforms[i] == null)
{
continue;
}
worldSpaces[i] = transforms[i].TransformPoint(thisPivot);
}

// Then, convert to canvas space
for (int i = 0; i < transforms.Length; i++)
{
canvasSpaces[i] = canvas.InverseTransformPoint(worldSpaces[i]);
}

// Calculate delta from the canvas pivot point
for (int i = 0; i < transforms.Length; i++)
{
points[i] = new Vector2(canvasSpaces[i].x, canvasSpaces[i].y);
previousPositions[i] = transforms[i].position;
}
}

// And assign the converted points to the line renderer
lr.Points = points;
lr.RelativeSize = false;
lr.drivenExternally = true;

previousPositions = new Vector3[transforms.Length];
private void CalculateLinePoints()
{
Vector2[] points = new Vector2[transforms.Length];
for (int i = 0; i < transforms.Length; i++)
{
if (transforms[i] == null)
{
continue;
}
previousPositions[i] = transforms[i].position;
var offsetPos = rt.InverseTransformPoint(transforms[i].position);
points[i] = new Vector2(offsetPos.x, offsetPos.y);
}

// And assign the converted points to the line renderer
lr.Points = points;
lr.RelativeSize = false;
lr.drivenExternally = true;
}
}
}
Loading
Loading