2024-05-25 16:31:19 +00: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-25 18:17:27 +00:00
|
|
|
using System;
|
2024-05-25 16:31:19 +00: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-25 19:44:08 +00:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2024-05-25 16:31:19 +00:00
|
|
|
using osu.Game.Graphics.UserInterfaceV2;
|
|
|
|
using osu.Game.Screens.Edit.Components.RadioButtons;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.Edit
|
|
|
|
{
|
|
|
|
public partial class PreciseScalePopover : OsuPopover
|
|
|
|
{
|
2024-05-28 15:12:16 +00:00
|
|
|
private readonly OsuSelectionScaleHandler scaleHandler;
|
2024-05-25 16:31:19 +00:00
|
|
|
|
2024-07-03 10:40:22 +00:00
|
|
|
private readonly OsuGridToolboxGroup gridToolbox;
|
|
|
|
|
2024-05-25 16:31:19 +00:00
|
|
|
private readonly Bindable<PreciseScaleInfo> scaleInfo = new Bindable<PreciseScaleInfo>(new PreciseScaleInfo(1, ScaleOrigin.PlayfieldCentre, true, true));
|
|
|
|
|
|
|
|
private SliderWithTextBoxInput<float> scaleInput = null!;
|
2024-05-25 18:17:27 +00:00
|
|
|
private BindableNumber<float> scaleInputBindable = null!;
|
2024-05-25 16:31:19 +00:00
|
|
|
private EditorRadioButtonCollection scaleOrigin = null!;
|
|
|
|
|
2024-05-28 16:27:01 +00:00
|
|
|
private RadioButton playfieldCentreButton = null!;
|
2024-05-25 16:31:19 +00:00
|
|
|
private RadioButton selectionCentreButton = null!;
|
|
|
|
|
2024-05-25 19:44:08 +00:00
|
|
|
private OsuCheckbox xCheckBox = null!;
|
|
|
|
private OsuCheckbox yCheckBox = null!;
|
|
|
|
|
2024-07-03 10:40:22 +00:00
|
|
|
public PreciseScalePopover(OsuSelectionScaleHandler scaleHandler, OsuGridToolboxGroup gridToolbox)
|
2024-05-25 16:31:19 +00:00
|
|
|
{
|
|
|
|
this.scaleHandler = scaleHandler;
|
2024-07-03 10:40:22 +00:00
|
|
|
this.gridToolbox = gridToolbox;
|
2024-05-25 16:31:19 +00:00
|
|
|
|
|
|
|
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-25 18:17:27 +00:00
|
|
|
Current = scaleInputBindable = new BindableNumber<float>
|
2024-05-25 16:31:19 +00:00
|
|
|
{
|
|
|
|
MinValue = 0.5f,
|
|
|
|
MaxValue = 2,
|
|
|
|
Precision = 0.001f,
|
|
|
|
Value = 1,
|
|
|
|
Default = 1,
|
|
|
|
},
|
|
|
|
Instantaneous = true
|
|
|
|
},
|
|
|
|
scaleOrigin = new EditorRadioButtonCollection
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Items = new[]
|
|
|
|
{
|
2024-05-28 16:27:01 +00:00
|
|
|
playfieldCentreButton = new RadioButton("Playfield centre",
|
2024-05-25 18:17:27 +00:00
|
|
|
() => setOrigin(ScaleOrigin.PlayfieldCentre),
|
2024-05-25 16:31:19 +00:00
|
|
|
() => new SpriteIcon { Icon = FontAwesome.Regular.Square }),
|
|
|
|
selectionCentreButton = new RadioButton("Selection centre",
|
2024-05-25 18:17:27 +00:00
|
|
|
() => setOrigin(ScaleOrigin.SelectionCentre),
|
2024-05-25 16:31:19 +00:00
|
|
|
() => new SpriteIcon { Icon = FontAwesome.Solid.VectorSquare })
|
|
|
|
}
|
2024-05-25 19:44:08 +00: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-25 16:31:19 +00:00
|
|
|
}
|
|
|
|
};
|
2024-05-28 16:27:01 +00:00
|
|
|
playfieldCentreButton.Selected.DisabledChanged += isDisabled =>
|
|
|
|
{
|
2024-05-29 07:59:19 +00:00
|
|
|
playfieldCentreButton.TooltipText = isDisabled ? "The current selection cannot be scaled relative to playfield centre." : string.Empty;
|
2024-05-28 16:27:01 +00:00
|
|
|
};
|
2024-05-25 16:31:19 +00:00
|
|
|
selectionCentreButton.Selected.DisabledChanged += isDisabled =>
|
|
|
|
{
|
2024-05-29 07:59:19 +00:00
|
|
|
selectionCentreButton.TooltipText = isDisabled ? "The current selection cannot be scaled relative to its centre." : string.Empty;
|
2024-05-25 16:31:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2024-05-25 16:41:31 +00:00
|
|
|
ScheduleAfterChildren(() =>
|
|
|
|
{
|
|
|
|
scaleInput.TakeFocus();
|
|
|
|
scaleInput.SelectAll();
|
|
|
|
});
|
2024-05-25 16:31:19 +00:00
|
|
|
scaleInput.Current.BindValueChanged(scale => scaleInfo.Value = scaleInfo.Value with { Scale = scale.NewValue });
|
|
|
|
|
2024-05-25 19:44:08 +00:00
|
|
|
xCheckBox.Current.BindValueChanged(x => setAxis(x.NewValue, yCheckBox.Current.Value));
|
|
|
|
yCheckBox.Current.BindValueChanged(y => setAxis(xCheckBox.Current.Value, y.NewValue));
|
|
|
|
|
2024-05-28 16:27:01 +00:00
|
|
|
selectionCentreButton.Selected.Disabled = !(scaleHandler.CanScaleX.Value || scaleHandler.CanScaleY.Value);
|
|
|
|
playfieldCentreButton.Selected.Disabled = scaleHandler.IsScalingSlider.Value && !selectionCentreButton.Selected.Disabled;
|
2024-05-28 14:19:57 +00:00
|
|
|
|
2024-05-29 08:01:04 +00:00
|
|
|
scaleOrigin.Items.First(b => !b.Selected.Disabled).Select();
|
2024-05-28 14:57:24 +00:00
|
|
|
|
2024-05-25 16:31:19 +00:00
|
|
|
scaleInfo.BindValueChanged(scale =>
|
|
|
|
{
|
2024-07-03 14:23:19 +00:00
|
|
|
var newScale = new Vector2(scale.NewValue.Scale, scale.NewValue.Scale);
|
|
|
|
scaleHandler.Update(newScale, getOriginPosition(scale.NewValue), getAdjustAxis(scale.NewValue), gridToolbox.GridLinesRotation.Value);
|
2024-05-25 16:31:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-28 15:24:31 +00:00
|
|
|
private void updateAxisCheckBoxesEnabled()
|
|
|
|
{
|
|
|
|
if (scaleInfo.Value.Origin == ScaleOrigin.PlayfieldCentre)
|
|
|
|
{
|
2024-05-29 08:04:49 +00:00
|
|
|
toggleAxisAvailable(xCheckBox.Current, true);
|
|
|
|
toggleAxisAvailable(yCheckBox.Current, true);
|
2024-05-28 15:24:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-05-29 08:04:49 +00:00
|
|
|
toggleAxisAvailable(xCheckBox.Current, scaleHandler.CanScaleX.Value);
|
|
|
|
toggleAxisAvailable(yCheckBox.Current, scaleHandler.CanScaleY.Value);
|
2024-05-28 15:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 08:04:49 +00:00
|
|
|
private void toggleAxisAvailable(Bindable<bool> axisBindable, bool available)
|
2024-05-28 15:24:31 +00:00
|
|
|
{
|
2024-05-29 08:04:49 +00:00
|
|
|
// enable the bindable to allow setting the value
|
|
|
|
axisBindable.Disabled = false;
|
|
|
|
// restore the presumed default value given the axis's new availability state
|
|
|
|
axisBindable.Value = available;
|
|
|
|
axisBindable.Disabled = !available;
|
2024-05-28 15:24:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-25 18:17:27 +00:00
|
|
|
private void updateMaxScale()
|
|
|
|
{
|
|
|
|
if (!scaleHandler.OriginalSurroundingQuad.HasValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const float max_scale = 10;
|
2024-07-03 14:23:19 +00:00
|
|
|
var scale = scaleHandler.ClampScaleToPlayfieldBounds(new Vector2(max_scale), getOriginPosition(scaleInfo.Value), getAdjustAxis(scaleInfo.Value), gridToolbox.GridLinesRotation.Value);
|
2024-05-25 18:17:27 +00:00
|
|
|
|
|
|
|
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();
|
2024-05-28 15:24:31 +00:00
|
|
|
updateAxisCheckBoxesEnabled();
|
2024-05-25 18:17:27 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 10:40:22 +00:00
|
|
|
private Vector2? getOriginPosition(PreciseScaleInfo scale) => scale.Origin == ScaleOrigin.PlayfieldCentre ? gridToolbox.StartPosition.Value : null;
|
2024-05-25 18:17:27 +00:00
|
|
|
|
2024-07-03 14:23:19 +00:00
|
|
|
private Axes getAdjustAxis(PreciseScaleInfo scale) => scale.XAxis ? scale.YAxis ? Axes.Both : Axes.X : Axes.Y;
|
|
|
|
|
2024-05-25 19:44:08 +00:00
|
|
|
private void setAxis(bool x, bool y)
|
|
|
|
{
|
|
|
|
scaleInfo.Value = scaleInfo.Value with { XAxis = x, YAxis = y };
|
|
|
|
updateMaxScale();
|
|
|
|
}
|
|
|
|
|
2024-05-25 16:31:19 +00:00
|
|
|
protected override void PopIn()
|
|
|
|
{
|
|
|
|
base.PopIn();
|
|
|
|
scaleHandler.Begin();
|
2024-05-25 18:17:27 +00:00
|
|
|
updateMaxScale();
|
2024-05-25 16:31:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void PopOut()
|
|
|
|
{
|
|
|
|
base.PopOut();
|
|
|
|
|
2024-05-28 16:27:01 +00:00
|
|
|
if (IsLoaded) scaleHandler.Commit();
|
2024-05-25 16:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum ScaleOrigin
|
|
|
|
{
|
|
|
|
PlayfieldCentre,
|
|
|
|
SelectionCentre
|
|
|
|
}
|
|
|
|
|
|
|
|
public record PreciseScaleInfo(float Scale, ScaleOrigin Origin, bool XAxis, bool YAxis);
|
|
|
|
}
|