Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Анимированный фон в лобби #306

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions Content.Client/ADT/UI/AnimatedBackground/AnimatedBackgroundControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Linq;
using Content.Shared.ADT;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Graphics.RSI;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;

namespace Content.Client.ADT.UI.AnimatedBackground;

public sealed class AnimatedBackgroundControl : TextureRect
{
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly IClyde _clyde = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;

private string _rsiPath = "/Textures/ADT/LobbyScreens/backgrounds/native.rsi";
public RSI? _RSI;
private const int States = 1;

private IRenderTexture? _buffer;

private readonly float[] _timer = new float[States];
private readonly float[][] _frameDelays = new float[States][];
private readonly int[] _frameCounter = new int[States];
private readonly Texture[][] _frames = new Texture[States][];

public AnimatedBackgroundControl()
{
IoCManager.InjectDependencies(this);

InitializeStates();
}

private void InitializeStates()
{
_RSI ??= _resourceCache.GetResource<RSIResource>(_rsiPath).RSI;

for (var i = 0; i < States; i++)
{
if (!_RSI.TryGetState((i + 1).ToString(), out var state))
continue;

_frames[i] = state.GetFrames(RsiDirection.South);
_frameDelays[i] = state.GetDelays();
_frameCounter[i] = 0;
}
}

public void SetRSI(RSI? rsi)
{
_RSI = rsi;
InitializeStates();
}

protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);

for (var i = 0; i < _frames.Length; i++)
{
var delays = _frameDelays[i];
if (delays.Length == 0)
continue;

_timer[i] += args.DeltaSeconds;

var currentFrameIndex = _frameCounter[i];

if (!(_timer[i] >= delays[currentFrameIndex]))
continue;

_timer[i] -= delays[currentFrameIndex];
_frameCounter[i] = (currentFrameIndex + 1) % _frames[i].Length;
Texture = _frames[i][_frameCounter[i]];
}
}

protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);

if (_buffer is null)
return;

handle.DrawTextureRect(_buffer.Texture, PixelSizeBox);
}

protected override void Resized()
{
base.Resized();
_buffer?.Dispose();
_buffer = _clyde.CreateRenderTarget(PixelSize, RenderTargetColorFormat.Rgba8Srgb);
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_buffer?.Dispose();
}

public void RandomizeBackground()
{
var backgroundsProto = _prototypeManager.EnumeratePrototypes<AnimatedLobbyScreenPrototype>().ToList();
var random = new Random();
var index = random.Next(backgroundsProto.Count);
_rsiPath = $"/Textures/{backgroundsProto[index].Path}";
InitializeStates();
}
}
6 changes: 1 addition & 5 deletions Content.Client/Lobby/LobbyState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,7 @@ private void UpdateLobbyBackground()
{
if (_gameTicker.LobbyBackground != null)
{
Lobby!.Background.Texture = _resourceCache.GetResource<TextureResource>(_gameTicker.LobbyBackground );
}
else
{
Lobby!.Background.Texture = null;
Lobby!.Background.SetRSI(_resourceCache.GetResource<RSIResource>(_gameTicker.LobbyBackground).RSI); // ADT Tweak
}

}
Expand Down
8 changes: 5 additions & 3 deletions Content.Client/Lobby/UI/LobbyGui.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
xmlns:style="clr-namespace:Content.Client.Stylesheets"
xmlns:lobbyUi="clr-namespace:Content.Client.Lobby.UI"
xmlns:info="clr-namespace:Content.Client.Info"
xmlns:widgets="clr-namespace:Content.Client.UserInterface.Systems.Chat.Widgets">
xmlns:widgets="clr-namespace:Content.Client.UserInterface.Systems.Chat.Widgets"
xmlns:ui="clr-namespace:Content.Client.ADT.UI"
xmlns:animatedBackground="clr-namespace:Content.Client.ADT.UI.AnimatedBackground">
<!-- Background -->
<TextureRect Access="Public" VerticalExpand="True" HorizontalExpand="True" Name="Background"
Stretch="KeepAspectCovered" />
<animatedBackground:AnimatedBackgroundControl Access="Public" Name="Background" VerticalExpand="True" HorizontalExpand="True"
Stretch="KeepAspectCovered" />
<BoxContainer Name="MainContainer" VerticalExpand="True" HorizontalExpand="True" Orientation="Horizontal"
Margin="10 10 10 10" SeparationOverride="2">
<SplitContainer State="Auto" HorizontalExpand="True">
Expand Down
15 changes: 7 additions & 8 deletions Content.Server/GameTicking/GameTicker.LobbyBackground.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Content.Server.GameTicking.Prototypes;
using Robust.Shared.Random;
using Content.Shared.ADT;
using Robust.Shared.Utility;
using System.Linq;

