/// <summary> /// Remove the thickness values into the size calculation of a UI element. /// </summary> /// <param name="sizeWithMargins">The size with the thickness included</param> /// <param name="thickness">The thickness to remove in the space</param> /// <returns>The size with the margins not included</returns> protected static Vector3 CalculateSizeWithoutThickness(ref Vector3 sizeWithMargins, ref Thickness thickness) { return new Vector3( Math.Max(0, sizeWithMargins.X - thickness.Left - thickness.Right), Math.Max(0, sizeWithMargins.Y - thickness.Top - thickness.Bottom), Math.Max(0, sizeWithMargins.Z - thickness.Front - thickness.Back)); }
/// <summary> /// Computes the (X,Y,Z) offsets to position correctly the UI element given the total provided space to it. /// </summary> /// <param name="thickness">The thickness around the element to position.</param> /// <param name="providedSpace">The total space given to the child element by the parent</param> /// <param name="usedSpaceWithoutThickness">The space used by the child element without the thickness included in it.</param> /// <returns>The offsets</returns> protected Vector3 CalculateAdjustmentOffsets(ref Thickness thickness, ref Vector3 providedSpace, ref Vector3 usedSpaceWithoutThickness) { // compute the size of the element with the thickness included var usedSpaceWithThickness = CalculateSizeWithThickness(ref usedSpaceWithoutThickness, ref thickness); // set offset for left and stretch alignments var offsets = new Vector3(thickness.Left, thickness.Top, thickness.Front); // align the element horizontally switch (HorizontalAlignment) { case HorizontalAlignment.Center: case HorizontalAlignment.Stretch: offsets.X += (providedSpace.X - usedSpaceWithThickness.X) / 2; break; case HorizontalAlignment.Right: offsets.X += providedSpace.X - usedSpaceWithThickness.X; break; } // align the element vertically switch (VerticalAlignment) { case VerticalAlignment.Center: case VerticalAlignment.Stretch: offsets.Y += (providedSpace.Y - usedSpaceWithThickness.Y) / 2; break; case VerticalAlignment.Bottom: offsets.Y += providedSpace.Y - usedSpaceWithThickness.Y; break; } // align the element vertically switch (DepthAlignment) { case DepthAlignment.Center: case DepthAlignment.Stretch: offsets.Z += (providedSpace.Z - usedSpaceWithThickness.Z) / 2; break; case DepthAlignment.Back: offsets.Z += providedSpace.Z - usedSpaceWithThickness.Z; break; } return offsets; }
/// <summary> /// Add the thickness values into the size calculation of a UI element. /// </summary> /// <param name="sizeWithoutMargins">The size without the thickness included</param> /// <param name="thickness">The thickness to add to the space</param> /// <returns>The size with the margins included</returns> protected static Vector3 CalculateSizeWithThickness(ref Vector3 sizeWithoutMargins, ref Thickness thickness) { var negativeThickness = -thickness; return CalculateSizeWithoutThickness(ref sizeWithoutMargins, ref negativeThickness); }