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;
|
|
|
|
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 osu.Game.Screens.Play.HUD;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning.Editor
|
|
|
|
{
|
2021-04-28 17:34:34 +08:00
|
|
|
public class SkinSelectionHandler : SelectionHandler<ISkinnableComponent>
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
2021-04-28 17:34:34 +08:00
|
|
|
protected override void DeleteItems(IEnumerable<ISkinnableComponent> items)
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
|
|
|
foreach (var i in items)
|
2021-04-28 17:34:34 +08:00
|
|
|
i.Hide();
|
2021-04-28 14:14:48 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 17:34:34 +08:00
|
|
|
protected override IEnumerable<MenuItem> GetContextMenuItemsForSelection(IEnumerable<SelectionBlueprint<ISkinnableComponent>> selection)
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
|
|
|
yield return new OsuMenuItem("Anchor")
|
|
|
|
{
|
|
|
|
Items = createAnchorItems().ToArray()
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var item in base.GetContextMenuItemsForSelection(selection))
|
|
|
|
yield return item;
|
|
|
|
|
|
|
|
IEnumerable<AnchorMenuItem> createAnchorItems()
|
|
|
|
{
|
|
|
|
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-04-28 17:34:34 +08:00
|
|
|
var countExisting = selection.Count(b => ((Drawable)b.Item).Anchor == a);
|
2021-04-28 14:14:48 +08:00
|
|
|
var countTotal = selection.Count();
|
|
|
|
|
|
|
|
TernaryState state;
|
|
|
|
|
|
|
|
if (countExisting == countTotal)
|
|
|
|
state = TernaryState.True;
|
|
|
|
else if (countExisting > 0)
|
|
|
|
state = TernaryState.Indeterminate;
|
|
|
|
else
|
|
|
|
state = TernaryState.False;
|
|
|
|
|
|
|
|
return new AnchorMenuItem(a, selection, _ => applyAnchor(a))
|
|
|
|
{
|
|
|
|
State = { Value = state }
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void applyAnchor(Anchor anchor)
|
|
|
|
{
|
|
|
|
foreach (var item in SelectedItems)
|
2021-04-28 17:34:34 +08:00
|
|
|
((Drawable)item).Anchor = anchor;
|
2021-04-28 14:14:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnSelectionChanged()
|
|
|
|
{
|
|
|
|
base.OnSelectionChanged();
|
|
|
|
|
|
|
|
SelectionBox.CanRotate = true;
|
|
|
|
SelectionBox.CanScaleX = true;
|
|
|
|
SelectionBox.CanScaleY = true;
|
|
|
|
SelectionBox.CanReverse = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool HandleRotation(float angle)
|
|
|
|
{
|
2021-04-29 15:16:52 +08:00
|
|
|
// TODO: this doesn't correctly account for origin/anchor specs being different in a multi-selection.
|
2021-04-28 14:14:48 +08:00
|
|
|
foreach (var c in SelectedBlueprints)
|
2021-04-28 17:34:34 +08:00
|
|
|
((Drawable)c.Item).Rotation += angle;
|
2021-04-28 14:14:48 +08:00
|
|
|
|
|
|
|
return base.HandleRotation(angle);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool HandleScale(Vector2 scale, Anchor anchor)
|
|
|
|
{
|
|
|
|
adjustScaleFromAnchor(ref scale, anchor);
|
|
|
|
|
|
|
|
foreach (var c in SelectedBlueprints)
|
2021-04-28 17:34:34 +08:00
|
|
|
((Drawable)c.Item).Scale += scale * 0.01f;
|
2021-04-28 14:14:48 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-28 17:34:34 +08:00
|
|
|
public override bool HandleMovement(MoveSelectionEvent<ISkinnableComponent> moveEvent)
|
2021-04-28 14:14:48 +08:00
|
|
|
{
|
|
|
|
foreach (var c in SelectedBlueprints)
|
2021-04-28 14:28:32 +08:00
|
|
|
{
|
2021-04-29 14:29:25 +08:00
|
|
|
Drawable drawable = (Drawable)c.Item;
|
|
|
|
drawable.Position += drawable.ScreenSpaceDeltaToParentSpace(moveEvent.ScreenSpaceDelta);
|
2021-04-28 14:28:32 +08:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:14:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public class AnchorMenuItem : TernaryStateMenuItem
|
|
|
|
{
|
2021-04-28 17:34:34 +08:00
|
|
|
public AnchorMenuItem(Anchor anchor, IEnumerable<SelectionBlueprint<ISkinnableComponent>> selection, Action<TernaryState> action)
|
2021-04-28 14:14:48 +08:00
|
|
|
: base(anchor.ToString(), getNextState, MenuItemType.Standard, action)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateState(TernaryState obj)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static TernaryState getNextState(TernaryState state) => TernaryState.True;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|