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

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

208 lines
7.1 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.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 Container outlineBox = null!;
2023-01-26 16:46:19 +08:00
private AnchorOriginVisualiser anchorOriginVisualiser = null!;
private Drawable drawable => (Drawable)Item;
protected override bool ShouldBeAlive => drawable.IsAlive && Item.IsPresent;
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
{
2023-02-17 16:51:52 +08:00
Padding = new MarginPadding(-SkinSelectionHandler.INFLATE_SIZE),
Children = new Drawable[]
{
outlineBox = 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,
},
}
},
new OsuSpriteText
{
Text = Item.GetType().Name,
Font = OsuFont.Default.With(size: 10, weight: FontWeight.Bold),
Anchor = Anchor.BottomRight,
Origin = Anchor.TopRight,
},
},
},
anchorOriginVisualiser = new AnchorOriginVisualiser(drawable)
{
Alpha = 0,
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
updateSelectedState();
this.FadeInFromZero(200, Easing.OutQuint);
}
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);
}
private Quad drawableQuad;
public override Quad ScreenSpaceDrawQuad => drawableQuad;
protected override void Update()
{
base.Update();
drawableQuad = drawable.ScreenSpaceDrawQuad;
var quad = ToLocalSpace(drawable.ScreenSpaceDrawQuad);
box.Position = drawable.ToSpaceOfOtherDrawable(Vector2.Zero, this);
box.Size = quad.Size;
box.Rotation = drawable.Rotation;
box.Scale = new Vector2(MathF.Sign(drawable.Scale.X), MathF.Sign(drawable.Scale.Y));
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => drawable.ReceivePositionalInputAt(screenSpacePos);
public override Vector2 ScreenSpaceSelectionPoint => drawable.ToScreenSpace(drawable.OriginPosition);
public override Quad SelectionQuad => drawable.ScreenSpaceDrawQuad;
}
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;
protected override void Update()
{
base.Update();
if (drawable.Parent == null)
return;
2023-02-17 16:51:52 +08:00
var newAnchor = drawable.Parent.ToSpaceOfOtherDrawable(drawable.AnchorPosition, this);
anchorPosition ??= newAnchor;
anchorPosition =
new Vector2(
(float)Interpolation.DampContinuously(anchorPosition.Value.X, newAnchor.X, 25, Clock.ElapsedFrameTime),
(float)Interpolation.DampContinuously(anchorPosition.Value.Y, newAnchor.Y, 25, Clock.ElapsedFrameTime)
);
originBox.Position = drawable.ToSpaceOfOtherDrawable(drawable.OriginPosition, this);
2023-02-17 16:51:52 +08:00
anchorBox.Position = anchorPosition.Value;
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));
}
}
}