75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media.Animation;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Threading;
|
|
|
|
namespace HighGroundRoyaleLauncher
|
|
{
|
|
public class BackgroundSlideshowManager
|
|
{
|
|
private readonly string[] _imagePaths;
|
|
private readonly Image _targetImage;
|
|
private readonly DispatcherTimer _timer;
|
|
private int _currentIndex = 0;
|
|
|
|
public BackgroundSlideshowManager(Image targetImage, string[] imagePaths, TimeSpan interval)
|
|
{
|
|
_targetImage = targetImage;
|
|
_imagePaths = imagePaths;
|
|
|
|
_timer = new DispatcherTimer
|
|
{
|
|
Interval = interval
|
|
};
|
|
_timer.Tick += OnTimerTick;
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
UpdateImage(); // first image
|
|
_timer.Start();
|
|
}
|
|
|
|
private void OnTimerTick(object sender, EventArgs e)
|
|
{
|
|
_currentIndex = (_currentIndex + 1) % _imagePaths.Length;
|
|
FadeToNextImage();
|
|
}
|
|
|
|
private void FadeToNextImage()
|
|
{
|
|
var fadeOut = new DoubleAnimation(1.0, 0.0, new Duration(TimeSpan.FromSeconds(1.5)));
|
|
fadeOut.Completed += (s, e) =>
|
|
{
|
|
UpdateImage();
|
|
|
|
var fadeIn = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(1.5)));
|
|
_targetImage.BeginAnimation(UIElement.OpacityProperty, fadeIn);
|
|
};
|
|
|
|
_targetImage.BeginAnimation(UIElement.OpacityProperty, fadeOut);
|
|
}
|
|
|
|
private void UpdateImage()
|
|
{
|
|
try
|
|
{
|
|
var uri = new Uri($"pack://application:,,,/{_imagePaths[_currentIndex]}", UriKind.Absolute);
|
|
var bitmap = new BitmapImage(uri);
|
|
_targetImage.Source = bitmap;
|
|
}
|
|
catch
|
|
{
|
|
// Optional: silently fail or log if an image can't load
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
_timer.Stop();
|
|
}
|
|
}
|
|
}
|