Skip to content

Commit

Permalink
transfer sound logic from child to parent
Browse files Browse the repository at this point in the history
  • Loading branch information
WeylonSantana committed Feb 26, 2025
1 parent fb3c1a7 commit c2ff7a7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 161 deletions.
111 changes: 81 additions & 30 deletions Intersect.Client.Framework/Gwen/ControlInternal/Dragger.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ButtonSoundState, string> _stateSoundNames = [];
protected DateTime _ignoreMouseUpSoundsUntil;

private IGameTexture? mClickedImage;
private string? mClickedImageFilename;
Expand Down Expand Up @@ -54,6 +53,16 @@ internal Base Target
/// </summary>
public event GwenEventHandler<EventArgs>? 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);
Expand All @@ -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);
Expand All @@ -80,7 +89,7 @@ protected override void OnMouseUp(MouseButton mouseButton, Point mousePosition,

if (userAction)
{
base.PlaySound(mMouseUpSound);
PlaySound(_stateSoundNames[ButtonSoundState.MouseUp]);
}

InputHandler.MouseFocus = null;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<string>()?.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];
}

/// <summary>
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
131 changes: 0 additions & 131 deletions Intersect.Client.Framework/Gwen/ControlInternal/ScrollBarBar.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,10 +11,6 @@ public partial class ScrollBarBar : Dragger
{
private bool mHorizontal;

private readonly Dictionary<ButtonSoundState, string> _stateSoundNames = [];

private DateTime _ignoreMouseUpSoundsUntil;

/// <summary>
/// Initializes a new instance of the <see cref="ScrollBarBar" /> class.
/// </summary>
Expand Down Expand Up @@ -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);
}
}

/// <summary>
/// Lays out the control's interior according to alignment, padding, dock etc.
/// </summary>
Expand All @@ -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<string>()?.Trim();
if (stringPropertyValue is { Length: > 0 })
{
_stateSoundNames[buttonSoundState] = stringPropertyValue;
}
else
{
_stateSoundNames.Remove(buttonSoundState);
}
}
}
}
}

0 comments on commit c2ff7a7

Please sign in to comment.