示例#1
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     arrowUp.Size   = new Vector2(Width);
     arrowDown.Size = new Vector2(Width);
     slider.Size    = new Vector2(Width, 10);
     base.DoLayout(category);
 }
示例#2
0
 public virtual void DoLayoutOnChildren(ConstraintCategory category = ConstraintCategory.All)
 {
     if (HasDecorations)
     {
         decorations.ForEach(c => c.DoLayout(category));
     }
 }
示例#3
0
        public void CheckScrollBarsVisible(ConstraintCategory category = ConstraintCategory.All)
        {
            if (mode.ContainsFlag(ScrollBarMode.Vertical))
            {
                var v = ScrollPanel.ContentPanel.size.Y > size.Y;
                if (v != VerticalScrollBar.Visible)
                {
                    VerticalScrollBar.Visible = v;
                    VerticalScrollBar.DoLayout(category);
                }
            }
            if (mode.ContainsFlag(ScrollBarMode.Horizontal))
            {
                var v = ScrollPanel.ContentPanel.size.X > size.X;
                if (v != HorizontalScrollBar.Visible)
                {
                    HorizontalScrollBar.Visible = v;
                    HorizontalScrollBar.DoLayout(category);
                }
            }

            if (mode == ScrollBarMode.Both)
            {
                var showFiller = VerticalScrollBar.Visible && HorizontalScrollBar.Visible;
                if (showFiller != fillerPanel.Visible)
                {
                    fillerPanel.Visible = showFiller;
                    base.DoLayout(category);
                }
            }
        }
 public UIConstraint(Edge controlEdge, UIControl anchor, Edge anchorEdge, float distance = 0f, ConstraintCategory constraintCategory = ConstraintCategory.All)
 {
     ControlEdge = controlEdge;
     Anchor      = anchor;
     AnchorEdge  = anchorEdge;
     Distance    = distance;
     Category    = constraintCategory;
 }
示例#5
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     base.DoLayout(category);
     if (AutoSize)
     {
         ResizeToContent();
     }
 }
示例#6
0
 public UIConstraint(Edge controlEdge, UIControl anchor, Edge anchorEdge, float distance = 0f, ConstraintCategory constraintCategory = ConstraintCategory.All)
 {
     ControlEdge = controlEdge;
     Anchor = anchor;
     AnchorEdge = anchorEdge;
     Distance = distance;
     Category = constraintCategory;
 }
示例#7
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     base.DoLayout(category);
     if (multiLine)
     {
         SplitText(base.Size.X);
     }
 }
示例#8
0
        public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
        {
            var oldPos = contentPanel.Position;

            base.DoLayout(category);
            contentPanel.ResizeToContent(category);

            contentPanel.Position = oldPos;
        }
 public virtual void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     this.EnforceConstraints(category);
     if (HasDecorations)
     {
         decorations.ForEach(d => d.DoLayout(category));
     }
     layoutDone = true;
 }
示例#10
0
        //public override Vector2 Size
        //{
        //	get { return base.Size; }
        //	set
        //	{
        //		if (size != value)
        //		{
        //			size = value;
        //			if (!SuspendLayout)
        //			{
        //				base.DoLayout(ConstraintCategory.Update);
        //			}
        //			UpdateTransformation = true;
        //		}
        //	}
        //}

        public void ResizeToContent(ConstraintCategory category = ConstraintCategory.All)
        {
            RectangleF rect = CalcContentBounds(true);

            size = new Vector2(rect.Width, rect.Height);
            UpdateTransformation = true;
            base.DoLayout(category);

            //SetMargin();
        }
示例#11
0
 public virtual void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     if (Enabled)
     {
         suspendLayout = false;
         this.EnforceConstraints(category);
         UpdateTransform();
         DoLayoutOnChildren(category);
         layoutDone = true;
     }
 }
        internal static void EnforceConstraints(this UIControl control, ConstraintCategory category = ConstraintCategory.All)
        {
            if (control.SuspendLayout)
            {
                return;
            }

            UIManager.layoutProfiler.Start();
            EnforceConstraints(control, control.Constrainer, category);
            UIManager.layoutProfiler.Stop();
        }
示例#13
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     base.DoLayout(category);
     foreach (UIControl control in controls)
     {
         if (control.Enabled && control.Visible)
         {
             control.DoLayout(category);
         }
     }
 }
