mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 04:53:29 +01:00
Added some useful additional constructors to various elements
This commit is contained in:
parent
1436cdb987
commit
bd0a723d86
6 changed files with 134 additions and 30 deletions
|
@ -35,6 +35,7 @@ Additions
|
|||
- Added UiControls.NavType, which stores the most recently used type of ui navigation
|
||||
- Added SetWidthBasedOnAspect and SetHeightBasedOnAspect to images
|
||||
- Added the ability to set a custom SamplerState for images
|
||||
- Added some useful additional constructors to various elements
|
||||
|
||||
Improvements
|
||||
- Allow scrolling panels to contain other scrolling panels
|
||||
|
|
|
@ -87,6 +87,13 @@ namespace MLEM.Ui.Elements {
|
|||
|
||||
private bool isDisabled;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new button with the given settings and no text or tooltip.
|
||||
/// </summary>
|
||||
/// <param name="anchor">The button's anchor</param>
|
||||
/// <param name="size">The button's size</param>
|
||||
public Button(Anchor anchor, Vector2 size) : base(anchor, size) {}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new button with the given settings
|
||||
/// </summary>
|
||||
|
@ -94,7 +101,7 @@ namespace MLEM.Ui.Elements {
|
|||
/// <param name="size">The button's size</param>
|
||||
/// <param name="text">The text that should be displayed on the 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) : this(anchor, size) {
|
||||
if (text != null) {
|
||||
this.Text = new Paragraph(Anchor.Center, 1, text, true);
|
||||
this.Text.Padding = this.Text.Padding.OrStyle(new Padding(1), 1);
|
||||
|
@ -104,6 +111,23 @@ namespace MLEM.Ui.Elements {
|
|||
this.Tooltip = this.AddTooltip(tooltipText);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new button with the given settings
|
||||
/// </summary>
|
||||
/// <param name="anchor">The button's anchor</param>
|
||||
/// <param name="size">The button's size</param>
|
||||
/// <param name="textCallback">The text that should be displayed on the button</param>
|
||||
/// <param name="tooltipTextCallback">The text that should be displayed in a <see cref="Tooltip"/> when hovering over this button</param>
|
||||
public Button(Anchor anchor, Vector2 size, Paragraph.TextCallback textCallback = null, Paragraph.TextCallback tooltipTextCallback = null) : this(anchor, size) {
|
||||
if (textCallback != null) {
|
||||
this.Text = new Paragraph(Anchor.Center, 1, textCallback, true);
|
||||
this.Text.Padding = this.Text.Padding.OrStyle(new Padding(1), 1);
|
||||
this.AddChild(this.Text);
|
||||
}
|
||||
if (tooltipTextCallback != null)
|
||||
this.Tooltip = this.AddTooltip(tooltipTextCallback);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Draw(GameTime time, SpriteBatch batch, float alpha, SpriteBatchContext context) {
|
||||
var tex = this.Texture;
|
||||
|
|
|
@ -12,8 +12,7 @@ namespace MLEM.Ui.Elements {
|
|||
/// <summary>
|
||||
/// The panel that this dropdown contains. It will be displayed upon pressing the dropdown button.
|
||||
/// </summary>
|
||||
public readonly Panel Panel;
|
||||
|
||||
public Panel Panel { get; private set; }
|
||||
/// <summary>
|
||||
/// This property stores whether the dropdown is currently opened or not
|
||||
/// </summary>
|
||||
|
@ -29,6 +28,18 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
public GenericCallback OnOpenedOrClosed;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new dropdown with the given settings and no text or tooltip.
|
||||
/// </summary>
|
||||
/// <param name="anchor">The dropdown's anchor</param>
|
||||
/// <param name="size">The dropdown button's size</param>
|
||||
/// <param name="panelHeight">The height of the <see cref="Panel"/>. If this is 0, the panel will be set to <see cref="Element.SetHeightBasedOnChildren"/>.</param>
|
||||
/// <param name="scrollPanel">Whether this dropdown's <see cref="Panel"/> should automatically add a scroll bar to scroll towards elements that are beyond the area it covers.</param>
|
||||
/// <param name="autoHidePanelScrollbar">Whether this dropdown's <see cref="Panel"/>'s scroll bar should be hidden automatically if the panel does not contain enough children to allow for scrolling. This only has an effect if <paramref name="scrollPanel"/> is <see langword="true"/>.</param>
|
||||
public Dropdown(Anchor anchor, Vector2 size, float panelHeight = 0, bool scrollPanel = false, bool autoHidePanelScrollbar = true) : base(anchor, size) {
|
||||
this.Initialize(panelHeight, scrollPanel, autoHidePanelScrollbar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new dropdown with the given settings
|
||||
/// </summary>
|
||||
|
@ -40,30 +51,21 @@ namespace MLEM.Ui.Elements {
|
|||
/// <param name="scrollPanel">Whether this dropdown's <see cref="Panel"/> should automatically add a scroll bar to scroll towards elements that are beyond the area it covers.</param>
|
||||
/// <param name="autoHidePanelScrollbar">Whether this dropdown's <see cref="Panel"/>'s scroll bar should be hidden automatically if the panel does not contain enough children to allow for scrolling. This only has an effect if <paramref name="scrollPanel"/> is <see langword="true"/>.</param>
|
||||
public Dropdown(Anchor anchor, Vector2 size, string text = null, string tooltipText = null, float panelHeight = 0, bool scrollPanel = false, bool autoHidePanelScrollbar = true) : base(anchor, size, text, tooltipText) {
|
||||
this.Panel = this.AddChild(new Panel(Anchor.TopCenter, Vector2.Zero, Vector2.Zero, panelHeight == 0, scrollPanel, autoHidePanelScrollbar) {
|
||||
IsHidden = true
|
||||
});
|
||||
this.OnAreaUpdated += e => {
|
||||
this.Panel.Size = new Vector2(e.Area.Width / e.Scale, panelHeight);
|
||||
this.Panel.PositionOffset = new Vector2(0, e.Area.Height / e.Scale);
|
||||
};
|
||||
this.OnOpenedOrClosed += e => this.Priority = this.IsOpen ? 10000 : 0;
|
||||
this.OnPressed += e => {
|
||||
this.IsOpen = !this.IsOpen;
|
||||
// close other dropdowns in the same root when we open
|
||||
if (this.IsOpen) {
|
||||
this.Root.Element.AndChildren(o => {
|
||||
if (o != this && o is Dropdown d && d.IsOpen)
|
||||
d.IsOpen = false;
|
||||
});
|
||||
}
|
||||
};
|
||||
this.GetGamepadNextElement = (dir, usualNext) => {
|
||||
// Force navigate down to our first child if we're open
|
||||
if (this.IsOpen && dir == Direction2.Down)
|
||||
return this.Panel.GetChildren().FirstOrDefault(c => c.CanBeSelected) ?? usualNext;
|
||||
return usualNext;
|
||||
};
|
||||
this.Initialize(panelHeight, scrollPanel, autoHidePanelScrollbar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new dropdown with the given settings
|
||||
/// </summary>
|
||||
/// <param name="anchor">The dropdown's anchor</param>
|
||||
/// <param name="size">The dropdown button's size</param>
|
||||
/// <param name="textCallback">The text displayed on the dropdown button</param>
|
||||
/// <param name="tooltipTextCallback">The text displayed as a tooltip when hovering over the dropdown button</param>
|
||||
/// <param name="panelHeight">The height of the <see cref="Panel"/>. If this is 0, the panel will be set to <see cref="Element.SetHeightBasedOnChildren"/>.</param>
|
||||
/// <param name="scrollPanel">Whether this dropdown's <see cref="Panel"/> should automatically add a scroll bar to scroll towards elements that are beyond the area it covers.</param>
|
||||
/// <param name="autoHidePanelScrollbar">Whether this dropdown's <see cref="Panel"/>'s scroll bar should be hidden automatically if the panel does not contain enough children to allow for scrolling. This only has an effect if <paramref name="scrollPanel"/> is <see langword="true"/>.</param>
|
||||
public Dropdown(Anchor anchor, Vector2 size, Paragraph.TextCallback textCallback = null, Paragraph.TextCallback tooltipTextCallback = null, float panelHeight = 0, bool scrollPanel = false, bool autoHidePanelScrollbar = true) : base(anchor, size, textCallback, tooltipTextCallback) {
|
||||
this.Initialize(panelHeight, scrollPanel, autoHidePanelScrollbar);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -119,5 +121,32 @@ namespace MLEM.Ui.Elements {
|
|||
return paragraph;
|
||||
}
|
||||
|
||||
private void Initialize(float panelHeight, bool scrollPanel, bool autoHidePanelScrollbar) {
|
||||
this.Panel = this.AddChild(new Panel(Anchor.TopCenter, Vector2.Zero, panelHeight == 0, scrollPanel, autoHidePanelScrollbar) {
|
||||
IsHidden = true
|
||||
});
|
||||
this.OnAreaUpdated += e => {
|
||||
this.Panel.Size = new Vector2(e.Area.Width / e.Scale, panelHeight);
|
||||
this.Panel.PositionOffset = new Vector2(0, e.Area.Height / e.Scale);
|
||||
};
|
||||
this.OnOpenedOrClosed += e => this.Priority = this.IsOpen ? 10000 : 0;
|
||||
this.OnPressed += e => {
|
||||
this.IsOpen = !this.IsOpen;
|
||||
// close other dropdowns in the same root when we open
|
||||
if (this.IsOpen) {
|
||||
this.Root.Element.AndChildren(o => {
|
||||
if (o != this && o is Dropdown d && d.IsOpen)
|
||||
d.IsOpen = false;
|
||||
});
|
||||
}
|
||||
};
|
||||
this.GetGamepadNextElement = (dir, usualNext) => {
|
||||
// Force navigate down to our first child if we're open
|
||||
if (this.IsOpen && dir == Direction2.Down)
|
||||
return this.Panel.GetChildren().FirstOrDefault(c => c.CanBeSelected) ?? usualNext;
|
||||
return usualNext;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,18 @@ namespace MLEM.Ui.Elements {
|
|||
/// </summary>
|
||||
/// <param name="anchor">The group's anchor</param>
|
||||
/// <param name="size">The group's size</param>
|
||||
/// <param name="setHeightBasedOnChildren">Whether the group's height should be based on its children's height</param>
|
||||
public Group(Anchor anchor, Vector2 size, bool setHeightBasedOnChildren = true) : base(anchor, size) {
|
||||
/// <param name="setHeightBasedOnChildren">Whether the group's height should be based on its children's height, see <see cref="Element.SetHeightBasedOnChildren"/>.</param>
|
||||
public Group(Anchor anchor, Vector2 size, bool setHeightBasedOnChildren = true) : this(anchor, size, false, setHeightBasedOnChildren) {}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new group with the given settings
|
||||
/// </summary>
|
||||
/// <param name="anchor">The group's anchor</param>
|
||||
/// <param name="size">The group's size</param>
|
||||
/// <param name="setWidthBasedOnChildren">Whether the group's width should be based on its children's width, see <see cref="Element.SetWidthBasedOnChildren"/>.</param>
|
||||
/// <param name="setHeightBasedOnChildren">Whether the group's height should be based on its children's height, see <see cref="Element.SetHeightBasedOnChildren"/>.</param>
|
||||
public Group(Anchor anchor, Vector2 size, bool setWidthBasedOnChildren, bool setHeightBasedOnChildren) : base(anchor, size) {
|
||||
this.SetWidthBasedOnChildren = setWidthBasedOnChildren;
|
||||
this.SetHeightBasedOnChildren = setHeightBasedOnChildren;
|
||||
this.CanBeSelected = false;
|
||||
}
|
||||
|
|
|
@ -104,6 +104,16 @@ namespace MLEM.Ui.Elements {
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new panel with the given settings.
|
||||
/// </summary>
|
||||
/// <param name="anchor">The panel's anchor</param>
|
||||
/// <param name="size">The panel's default size</param>
|
||||
/// <param name="setHeightBasedOnChildren">Whether the panel should automatically calculate its height based on its children's size</param>
|
||||
/// <param name="scrollOverflow">Whether this panel should automatically add a scroll bar to scroll towards elements that are beyond the area this panel covers</param>
|
||||
/// <param name="autoHideScrollbar">Whether the scroll bar should be hidden automatically if the panel does not contain enough children to allow for scrolling. This only has an effect if <paramref name="scrollOverflow"/> is <see langword="true"/>.</param>
|
||||
public Panel(Anchor anchor, Vector2 size, bool setHeightBasedOnChildren = false, bool scrollOverflow = false, bool autoHideScrollbar = true) : this(anchor, size, Vector2.Zero, setHeightBasedOnChildren, scrollOverflow, autoHideScrollbar) {}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void ForceUpdateArea() {
|
||||
if (this.scrollOverflow) {
|
||||
|
|
|
@ -166,6 +166,30 @@ namespace MLEM.Ui.Elements {
|
|||
private float textScaleMultiplier = 1;
|
||||
private bool autoAdjustWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new paragraph with the given settings.
|
||||
/// </summary>
|
||||
/// <param name="anchor">The paragraph's anchor</param>
|
||||
/// <param name="width">The paragraph's width. Note that its height is automatically calculated.</param>
|
||||
/// <param name="textCallback">The paragraph's text</param>
|
||||
/// <param name="alignment">The paragraph's text alignment.</param>
|
||||
/// <param name="autoAdjustWidth">Whether the paragraph's width should automatically be calculated based on the text within it.</param>
|
||||
public Paragraph(Anchor anchor, float width, TextCallback textCallback, TextAlignment alignment, bool autoAdjustWidth = false) : this(anchor, width, textCallback, autoAdjustWidth) {
|
||||
this.Alignment = alignment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new paragraph with the given settings.
|
||||
/// </summary>
|
||||
/// <param name="anchor">The paragraph's anchor</param>
|
||||
/// <param name="width">The paragraph's width. Note that its height is automatically calculated.</param>
|
||||
/// <param name="text">The paragraph's text</param>
|
||||
/// <param name="alignment">The paragraph's text alignment.</param>
|
||||
/// <param name="autoAdjustWidth">Whether the paragraph's width should automatically be calculated based on the text within it.</param>
|
||||
public Paragraph(Anchor anchor, float width, string text, TextAlignment alignment, bool autoAdjustWidth = false) : this(anchor, width, text, autoAdjustWidth) {
|
||||
this.Alignment = alignment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new paragraph with the given settings.
|
||||
/// </summary>
|
||||
|
@ -177,7 +201,13 @@ namespace MLEM.Ui.Elements {
|
|||
this.GetTextCallback = textCallback;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Paragraph(Anchor,float,TextCallback,bool)"/>
|
||||
/// <summary>
|
||||
/// Creates a new paragraph with the given settings.
|
||||
/// </summary>
|
||||
/// <param name="anchor">The paragraph's anchor</param>
|
||||
/// <param name="width">The paragraph's width. Note that its height is automatically calculated.</param>
|
||||
/// <param name="text">The paragraph's text</param>
|
||||
/// <param name="autoAdjustWidth">Whether the paragraph's width should automatically be calculated based on the text within it.</param>
|
||||
public Paragraph(Anchor anchor, float width, string text, bool autoAdjustWidth = false) : base(anchor, new Vector2(width, 0)) {
|
||||
this.Text = text;
|
||||
this.AutoAdjustWidth = autoAdjustWidth;
|
||||
|
|
Loading…
Reference in a new issue