1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 13:07:24 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Edit/TransformToolboxGroup.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

126 lines
4.7 KiB
C#
Raw Normal View History

// 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.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
using osu.Game.Screens.Edit.Components;
using osu.Game.Screens.Edit.Compose.Components;
using osuTK;
namespace osu.Game.Rulesets.Osu.Edit
{
public partial class TransformToolboxGroup : EditorToolboxGroup, IKeyBindingHandler<GlobalAction>
{
private readonly Bindable<bool> canRotate = new BindableBool();
2024-05-26 00:31:19 +08:00
private readonly Bindable<bool> canScale = new BindableBool();
private EditorToolButton rotateButton = null!;
2024-05-26 00:31:19 +08:00
private EditorToolButton scaleButton = null!;
private Bindable<bool> canRotatePlayfieldOrigin = null!;
private Bindable<bool> canRotateSelectionOrigin = null!;
2024-05-26 00:31:19 +08:00
private Bindable<bool> canScaleX = null!;
private Bindable<bool> canScaleY = null!;
private Bindable<bool> canScaleDiagonally = null!;
public SelectionRotationHandler RotationHandler { get; init; } = null!;
2024-05-26 00:31:19 +08:00
public SelectionScaleHandler ScaleHandler { get; init; } = null!;
public TransformToolboxGroup()
: base("transform")
{
}
[BackgroundDependencyLoader]
private void load()
{
Child = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(5),
Children = new Drawable[]
{
rotateButton = new EditorToolButton("Rotate",
() => new SpriteIcon { Icon = FontAwesome.Solid.Undo },
() => new PreciseRotationPopover(RotationHandler)),
2024-05-26 00:31:19 +08:00
scaleButton = new EditorToolButton("Scale",
() => new SpriteIcon { Icon = FontAwesome.Solid.ArrowsAlt },
() => new PreciseScalePopover(ScaleHandler))
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
// aggregate two values into canRotate
canRotatePlayfieldOrigin = RotationHandler.CanRotatePlayfieldOrigin.GetBoundCopy();
canRotatePlayfieldOrigin.BindValueChanged(_ => updateCanRotateAggregate());
canRotateSelectionOrigin = RotationHandler.CanRotateSelectionOrigin.GetBoundCopy();
canRotateSelectionOrigin.BindValueChanged(_ => updateCanRotateAggregate());
void updateCanRotateAggregate()
{
canRotate.Value = RotationHandler.CanRotatePlayfieldOrigin.Value || RotationHandler.CanRotateSelectionOrigin.Value;
}
2024-05-26 00:31:19 +08:00
// aggregate three values into canScale
canScaleX = ScaleHandler.CanScaleX.GetBoundCopy();
canScaleX.BindValueChanged(_ => updateCanScaleAggregate());
canScaleY = ScaleHandler.CanScaleY.GetBoundCopy();
canScaleY.BindValueChanged(_ => updateCanScaleAggregate());
canScaleDiagonally = ScaleHandler.CanScaleDiagonally.GetBoundCopy();
canScaleDiagonally.BindValueChanged(_ => updateCanScaleAggregate());
void updateCanScaleAggregate()
{
canScale.Value = ScaleHandler.CanScaleX.Value || ScaleHandler.CanScaleY.Value || ScaleHandler.CanScaleDiagonally.Value;
}
// 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.
canRotate.BindValueChanged(_ => rotateButton.Enabled.Value = canRotate.Value, true);
2024-05-26 00:31:19 +08:00
canScale.BindValueChanged(_ => scaleButton.Enabled.Value = canScale.Value, true);
}
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat) return false;
switch (e.Action)
{
case GlobalAction.EditorToggleRotateControl:
{
rotateButton.TriggerClick();
return true;
}
2024-05-26 00:31:19 +08:00
case GlobalAction.EditorToggleScaleControl:
{
scaleButton.TriggerClick();
return true;
}
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
}
}