1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 17:37:24 +08:00
osu-lazer/osu.Game/Overlays/SkinEditor/SkinBlueprint.cs

231 lines
8.3 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 System;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
2023-02-17 16:51:52 +08:00
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shapes;
2023-02-17 16:51:52 +08:00
using osu.Framework.Utils;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets.Edit;
2023-02-17 16:51:52 +08:00
using osu.Game.Screens.Edit.Compose.Components;
2023-01-26 17:21:04 +08:00
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
2023-01-26 17:21:04 +08:00
namespace osu.Game.Overlays.SkinEditor
{
public partial class SkinBlueprint : SelectionBlueprint<ISerialisableDrawable>
{
2023-01-26 16:46:19 +08:00
private Container box = null!;
2023-01-26 16:46:19 +08:00
private AnchorOriginVisualiser anchorOriginVisualiser = null!;
private OsuSpriteText label = null!;
private Drawable drawable => (Drawable)Item;
protected override bool ShouldBeAlive => drawable.IsAlive && Item.IsPresent;
private Quad drawableQuad;
public override Quad ScreenSpaceDrawQuad => drawableQuad;
public override Quad SelectionQuad => drawable.ScreenSpaceDrawQuad;
public override bool Contains(Vector2 screenSpacePos) => drawableQuad.Contains(screenSpacePos);
public override Vector2 ScreenSpaceSelectionPoint =>
// Important to use a stable position (not based on origin) as origin may be automatically updated during drag operations.
drawable.ScreenSpaceDrawQuad.Centre;
protected override bool ReceivePositionalInputAtSubTree(Vector2 screenSpacePos) =>
drawableQuad.Contains(screenSpacePos);
public SkinBlueprint(ISerialisableDrawable component)
: base(component)
{
}
[BackgroundDependencyLoader]
2023-02-17 16:51:52 +08:00
private void load(OsuColour colours)
{
InternalChildren = new Drawable[]
{
box = new Container
{
Children = new Drawable[]
{
2023-02-18 04:51:19 +08:00
new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
2023-02-17 16:51:52 +08:00
CornerRadius = 3,
BorderThickness = SelectionBox.BORDER_RADIUS / 2,
BorderColour = ColourInfo.GradientVertical(colours.Pink4.Darken(0.4f), colours.Pink4),
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
2023-02-17 16:51:52 +08:00
Blending = BlendingParameters.Additive,
Alpha = 0.2f,
Colour = ColourInfo.GradientVertical(colours.Pink2, colours.Pink4),
AlwaysPresent = true,
},
}
},
label = new OsuSpriteText
{
Text = Item.GetType().Name,
Font = OsuFont.Default.With(size: 10, weight: FontWeight.Bold),
Alpha = 0,
Anchor = Anchor.BottomRight,
Origin = Anchor.TopRight,
},
},
},
anchorOriginVisualiser = new AnchorOriginVisualiser(drawable)
{
Alpha = 0,
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
updateSelectedState();
}
protected override bool OnHover(HoverEvent e)
{
updateSelectedState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
updateSelectedState();
base.OnHoverLost(e);
}
protected override void OnSelected()
{
// base logic hides selected blueprints when not selected, but skin blueprints don't do that.
updateSelectedState();
}
protected override void OnDeselected()
{
// base logic hides selected blueprints when not selected, but skin blueprints don't do that.
updateSelectedState();
}
private void updateSelectedState()
{
anchorOriginVisualiser.FadeTo(IsSelected ? 1 : 0, 200, Easing.OutQuint);
label.FadeTo(IsSelected || IsHovered ? 1 : 0, 200, Easing.OutQuint);
}
protected override void Update()
{
base.Update();
2023-12-21 20:26:39 +08:00
Vector2 scale = drawable.DrawInfo.MatrixInverse.ExtractScale().Xy;
drawableQuad = drawable.ToScreenSpace(
drawable.DrawRectangle
2023-12-21 20:26:39 +08:00
.Inflate(SkinSelectionHandler.INFLATE_SIZE * scale));
var localSpaceQuad = ToLocalSpace(drawableQuad);
box.Position = localSpaceQuad.TopLeft;
box.Size = localSpaceQuad.Size;
box.Rotation = drawable.Rotation;
box.Scale = new Vector2(MathF.Sign(drawable.Scale.X), MathF.Sign(drawable.Scale.Y));
}
}
2022-11-24 13:32:20 +08:00
internal partial class AnchorOriginVisualiser : CompositeDrawable
{
private readonly Drawable drawable;
2023-02-17 16:51:52 +08:00
private Drawable originBox = null!;
2023-02-17 16:51:52 +08:00
private Drawable anchorBox = null!;
private Drawable anchorLine = null!;
public AnchorOriginVisualiser(Drawable drawable)
{
this.drawable = drawable;
2023-02-17 16:51:52 +08:00
}
2023-02-17 16:51:52 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Color4 anchorColour = colours.Red1;
Color4 originColour = colours.Red3;
InternalChildren = new[]
{
2023-02-17 16:51:52 +08:00
anchorLine = new Circle
{
2023-02-17 16:51:52 +08:00
Height = 3f,
2022-04-21 15:16:52 +08:00
Origin = Anchor.CentreLeft,
2023-02-17 16:51:52 +08:00
Colour = ColourInfo.GradientHorizontal(originColour.Opacity(0.5f), originColour),
},
2023-02-17 16:51:52 +08:00
originBox = new Circle
{
2023-02-17 16:51:52 +08:00
Colour = originColour,
Origin = Anchor.Centre,
2023-02-17 16:51:52 +08:00
Size = new Vector2(7),
},
2023-02-17 16:51:52 +08:00
anchorBox = new Circle
{
2023-02-17 16:51:52 +08:00
Colour = anchorColour,
Origin = Anchor.Centre,
2023-02-17 16:51:52 +08:00
Size = new Vector2(10),
},
};
}
2023-02-17 16:51:52 +08:00
private Vector2? anchorPosition;
2023-02-19 22:52:21 +08:00
private Vector2? originPositionInDrawableSpace;
2023-02-17 16:51:52 +08:00
protected override void Update()
{
base.Update();
if (drawable.Parent == null)
return;
var newAnchor = drawable.Parent!.ToSpaceOfOtherDrawable(drawable.AnchorPosition, this);
2023-02-19 22:52:21 +08:00
anchorPosition = tweenPosition(anchorPosition ?? newAnchor, newAnchor);
2023-02-17 16:51:52 +08:00
anchorBox.Position = anchorPosition.Value;
2023-02-19 22:52:21 +08:00
// for the origin, tween in the drawable's local space to avoid unwanted tweening when the drawable is being dragged.
originPositionInDrawableSpace = originPositionInDrawableSpace != null ? tweenPosition(originPositionInDrawableSpace.Value, drawable.OriginPosition) : drawable.OriginPosition;
originBox.Position = drawable.ToSpaceOfOtherDrawable(originPositionInDrawableSpace.Value, this);
var point1 = ToLocalSpace(anchorBox.ScreenSpaceDrawQuad.Centre);
var point2 = ToLocalSpace(originBox.ScreenSpaceDrawQuad.Centre);
anchorLine.Position = point1;
anchorLine.Width = (point2 - point1).Length;
anchorLine.Rotation = MathHelper.RadiansToDegrees(MathF.Atan2(point2.Y - point1.Y, point2.X - point1.X));
}
2023-02-19 22:52:21 +08:00
private Vector2 tweenPosition(Vector2 oldPosition, Vector2 newPosition)
=> new Vector2(
(float)Interpolation.DampContinuously(oldPosition.X, newPosition.X, 25, Clock.ElapsedFrameTime),
(float)Interpolation.DampContinuously(oldPosition.Y, newPosition.Y, 25, Clock.ElapsedFrameTime)
);
}
}