private void TopLeftAnchor(AnchorPen pen, AnchorLayoutControl control) { // Check if reached limit. if (pen.Left + control.Control.Width >= pen.WidthLimit) { pen.Left = Padding.X; pen.Top = pen.NeededTop; } // Apply left margin. pen.Left += control.Margin.X; // Apply top margin. float topPenCopy = pen.Top + control.Margin.Y; // Position at top left. control.Control.X = pen.Left; control.Control.Y = topPenCopy; // Apply right margin for the next control. pen.Left += control.Control.Width + control.Margin.Width; // Set the max position of the top pen. float neededPenTop = control.Control.Height + topPenCopy + control.Margin.Height; if (neededPenTop > pen.NeededTop) { pen.NeededTop = neededPenTop; } }
private void BottomRightAnchor(AnchorPen pen, AnchorLayoutControl control) { // Check if reached limit. if (pen.Left - control.Control.Width - control.Margin.Width < pen.WidthLimit) { pen.Left = pen.Holder; pen.Top = pen.NeededTop; } // Apply right margin and size. pen.Left -= control.Margin.Width + control.Control.Width; // Apply bottom margin and height. float topPenCopy = pen.Top - control.Margin.Height - control.Control.Height; // Position at top left. control.Control.X = pen.Left; control.Control.Y = topPenCopy; // Apply left margin for the next control. pen.Left -= control.Margin.X; // Set the max position of the top pen. float neededPenTop = topPenCopy - control.Margin.Y; if (neededPenTop < pen.NeededTop) { pen.NeededTop = neededPenTop; } }
/// <summary> /// Add a control to the corner anchor. /// </summary> /// <param name="transform">The control to add.</param> /// <param name="anchor">The corner of the screen to anchor the control to.</param> /// <param name="margin">The margin of the control.</param> public void AddChild(Transform transform, AnchorLocation anchor, Rectangle margin) { AnchorLayoutControl anchorControl = new AnchorLayoutControl { Control = transform, Anchor = anchor, Margin = margin }; lock (_controls) { _controls.Add(anchorControl); } // Hook up to event. transform.OnResize += _updateEvent; ApplyLogic(); base.AddChild(transform); }
/// <summary> /// Remove a control from the corner anchor. If not found nothing will happen. /// </summary> /// <param name="transform">The control to remove.</param> public override void RemoveChild(Transform transform) { lock (_controls) { AnchorLayoutControl match = _controls.FirstOrDefault(x => x.Control == transform); if (match != null) { _controls.Remove(match); } else { return; } } // Unhook from event. transform.OnResize -= _updateEvent; ApplyLogic(); base.RemoveChild(transform); }