1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 20:07:26 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sections/Graphics/LayoutSettings.cs

141 lines
5.6 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Collections.Generic;
2018-06-10 21:17:57 +08:00
using System.Drawing;
using System.Linq;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Platform;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Settings.Sections.Graphics
{
public class LayoutSettings : SettingsSubsection
{
protected override string Header => "Layout";
private FillFlowContainer letterboxSettings;
private Bindable<bool> letterboxing;
2018-06-10 21:17:57 +08:00
private Bindable<Size> sizeFullscreen;
2018-06-22 01:42:22 +08:00
private readonly BindableInt resolutionDropdownBindable = new BindableInt();
2018-04-13 17:19:50 +08:00
private OsuGameBase game;
private SettingsDropdown<int> resolutionDropdown;
private SettingsEnumDropdown<WindowMode> windowModeDropdown;
2018-06-10 21:17:57 +08:00
2018-04-13 17:19:50 +08:00
private const int transition_duration = 400;
[BackgroundDependencyLoader]
private void load(FrameworkConfigManager config, OsuGameBase game)
2018-04-13 17:19:50 +08:00
{
this.game = game;
2018-04-13 17:19:50 +08:00
letterboxing = config.GetBindable<bool>(FrameworkSetting.Letterboxing);
2018-06-10 21:17:57 +08:00
sizeFullscreen = config.GetBindable<Size>(FrameworkSetting.SizeFullscreen);
2018-06-22 01:42:22 +08:00
sizeFullscreen.ValueChanged += size =>
{
KeyValuePair<string, int> valuePair = getResolutions().FirstOrDefault(r => r.Key.StartsWith($"{size.Width}x{size.Height}"));
resolutionDropdownBindable.Value = valuePair.Value;
};
2018-06-10 21:17:57 +08:00
2018-06-13 00:27:55 +08:00
resolutionDropdownBindable.ValueChanged += resolution =>
2018-06-10 21:17:57 +08:00
{
2018-06-22 01:42:22 +08:00
var newSelection = getResolutions().First(r => r.Value == resolution);
var newSelectionParts = newSelection.Key.Split('x');
var newSelectionWidth = int.Parse(newSelectionParts.First());
var newSelectionHeight = int.Parse(newSelectionParts.Last());
if (sizeFullscreen.Value.Width != newSelectionWidth || sizeFullscreen.Value.Height != newSelectionHeight)
sizeFullscreen.Value = new Size(newSelectionWidth, newSelectionHeight);
2018-06-10 21:17:57 +08:00
};
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
windowModeDropdown = new SettingsEnumDropdown<WindowMode>
2018-04-13 17:19:50 +08:00
{
LabelText = "Screen mode",
Bindable = config.GetBindable<WindowMode>(FrameworkSetting.WindowMode),
},
resolutionDropdown = new SettingsDropdown<int>
{
LabelText = "Resolution",
2018-06-22 01:42:22 +08:00
Items = getResolutions(),
2018-06-10 21:17:57 +08:00
Bindable = resolutionDropdownBindable
},
2018-04-13 17:19:50 +08:00
new SettingsCheckbox
{
LabelText = "Letterboxing",
Bindable = letterboxing,
},
letterboxSettings = new FillFlowContainer
{
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
AutoSizeDuration = transition_duration,
AutoSizeEasing = Easing.OutQuint,
Masking = true,
Children = new Drawable[]
{
new SettingsSlider<double>
{
LabelText = "Horizontal position",
Bindable = config.GetBindable<double>(FrameworkSetting.LetterboxPositionX),
KeyboardStep = 0.1f
},
new SettingsSlider<double>
{
LabelText = "Vertical position",
Bindable = config.GetBindable<double>(FrameworkSetting.LetterboxPositionY),
KeyboardStep = 0.1f
},
}
},
};
2018-06-13 00:27:55 +08:00
windowModeDropdown.Bindable.ValueChanged += windowMode =>
{
2018-06-13 00:27:55 +08:00
if (windowMode == WindowMode.Fullscreen)
2018-06-22 01:42:22 +08:00
{
resolutionDropdown.Show();
2018-06-22 01:42:22 +08:00
sizeFullscreen.TriggerChange();
}
else
resolutionDropdown.Hide();
};
windowModeDropdown.Bindable.TriggerChange();
2018-04-13 17:19:50 +08:00
letterboxing.ValueChanged += isVisible =>
{
letterboxSettings.ClearTransforms();
letterboxSettings.AutoSizeAxes = isVisible ? Axes.Y : Axes.None;
if (!isVisible)
letterboxSettings.ResizeHeightTo(0, transition_duration, Easing.OutQuint);
};
letterboxing.TriggerChange();
}
2018-06-10 21:17:57 +08:00
private List<KeyValuePair<string, int>> getResolutions()
{
var availableDisplayResolutions = (game.Window as DesktopGameWindow)?.GetCurrentDisplay().AvailableResolutions
.Where(r => r.Width >= 800 && r.Height >= 600)
.OrderByDescending(r => r.Width).ThenByDescending(r => r.Height);
2018-06-10 21:17:57 +08:00
if (availableDisplayResolutions == null)
return new List<KeyValuePair<string, int>>();
var availableDisplayResolutionsStr = availableDisplayResolutions.Select(r => $"{r.Width}x{r.Height}").Distinct();
2018-06-10 21:17:57 +08:00
return availableDisplayResolutionsStr.Select((t, i) => new KeyValuePair<string, int>(t, i)).ToList();
}
2018-04-13 17:19:50 +08:00
}
}