diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 422209d..1b46b05 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using MLEM.Extensions; @@ -140,6 +141,8 @@ namespace MLEM.Ui.Elements { private bool areaDirty; private bool sortedChildrenDirty; public StyleProp SelectionIndicator; + public StyleProp ActionSound; + public StyleProp SecondActionSound; public Element(Anchor anchor, Vector2 size) { this.anchor = anchor; @@ -498,6 +501,8 @@ namespace MLEM.Ui.Elements { protected virtual void InitStyle(UiStyle style) { this.SelectionIndicator.SetFromStyle(style.SelectionIndicator); + this.ActionSound.SetFromStyle(style.ActionSound); + this.SecondActionSound.SetFromStyle(style.ActionSound); } public delegate void TextInputCallback(Element element, Keys key, char character); diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 444f398..1d55bd6 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -1,4 +1,5 @@ using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Audio; using MLEM.Font; using MLEM.Formatting; using MLEM.Textures; @@ -36,6 +37,7 @@ namespace MLEM.Ui.Style { public IGenericFont ItalicFont; public FormatSettings FormatSettings; public float TextScale = 1; + public SoundEffectInstance ActionSound; } } \ No newline at end of file diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 9717d2f..e5f6f1a 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -81,11 +81,7 @@ namespace MLEM.Ui { root.Element.ForceUpdateArea(); }; - window.AddTextInputListener((sender, key, character) => { - foreach (var root in this.rootElements) - root.Element.AndChildren(e => e.OnTextInput?.Invoke(e, key, character)); - }); - + window.AddTextInputListener((sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character))); this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e)); this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e)); this.OnSelectedElementDrawn = (element, time, batch, alpha) => { @@ -93,6 +89,14 @@ namespace MLEM.Ui { batch.Draw(element.SelectionIndicator, element.DisplayArea, Color.White * alpha, element.Scale / 2); } }; + this.OnElementPressed += e => { + if (e.OnPressed != null) + e.ActionSound.Value?.Replay(); + }; + this.OnElementSecondaryPressed += e => { + if (e.OnSecondaryPressed != null) + e.SecondActionSound.Value?.Replay(); + }; } public override void Update(GameTime time) {