1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-24 21:48:35 +01:00

Compare commits

...

3 commits

17 changed files with 134 additions and 119 deletions

View file

@ -27,6 +27,8 @@ Improvements
- Exposed the epsilon value used by Element calculations - Exposed the epsilon value used by Element calculations
- Made Image ScaleToImage take ui scale into account - Made Image ScaleToImage take ui scale into account
- Added style properties for a lot of hardcoded default element styles - 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 Fixes
- Fixed VerticalSpace height parameter being an integer - Fixed VerticalSpace height parameter being an integer

View file

@ -69,7 +69,7 @@ namespace Demos {
this.UiRoot.AddChild(this.root); 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.")); 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 // 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) // (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.") { 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 // 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) { private static IEnumerator<Wait> WobbleButton(Element button) {
var counter = 0F; 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 // 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 // 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. // anything else that can be done in the SpriteBatch's Begin call.

View file

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
using MLEM.Ui.Style; 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. /// Note that this is only nonnull by default if the constructor was passed a nonnull tooltip text.
/// </summary> /// </summary>
public Tooltip Tooltip; public Tooltip Tooltip;
private bool isDisabled;
/// <summary> /// <summary>
/// Set this property to true to mark the button as disabled. /// Set this property to true to mark the button as disabled.
/// A disabled button cannot be moused over, selected or pressed. /// A disabled button cannot be moused over, selected or pressed.
@ -72,6 +71,8 @@ namespace MLEM.Ui.Elements {
} }
} }
private bool isDisabled;
/// <summary> /// <summary>
/// Creates a new button with the given settings /// Creates a new button with the given settings
/// </summary> /// </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> /// <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) { public Button(Anchor anchor, Vector2 size, string text = null, string tooltipText = null) : base(anchor, size) {
if (text != null) { 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); this.AddChild(this.Text);
} }
if (tooltipText != null) if (tooltipText != null)

View file

