mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-24 13:38:34 +01:00
Compare commits
3 commits
1c48a69371
...
4966886b70
Author | SHA1 | Date | |
---|---|---|---|
4966886b70 | |||
ca89d03ca4 | |||
3648352540 |
17 changed files with 134 additions and 119 deletions
|
@ -27,6 +27,8 @@ Improvements
|
|||
- Exposed the epsilon value used by Element calculations
|
||||
- Made Image ScaleToImage take ui scale into account
|
||||
- Added style properties for a lot of hardcoded default element styles
|
||||
- Allow style properties to set style values with a higher priority, which allows elements to style their default children
|
||||
- Allow changing the entire ui style for a single element
|
||||
|
||||
Fixes
|
||||
- Fixed VerticalSpace height parameter being an integer
|
||||
|
|
|
@ -69,7 +69,7 @@ namespace Demos {
|
|||
this.UiRoot.AddChild(this.root);
|
||||
|
||||
this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a small demo for MLEM.Ui, a user interface library that is part of the MLEM Library for Extending MonoGame."));
|
||||
var image = this.root.AddChild(new Image(Anchor.AutoCenter, new Vector2(50, 50), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true, Padding = new Vector2(3)});
|
||||
var image = this.root.AddChild(new Image(Anchor.AutoCenter, new Vector2(50, 50), new TextureRegion(this.testTexture, 0, 0, 8, 8)) {IsHidden = true, Padding = new Padding(3)});
|
||||
// Setting the x or y coordinate of the size to 1 or a lower number causes the width or height to be a percentage of the parent's width or height
|
||||
// (for example, setting the size's x to 0.75 would make the element's width be 0.75*parentWidth)
|
||||
this.root.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), "Toggle Grass Image", "This button shows a grass tile above it to show the automatic anchoring of objects.") {
|
||||
|
@ -241,7 +241,7 @@ namespace Demos {
|
|||
// Note that this particular example makes use of the Coroutine package, which is not required but makes demonstration easier
|
||||
private static IEnumerator<Wait> WobbleButton(Element button) {
|
||||
var counter = 0F;
|
||||
while (counter < 4 * Math.PI) {
|
||||
while (counter < 4 * Math.PI && button.Root != null) {
|
||||
// Every element allows the implementation of any sort of custom rendering for itself and all of its child components
|
||||
// This includes simply changing the transform matrix like here, but also applying custom effects and doing
|
||||
// anything else that can be done in the SpriteBatch's Begin call.
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MLEM.Misc;
|
||||
using MLEM.Textures;
|
||||
using MLEM.Ui.Style;
|
||||
|
||||
|
@ -46,8 +47,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// Note that this is only nonnull by default if the constructor was passed a nonnull tooltip text.
|
||||
/// </summary>
|
||||
public Tooltip Tooltip;
|
||||
|
||||
private bool isDisabled;
|
||||
/// <summary>
|
||||
/// Set this property to true to mark the button as disabled.
|
||||
/// A disabled button cannot be moused over, selected or pressed.
|
||||
|
@ -72,6 +71,8 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
}
|
||||
|
||||
private bool isDisabled;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new button with the given settings
|
||||
/// </summary>
|
||||
|
@ -81,7 +82,8 @@ namespace MLEM.Ui.Elements {
|
|||
/// <param name="tooltipText">The text that should be displayed in a <see cref="Tooltip"/> when hovering over this button</param>
|
||||
public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
|
||||
if (text != null) {
|
||||
this.Text = new Paragraph(Anchor.Center, 1, text, true) {Padding = new Vector2(1)};
|
||||
this.Text = new Paragraph(Anchor.Center, 1, text, true);
|
||||
this.Text.Padding.SetFromStyle(new Padding(1), 1);
|
||||
this.AddChild(this.Text);
|
||||
}
|
||||
if (tooltipText != null)
|
||||
|
|
|
@ -37,8 +37,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The width of the space between this checkbox and its <see cref="Label"/>
|
||||
/// </summary>
|
||||
public StyleProp<float> TextOffsetX;
|
||||
|
||||
private bool checced;
|
||||
/// <summary>
|
||||
/// Whether or not this checkbox is currently checked.
|
||||
/// </summary>
|
||||
|
@ -56,6 +54,8 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public CheckStateChange OnCheckStateChange;
|
||||
|
||||
private bool checced;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new checkbox with the given settings
|
||||
/// </summary>
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace MLEM.Ui.Elements {
|
|||
/// The panel that this dropdown contains. It will be displayed upon pressing the dropdown button.
|
||||
/// </summary>
|
||||
public readonly Panel Panel;
|
||||
|
||||
/// <summary>
|
||||
/// This property stores whether the dropdown is currently opened or not
|
||||
/// </summary>
|
||||
|
|
|
@ -24,25 +24,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public static float Epsilon = 0.01F;
|
||||
|
||||
/// <summary>
|
||||
/// A list of all of this element's direct children.
|
||||
/// Use <see cref="AddChild{T}"/> or <see cref="RemoveChild"/> to manipulate this list while calling all of the necessary callbacks.
|
||||
/// </summary>
|
||||
protected readonly IList<Element> Children;
|
||||
private readonly List<Element> children = new List<Element>();
|
||||
/// <summary>
|
||||
/// A sorted version of <see cref="Children"/>. The children are sorted by their <see cref="Priority"/>.
|
||||
/// </summary>
|
||||
protected IList<Element> SortedChildren {
|
||||
get {
|
||||
this.UpdateSortedChildrenIfDirty();
|
||||
return this.sortedChildren;
|
||||
}
|
||||
}
|
||||
private bool sortedChildrenDirty;
|
||||
private IList<Element> sortedChildren;
|
||||
|
||||
private UiSystem system;
|
||||
/// <summary>
|
||||
/// The ui system that this element is currently a part of
|
||||
/// </summary>
|
||||
|
@ -51,8 +32,17 @@ namespace MLEM.Ui.Elements {
|
|||
internal set {
|
||||
this.system = value;
|
||||
this.Controls = value?.Controls;
|
||||
if (this.system != null)
|
||||
this.InitStyle(this.system.Style);
|
||||
this.Style = value?.Style;
|
||||
}
|
||||
}
|
||||
public UiStyle Style {
|
||||
get => this.style;
|
||||
set {
|
||||
if (this.style != value) {
|
||||
this.style = value;
|
||||
if (value != null)
|
||||
this.InitStyle(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
|
@ -60,10 +50,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public UiControls Controls;
|
||||
/// <summary>
|
||||
/// The input handler that this element's <see cref="Controls"/> use
|
||||
/// </summary>
|
||||
protected InputHandler Input => this.Controls.Input;
|
||||
/// <summary>
|
||||
/// This element's parent element.
|
||||
/// If this element has no parent (it is the <see cref="RootElement"/> of a ui system), this value is <c>null</c>.
|
||||
/// </summary>
|
||||
|
@ -77,8 +63,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The scale that this ui element renders with
|
||||
/// </summary>
|
||||
public float Scale => this.Root.ActualScale;
|
||||
|
||||
private Anchor anchor;
|
||||
/// <summary>
|
||||
/// The <see cref="Anchor"/> that this element uses for positioning within its parent
|
||||
/// </summary>
|
||||
|
@ -91,8 +75,6 @@ namespace MLEM.Ui.Elements {
|
|||
this.SetAreaDirty();
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 size;
|
||||
/// <summary>
|
||||
/// The size of this element, where X represents the width and Y represents the height.
|
||||
/// If the x or y value of the size is between 0 and 1, the size will be seen as a percentage of its parent's size rather than as an absolute value.
|
||||
|
@ -118,8 +100,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The <see cref="Size"/>, but with <see cref="Scale"/> applied.
|
||||
/// </summary>
|
||||
public Vector2 ScaledSize => this.size * this.Scale;
|
||||
|
||||
private Vector2 offset;
|
||||
/// <summary>
|
||||
/// This element's offset from its default position, which is dictated by its <see cref="Anchor"/>.
|
||||
/// Note that, depending on the side that the element is anchored to, this offset moves it in a different direction.
|
||||
|
@ -137,41 +117,18 @@ namespace MLEM.Ui.Elements {
|
|||
/// The <see cref="PositionOffset"/>, but with <see cref="Scale"/> applied.
|
||||
/// </summary>
|
||||
public Vector2 ScaledOffset => this.offset * this.Scale;
|
||||
|
||||
/// <summary>
|
||||
/// The padding that this element has.
|
||||
/// The padding is subtracted from the element's <see cref="Size"/>, and it is an area that the element does not extend into. This means that this element's resulting <see cref="DisplayArea"/> does not include this padding.
|
||||
/// </summary>
|
||||
public Padding Padding;
|
||||
/// <summary>
|
||||
/// The <see cref="Padding"/>, but with <see cref="Scale"/> applied.
|
||||
/// </summary>
|
||||
public Padding ScaledPadding => this.Padding * this.Scale;
|
||||
|
||||
private Padding childPadding;
|
||||
/// <summary>
|
||||
/// The child padding that this element has.
|
||||
/// The child padding moves any <see cref="Children"/> added to this element inwards by the given amount in each direction.
|
||||
/// </summary>
|
||||
public Padding ChildPadding {
|
||||
get => this.childPadding;
|
||||
set {
|
||||
if (this.childPadding == value)
|
||||
return;
|
||||
this.childPadding = value;
|
||||
this.SetAreaDirty();
|
||||
}
|
||||
}
|
||||
public Padding ScaledPadding => this.Padding.Value * this.Scale;
|
||||
/// <summary>
|
||||
/// The <see cref="ChildPadding"/>, but with <see cref="Scale"/> applied.
|
||||
/// </summary>
|
||||
public Padding ScaledChildPadding => this.childPadding * this.Scale;
|
||||
public Padding ScaledChildPadding => this.ChildPadding.Value * this.Scale;
|
||||
/// <summary>
|
||||
/// This element's current <see cref="Area"/>, but with <see cref="ScaledChildPadding"/> applied.
|
||||
/// </summary>
|
||||
public RectangleF ChildPaddedArea => this.UnscrolledArea.Shrink(this.ScaledChildPadding);
|
||||
|
||||
private RectangleF area;
|
||||
/// <summary>
|
||||
/// This element's area, without respecting its <see cref="ScrollOffset"/>.
|
||||
/// This area is updated automatically to fit this element's sizing and positioning properties.
|
||||
|
@ -182,7 +139,6 @@ namespace MLEM.Ui.Elements {
|
|||
return this.area;
|
||||
}
|
||||
}
|
||||
private bool areaDirty;
|
||||
/// <summary>
|
||||
/// The <see cref="UnscrolledArea"/> of this element, but with <see cref="ScaledScrollOffset"/> applied.
|
||||
/// </summary>
|
||||
|
@ -192,7 +148,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// This is the property that should be used for drawing this element, as well as mouse input handling and culling.
|
||||
/// </summary>
|
||||
public RectangleF DisplayArea => this.Area.Shrink(this.ScaledPadding);
|
||||
|
||||
/// <summary>
|
||||
/// The offset that this element has as a result of <see cref="Panel"/> scrolling.
|
||||
/// </summary>
|
||||
|
@ -201,8 +156,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The <see cref="ScrollOffset"/>, but with <see cref="Scale"/> applied.
|
||||
/// </summary>
|
||||
public Vector2 ScaledScrollOffset => this.ScrollOffset * this.Scale;
|
||||
|
||||
private bool isHidden;
|
||||
/// <summary>
|
||||
/// Set this property to <c>true</c> to cause this element to be hidden.
|
||||
/// Hidden elements don't receive input events, aren't rendered and don't factor into auto-anchoring.
|
||||
|
@ -216,8 +169,6 @@ namespace MLEM.Ui.Elements {
|
|||
this.SetAreaDirty();
|
||||
}
|
||||
}
|
||||
|
||||
private int priority;
|
||||
/// <summary>
|
||||
/// The priority of this element as part of its <see cref="Parent"/> element.
|
||||
/// A higher priority means the element will be drawn first and, if auto-anchoring is used, anchored higher up within its parent.
|
||||
|
@ -232,7 +183,6 @@ namespace MLEM.Ui.Elements {
|
|||
this.Parent.SetSortedChildrenDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This element's transform matrix.
|
||||
/// Can easily be scaled using <see cref="ScaleTransform"/>.
|
||||
|
@ -244,7 +194,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// Note that, when this is non-null, a new <see cref="SpriteBatch.Begin"/> call is used for this element.
|
||||
/// </summary>
|
||||
public BeginDelegate BeginImpl;
|
||||
|
||||
/// <summary>
|
||||
/// Set this field to false to disallow the element from being selected.
|
||||
/// An unselectable element is skipped by automatic navigation and its <see cref="OnSelected"/> callback will never be called.
|
||||
|
@ -294,7 +243,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// Note that, when <see cref="Draw"/> is called, this alpha value is multiplied with the <see cref="Parent"/>'s alpha value and passed down to this element's <see cref="Children"/>.
|
||||
/// </summary>
|
||||
public float DrawAlpha = 1;
|
||||
|
||||
/// <summary>
|
||||
/// Stores whether this element is currently being moused over or touched.
|
||||
/// </summary>
|
||||
|
@ -304,6 +252,30 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public bool IsSelected { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// A style property that contains the selection indicator that is displayed on this element if it is the <see cref="RootElement.SelectedElement"/>
|
||||
/// </summary>
|
||||
public StyleProp<NinePatch> SelectionIndicator;
|
||||
/// <summary>
|
||||
/// A style property that contains the sound effect that is played when this element's <see cref="OnPressed"/> is called
|
||||
/// </summary>
|
||||
public StyleProp<SoundEffectInfo> ActionSound;
|
||||
/// <summary>
|
||||
/// A style property that contains the sound effect that is played when this element's <see cref="OnSecondaryPressed"/> is called
|
||||
/// </summary>
|
||||
public StyleProp<SoundEffectInfo> SecondActionSound;
|
||||
/// <summary>
|
||||
/// The padding that this element has.
|
||||
/// The padding is subtracted from the element's <see cref="Size"/>, and it is an area that the element does not extend into. This means that this element's resulting <see cref="DisplayArea"/> does not include this padding.
|
||||
/// </summary>
|
||||
public StyleProp<Padding> Padding;
|
||||
/// <summary>
|
||||
/// The child padding that this element has.
|
||||
/// The child padding moves any <see cref="Children"/> added to this element inwards by the given amount in each direction.
|
||||
/// When setting this style after this element has already been added to a ui, <see cref="SetAreaDirty"/> should be called.
|
||||
/// </summary>
|
||||
public StyleProp<Padding> ChildPadding;
|
||||
|
||||
/// <summary>
|
||||
/// Event that is called after this element is drawn, but before its children are drawn
|
||||
/// </summary>
|
||||
|
@ -347,7 +319,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// <summary>
|
||||
/// Event that is called when text input is made.
|
||||
/// When an element uses this event, it should call <see cref="MlemPlatform.EnsureExists"/> on construction to ensure that the MLEM platform is initialized.
|
||||
///
|
||||
/// Note that this event is called for every element, even if it is not selected.
|
||||
/// Also note that if the active <see cref="MlemPlatform"/> uses an on-screen keyboard, this event is never called.
|
||||
/// </summary>
|
||||
|
@ -396,17 +367,36 @@ namespace MLEM.Ui.Elements {
|
|||
public GenericCallback OnDisposed;
|
||||
|
||||
/// <summary>
|
||||
/// A style property that contains the selection indicator that is displayed on this element if it is the <see cref="RootElement.SelectedElement"/>
|
||||
/// A list of all of this element's direct children.
|
||||
/// Use <see cref="AddChild{T}"/> or <see cref="RemoveChild"/> to manipulate this list while calling all of the necessary callbacks.
|
||||
/// </summary>
|
||||
public StyleProp<NinePatch> SelectionIndicator;
|
||||
protected readonly IList<Element> Children;
|
||||
/// <summary>
|
||||
/// A style property that contains the sound effect that is played when this element's <see cref="OnPressed"/> is called
|
||||
/// A sorted version of <see cref="Children"/>. The children are sorted by their <see cref="Priority"/>.
|
||||
/// </summary>
|
||||
public StyleProp<SoundEffectInfo> ActionSound;
|
||||
protected IList<Element> SortedChildren {
|
||||
get {
|
||||
this.UpdateSortedChildrenIfDirty();
|
||||
return this.sortedChildren;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// A style property that contains the sound effect that is played when this element's <see cref="OnSecondaryPressed"/> is called
|
||||
/// The input handler that this element's <see cref="Controls"/> use
|
||||
/// </summary>
|
||||
public StyleProp<SoundEffectInfo> SecondActionSound;
|
||||
protected InputHandler Input => this.Controls.Input;
|
||||
|
||||
private readonly List<Element> children = new List<Element>();
|
||||
private bool sortedChildrenDirty;
|
||||
private IList<Element> sortedChildren;
|
||||
private UiSystem system;
|
||||
private Anchor anchor;
|
||||
private Vector2 size;
|
||||
private Vector2 offset;
|
||||
private RectangleF area;
|
||||
private bool areaDirty;
|
||||
private bool isHidden;
|
||||
private int priority;
|
||||
private UiStyle style;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new element with the given anchor and size and sets up some default event reactions.
|
||||
|
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
using MLEM.Input;
|
||||
using MLEM.Misc;
|
||||
using MLEM.Textures;
|
||||
|
||||
namespace MLEM.Ui.Elements {
|
||||
|
@ -22,7 +23,8 @@ namespace MLEM.Ui.Elements {
|
|||
/// <returns>An image button</returns>
|
||||
public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float imagePadding = 2) {
|
||||
var button = new Button(anchor, size, text, tooltipText);
|
||||
var image = new Image(Anchor.CenterLeft, Vector2.One, texture) {Padding = new Vector2(imagePadding)};
|
||||
var image = new Image(Anchor.CenterLeft, Vector2.One, texture);
|
||||
image.Padding.SetFromStyle(new Padding(imagePadding), 1);
|
||||
button.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale;
|
||||
button.AddChild(image, 0);
|
||||
return button;
|
||||
|
|
|
@ -16,7 +16,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The color to render the image at
|
||||
/// </summary>
|
||||
public StyleProp<Color> Color;
|
||||
private TextureRegion texture;
|
||||
/// <summary>
|
||||
/// A callback to retrieve the <see cref="TextureRegion"/> that this image should render.
|
||||
/// This can be used if the image changes frequently.
|
||||
|
@ -40,7 +39,6 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
}
|
||||
}
|
||||
private bool scaleToImage;
|
||||
/// <summary>
|
||||
/// Whether this image element's <see cref="Element.Size"/> should be based on the size of the <see cref="TextureRegion"/> given.
|
||||
/// Note that, when scaling to the image's size, the <see cref="Element.Scale"/> is also taken into account.
|
||||
|
@ -73,6 +71,9 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public float ImageRotation;
|
||||
|
||||
private bool scaleToImage;
|
||||
private TextureRegion texture;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new image with the given settings
|
||||
/// </summary>
|
||||
|
|
|
@ -17,6 +17,13 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public class Panel : Element {
|
||||
|
||||
/// <summary>
|
||||
/// The scroll bar that this panel contains.
|
||||
/// This is only nonnull if <see cref="scrollOverflow"/> is true.
|
||||
/// Note that some scroll bar styling is controlled by this panel, namely <see cref="StepPerScroll"/> and <see cref="ScrollerSize"/>.
|
||||
/// </summary>
|
||||
public readonly ScrollBar ScrollBar;
|
||||
|
||||
/// <summary>
|
||||
/// The texture that this panel should have, or null if it should be invisible.
|
||||
/// </summary>
|
||||
|
@ -35,15 +42,11 @@ namespace MLEM.Ui.Elements {
|
|||
/// The size that the <see cref="ScrollBar"/>'s scroller should have, in pixels
|
||||
/// </summary>
|
||||
public StyleProp<Vector2> ScrollerSize;
|
||||
/// <summary>
|
||||
/// The scroll bar that this panel contains.
|
||||
/// This is only nonnull if <see cref="scrollOverflow"/> is true.
|
||||
/// Note that some scroll bar styling is controlled by this panel, namely <see cref="StepPerScroll"/> and <see cref="ScrollerSize"/>.
|
||||
/// </summary>
|
||||
public readonly ScrollBar ScrollBar;
|
||||
private readonly bool scrollOverflow;
|
||||
private RenderTarget2D renderTarget;
|
||||
|
||||
private readonly List<Element> relevantChildren = new List<Element>();
|
||||
private readonly bool scrollOverflow;
|
||||
|
||||
private RenderTarget2D renderTarget;
|
||||
private bool relevantChildrenDirty;
|
||||
|
||||
/// <summary>
|
||||
|
@ -68,8 +71,12 @@ namespace MLEM.Ui.Elements {
|
|||
AutoHideWhenEmpty = autoHideScrollbar,
|
||||
IsHidden = autoHideScrollbar
|
||||
};
|
||||
if (autoHideScrollbar)
|
||||
this.ScrollBar.OnAutoHide += e => this.ChildPadding += new Padding(0, this.ScrollerSize.Value.X, 0, 0) * (e.IsHidden ? -1 : 1);
|
||||
if (autoHideScrollbar) {
|
||||
this.ScrollBar.OnAutoHide += e => {
|
||||
this.ChildPadding += new Padding(0, this.ScrollerSize.Value.X, 0, 0) * (e.IsHidden ? -1 : 1);
|
||||
this.SetAreaDirty();
|
||||
};
|
||||
}
|
||||
|
||||
// handle automatic element selection, the scroller needs to scroll to the right location
|
||||
this.OnSelectedElementChanged += (element, otherElement) => {
|
||||
|
@ -213,8 +220,7 @@ namespace MLEM.Ui.Elements {
|
|||
this.Texture.SetFromStyle(style.PanelTexture);
|
||||
this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll);
|
||||
this.ScrollerSize.SetFromStyle(style.PanelScrollerSize);
|
||||
if (this.ChildPadding == default)
|
||||
this.ChildPadding = style.PanelChildPadding;
|
||||
this.ChildPadding.SetFromStyle(style.PanelChildPadding);
|
||||
this.SetScrollBarStyle();
|
||||
}
|
||||
|
||||
|
@ -232,7 +238,7 @@ namespace MLEM.Ui.Elements {
|
|||
var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden);
|
||||
// the max value of the scrollbar is the amount of non-scaled pixels taken up by overflowing components
|
||||
var childrenHeight = lowestChild.Area.Bottom - firstChild.Area.Top;
|
||||
this.ScrollBar.MaxValue = (childrenHeight - this.Area.Height) / this.Scale + this.ChildPadding.Height;
|
||||
this.ScrollBar.MaxValue = (childrenHeight - this.Area.Height) / this.Scale + this.ChildPadding.Value.Height;
|
||||
|
||||
// update the render target
|
||||
var targetArea = (Rectangle) this.GetRenderTargetArea();
|
||||
|
|
|
@ -25,7 +25,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The tokenized version of the <see cref="Text"/>
|
||||
/// </summary>
|
||||
public TokenizedString TokenizedText { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The color that the text will be rendered with
|
||||
/// </summary>
|
||||
|
|
|
@ -34,7 +34,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The color that this progress bar's <see cref="ProgressTexture"/> is rendered with.
|
||||
/// </summary>
|
||||
public StyleProp<Color> ProgressColor;
|
||||
|
||||
/// <summary>
|
||||
/// The direction that this progress bar goes in.
|
||||
/// Note that only <see cref="Direction2Helper.Adjacent"/> directions are supported.
|
||||
|
@ -44,7 +43,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The maximum value that this progress bar should be able to have.
|
||||
/// </summary>
|
||||
public float MaxValue;
|
||||
private float currentValue;
|
||||
/// <summary>
|
||||
/// The current value that this progress bar has.
|
||||
/// This value is always between 0 and <see cref="MaxValue"/>.
|
||||
|
@ -54,6 +52,8 @@ namespace MLEM.Ui.Elements {
|
|||
set => this.currentValue = MathHelper.Clamp(value, 0, this.MaxValue);
|
||||
}
|
||||
|
||||
private float currentValue;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new progress bar with the given settings
|
||||
/// </summary>
|
||||
|
|
|
@ -17,6 +17,11 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public class ScrollBar : Element {
|
||||
|
||||
/// <summary>
|
||||
/// Whether this scroll bar is horizontal
|
||||
/// </summary>
|
||||
public readonly bool Horizontal;
|
||||
|
||||
/// <summary>
|
||||
/// The background texture for this scroll bar
|
||||
/// </summary>
|
||||
|
@ -33,7 +38,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// The scroller's width and height
|
||||
/// </summary>
|
||||
public Vector2 ScrollerSize;
|
||||
private float maxValue;
|
||||
/// <summary>
|
||||
/// The max value that this scroll bar should be able to scroll to.
|
||||
/// Note that the maximum value does not change the height of the scroll bar.
|
||||
|
@ -52,8 +56,6 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
}
|
||||
}
|
||||
private float scrollAdded;
|
||||
private float currValue;
|
||||
/// <summary>
|
||||
/// The current value of the scroll bar.
|
||||
/// This is between 0 and <see cref="MaxValue"/> at all times.
|
||||
|
@ -71,10 +73,6 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Whether this scroll bar is horizontal
|
||||
/// </summary>
|
||||
public readonly bool Horizontal;
|
||||
/// <summary>
|
||||
/// The amount added or removed from <see cref="CurrentValue"/> per single movement of the scroll wheel
|
||||
/// </summary>
|
||||
public float StepPerScroll = 1;
|
||||
|
@ -90,9 +88,6 @@ namespace MLEM.Ui.Elements {
|
|||
/// This property is true while the user scrolls on the scroll bar using the mouse or touch input
|
||||
/// </summary>
|
||||
public bool IsBeingScrolled => this.isMouseHeld || this.isDragging || this.isTouchHeld;
|
||||
private bool isMouseHeld;
|
||||
private bool isDragging;
|
||||
private bool isTouchHeld;
|
||||
/// <summary>
|
||||
/// This field determines if this scroll bar should automatically be hidden from a <see cref="Panel"/> if there aren't enough children to allow for scrolling.
|
||||
/// </summary>
|
||||
|
@ -107,6 +102,13 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public StyleProp<float> SmoothScrollFactor;
|
||||
|
||||
private bool isMouseHeld;
|
||||
private bool isDragging;
|
||||
private bool isTouchHeld;
|
||||
private float maxValue;
|
||||
private float scrollAdded;
|
||||
private float currValue;
|
||||
|
||||
static ScrollBar() {
|
||||
InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.VerticalDrag);
|
||||
}
|
||||
|
|
|
@ -79,10 +79,9 @@ namespace MLEM.Ui.Elements {
|
|||
this.Texture.SetFromStyle(style.TooltipBackground);
|
||||
this.MouseOffset.SetFromStyle(style.TooltipOffset);
|
||||
this.Delay.SetFromStyle(style.TooltipDelay);
|
||||
this.ChildPadding = style.TooltipChildPadding;
|
||||
this.ChildPadding.SetFromStyle(style.TooltipChildPadding);
|
||||
if (this.Paragraph != null) {
|
||||
// we can't set from style here since it's a different element
|
||||
this.Paragraph.TextColor.Set(style.TooltipTextColor);
|
||||
this.Paragraph.TextColor.SetFromStyle(style.TooltipTextColor, 1);
|
||||
this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,14 +14,14 @@ namespace MLEM.Ui.Style {
|
|||
/// The currently applied style
|
||||
/// </summary>
|
||||
public T Value { get; private set; }
|
||||
private bool isCustom;
|
||||
private byte lastSetPriority;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new style property with the given custom style.
|
||||
/// </summary>
|
||||
/// <param name="value">The custom style to apply</param>
|
||||
public StyleProp(T value) {
|
||||
this.isCustom = true;
|
||||
this.lastSetPriority = byte.MaxValue;
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
|
@ -30,9 +30,12 @@ namespace MLEM.Ui.Style {
|
|||
/// This allows this property to be overridden by custom style settings using <see cref="Set"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The style to apply</param>
|
||||
public void SetFromStyle(T value) {
|
||||
if (!this.isCustom)
|
||||
/// <param name="priority">The priority that this style value has. Higher priority style values will override lower priority style values.</param>
|
||||
public void SetFromStyle(T value, byte priority = 0) {
|
||||
if (priority >= this.lastSetPriority) {
|
||||
this.Value = value;
|
||||
this.lastSetPriority = priority;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -41,7 +44,7 @@ namespace MLEM.Ui.Style {
|
|||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
public void Set(T value) {
|
||||
this.isCustom = true;
|
||||
this.lastSetPriority = byte.MaxValue;
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
|
@ -64,7 +67,7 @@ namespace MLEM.Ui.Style {
|
|||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() {
|
||||
return $"{this.Value} (Custom: {this.isCustom})";
|
||||
return this.Value?.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -52,6 +52,14 @@ namespace MLEM.Misc {
|
|||
this.Bottom = bottom;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new padding with the specified value, which will be applied to each edge.
|
||||
/// </summary>
|
||||
/// <param name="value">The padding to apply to each edge</param>
|
||||
public Padding(float value) :
|
||||
this(value, value) {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new padding with the specified x and y values, applying them to both edges.
|
||||
/// </summary>
|
||||
|
|
|
@ -224,7 +224,7 @@ namespace Sandbox {
|
|||
var button = loadPanel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) {
|
||||
SetHeightBasedOnChildren = true,
|
||||
Padding = new Padding(0, 0, 0, 1),
|
||||
ChildPadding = new Vector2(3)
|
||||
ChildPadding = new Padding(3)
|
||||
});
|
||||
button.AddChild(new Group(Anchor.AutoLeft, new Vector2(0.5F, 30), false) {
|
||||
CanBeMoused = false
|
||||
|
|
|
@ -49,7 +49,7 @@ namespace Tests {
|
|||
var button = panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) {
|
||||
SetHeightBasedOnChildren = true,
|
||||
Padding = new Padding(0, 0, 0, 1),
|
||||
ChildPadding = new Vector2(3)
|
||||
ChildPadding = new Padding(3)
|
||||
});
|
||||
button.AddChild(new Group(Anchor.AutoLeft, new Vector2(0.5F, 30), false) {
|
||||
CanBeMoused = false
|
||||
|
|
Loading…
Reference in a new issue