From f56b7fbeff91494629d64ffb6d91a388d914aeb8 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 13 Aug 2019 16:02:29 +0200 Subject: [PATCH] made scroll bar clickable --- MLEM.Ui/Elements/Element.cs | 2 +- MLEM.Ui/Elements/ScrollBar.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 2b83e55..c5ab6ef 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -93,7 +93,7 @@ namespace MLEM.Ui.Elements { protected InputHandler Input => this.System.InputHandler; public RootElement Root { get; private set; } public float Scale => this.Root.ActualScale; - public Vector2 MousePos => this.Input.MousePosition.ToVector2(); + public Point MousePos => this.Input.MousePosition; public Element Parent { get; private set; } public bool IsMouseOver { get; private set; } public bool IsSelected { get; private set; } diff --git a/MLEM.Ui/Elements/ScrollBar.cs b/MLEM.Ui/Elements/ScrollBar.cs index 3463c07..e805341 100644 --- a/MLEM.Ui/Elements/ScrollBar.cs +++ b/MLEM.Ui/Elements/ScrollBar.cs @@ -2,6 +2,7 @@ using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Extensions; +using MLEM.Input; using MLEM.Textures; using MLEM.Ui.Style; @@ -35,6 +36,7 @@ namespace MLEM.Ui.Elements { } public float StepPerScroll = 1; public ValueChanged OnValueChanged; + private bool isMouseHeld; public ScrollBar(Anchor anchor, Vector2 size, int scrollerHeight, float maxValue) : base(anchor, size) { this.maxValue = maxValue; @@ -44,6 +46,17 @@ namespace MLEM.Ui.Elements { public override void Update(GameTime time) { base.Update(time); var moused = this.System.MousedElement; + if (moused == this && this.Input.IsMouseButtonDown(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 (moused == this.Parent || moused?.Parent == this.Parent) { var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel; if (scroll != 0)