2021-04-28 14:14:48 +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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2021-05-14 15:03:22 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-05-13 16:51:57 +08:00
|
|
|
using osu.Framework.Extensions.EnumExtensions;
|
2021-04-28 14:14:48 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2021-04-29 14:29:25 +08:00
|
|
|
using osu.Game.Extensions;
|
2021-04-28 14:14:48 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Rulesets.Edit;
|
|
|
|
using osu.Game.Screens.Edit.Compose.Components;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning.Editor
|
|
|
|
{
|
2021-05-13 16:06:00 +08:00
|
|
|
public class SkinSelectionHandler : SelectionHandler<ISkinnableDrawable>
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
2021-05-14 15:03:22 +08:00
|
|
|
[Resolved]
|
|
|
|
private SkinEditor skinEditor { get; set; }
|
|
|
|
|
2021-05-03 14:15:00 +08:00
|
|
|
public override bool HandleRotation(float angle)
|
|
|
|
{
|
|
|
|
// TODO: this doesn't correctly account for origin/anchor specs being different in a multi-selection.
|
|
|
|
foreach (var c in SelectedBlueprints)
|
|
|
|
((Drawable)c.Item).Rotation += angle;
|
|
|
|
|
|
|
|
return base.HandleRotation(angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool HandleScale(Vector2 scale, Anchor anchor)
|
|
|
|
{
|
|
|
|
adjustScaleFromAnchor(ref scale, anchor);
|
|
|
|
|
|
|
|
foreach (var c in SelectedBlueprints)
|
2021-05-03 14:24:51 +08:00
|
|
|
// TODO: this is temporary and will be fixed with a separate refactor of selection transform logic.
|
|
|
|
((Drawable)c.Item).Scale += scale * 0.02f;
|
2021-05-03 14:15:00 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-13 16:51:57 +08:00
|
|
|
public override bool HandleFlip(Direction direction)
|
|
|
|
{
|
|
|
|
// TODO: this is temporary as well.
|
|
|
|
foreach (var c in SelectedBlueprints)
|
|
|
|
{
|
|
|
|
((Drawable)c.Item).Scale *= new Vector2(
|
|
|
|
direction == Direction.Horizontal ? -1 : 1,
|
|
|
|
direction == Direction.Vertical ? -1 : 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-13 16:06:00 +08:00
|
|
|
public override bool HandleMovement(MoveSelectionEvent<ISkinnableDrawable> moveEvent)
|
2021-05-03 14:15:00 +08:00
|
|
|
{
|
|
|
|
foreach (var c in SelectedBlueprints)
|
|
|
|
{
|
|
|
|
Drawable drawable = (Drawable)c.Item;
|
|
|
|
drawable.Position += drawable.ScreenSpaceDeltaToParentSpace(moveEvent.ScreenSpaceDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnSelectionChanged()
|
|
|
|
{
|
|
|
|
base.OnSelectionChanged();
|
|
|
|
|
|
|
|
SelectionBox.CanRotate = true;
|
|
|
|
SelectionBox.CanScaleX = true;
|
|
|
|
SelectionBox.CanScaleY = true;
|
|
|
|
SelectionBox.CanReverse = false;
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:03:22 +08:00
|
|
|
protected override void DeleteItems(IEnumerable<ISkinnableDrawable> items) =>
|
|
|
|
skinEditor.DeleteItems(items.ToArray());
|
2021-04-28 14:14:48 +08:00
|
|
|
|
2021-05-13 16:06:00 +08:00
|
|
|
protected override IEnumerable<MenuItem> GetContextMenuItemsForSelection(IEnumerable<SelectionBlueprint<ISkinnableDrawable>> selection)
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
|
|
|
yield return new OsuMenuItem("Anchor")
|
|
|
|
{
|
2021-05-11 13:09:56 +08:00
|
|
|
Items = createAnchorItems(d => d.Anchor, applyAnchor).ToArray()
|
|
|
|
};
|
|
|
|
|
|
|
|
yield return new OsuMenuItem("Origin")
|
|
|
|
{
|
|
|
|
Items = createAnchorItems(d => d.Origin, applyOrigin).ToArray()
|
2021-04-28 14:14:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var item in base.GetContextMenuItemsForSelection(selection))
|
|
|
|
yield return item;
|
|
|
|
|
2021-05-11 13:09:56 +08:00
|
|
|
IEnumerable<AnchorMenuItem> createAnchorItems(Func<Drawable, Anchor> checkFunction, Action<Anchor> applyFunction)
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
|
|
|
var displayableAnchors = new[]
|
|
|
|
{
|
|
|
|
Anchor.TopLeft,
|
|
|
|
Anchor.TopCentre,
|
|
|
|
Anchor.TopRight,
|
|
|
|
Anchor.CentreLeft,
|
|
|
|
Anchor.Centre,
|
|
|
|
Anchor.CentreRight,
|
|
|
|
Anchor.BottomLeft,
|
|
|
|
Anchor.BottomCentre,
|
|
|
|
Anchor.BottomRight,
|
|
|
|
};
|
|
|
|
|
|
|
|
return displayableAnchors.Select(a =>
|
|
|
|
{
|
2021-05-11 13:09:56 +08:00
|
|
|
return new AnchorMenuItem(a, selection, _ => applyFunction(a))
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
2021-05-11 13:09:56 +08:00
|
|
|
State = { Value = GetStateFromSelection(selection, c => checkFunction((Drawable)c.Item) == a) }
|
2021-04-28 14:14:48 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 13:09:56 +08:00
|
|
|
private void applyOrigin(Anchor anchor)
|
|
|
|
{
|
|
|
|
foreach (var item in SelectedItems)
|
2021-05-12 14:30:52 +08:00
|
|
|
{
|
|
|
|
var drawable = (Drawable)item;
|
|
|
|
|
|
|
|
var previousOrigin = drawable.OriginPosition;
|
|
|
|
drawable.Origin = anchor;
|
|
|
|
drawable.Position += drawable.OriginPosition - previousOrigin;
|
|
|
|
}
|
2021-05-11 13:09:56 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:14:48 +08:00
|
|
|
private void applyAnchor(Anchor anchor)
|
|
|
|
{
|
|
|
|
foreach (var item in SelectedItems)
|
2021-05-12 14:30:52 +08:00
|
|
|
{
|
|
|
|
var drawable = (Drawable)item;
|
|
|
|
|
2021-05-13 21:11:58 +08:00
|
|
|
var previousAnchor = drawable.AnchorPosition;
|
2021-05-12 14:30:52 +08:00
|
|
|
drawable.Anchor = anchor;
|
|
|
|
drawable.Position -= drawable.AnchorPosition - previousAnchor;
|
|
|
|
}
|
2021-04-28 14:14:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void adjustScaleFromAnchor(ref Vector2 scale, Anchor reference)
|
|
|
|
{
|
|
|
|
// cancel out scale in axes we don't care about (based on which drag handle was used).
|
|
|
|
if ((reference & Anchor.x1) > 0) scale.X = 0;
|
|
|
|
if ((reference & Anchor.y1) > 0) scale.Y = 0;
|
|
|
|
|
|
|
|
// reverse the scale direction if dragging from top or left.
|
|
|
|
if ((reference & Anchor.x0) > 0) scale.X = -scale.X;
|
|
|
|
if ((reference & Anchor.y0) > 0) scale.Y = -scale.Y;
|
2021-05-13 16:51:57 +08:00
|
|
|
|
|
|
|
// for now aspect lock scale adjustments that occur at corners.
|
|
|
|
if (!reference.HasFlagFast(Anchor.x1) && !reference.HasFlagFast(Anchor.y1))
|
|
|
|
{
|
2021-05-13 23:50:12 +08:00
|
|
|
// TODO: temporary implementation - only dragging the corner handles across the X axis changes size.
|
|
|
|
scale.Y = scale.X;
|
2021-05-13 16:51:57 +08:00
|
|
|
}
|
2021-04-28 14:14:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public class AnchorMenuItem : TernaryStateMenuItem
|
|
|
|
{
|
2021-05-13 16:06:00 +08:00
|
|
|
public AnchorMenuItem(Anchor anchor, IEnumerable<SelectionBlueprint<ISkinnableDrawable>> selection, Action<TernaryState> action)
|
2021-04-28 14:14:48 +08:00
|
|
|
: base(anchor.ToString(), getNextState, MenuItemType.Standard, action)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private static TernaryState getNextState(TernaryState state) => TernaryState.True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|