1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 06:43:03 +08:00
osu-lazer/osu.Game/Overlays/Settings/Sections/Input/TabletAreaSelection.cs

253 lines
8.4 KiB
C#
Raw Normal View History

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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
2021-03-15 15:25:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.MatrixExtensions;
2021-03-15 15:25:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
2021-03-15 15:25:50 +08:00
using osu.Framework.Graphics.Shapes;
2023-02-20 13:06:20 +08:00
using osu.Framework.Input.Events;
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;
namespace osu.Game.Overlays.Settings.Sections.Input
2021-03-15 15:25:50 +08:00
{
2022-11-24 13:32:20 +08:00
public partial class TabletAreaSelection : CompositeDrawable
2021-03-15 15:25:50 +08:00
{
2021-09-04 14:35:46 +08:00
public bool IsWithinBounds { get; private set; }
private readonly ITabletHandler handler;
2021-03-15 15:25:50 +08:00
private Container tabletContainer;
private Container usableAreaContainer;
2021-03-15 15:25:50 +08:00
private readonly Bindable<Vector2> areaOffset = new Bindable<Vector2>();
private readonly Bindable<Vector2> areaSize = new Bindable<Vector2>();
2021-03-30 11:40:50 +08:00
private readonly BindableNumber<float> rotation = new BindableNumber<float>();
private readonly IBindable<TabletInfo> tablet = new Bindable<TabletInfo>();
2021-03-16 15:23:46 +08:00
private OsuSpriteText tabletName;
private Box usableFill;
private OsuSpriteText usableAreaText;
public TabletAreaSelection(ITabletHandler handler)
2021-03-15 15:25:50 +08:00
{
this.handler = handler;
Padding = new MarginPadding { Horizontal = SettingsPanel.CONTENT_MARGINS };
}
2021-03-15 15:25:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
InternalChild = tabletContainer = new Container
2021-03-15 15:25:50 +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,
Children = new Drawable[]
2021-03-15 15:25:50 +08:00
{
new Box
2021-03-15 15:25:50 +08:00
{
RelativeSizeAxes = Axes.Both,
2021-03-16 15:23:46 +08:00
Colour = colour.Gray1,
},
2023-02-20 13:06:20 +08:00
usableAreaContainer = new UsableAreaContainer(handler)
{
Origin = Anchor.Centre,
Children = new Drawable[]
2021-03-15 15:25:50 +08:00
{
usableFill = new Box
{
RelativeSizeAxes = Axes.Both,
2021-03-16 15:23:46 +08:00
Alpha = 0.6f,
},
2021-03-17 12:05:43 +08:00
new Box
{
Colour = Color4.White,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Height = 5,
},
new Box
{
Colour = Color4.White,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Width = 5,
},
usableAreaText = new OsuSpriteText
2021-03-15 15:25:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Colour = Color4.White,
2021-03-17 12:05:43 +08:00
Font = OsuFont.Default.With(size: 12),
Y = 10
2021-03-15 15:25:50 +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
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-03-15 15:25:50 +08:00
areaOffset.BindTo(handler.AreaOffset);
areaOffset.BindValueChanged(val =>
{
usableAreaContainer.MoveTo(val.NewValue, 100, Easing.OutQuint);
checkBounds();
}, true);
2021-03-15 15:25:50 +08:00
areaSize.BindTo(handler.AreaSize);
areaSize.BindValueChanged(val =>
{
usableAreaContainer.ResizeTo(val.NewValue, 100, Easing.OutQuint);
2022-11-12 00:14:34 +08:00
int x = (int)Math.Round(val.NewValue.X);
int y = (int)Math.Round(val.NewValue.Y);
int commonDivider = greatestCommonDivider(x, y);
2022-11-12 00:14:34 +08:00
usableAreaText.Text = $"{x / commonDivider}:{y / commonDivider}";
checkBounds();
}, true);
2021-03-30 11:40:50 +08:00
rotation.BindTo(handler.Rotation);
rotation.BindValueChanged(val =>
{
usableAreaContainer.RotateTo(val.NewValue, 100, Easing.OutQuint);
tabletContainer.RotateTo(-val.NewValue, 800, Easing.OutQuint);
checkBounds();
2021-04-21 15:12:09 +08:00
}, true);
2021-03-30 11:40:50 +08:00
tablet.BindTo(handler.Tablet);
tablet.BindValueChanged(_ => Scheduler.AddOnce(updateTabletDetails));
updateTabletDetails();
// initial animation should be instant.
FinishTransforms(true);
}
private void updateTabletDetails()
{
tabletContainer.Size = tablet.Value?.Size ?? Vector2.Zero;
tabletName.Text = tablet.Value?.Name ?? string.Empty;
checkBounds();
}
private static int greatestCommonDivider(int a, int b)
{
while (b != 0)
{
int remainder = a % b;
a = b;
b = remainder;
}
return a;
}
[Resolved]
private OsuColour colour { get; set; }
private void checkBounds()
{
if (tablet.Value == null)
return;
// allow for some degree of floating point error, as we don't care about being perfect here.
const float lenience = 0.5f;
var tabletArea = new Quad(-lenience, -lenience, tablet.Value.Size.X + lenience * 2, tablet.Value.Size.Y + lenience * 2);
var halfUsableArea = areaSize.Value / 2;
var offset = areaOffset.Value;
var usableAreaQuad = new Quad(
new Vector2(-halfUsableArea.X, -halfUsableArea.Y),
new Vector2(halfUsableArea.X, -halfUsableArea.Y),
new Vector2(-halfUsableArea.X, halfUsableArea.Y),
new Vector2(halfUsableArea.X, halfUsableArea.Y)
);
var matrix = Matrix3.Identity;
MatrixExtensions.TranslateFromLeft(ref matrix, offset);
2024-03-06 10:42:20 +08:00
MatrixExtensions.RotateFromLeft(ref matrix, float.DegreesToRadians(rotation.Value));
usableAreaQuad *= matrix;
IsWithinBounds =
tabletArea.Contains(usableAreaQuad.TopLeft) &&
tabletArea.Contains(usableAreaQuad.TopRight) &&
tabletArea.Contains(usableAreaQuad.BottomLeft) &&
tabletArea.Contains(usableAreaQuad.BottomRight);
2021-09-04 14:35:46 +08:00
usableFill.FadeColour(IsWithinBounds ? colour.Blue : colour.RedLight, 100);
}
protected override void Update()
{
base.Update();
2022-11-12 15:49:03 +08:00
if (!(tablet.Value?.Size is Vector2 size))
return;
2022-11-12 15:49:03 +08:00
float maxDimension = size.LengthFast;
2022-11-12 15:49:03 +08:00
float fitX = maxDimension / (DrawWidth - Padding.Left - Padding.Right);
float fitY = maxDimension / DrawHeight;
2022-11-12 15:49:03 +08:00
float adjust = MathF.Max(fitX, fitY);
tabletContainer.Scale = new Vector2(1 / adjust);
2021-03-15 15:25:50 +08:00
}
}
2023-02-20 13:06:20 +08:00
public partial class UsableAreaContainer : Container
{
private readonly Bindable<Vector2> areaOffset;
public UsableAreaContainer(ITabletHandler tabletHandler)
{
areaOffset = tabletHandler.AreaOffset.GetBoundCopy();
}
protected override bool OnDragStart(DragStartEvent e) => true;
protected override void OnDrag(DragEvent e)
{
var newPos = Position + e.Delta;
this.MoveTo(Vector2.Clamp(newPos, Vector2.Zero, Parent!.Size));
2023-02-20 13:06:20 +08:00
}
protected override void OnDragEnd(DragEndEvent e)
{
areaOffset.Value = Position;
base.OnDragEnd(e);
}
}
2021-03-15 15:25:50 +08:00
}