1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 12:57:36 +08:00
osu-lazer/osu.Game/Graphics/ScreenshotManager.cs

90 lines
3.0 KiB
C#
Raw Normal View History

2018-03-14 05:17:12 +08:00
using System;
using System.Drawing.Imaging;
using System.IO;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
2018-03-14 05:17:12 +08:00
using osu.Framework.Platform;
using osu.Game.Configuration;
using osu.Game.Input.Bindings;
2018-03-17 02:25:00 +08:00
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
2018-03-14 05:17:12 +08:00
namespace osu.Game.Graphics
{
public class ScreenshotManager : Container, IKeyBindingHandler<GlobalAction>, IHandleGlobalInput
2018-03-14 05:17:12 +08:00
{
private Bindable<ScreenshotFormat> screenshotFormat;
private GameHost host;
private Storage storage;
2018-03-17 02:25:00 +08:00
private NotificationOverlay notificationOverlay;
2018-03-14 05:17:12 +08:00
[BackgroundDependencyLoader]
2018-03-17 02:25:00 +08:00
private void load(GameHost host, OsuConfigManager config, Storage storage, NotificationOverlay notificationOverlay)
2018-03-14 05:17:12 +08:00
{
this.host = host;
this.storage = storage.GetStorageForDirectory(@"screenshots");
2018-03-17 02:25:00 +08:00
this.notificationOverlay = notificationOverlay;
2018-03-14 05:17:12 +08:00
screenshotFormat = config.GetBindable<ScreenshotFormat>(OsuSetting.ScreenshotFormat);
}
public bool OnPressed(GlobalAction action)
{
switch (action)
{
case GlobalAction.TakeScreenshot:
TakeScreenshotAsync();
return true;
}
return false;
}
public bool OnReleased(GlobalAction action) => false;
public async void TakeScreenshotAsync()
2018-03-14 05:17:12 +08:00
{
using (var bitmap = await host.TakeScreenshotAsync())
2018-03-14 05:17:12 +08:00
{
2018-03-17 02:25:00 +08:00
var fileName = getFileName();
var stream = storage.GetStream(fileName, FileAccess.Write);
2018-03-14 05:17:12 +08:00
switch (screenshotFormat.Value)
{
case ScreenshotFormat.Png:
bitmap.Save(stream, ImageFormat.Png);
2018-03-14 05:17:12 +08:00
break;
case ScreenshotFormat.Jpg:
bitmap.Save(stream, ImageFormat.Jpeg);
2018-03-14 05:17:12 +08:00
break;
default:
throw new ArgumentOutOfRangeException(nameof(screenshotFormat));
}
2018-03-17 02:25:00 +08:00
notificationOverlay.Post(new SimpleNotification { Text = $"{fileName} saved" });
}
2018-03-14 05:17:12 +08:00
}
2018-03-17 02:05:25 +08:00
2018-03-17 02:25:00 +08:00
private string getFileName()
2018-03-17 02:05:25 +08:00
{
var fileExt = screenshotFormat.ToString().ToLower();
var withoutIndex = $"Screenshot.{fileExt}";
if (!storage.Exists(withoutIndex))
2018-03-17 02:25:00 +08:00
return withoutIndex;
2018-03-17 02:05:25 +08:00
for (ulong i = 1; i < ulong.MaxValue; i++)
{
var indexedName = $"Screenshot-{i}.{fileExt}";
if (!storage.Exists(indexedName))
2018-03-17 02:25:00 +08:00
return indexedName;
2018-03-17 02:05:25 +08:00
}
2018-03-17 02:25:00 +08:00
throw new Exception($"Failed to find suitable file name for saving {fileExt} image");
2018-03-17 02:05:25 +08:00
}
2018-03-14 05:17:12 +08:00
}
}