@ -37,8 +37,6 @@ namespace MLEM.Ui.Elements {
/// The width of the space between this checkbox and its <see cref="Label"/> /// The width of the space between this checkbox and its <see cref="Label"/>
/// </summary> /// </summary>
public StyleProp<float> TextOffsetX; public StyleProp<float> TextOffsetX;
private bool checced;
/// <summary> /// <summary>
/// Whether or not this checkbox is currently checked. /// Whether or not this checkbox is currently checked.
/// </summary> /// </summary>
@ -56,6 +54,8 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public CheckStateChange OnCheckStateChange; public CheckStateChange OnCheckStateChange;
private bool checced;
/// <summary> /// <summary>
/// Creates a new checkbox with the given settings /// Creates a new checkbox with the given settings
/// </summary> /// </summary>

View file

@ -13,6 +13,7 @@ namespace MLEM.Ui.Elements {
/// The panel that this dropdown contains. It will be displayed upon pressing the dropdown button. /// The panel that this dropdown contains. It will be displayed upon pressing the dropdown button.
/// </summary> /// </summary>
public readonly Panel Panel; public readonly Panel Panel;
/// <summary> /// <summary>
/// This property stores whether the dropdown is currently opened or not /// This property stores whether the dropdown is currently opened or not
/// </summary> /// </summary>

View file

@ -24,25 +24,6 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public static float Epsilon = 0.01F; 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> /// <summary>
/// The ui system that this element is currently a part of /// The ui system that this element is currently a part of
/// </summary> /// </summary>
@ -51,8 +32,17 @@ namespace MLEM.Ui.Elements {
internal set { internal set {
this.system = value; this.system = value;
this.Controls = value?.Controls; this.Controls = value?.Controls;
if (this.system != null) this.Style = value?.Style;
this.InitStyle(this.system.Style); }
}
public UiStyle Style {
get => this.style;
set {
if (this.style != value) {
this.style = value;
if (value != null)
this.InitStyle(value);
}
} }
} }
/// <summary> /// <summary>
@ -60,10 +50,6 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public UiControls Controls; public UiControls Controls;
/// <summary> /// <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. /// 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>. /// If this element has no parent (it is the <see cref="RootElement"/> of a ui system), this value is <c>null</c>.
/// </summary> /// </summary>
@ -77,8 +63,6 @@ namespace MLEM.Ui.Elements {
/// The scale that this ui element renders with /// The scale that this ui element renders with
/// </summary> /// </summary>
public float Scale => this.Root.ActualScale; public float Scale => this.Root.ActualScale;
private Anchor anchor;
/// <summary> /// <summary>
/// The <see cref="Anchor"/> that this element uses for positioning within its parent /// The <see cref="Anchor"/> that this element uses for positioning within its parent
/// </summary> /// </summary>
@ -91,8 +75,6 @@ namespace MLEM.Ui.Elements {
this.SetAreaDirty(); this.SetAreaDirty();
} }
} }
private Vector2 size;
/// <summary> /// <summary>
/// The size of this element, where X represents the width and Y represents the height. /// 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. /// 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. /// The <see cref="Size"/>, but with <see cref="Scale"/> applied.
/// </summary> /// </summary>
public Vector2 ScaledSize => this.size * this.Scale; public Vector2 ScaledSize => this.size * this.Scale;
private Vector2 offset;
/// <summary> /// <summary>
/// This element's offset from its default position, which is dictated by its <see cref="Anchor"/>. /// 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. /// 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. /// The <see cref="PositionOffset"/>, but with <see cref="Scale"/> applied.
/// </summary> /// </summary>
public Vector2 ScaledOffset => this.offset * this.Scale; 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> /// <summary>
/// The <see cref="Padding"/>, but with <see cref="Scale"/> applied. /// The <see cref="Padding"/>, but with <see cref="Scale"/> applied.
/// </summary> /// </summary>
public Padding ScaledPadding => this.Padding * this.Scale; public Padding ScaledPadding => this.Padding.Value * 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();
}
}
/// <summary> /// <summary>
/// The <see cref="ChildPadding"/>, but with <see cref="Scale"/> applied. /// The <see cref="ChildPadding"/>, but with <see cref="Scale"/> applied.
/// </summary> /// </summary>
public Padding ScaledChildPadding => this.childPadding * this.Scale; public Padding ScaledChildPadding => this.ChildPadding.Value * this.Scale;
/// <summary> /// <summary>
/// This element's current <see cref="Area"/>, but with <see cref="ScaledChildPadding"/> applied. /// This element's current <see cref="Area"/>, but with <see cref="ScaledChildPadding"/> applied.
/// </summary> /// </summary>
public RectangleF ChildPaddedArea => this.UnscrolledArea.Shrink(this.ScaledChildPadding); public RectangleF ChildPaddedArea => this.UnscrolledArea.Shrink(this.ScaledChildPadding);
private RectangleF area;
/// <summary> /// <summary>
/// This element's area, without respecting its <see cref="ScrollOffset"/>. /// This element's area, without respecting its <see cref="ScrollOffset"/>.
/// This area is updated automatically to fit this element's sizing and positioning properties. /// 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; return this.area;
} }
} }
private bool areaDirty;
/// <summary> /// <summary>
/// The <see cref="UnscrolledArea"/> of this element, but with <see cref="ScaledScrollOffset"/> applied. /// The <see cref="UnscrolledArea"/> of this element, but with <see cref="ScaledScrollOffset"/> applied.
/// </summary> /// </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. /// This is the property that should be used for drawing this element, as well as mouse input handling and culling.
/// </summary> /// </summary>
public RectangleF DisplayArea => this.Area.Shrink(this.ScaledPadding); public RectangleF DisplayArea => this.Area.Shrink(this.ScaledPadding);
/// <summary> /// <summary>
/// The offset that this element has as a result of <see cref="Panel"/> scrolling. /// The offset that this element has as a result of <see cref="Panel"/> scrolling.
/// </summary> /// </summary>
@ -201,8 +156,6 @@ namespace MLEM.Ui.Elements {
/// The <see cref="ScrollOffset"/>, but with <see cref="Scale"/> applied. /// The <see cref="ScrollOffset"/>, but with <see cref="Scale"/> applied.
/// </summary> /// </summary>
public Vector2 ScaledScrollOffset => this.ScrollOffset * this.Scale; public Vector2 ScaledScrollOffset => this.ScrollOffset * this.Scale;
private bool isHidden;
/// <summary> /// <summary>
/// Set this property to <c>true</c> to cause this element to be hidden. /// 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. /// 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(); this.SetAreaDirty();
} }
} }
private int priority;
/// <summary> /// <summary>
/// The priority of this element as part of its <see cref="Parent"/> element. /// 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. /// 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(); this.Parent.SetSortedChildrenDirty();
} }
} }
/// <summary> /// <summary>
/// This element's transform matrix. /// This element's transform matrix.
/// Can easily be scaled using <see cref="ScaleTransform"/>. /// 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. /// Note that, when this is non-null, a new <see cref="SpriteBatch.Begin"/> call is used for this element.
/// </summary> /// </summary>
public BeginDelegate BeginImpl; public BeginDelegate BeginImpl;
/// <summary> /// <summary>
/// Set this field to false to disallow the element from being selected. /// 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. /// 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"/>. /// 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> /// </summary>
public float DrawAlpha = 1; public float DrawAlpha = 1;
/// <summary> /// <summary>
/// Stores whether this element is currently being moused over or touched. /// Stores whether this element is currently being moused over or touched.
/// </summary> /// </summary>
@ -304,6 +252,30 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public bool IsSelected { get; protected set; } 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> /// <summary>
/// Event that is called after this element is drawn, but before its children are drawn /// Event that is called after this element is drawn, but before its children are drawn
/// </summary> /// </summary>
@ -347,7 +319,6 @@ namespace MLEM.Ui.Elements {
/// <summary> /// <summary>
/// Event that is called when text input is made. /// 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. /// 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. /// 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. /// Also note that if the active <see cref="MlemPlatform"/> uses an on-screen keyboard, this event is never called.
/// </summary> /// </summary>
@ -396,17 +367,36 @@ namespace MLEM.Ui.Elements {
public GenericCallback OnDisposed; public GenericCallback OnDisposed;
/// <summary> /// <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> /// </summary>
public StyleProp<NinePatch> SelectionIndicator; protected readonly IList<Element> Children;
/// <summary> /// <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> /// </summary>
public StyleProp<SoundEffectInfo> ActionSound; protected IList<Element> SortedChildren {
get {
this.UpdateSortedChildrenIfDirty();
return this.sortedChildren;
}
}
/// <summary> /// <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> /// </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> /// <summary>
/// Creates a new element with the given anchor and size and sets up some default event reactions. /// Creates a new element with the given anchor and size and sets up some default event reactions.

View file

@ -2,6 +2,7 @@ using System;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Input; using MLEM.Input;
using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
namespace MLEM.Ui.Elements { namespace MLEM.Ui.Elements {
@ -22,7 +23,8 @@ namespace MLEM.Ui.Elements {
/// <returns>An image button</returns> /// <returns>An image button</returns>
public static Button ImageButton(Anchor anchor, Vector2 size, TextureRegion texture, string text = null, string tooltipText = null, float imagePadding = 2) { 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 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.OnAreaUpdated += e => image.Size = new Vector2(e.Area.Height, e.Area.Height) / e.Scale;
button.AddChild(image, 0); button.AddChild(image, 0);
return button; return button;

View file

@ -16,7 +16,6 @@ namespace MLEM.Ui.Elements {
/// The color to render the image at /// The color to render the image at
/// </summary> /// </summary>
public StyleProp<Color> Color; public StyleProp<Color> Color;
private TextureRegion texture;
/// <summary> /// <summary>
/// A callback to retrieve the <see cref="TextureRegion"/> that this image should render. /// A callback to retrieve the <see cref="TextureRegion"/> that this image should render.
/// This can be used if the image changes frequently. /// This can be used if the image changes frequently.
@ -40,7 +39,6 @@ namespace MLEM.Ui.Elements {
} }
} }
} }
private bool scaleToImage;
/// <summary> /// <summary>
/// Whether this image element's <see cref="Element.Size"/> should be based on the size of the <see cref="TextureRegion"/> given. /// 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. /// 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> /// </summary>
public float ImageRotation; public float ImageRotation;
private bool scaleToImage;
private TextureRegion texture;
/// <summary> /// <summary>
/// Creates a new image with the given settings /// Creates a new image with the given settings
/// </summary> /// </summary>

View file

@ -17,6 +17,13 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public class Panel : Element { 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> /// <summary>
/// The texture that this panel should have, or null if it should be invisible. /// The texture that this panel should have, or null if it should be invisible.
/// </summary> /// </summary>
@ -35,15 +42,11 @@ namespace MLEM.Ui.Elements {
/// The size that the <see cref="ScrollBar"/>'s scroller should have, in pixels /// The size that the <see cref="ScrollBar"/>'s scroller should have, in pixels
/// </summary> /// </summary>
public StyleProp<Vector2> ScrollerSize; 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 List<Element> relevantChildren = new List<Element>();
private readonly bool scrollOverflow;
private RenderTarget2D renderTarget;
private bool relevantChildrenDirty; private bool relevantChildrenDirty;
/// <summary> /// <summary>
@ -68,8 +71,12 @@ namespace MLEM.Ui.Elements {
AutoHideWhenEmpty = autoHideScrollbar, AutoHideWhenEmpty = autoHideScrollbar,
IsHidden = autoHideScrollbar IsHidden = autoHideScrollbar
}; };
if (autoHideScrollbar) if (autoHideScrollbar) {
this.ScrollBar.OnAutoHide += e => this.ChildPadding += new Padding(0, this.ScrollerSize.Value.X, 0, 0) * (e.IsHidden ? -1 : 1); 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 // handle automatic element selection, the scroller needs to scroll to the right location
this.OnSelectedElementChanged += (element, otherElement) => { this.OnSelectedElementChanged += (element, otherElement) => {
@ -213,8 +220,7 @@ namespace MLEM.Ui.Elements {
this.Texture.SetFromStyle(style.PanelTexture); this.Texture.SetFromStyle(style.PanelTexture);
this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll); this.StepPerScroll.SetFromStyle(style.PanelStepPerScroll);
this.ScrollerSize.SetFromStyle(style.PanelScrollerSize); this.ScrollerSize.SetFromStyle(style.PanelScrollerSize);
if (this.ChildPadding == default) this.ChildPadding.SetFromStyle(style.PanelChildPadding);
this.ChildPadding = style.PanelChildPadding;
this.SetScrollBarStyle(); this.SetScrollBarStyle();
} }
@ -232,7 +238,7 @@ namespace MLEM.Ui.Elements {
var lowestChild = this.GetLowestChild(c => c != this.ScrollBar && !c.IsHidden); 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 // 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; 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 // update the render target
var targetArea = (Rectangle) this.GetRenderTargetArea(); var targetArea = (Rectangle) this.GetRenderTargetArea();

View file

@ -25,7 +25,6 @@ namespace MLEM.Ui.Elements {
/// The tokenized version of the <see cref="Text"/> /// The tokenized version of the <see cref="Text"/>
/// </summary> /// </summary>
public TokenizedString TokenizedText { get; private set; } public TokenizedString TokenizedText { get; private set; }
/// <summary> /// <summary>
/// The color that the text will be rendered with /// The color that the text will be rendered with
/// </summary> /// </summary>

View file

@ -34,7 +34,6 @@ namespace MLEM.Ui.Elements {
/// The color that this progress bar's <see cref="ProgressTexture"/> is rendered with. /// The color that this progress bar's <see cref="ProgressTexture"/> is rendered with.
/// </summary> /// </summary>
public StyleProp<Color> ProgressColor; public StyleProp<Color> ProgressColor;
/// <summary> /// <summary>
/// The direction that this progress bar goes in. /// The direction that this progress bar goes in.
/// Note that only <see cref="Direction2Helper.Adjacent"/> directions are supported. /// 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. /// The maximum value that this progress bar should be able to have.
/// </summary> /// </summary>
public float MaxValue; public float MaxValue;
private float currentValue;
/// <summary> /// <summary>
/// The current value that this progress bar has. /// The current value that this progress bar has.
/// This value is always between 0 and <see cref="MaxValue"/>. /// 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); set => this.currentValue = MathHelper.Clamp(value, 0, this.MaxValue);
} }
private float currentValue;
/// <summary> /// <summary>
/// Creates a new progress bar with the given settings /// Creates a new progress bar with the given settings
/// </summary> /// </summary>

View file

@ -17,6 +17,11 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public class ScrollBar : Element { public class ScrollBar : Element {
/// <summary>
/// Whether this scroll bar is horizontal
/// </summary>
public readonly bool Horizontal;
/// <summary> /// <summary>
/// The background texture for this scroll bar /// The background texture for this scroll bar
/// </summary> /// </summary>
@ -33,7 +38,6 @@ namespace MLEM.Ui.Elements {
/// The scroller's width and height /// The scroller's width and height
/// </summary> /// </summary>
public Vector2 ScrollerSize; public Vector2 ScrollerSize;
private float maxValue;
/// <summary> /// <summary>
/// The max value that this scroll bar should be able to scroll to. /// 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. /// 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> /// <summary>
/// The current value of the scroll bar. /// The current value of the scroll bar.
/// This is between 0 and <see cref="MaxValue"/> at all times. /// This is between 0 and <see cref="MaxValue"/> at all times.
@ -71,10 +73,6 @@ namespace MLEM.Ui.Elements {
} }
} }
/// <summary> /// <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 /// The amount added or removed from <see cref="CurrentValue"/> per single movement of the scroll wheel
/// </summary> /// </summary>
public float StepPerScroll = 1; 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 /// This property is true while the user scrolls on the scroll bar using the mouse or touch input
/// </summary> /// </summary>
public bool IsBeingScrolled => this.isMouseHeld || this.isDragging || this.isTouchHeld; public bool IsBeingScrolled => this.isMouseHeld || this.isDragging || this.isTouchHeld;
private bool isMouseHeld;
private bool isDragging;
private bool isTouchHeld;
/// <summary> /// <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. /// 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> /// </summary>
@ -107,6 +102,13 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public StyleProp<float> SmoothScrollFactor; public StyleProp<float> SmoothScrollFactor;
private bool isMouseHeld;
private bool isDragging;
private bool isTouchHeld;
private float maxValue;
private float scrollAdded;
private float currValue;
static ScrollBar() { static ScrollBar() {
InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.VerticalDrag); InputHandler.EnableGestures(GestureType.HorizontalDrag, GestureType.VerticalDrag);
} }

View file

@ -79,10 +79,9 @@ namespace MLEM.Ui.Elements {
this.Texture.SetFromStyle(style.TooltipBackground); this.Texture.SetFromStyle(style.TooltipBackground);
this.MouseOffset.SetFromStyle(style.TooltipOffset); this.MouseOffset.SetFromStyle(style.TooltipOffset);
this.Delay.SetFromStyle(style.TooltipDelay); this.Delay.SetFromStyle(style.TooltipDelay);
this.ChildPadding = style.TooltipChildPadding; this.ChildPadding.SetFromStyle(style.TooltipChildPadding);
if (this.Paragraph != null) { if (this.Paragraph != null) {
// we can't set from style here since it's a different element this.Paragraph.TextColor.SetFromStyle(style.TooltipTextColor, 1);
this.Paragraph.TextColor.Set(style.TooltipTextColor);
this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0); this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0);
} }
} }

View file

@ -14,14 +14,14 @@ namespace MLEM.Ui.Style {
/// The currently applied style /// The currently applied style
/// </summary> /// </summary>
public T Value { get; private set; } public T Value { get; private set; }
private bool isCustom; private byte lastSetPriority;
/// <summary> /// <summary>
/// Creates a new style property with the given custom style. /// Creates a new style property with the given custom style.
/// </summary> /// </summary>
/// <param name="value">The custom style to apply</param> /// <param name="value">The custom style to apply</param>
public StyleProp(T value) { public StyleProp(T value) {
this.isCustom = true; this.lastSetPriority = byte.MaxValue;
this.Value = value; 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"/>. /// This allows this property to be overridden by custom style settings using <see cref="Set"/>.
/// </summary> /// </summary>
/// <param name="value">The style to apply</param> /// <param name="value">The style to apply</param>
public void SetFromStyle(T value) { /// <param name="priority">The priority that this style value has. Higher priority style values will override lower priority style values.</param>
if (!this.isCustom) public void SetFromStyle(T value, byte priority = 0) {
if (priority >= this.lastSetPriority) {
this.Value = value; this.Value = value;
this.lastSetPriority = priority;
}
} }
/// <summary> /// <summary>
@ -41,7 +44,7 @@ namespace MLEM.Ui.Style {
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
public void Set(T value) { public void Set(T value) {
this.isCustom = true; this.lastSetPriority = byte.MaxValue;
this.Value = value; this.Value = value;
} }
@ -64,7 +67,7 @@ namespace MLEM.Ui.Style {
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() { public override string ToString() {
return $"{this.Value} (Custom: {this.isCustom})"; return this.Value?.ToString();
} }
/// <summary> /// <summary>

View file

@ -52,6 +52,14 @@ namespace MLEM.Misc {
this.Bottom = bottom; 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> /// <summary>
/// Creates a new padding with the specified x and y values, applying them to both edges. /// Creates a new padding with the specified x and y values, applying them to both edges.
/// </summary> /// </summary>

View file

@ -224,7 +224,7 @@ namespace Sandbox {
var button = loadPanel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) { var button = loadPanel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) {
SetHeightBasedOnChildren = true, SetHeightBasedOnChildren = true,
Padding = new Padding(0, 0, 0, 1), 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) { button.AddChild(new Group(Anchor.AutoLeft, new Vector2(0.5F, 30), false) {
CanBeMoused = false CanBeMoused = false

View file

@ -49,7 +49,7 @@ namespace Tests {
var button = panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) { var button = panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(1)) {
SetHeightBasedOnChildren = true, SetHeightBasedOnChildren = true,
Padding = new Padding(0, 0, 0, 1), 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) { button.AddChild(new Group(Anchor.AutoLeft, new Vector2(0.5F, 30), false) {
CanBeMoused = false CanBeMoused = false