示例#14
0
        /// <summary>
        /// Adds a UI constraint to this control.
        /// </summary>
        /// <param name="controlEdge">Control edge to constraint.</param>
        /// <param name="anchor">Anchoring control on which the control edge is constrained. Setting this to 'null' the control is anchored relative to the screen.</param>
        /// <param name="anchorEdge">Anchor edge, on which the control edge is constrained relative to.</param>
        /// <param name="edgeDistance">Distance between control and anchor constraint edge.</param>
        internal void AddConstraint(Edge controlEdge, UIControl anchor, Edge anchorEdge, float edgeDistance, ConstraintCategory category)
        {
            // Separate edges into their basic flags.
            // E.g. 'Edge.BottomRight' is separated into 'Edge.Bottom' and 'Edge.Right', and added as individual edges.
            List<Edge> controlEdges = new List<Edge>(4);
            List<Edge> anchorEdges = new List<Edge>(4);
            foreach (Edge edgeType in EdgeTypes)
            {
                if (controlEdge.HasFlag(edgeType))
                {
                    controlEdges.Add(edgeType);
                }
                if (anchorEdge.HasFlag(edgeType))
                {
                    anchorEdges.Add(edgeType);
                }
            }

            if (controlEdges.Count != anchorEdges.Count)
            {
                throw new ArgumentException("There must be an equal amount of control and anchor edges.");
            }

            for (int i = 0; i < controlEdges.Count; i++)
            {
                var cEdge = controlEdges[i];
                var aEdge = anchorEdges[i];

                if (!OnSameAxis(cEdge, aEdge))
                {
                    throw new ArgumentException($"Control and anchor edge and must be on the same axis: {cEdge}, {aEdge}");
                }

                foreach (var edge in Constraints.Select(c => c.ControlEdge))
                {
                    if (edge.Equals(cEdge))
                    {
                        throw new ArgumentException($"Control edge already bound: {cEdge}");
                    }
                    if (Edge.CenterXY.ContainsFlag(cEdge | edge) && OnSameAxis(cEdge, edge)) // Special case to check that no edge is bound to an axis along with a center edge. E.g.: CenterY and Top.
                    {
                        throw new ArgumentException($"Control axis already bound: {cEdge}, {edge}");
                    }
                }

                Constraints.Add(new UIConstraint(cEdge, anchor, aEdge, edgeDistance, category));
            }
        }
示例#15
0
        public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
        {
            base.DoLayout(category);

            Vector2   size = ViewportSize;
            Rectangle rect = new Rectangle(0, 0, (int)size.X, (int)size.Y);

            if (rect.Intersects(position))
            {
                State = TweenState.Shown;
            }
            else
            {
                State = TweenState.Hidden;
            }
        }
示例#16
0
        //public override bool AbsorbPointer
        //{
        //	get { return base.AbsorbPointer; }
        //	set
        //	{
        //		base.AbsorbPointer = value;
        //		if (slider != null)
        //		{
        //			slider.AbsorbPointer = value;
        //			arrowDown.AbsorbPointer = value;
        //			arrowUp.AbsorbPointer = value;
        //			sliderBackground.AbsorbPointer = value;
        //		}
        //	}
        //}

        public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
        {
            switch (Orientation)
            {
            case ScrollBarOrientation.Vertical:
                arrowA.Size = new Vector2(Width);
                arrowB.Size = new Vector2(Width);
                break;

            case ScrollBarOrientation.Horizontal:
                arrowA.Size = new Vector2(Height);
                arrowB.Size = new Vector2(Height);
                break;
            }

            base.DoLayout(category);
        }
