1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 19:02:55 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sections/Input/TabletSettings.cs

344 lines
13 KiB
C#
Raw Normal View History

// 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.
using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Handlers.Tablet;
using osu.Framework.Localisation;
using osu.Framework.Platform;
using osu.Framework.Threading;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osuTK;
using osu.Game.Localisation;
using osu.Game.Online.Chat;
namespace osu.Game.Overlays.Settings.Sections.Input
{
public class TabletSettings : SettingsSubsection
{
2021-09-04 14:35:46 +08:00
public TabletAreaSelection AreaSelection { get; private set; }
private readonly ITabletHandler tabletHandler;
private readonly Bindable<bool> enabled = new BindableBool(true);
private readonly Bindable<Vector2> areaOffset = new Bindable<Vector2>();
private readonly Bindable<Vector2> areaSize = new Bindable<Vector2>();
private readonly IBindable<TabletInfo> tablet = new Bindable<TabletInfo>();
private readonly BindableNumber<float> offsetX = new BindableNumber<float> { MinValue = 0 };
private readonly BindableNumber<float> offsetY = new BindableNumber<float> { MinValue = 0 };
private readonly BindableNumber<float> sizeX = new BindableNumber<float> { MinValue = 10 };
private readonly BindableNumber<float> sizeY = new BindableNumber<float> { MinValue = 10 };
2021-03-30 11:40:50 +08:00
private readonly BindableNumber<float> rotation = new BindableNumber<float> { MinValue = 0, MaxValue = 360 };
[Resolved]
private GameHost host { get; set; }
/// <summary>
/// Based on ultrawide monitor configurations.
/// </summary>
private const float largest_feasible_aspect_ratio = 21f / 9;
2021-03-16 16:04:22 +08:00
private readonly BindableNumber<float> aspectRatio = new BindableFloat(1)
{
MinValue = 1 / largest_feasible_aspect_ratio,
MaxValue = largest_feasible_aspect_ratio,
2021-03-16 16:04:22 +08:00
Precision = 0.01f,
};
private readonly BindableBool aspectLock = new BindableBool();
private ScheduledDelegate aspectRatioApplication;
private FillFlowContainer mainSettings;
private FillFlowContainer noTabletMessage;
protected override LocalisableString Header => TabletSettingsStrings.Tablet;
public TabletSettings(ITabletHandler tabletHandler)
{
this.tabletHandler = tabletHandler;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Children = new Drawable[]
{
new SettingsCheckbox
{
LabelText = CommonStrings.Enabled,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Current = enabled,
},
noTabletMessage = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Padding = new MarginPadding { Horizontal = SettingsPanel.CONTENT_MARGINS },
Spacing = new Vector2(5f),
Children = new Drawable[]
{
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = TabletSettingsStrings.NoTabletDetected,
},
new LinkFlowContainer(cp => cp.Colour = colours.Yellow)
{
TextAnchor = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
}.With(t =>
{
2021-08-13 14:26:53 +08:00
if (RuntimeInfo.OS == RuntimeInfo.Platform.Windows || RuntimeInfo.OS == RuntimeInfo.Platform.Linux)
{
t.NewLine();
t.AddText("If your tablet is not detected, please read ");
t.AddLink("this FAQ", LinkAction.External, RuntimeInfo.OS == RuntimeInfo.Platform.Windows
? @"https://opentabletdriver.net/Wiki/FAQ/Windows"
: @"https://opentabletdriver.net/Wiki/FAQ/Linux");
t.AddText(" for troubleshooting steps.");
2021-08-13 14:26:53 +08:00
}
}),
}
},
mainSettings = new FillFlowContainer
{
Alpha = 0,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0, 8),
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
2021-09-04 14:35:46 +08:00
AreaSelection = new TabletAreaSelection(tabletHandler)
{
RelativeSizeAxes = Axes.X,
Height = 300,
},
new DangerousSettingsButton
{
Text = TabletSettingsStrings.ResetToFullArea,
Action = () =>
{
aspectLock.Value = false;
areaOffset.SetDefault();
areaSize.SetDefault();
},
},
new SettingsButton
{
Text = TabletSettingsStrings.ConformToCurrentGameAspectRatio,
Action = () =>
{
forceAspectRatio((float)host.Window.ClientSize.Width / host.Window.ClientSize.Height);
}
},
new SettingsSlider<float>
{
2021-03-17 12:23:23 +08:00
TransferValueOnCommit = true,
LabelText = TabletSettingsStrings.XOffset,
Current = offsetX
},
new SettingsSlider<float>
{
2021-03-17 12:23:23 +08:00
TransferValueOnCommit = true,
LabelText = TabletSettingsStrings.YOffset,
Current = offsetY
},
new SettingsSlider<float>
2021-03-30 11:40:50 +08:00
{
TransferValueOnCommit = true,
LabelText = TabletSettingsStrings.Rotation,
2021-03-30 11:40:50 +08:00
Current = rotation
},
new RotationPresetButtons(tabletHandler)
{
Padding = new MarginPadding
{
Horizontal = SettingsPanel.CONTENT_MARGINS
}
},
2021-03-30 11:40:50 +08:00
new SettingsSlider<float>
{
TransferValueOnCommit = true,
LabelText = TabletSettingsStrings.AspectRatio,
Current = aspectRatio
},
new SettingsCheckbox
{
LabelText = TabletSettingsStrings.LockAspectRatio,
Current = aspectLock
},
new SettingsSlider<float>
{
2021-03-17 12:23:23 +08:00
TransferValueOnCommit = true,
LabelText = CommonStrings.Width,
Current = sizeX
},
new SettingsSlider<float>
{
2021-03-17 12:23:23 +08:00
TransferValueOnCommit = true,
LabelText = CommonStrings.Height,
Current = sizeY
},
}
},
};
}
protected override void LoadComplete()
{
base.LoadComplete();
enabled.BindTo(tabletHandler.Enabled);
enabled.BindValueChanged(_ => Scheduler.AddOnce(updateVisibility));
2021-03-30 11:40:50 +08:00
rotation.BindTo(tabletHandler.Rotation);
areaOffset.BindTo(tabletHandler.AreaOffset);
areaOffset.BindValueChanged(val =>
{
offsetX.Value = val.NewValue.X;
offsetY.Value = val.NewValue.Y;
}, true);
offsetX.BindValueChanged(val => areaOffset.Value = new Vector2(val.NewValue, areaOffset.Value.Y));
offsetY.BindValueChanged(val => areaOffset.Value = new Vector2(areaOffset.Value.X, val.NewValue));
areaSize.BindTo(tabletHandler.AreaSize);
areaSize.BindValueChanged(val =>
{
sizeX.Value = val.NewValue.X;
sizeY.Value = val.NewValue.Y;
}, true);
sizeX.BindValueChanged(val =>
{
areaSize.Value = new Vector2(val.NewValue, areaSize.Value.Y);
2021-03-16 16:04:22 +08:00
aspectRatioApplication?.Cancel();
aspectRatioApplication = Schedule(() => applyAspectRatio(sizeX));
});
sizeY.BindValueChanged(val =>
{
areaSize.Value = new Vector2(areaSize.Value.X, val.NewValue);
aspectRatioApplication?.Cancel();
aspectRatioApplication = Schedule(() => applyAspectRatio(sizeY));
});
updateAspectRatio();
aspectRatio.BindValueChanged(aspect =>
{
2021-03-16 17:14:29 +08:00
aspectRatioApplication?.Cancel();
aspectRatioApplication = Schedule(() => forceAspectRatio(aspect.NewValue));
});
tablet.BindTo(tabletHandler.Tablet);
tablet.BindValueChanged(val =>
{
Scheduler.AddOnce(updateVisibility);
var tab = val.NewValue;
bool tabletFound = tab != null;
if (!tabletFound)
return;
offsetX.MaxValue = tab.Size.X;
offsetX.Default = tab.Size.X / 2;
sizeX.Default = sizeX.MaxValue = tab.Size.X;
offsetY.MaxValue = tab.Size.Y;
offsetY.Default = tab.Size.Y / 2;
sizeY.Default = sizeY.MaxValue = tab.Size.Y;
areaSize.Default = new Vector2(sizeX.Default, sizeY.Default);
}, true);
}
private void updateVisibility()
{
2021-08-16 21:07:41 +08:00
mainSettings.Hide();
noTabletMessage.Hide();
if (!tabletHandler.Enabled.Value)
return;
2021-08-16 21:07:41 +08:00
if (tablet.Value != null)
mainSettings.Show();
else
noTabletMessage.Show();
}
private void applyAspectRatio(BindableNumber<float> sizeChanged)
{
2021-03-16 17:14:29 +08:00
try
{
2021-03-16 17:14:29 +08:00
if (!aspectLock.Value)
{
2021-03-20 19:34:41 +08:00
float proposedAspectRatio = currentAspectRatio;
2021-03-16 17:14:29 +08:00
if (proposedAspectRatio >= aspectRatio.MinValue && proposedAspectRatio <= aspectRatio.MaxValue)
{
// aspect ratio was in a valid range.
2021-03-16 17:14:29 +08:00
updateAspectRatio();
return;
}
}
// if lock is applied (or the specified values were out of range) aim to adjust the axis the user was not adjusting to conform.
if (sizeChanged == sizeX)
sizeY.Value = (int)(areaSize.Value.X / aspectRatio.Value);
2021-03-16 17:14:29 +08:00
else
sizeX.Value = (int)(areaSize.Value.Y * aspectRatio.Value);
}
2021-03-16 17:14:29 +08:00
finally
{
2021-03-16 17:14:29 +08:00
// cancel any event which may have fired while updating variables as a result of aspect ratio limitations.
// this avoids a potential feedback loop.
aspectRatioApplication?.Cancel();
}
2021-03-16 17:14:29 +08:00
}
private void forceAspectRatio(float aspectRatio)
{
aspectLock.Value = false;
int proposedHeight = (int)(sizeX.Value / aspectRatio);
if (proposedHeight < sizeY.MaxValue)
sizeY.Value = proposedHeight;
else
sizeX.Value = (int)(sizeY.Value * aspectRatio);
2021-03-16 17:14:29 +08:00
updateAspectRatio();
aspectRatioApplication?.Cancel();
aspectLock.Value = true;
}
2021-03-20 19:34:41 +08:00
private void updateAspectRatio() => aspectRatio.Value = currentAspectRatio;
2021-03-20 19:34:41 +08:00
private float currentAspectRatio => sizeX.Value / sizeY.Value;
}
}