Beispiel #1
0
        /// <summary>
        /// It gets the height of the parent (if it has one) and replaces the widget's height with it, then updates the spaces of its children.
        /// </summary>
        /// <param name="widget"></param>
        public void ApplyOn(IWidget widget)
        {
            ApplicationDone = false;
            if (widget.IsRoot)
            {
                return;
            }
            var(x, y, w, _) = widget.Space;

            if (!widget.Props.Contains <HorizontalStretch>() && widget.Children.Any())
            {
                w = widget.Children.Max(c => c.Space.Width);
            }

            var(nh, nonFinishedHorSiblings) = ApplyUtils.StretchedHeightUsingHeightSiblings(widget);
            nonFinishedHorSiblings.ForEach(s =>
            {
                var hp      = (IApplicableProp)s.Props.GetByProp <HorizontalStretch>().First();
                hp.Applied += (_, _) =>
                {
                    ApplicationDone = false;
                    WidgetsSpaceHelper.UpdateSpace(widget, new RectangleF(x, y, w, nh - s.Space.Height));
                    ApplicationDone = true;
                };
            });

            WidgetsSpaceHelper.UpdateSpace(widget,
                                           new RectangleF(x, y, w, nh - widget.Margins.Top - widget.Margins.Bottom));
            ApplicationDone = nonFinishedHorSiblings.Count == 0;
            OnApplied();
        }
Beispiel #2
0
        private static void IncreaseSpaceWithChildren(IWidget widgetNode)
        {
            float w = 0, h = 0;

            foreach (var c in widgetNode.Children)
            {
                var(x, y, _, _)   = widgetNode.Space;
                var(_, _, cw, ch) = c.Space;
                w += cw;
                h += ch;
                if (widgetNode.IsRoot == false)
                {
                    var(_, _, parentW, parentH) = widgetNode.Parent !.Space;
                    if (w > parentW)
                    {
                        w -= cw;
                    }
                    if (h > parentH)
                    {
                        h -= ch;
                    }
                }

                WidgetsSpaceHelper.UpdateSpace(widgetNode, new RectangleF(x, y, w, h));
            }
        }
Beispiel #3
0
        /// <summary>
        /// It gets the width of the parent (if it has one) and replaces the widget's width with it, then updates the spaces of its children.
        /// </summary>
        /// <param name="widget"></param>
        public void ApplyOn(IWidget widget)
        {
            ApplicationDone = false;
            if (widget.IsRoot)
            {
                return;
            }
            var(x, y, _, h) = widget.Space;

            if (!widget.Props.Contains <VerticalStretch>() && widget.Children.Any())
            {
                h = widget.Children.Max(c => c.TotalSpaceOccupied.Height);
            }

            var(nw, nonFinishedHorSiblings) = ApplyUtils.StretchedWidthUsingWidthSiblings(widget);
            nonFinishedHorSiblings.ForEach(s =>
            {
                var hp      = (IApplicableProp)s.Props.GetByProp <HorizontalStretch>().First();
                hp.Applied += (_, _) =>
                {
                    ApplicationDone = false;
                    WidgetsSpaceHelper.UpdateSpace(widget, new RectangleF(x, y, nw - s.Space.Width, h));
                    ApplicationDone = true;
                };
            });
            WidgetsSpaceHelper.UpdateSpace(widget,
                                           new RectangleF(x, y, nw - widget.Margins.Left - widget.Margins.Right, h));
            ApplicationDone = true;
            OnApplied();
        }
Beispiel #4
0
        public static void ItemCenterHorizontal(IWidget wTree, List <WidgetsDataSubList> wLists)
        {
            var children   = wTree.Children;
            var totalWidth = wLists.Sum(l => l.Width);
            var startX     = CenterCoord(wTree.Space.X, wTree.Space.Width, totalWidth);
            var accX       = wTree.Margins.Left / 2;

            wLists.ForEach(l =>
            {
                l.X   = startX + accX;
                accX += l.Width;
            });
            wLists.ForEach(l =>
            {
                for (var i = l.FirstWidgetIndex; i < l.LastWidgetIndex; i++)
                {
                    var ith         = children.ElementAt(i);
                    var x           = l.X;
                    var(_, y, w, h) = ith.Space;
                    if (Math.Abs(ith.Space.Width - l.Width) > 0.01f)
                    {
                        x = CenterCoord(l.X, l.Width, w);
                    }

                    WidgetsSpaceHelper.UpdateSpace(ith, new RectangleF(x, y, w, h));
                }
            });
        }