示例#17
0
        /// <summary>
        /// Adds a UI constraint to this control.
        /// </summary>
        /// <param name="control">Control instance.</param>
        /// <param name="controlEdge">Control edge to constraint.</param>
        /// <param name="anchor">Anchoring control on which the control edge is constrained. If 'null', the control is anchored relative to the viewport.</param>
        /// <param name="anchorEdge">Anchor edge on which the control edge is constrained relative to.</param>
        /// <param name="edgeDistance">Distance between control and anchor constraint edges.</param>
        /// <param name="category">The category this constraint belongs to.</param>
        public static void AddConstraint(this UIControl control, Edge controlEdge, UIControl anchor, Edge anchorEdge, float edgeDistance = 0, ConstraintCategory category = ConstraintCategory.All)
        {
            if (control == anchor)
            {
                throw new ArgumentException("Cannot anchor control to itself.");
            }

            //// Separate edges into their basic flags.
            //// E.g. 'Edge.BottomRight' is separated into 'Edge.Bottom' and 'Edge.Right', and added as individual edges.
            //List<Edge> controlEdges = new List<Edge>(4);
            //List<Edge> anchorEdges = new List<Edge>(4);
            //foreach (Edge edgeType in UIConstrainer.EdgeTypes)
            //{
            //	if (controlEdge.ContainsFlag(edgeType))
            //	{
            //		controlEdges.Add(edgeType);
            //	}
            //	if (anchorEdge.ContainsFlag(edgeType))
            //	{
            //		anchorEdges.Add(edgeType);
            //	}
            //}

            //if (controlEdges.Count != anchorEdges.Count)
            //{
            //	throw new ArgumentException("There must be an equal amount of control and anchor edges.");
            //}
            if (control.Constrainer == null)
            {
                control.Constrainer = new UIConstrainer();
            }
            control.Constrainer.AddConstraint(controlEdge, anchor, anchorEdge, edgeDistance, category);

            //for (int i = 0; i < controlEdges.Count; i++)
            //{
            //	control.Constrainer.AddConstraint(controlEdges[i], anchor, anchorEdges[i], edgeDistance, category);
            //}
        }
示例#18
0
        //public override Vector2 Size
        //{
        //    get { return base.Size; }
        //    set
        //    {
        //        if (size != value)
        //        {
        //            size = value;
        //            if (!SuspendLayout)
        //            {
        //                base.DoLayout(ConstraintCategory.Update);
        //            }
        //            UpdateTransformation = true;
        //        }
        //    }
        //}
        public void ResizeToContent(ConstraintCategory category = ConstraintCategory.All)
        {
            RectangleF rect = CalcContentBounds(true);
            size = new Vector2(rect.Width, rect.Height);
            UpdateTransformation = true;
            base.DoLayout(category);

            //SetMargin();
        }
示例#19
0
        //public override bool AbsorbPointer
        //{
        //    get { return base.AbsorbPointer; }
        //    set
        //    {
        //        base.AbsorbPointer = value;
        //        if (slider != null)
        //        {
        //            slider.AbsorbPointer = value;
        //            arrowDown.AbsorbPointer = value;
        //            arrowUp.AbsorbPointer = value;
        //            sliderBackground.AbsorbPointer = value;
        //        }
        //    }
        //}
        public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
        {
            switch (Orientation)
            {
                case ScrollBarOrientation.Vertical:
                    arrowA.Size = new Vector2(Width);
                    arrowB.Size = new Vector2(Width);
                    break;

                case ScrollBarOrientation.Horizontal:
                    arrowA.Size = new Vector2(Height);
                    arrowB.Size = new Vector2(Height);
                    break;
            }

            base.DoLayout(category);
        }
