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;
|
2018-03-15 03:55:24 +08:00
|
|
|
|
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;
|
2018-03-15 03:55:24 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
2018-03-15 03:55:24 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-15 03:55:24 +08:00
|
|
|
|
public bool OnPressed(GlobalAction action)
|
|
|
|
|
{
|
|
|
|
|
switch (action)
|
|
|
|
|
{
|
|
|
|
|
case GlobalAction.TakeScreenshot:
|
2018-03-20 03:39:00 +08:00
|
|
|
|
TakeScreenshotAsync();
|
2018-03-15 03:55:24 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool OnReleased(GlobalAction action) => false;
|
|
|
|
|
|
2018-03-20 03:39:00 +08:00
|
|
|
|
public async void TakeScreenshotAsync()
|
2018-03-14 05:17:12 +08:00
|
|
|
|
{
|
2018-03-20 03:39:00 +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:
|
2018-03-20 03:39:00 +08:00
|
|
|
|
bitmap.Save(stream, ImageFormat.Png);
|
2018-03-14 05:17:12 +08:00
|
|
|
|
break;
|
|
|
|
|
case ScreenshotFormat.Jpg:
|
2018-03-20 03:39:00 +08:00
|
|
|
|
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-20 03:39:00 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|