2019-08-12 19:44:16 +02:00
|
|
|
using System;
|
2019-08-21 17:00:22 +02:00
|
|
|
using System.Linq;
|
2019-08-12 19:44:16 +02:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using MLEM.Extensions;
|
2019-08-13 16:02:29 +02:00
|
|
|
using MLEM.Input;
|
2019-08-12 19:44:16 +02:00
|
|
|
using MLEM.Textures;
|
|
|
|
using MLEM.Ui.Style;
|
|
|
|
|
|
|
|
namespace MLEM.Ui.Elements {
|
|
|
|
public class ScrollBar : Element {
|
|
|
|
|
|
|
|
public NinePatch Background;
|
|
|
|
public NinePatch ScrollerTexture;
|
|
|
|
public Point ScrollerOffset;
|
|
|
|
public Point ScrollerSize;
|
|
|
|
private float maxValue;
|
|
|
|
public float MaxValue {
|
|
|
|
get => this.maxValue;
|
|
|
|
set {
|
|
|
|
this.maxValue = Math.Max(0, value);
|
|
|
|
// force current value to be clamped
|
|
|
|
this.CurrentValue = this.currValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private float currValue;
|
|
|
|
public float CurrentValue {
|
|
|
|
get => this.currValue;
|
|
|
|
set {
|
|
|
|
var val = MathHelper.Clamp(value, 0, this.maxValue);
|
|
|
|
if (this.currValue != val) {
|
|
|
|
this.currValue = val;
|
|
|
|
this.OnValueChanged?.Invoke(this, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-16 19:08:36 +02:00
|
|
|
public readonly bool Horizontal;
|
2019-08-12 19:44:16 +02:00
|
|
|
public float StepPerScroll = 1;
|
|
|
|
public ValueChanged OnValueChanged;
|
2019-08-13 16:02:29 +02:00
|
|
|
private bool isMouseHeld;
|
2019-08-12 19:44:16 +02:00
|
|
|
|
2019-08-16 19:08:36 +02:00
|
|
|
public ScrollBar(Anchor anchor, Vector2 size, int scrollerSize, float maxValue, bool horizontal = false) : base(anchor, size) {
|
2019-08-12 19:44:16 +02:00
|
|
|
this.maxValue = maxValue;
|
2019-08-16 19:08:36 +02:00
|
|
|
this.Horizontal = horizontal;
|
|
|
|
this.ScrollerSize = new Point(horizontal ? scrollerSize : size.X.Floor(), !horizontal ? scrollerSize : size.Y.Floor());
|
2019-08-12 19:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Update(GameTime time) {
|
|
|
|
base.Update(time);
|
2019-08-12 19:46:43 +02:00
|
|
|
var moused = this.System.MousedElement;
|
2019-08-16 19:08:36 +02:00
|
|
|
if (moused == this && this.Input.IsMouseButtonPressed(MouseButton.Left)) {
|
2019-08-13 16:02:29 +02:00
|
|
|
this.isMouseHeld = true;
|
|
|
|
} else if (this.isMouseHeld && this.Input.IsMouseButtonUp(MouseButton.Left)) {
|
|
|
|
this.isMouseHeld = false;
|
|
|
|
}
|
2019-08-16 19:08:36 +02:00
|
|
|
|
2019-08-13 16:02:29 +02:00
|
|
|
if (this.isMouseHeld) {
|
2019-08-16 19:08:36 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-08-13 16:02:29 +02:00
|
|
|
}
|
2019-08-16 19:08:36 +02:00
|
|
|
|
2019-08-21 17:00:22 +02:00
|
|
|
if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
|
2019-08-12 19:46:43 +02:00
|
|
|
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
|
|
|
|
if (scroll != 0)
|
|
|
|
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
|
|
|
|
}
|
2019-08-12 19:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
|
|
|
|
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);
|
2019-08-16 19:08:36 +02:00
|
|
|
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));
|
2019-08-12 19:44:16 +02:00
|
|
|
batch.Draw(this.ScrollerTexture, scrollerRect, Color.White * alpha, this.Scale);
|
|
|
|
base.Draw(time, batch, alpha, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void InitStyle(UiStyle style) {
|
|
|
|
base.InitStyle(style);
|
|
|
|
this.Background = style.ScrollBarBackground;
|
|
|
|
this.ScrollerTexture = style.ScrollBarScrollerTexture;
|
|
|
|
}
|
|
|
|
|
|
|
|
public delegate void ValueChanged(Element element, float value);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|