示例#20
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     if (AutoSize)
     {
         ResizeToContent(category);
     }
     else
     {
         base.DoLayout(category);
     }
 }
        private static void EnforceConstraints(this UIControl control, UIConstrainer constrainer, ConstraintCategory category = ConstraintCategory.All)
        {
            if (constrainer != null)
            {
                foreach (UIConstraint constraint in constrainer.Constraints)
                {
                    // Check whether the constraint should be enforced.
                    if (category != ConstraintCategory.All && !category.ContainsFlag(constraint.Category))
                    {
                        continue;
                    }

                    Edge      controlEdge   = constraint.ControlEdge;
                    UIControl anchor        = constraint.Anchor;
                    float     distance      = constraint.Distance;
                    bool      anchorVisible = true;

                    Vector2 pos        = Vector2.Zero;
                    Vector2 anchorSize = Engine.ScreenSize;

                    if (anchor != null)
                    {
                        anchorVisible = anchor.Visible;
                        anchorSize    = anchorVisible ? (anchor.Size * anchor.scale).Max(anchor.MinimumSize * anchor.scale) : Vector2.Zero;
                        if (anchor.Parent == control.Parent)                            // Is anchor and control children of the same parent?
                        {
                            pos = anchor.position;                                      // ..then set control position relative to the anchor.
                        }

                        Vector2 scaleDelta = anchor.ScaledSize - anchor.Size;
                        pos -= anchor.NormalizedOrigin * scaleDelta;
                    }

                    Vector2 halfAnchorSize = anchorSize * 0.5f;

                    switch (constraint.AnchorEdge)
                    {
                    case Edge.Left:
                        pos.X += distance;
                        break;

                    case Edge.Top:
                        pos.Y += distance;
                        break;

                    case Edge.Right:
                        pos.X += anchorSize.X - distance;
                        break;

                    case Edge.Bottom:
                        pos.Y += anchorSize.Y - distance;
                        break;

                    case Edge.CenterX:
                        pos.X += halfAnchorSize.X + distance;
                        break;

                    case Edge.CenterY:
                        pos.Y += halfAnchorSize.Y + distance;
                        break;
                    }
                    if (!constrainer.AnchoredOpposite(controlEdge))
                    {
                        switch (controlEdge)
                        {
                        case Edge.Left:
                            control.position.X = pos.X;
                            break;

                        case Edge.Top:
                            control.position.Y = pos.Y;
                            break;

                        case Edge.Right:
                            control.position.X = pos.X - Math.Max(control.Width, control.MinimumSize.X);
                            break;

                        case Edge.Bottom:
                            control.position.Y = pos.Y - Math.Max(control.Height, control.MinimumSize.Y);
                            break;

                        case Edge.CenterX:
                            control.position.X = pos.X - Math.Max(control.HalfWidth, control.MinimumSize.X * 0.5f);
                            break;

                        case Edge.CenterY:
                            control.position.Y = pos.Y - Math.Max(control.HalfHeight, control.MinimumSize.Y * 0.5f);
                            break;
                        }
                    }
                    else
                    {
                        switch (controlEdge)
                        {
                        case Edge.Left:
                        case Edge.CenterX:
                            control.size.X    += control.position.X - pos.X;
                            control.position.X = pos.X;
                            break;

                        case Edge.Top:
                        case Edge.CenterY:
                            control.size.Y    += control.position.Y - pos.Y;
                            control.position.Y = pos.Y;
                            break;

                        case Edge.Right:
                            control.size.X += (pos.X - control.Width) - control.position.X;
                            if (!anchorVisible)
                            {
                                control.size.X += anchor.size.X;                                         // TODO: Needs proper testing
                            }
                            //control.location.X += pos.X - control.size.X;
                            break;

                        case Edge.Bottom:
                            control.size.Y += (pos.Y - control.Height) - control.position.Y;
                            if (!anchorVisible)
                            {
                                control.size.Y += anchor.size.Y;                                         // TODO: Needs proper testing
                            }
                            //control.location.Y += pos.Y - control.size.Y;
                            break;
                        }
                        control.UpdateTransformation = true;                         // Control transformation needs a recalculation when its size is updated.
                    }
                }
            }

            if (control.MinimumSize != Vector2.Zero)
            {
                control.size = control.size.Max(control.MinimumSize);
                control.UpdateTransformation = true;
            }

            _UpdateDrawPosition(control);
        }
示例#22
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     base.DoLayout(category);
     CheckScrollBarsVisible(category);
     UpdateScrollBarPosition();
 }
示例#23
0
 /// <summary>
 /// Adds a UI constraint to this control.
 /// </summary>
 /// <param name="control">Control instance.</param>
 /// <param name="controlEdge">Control edge to constraint.</param>
 /// <param name="anchor">Anchoring control on which the control edge is constrained. If 'null', the control is anchored relative to the viewport.</param>
 /// <param name="anchorEdge">Anchor edge on which the control edge is constrained relative to.</param>
 /// <param name="category">The category this constraint belongs to.</param>
 public static void AddConstraint(this UIControl control, Edge controlEdge, UIControl anchor, Edge anchorEdge, ConstraintCategory category)
 {
     control.AddConstraint(controlEdge, anchor, anchorEdge, 0f, category);
 }
