Screenshot button.

This commit is contained in:
Pieter-Jan Briers
2020-04-30 12:45:21 +02:00
parent ccf2139c63
commit d4ca0a65ee
6 changed files with 98 additions and 0 deletions

View File

@@ -31,6 +31,7 @@ namespace Content.Client
IoCManager.Register<IClientPreferencesManager, ClientPreferencesManager>();
IoCManager.Register<IItemSlotManager, ItemSlotManager>();
IoCManager.Register<IStylesheetManager, StylesheetManager>();
IoCManager.Register<IScreenshotHook, ScreenshotHook>();
}
}
}

View File

@@ -178,6 +178,7 @@ namespace Content.Client
IoCManager.Resolve<IParallaxManager>().LoadParallax();
IoCManager.Resolve<IBaseClient>().PlayerJoinedServer += SubscribePlayerAttachmentEvents;
IoCManager.Resolve<IStylesheetManager>().Initialize();
IoCManager.Resolve<IScreenshotHook>().Initialize();
IoCManager.InjectDependencies(this);

View File

@@ -16,6 +16,8 @@ namespace Content.Client.Input
common.AddFunction(ContentKeyFunctions.ExamineEntity);
common.AddFunction(ContentKeyFunctions.OpenTutorial);
common.AddFunction(ContentKeyFunctions.UseOrAttack);
common.AddFunction(ContentKeyFunctions.TakeScreenshot);
common.AddFunction(ContentKeyFunctions.TakeScreenshotNoUI);
var human = contexts.GetContext("human");
human.AddFunction(ContentKeyFunctions.SwapHands);

View File

@@ -0,0 +1,85 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Content.Shared.Input;
using Robust.Client.Interfaces.Graphics;
using Robust.Client.Interfaces.Input;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.Resources;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
using SixLabors.ImageSharp;
namespace Content.Client
{
internal class ScreenshotHook : IScreenshotHook
{
private static readonly ResourcePath BaseScreenshotPath = new ResourcePath("/Screenshots");
[Dependency] private readonly IInputManager _inputManager = default;
[Dependency] private readonly IClyde _clyde = default;
[Dependency] private readonly IResourceManager _resourceManager = default;
public void Initialize()
{
_inputManager.SetInputCommand(ContentKeyFunctions.TakeScreenshot, InputCmdHandler.FromDelegate(_ =>
{
Take(ScreenshotType.AfterUI);
}));
_inputManager.SetInputCommand(ContentKeyFunctions.TakeScreenshotNoUI, InputCmdHandler.FromDelegate(_ =>
{
Take(ScreenshotType.BeforeUI);
}));
}
private async void Take(ScreenshotType type)
{
var screenshot = await _clyde.ScreenshotAsync(type);
var time = DateTime.Now.ToString("yyyy-M-dd_HH.mm.ss");
if (!_resourceManager.UserData.IsDir(BaseScreenshotPath))
{
_resourceManager.UserData.CreateDir(BaseScreenshotPath);
}
for (var i = 0; i < 5; i++)
{
try
{
var filename = time;
if (i != 0)
{
filename = $"{filename}-{i}";
}
await using var file =
_resourceManager.UserData.Open(BaseScreenshotPath / $"{filename}.png", FileMode.CreateNew);
await Task.Run(() =>
{
// Saving takes forever, so don't hang the game on it.
screenshot.SaveAsPng(file);
});
Logger.InfoS("screenshot", "Screenshot taken as {0}.png", filename);
return;
}
catch (IOException e)
{
Logger.WarningS("screenshot", "Failed to save screenshot, retrying?:\n{0}", e);
}
}
Logger.ErrorS("screenshot", "Unable to save screenshot.");
}
}
public interface IScreenshotHook
{
void Initialize();
}
}

View File

@@ -24,5 +24,7 @@ namespace Content.Shared.Input
public static readonly BoundKeyFunction OpenEntitySpawnWindow = "OpenEntitySpawnWindow";
public static readonly BoundKeyFunction OpenSandboxWindow = "OpenSandboxWindow";
public static readonly BoundKeyFunction OpenTileSpawnWindow = "OpenTileSpawnWindow";
public static readonly BoundKeyFunction TakeScreenshot = "TakeScreenshot";
public static readonly BoundKeyFunction TakeScreenshotNoUI = "TakeScreenshotNoUI";
}
}

View File

@@ -155,3 +155,10 @@ binds:
- function: OpenSandboxWindow
type: state
key: B
- function: TakeScreenshot
type: state
key: F2
- function: TakeScreenshotNoUI
type: state
key: F2
mod1: Shift