Exemple #1
0
        /// <summary>
        /// Sets slider position based on value.
        /// </summary>
        private void UpdateSliderPosition(float value)
        {
            float p             = (value - Min) / (Max - Min);
            var   fillTransform = SliderFillRegion.RectTransform;

            float sliderHandleWidth = SliderHandleImageView.Sprite == null && SliderHandleImageView.Color.a <= 0
                ? 0 : SliderHandleImageView.Width.Value;

            // set handle offset
            float fillWidth        = fillTransform.rect.width;
            float slideAreaWidth   = fillWidth - sliderHandleWidth;
            float sliderFillMargin = IsReversed ? SliderFillRegion.Margin.Right.Pixels : SliderFillRegion.Margin.Left.Pixels;
            float handleOffset     = p * slideAreaWidth + sliderFillMargin;

            SliderHandleImageView.DisableLayoutUpdate = true;
            SliderHandleImageView.OffsetFromParent    = IsReversed ?
                                                        ElementMargin.FromRight(new ElementSize(handleOffset, ElementSizeUnit.Pixels)) :
                                                        ElementMargin.FromLeft(new ElementSize(handleOffset, ElementSizeUnit.Pixels));
            SliderHandleImageView.UpdateLayout(false);
            SliderHandleImageView.DisableLayoutUpdate = false;

            // set fill percentage as to match the offset of the handle
            float fillP = (handleOffset + sliderHandleWidth / 2f) / fillWidth;

            SliderFillImageView.DisableLayoutUpdate = true;
            SliderFillImageView.Width = new ElementSize(fillP, ElementSizeUnit.Percents);
            SliderFillImageView.UpdateLayout(false);
            SliderFillImageView.DisableLayoutUpdate = false;
        }
Exemple #2
0
        /// <summary>
        /// Updates the layout of the grid.
        /// </summary>
        public override bool UpdateLayout(bool notifyParent = true)
        {
            base.UpdateLayout(notifyParent);

            bool defaultDisableLayoutUpdate = DisableLayoutUpdate;

            DisableLayoutUpdate = true;

            var children = new List <UIView>();

            Content.ForEach <UIView>(x => children.Add(x), false);

            UpdateRowAndColumnDefinitions();

            // arrange children into grid
            for (int i = 0; i < children.Count; ++i)
            {
                var child     = children[i];
                var cellIndex = Cell.GetValue(child);
                if (cellIndex == null)
                {
                    if (!(child is GridSplitter))
                    {
                        Debug.LogWarning(String.Format("#Delight# {0}: Unable to arrange view \"{1}\" in the grid as it doesn't specify its cell index. Specify cell index as an attached property on the view, e.g. <{1} Grid.CellIndex=\"0,1\" ...>, to put the view in the first row and second column.", Name, child.Name));
                    }
                    continue;
                }

                // calculate width, height and offset of view based on cell index
                var columnDefinition = GetColumnDefinition(child);
                var rowDefinition    = GetRowDefinition(child);

                ElementMargin cellOffset = new ElementMargin();
                cellOffset.Left = columnDefinition.ActualOffset;
                cellOffset.Top  = rowDefinition.ActualOffset;

                // update child layout
                if (!cellOffset.Equals(child.OffsetFromParent) ||
                    !columnDefinition.ActualWidth.Equals(child.Width) ||
                    !rowDefinition.ActualHeight.Equals(child.Height) ||
                    child.Alignment != ElementAlignment.TopLeft)
                {
                    bool defaultDisableChildLayoutUpdate = child.DisableLayoutUpdate;
                    child.DisableLayoutUpdate = true;

                    child.OffsetFromParent = cellOffset;
                    child.Width            = columnDefinition.ActualWidth;
                    child.Height           = rowDefinition.ActualHeight;
                    child.Alignment        = ElementAlignment.TopLeft;

                    child.UpdateLayout(false);
                    child.DisableLayoutUpdate = defaultDisableChildLayoutUpdate;
                }
            }

            DisableLayoutUpdate = defaultDisableLayoutUpdate;

            return(false);
        }
        public virtual void ScrollTo(int index, ElementAlignment?alignment = null, ElementMargin offset = null)
        {
            if (index < 0 || index >= Count)
            {
                return;
            }

            ScrollTo(this[index], alignment, offset);
        }
 public virtual void ScrollTo(T item, ElementAlignment?alignment = null, ElementMargin offset = null)
 {
     Notify(new CollectionSelectionEventArgs
     {
         ChangeAction = CollectionChangeAction.ScrollTo,
         Item         = item,
         Alignment    = alignment,
         Offset       = offset
     });
 }