Beispiel #5
0
        public static void ItemCenterVertical(IWidget widgetTree, List <WidgetsDataSubList> wLists)
        {
            var children    = widgetTree.Children;
            var totalHeight = wLists.Sum(l => l.Height);
            var startY      = CenterCoord(widgetTree.Space.Y, widgetTree.Space.Height,
                                          totalHeight);
            var accY = widgetTree.Margins.Top / 2;

            wLists.ForEach(l =>
            {
                l.Y   = startY + accY;
                accY += l.Height;
            });
            wLists.ForEach(l =>
            {
                for (var i = l.FirstWidgetIndex; i < l.LastWidgetIndex; i++)
                {
                    var ith         = children.ElementAt(i);
                    var y           = l.Y;
                    var(x, _, w, h) = ith.Space;
                    if (Math.Abs(ith.Space.Height - l.Height) > 0.01f)
                    {
                        y = CenterCoord(l.Y, l.Height, h);
                    }

                    WidgetsSpaceHelper.UpdateSpace(ith, new RectangleF(x, y, w, h));
                }
            });
        }
Beispiel #6
0
        /// <summary>
        /// Adds margin space to the widget. It updates the space required by the widget and applies the changes to all the children.
        /// </summary>
        /// <param name="widget"></param>
        public void ApplyOn(IWidget widget)
        {
            ApplicationDone = false;

            var(x, y, w, h) = widget.Space;
            widget.Margins  = AddMargin(widget);

            var newSpace = new RectangleF(x + _marginValues.Left, y + _marginValues.Top, w, h);

            WidgetsSpaceHelper.UpdateSpace(widget, newSpace);

            ApplicationDone = true;
            OnApplied();
        }
Beispiel #7
0
        private static void DivideSpaceEvenly(float start, float parentSize, List <IWidget> widgets,
                                              Func <IWidget, float> getSize, Func <IWidget, float, Vector2> updateLoc)
        {
            float startPoint   = start;
            float usedPixels   = widgets.Sum(getSize);
            float freePixels   = parentSize - usedPixels;
            float spaceBetween = freePixels / (widgets.Count - 1.0f);

            widgets.ForEach(w =>
            {
                WidgetsSpaceHelper.UpdateSpace(w, new RectangleF(updateLoc(w, start), w.Space.Size));
                startPoint += getSize(w) + spaceBetween;
                start       = (int)Math.Round(startPoint);
            });
        }
Beispiel #8
0
        /// <summary>
        /// Gets the children of the widget and expands the space enough to accomodate them,
        /// depending on if the parent is Row or Column.
        /// </summary>
        /// <param name="widget"></param>
        public void ApplyOn(IWidget widget)
        {
            ApplicationDone = false;
            IncreaseSpaceWithChildren(widget);

            var(x, y, _, _) = widget.Space;

            var maybeRow = ApplyUtils.ExtractRowProp(widget);

            if (maybeRow.TryGetValue(out var row))
            {
                row.Applied += (sender, args) =>
                {
                    WidgetsSpaceHelper.UpdateSpace(widget,
                                                   new RectangleF(
                                                       x,
                                                       y,
                                                       row.Rows.Max(r => r.Width),
                                                       row.Rows.Sum(r => r.Height)));
                };
            }
            else
            {
                var maybeCol = ApplyUtils.ExtractColumnProp(widget);
                if (maybeCol.TryGetValue(out var col))
                {
                    col.Applied += (sender, args) =>
                    {
                        WidgetsSpaceHelper.UpdateSpace(widget,
                                                       new RectangleF(
                                                           x,
                                                           y,
                                                           col.Columns.Sum(c => c.Width),
                                                           col.Columns.Sum(c => c.Height)));
                    };
                }
                else
                {
                    Log.Error(
                        "Flex can only be applied to a Row or Column Widget! Make sure the widget you applied Flex to has a Row or Column Prop");
                    throw new IncompatibleWidgetException(
                              "Tried to apply Flex to a widget without a Row or Column Prop");
                }
            }

            ApplicationDone = true;
            OnApplied();
        }
