1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 01:03:02 +08:00
osu-lazer/osu.Game/Graphics/ScreenshotManager.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2018-03-21 11:29:44 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
2018-03-14 05:17:12 +08:00
using System.Drawing.Imaging;
using System.IO;
using osu.Framework.Allocation;
2018-03-22 19:35:07 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2018-03-14 05:17:12 +08:00
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
2018-03-22 19:35:07 +08:00
private SampleChannel shutter;
2018-03-14 05:17:12 +08:00
[BackgroundDependencyLoader]
2018-03-22 19:35:07 +08:00
private void load(GameHost host, OsuConfigManager config, Storage storage, NotificationOverlay notificationOverlay, AudioManager audio)
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-22 19:35:07 +08:00
shutter = audio.Sample.Get("UI/shutter");
2018-03-14 05:17:12 +08:00
}
public bool OnPressed(GlobalAction action)
{
switch (action)
{
case GlobalAction.TakeScreenshot:
2018-03-22 19:35:07 +08:00
shutter.Play();
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();
if (fileName == null) return;
2018-03-17 02:25:00 +08:00
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!",
Activated = () =>
{
storage.OpenInNativeExplorer();
return true;
}
});
}
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
{
2018-03-21 23:27:08 +08:00
var dt = DateTime.Now;
2018-03-17 02:05:25 +08:00
var fileExt = screenshotFormat.ToString().ToLower();
2018-03-21 23:27:08 +08:00
var withoutIndex = $"osu_{dt:yyyy-MM-dd_HH-mm-ss}.{fileExt}";
2018-03-17 02:05:25 +08:00
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++)
{
2018-03-21 23:27:08 +08:00
var indexedName = $"osu_{dt:yyyy-MM-dd_HH-mm-ss}-{i}.{fileExt}";
2018-03-17 02:05:25 +08:00
if (!storage.Exists(indexedName))
2018-03-17 02:25:00 +08:00
return indexedName;
2018-03-17 02:05:25 +08:00
}
return null;
2018-03-17 02:05:25 +08:00
}
2018-03-14 05:17:12 +08:00
}
}