55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace OpenMacroBoard.SDK
|
|
{
|
|
/// <summary>
|
|
/// A <see cref="IMacroBoard"/> adapter that replays brightness and key bitmaps if a device is disconnected.
|
|
/// </summary>
|
|
public class DisconnectReplayAdapter : MacroBoardAdapter
|
|
{
|
|
private readonly Dictionary<int, KeyBitmap> mostRecentKeyBitmaps = new();
|
|
private byte? mostRecentBrightness = null;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="DisconnectReplayAdapter"/> class.
|
|
/// </summary>
|
|
public DisconnectReplayAdapter(IMacroBoard macroBoard)
|
|
: base(macroBoard)
|
|
{
|
|
ConnectionStateChanged += ReplayEventsForConnectionStateChange;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void SetBrightness(byte percent)
|
|
{
|
|
mostRecentBrightness = percent;
|
|
base.SetBrightness(percent);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public override void SetButtonImage(int keyId, KeyBitmap bitmapData)
|
|
{
|
|
mostRecentKeyBitmaps[keyId] = bitmapData;
|
|
base.SetButtonImage(keyId, bitmapData);
|
|
}
|
|
|
|
private void ReplayEventsForConnectionStateChange(object sender, ConnectionEventArgs e)
|
|
{
|
|
if (e.NewConnectionState)
|
|
{
|
|
// devices connected again: replay last known values
|
|
|
|
if (mostRecentBrightness is not null)
|
|
{
|
|
base.SetBrightness(mostRecentBrightness.Value);
|
|
}
|
|
|
|
foreach (var bmp in mostRecentKeyBitmaps)
|
|
{
|
|
base.SetButtonImage(bmp.Key, bmp.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|