Пример #1
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));
            }
        }