Exemple #5
0
        /// <summary>
        /// Gets current handle offset as vector.
        /// </summary>
        public Vector2 GetHandleOffset()
        {
            if (OffsetFromParent == null)
            {
                OffsetFromParent = new ElementMargin();
            }

            return(new Vector2(OffsetFromParent.Left.Pixels,
                               OffsetFromParent.Top.Pixels));
        }
 public virtual void Select(T item, ElementAlignment?alignment = null, ElementMargin offset = null, bool scrollTo = false)
 {
     Notify(new CollectionSelectionEventArgs
     {
         ChangeAction = CollectionChangeAction.Select,
         Item         = item,
         Alignment    = alignment,
         Offset       = offset,
         ScrollTo     = scrollTo
     });
 }
Exemple #7
0
        /// <summary>
        /// Gets actual offset.
        /// </summary>
        public ElementMargin GetActualOffset(float actualWidth, float actualHeight)
        {
            if (Left.Unit == ElementSizeUnit.Percents || Top.Unit == ElementSizeUnit.Percents ||
                Right.Unit == ElementSizeUnit.Percents || Bottom.Unit == ElementSizeUnit.Percents)
            {
                var elementMargin = new ElementMargin();
                elementMargin.Left   = Left.Unit == ElementSizeUnit.Percents ? actualWidth * Left.Percent : Left.Pixels;
                elementMargin.Top    = Top.Unit == ElementSizeUnit.Percents ? actualHeight * Top.Percent : Top.Pixels;
                elementMargin.Right  = Right.Unit == ElementSizeUnit.Percents ? actualWidth * Right.Percent : Right.Pixels;
                elementMargin.Bottom = Bottom.Unit == ElementSizeUnit.Percents ? actualHeight * Bottom.Percent : Bottom.Pixels;
                return(elementMargin);
            }

            return(this);
        }
Exemple #8
0
        /// <summary>
        /// Sets content offset as vector.
        /// </summary>
        private void SetHandleOffset(Vector2 contentOffset)
        {
            if (OffsetFromParent == null)
            {
                OffsetFromParent = new ElementMargin();
            }

            if (IsColumnSplitter)
            {
                if (OffsetFromParent.Left.Pixels != contentOffset.x)
                {
                    OffsetFromParent.Left.Pixels = contentOffset.x;
                }
            }
            else
            {
                if (OffsetFromParent.Top.Pixels != contentOffset.y)
                {
                    OffsetFromParent.Top.Pixels = contentOffset.y;
                }
            }
        }
Exemple #9
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public VirtualItem(ElementSize width, ElementSize height)
 {
     Width  = width != null ? new ElementSize(width) : ElementSize.FromPercents(1);
     Height = height != null ? new ElementSize(height) : ElementSize.FromPercents(1);
     Offset = new ElementMargin();
 }
Exemple #10
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public VirtualItem()
 {
     Width  = ElementSize.FromPercents(1);
     Height = ElementSize.FromPercents(1);
     Offset = new ElementMargin();
 }
