2019-01-24 16:43:03 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-06 08:22:37 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-02-22 16:14:00 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2018-11-14 17:02:38 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2021-08-11 17:10:08 +08:00
|
|
|
|
using osu.Game.Localisation;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-05-15 09:55:29 +08:00
|
|
|
|
namespace osu.Game.Overlays.Settings.Sections.Audio
|
2017-02-06 19:26:32 +08:00
|
|
|
|
{
|
2017-05-15 09:55:29 +08:00
|
|
|
|
public class AudioDevicesSettings : SettingsSubsection
|
2017-02-06 19:26:32 +08:00
|
|
|
|
{
|
2021-08-11 17:10:08 +08:00
|
|
|
|
protected override LocalisableString Header => AudioSettingsStrings.AudioDevicesHeader;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-02-14 21:14:00 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private AudioManager audio { get; set; }
|
2020-02-14 21:30:27 +08:00
|
|
|
|
|
2017-05-15 09:55:29 +08:00
|
|
|
|
private SettingsDropdown<string> dropdown;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-08-16 18:17:36 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
2017-02-06 08:22:37 +08:00
|
|
|
|
{
|
2021-08-16 18:17:36 +08:00
|
|
|
|
Children = new Drawable[]
|
2017-08-16 19:16:40 +08:00
|
|
|
|
{
|
2021-08-16 18:17:36 +08:00
|
|
|
|
dropdown = new AudioDeviceSettingsDropdown
|
|
|
|
|
{
|
2021-10-12 13:42:22 +08:00
|
|
|
|
LabelText = AudioSettingsStrings.OutputDevice,
|
2021-08-16 18:17:36 +08:00
|
|
|
|
Keywords = new[] { "speaker", "headphone", "output" }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateItems();
|
|
|
|
|
|
|
|
|
|
audio.OnNewDevice += onDeviceChanged;
|
|
|
|
|
audio.OnLostDevice += onDeviceChanged;
|
|
|
|
|
dropdown.Current = audio.AudioDevice;
|
2017-02-11 23:29:33 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-08-16 18:17:36 +08:00
|
|
|
|
private void onDeviceChanged(string name) => updateItems();
|
|
|
|
|
|
2017-02-11 23:29:33 +08:00
|
|
|
|
private void updateItems()
|
|
|
|
|
{
|
2018-11-14 17:02:38 +08:00
|
|
|
|
var deviceItems = new List<string> { string.Empty };
|
|
|
|
|
deviceItems.AddRange(audio.AudioDeviceNames);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
|
string preferredDeviceName = audio.AudioDevice.Value;
|
2018-11-14 17:02:38 +08:00
|
|
|
|
if (deviceItems.All(kv => kv != preferredDeviceName))
|
|
|
|
|
deviceItems.Add(preferredDeviceName);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-04-07 23:00:34 +08:00
|
|
|
|
// The option dropdown for audio device selection lists all audio
|
|
|
|
|
// device names. Dropdowns, however, may not have multiple identical
|
|
|
|
|
// keys. Thus, we remove duplicate audio device names from
|
|
|
|
|
// the dropdown. BASS does not give us a simple mechanism to select
|
2017-04-08 17:01:41 +08:00
|
|
|
|
// specific audio devices in such a case anyways. Such
|
|
|
|
|
// functionality would require involved OS-specific code.
|
2017-04-07 23:00:34 +08:00
|
|
|
|
dropdown.Items = deviceItems.Distinct().ToList();
|
2017-02-11 23:29:33 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-08-16 18:17:36 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
2017-02-11 23:29:33 +08:00
|
|
|
|
{
|
2021-08-16 18:17:36 +08:00
|
|
|
|
base.Dispose(isDisposing);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-08-16 18:17:36 +08:00
|
|
|
|
if (audio != null)
|
2017-02-06 19:26:32 +08:00
|
|
|
|
{
|
2021-08-16 18:17:36 +08:00
|
|
|
|
audio.OnNewDevice -= onDeviceChanged;
|
|
|
|
|
audio.OnLostDevice -= onDeviceChanged;
|
|
|
|
|
}
|
2017-02-06 19:26:32 +08:00
|
|
|
|
}
|
2018-11-14 17:02:38 +08:00
|
|
|
|
|
|
|
|
|
private class AudioDeviceSettingsDropdown : SettingsDropdown<string>
|
|
|
|
|
{
|
2019-06-25 11:00:05 +08:00
|
|
|
|
protected override OsuDropdown<string> CreateDropdown() => new AudioDeviceDropdownControl();
|
2018-11-14 17:02:38 +08:00
|
|
|
|
|
|
|
|
|
private class AudioDeviceDropdownControl : DropdownControl
|
|
|
|
|
{
|
2021-02-22 16:14:00 +08:00
|
|
|
|
protected override LocalisableString GenerateItemText(string item)
|
2021-08-11 17:10:08 +08:00
|
|
|
|
=> string.IsNullOrEmpty(item) ? CommonStrings.Default : base.GenerateItemText(item);
|
2018-11-14 17:02:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-06 19:26:32 +08:00
|
|
|
|
}
|
2018-01-05 19:21:19 +08:00
|
|
|
|
}
|