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.Game.Configuration;
using osu.Game.Input.Bindings;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Graphics
{
@ -17,12 +19,14 @@ public class ScreenshotManager : Container, IKeyBindingHandler<GlobalAction>, IH
private Bindable<ScreenshotFormat> screenshotFormat;
private GameHost host;
private Storage storage;
private NotificationOverlay notificationOverlay;
[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.storage = storage.GetStorageForDirectory(@"screenshots");
this.notificationOverlay = notificationOverlay;
screenshotFormat = config.GetBindable<ScreenshotFormat>(OsuSetting.ScreenshotFormat);
}
@ -45,7 +49,8 @@ public void TakeScreenshot()
{
host.TakeScreenshot(screenshotBitmap =>
{
var stream = getFileStream();
var fileName = getFileName();
var stream = storage.GetStream(fileName, FileAccess.Write);
switch (screenshotFormat.Value)
{
@ -58,25 +63,27 @@ public void TakeScreenshot()
default:
throw new ArgumentOutOfRangeException(nameof(screenshotFormat));
}
notificationOverlay.Post(new SimpleNotification { Text = $"{fileName} saved" });
});
}
private Stream getFileStream()
private string getFileName()
{
var fileExt = screenshotFormat.ToString().ToLower();
var withoutIndex = $"Screenshot.{fileExt}";
if (!storage.Exists(withoutIndex))
return storage.GetStream(withoutIndex, FileAccess.Write);
return withoutIndex;
for (ulong i = 1; i < ulong.MaxValue; i++)
{
var indexedName = $"Screenshot-{i}.{fileExt}";
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 },
overlayContent = new Container { RelativeSizeAxes = Axes.Both, Depth = float.MinValue },
new ScreenshotManager()
});
loadComponentSingleFile(screenStack = new Loader(), d =>
@ -286,6 +285,8 @@ protected override void LoadComplete()
dependencies.Cache(notifications);
dependencies.Cache(dialogOverlay);
Add(new ScreenshotManager());
// ensure only one of these overlays are open at once.
var singleDisplayOverlays = new OverlayContainer[] { chat, social, direct };
foreach (var overlay in singleDisplayOverlays)