Add simple screenshot notification

This commit is contained in:
TocoToucan 2018-03-16 21:25:00 +03:00
parent 8429408974
commit 245200d3ee
2 changed files with 15 additions and 7 deletions

View File

@ -9,6 +9,8 @@
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Input.Bindings; using osu.Game.Input.Bindings;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Graphics namespace osu.Game.Graphics
{ {
@ -17,12 +19,14 @@ public class ScreenshotManager : Container, IKeyBindingHandler<GlobalAction>, IH
private Bindable<ScreenshotFormat> screenshotFormat; private Bindable<ScreenshotFormat> screenshotFormat;
private GameHost host; private GameHost host;
private Storage storage; private Storage storage;
private NotificationOverlay notificationOverlay;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(GameHost host, OsuConfigManager config, Storage storage) private void load(GameHost host, OsuConfigManager config, Storage storage, NotificationOverlay notificationOverlay)
{ {
this.host = host; this.host = host;
this.storage = storage.GetStorageForDirectory(@"screenshots"); this.storage = storage.GetStorageForDirectory(@"screenshots");
this.notificationOverlay = notificationOverlay;
screenshotFormat = config.GetBindable<ScreenshotFormat>(OsuSetting.ScreenshotFormat); screenshotFormat = config.GetBindable<ScreenshotFormat>(OsuSetting.ScreenshotFormat);
} }
@ -45,7 +49,8 @@ public void TakeScreenshot()
{ {
host.TakeScreenshot(screenshotBitmap => host.TakeScreenshot(screenshotBitmap =>
{ {
var stream = getFileStream(); var fileName = getFileName();
var stream = storage.GetStream(fileName, FileAccess.Write);
switch (screenshotFormat.Value) switch (screenshotFormat.Value)
{ {
@ -58,25 +63,27 @@ public void TakeScreenshot()
default: default:
throw new ArgumentOutOfRangeException(nameof(screenshotFormat)); throw new ArgumentOutOfRangeException(nameof(screenshotFormat));
} }
notificationOverlay.Post(new SimpleNotification { Text = $"{fileName} saved" });
}); });
} }
private Stream getFileStream() private string getFileName()
{ {
var fileExt = screenshotFormat.ToString().ToLower(); var fileExt = screenshotFormat.ToString().ToLower();
var withoutIndex = $"Screenshot.{fileExt}"; var withoutIndex = $"Screenshot.{fileExt}";
if (!storage.Exists(withoutIndex)) if (!storage.Exists(withoutIndex))
return storage.GetStream(withoutIndex, FileAccess.Write); return withoutIndex;
for (ulong i = 1; i < ulong.MaxValue; i++) for (ulong i = 1; i < ulong.MaxValue; i++)
{ {
var indexedName = $"Screenshot-{i}.{fileExt}"; var indexedName = $"Screenshot-{i}.{fileExt}";
if (!storage.Exists(indexedName)) if (!storage.Exists(indexedName))
return storage.GetStream(indexedName, FileAccess.Write); return indexedName;
} }
throw new Exception($"Failed to get stream for saving {fileExt} file"); throw new Exception($"Failed to find suitable file name for saving {fileExt} image");
} }
} }
} }

View File

@ -218,7 +218,6 @@ protected override void LoadComplete()
}, },
mainContent = new Container { RelativeSizeAxes = Axes.Both }, mainContent = new Container { RelativeSizeAxes = Axes.Both },
overlayContent = new Container { RelativeSizeAxes = Axes.Both, Depth = float.MinValue }, overlayContent = new Container { RelativeSizeAxes = Axes.Both, Depth = float.MinValue },
new ScreenshotManager()
}); });
loadComponentSingleFile(screenStack = new Loader(), d => loadComponentSingleFile(screenStack = new Loader(), d =>
@ -286,6 +285,8 @@ protected override void LoadComplete()
dependencies.Cache(notifications); dependencies.Cache(notifications);
dependencies.Cache(dialogOverlay); dependencies.Cache(dialogOverlay);
Add(new ScreenshotManager());
// ensure only one of these overlays are open at once. // ensure only one of these overlays are open at once.
var singleDisplayOverlays = new OverlayContainer[] { chat, social, direct }; var singleDisplayOverlays = new OverlayContainer[] { chat, social, direct };
foreach (var overlay in singleDisplayOverlays) foreach (var overlay in singleDisplayOverlays)