1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

added smooth scrolling

This commit is contained in:
Ellpeck 2020-03-07 22:09:11 +01:00
parent bc18d4db15
commit 76cb94bb34
2 changed files with 17 additions and 4 deletions

View file

@ -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."));

View file

@ -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);
}