1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 05:58:35 +01:00

Compare commits

..

No commits in common. "91d34c0a83ce57d36e56d2e17e5898732038917a" and "cd48ad6e2ac01a95ce183ea9cc5c95c9e9f946c5" have entirely different histories.

2 changed files with 18 additions and 24 deletions

View file

@ -31,16 +31,12 @@ Improvements
- Allow changing the entire ui style for a single element - Allow changing the entire ui style for a single element
- Skip unnecessary area updates for elements with dirty parents - Skip unnecessary area updates for elements with dirty parents
- Calculate panel scroll bar height based on content height - Calculate panel scroll bar height based on content height
- Remember the location that a scroll bar scroller was grabbed in when scrolling
Fixes Fixes
- Fixed VerticalSpace height parameter being an integer - Fixed VerticalSpace height parameter being an integer
- Fixed text not being pasted into a text field at all if it contains characters that don't match the input rule - Fixed text not being pasted into a text field at all if it contains characters that don't match the input rule
- Fixed panels that don't auto-hide their scroll bars ignoring their width for child padding - Fixed panels that don't auto-hide their scroll bars ignoring their width for child padding
Removals
- Removed ScrollBar ScrollerOffset (which didn't actually work)
### MLEM.Data ### MLEM.Data
Additions Additions
- Allow RuntimeTexturePacker to automatically dispose submitted textures when packing - Allow RuntimeTexturePacker to automatically dispose submitted textures when packing

View file

@ -3,6 +3,7 @@ using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch; using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Extensions;
using MLEM.Input; using MLEM.Input;
using MLEM.Misc; using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
@ -30,6 +31,10 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public StyleProp<NinePatch> ScrollerTexture; public StyleProp<NinePatch> ScrollerTexture;
/// <summary> /// <summary>
/// The <see cref="ScrollerTexture"/>'s offset from the calculated position of the scroller. Use this to pad the scroller.
/// </summary>
public Vector2 ScrollerOffset;
/// <summary>
/// The scroller's width and height /// The scroller's width and height
/// </summary> /// </summary>
public Vector2 ScrollerSize; public Vector2 ScrollerSize;
@ -88,13 +93,6 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public bool AutoHideWhenEmpty; public bool AutoHideWhenEmpty;
/// <summary> /// <summary>
/// The position that this scroll bar's scroller is currently at.
/// This value takes the <see cref="Element.DisplayArea"/>, as well as the <see cref="Element.Scale"/> into account.
/// </summary>
public Vector2 ScrollerPosition => this.DisplayArea.Location + new Vector2(
!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));
/// <summary>
/// Whether smooth scrolling should be enabled for this scroll bar. /// Whether smooth scrolling should be enabled for this scroll bar.
/// Smooth scrolling causes the <see cref="CurrentValue"/> to change gradually rather than instantly when scrolling. /// Smooth scrolling causes the <see cref="CurrentValue"/> to change gradually rather than instantly when scrolling.
/// </summary> /// </summary>
@ -110,7 +108,6 @@ namespace MLEM.Ui.Elements {
private float maxValue; private float maxValue;
private float scrollAdded; private float scrollAdded;
private float currValue; private float currValue;
private Vector2 scrollStartOffset;
static ScrollBar() { static ScrollBar() {
InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.VerticalDrag); InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.VerticalDrag);
@ -137,15 +134,13 @@ namespace MLEM.Ui.Elements {
// MOUSE INPUT // MOUSE INPUT
var moused = this.Controls.MousedElement; var moused = this.Controls.MousedElement;
var mousedPos = Vector2.Transform(this.Input.MousePosition.ToVector2(), this.Root.InvTransform);
if (moused == this && this.Controls.Input.IsMouseButtonPressed(MouseButton.Left)) { if (moused == this && this.Controls.Input.IsMouseButtonPressed(MouseButton.Left)) {
this.isMouseHeld = true; this.isMouseHeld = true;
this.scrollStartOffset = mousedPos - this.ScrollerPosition;
} else if (this.isMouseHeld && !this.Controls.Input.IsMouseButtonDown(MouseButton.Left)) { } else if (this.isMouseHeld && !this.Controls.Input.IsMouseButtonDown(MouseButton.Left)) {
this.isMouseHeld = false; this.isMouseHeld = false;
} }
if (this.isMouseHeld) if (this.isMouseHeld)
this.ScrollToPos(mousedPos); this.ScrollToPos(this.Input.MousePosition.Transform(this.Root.InvTransform));
if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) { if (!this.Horizontal && moused != null && (moused == this.Parent || moused.GetParentTree().Contains(this.Parent))) {
var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel; var scroll = this.Input.LastScrollWheel - this.Input.ScrollWheel;
if (scroll != 0) if (scroll != 0)
@ -177,12 +172,11 @@ namespace MLEM.Ui.Elements {
// if we just started touching and are on top of the scroller, then we should start scrolling // if we just started touching and are on top of the scroller, then we should start scrolling
if (this.DisplayArea.Contains(pos) && !loc.TryGetPreviousLocation(out _)) { if (this.DisplayArea.Contains(pos) && !loc.TryGetPreviousLocation(out _)) {
this.isTouchHeld = true; this.isTouchHeld = true;
this.scrollStartOffset = pos - this.ScrollerPosition;
break; break;
} }
// scroll no matter if we're on the scroller right now // scroll no matter if we're on the scroller right now
if (this.isTouchHeld) if (this.isTouchHeld)
this.ScrollToPos(pos); this.ScrollToPos(pos.ToPoint());
} }
} }
@ -194,22 +188,26 @@ namespace MLEM.Ui.Elements {
} }
} }
private void ScrollToPos(Vector2 position) { private void ScrollToPos(Point position) {
var (width, height) = this.ScrollerSize * this.Scale;
if (this.Horizontal) { if (this.Horizontal) {
var offset = this.scrollStartOffset.X >= 0 && this.scrollStartOffset.X <= width ? this.scrollStartOffset.X : width / 2; var internalX = position.X - this.Area.X - this.ScrollerSize.X * this.Scale / 2;
this.CurrentValue = (position.X - this.Area.X - offset) / (this.Area.Width - width) * this.MaxValue; this.CurrentValue = internalX / (this.Area.Width - this.ScrollerSize.X * this.Scale) * this.MaxValue;
} else { } else {
var offset = this.scrollStartOffset.Y >= 0 && this.scrollStartOffset.Y <= height ? this.scrollStartOffset.Y : height / 2; var internalY = position.Y - this.Area.Y - this.ScrollerSize.Y * this.Scale / 2;
this.CurrentValue = (position.Y - this.Area.Y - offset) / (this.Area.Height - height) * this.MaxValue; this.CurrentValue = internalY / (this.Area.Height - this.ScrollerSize.Y * this.Scale) * this.MaxValue;
} }
} }
/// <inheritdoc /> /// <inheritdoc />
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
batch.Draw(this.Background, this.DisplayArea, Color.White * alpha, this.Scale); batch.Draw(this.Background, this.DisplayArea, Color.White * alpha, this.Scale);
if (this.MaxValue > 0) { if (this.MaxValue > 0) {
var scrollerRect = new RectangleF(this.ScrollerPosition, this.ScrollerSize * this.Scale); var scrollerPos = new Vector2(this.DisplayArea.X + this.ScrollerOffset.X, this.DisplayArea.Y + this.ScrollerOffset.Y);
var scrollerOffset = new Vector2(
!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); batch.Draw(this.ScrollerTexture, scrollerRect, Color.White * alpha, this.Scale);
} }
base.Draw(time, batch, alpha, blendState, samplerState, matrix); base.Draw(time, batch, alpha, blendState, samplerState, matrix);