示例#24
0
 /// <summary>
 /// Adds a UI constraint to this control.
 /// </summary>
 /// <param name="control">Control instance.</param>
 /// <param name="controlEdge">Control edge to constraint.</param>
 /// <param name="anchor">Anchoring control on which the control edge is constrained. If 'null', the control is anchored relative to the viewport.</param>
 /// <param name="anchorEdge">Anchor edge on which the control edge is constrained relative to.</param>
 /// <param name="category">The category this constaint belongs to.</param>
 public static void AddConstraint(this UIControl control, Edge controlEdge, UIControl anchor, Edge anchorEdge, ConstraintCategory category)
 {
     control.AddConstraint(controlEdge, anchor, anchorEdge, 0f, category);
 }
示例#25
0
        public void CheckScrollBarsVisible(ConstraintCategory category = ConstraintCategory.All)
        {
            if (mode.ContainsFlag(ScrollBarMode.Vertical))
            {
                var v = ScrollPanel.ContentPanel.size.Y > size.Y;
                if (v != VerticalScrollBar.Visible)
                {
                    VerticalScrollBar.Visible = v;
                    VerticalScrollBar.DoLayout(category);
                }
            }
            if (mode.ContainsFlag(ScrollBarMode.Horizontal))
            {
                var v = ScrollPanel.ContentPanel.size.X > size.X;
                if (v != HorizontalScrollBar.Visible)
                {
                    HorizontalScrollBar.Visible = v;
                    HorizontalScrollBar.DoLayout(category);
                }
            }

            if (mode == ScrollBarMode.Both)
            {
                var showFiller = VerticalScrollBar.Visible && HorizontalScrollBar.Visible;
                if (showFiller != fillerPanel.Visible)
                {
                    fillerPanel.Visible = showFiller;
                    base.DoLayout(category);
                }
            }
        }
示例#26
0
 public override void DoLayoutOnChildren(ConstraintCategory category = ConstraintCategory.All)
 {
     base.DoLayoutOnChildren(category);
     controls.ForEach(c => c.DoLayout(category));
 }
        /// <summary>
        /// Adds a UI constraint to this control.
        /// </summary>
        /// <param name="controlEdge">Control edge to constraint.</param>
        /// <param name="anchor">Anchoring control on which the control edge is constrained. Setting this to 'null' the control is anchored relative to the screen.</param>
        /// <param name="anchorEdge">Anchor edge, on which the control edge is constrained relative to.</param>
        /// <param name="edgeDistance">Distance between control and anchor constraint edge.</param>
        internal void AddConstraint(Edge controlEdge, UIControl anchor, Edge anchorEdge, float edgeDistance, ConstraintCategory category)
        {
            // Separate edges into their basic flags.
            // E.g. 'Edge.BottomRight' is separated into 'Edge.Bottom' and 'Edge.Right', and added as individual edges.
            List <Edge> controlEdges = new List <Edge>(4);
            List <Edge> anchorEdges  = new List <Edge>(4);

            foreach (Edge edgeType in EdgeTypes)
            {
                if (controlEdge.HasFlag(edgeType))
                {
                    controlEdges.Add(edgeType);
                }
                if (anchorEdge.HasFlag(edgeType))
                {
                    anchorEdges.Add(edgeType);
                }
            }

            if (controlEdges.Count != anchorEdges.Count)
            {
                throw new ArgumentException("There must be an equal amount of control and anchor edges.");
            }

            for (int i = 0; i < controlEdges.Count; i++)
            {
                var cEdge = controlEdges[i];
                var aEdge = anchorEdges[i];

                if (!OnSameAxis(cEdge, aEdge))
                {
                    throw new ArgumentException($"Control and anchor edge and must be on the same axis: {cEdge}, {aEdge}");
                }

                foreach (var edge in Constraints.Select(c => c.ControlEdge))
                {
                    if (edge.Equals(cEdge))
                    {
                        throw new ArgumentException($"Control edge already bound: {cEdge}");
                    }
                    if (Edge.CenterXY.ContainsFlag(cEdge | edge) && OnSameAxis(cEdge, edge))                     // Special case to check that no edge is bound to an axis along with a center edge. E.g.: CenterY and Top.
                    {
                        throw new ArgumentException($"Control axis already bound: {cEdge}, {edge}");
                    }
                }

                Constraints.Add(new UIConstraint(cEdge, anchor, aEdge, edgeDistance, category));
            }
        }
示例#28
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     base.DoLayout(category);
     CheckScrollBarsVisible(category);
     UpdateScrollBarPosition();
 }
