2024-05-26 00:31:19 +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.
|
|
|
|
|
2024-05-26 02:17:27 +08:00
|
|
|
using System;
|
2024-05-26 00:31:19 +08:00
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2024-05-26 03:44:08 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2024-05-26 00:31:19 +08:00
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
|
|
|
using osu.Game.Rulesets.Osu.UI;
|
|
|
|
using osu.Game.Screens.Edit.Components.RadioButtons;
|
|
|
|
using osu.Game.Screens.Edit.Compose.Components;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.Edit
|
|
|
|
{
|
|
|
|
public partial class PreciseScalePopover : OsuPopover
|
|
|
|
{
|
|
|
|
private readonly SelectionScaleHandler scaleHandler;
|
|
|
|
|
|
|
|
private readonly Bindable<PreciseScaleInfo> scaleInfo = new Bindable<PreciseScaleInfo>(new PreciseScaleInfo(1, ScaleOrigin.PlayfieldCentre, true, true));
|
|
|
|
|
|
|
|
private SliderWithTextBoxInput<float> scaleInput = null!;
|
2024-05-26 02:17:27 +08:00
|
|
|
private BindableNumber<float> scaleInputBindable = null!;
|
2024-05-26 00:31:19 +08:00
|
|
|
private EditorRadioButtonCollection scaleOrigin = null!;
|
|
|
|
|
|
|
|
private RadioButton selectionCentreButton = null!;
|
|
|
|
|
2024-05-26 03:44:08 +08:00
|
|
|
private OsuCheckbox xCheckBox = null!;
|
|
|
|
private OsuCheckbox yCheckBox = null!;
|
|
|
|
|
2024-05-28 22:19:57 +08:00
|
|
|
private Bindable<bool> canScaleX = null!;
|
|
|
|
private Bindable<bool> canScaleY = null!;
|
|
|
|
|
2024-05-26 00:31:19 +08:00
|
|
|
public PreciseScalePopover(SelectionScaleHandler scaleHandler)
|
|
|
|
{
|
|
|
|
this.scaleHandler = scaleHandler;
|
|
|
|
|
|
|
|
AllowableAnchors = new[] { Anchor.CentreLeft, Anchor.CentreRight };
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
Child = new FillFlowContainer
|
|
|
|
{
|
|
|
|
Width = 220,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Spacing = new Vector2(20),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
scaleInput = new SliderWithTextBoxInput<float>("Scale:")
|
|
|
|
{
|
2024-05-26 02:17:27 +08:00
|
|
|
Current = scaleInputBindable = new BindableNumber<float>
|
2024-05-26 00:31:19 +08:00
|
|
|
{
|
|
|
|
MinValue = 0.5f,
|
|
|
|
MaxValue = 2,
|
|
|
|
Precision = 0.001f,
|
|
|
|
Value = 1,
|
|
|
|
Default = 1,
|
|
|
|
},
|
|
|
|
Instantaneous = true
|
|
|
|
},
|
|
|
|
scaleOrigin = new EditorRadioButtonCollection
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Items = new[]
|
|
|
|
{
|
|
|
|
new RadioButton("Playfield centre",
|
2024-05-26 02:17:27 +08:00
|
|
|
() => setOrigin(ScaleOrigin.PlayfieldCentre),
|
2024-05-26 00:31:19 +08:00
|
|
|
() => new SpriteIcon { Icon = FontAwesome.Regular.Square }),
|
|
|
|
selectionCentreButton = new RadioButton("Selection centre",
|
2024-05-26 02:17:27 +08:00
|
|
|
() => setOrigin(ScaleOrigin.SelectionCentre),
|
2024-05-26 00:31:19 +08:00
|
|
|
() => new SpriteIcon { Icon = FontAwesome.Solid.VectorSquare })
|
|
|
|
}
|
2024-05-26 03:44:08 +08:00
|
|
|
},
|
|
|
|
new FillFlowContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Spacing = new Vector2(4),
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
xCheckBox = new OsuCheckbox(false)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
LabelText = "X-axis",
|
|
|
|
Current = { Value = true },
|
|
|
|
},
|
|
|
|
yCheckBox = new OsuCheckbox(false)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
LabelText = "Y-axis",
|
|
|
|
Current = { Value = true },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2024-05-26 00:31:19 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
selectionCentreButton.Selected.DisabledChanged += isDisabled =>
|
|
|
|
{
|
|
|
|
selectionCentreButton.TooltipText = isDisabled ? "Select more than one object to perform selection-based scaling." : string.Empty;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2024-05-26 00:41:31 +08:00
|
|
|
ScheduleAfterChildren(() =>
|
|
|
|
{
|
|
|
|
scaleInput.TakeFocus();
|
|
|
|
scaleInput.SelectAll();
|
|
|
|
});
|
2024-05-26 00:31:19 +08:00
|
|
|
scaleInput.Current.BindValueChanged(scale => scaleInfo.Value = scaleInfo.Value with { Scale = scale.NewValue });
|
|
|
|
scaleOrigin.Items.First().Select();
|
|
|
|
|
2024-05-26 03:44:08 +08:00
|
|
|
xCheckBox.Current.BindValueChanged(x => setAxis(x.NewValue, yCheckBox.Current.Value));
|
|
|
|
yCheckBox.Current.BindValueChanged(y => setAxis(xCheckBox.Current.Value, y.NewValue));
|
|
|
|
|
2024-05-28 22:19:57 +08:00
|
|
|
// aggregate two values into canScaleFromSelectionCentre
|
|
|
|
canScaleX = scaleHandler.CanScaleX.GetBoundCopy();
|
|
|
|
canScaleX.BindValueChanged(_ => updateCanScaleFromSelectionCentre());
|
|
|
|
|
|
|
|
canScaleY = scaleHandler.CanScaleY.GetBoundCopy();
|
|
|
|
canScaleY.BindValueChanged(_ => updateCanScaleFromSelectionCentre(), true);
|
|
|
|
|
|
|
|
void updateCanScaleFromSelectionCentre() =>
|
|
|
|
selectionCentreButton.Selected.Disabled = !(scaleHandler.CanScaleY.Value || scaleHandler.CanScaleFromPlayfieldOrigin.Value);
|
2024-05-26 00:31:19 +08:00
|
|
|
|
|
|
|
scaleInfo.BindValueChanged(scale =>
|
|
|
|
{
|
|
|
|
var newScale = new Vector2(scale.NewValue.XAxis ? scale.NewValue.Scale : 1, scale.NewValue.YAxis ? scale.NewValue.Scale : 1);
|
2024-05-26 02:17:27 +08:00
|
|
|
scaleHandler.Update(newScale, getOriginPosition(scale.NewValue));
|
2024-05-26 00:31:19 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-26 02:17:27 +08:00
|
|
|
private void updateMaxScale()
|
|
|
|
{
|
|
|
|
if (!scaleHandler.OriginalSurroundingQuad.HasValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const float max_scale = 10;
|
|
|
|
var scale = scaleHandler.GetClampedScale(new Vector2(max_scale), getOriginPosition(scaleInfo.Value));
|
|
|
|
|
|
|
|
if (!scaleInfo.Value.XAxis)
|
|
|
|
scale.X = max_scale;
|
|
|
|
if (!scaleInfo.Value.YAxis)
|
|
|
|
scale.Y = max_scale;
|
|
|
|
|
|
|
|
scaleInputBindable.MaxValue = MathF.Max(1, MathF.Min(scale.X, scale.Y));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setOrigin(ScaleOrigin origin)
|
|
|
|
{
|
|
|
|
scaleInfo.Value = scaleInfo.Value with { Origin = origin };
|
|
|
|
updateMaxScale();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Vector2? getOriginPosition(PreciseScaleInfo scale) => scale.Origin == ScaleOrigin.PlayfieldCentre ? OsuPlayfield.BASE_SIZE / 2 : null;
|
|
|
|
|
2024-05-26 03:44:08 +08:00
|
|
|
private void setAxis(bool x, bool y)
|
|
|
|
{
|
|
|
|
scaleInfo.Value = scaleInfo.Value with { XAxis = x, YAxis = y };
|
|
|
|
updateMaxScale();
|
|
|
|
}
|
|
|
|
|
2024-05-26 00:31:19 +08:00
|
|
|
protected override void PopIn()
|
|
|
|
{
|
|
|
|
base.PopIn();
|
|
|
|
scaleHandler.Begin();
|
2024-05-26 02:17:27 +08:00
|
|
|
updateMaxScale();
|
2024-05-26 00:31:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void PopOut()
|
|
|
|
{
|
|
|
|
base.PopOut();
|
|
|
|
|
|
|
|
if (IsLoaded)
|
|
|
|
scaleHandler.Commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum ScaleOrigin
|
|
|
|
{
|
|
|
|
PlayfieldCentre,
|
|
|
|
SelectionCentre
|
|
|
|
}
|
|
|
|
|
|
|
|
public record PreciseScaleInfo(float Scale, ScaleOrigin Origin, bool XAxis, bool YAxis);
|
|
|
|
}
|