From c2ff7a70063457008077a2e5dbc773a191a65a27 Mon Sep 17 00:00:00 2001 From: WeylonSantana Date: Wed, 26 Feb 2025 18:18:31 -0300 Subject: [PATCH] transfer sound logic from child to parent --- .../Gwen/ControlInternal/Dragger.cs | 111 +++++++++++---- .../Gwen/ControlInternal/ScrollBarBar.cs | 131 ------------------ 2 files changed, 81 insertions(+), 161 deletions(-) diff --git a/Intersect.Client.Framework/Gwen/ControlInternal/Dragger.cs b/Intersect.Client.Framework/Gwen/ControlInternal/Dragger.cs index 383d18fadd..590f73c942 100644 --- a/Intersect.Client.Framework/Gwen/ControlInternal/Dragger.cs +++ b/Intersect.Client.Framework/Gwen/ControlInternal/Dragger.cs @@ -1,4 +1,4 @@ -using Intersect.Client.Framework.File_Management; +using Intersect.Client.Framework.File_Management; using Intersect.Client.Framework.GenericClasses; using Intersect.Client.Framework.Graphics; using Intersect.Client.Framework.Gwen.Control; @@ -17,9 +17,8 @@ public partial class Dragger : Base protected Point mHoldPos; //Sound Effects - private string? mHoverSound; - private string? mMouseDownSound; - private string? mMouseUpSound; + protected readonly Dictionary _stateSoundNames = []; + protected DateTime _ignoreMouseUpSoundsUntil; private IGameTexture? mClickedImage; private string? mClickedImageFilename; @@ -54,6 +53,16 @@ internal Base Target /// public event GwenEventHandler? Dragged; + protected override void OnMouseEntered() + { + base.OnMouseEntered(); + + if (ShouldDrawHover) + { + PlaySound(_stateSoundNames[ButtonSoundState.Hover]); + } + } + protected override void OnMouseDown(MouseButton mouseButton, Point mousePosition, bool userAction = true) { base.OnMouseDown(mouseButton, mousePosition, userAction); @@ -67,7 +76,7 @@ protected override void OnMouseDown(MouseButton mouseButton, Point mousePosition if (userAction) { - base.PlaySound(mMouseDownSound); + PlaySound(_stateSoundNames[ButtonSoundState.MouseDown]); } mHoldPos = _target.CanvasPosToLocal(mousePosition); @@ -80,7 +89,7 @@ protected override void OnMouseUp(MouseButton mouseButton, Point mousePosition, if (userAction) { - base.PlaySound(mMouseUpSound); + PlaySound(_stateSoundNames[ButtonSoundState.MouseUp]); } InputHandler.MouseFocus = null; @@ -169,16 +178,20 @@ protected override void OnBoundsChanged(Rectangle oldBounds, Rectangle newBounds serializedProperties.Add("HoveredImage", GetImageFilename(ComponentState.Hovered)); serializedProperties.Add("ClickedImage", GetImageFilename(ComponentState.Active)); serializedProperties.Add("DisabledImage", GetImageFilename(ComponentState.Disabled)); - serializedProperties.Add("HoverSound", mHoverSound); - serializedProperties.Add("MouseUpSound", mMouseUpSound); - serializedProperties.Add("MouseDownSound", mMouseDownSound); + serializedProperties.Add(nameof(_stateSoundNames), JObject.FromObject(_stateSoundNames)); return base.FixJson(serializedProperties); } - public override void LoadJson(JToken obj, bool isRoot = default) + public override void LoadJson(JToken token, bool isRoot = default) { - base.LoadJson(obj); + base.LoadJson(token, isRoot); + + if (token is not JObject obj) + { + return; + } + if (obj["NormalImage"] != null) { SetImage( @@ -215,25 +228,50 @@ public override void LoadJson(JToken obj, bool isRoot = default) ); } - if (obj["HoverSound"] != null) + if (obj.TryGetValue(nameof(_stateSoundNames), out var tokenStateSoundNames) && tokenStateSoundNames is JObject valueStateSoundNames) { - mHoverSound = (string) obj["HoverSound"]; + foreach (var (propertyName, propertyValueToken) in valueStateSoundNames) + { + if (!Enum.TryParse(propertyName, out ButtonSoundState buttonSoundState) || + buttonSoundState == ButtonSoundState.None) + { + continue; + } + + if (propertyValueToken is not JValue { Type: JTokenType.String } valuePropertyValue) + { + continue; + } + + var stringPropertyValue = valuePropertyValue.Value()?.Trim(); + if (stringPropertyValue is { Length: > 0 }) + { + _stateSoundNames[buttonSoundState] = stringPropertyValue; + } + else + { + _stateSoundNames.Remove(buttonSoundState); + } + } } + } - if (obj["MouseUpSound"] != null) + public void SetSound(ButtonSoundState state, string? soundName) + { + soundName = soundName?.Trim(); + if (string.IsNullOrEmpty(soundName)) { - mMouseUpSound = (string) obj["MouseUpSound"]; + _stateSoundNames.Remove(state); } - - if (obj["MouseDownSound"] != null) + else { - mMouseDownSound = (string) obj["MouseDownSound"]; + _stateSoundNames[state] = soundName; } } public string GetMouseUpSound() { - return mMouseUpSound; + return _stateSoundNames[ButtonSoundState.MouseUp]; } /// @@ -303,22 +341,35 @@ public virtual string GetImageFilename(ComponentState state) } } - protected override void OnMouseEntered() + public void PlaySound(ButtonSoundState soundState) { - base.OnMouseEntered(); + if (soundState == ButtonSoundState.MouseUp) + { + if (_ignoreMouseUpSoundsUntil > DateTime.UtcNow) + { + return; + } + } - //Play Mouse Entered Sound - if (ShouldDrawHover) + if (!_stateSoundNames.TryGetValue(soundState, out var soundName)) + { + return; + } + + if (!base.PlaySound(soundName)) + { + return; + } + + if (soundState == ButtonSoundState.MouseDown) { - base.PlaySound(mHoverSound); + _ignoreMouseUpSoundsUntil = DateTime.UtcNow.AddMilliseconds(200); } } public void ClearSounds() { - mHoverSound = string.Empty; - mMouseDownSound = string.Empty; - mMouseUpSound = string.Empty; + _stateSoundNames.Clear(); } public void SetSound(string sound, ButtonSoundState state) @@ -328,13 +379,13 @@ public void SetSound(string sound, ButtonSoundState state) case ButtonSoundState.None: break; case ButtonSoundState.Hover: - mHoverSound = sound; + _stateSoundNames[ButtonSoundState.Hover] = sound; break; case ButtonSoundState.MouseDown: - mMouseDownSound = sound; + _stateSoundNames[ButtonSoundState.MouseDown] = sound; break; case ButtonSoundState.MouseUp: - mMouseUpSound = sound; + _stateSoundNames[ButtonSoundState.MouseUp] = sound; break; default: throw new ArgumentOutOfRangeException(nameof(state), state, null); diff --git a/Intersect.Client.Framework/Gwen/ControlInternal/ScrollBarBar.cs b/Intersect.Client.Framework/Gwen/ControlInternal/ScrollBarBar.cs index 969d0d1657..6417178359 100644 --- a/Intersect.Client.Framework/Gwen/ControlInternal/ScrollBarBar.cs +++ b/Intersect.Client.Framework/Gwen/ControlInternal/ScrollBarBar.cs @@ -1,7 +1,6 @@ using Intersect.Client.Framework.GenericClasses; using Intersect.Client.Framework.Gwen.Control; using Intersect.Client.Framework.Input; -using Newtonsoft.Json.Linq; namespace Intersect.Client.Framework.Gwen.ControlInternal; @@ -12,10 +11,6 @@ public partial class ScrollBarBar : Dragger { private bool mHorizontal; - private readonly Dictionary _stateSoundNames = []; - - private DateTime _ignoreMouseUpSoundsUntil; - /// /// Initializes a new instance of the class. /// @@ -76,62 +71,6 @@ protected override void OnMouseMoved(int x, int y, int dx, int dy) InvalidateParent(); } - protected override void OnMouseEntered() - { - base.OnMouseEntered(); - PlaySound(ButtonSoundState.Hover); - } - - protected override void OnMouseDown(MouseButton mouseButton, Point mousePosition, bool userAction = true) - { - base.OnMouseDown(mouseButton, mousePosition, userAction); - PlaySound(ButtonSoundState.MouseDown); - } - - protected override void OnMouseUp(MouseButton mouseButton, Point mousePosition, bool userAction = true) - { - base.OnMouseUp(mouseButton, mousePosition, userAction); - PlaySound(ButtonSoundState.MouseUp); - } - - protected override void OnMouseClicked(MouseButton mouseButton, Point mousePosition, bool userAction = true) - { - base.OnMouseClicked(mouseButton, mousePosition, userAction); - - if (mouseButton == MouseButton.Left) - { - InvalidateParent(); - } - - PlaySound(ButtonSoundState.MouseClicked); - } - - public void PlaySound(ButtonSoundState soundState) - { - if (soundState == ButtonSoundState.MouseUp) - { - if (_ignoreMouseUpSoundsUntil > DateTime.UtcNow) - { - return; - } - } - - if (!_stateSoundNames.TryGetValue(soundState, out var soundName)) - { - return; - } - - if (!base.PlaySound(soundName)) - { - return; - } - - if (soundState == ButtonSoundState.MouseDown) - { - _ignoreMouseUpSoundsUntil = DateTime.UtcNow.AddMilliseconds(200); - } - } - /// /// Lays out the control's interior according to alignment, padding, dock etc. /// @@ -153,74 +92,4 @@ protected override void OnBoundsChanged(Rectangle oldBounds, Rectangle newBounds InvalidateParent(); } - - public void SetSound(ButtonSoundState state, string? soundName) - { - soundName = soundName?.Trim(); - if (string.IsNullOrEmpty(soundName)) - { - _stateSoundNames.Remove(state); - } - else - { - _stateSoundNames[state] = soundName; - } - } - - public override JObject? GetJson(bool isRoot = false, bool onlySerializeIfNotEmpty = false) - { - var serializedProperties = base.GetJson(isRoot, onlySerializeIfNotEmpty); - if (serializedProperties is null) - { - return null; - } - - serializedProperties.Add(nameof(_stateSoundNames), JObject.FromObject(_stateSoundNames)); - return FixJson(serializedProperties); - } - - public override JObject FixJson(JObject json) - { - json.Remove("HoverSound"); - json.Remove("MouseUpSound"); - json.Remove("MouseDownSound"); - return base.FixJson(json); - } - - public override void LoadJson(JToken token, bool isRoot = default) - { - base.LoadJson(token, isRoot); - - if (token is not JObject obj) - { - return; - } - - if (obj.TryGetValue(nameof(_stateSoundNames), out var tokenStateSoundNames) && tokenStateSoundNames is JObject valueStateSoundNames) - { - foreach (var (propertyName, propertyValueToken) in valueStateSoundNames) - { - if (!Enum.TryParse(propertyName, out ButtonSoundState buttonSoundState) || - buttonSoundState == ButtonSoundState.None) - { - continue; - } - - if (propertyValueToken is not JValue { Type: JTokenType.String } valuePropertyValue) - { - continue; - } - - var stringPropertyValue = valuePropertyValue.Value()?.Trim(); - if (stringPropertyValue is { Length: > 0 }) - { - _stateSoundNames[buttonSoundState] = stringPropertyValue; - } - else - { - _stateSoundNames.Remove(buttonSoundState); - } - } - } - } }