示例#29
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     base.DoLayout(category);
     if (multiLine)
     {
         SplitText(base.Size.X);
     }
 }
示例#30
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     checkmark.Size = size;
     base.DoLayout(category);
 }
示例#31
0
 public UIConstraint(Edge controlEdge, UIControl anchor, Edge anchorEdge, float distance, ConstraintCategory constraintCategory)
 {
     this.ControlEdge = controlEdge;
     this.Anchor      = anchor;
     this.AnchorEdge  = anchorEdge;
     this.Distance    = distance;
     this.Category    = constraintCategory;
 }
示例#32
0
        internal static void EnforceConstraints(this UIControl control, ConstraintCategory category = ConstraintCategory.All)
        {
            if (control.SuspendLayout)
            {
                return;
            }

            UIManager.layoutProfiler.Start();
            EnforceConstraints(control, control.Constrainer, category);
            UIManager.layoutProfiler.Stop();
        }
示例#33
0
 public override void DoLayout(ConstraintCategory category = ConstraintCategory.All)
 {
     ScrollBar.Width = Math.Min(30, Width * 0.2f);
     base.DoLayout(category);
 }
示例#34
0
        private static void EnforceConstraints(this UIControl control, UIConstrainer constrainer, ConstraintCategory category = ConstraintCategory.All)
        {
            if (constrainer != null)
            {
                foreach (UIConstraint constraint in constrainer.Constraints)
                {
                    // Check whether the constraint should be enforced.
                    if (category != ConstraintCategory.All && !category.ContainsFlag(constraint.Category))
                    {
                        continue;
                    }

                    Edge controlEdge = constraint.ControlEdge;
                    UIControl anchor = constraint.Anchor;
                    float distance = constraint.Distance;
                    bool anchorVisible = true;

                    Vector2 pos = Vector2.Zero;
                    Vector2 anchorSize = Engine.ScreenSize;

                    if (anchor != null)
                    {
                        anchorVisible = anchor.Visible;
                        anchorSize = anchorVisible ? (anchor.Size * anchor.scale).Max(anchor.MinimumSize * anchor.scale) : Vector2.Zero;
                        if (anchor.Parent == control.Parent)    // Is anchor and control children of the same parent?
                        {
                            pos = anchor.position;              // ..then set control position relative to the anchor.
                        }

                        Vector2 scaleDelta = anchor.ScaledSize - anchor.Size;
                        pos -= anchor.NormalizedOrigin * scaleDelta;
                    }

                    Vector2 halfAnchorSize = anchorSize * 0.5f;

                    switch (constraint.AnchorEdge)
                    {
                        case Edge.Left:
                            pos.X += distance;
                            break;
                        case Edge.Top:
                            pos.Y += distance;
                            break;
                        case Edge.Right:
                            pos.X += anchorSize.X - distance;
                            break;
                        case Edge.Bottom:
                            pos.Y += anchorSize.Y - distance;
                            break;
                        case Edge.CenterX:
                            pos.X += halfAnchorSize.X + distance;
                            break;
                        case Edge.CenterY:
                            pos.Y += halfAnchorSize.Y + distance;
                            break;
                    }
                    if (!constrainer.AnchoredOpposite(controlEdge))
                    {
                        switch (controlEdge)
                        {
                            case Edge.Left:
                                control.position.X = pos.X;
                                break;
                            case Edge.Top:
                                control.position.Y = pos.Y;
                                break;
                            case Edge.Right:
                                control.position.X = pos.X - Math.Max(control.Width, control.MinimumSize.X);
                                break;
                            case Edge.Bottom:
                                control.position.Y = pos.Y - Math.Max(control.Height, control.MinimumSize.Y);
                                break;
                            case Edge.CenterX:
                                control.position.X = pos.X - Math.Max(control.HalfWidth, control.MinimumSize.X * 0.5f);
                                break;
                            case Edge.CenterY:
                                control.position.Y = pos.Y - Math.Max(control.HalfHeight, control.MinimumSize.Y * 0.5f);
                                break;
                        }
                    }
                    else
                    {
                        switch (controlEdge)
                        {
                            case Edge.Left:
                            case Edge.CenterX:
                                control.size.X += control.position.X - pos.X;
                                control.position.X = pos.X;
                                break;
                            case Edge.Top:
                            case Edge.CenterY:
                                control.size.Y += control.position.Y - pos.Y;
                                control.position.Y = pos.Y;
                                break;
                            case Edge.Right:
                                control.size.X += (pos.X - control.Width) - control.position.X;
                                if (!anchorVisible)
                                {
                                    control.size.X += anchor.size.X; // TODO: Needs proper testing
                                }
                                //control.location.X += pos.X - control.size.X;
                                break;
                            case Edge.Bottom:
                                control.size.Y += (pos.Y - control.Height) - control.position.Y;
                                if (!anchorVisible)
                                {
                                    control.size.Y += anchor.size.Y; // TODO: Needs proper testing
                                }
                                //control.location.Y += pos.Y - control.size.Y;
                                break;
                        }
                        control.UpdateTransformation = true; // Control transformation needs a recalculation when its size is updated.
                    }
                }
            }

            if (control.MinimumSize != Vector2.Zero)
            {
                control.size = control.size.Max(control.MinimumSize);
                control.UpdateTransformation = true;
            }

            _UpdateDrawPosition(control);
        }
        /// <summary>
        /// Adds a UI constraint to this control.
        /// </summary>
        /// <param name="control">Control instance.</param>
        /// <param name="controlEdge">Control edge to constraint.</param>
        /// <param name="anchor">Anchoring control on which the control edge is constrained. If 'null', the control is anchored relative to the viewport.</param>
        /// <param name="anchorEdge">Anchor edge on which the control edge is constrained relative to.</param>
        /// <param name="edgeDistance">Distance between control and anchor constraint edges.</param>
        /// <param name="category">The category this constraint belongs to.</param>
        public static void AddConstraint(this UIControl control, Edge controlEdge, UIControl anchor, Edge anchorEdge, float edgeDistance = 0, ConstraintCategory category = ConstraintCategory.All)
        {
            if (control == anchor)
            {
                throw new ArgumentException("Cannot anchor control to itself.");
            }

            //// Separate edges into their basic flags.
            //// E.g. 'Edge.BottomRight' is separated into 'Edge.Bottom' and 'Edge.Right', and added as individual edges.
            //List<Edge> controlEdges = new List<Edge>(4);
            //List<Edge> anchorEdges = new List<Edge>(4);
            //foreach (Edge edgeType in UIConstrainer.EdgeTypes)
            //{
            //	if (controlEdge.ContainsFlag(edgeType))
            //	{
            //		controlEdges.Add(edgeType);
            //	}
            //	if (anchorEdge.ContainsFlag(edgeType))
            //	{
            //		anchorEdges.Add(edgeType);
            //	}
            //}

            //if (controlEdges.Count != anchorEdges.Count)
            //{
            //	throw new ArgumentException("There must be an equal amount of control and anchor edges.");
            //}
            if (control.Constrainer == null)
            {
                control.Constrainer = new UIConstrainer();
            }
            control.Constrainer.AddConstraint(controlEdge, anchor, anchorEdge, edgeDistance, category);

            //for (int i = 0; i < controlEdges.Count; i++)
            //{
            //	control.Constrainer.AddConstraint(controlEdges[i], anchor, anchorEdges[i], edgeDistance, category);
            //}
        }
