From 4e1c6d81287d7544f8f5c2ba7e210d07ce5ee02a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 16 Aug 2019 19:08:36 +0200 Subject: [PATCH] added sliders (horizontal scroll bars) --- Demos/UiDemo.cs | 5 +++++ MLEM.Ui/Elements/Paragraph.cs | 22 ++++++++++++++++++++-- MLEM.Ui/Elements/ScrollBar.cs | 29 +++++++++++++++++++---------- MLEM.Ui/Elements/Slider.cs | 11 +++++++++++ 4 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 MLEM.Ui/Elements/Slider.cs diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index e6d0f0f..7f7fbc3 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Coroutine; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; using MLEM.Font; using MLEM.Input; using MLEM.Startup; @@ -121,6 +122,10 @@ namespace Demos { tooltip.IsHidden = !tooltip.IsHidden; } }); + + var slider = new Slider(Anchor.AutoLeft, new Vector2(1, 10), 5, 1); + root.AddChild(new Paragraph(Anchor.AutoLeft, 1, paragraph => "Slider is at " + (slider.CurrentValue * 100).Floor() + "%") {PositionOffset = new Vector2(0, 1)}); + root.AddChild(slider); } protected override void DoDraw(GameTime gameTime) { diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index cd96d69..3027b1e 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -25,11 +26,20 @@ namespace MLEM.Ui.Elements { public string Text { get => this.text; set { - this.text = value; - this.SetAreaDirty(); + if (this.text != value) { + this.text = value; + this.SetAreaDirty(); + } } } public bool AutoAdjustWidth; + public TextCallback GetTextCallback; + + public Paragraph(Anchor anchor, float width, TextCallback textCallback, bool centerText = false, IGenericFont font = null) + : this(anchor, width, "", centerText, font) { + this.GetTextCallback = textCallback; + this.Text = textCallback(this); + } public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) { this.text = text; @@ -54,6 +64,12 @@ namespace MLEM.Ui.Elements { return new Point(this.AutoAdjustWidth ? this.longestLineLength.Ceil() + this.ScaledPadding.X * 2 : size.X, (this.lineHeight * this.splitText.Length).Ceil() + this.ScaledPadding.Y * 2); } + public override void Update(GameTime time) { + base.Update(time); + if (this.GetTextCallback != null) + this.Text = this.GetTextCallback(this); + } + public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) { if (this.Background != null) batch.Draw(this.Background, this.Area.OffsetCopy(offset), this.BackgroundColor * alpha); @@ -77,6 +93,8 @@ namespace MLEM.Ui.Elements { this.font = style.Font; } + public delegate string TextCallback(Paragraph paragraph); + } } \ No newline at end of file diff --git a/MLEM.Ui/Elements/ScrollBar.cs b/MLEM.Ui/Elements/ScrollBar.cs index 74660f0..b88b86b 100644 --- a/MLEM.Ui/Elements/ScrollBar.cs +++ b/MLEM.Ui/Elements/ScrollBar.cs @@ -33,30 +33,37 @@ namespace MLEM.Ui.Elements { } } } + public readonly bool Horizontal; public float StepPerScroll = 1; public ValueChanged OnValueChanged; private bool isMouseHeld; - public ScrollBar(Anchor anchor, Vector2 size, int scrollerHeight, float maxValue) : base(anchor, size) { + public ScrollBar(Anchor anchor, Vector2 size, int scrollerSize, float maxValue, bool horizontal = false) : base(anchor, size) { this.maxValue = maxValue; - this.ScrollerSize = new Point(size.X.Floor(), scrollerHeight); + this.Horizontal = horizontal; + this.ScrollerSize = new Point(horizontal ? scrollerSize : size.X.Floor(), !horizontal ? scrollerSize : size.Y.Floor()); } public override void Update(GameTime time) { base.Update(time); var moused = this.System.MousedElement; - if (moused == this && this.Input.IsMouseButtonDown(MouseButton.Left)) { + if (moused == this && this.Input.IsMouseButtonPressed(MouseButton.Left)) { this.isMouseHeld = true; } else if (this.isMouseHeld && this.Input.IsMouseButtonUp(MouseButton.Left)) { this.isMouseHeld = false; } - + if (this.isMouseHeld) { - var internalY = this.MousePos.Y - this.Area.Y; - this.CurrentValue = internalY / (float) this.Area.Height * this.MaxValue; + if (this.Horizontal) { + var internalX = this.MousePos.X - this.Area.X; + this.CurrentValue = internalX / (float) this.Area.Width * this.MaxValue; + } else { + var internalY = this.MousePos.Y - this.Area.Y; + this.CurrentValue = internalY / (float) this.Area.Height * this.MaxValue; + } } - - if (moused == this.Parent || moused?.Parent == this.Parent) { + + if (!this.Horizontal && (moused == this.Parent || moused?.Parent == this.Parent)) { var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel; if (scroll != 0) this.CurrentValue += this.StepPerScroll * Math.Sign(scroll); @@ -67,8 +74,10 @@ namespace MLEM.Ui.Elements { batch.Draw(this.Background, this.DisplayArea.OffsetCopy(offset), Color.White * alpha, this.Scale); var scrollerPos = new Point(this.DisplayArea.X + offset.X + this.ScrollerOffset.X, this.DisplayArea.Y + offset.Y + this.ScrollerOffset.Y); - var scrollerYOffset = (this.currValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale)).Floor(); - var scrollerRect = new Rectangle(scrollerPos + new Point(0, scrollerYOffset), this.ScrollerSize.Multiply(this.Scale)); + var scrollerOffset = new Point( + !this.Horizontal ? 0 : (this.currValue / this.maxValue * (this.DisplayArea.Width - this.ScrollerSize.X * this.Scale)).Floor(), + this.Horizontal ? 0 : (this.currValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale)).Floor()); + var scrollerRect = new Rectangle(scrollerPos + scrollerOffset, this.ScrollerSize.Multiply(this.Scale)); batch.Draw(this.ScrollerTexture, scrollerRect, Color.White * alpha, this.Scale); base.Draw(time, batch, alpha, offset); } diff --git a/MLEM.Ui/Elements/Slider.cs b/MLEM.Ui/Elements/Slider.cs new file mode 100644 index 0000000..af01204 --- /dev/null +++ b/MLEM.Ui/Elements/Slider.cs @@ -0,0 +1,11 @@ +using Microsoft.Xna.Framework; + +namespace MLEM.Ui.Elements { + public class Slider : ScrollBar { + + public Slider(Anchor anchor, Vector2 size, int scrollerSize, float maxValue) : + base(anchor, size, scrollerSize, maxValue, true) { + } + + } +} \ No newline at end of file