mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 22:18:34 +01:00
added sliders (horizontal scroll bars)
This commit is contained in:
parent
7cf031478d
commit
4e1c6d8128
4 changed files with 55 additions and 12 deletions
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using Coroutine;
|
using Coroutine;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
using MLEM.Extensions;
|
||||||
using MLEM.Font;
|
using MLEM.Font;
|
||||||
using MLEM.Input;
|
using MLEM.Input;
|
||||||
using MLEM.Startup;
|
using MLEM.Startup;
|
||||||
|
@ -121,6 +122,10 @@ namespace Demos {
|
||||||
tooltip.IsHidden = !tooltip.IsHidden;
|
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) {
|
protected override void DoDraw(GameTime gameTime) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
@ -25,11 +26,20 @@ namespace MLEM.Ui.Elements {
|
||||||
public string Text {
|
public string Text {
|
||||||
get => this.text;
|
get => this.text;
|
||||||
set {
|
set {
|
||||||
this.text = value;
|
if (this.text != value) {
|
||||||
this.SetAreaDirty();
|
this.text = value;
|
||||||
|
this.SetAreaDirty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public bool AutoAdjustWidth;
|
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)) {
|
public Paragraph(Anchor anchor, float width, string text, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
|
||||||
this.text = text;
|
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);
|
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) {
|
public override void Draw(GameTime time, SpriteBatch batch, float alpha, Point offset) {
|
||||||
if (this.Background != null)
|
if (this.Background != null)
|
||||||
batch.Draw(this.Background, this.Area.OffsetCopy(offset), this.BackgroundColor * alpha);
|
batch.Draw(this.Background, this.Area.OffsetCopy(offset), this.BackgroundColor * alpha);
|
||||||
|
@ -77,6 +93,8 @@ namespace MLEM.Ui.Elements {
|
||||||
this.font = style.Font;
|
this.font = style.Font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public delegate string TextCallback(Paragraph paragraph);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -33,30 +33,37 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public readonly bool Horizontal;
|
||||||
public float StepPerScroll = 1;
|
public float StepPerScroll = 1;
|
||||||
public ValueChanged OnValueChanged;
|
public ValueChanged OnValueChanged;
|
||||||
private bool isMouseHeld;
|
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.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) {
|
public override void Update(GameTime time) {
|
||||||
base.Update(time);
|
base.Update(time);
|
||||||
var moused = this.System.MousedElement;
|
var moused = this.System.MousedElement;
|
||||||
if (moused == this && this.Input.IsMouseButtonDown(MouseButton.Left)) {
|
if (moused == this && this.Input.IsMouseButtonPressed(MouseButton.Left)) {
|
||||||
this.isMouseHeld = true;
|
this.isMouseHeld = true;
|
||||||
} else if (this.isMouseHeld && this.Input.IsMouseButtonUp(MouseButton.Left)) {
|
} else if (this.isMouseHeld && this.Input.IsMouseButtonUp(MouseButton.Left)) {
|
||||||
this.isMouseHeld = false;
|
this.isMouseHeld = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isMouseHeld) {
|
if (this.isMouseHeld) {
|
||||||
var internalY = this.MousePos.Y - this.Area.Y;
|
if (this.Horizontal) {
|
||||||
this.CurrentValue = internalY / (float) this.Area.Height * this.MaxValue;
|
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;
|
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
|
||||||
if (scroll != 0)
|
if (scroll != 0)
|
||||||
this.CurrentValue += this.StepPerScroll * Math.Sign(scroll);
|
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);
|
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 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 scrollerOffset = new Point(
|
||||||
var scrollerRect = new Rectangle(scrollerPos + new Point(0, scrollerYOffset), this.ScrollerSize.Multiply(this.Scale));
|
!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);
|
batch.Draw(this.ScrollerTexture, scrollerRect, Color.White * alpha, this.Scale);
|
||||||
base.Draw(time, batch, alpha, offset);
|
base.Draw(time, batch, alpha, offset);
|
||||||
}
|
}
|
||||||
|
|
11
MLEM.Ui/Elements/Slider.cs
Normal file
11
MLEM.Ui/Elements/Slider.cs
Normal file
|
@ -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) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue