private void UpdateScrollBar(ScrollRect scrollView, Entity scrollBarEntity, float2 contentToViewportRatio, ref ScrollViewTransforms transforms, ScrollBarAxis axis) { var scrollBar = ScrollBarFromEntity[scrollBarEntity]; var areaTransform = RectTransformFromEntity[scrollBarEntity]; var handleTransform = RectTransformFromEntity[scrollBar.ScrollHandle]; var scrollBarSize = math.saturate(contentToViewportRatio); float2 moveRange = (transforms.ViewportRect.Size - transforms.ContentRect.Size); float2 contentSize = transforms.ContentRect.Size; if (axis == ScrollBarAxis.Vertical) { areaTransform.AnchorMin = new float2(areaTransform.AnchorMin.x, 0.0f); areaTransform.AnchorMax = new float2(areaTransform.AnchorMax.x, 1.0f); areaTransform.Pivot = new float2(1.0f, 1.0f); areaTransform.SizeDelta = new float2(areaTransform.SizeDelta.x, -(areaTransform.SizeDelta.x + scrollView.HorizontalBarSpacing)); var areaRect = RectTransformUtils.CalculateWorldSpaceRect(transforms.ScrollViewRect, ElementScaleFromEntity[scrollBarEntity].Value, areaTransform); handleTransform.AnchorMin = new float2(0.0f, scrollBar.Value * (1.0f - scrollBarSize.y)); handleTransform.AnchorMax = new float2(1.0f, handleTransform.AnchorMin.y + scrollBarSize.y); WorldSpaceRect handleRect = RectTransformUtils.CalculateWorldSpaceRect(areaRect, ElementScaleFromEntity[scrollBarEntity].Value, handleTransform); scrollBar.HandleDragSensitivity = 1.0f / (areaRect.Size.y - handleRect.Size.y); scrollBar.RectDragSensitivity = scrollBar.HandleDragSensitivity / ((transforms.ContentRect.Size.y - transforms.ViewportRect.Size.y) / (areaRect.Size.y - handleRect.Size.y)); transforms.ContentRect.Min.y = transforms.ViewportRect.Min.y + scrollBar.Value * moveRange.y; transforms.ContentRect.Max.y = transforms.ContentRect.Min.y + contentSize.y; } else { areaTransform.AnchorMin = new float2(0.0f, areaTransform.AnchorMin.y); areaTransform.AnchorMax = new float2(1.0f, areaTransform.AnchorMax.y); areaTransform.Pivot = new float2(0.0f, 0.0f); areaTransform.SizeDelta = new float2(-(areaTransform.SizeDelta.y + scrollView.VerticalBarSpacing), areaTransform.SizeDelta.y); var areaRect = RectTransformUtils.CalculateWorldSpaceRect(transforms.ScrollViewRect, ElementScaleFromEntity[scrollBarEntity].Value, areaTransform); handleTransform.AnchorMin = new float2(scrollBar.Value * (1.0f - scrollBarSize.x), 0.0f); handleTransform.AnchorMax = new float2(handleTransform.AnchorMin.x + scrollBarSize.x, 1.0f); WorldSpaceRect handleRect = RectTransformUtils.CalculateWorldSpaceRect(areaRect, ElementScaleFromEntity[scrollBarEntity].Value, handleTransform); scrollBar.HandleDragSensitivity = 1.0f / (areaRect.Size.x - handleRect.Size.x); scrollBar.RectDragSensitivity = scrollBar.HandleDragSensitivity / ((transforms.ContentRect.Size.x - transforms.ViewportRect.Size.x) / (areaRect.Size.x - handleRect.Size.x)); transforms.ContentRect.Min.x = transforms.ViewportRect.Min.x + scrollBar.Value * moveRange.x; transforms.ContentRect.Max.x = transforms.ContentRect.Min.x + contentSize.x; } ScrollBarFromEntity[scrollBarEntity] = scrollBar; RectTransformFromEntity[scrollBar.ScrollHandle] = handleTransform; RectTransformFromEntity[scrollBarEntity] = areaTransform; }
private void UpdateScrollRectTransform(Entity scrollEntity, WorldSpaceRect scrollRectWorldSpace, HierarchyRebuildContext rebuildContext) { var children = ChildrenFromEntity[scrollEntity]; var scrollMask = WorldSpaceMaskFromEntity[scrollEntity]; var scale = ElementScaleFromEntity[scrollEntity].Value; for (int j = 0; j < children.Length; j++) { rebuildContext.UpdateTransformRecursive(ref scrollRectWorldSpace, scrollMask, children[j].Value, scale); } }
private bool IsInRect(ref WorldSpaceRect rect, float2 position) { return(position.x >= rect.Min.x && position.x <= rect.Max.x && position.y >= rect.Min.y && position.y <= rect.Max.y); }
public void InverseRectTransform() { NativeArray <WorldSpaceRect> parentRects = new NativeArray <WorldSpaceRect>(4, Allocator.Temp); parentRects[0] = new WorldSpaceRect() { Min = new float2(0.0f, 0.0f), Max = new float2(100.0f, 100.0f) }; parentRects[1] = new WorldSpaceRect() { Min = new float2(10.0f, 10.0f), Max = new float2(100.0f, 100.0f) }; parentRects[2] = new WorldSpaceRect() { Min = new float2(-10.0f, -10.0f), Max = new float2(100.0f, 100.0f) }; parentRects[3] = new WorldSpaceRect() { Min = new float2(-100.0f, -100.0f), Max = new float2(-10.0f, -10.0f) }; NativeArray <RectTransform> rectTransforms = new NativeArray <RectTransform>(4, Allocator.Temp); rectTransforms[0] = new RectTransform() { AnchorMin = new float2(0.0f, 0.0f), AnchorMax = new float2(1.0f, 1.0f), Pivot = new float2(0.5f, 0.5f), Position = new float2(5.0f, 5.0f), SizeDelta = new float2(20.0f, 20.0f) }; rectTransforms[1] = new RectTransform() { AnchorMin = new float2(0.0f, 0.0f), AnchorMax = new float2(0.0f, 1.0f), Pivot = new float2(1.0f, 1.0f), Position = new float2(5.0f, 5.0f), SizeDelta = new float2(20.0f, 20.0f) }; rectTransforms[2] = new RectTransform() { AnchorMin = new float2(0.5f, 0.5f), AnchorMax = new float2(0.5f, 0.5f), Pivot = new float2(0.5f, 0.5f), Position = new float2(-5.0f, -5.0f), SizeDelta = new float2(20.0f, 20.0f) }; rectTransforms[3] = new RectTransform() { AnchorMin = new float2(1.0f, 1.0f), AnchorMax = new float2(1.0f, 1.0f), Pivot = new float2(0.0f, 0.0f), Position = new float2(-5.0f, -5.0f), SizeDelta = new float2(20.0f, 20.0f) }; for (int i = 0; i < parentRects.Length; i++) { for (int j = 0; j < rectTransforms.Length; j++) { var worldSpace = RectTransformUtils.CalculateWorldSpaceRect(parentRects[i], new float2(1.0f, 1.0f), rectTransforms[j]); var emptyTransform = new RectTransform() { AnchorMin = rectTransforms[j].AnchorMin, AnchorMax = rectTransforms[j].AnchorMax, Pivot = rectTransforms[j].Pivot }; var calculatedTransform = RectTransformUtils.CalculateInverseTransformWithAnchors(worldSpace, parentRects[i], emptyTransform, new float2(1.0f, 1.0f)); Assert.IsTrue(RectTransformEquals(rectTransforms[j], calculatedTransform)); } } }
private bool NeedUpdate(WorldSpaceRect oldContentRect, WorldSpaceRect contentRect) { return(math.distance(oldContentRect.Min, contentRect.Min) > 0.5f || math.distance(oldContentRect.Max, contentRect.Max) > 0.5f); }