From 76cb94bb3445846f8f265ce7c2671b01fa873327 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 7 Mar 2020 22:09:11 +0100 Subject: [PATCH] added smooth scrolling --- Demos/UiDemo.cs | 1 + MLEM.Ui/Elements/ScrollBar.cs | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index b11d105..54f579a 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -70,6 +70,7 @@ namespace Demos { // create the root panel that all the other components sit on and add it to the ui system var root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true, new Point(5, 10)); + root.ScrollBar.SmoothScrolling = true; 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.")); diff --git a/MLEM.Ui/Elements/ScrollBar.cs b/MLEM.Ui/Elements/ScrollBar.cs index 94499a8..8a8a5a3 100644 --- a/MLEM.Ui/Elements/ScrollBar.cs +++ b/MLEM.Ui/Elements/ScrollBar.cs @@ -22,19 +22,22 @@ namespace MLEM.Ui.Elements { set { this.maxValue = Math.Max(0, value); // force current value to be clamped - this.CurrentValue = this.currValue; + this.CurrentValue = this.CurrentValue; if (this.AutoHideWhenEmpty && this.IsHidden != this.maxValue <= 0) { this.IsHidden = this.maxValue <= 0; this.OnAutoHide?.Invoke(this); } } } + private float scrollAdded; private float currValue; public float CurrentValue { - get => this.currValue; + get => this.currValue - this.scrollAdded; set { var val = MathHelper.Clamp(value, 0, this.maxValue); if (this.currValue != val) { + if (this.SmoothScrolling) + this.scrollAdded = val - this.currValue; this.currValue = val; this.OnValueChanged?.Invoke(this, val); } @@ -48,6 +51,8 @@ namespace MLEM.Ui.Elements { private bool isDragging; private bool isTouchHeld; public bool AutoHideWhenEmpty; + public bool SmoothScrolling; + public float SmoothScrollFactor = 0.75F; static ScrollBar() { InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.VerticalDrag); @@ -110,6 +115,13 @@ namespace MLEM.Ui.Elements { this.ScrollToPos(pos.ToPoint()); } } + + if (this.SmoothScrolling && this.scrollAdded != 0) { + this.scrollAdded *= this.SmoothScrollFactor; + if (Math.Abs(this.scrollAdded) <= 0.1F) + this.scrollAdded = 0; + this.OnValueChanged?.Invoke(this, this.CurrentValue); + } } private void ScrollToPos(Point position) { @@ -128,8 +140,8 @@ namespace MLEM.Ui.Elements { if (this.MaxValue > 0) { var scrollerPos = new Vector2(this.DisplayArea.X + this.ScrollerOffset.X, this.DisplayArea.Y + this.ScrollerOffset.Y); var scrollerOffset = new Vector2( - !this.Horizontal ? 0 : this.currValue / this.maxValue * (this.DisplayArea.Width - this.ScrollerSize.X * this.Scale), - this.Horizontal ? 0 : this.currValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale)); + !this.Horizontal ? 0 : this.CurrentValue / this.maxValue * (this.DisplayArea.Width - this.ScrollerSize.X * this.Scale), + this.Horizontal ? 0 : this.CurrentValue / this.maxValue * (this.DisplayArea.Height - this.ScrollerSize.Y * this.Scale)); var scrollerRect = new RectangleF(scrollerPos + scrollerOffset, this.ScrollerSize * this.Scale); batch.Draw(this.ScrollerTexture, scrollerRect, Color.White * alpha, this.Scale); }