Exemple #11
0
        /// <summary>
        /// Updates the layout of the group.
        /// </summary>
        public override bool UpdateLayout(bool notifyParent = true)
        {
            bool defaultDisableLayoutUpdate = DisableLayoutUpdate;

            DisableLayoutUpdate = true;

            bool          hasNewSize       = false;
            float         maxWidth         = 0f;
            float         maxHeight        = 0f;
            float         totalWidth       = 0f;
            float         totalHeight      = 0f;
            bool          percentageWidth  = false;
            bool          percentageHeight = false;
            bool          isHorizontal     = Orientation == ElementOrientation.Horizontal;
            List <UIView> children         = new List <UIView>();

            this.ForEach <UIView>(x =>
            {
                children.Add(x);
            }, false);

            // get size of content and set content offsets and alignment
            var spacing    = Spacing ?? ElementSize.Default;
            int childCount = children.Count;
            int childIndex = 0;

            for (int i = 0; i < childCount; ++i)
            {
                var childView = children[i];
                if (!childView.IsActive)
                {
                    // don't group inactive views
                    continue;
                }

                var childWidth  = childView.OverrideWidth ?? (childView.Width ?? ElementSize.Default);
                var childHeight = childView.OverrideHeight ?? (childView.Height ?? ElementSize.Default);

                if (childWidth.Unit == ElementSizeUnit.Percents)
                {
                    if (isHorizontal)
                    {
                        Debug.LogWarning(String.Format("#Delight# Unable to group view \"{0}\" horizontally as it doesn't specify its width in pixels.", childView.Name));
                        continue;
                    }
                    else
                    {
                        percentageWidth = true;
                    }
                }

                if (childHeight.Unit == ElementSizeUnit.Percents)
                {
                    if (!isHorizontal)
                    {
                        Debug.LogWarning(String.Format("#Delight# Unable to group view \"{0}\" vertically as it doesn't specify its height in pixels or elements.", childView.Name));
                        continue;
                    }
                    else
                    {
                        percentageHeight = true;
                    }
                }

                bool defaultDisableChildLayoutUpdate = childView.DisableLayoutUpdate;
                childView.DisableLayoutUpdate = true;

                // set offsets and alignment
                var offset = new ElementMargin(
                    new ElementSize(isHorizontal ? totalWidth + spacing.Pixels * childIndex : 0f, ElementSizeUnit.Pixels),
                    new ElementSize(!isHorizontal ? totalHeight + spacing.Pixels * childIndex : 0f, ElementSizeUnit.Pixels));

                // set desired alignment if it is valid for the orientation otherwise use defaults
                var alignment        = ElementAlignment.Center;
                var defaultAlignment = isHorizontal ? ElementAlignment.Left : ElementAlignment.Top;
                var desiredAlignment = !ContentAlignmentProperty.IsUndefined(this) ? ContentAlignment : childView.Alignment;
                if (isHorizontal && (desiredAlignment == ElementAlignment.Top || desiredAlignment == ElementAlignment.Bottom ||
                                     desiredAlignment == ElementAlignment.TopLeft || desiredAlignment == ElementAlignment.BottomLeft))
                {
                    alignment = defaultAlignment | desiredAlignment;
                }
                else if (!isHorizontal && (desiredAlignment == ElementAlignment.Left || desiredAlignment == ElementAlignment.Right ||
                                           desiredAlignment == ElementAlignment.TopLeft || desiredAlignment == ElementAlignment.TopRight))
                {
                    alignment = defaultAlignment | desiredAlignment;
                }
                else
                {
                    alignment = defaultAlignment;
                }

                // get size of content
                if (!percentageWidth)
                {
                    totalWidth += childWidth;
                    maxWidth    = childWidth.Pixels > maxWidth ? childWidth.Pixels : maxWidth;
                }

                if (!percentageHeight)
                {
                    totalHeight += childHeight;
                    maxHeight    = childHeight.Pixels > maxHeight ? childHeight.Pixels : maxHeight;
                }

                // update child layout
                if (!offset.Equals(childView.OffsetFromParent) || alignment != childView.Alignment)
                {
                    childView.OffsetFromParent = offset;
                    childView.Alignment        = alignment;

                    childView.UpdateLayout(false);
                }
                ++childIndex;
                childView.DisableLayoutUpdate = defaultDisableChildLayoutUpdate;
            }

            // set width and height
            float totalSpacing = childCount > 1 ? (childIndex - 1) * spacing.Pixels : 0f;

            // adjust width to content
            if (WidthProperty.IsUndefined(this))
            {
                if (!percentageWidth)
                {
                    // add margins
                    var margin = Margin ?? ElementMargin.Default;
                    totalWidth += isHorizontal ? totalSpacing : 0f;
                    totalWidth += margin.Left.Pixels + margin.Right.Pixels;
                    maxWidth   += margin.Left.Pixels + margin.Right.Pixels;

                    // adjust width to content
                    var newWidth = new ElementSize(isHorizontal ? totalWidth : maxWidth, ElementSizeUnit.Pixels);
                    if (!newWidth.Equals(Width))
                    {
                        OverrideWidth = newWidth;
                        hasNewSize    = true;
                    }
                }
                else
                {
                    var newWidth = new ElementSize(1, ElementSizeUnit.Percents);
                    if (!newWidth.Equals(Width))
                    {
                        OverrideWidth = newWidth;
                        hasNewSize    = true;
                    }
                }
            }
            else if (OverrideWidth != null && !OverrideWidth.Equals(Width))
            {
                // clear override
                OverrideWidth = null;
                hasNewSize    = true;
            }

            // adjust height to content
            if (HeightProperty.IsUndefined(this))
            {
                if (!percentageHeight)
                {
                    // add margins
                    var margin = Margin ?? ElementMargin.Default;
                    totalHeight += !isHorizontal ? totalSpacing : 0f;
                    totalHeight += margin.Top.Pixels + margin.Bottom.Pixels;
                    maxHeight   += margin.Top.Pixels + margin.Bottom.Pixels;

                    // adjust height to content
                    var newHeight = new ElementSize(!isHorizontal ? totalHeight : maxHeight, ElementSizeUnit.Pixels);
                    if (!newHeight.Equals(Height))
                    {
                        OverrideHeight = newHeight;
                        hasNewSize     = true;
                    }
                }
                else
                {
                    var newHeight = new ElementSize(1, ElementSizeUnit.Percents);
                    if (!newHeight.Equals(Height))
                    {
                        OverrideHeight = newHeight;
                        hasNewSize     = true;
                    }
                }
            }
            else if (OverrideHeight != null && !OverrideHeight.Equals(Height))
            {
                // clear override
                OverrideHeight = null;
                hasNewSize     = true;
            }

            DisableLayoutUpdate = defaultDisableLayoutUpdate;

            return(base.UpdateLayout(notifyParent) || hasNewSize);
        }