2021-03-15 15:25:50 +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.
|
|
|
|
|
2021-03-16 13:45:21 +08:00
|
|
|
using System;
|
2021-03-15 15:25:50 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-03-15 17:37:46 +08:00
|
|
|
using osu.Framework.Bindables;
|
2021-03-15 15:25:50 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2021-03-15 17:37:46 +08:00
|
|
|
using osu.Framework.Input.Handlers.Tablet;
|
2021-03-15 15:25:50 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osuTK;
|
|
|
|
using osuTK.Graphics;
|
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
namespace osu.Game.Overlays.Settings.Sections.Input
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-15 17:37:46 +08:00
|
|
|
public class TabletAreaSelection : CompositeDrawable
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-15 17:37:46 +08:00
|
|
|
private readonly ITabletHandler handler;
|
2021-03-15 15:25:50 +08:00
|
|
|
|
2021-03-16 13:45:21 +08:00
|
|
|
private Container tabletContainer;
|
|
|
|
private Container usableAreaContainer;
|
2021-03-15 15:25:50 +08:00
|
|
|
|
2021-03-17 11:50:02 +08:00
|
|
|
private readonly Bindable<Vector2> areaOffset = new Bindable<Vector2>();
|
|
|
|
private readonly Bindable<Vector2> areaSize = new Bindable<Vector2>();
|
|
|
|
|
|
|
|
private readonly IBindable<TabletInfo> tablet = new Bindable<TabletInfo>();
|
2021-03-15 17:37:46 +08:00
|
|
|
|
2021-03-16 15:23:46 +08:00
|
|
|
private OsuSpriteText tabletName;
|
|
|
|
|
2021-03-17 12:03:06 +08:00
|
|
|
private Box usableFill;
|
|
|
|
private OsuSpriteText usableAreaText;
|
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
public TabletAreaSelection(ITabletHandler handler)
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-15 17:37:46 +08:00
|
|
|
this.handler = handler;
|
2021-03-16 22:57:05 +08:00
|
|
|
|
|
|
|
Padding = new MarginPadding { Horizontal = SettingsPanel.CONTENT_MARGINS };
|
2021-03-16 13:45:21 +08:00
|
|
|
}
|
2021-03-15 15:25:50 +08:00
|
|
|
|
2021-03-16 13:45:21 +08:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
InternalChild = tabletContainer = new Container
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-16 13:45:21 +08:00
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
Masking = true,
|
|
|
|
CornerRadius = 5,
|
|
|
|
BorderThickness = 2,
|
2021-03-16 15:23:46 +08:00
|
|
|
BorderColour = colour.Gray3,
|
2021-03-16 13:45:21 +08:00
|
|
|
Children = new Drawable[]
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-16 13:45:21 +08:00
|
|
|
new Box
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-16 13:45:21 +08:00
|
|
|
RelativeSizeAxes = Axes.Both,
|
2021-03-16 15:23:46 +08:00
|
|
|
Colour = colour.Gray1,
|
2021-03-16 13:45:21 +08:00
|
|
|
},
|
|
|
|
usableAreaContainer = new Container
|
|
|
|
{
|
2021-03-17 11:50:02 +08:00
|
|
|
Origin = Anchor.Centre,
|
2021-03-16 13:45:21 +08:00
|
|
|
Children = new Drawable[]
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-17 12:03:06 +08:00
|
|
|
usableFill = new Box
|
2021-03-16 13:45:21 +08:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2021-03-16 15:23:46 +08:00
|
|
|
Alpha = 0.6f,
|
2021-03-16 13:45:21 +08:00
|
|
|
},
|
2021-03-17 12:03:06 +08:00
|
|
|
usableAreaText = new OsuSpriteText
|
2021-03-15 15:25:50 +08:00
|
|
|
{
|
2021-03-16 13:45:21 +08:00
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
Origin = Anchor.Centre,
|
2021-03-17 12:03:06 +08:00
|
|
|
Colour = Color4.White,
|
2021-03-16 13:45:21 +08:00
|
|
|
Font = OsuFont.Default.With(size: 12)
|
2021-03-15 15:25:50 +08:00
|
|
|
}
|
2021-03-16 13:45:21 +08:00
|
|
|
}
|
|
|
|
},
|
2021-03-16 15:23:46 +08:00
|
|
|
tabletName = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Padding = new MarginPadding(3),
|
|
|
|
Font = OsuFont.Default.With(size: 8)
|
|
|
|
},
|
2021-03-15 15:25:50 +08:00
|
|
|
}
|
|
|
|
};
|
2021-03-16 22:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2021-03-15 15:25:50 +08:00
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
areaOffset.BindTo(handler.AreaOffset);
|
|
|
|
areaOffset.BindValueChanged(val =>
|
|
|
|
{
|
2021-03-17 11:50:02 +08:00
|
|
|
usableAreaContainer.MoveTo(val.NewValue, 100, Easing.OutQuint)
|
|
|
|
.OnComplete(_ => checkBounds()); // required as we are using SSDQ.
|
2021-03-15 17:37:46 +08:00
|
|
|
}, true);
|
2021-03-15 15:25:50 +08:00
|
|
|
|
2021-03-15 17:37:46 +08:00
|
|
|
areaSize.BindTo(handler.AreaSize);
|
|
|
|
areaSize.BindValueChanged(val =>
|
|
|
|
{
|
2021-03-17 11:50:02 +08:00
|
|
|
usableAreaContainer.ResizeTo(val.NewValue, 100, Easing.OutQuint)
|
|
|
|
.OnComplete(_ => checkBounds()); // required as we are using SSDQ.
|
2021-03-17 12:03:06 +08:00
|
|
|
|
|
|
|
int x = (int)val.NewValue.X;
|
|
|
|
int y = (int)val.NewValue.Y;
|
|
|
|
int commonDivider = greatestCommonDivider(x, y);
|
|
|
|
|
|
|
|
usableAreaText.Text = $"{(float)x / commonDivider}:{(float)y / commonDivider}";
|
2021-03-15 17:37:46 +08:00
|
|
|
}, true);
|
|
|
|
|
2021-03-17 11:50:02 +08:00
|
|
|
tablet.BindTo(handler.Tablet);
|
|
|
|
tablet.BindValueChanged(val =>
|
2021-03-15 17:37:46 +08:00
|
|
|
{
|
2021-03-17 11:50:02 +08:00
|
|
|
tabletContainer.Size = val.NewValue?.Size ?? Vector2.Zero;
|
|
|
|
tabletName.Text = val.NewValue?.Name ?? string.Empty;
|
2021-03-16 15:02:39 +08:00
|
|
|
checkBounds();
|
2021-03-16 22:47:18 +08:00
|
|
|
}, true);
|
2021-03-17 11:50:02 +08:00
|
|
|
|
|
|
|
// initial animation should be instant.
|
|
|
|
FinishTransforms(true);
|
2021-03-16 13:45:21 +08:00
|
|
|
}
|
|
|
|
|
2021-03-17 12:03:06 +08:00
|
|
|
private static int greatestCommonDivider(int a, int b)
|
|
|
|
{
|
|
|
|
while (b != 0)
|
|
|
|
{
|
|
|
|
int remainder = a % b;
|
|
|
|
a = b;
|
|
|
|
b = remainder;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2021-03-16 15:02:39 +08:00
|
|
|
[Resolved]
|
|
|
|
private OsuColour colour { get; set; }
|
|
|
|
|
|
|
|
private void checkBounds()
|
|
|
|
{
|
2021-03-17 11:50:02 +08:00
|
|
|
if (tablet.Value == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var usableSsdq = usableAreaContainer.ScreenSpaceDrawQuad;
|
2021-03-16 15:02:39 +08:00
|
|
|
|
2021-03-17 11:50:02 +08:00
|
|
|
bool isWithinBounds = tabletContainer.ScreenSpaceDrawQuad.Contains(usableSsdq.TopLeft) &&
|
|
|
|
tabletContainer.ScreenSpaceDrawQuad.Contains(usableSsdq.BottomRight);
|
2021-03-16 15:02:39 +08:00
|
|
|
|
2021-03-17 12:03:06 +08:00
|
|
|
usableFill.FadeColour(isWithinBounds ? colour.Blue : colour.RedLight, 100);
|
2021-03-16 15:02:39 +08:00
|
|
|
}
|
|
|
|
|
2021-03-16 13:45:21 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2021-03-17 11:50:02 +08:00
|
|
|
if (!(tablet.Value?.Size is Vector2 size))
|
2021-03-16 17:40:21 +08:00
|
|
|
return;
|
|
|
|
|
2021-03-17 11:50:02 +08:00
|
|
|
float fitX = size.X / (DrawWidth - Padding.Left - Padding.Right);
|
|
|
|
float fitY = size.Y / DrawHeight;
|
2021-03-16 13:45:21 +08:00
|
|
|
|
|
|
|
float adjust = MathF.Max(fitX, fitY);
|
|
|
|
|
|
|
|
tabletContainer.Scale = new Vector2(1 / adjust);
|
2021-03-15 15:25:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|