Пример #1
0
        /// <summary>
        /// Moves an element.
        /// </summary>
        /// <param name="element">The element to move.</param>
        /// <param name="delta"></param>
        /// <param name="magnetDistance">The maximum distance at which magnet will be applied</param>
        /// <param name="resolution">The resolution of the UI.</param>
        /// <returns><c>true</c> if the element has moved; otherwise, <c>false</c>.</returns>
        public static bool Move([NotNull] UIElement element, ref Vector3 delta, float magnetDistance, ref Vector3 resolution)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            if (element.VisualParent is ContentControl)
            {
                return(false);
            }

            // moving is almost equivalent to resizing in all direction
            var parameters = new LayoutParameters
            {
                Left      = true,
                Right     = true,
                Top       = true,
                Bottom    = true,
                CanResize = false,
            };

            UpdateElementLayout(element, ref delta, magnetDistance, ref resolution, parameters);
            return(true);
        }
Пример #2
0
            public Label(string title, UIFont font)
            {
                View = new UILabel(CGRect.Empty)
                {
                    Text            = title,
                    Font            = font,
                    BackgroundColor = UIColor.Clear,
                    TextColor       = UIColor.DarkGray,
                };

                LayoutParameters = new LayoutParameters(AutoSize.WrapContent, AutoSize.WrapContent);
            }
Пример #3
0
            public Button(string title, Action handler)
            {
                // Setup the button
                //var button = new UIButton(UIButtonType.RoundedRect);
                var button = new GlassButton();

                button.SetTitle(title, UIControlState.Normal);
                View = button;

                // Attach an event handler and forward the event
                button.TouchUpInside += (sender, e) => handler();

                // Setup the layout parameters
                LayoutParameters          = new LayoutParameters(AutoSize.FillParent, AutoSize.WrapContent);
                LayoutParameters.MaxWidth = 160;
            }
Пример #4
0
        /// <summary>
        /// Resizes an element.
        /// </summary>
        /// <param name="element">The element to move.</param>
        /// <param name="resizingDirection"></param>
        /// <param name="delta"></param>
        /// <param name="magnetDistance">The maximum distance at which magnet will be applied</param>
        /// <param name="resolution">The resolution of the UI.</param>
        /// <returns></returns>
        public static bool Resize([NotNull] UIElement element, ResizingDirection resizingDirection, ref Vector3 delta, float magnetDistance, ref Vector3 resolution)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            var parameters = new LayoutParameters
            {
                Left      = resizingDirection.HasFlag(ResizingDirection.Left),
                Right     = resizingDirection.HasFlag(ResizingDirection.Right),
                Top       = resizingDirection.HasFlag(ResizingDirection.Top),
                Bottom    = resizingDirection.HasFlag(ResizingDirection.Bottom),
                CanResize = true,
            };

            UpdateElementLayout(element, ref delta, magnetDistance, ref resolution, parameters);
            return(true);
        }