Expand All @@ -11,21 +12,19 @@ public sealed partial class GameTicker
public string? LobbyBackground { get; private set; }

[ViewVariables]
private List<ResPath>? _lobbyBackgrounds;

private static readonly string[] WhitelistedBackgroundExtensions = new string[] {"png", "jpg", "jpeg", "webp"};
private List<string>? _lobbyBackgrounds; // ADT Tweak

private void InitializeLobbyBackground()
{
_lobbyBackgrounds = _prototypeManager.EnumeratePrototypes<LobbyBackgroundPrototype>()
.Select(x => x.Background)
.Where(x => WhitelistedBackgroundExtensions.Contains(x.Extension))
_lobbyBackgrounds = _prototypeManager.EnumeratePrototypes<AnimatedLobbyScreenPrototype>() // ADT Tweak
.Select(x => x.Path)
.ToList();

RandomizeLobbyBackground();
}

private void RandomizeLobbyBackground() {
LobbyBackground = _lobbyBackgrounds!.Any() ? _robustRandom.Pick(_lobbyBackgrounds!).ToString() : null;
private void RandomizeLobbyBackground()
Darkiich marked this conversation as resolved.
Show resolved Hide resolved
{
LobbyBackground = _lobbyBackgrounds!.Any() ? _robustRandom.Pick(_lobbyBackgrounds!) : null; // ADT Tweak
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Robust.Shared.Prototypes;

namespace Content.Shared.ADT;

/// <summary>
/// This is a prototype for...
/// </summary>
[Prototype]
public sealed partial class AnimatedLobbyScreenPrototype : IPrototype
{
/// <inheritdoc/>
[IdDataField]
public string ID { get; } = default!;

[DataField(required: true)]
public string Path = default!;
}
35 changes: 35 additions & 0 deletions Resources/Prototypes/ADT/AnimatedLobbyScreen/lobbyScreens.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- type: animatedLobbyScreen
id: SeaLobbyScreen
path: /Textures/ADT/LobbyScreens/backgrounds/sea.rsi

- type: animatedLobbyScreen
id: RocketLobbyScreen
path: /Textures/ADT/LobbyScreens/backgrounds/rocket.rsi

- type: animatedLobbyScreen
id: CubeLobbyScreen
path: /Textures/ADT/LobbyScreens/backgrounds/cube.rsi

- type: animatedLobbyScreen
id: MilkywayLobbyScreen
path: /Textures/ADT/LobbyScreens/backgrounds/milkyway.rsi

#- type: animatedLobbyScreen
# id: NativeLobbyScreen
# path: White/Lobby/backgrounds/native.rsi

- type: animatedLobbyScreen
id: CentcommLobbyScreen
path: /Textures/ADT/LobbyScreens/backgrounds/centcomm.rsi

- type: animatedLobbyScreen
id: VangoghLobbyScreen
path: /Textures/ADT/LobbyScreens/backgrounds/vangogh.rsi

#- type: animatedLobbyScreen
# id: ThirdLobbyScreen
# path: Ohio/Lobby/backgrounds/3.rsi

#- type: animatedLobbyScreen
# id: FifthLobbyScreen
# path: Ohio/Lobby/backgrounds/5.rsi
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions Resources/Textures/ADT/LobbyScreens/backgrounds/3.rsi/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": 1,
"license": "CC-BY-NC-SA-4.0",
"copyright": "WhiteDream",
"size": {
"x": 1920,
"y": 875
},
"states": [
{
"name": "1",
"delays": [
[
0.1
]
]
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": 1,
"license": "CC-BY-NC-SA-4.0",
"copyright": "WhiteDream",
"size": {
"x": 901,
"y": 676
},
"states": [
{
"name": "1",
"delays": [
[
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015,
0.015
]
]
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading