From 0e69ab161549dd32e0e2144bf49b11da21a4a165 Mon Sep 17 00:00:00 2001 From: TocoToucan Date: Wed, 14 Mar 2018 00:17:12 +0300 Subject: [PATCH] Introduce ScreenshotManager class --- osu.Game/Graphics/ScreenshotManager.cs | 50 ++++++++++++++++++++++++++ osu.Game/OsuGame.cs | 47 ++++-------------------- osu.Game/osu.Game.csproj | 1 + 3 files changed, 57 insertions(+), 41 deletions(-) create mode 100644 osu.Game/Graphics/ScreenshotManager.cs diff --git a/osu.Game/Graphics/ScreenshotManager.cs b/osu.Game/Graphics/ScreenshotManager.cs new file mode 100644 index 0000000000..7304d653cd --- /dev/null +++ b/osu.Game/Graphics/ScreenshotManager.cs @@ -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; + 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(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)); + } + }); + } + } +} diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 1078548bef..18ea093e97 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -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 configRuleset; public Bindable Ruleset = new Bindable(); - private Bindable configSkin; + private ScreenshotManager screenshotManager; - private Bindable screenshotFormat; + private Bindable configSkin; private readonly string[] args; @@ -137,9 +135,6 @@ namespace osu.Game // bind config int to database SkinInfo configSkin = LocalConfig.GetBindable(OsuSetting.Skin); - - screenshotFormat = LocalConfig.GetBindable(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() diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index fdda575a6c..7576dd6f0c 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -288,6 +288,7 @@ +