[Feat] RadialContainer (#177)
* Added radial button 🥳 * Added animations 🔔 * Added network radial classes (dont forget remove InternalsSystem.cs, i mean my test hook, And StorageBoundUserInterface.cs * Functionality meow 🏛️ * Фиксы * More tests * Support now more 8 buttons * Update StorageBoundUserInterface.cs
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
<Control
|
||||
xmlns="https://spacestation14.io"
|
||||
xmlns:controls="clr-namespace:Content.Client.White.UserInterface.Controls">
|
||||
|
||||
<BoxContainer>
|
||||
<TextureButton
|
||||
Name="Controller"
|
||||
Access="Public"
|
||||
VerticalExpand="True"
|
||||
HorizontalExpand="True">
|
||||
<TextureRect Access="Public" Name="BackgroundTexture"
|
||||
VerticalExpand="True"
|
||||
HorizontalExpand="True"
|
||||
VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="KeepAspect">
|
||||
</TextureRect>
|
||||
</TextureButton>
|
||||
</BoxContainer>
|
||||
</Control>
|
||||
@@ -0,0 +1,39 @@
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared.Animations;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.White.UserInterface.Controls;
|
||||
|
||||
[GenerateTypedNameReferences, Virtual, PublicAPI]
|
||||
public sealed partial class RadialButton : Control
|
||||
{
|
||||
[Animatable] public Vector2 Offset { get; set; }
|
||||
public string? Content { get; set; }
|
||||
|
||||
public string Texture
|
||||
{
|
||||
set => Controller.TexturePath = value;
|
||||
}
|
||||
|
||||
[Animatable]
|
||||
public Vector2 ButtonSize
|
||||
{
|
||||
get => this.Size;
|
||||
set => this.SetSize = value;
|
||||
}
|
||||
|
||||
public RadialButton()
|
||||
{
|
||||
RobustXamlLoader.Load(this);
|
||||
Offset = Vector2.Zero;
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
base.FrameUpdate(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<controls:RadialContainer
|
||||
xmlns="https://spacestation14.io"
|
||||
xmlns:controls="clr-namespace:Content.Client.White.UserInterface.Controls"
|
||||
Visible="False"
|
||||
MaxSize="0 0">
|
||||
<BoxContainer>
|
||||
<controls:RadialButton
|
||||
Name="CloseButton"
|
||||
Access="Public"
|
||||
VerticalExpand="True"
|
||||
HorizontalExpand="True"
|
||||
Content="Close"
|
||||
Texture="/Textures/Interface/Default/blocked.png"/>
|
||||
<LayoutContainer Access="Public" Name="Layout" HorizontalExpand="True" VerticalExpand="True"></LayoutContainer>
|
||||
<BoxContainer Access="Public" Name="ActionBox" HorizontalExpand="True" VerticalExpand="True">
|
||||
<Label Access="Public" Name="ActionLabel" HorizontalExpand="True" Visible="False"/>
|
||||
</BoxContainer>
|
||||
</BoxContainer>
|
||||
</controls:RadialContainer>
|
||||
@@ -0,0 +1,257 @@
|
||||
using System.Linq;
|
||||
using Content.Client.Gameplay;
|
||||
using Content.Client.Resources;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.Animations;
|
||||
using Robust.Client.AutoGenerated;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Client.Utility;
|
||||
using Robust.Shared.Animations;
|
||||
using Robust.Shared.Console;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Client.White.UserInterface.Controls;
|
||||
|
||||
public sealed class RadialContainerCommandTest : LocalizedCommands
|
||||
{
|
||||
public override string Command => "radialtest";
|
||||
|
||||
public override void Execute(IConsoleShell shell, string argStr, string[] args)
|
||||
{
|
||||
var radial = new RadialContainer();
|
||||
for (int i = 0; i < 24; i++)
|
||||
{
|
||||
var testButton = radial.AddButton("Action " + i, "/Textures/Interface/emotions.svg.192dpi.png");
|
||||
testButton.Controller.OnPressed += (_) => { Logger.Debug("Press gay"); };
|
||||
}
|
||||
|
||||
radial.CloseButton.Controller.OnPressed += (_) =>
|
||||
{
|
||||
radial.Close();
|
||||
radial.Dispose();
|
||||
};
|
||||
//radial.OpenCentered();
|
||||
var usrMngr = IoCManager.Resolve<IUserInterfaceManager>();
|
||||
radial.Open(usrMngr.MousePositionScaled.Position);
|
||||
}
|
||||
}
|
||||
|
||||
[GenerateTypedNameReferences, Virtual]
|
||||
public partial class RadialContainer : Control
|
||||
{
|
||||
private bool _isOpened = false;
|
||||
|
||||
private Vector2 _focusSize = new Vector2(64, 64);
|
||||
private Vector2 _normalSize = new Vector2(50, 50);
|
||||
|
||||
private float _moveAniTime = 0.3f;
|
||||
private float _focusAniTime = 0.25f;
|
||||
|
||||
private int _maxButtons = 8;
|
||||
|
||||
private string _backgroundTexture = "/Textures/Interface/Default/SlotBackground.png";
|
||||
|
||||
public const string MoveAnimationKey = "move";
|
||||
public const string InSizeAnimationKey = "insize";
|
||||
public const string OutSizeAnimationKey = "outsize";
|
||||
|
||||
public float FocusSize
|
||||
{
|
||||
get => _focusSize.Y;
|
||||
set => _focusSize = new Vector2(value, value);
|
||||
}
|
||||
public float NormalSize
|
||||
{
|
||||
get => _normalSize.Y;
|
||||
set => _normalSize = new Vector2(value, value);
|
||||
}
|
||||
|
||||
public float MoveAnimationTime
|
||||
{
|
||||
get => _moveAniTime;
|
||||
set => _moveAniTime = value;
|
||||
}
|
||||
public float FocusAnimationTime
|
||||
{
|
||||
get => _focusAniTime;
|
||||
set => _focusAniTime = value;
|
||||
}
|
||||
|
||||
public bool IsAction = true;
|
||||
|
||||
public RadialContainer() : base()
|
||||
{
|
||||
RobustXamlLoader.Load(this);
|
||||
}
|
||||
|
||||
public void Open(Vector2 position)
|
||||
{
|
||||
AddToRoot();
|
||||
LayoutContainer.SetPosition(this, position);
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
public void OpenCentered()
|
||||
{
|
||||
AddToRoot();
|
||||
if (Parent != null)
|
||||
LayoutContainer.SetPosition(this, (Parent.Size/2) - (this.Size/2));
|
||||
else
|
||||
LayoutContainer.SetPosition(this, (UserInterfaceManager.MainViewport.Size/2) - (this.Size/2));
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
public void Close()
|
||||
{
|
||||
Parent?.RemoveChild(this);
|
||||
Visible = false;
|
||||
_isOpened = false;
|
||||
}
|
||||
|
||||
public RadialButton AddButton(string action, string? texture = null)
|
||||
{
|
||||
var button = new RadialButton();
|
||||
button.Content = action;
|
||||
button.Controller.TextureNormal = IoCManager.Resolve<IResourceCache>().GetTexture(_backgroundTexture);
|
||||
if (texture != null)
|
||||
button.BackgroundTexture.Texture = IoCManager.Resolve<IResourceCache>().GetTexture(texture);
|
||||
Layout.AddChild(button);
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
private void AddToRoot()
|
||||
{
|
||||
if (_isOpened)
|
||||
return;
|
||||
UserInterfaceManager.WindowRoot.AddChild(this);
|
||||
_isOpened = !_isOpened;
|
||||
}
|
||||
|
||||
private void UpdateButtons()
|
||||
{
|
||||
Visible = true;
|
||||
|
||||
var angleDegrees = 360/Layout.ChildCount;
|
||||
var stepAngle = -angleDegrees + -90;
|
||||
var distance = FocusSize * 1.2;
|
||||
if (Layout.Children.Count() > _maxButtons)
|
||||
{
|
||||
for (int i = 0; i < (Layout.Children.Count() - _maxButtons); i++)
|
||||
{
|
||||
distance += (NormalSize/3);
|
||||
}
|
||||
}
|
||||
foreach (var child in Layout.Children)
|
||||
{
|
||||
var button = (RadialButton)child;
|
||||
button.ButtonSize = _normalSize;
|
||||
stepAngle += angleDegrees;
|
||||
var pos = GetPointFromPolar(stepAngle, distance);
|
||||
PlayRadialAnimation(button, pos, MoveAnimationKey);
|
||||
|
||||
button.Controller.OnMouseEntered += (_) =>
|
||||
{
|
||||
PlaySizeAnimation(button, _focusSize, OutSizeAnimationKey, InSizeAnimationKey);
|
||||
ActionLabel.Text = button.Content ?? string.Empty;
|
||||
ActionLabel.Visible = IsAction;
|
||||
};
|
||||
button.Controller.OnMouseExited += (_) =>
|
||||
{
|
||||
PlaySizeAnimation(button, _normalSize, InSizeAnimationKey, OutSizeAnimationKey);
|
||||
ActionLabel.Visible = false;
|
||||
};
|
||||
}
|
||||
|
||||
CloseButton.ButtonSize = _normalSize;
|
||||
CloseButton.Controller.OnMouseEntered += (_) =>
|
||||
{
|
||||
PlaySizeAnimation(CloseButton, _focusSize, OutSizeAnimationKey, InSizeAnimationKey);
|
||||
ActionLabel.Text = CloseButton.Content ?? string.Empty;
|
||||
ActionLabel.Visible = true;
|
||||
};
|
||||
CloseButton.Controller.OnMouseExited += (_) =>
|
||||
{
|
||||
PlaySizeAnimation(CloseButton, _normalSize, InSizeAnimationKey, OutSizeAnimationKey);
|
||||
ActionLabel.Visible = false;
|
||||
};
|
||||
}
|
||||
|
||||
private void PlayRadialAnimation(Control button, Vector2 pos, string playKey)
|
||||
{
|
||||
var anim = new Animation
|
||||
{
|
||||
Length = TimeSpan.FromMilliseconds(_moveAniTime * 1000),
|
||||
AnimationTracks =
|
||||
{
|
||||
new AnimationTrackControlProperty
|
||||
{
|
||||
Property = nameof(RadialButton.Offset),
|
||||
InterpolationMode = AnimationInterpolationMode.Linear,
|
||||
KeyFrames =
|
||||
{
|
||||
new AnimationTrackProperty.KeyFrame(new Vector2(0,0), 0f),
|
||||
new AnimationTrackProperty.KeyFrame(pos, _moveAniTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
if (!button.HasRunningAnimation(playKey))
|
||||
button.PlayAnimation(anim, playKey);
|
||||
}
|
||||
|
||||
private void PlaySizeAnimation(Control button, Vector2 size, string playKey, string? stopKey)
|
||||
{
|
||||
var anim = new Animation
|
||||
{
|
||||
Length = TimeSpan.FromMilliseconds(_focusAniTime * 1000),
|
||||
AnimationTracks =
|
||||
{
|
||||
new AnimationTrackControlProperty
|
||||
{
|
||||
Property = nameof(RadialButton.ButtonSize),
|
||||
InterpolationMode = AnimationInterpolationMode.Linear,
|
||||
KeyFrames =
|
||||
{
|
||||
new AnimationTrackProperty.KeyFrame(button.Size, 0f),
|
||||
new AnimationTrackProperty.KeyFrame(size, _focusAniTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (stopKey != null && button.HasRunningAnimation(stopKey))
|
||||
button.StopAnimation(stopKey);
|
||||
if (!button.HasRunningAnimation(playKey))
|
||||
button.PlayAnimation(anim, playKey);
|
||||
}
|
||||
|
||||
protected override void Draw(DrawingHandleScreen handle)
|
||||
{
|
||||
base.Draw(handle);
|
||||
|
||||
foreach (var child in Layout.Children)
|
||||
{
|
||||
var button = (RadialButton)child;
|
||||
LayoutContainer.SetPosition(child, button.Offset - (button.Size/2));
|
||||
}
|
||||
LayoutContainer.SetPosition(CloseButton, CloseButton.Offset - (CloseButton.Size/2));
|
||||
LayoutContainer.SetPosition(ActionBox, new Vector2(0 - (ActionLabel.Size.X), FocusSize*1.5f));
|
||||
}
|
||||
|
||||
private static Vector2 GetPointFromPolar(double angleDegrees, double distance)
|
||||
{
|
||||
var angleRadians = angleDegrees * (Math.PI / 180.0);
|
||||
|
||||
var x = distance * Math.Cos(angleRadians);
|
||||
var y = distance * Math.Sin(angleRadians);
|
||||
|
||||
return new Vector2((int)Math.Round(x), (int)Math.Round(y));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user