1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Simplify toolbox button enable logic

This commit is contained in:
Bartłomiej Dach 2024-05-29 09:49:16 +02:00
parent 4a8273b6ed
commit bd5060965f
No known key found for this signature in database

View File

@ -18,8 +18,8 @@ namespace osu.Game.Rulesets.Osu.Edit
{ {
public partial class TransformToolboxGroup : EditorToolboxGroup, IKeyBindingHandler<GlobalAction> public partial class TransformToolboxGroup : EditorToolboxGroup, IKeyBindingHandler<GlobalAction>
{ {
private readonly Bindable<bool> canRotate = new BindableBool(); private readonly AggregateBindable<bool> canRotate = new AggregateBindable<bool>((x, y) => x || y);
private readonly Bindable<bool> canScale = new BindableBool(); private readonly AggregateBindable<bool> canScale = new AggregateBindable<bool>((x, y) => x || y);
private EditorToolButton rotateButton = null!; private EditorToolButton rotateButton = null!;
private EditorToolButton scaleButton = null!; private EditorToolButton scaleButton = null!;
@ -63,37 +63,17 @@ namespace osu.Game.Rulesets.Osu.Edit
{ {
base.LoadComplete(); base.LoadComplete();
// aggregate two values into canRotate canRotate.AddSource(RotationHandler.CanRotateAroundPlayfieldOrigin);
canRotatePlayfieldOrigin = RotationHandler.CanRotateAroundPlayfieldOrigin.GetBoundCopy(); canRotate.AddSource(RotationHandler.CanRotateAroundSelectionOrigin);
canRotatePlayfieldOrigin.BindValueChanged(_ => updateCanRotateAggregate());
canRotateSelectionOrigin = RotationHandler.CanRotateAroundSelectionOrigin.GetBoundCopy(); canScale.AddSource(ScaleHandler.CanScaleX);
canRotateSelectionOrigin.BindValueChanged(_ => updateCanRotateAggregate()); canScale.AddSource(ScaleHandler.CanScaleY);
canScale.AddSource(ScaleHandler.CanScaleFromPlayfieldOrigin);
void updateCanRotateAggregate()
{
canRotate.Value = RotationHandler.CanRotateAroundPlayfieldOrigin.Value || RotationHandler.CanRotateAroundSelectionOrigin.Value;
}
// aggregate three values into canScale
canScaleX = ScaleHandler.CanScaleX.GetBoundCopy();
canScaleX.BindValueChanged(_ => updateCanScaleAggregate());
canScaleY = ScaleHandler.CanScaleY.GetBoundCopy();
canScaleY.BindValueChanged(_ => updateCanScaleAggregate());
canScalePlayfieldOrigin = ScaleHandler.CanScaleFromPlayfieldOrigin.GetBoundCopy();
canScalePlayfieldOrigin.BindValueChanged(_ => updateCanScaleAggregate());
void updateCanScaleAggregate()
{
canScale.Value = ScaleHandler.CanScaleX.Value || ScaleHandler.CanScaleY.Value || ScaleHandler.CanScaleFromPlayfieldOrigin.Value;
}
// bindings to `Enabled` on the buttons are decoupled on purpose // bindings to `Enabled` on the buttons are decoupled on purpose
// due to the weird `OsuButton` behaviour of resetting `Enabled` to `false` when `Action` is set. // due to the weird `OsuButton` behaviour of resetting `Enabled` to `false` when `Action` is set.
canRotate.BindValueChanged(_ => rotateButton.Enabled.Value = canRotate.Value, true); canRotate.Result.BindValueChanged(rotate => rotateButton.Enabled.Value = rotate.NewValue, true);
canScale.BindValueChanged(_ => scaleButton.Enabled.Value = canScale.Value, true); canScale.Result.BindValueChanged(scale => scaleButton.Enabled.Value = scale.NewValue, true);
} }
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e) public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)