示例#36
0
        internal static void EnforceConstraints(this UIControl control, ConstraintCategory category = ConstraintCategory.All)
        {
            if (control.SuspendLayout)
            {
                return;
            }

            UIManager.layoutProfiler.Start();
            UIConstrainer constrainer = control.Constrainer;

            if (constrainer != null)
            {
                foreach (UIConstraint constraint in constrainer.Constraints)
                {
                    // Check whether the constaint should be enforced.
                    if (category != ConstraintCategory.All && !category.ContainsFlag(constraint.Category))
                    {
                        continue;
                    }

                    Edge      controlEdge = constraint.ControlEdge;
                    UIControl anchor      = constraint.Anchor;
                    float     distance    = constraint.Distance;

                    Vector2 pos        = Vector2.Zero;
                    Vector2 anchorSize = Engine.ScreenSize;

                    if (anchor != null)
                    {
                        anchorSize = anchor.Size * anchor.scale;
                        if (anchor.Parent == control.Parent)                            // Is anchor and control children of the same parent?
                        {
                            pos = anchor.location;                                      // ..then set control position relative to the anchor.
                        }

                        Vector2 scaleDelta = anchor.ScaledSize - anchor.Size;
                        pos -= anchor.NormalizedOrigin * scaleDelta;
                    }

                    Vector2 halfAnchorSize = anchorSize * 0.5f;

                    switch (constraint.AnchorEdge)
                    {
                    case Edge.Left:
                        pos.X += distance;
                        break;

                    case Edge.Top:
                        pos.Y += distance;
                        break;

                    case Edge.Right:
                        pos.X += anchorSize.X - distance;
                        break;

                    case Edge.Bottom:
                        pos.Y += anchorSize.Y - distance;
                        break;

                    case Edge.CenterX:
                        pos.X += halfAnchorSize.X + distance;
                        break;

                    case Edge.CenterY:
                        pos.Y += halfAnchorSize.Y + distance;
                        break;
                    }
                    if (!constrainer.ControlAnchoredOpposite(controlEdge))
                    {
                        switch (controlEdge)
                        {
                        case Edge.Left:
                            control.location.X = pos.X;
                            break;

                        case Edge.Top:
                            control.location.Y = pos.Y;
                            break;

                        case Edge.Right:
                            control.location.X = pos.X - control.Width;
                            break;

                        case Edge.Bottom:
                            control.location.Y = pos.Y - control.Height;
                            break;

                        case Edge.CenterX:
                            control.location.X = pos.X - control.HalfWidth;
                            break;

                        case Edge.CenterY:
                            control.location.Y = pos.Y - control.HalfHeight;
                            break;
                        }
                    }
                    else
                    {
                        switch (controlEdge)
                        {
                        case Edge.Left:
                        case Edge.CenterX:
                            control.size.X    += control.location.X - pos.X;
                            control.location.X = pos.X;
                            break;

                        case Edge.Top:
                        case Edge.CenterY:
                            control.size.Y    += control.location.Y - pos.Y;
                            control.location.Y = pos.Y;
                            break;

                        case Edge.Right:
                            control.size.X += (pos.X - control.Width) - control.location.X;
                            //control.location.X += pos.X - control.size.X;
                            break;

                        case Edge.Bottom:
                            control.size.Y += (pos.Y - control.Height) - control.location.Y;
                            //control.location.Y += pos.Y - control.size.Y;
                            break;
                        }
                    }
                }
            }

            _UpdateDrawLocation(control);
            UIManager.layoutProfiler.Stop();
        }
