From 36204e40cbb8160b24074f8350454baa3426ecae Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 26 Sep 2019 22:16:21 +0200 Subject: [PATCH] turn some points into vectors for added precision --- Demos/UiDemo.cs | 2 +- MLEM.Ui/Elements/Element.cs | 14 +++++++------- MLEM.Ui/Elements/ElementHelper.cs | 4 ++-- MLEM.Ui/Elements/Panel.cs | 6 +++--- MLEM.Ui/Elements/Tooltip.cs | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 9903270..edcbe25 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -72,7 +72,7 @@ namespace Demos { this.UiSystem.Add("Test", root); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a small demo for MLEM.Ui, a user interface library that is part of (M)LEM (L)ibrary by (E)llpeck for (M)onoGame.")); - var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(50, 50), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true, Padding = new Point(3)}); + var image = root.AddChild(new Image(Anchor.AutoCenter, new Vector2(50, 50), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true, Padding = new Vector2(3)}); // Setting the x or y coordinate of the size to 1 or a lower number causes the width or height to be a percentage of the parent's width or height // (for example, setting the size's x to 0.75 would make the element's width be 0.75*parentWidth) root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Toggle Test Image", "This button shows a grass tile as a test image to show the automatic anchoring of objects.") { diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index fc33673..129c31d 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -25,9 +25,9 @@ namespace MLEM.Ui.Elements { private Anchor anchor; private Vector2 size; private Vector2 offset; - public Point Padding; - public Point ScaledPadding => this.Padding.Multiply(this.Scale); - private Point childPadding; + public Vector2 Padding; + public Point ScaledPadding => (this.Padding * this.Scale).ToPoint(); + private Vector2 childPadding; public Anchor Anchor { get => this.anchor; set { @@ -57,7 +57,7 @@ namespace MLEM.Ui.Elements { } } public Point ScaledOffset => (this.offset * this.Scale).ToPoint(); - public Point ChildPadding { + public Vector2 ChildPadding { get => this.childPadding; set { if (this.childPadding == value) @@ -67,7 +67,7 @@ namespace MLEM.Ui.Elements { } } public Rectangle ChildPaddedArea => this.UnscrolledArea.Shrink(this.ScaledChildPadding); - public Point ScaledChildPadding => this.childPadding.Multiply(this.Scale); + public Point ScaledChildPadding => (this.childPadding * this.Scale).ToPoint(); public DrawCallback OnDrawn; public TimeCallback OnUpdated; @@ -125,8 +125,8 @@ namespace MLEM.Ui.Elements { } } public Rectangle Area => this.UnscrolledArea.OffsetCopy(this.ScaledScrollOffset); - public Point ScrollOffset; - public Point ScaledScrollOffset => this.ScrollOffset.Multiply(this.Scale); + public Vector2 ScrollOffset; + public Point ScaledScrollOffset => (this.ScrollOffset * this.Scale).ToPoint(); public Rectangle DisplayArea => this.Area.Shrink(this.ScaledPadding); private int priority; public int Priority { diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 55c1527..45f74b6 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -5,9 +5,9 @@ using MLEM.Textures; namespace MLEM.Ui.Elements { public static class ElementHelper { - public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float tooltipWidth = 50, int imagePadding = 2) { + public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float tooltipWidth = 50, float imagePadding = 2) { var button = new Button(anchor, size, text, tooltipText, tooltipWidth); - var image = new Image(Anchor.CenterLeft, Vector2.One, texture) {Padding = new Point(imagePadding)}; + var image = new Image(Anchor.CenterLeft, Vector2.One, texture) {Padding = new Vector2(imagePadding)}; button.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale; button.AddChild(image, 0); return button; diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 015be78..0d6ed28 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -22,7 +22,7 @@ namespace MLEM.Ui.Elements { this.PositionOffset = positionOffset; this.SetHeightBasedOnChildren = setHeightBasedOnChildren; this.scrollOverflow = scrollOverflow; - this.ChildPadding = new Point(5); + this.ChildPadding = new Vector2(5); this.CanBeSelected = false; if (scrollOverflow) { @@ -37,7 +37,7 @@ namespace MLEM.Ui.Elements { // modify the padding so that the scroll bar isn't over top of something else this.ScrollBar.PositionOffset -= new Vector2(scrollSize.X + 1, 0); - this.ChildPadding += new Point(scrollSize.X, 0); + this.ChildPadding += new Vector2(scrollSize.X, 0); // handle automatic element selection, the scroller needs to scroll to the right location this.OnSelectedElementChanged += (element, otherElement) => { @@ -92,7 +92,7 @@ namespace MLEM.Ui.Elements { return; var offset = -this.ScrollBar.CurrentValue.Floor(); foreach (var child in this.GetChildren(c => c != this.ScrollBar, true)) - child.ScrollOffset = new Point(0, offset); + child.ScrollOffset = new Vector2(0, offset); this.relevantChildrenDirty = true; } diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index 3b34b11..785abc5 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -12,7 +12,7 @@ namespace MLEM.Ui.Elements { public Tooltip(float width, string text, Element elementToHover = null) : base(Anchor.TopLeft, width, text) { this.AutoAdjustWidth = true; - this.Padding = new Point(2); + this.Padding = new Vector2(2); this.CanBeSelected = false; if (elementToHover != null) {