1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-23 13:18:35 +01:00

Compare commits

..

3 commits

3 changed files with 18 additions and 3 deletions

View file

@ -13,7 +13,7 @@ protected override void LoadContent() {
// Load your other content here // Load your other content here
// Initialize the Ui system // Initialize the Ui system
this.UiSystem = new UiSystem(this.Window, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch)); this.UiSystem = new UiSystem(this, this.GraphicsDevice, new UntexturedStyle(this.SpriteBatch));
} }
protected override void Update(GameTime gameTime) { protected override void Update(GameTime gameTime) {

View file

@ -6,6 +6,7 @@ namespace MLEM.Ui.Elements {
/// <summary> /// <summary>
/// A group element to be used inside of a <see cref="UiSystem"/>. /// A group element to be used inside of a <see cref="UiSystem"/>.
/// A group is an element that has no rendering or interaction on its own, but that can aid with automatic placement of child elements. /// A group is an element that has no rendering or interaction on its own, but that can aid with automatic placement of child elements.
/// If a grouping whose children scroll, and which has a <see cref="ScrollBar"/>, is desired, a <see cref="Panel"/> with its <see cref="Panel.Texture"/> set to <see langword="null"/> can be used.
/// </summary> /// </summary>
public class Group : Element { public class Group : Element {

View file

@ -13,6 +13,7 @@ namespace MLEM.Ui.Elements {
/// A panel element to be used inside of a <see cref="UiSystem"/>. /// A panel element to be used inside of a <see cref="UiSystem"/>.
/// The panel is a complex element that displays a box as a background to all of its child elements. /// The panel is a complex element that displays a box as a background to all of its child elements.
/// Additionally, a panel can be set to scroll overflowing elements on construction, which causes all elements that don't fit into the panel to be hidden until scrolled to using a <see cref="ScrollBar"/>. /// Additionally, a panel can be set to scroll overflowing elements on construction, which causes all elements that don't fit into the panel to be hidden until scrolled to using a <see cref="ScrollBar"/>.
/// If an element similar to a <see cref="Group"/>, but with scrolling content, is desired, a panel with its <see cref="Texture"/> set to <see langword="null"/> can be used.
/// </summary> /// </summary>
public class Panel : Element { public class Panel : Element {
@ -24,7 +25,8 @@ namespace MLEM.Ui.Elements {
public readonly ScrollBar ScrollBar; 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.
/// If this is set to <see langword="null"/>, this panel will not have a texture, but all of its content will still be visible.
/// </summary> /// </summary>
public StyleProp<NinePatch> Texture; public StyleProp<NinePatch> Texture;
/// <summary> /// <summary>
@ -64,6 +66,7 @@ namespace MLEM.Ui.Elements {
private StyleProp<float> scrollBarOffset; private StyleProp<float> scrollBarOffset;
private float lastScrollOffset; private float lastScrollOffset;
private bool childrenDirtyForScroll; private bool childrenDirtyForScroll;
private bool scrollBarMaxHistoryDirty;
/// <summary> /// <summary>
/// Creates a new panel with the given settings. /// Creates a new panel with the given settings.
@ -83,6 +86,7 @@ namespace MLEM.Ui.Elements {
if (scrollOverflow) { if (scrollOverflow) {
this.scrollBarMaxHistory = new float[3]; this.scrollBarMaxHistory = new float[3];
this.scrollBarMaxHistoryDirty = true;
this.ResetScrollBarMaxHistory(); this.ResetScrollBarMaxHistory();
this.ScrollBar = new ScrollBar(Anchor.TopRight, Vector2.Zero, 0, 0) { this.ScrollBar = new ScrollBar(Anchor.TopRight, Vector2.Zero, 0, 0) {
@ -172,6 +176,14 @@ namespace MLEM.Ui.Elements {
base.RemoveChildren(e => e != this.ScrollBar && (condition == null || condition(e))); base.RemoveChildren(e => e != this.ScrollBar && (condition == null || condition(e)));
} }
/// <inheritdoc />
public override void Update(GameTime time) {
// reset the scroll bar's max history when an update happens, at which point we know that any scroll bar recursion has "settled"
// (this reset ensures that the max history is recursion-internal and old values aren't reused when elements get modified later)
this.ResetScrollBarMaxHistory();
base.Update(time);
}
/// <inheritdoc /> /// <inheritdoc />
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) { public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
// draw children onto the render target if we have one // draw children onto the render target if we have one
@ -334,6 +346,7 @@ namespace MLEM.Ui.Elements {
this.scrollBarMaxHistory[0] = this.scrollBarMaxHistory[1]; this.scrollBarMaxHistory[0] = this.scrollBarMaxHistory[1];
this.scrollBarMaxHistory[1] = this.scrollBarMaxHistory[2]; this.scrollBarMaxHistory[1] = this.scrollBarMaxHistory[2];
this.scrollBarMaxHistory[2] = scrollBarMax; this.scrollBarMaxHistory[2] = scrollBarMax;
this.scrollBarMaxHistoryDirty = true;
this.ScrollBar.MaxValue = scrollBarMax; this.ScrollBar.MaxValue = scrollBarMax;
this.relevantChildrenDirty = true; this.relevantChildrenDirty = true;
@ -419,9 +432,10 @@ namespace MLEM.Ui.Elements {
} }
private void ResetScrollBarMaxHistory() { private void ResetScrollBarMaxHistory() {
if (this.scrollOverflow) { if (this.scrollOverflow && this.scrollBarMaxHistoryDirty) {
for (var i = 0; i < this.scrollBarMaxHistory.Length; i++) for (var i = 0; i < this.scrollBarMaxHistory.Length; i++)
this.scrollBarMaxHistory[i] = -1; this.scrollBarMaxHistory[i] = -1;
this.scrollBarMaxHistoryDirty = false;
} }
} }