2022-12-18 08:16:29 +00:00
|
|
|
using System.Diagnostics;
|
2020-06-28 04:36:53 +00:00
|
|
|
using System.IO;
|
2019-08-21 02:50:28 +00:00
|
|
|
using System.Media;
|
|
|
|
using PKHeX.Core;
|
2021-11-27 23:48:08 +00:00
|
|
|
using PKHeX.Drawing.PokeSprite;
|
2019-08-21 02:50:28 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.WinForms.Controls;
|
|
|
|
|
|
|
|
public sealed class CryPlayer
|
2019-08-21 02:50:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
private readonly SoundPlayer Sounds = new();
|
|
|
|
|
2022-12-18 08:16:29 +00:00
|
|
|
public void PlayCry(ISpeciesForm pk, EntityContext context)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
|
|
|
if (pk.Species == 0)
|
|
|
|
return;
|
|
|
|
|
2022-12-18 08:16:29 +00:00
|
|
|
string path = GetCryPath(pk, Main.CryPath, context);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!File.Exists(path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Sounds.SoundLocation = path;
|
|
|
|
try { Sounds.Play(); }
|
|
|
|
catch { Debug.WriteLine("Failed to play sound."); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(Sounds.SoundLocation))
|
|
|
|
return;
|
|
|
|
|
|
|
|
try { Sounds.Stop(); }
|
|
|
|
catch { Debug.WriteLine("Failed to stop sound."); }
|
|
|
|
}
|
|
|
|
|
2022-12-18 08:16:29 +00:00
|
|
|
private static string GetCryPath(ISpeciesForm pk, string cryFolder, EntityContext context)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-12-18 08:16:29 +00:00
|
|
|
var name = GetCryFileName(pk, context);
|
2022-06-18 18:04:24 +00:00
|
|
|
var path = Path.Combine(cryFolder, $"{name}.wav");
|
|
|
|
if (!File.Exists(path))
|
|
|
|
path = Path.Combine(cryFolder, $"{pk.Species}.wav");
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-12-18 08:16:29 +00:00
|
|
|
private static string GetCryFileName(ISpeciesForm pk, EntityContext context)
|
2019-08-21 02:50:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (pk.Species == (int)Species.Urshifu && pk.Form == 1) // same sprite for both forms, but different cries
|
|
|
|
return "892-1";
|
|
|
|
|
|
|
|
// don't grab sprite of pk, no gender specific cries
|
2022-12-18 08:16:29 +00:00
|
|
|
var res = SpriteName.GetResourceStringSprite(pk.Species, pk.Form, 0, 0, context);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
// people like - instead of _ file names ;)
|
|
|
|
return res.Replace('_', '-')[1..]; // skip leading underscore
|
2019-08-21 02:50:28 +00:00
|
|
|
}
|
2021-05-14 22:30:55 +00:00
|
|
|
}
|