Пример #5
0
        private static void UpdateElementLayout([NotNull] UIElement element, ref Vector3 delta, float magnetDistance, ref Vector3 resolution, LayoutParameters parameters)
        {
            // Retrieve all notable rects from the parent (i.e. siblings, areas such as grid cell container, etc.)
            var rects = ExtractRects(element, ref resolution);
            // copy element's current rect
            var currentElementRect = rects.Element;

            // apply resizing delta to the element rect
            if (parameters.Left)
            {
                rects.Element.X     += delta.X;
                rects.Element.Width -= delta.X;
            }
            if (parameters.Right)
            {
                rects.Element.Width += delta.X;
            }
            if (parameters.Top)
            {
                rects.Element.Y      += delta.Y;
                rects.Element.Height -= delta.Y;
            }
            if (parameters.Bottom)
            {
                rects.Element.Height += delta.Y;
            }

            // magnetize
            var horizontallyMagnetized = false;
            var verticallyMagnetized   = false;
            // .. parent
            var contentControl = element.VisualParent as ContentControl;

            if (contentControl == null || !float.IsNaN(contentControl.Width))
            {
                if (parameters.Left)
                {
                    MagnetizeLeft(ref rects.Element, ref rects.Parent, ref horizontallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Right)
                {
                    MagnetizeRight(ref rects.Element, ref rects.Parent, ref horizontallyMagnetized, magnetDistance, parameters.CanResize);
                }
            }
            if (contentControl == null || !float.IsNaN(contentControl.Height))
            {
                if (parameters.Top)
                {
                    MagnetizeTop(ref rects.Element, ref rects.Parent, ref verticallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Bottom)
                {
                    MagnetizeBottom(ref rects.Element, ref rects.Parent, ref verticallyMagnetized, magnetDistance, parameters.CanResize);
                }
            }
            // .. container
            if (rects.Parent != rects.Container)
            {
                if (parameters.Left)
                {
                    MagnetizeLeft(ref rects.Element, ref rects.Container, ref horizontallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Right)
                {
                    MagnetizeRight(ref rects.Element, ref rects.Container, ref horizontallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Top)
                {
                    MagnetizeTop(ref rects.Element, ref rects.Container, ref verticallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Bottom)
                {
                    MagnetizeBottom(ref rects.Element, ref rects.Container, ref verticallyMagnetized, magnetDistance, parameters.CanResize);
                }
            }
            // .. sibling in same container
            for (var i = 0; i < rects.Siblings.Length; i++)
            {
                if (parameters.Left)
                {
                    MagnetizeLeft(ref rects.Element, ref rects.Siblings[i], ref horizontallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Right)
                {
                    MagnetizeRight(ref rects.Element, ref rects.Siblings[i], ref horizontallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Top)
                {
                    MagnetizeTop(ref rects.Element, ref rects.Siblings[i], ref verticallyMagnetized, magnetDistance, parameters.CanResize);
                }
                if (parameters.Bottom)
                {
                    MagnetizeBottom(ref rects.Element, ref rects.Siblings[i], ref verticallyMagnetized, magnetDistance, parameters.CanResize);
                }
            }

            // calculate resulting margin
            var margin = new Thickness
            {
                Left = rects.Element.Left,
                Top  = rects.Element.Top
            };
            var horizontalAlignment = element.HorizontalAlignment;
            var verticalAlignment   = element.VerticalAlignment;

            if (element.VisualParent is Canvas)
            {
                // inside a Canvas, the alignment should be left-top
                horizontalAlignment = HorizontalAlignment.Left;
                verticalAlignment   = VerticalAlignment.Top;
            }
            else if (element.VisualParent is ContentControl)
            {
                // inside a ContentControl, the margin remains unchanged ; only the size will be updated.
                margin = element.Margin;
            }
            else
            {
                margin.Right  = rects.Container.Right - rects.Element.Right;
                margin.Bottom = rects.Container.Bottom - rects.Element.Bottom;

                var stackPanel = element.VisualParent as StackPanel;
                if (stackPanel != null)
                {
                    // inside a stackpanel the alignment depends on the panel orientation
                    if (stackPanel.Orientation == Orientation.Horizontal)
                    {
                        horizontalAlignment = HorizontalAlignment.Left;
                    }
                    else
                    {
                        verticalAlignment = VerticalAlignment.Top;
                    }
                }
                else if (rects.Container == rects.Parent)
                {
                    // adjust alignment relatively to the parent/container
                    AdjustAlignmentInContainer(ref rects.Element, ref rects.Container, out horizontalAlignment, out verticalAlignment);
                }

                // final adjustment of alignment, size and margin
                switch (horizontalAlignment)
                {
                case HorizontalAlignment.Left:
                    // Compensate when out of container bounds
                    var overLeft = rects.Container.Width - (margin.Left + rects.Element.Width);
                    margin.Right = Math.Min(overLeft, 0);
                    break;

                case HorizontalAlignment.Center:
                    // Fall back to Stretch alignment
                    horizontalAlignment = HorizontalAlignment.Stretch;
                    break;

                case HorizontalAlignment.Right:
                    // Compensate when out of container bounds
                    var overRight = rects.Container.Width - (margin.Right + rects.Element.Width);
                    margin.Left = Math.Min(overRight, 0);
                    break;
                }
                switch (verticalAlignment)
                {
                case VerticalAlignment.Bottom:
                    // Compensate when out of container bounds
                    var overBottom = rects.Container.Height - (margin.Bottom + rects.Element.Height);
                    margin.Top = Math.Min(overBottom, 0);
                    break;

                case VerticalAlignment.Center:
                    // Fall back to Stretch alignment
                    verticalAlignment = VerticalAlignment.Stretch;
                    break;

                case VerticalAlignment.Top:
                    // Compensate when out of container bounds
                    var overTop = rects.Container.Height - (margin.Top + rects.Element.Height);
                    margin.Bottom = Math.Min(overTop, 0);
                    break;
                }
            }
            // update element properties
            element.Margin = margin;
            element.HorizontalAlignment = horizontalAlignment;
            element.VerticalAlignment   = verticalAlignment;
            element.Width  = MathUtil.Clamp(rects.Element.Width, element.MinimumWidth, element.MaximumWidth);
            element.Height = MathUtil.Clamp(rects.Element.Height, element.MinimumHeight, element.MaximumHeight);

            // update to the real delta that was applied
            delta = new Vector3
            {
                X = parameters.Left ? rects.Element.Left - currentElementRect.Left : rects.Element.Right - currentElementRect.Right,
                Y = parameters.Top ? rects.Element.Top - currentElementRect.Top : rects.Element.Bottom - currentElementRect.Bottom,
                Z = 0
            };
        }
Пример #6
0
 public AutoHeightWebview(T view, LayoutParameters lp)
     : base(view, lp)
 {
     Initialize();
     AttachKVOHandler(view);
 }