diff --git a/CHANGELOG.md b/CHANGELOG.md index e48fcbe..9a71dbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ Additions - Added Panel.IsVisible method to check if a child element is visible - Added TextField.OnEnterPressed event - Added Tooltip.IgnoreViewport and allow overriding the default viewport using Tooltip.Viewport +- Added the ability for Dropdown to display an arrow texture based on its open state Fixes - Fixed tooltips not being bounded correctly for viewports that don't start at the origin diff --git a/Demos/Content/Textures/Test.png b/Demos/Content/Textures/Test.png index 7d88c33..1259537 100644 Binary files a/Demos/Content/Textures/Test.png and b/Demos/Content/Textures/Test.png differ diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 7e6062a..8855fc1 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -50,7 +50,10 @@ namespace Demos { CheckboxCheckmark = new TextureRegion(this.testTexture, 24, 0, 8, 8), RadioTexture = new NinePatch(new TextureRegion(this.testTexture, 16, 0, 8, 8), 3), RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8), - AdditionalFonts = {{"Monospaced", new GenericSpriteFont(Demo.LoadContent("Fonts/MonospacedFont"))}} + AdditionalFonts = {{"Monospaced", new GenericSpriteFont(Demo.LoadContent("Fonts/MonospacedFont"))}}, + DropdownClosedArrowTexture = new TextureRegion(this.testTexture, 40, 0, 8, 8), + DropdownOpenedArrowTexture = new TextureRegion(this.testTexture, 48, 0, 8, 8), + DropdownArrowPadding = new Padding(0, 4, 0, 0) }; var untexturedStyle = new UntexturedStyle(this.SpriteBatch) { TextScale = style.TextScale, diff --git a/MLEM.Ui/Elements/Dropdown.cs b/MLEM.Ui/Elements/Dropdown.cs index bf6f805..ab839c8 100644 --- a/MLEM.Ui/Elements/Dropdown.cs +++ b/MLEM.Ui/Elements/Dropdown.cs @@ -1,6 +1,8 @@ using System.Linq; using Microsoft.Xna.Framework; using MLEM.Maths; +using MLEM.Textures; +using MLEM.Ui.Style; namespace MLEM.Ui.Elements { /// @@ -14,12 +16,17 @@ namespace MLEM.Ui.Elements { /// public Panel Panel { get; private set; } /// + /// The that contains the currently active arrow texture for this dropdown, which is either or . + /// + public Image Arrow { get; private set; } + /// /// This property stores whether the dropdown is currently opened or not /// public bool IsOpen { get => !this.Panel.IsHidden; set { this.Panel.IsHidden = !value; + this.UpdateArrowStyle(); this.OnOpenedOrClosed?.Invoke(this); } } @@ -28,6 +35,41 @@ namespace MLEM.Ui.Elements { /// public GenericCallback OnOpenedOrClosed; + /// + /// A style property containing the that should be passed to the child element. + /// + public StyleProp ArrowPadding { + get => this.arrowPadding; + set { + this.arrowPadding = value; + this.UpdateArrowStyle(); + } + } + /// + /// A style property containing the that should be displayed on this dropdown's when the dropdown is closed ( is ). + /// + public StyleProp ClosedArrowTexture { + get => this.closedArrowTexture; + set { + this.closedArrowTexture = value; + this.UpdateArrowStyle(); + } + } + /// + /// A style property containing the that should be displayed on this dropdown's when the dropdown is open ( is ). + /// + public StyleProp OpenedArrowTexture { + get => this.openedArrowTexture; + set { + this.openedArrowTexture = value; + this.UpdateArrowStyle(); + } + } + + private StyleProp openedArrowTexture; + private StyleProp closedArrowTexture; + private StyleProp arrowPadding; + /// /// Creates a new dropdown with the given settings and no text or tooltip. /// @@ -125,6 +167,8 @@ namespace MLEM.Ui.Elements { this.Panel = this.AddChild(new Panel(Anchor.TopCenter, Vector2.Zero, panelHeight == 0, scrollPanel, autoHidePanelScrollbar) { IsHidden = true }); + this.Arrow = this.AddChild(new Image(Anchor.CenterRight, new Vector2(-1, 1), this.ClosedArrowTexture)); + this.UpdateArrowStyle(); 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); @@ -148,5 +192,19 @@ namespace MLEM.Ui.Elements { }; } + /// + protected override void InitStyle(UiStyle style) { + base.InitStyle(style); + this.ArrowPadding = this.ArrowPadding.OrStyle(style.DropdownArrowPadding); + this.ClosedArrowTexture = this.ClosedArrowTexture.OrStyle(style.DropdownClosedArrowTexture); + this.OpenedArrowTexture = this.OpenedArrowTexture.OrStyle(style.DropdownOpenedArrowTexture); + this.UpdateArrowStyle(); + } + + private void UpdateArrowStyle() { + this.Arrow.Padding = this.Arrow.Padding.OrStyle(this.ArrowPadding, 1); + this.Arrow.Texture = this.IsOpen ? this.OpenedArrowTexture : this.ClosedArrowTexture; + } + } } diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 36b73ff..cb37e48 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -242,6 +242,22 @@ namespace MLEM.Ui.Style { /// This value is passed to . /// public Color? LinkColor = Color.CornflowerBlue; + /// + /// The default padding that a 's should have. + /// This value is passed to . + /// + public Padding DropdownArrowPadding; + /// + /// The texture that a 's should display when the dropdown is closed. + /// This value is passed to . + /// + public TextureRegion DropdownClosedArrowTexture; + /// + /// The texture that a 's should display when the dropdown is open. + /// This value is passed to . + /// + public TextureRegion DropdownOpenedArrowTexture; + /// /// A set of additional fonts that can be used for the <f FontName> formatting code ///