Beispiel #9
0
 private static Action <List <WidgetsDataSubList> > PutAtEnd(IWidget wTree, Func <IWidget, float> getSize,
                                                             Func <float, IWidget, Vector2> updateLoc, Func <IWidget, float> getMargin)
 {
     return(wLists =>
            wLists.ForEach(r =>
     {
         var acc = getSize(wTree);
         for (var i = r.LastWidgetIndex - 1; i >= r.FirstWidgetIndex; i--)
         {
             var child = wTree.Children.ElementAt(i);
             acc -= getSize(child) - getMargin(child);
             WidgetsSpaceHelper.UpdateSpace(child, new RectangleF(updateLoc(acc, child),
                                                                  child.Space.Size));
             acc -= getMargin(child);
         }
     }));
 }
Beispiel #10
0
        private static void CenterChildrenVertically(IWidget tree, List <WidgetsDataSubList> lists)
        {
            var children = tree.Children;

            lists.ForEach(w =>
            {
                var yAcc = w.Y;
                for (var i = w.FirstWidgetIndex; i < w.LastWidgetIndex; i++)
                {
                    var ith = children.ElementAt(i);
                    var wX  = w.X + ith.Margins.Left;
                    yAcc   += ith.Margins.Top;
                    WidgetsSpaceHelper.UpdateSpace(ith, new RectangleF(wX, yAcc,
                                                                       ith.Space.Width, ith.Space.Height));
                    yAcc += ith.Space.Height + ith.Margins.Bottom;
                }
            });
        }
Beispiel #11
0
        private static void CenterChildrenHorizontally(IWidget tree, List <WidgetsDataSubList> lists)
        {
            var children = tree.Children;

            lists.ForEach(w =>
            {
                var xRow = w.X;
                for (var i = w.FirstWidgetIndex; i < w.LastWidgetIndex; i++)
                {
                    var ith = children.ElementAt(i);
                    xRow   += ith.Margins.Left;
                    var wY  = w.Y + ith.Margins.Top;
                    WidgetsSpaceHelper.UpdateSpace(ith, new RectangleF(xRow, wY,
                                                                       ith.Space.Width, ith.Space.Height));
                    xRow += ith.Space.Width + ith.Margins.Right;
                }
            });
        }
Beispiel #12
0
        private void Dragging(IWidget widget, bool isXFixed, bool isYFixed, DragCtx dragCtx)
        {
            var devLoc = _device.GetPointedLocation();
            var devX   = devLoc.X;
            var devY   = devLoc.Y;

            var(x, y, w, h) = widget.Space;
            if (Math.Abs(dragCtx.DevX - devX) < 0.01f && Math.Abs(dragCtx.DevY - devY) < 0.01f)
            {
                return;
            }

            var nx = x + (isXFixed ? 0 : devX - dragCtx.DevX);
            var ny = y + (isYFixed ? 0 : devY - dragCtx.DevY);

            (nx, ny) = CheckBounds(widget, nx, ny, isXFixed, isYFixed);

            WidgetsSpaceHelper.UpdateSpace(widget, new RectangleF(nx, ny, w, h));
            dragCtx.Set(devLoc);
        }
Beispiel #13
0
        private static Action <List <WidgetsDataSubList> > PutAtBase(IWidget wTree,
                                                                     Func <WidgetsDataSubList, float> listSize, Func <IWidget, float> wSize,
                                                                     Func <float, IWidget, Vector2> updateLoc)
        {
            return(wLists =>
            {
                var acc = wSize(wTree);
                for (var i = wLists.Count - 1; i >= 0; i--)
                {
                    var list = wLists[i];
                    for (var j = list.FirstWidgetIndex; j < list.LastWidgetIndex; j++)
                    {
                        var child = wTree.Children.ElementAt(j);
                        var newCoord = acc - wSize(child);
                        WidgetsSpaceHelper.UpdateSpace(child, new RectangleF(updateLoc(newCoord, child),
                                                                             child.Space.Size));
                    }

                    acc -= listSize(list);
                }
            });
        }