mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 20:22:55 +08:00
Introduce ScreenshotManager class
This commit is contained in:
parent
e6c22e2287
commit
0e69ab1615
50
osu.Game/Graphics/ScreenshotManager.cs
Normal file
50
osu.Game/Graphics/ScreenshotManager.cs
Normal file
@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Configuration;
|
||||
|
||||
namespace osu.Game.Graphics
|
||||
{
|
||||
public class ScreenshotManager : Drawable
|
||||
{
|
||||
private Bindable<ScreenshotFormat> screenshotFormat;
|
||||
private GameHost host;
|
||||
private Storage storage;
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(GameHost host, OsuConfigManager config, Storage storage)
|
||||
{
|
||||
this.host = host;
|
||||
this.storage = storage.GetStorageForDirectory(@"screenshots");
|
||||
|
||||
screenshotFormat = config.GetBindable<ScreenshotFormat>(OsuSetting.ScreenshotFormat);
|
||||
}
|
||||
|
||||
public void TakeScreenshot()
|
||||
{
|
||||
host.TakeScreenshot(screenshotBitmap =>
|
||||
{
|
||||
var stream = storage.GetStream($"{DateTime.Now:yyyyMMddTHHmmss}.{screenshotFormat.ToString().ToLower()}", FileAccess.Write);
|
||||
|
||||
switch (screenshotFormat.Value)
|
||||
{
|
||||
case ScreenshotFormat.Bmp:
|
||||
screenshotBitmap.Save(stream, ImageFormat.Bmp);
|
||||
break;
|
||||
case ScreenshotFormat.Png:
|
||||
screenshotBitmap.Save(stream, ImageFormat.Png);
|
||||
break;
|
||||
case ScreenshotFormat.Jpg:
|
||||
screenshotBitmap.Save(stream, ImageFormat.Jpeg);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(screenshotFormat));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -3,8 +3,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using osu.Framework.Configuration;
|
||||
using osu.Framework.Screens;
|
||||
using osu.Game.Configuration;
|
||||
@ -83,9 +81,9 @@ namespace osu.Game
|
||||
private Bindable<int> configRuleset;
|
||||
public Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
|
||||
|
||||
private Bindable<int> configSkin;
|
||||
private ScreenshotManager screenshotManager;
|
||||
|
||||
private Bindable<ScreenshotFormat> screenshotFormat;
|
||||
private Bindable<int> configSkin;
|
||||
|
||||
private readonly string[] args;
|
||||
|
||||
@ -137,9 +135,6 @@ namespace osu.Game
|
||||
|
||||
// bind config int to database SkinInfo
|
||||
configSkin = LocalConfig.GetBindable<int>(OsuSetting.Skin);
|
||||
|
||||
screenshotFormat = LocalConfig.GetBindable<ScreenshotFormat>(OsuSetting.ScreenshotFormat);
|
||||
|
||||
SkinManager.CurrentSkinInfo.ValueChanged += s => configSkin.Value = s.ID;
|
||||
configSkin.ValueChanged += id => SkinManager.CurrentSkinInfo.Value = SkinManager.Query(s => s.ID == id) ?? SkinInfo.Default;
|
||||
configSkin.TriggerChange();
|
||||
@ -216,6 +211,9 @@ namespace osu.Game
|
||||
|
||||
BeatmapManager.GetStableStorage = GetStorageForStableInstall;
|
||||
|
||||
screenshotManager = new ScreenshotManager();
|
||||
Add(screenshotManager);
|
||||
|
||||
AddRange(new Drawable[]
|
||||
{
|
||||
new VolumeControlReceptor
|
||||
@ -439,46 +437,13 @@ namespace osu.Game
|
||||
direct.ToggleVisibility();
|
||||
return true;
|
||||
case GlobalAction.TakeScreenshot:
|
||||
if (Window.ScreenshotTakenAction == null)
|
||||
Window.ScreenshotTakenAction = (screenshotBitmap) =>
|
||||
{
|
||||
var fileName = getScreenshotFileName(screenshotFormat);
|
||||
|
||||
switch (screenshotFormat.Value)
|
||||
{
|
||||
case ScreenshotFormat.Bmp:
|
||||
screenshotBitmap.Save(fileName, ImageFormat.Bmp);
|
||||
break;
|
||||
case ScreenshotFormat.Png:
|
||||
screenshotBitmap.Save(fileName, ImageFormat.Png);
|
||||
break;
|
||||
case ScreenshotFormat.Jpg:
|
||||
screenshotBitmap.Save(fileName, ImageFormat.Jpeg);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(screenshotFormat));
|
||||
}
|
||||
};
|
||||
|
||||
RequestScreenshot();
|
||||
screenshotManager.TakeScreenshot();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private string getScreenshotFileName(ScreenshotFormat screenshotFormat)
|
||||
{
|
||||
// TODO Change screenshots location
|
||||
var baseDirectory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
|
||||
var screenshotsDirectory = baseDirectory.CreateSubdirectory("Screenshots");
|
||||
|
||||
var screenshotExtension = screenshotFormat.ToString().ToLower();
|
||||
var screenshots = screenshotsDirectory.GetFiles($"*.{screenshotExtension}");
|
||||
|
||||
return Path.Combine(screenshotsDirectory.FullName, $"screenshot{screenshots.Length + 1}.{screenshotExtension}");
|
||||
}
|
||||
|
||||
private readonly BindableDouble inactiveVolumeAdjust = new BindableDouble();
|
||||
|
||||
protected override void OnDeactivated()
|
||||
|
@ -288,6 +288,7 @@
|
||||
<Compile Include="Database\MutableDatabaseBackedStore.cs" />
|
||||
<Compile Include="Database\SingletonContextFactory.cs" />
|
||||
<Compile Include="Graphics\Containers\LinkFlowContainer.cs" />
|
||||
<Compile Include="Graphics\ScreenshotManager.cs" />
|
||||
<Compile Include="Graphics\Textures\LargeTextureStore.cs" />
|
||||
<Compile Include="IO\Archives\ArchiveReader.cs" />
|
||||
<Compile Include="IO\Archives\LegacyFilesystemReader.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user