示例#37
0
        /// <summary>
        /// Adds a UI constraint to this control.
        /// </summary>
        /// <param name="control">Control instance.</param>
        /// <param name="controlEdge">Control edge to constraint.</param>
        /// <param name="anchor">Anchoring control on which the control edge is constrained. If 'null', the control is anchored relative to the viewport.</param>
        /// <param name="anchorEdge">Anchor edge on which the control edge is constrained relative to.</param>
        /// <param name="edgeDistance">Distance between control and anchor constraint edges.</param>
        /// <param name="category">The category this constaint belongs to.</param>
        public static void AddConstraint(this UIControl control, Edge controlEdge, UIControl anchor, Edge anchorEdge, float edgeDistance = 0, ConstraintCategory category = ConstraintCategory.All)
        {
            if (control == anchor)
            {
                throw new ArgumentException("Cannot anchor control to itself.");
            }

            List <Edge> controlEdges = new List <Edge>(4);
            List <Edge> anchorEdges  = new List <Edge>(4);

            foreach (Edge edgeType in UIConstrainer.EdgeTypes)
            {
                if (controlEdge.ContainsFlag(edgeType))
                {
                    controlEdges.Add(edgeType);
                }
                if (anchorEdge.ContainsFlag(edgeType))
                {
                    anchorEdges.Add(edgeType);
                }
            }

            if (controlEdges.Count != anchorEdges.Count)
            {
                throw new ArgumentException("There must be an equal amount of control and anchor edges.");
            }
            if (control.Constrainer == null)
            {
                control.Constrainer = new UIConstrainer();
            }

            for (int i = 0; i < controlEdges.Count; i++)
            {
                control.Constrainer.AddConstraint(controlEdges[i], anchor, anchorEdges[i], edgeDistance, category);
            }
        }