145 lines
6.2 KiB
C#
145 lines
6.2 KiB
C#
using System;
|
|
using System.Reflection.PortableExecutable;
|
|
|
|
namespace OpenMacroBoard.SDK {
|
|
/// <summary>
|
|
/// Wraps an <see cref="IMacroBoard"/> and allows for hooks to implement "middle-ware" features.
|
|
/// </summary>
|
|
public abstract class MacroBoardAdapter : IMacroBoard {
|
|
private readonly IMacroBoard macroBoard;
|
|
private readonly Boolean leaveOpen;
|
|
|
|
private Boolean disposed = false;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MacroBoardAdapter"/> class.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>When this instance is disposed, the underlying board is disposed as well.</para>
|
|
/// </remarks>
|
|
/// <param name="macroBoard">The macroBoard that is wrapped.</param>
|
|
protected MacroBoardAdapter(IMacroBoard macroBoard) : this(macroBoard, false) {
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="MacroBoardAdapter"/> class.
|
|
/// </summary>
|
|
/// <param name="macroBoard">The macroBoard that is wrapped.</param>
|
|
/// <param name="leaveOpen">When true, the underlying macroBoard will not be disposed with this instance.</param>
|
|
protected MacroBoardAdapter(IMacroBoard macroBoard, Boolean leaveOpen) {
|
|
this.macroBoard = macroBoard ?? throw new ArgumentNullException(nameof(macroBoard));
|
|
this.leaveOpen = leaveOpen;
|
|
|
|
macroBoard.ConnectionStateChanged += this.OnConnectionStateChanged;
|
|
//macroBoard.KeyStateChanged += this.OnKeyStateChanged;
|
|
macroBoard.ButtonPressed += this.MacroBoard_ButtonPressed;
|
|
macroBoard.ButtonReleased += this.MacroBoard_ButtonReleased;
|
|
macroBoard.TouchbarTouched += this.MacroBoard_TouchbarTouched;
|
|
macroBoard.TouchbarPressed += this.MacroBoard_TouchbarPressed;
|
|
macroBoard.TouchFlipEvent += this.MacroBoard_TouchFlipEvent;
|
|
macroBoard.EncoderPressed += this.MacroBoard_EncoderPressed;
|
|
macroBoard.EncoderReleased += this.MacroBoard_EncoderReleased;
|
|
macroBoard.EncoderTurn += this.MacroBoard_EncoderTurn;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Finalizes an instance of the <see cref="MacroBoardAdapter"/> class.
|
|
/// </summary>
|
|
~MacroBoardAdapter() {
|
|
this.Dispose(false);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
//public event EventHandler<KeyEventArgs> KeyStateChanged;
|
|
|
|
public event EventHandler<KeyPressEvent> ButtonPressed;
|
|
public event EventHandler<KeyPressEvent> ButtonReleased;
|
|
public event EventHandler<TouchTipEvent> TouchbarTouched;
|
|
public event EventHandler<TouchTipEvent> TouchbarPressed;
|
|
public event EventHandler<TouchFlipEvent> TouchFlipEvent;
|
|
public event EventHandler<KnobPressEvent> EncoderPressed;
|
|
public event EventHandler<KnobPressEvent> EncoderReleased;
|
|
public event EventHandler<KnobTurnEvent> EncoderTurn;
|
|
|
|
/// <inheritdoc/>
|
|
public event EventHandler<ConnectionEventArgs> ConnectionStateChanged;
|
|
|
|
/// <inheritdoc/>
|
|
public virtual IKeyLayout Keys => this.macroBoard.Keys;
|
|
|
|
/// <inheritdoc/>
|
|
public virtual Boolean IsConnected => this.macroBoard.IsConnected;
|
|
|
|
/// <inheritdoc/>
|
|
public void Dispose() {
|
|
this.Dispose(true);
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public virtual void SetBrightness(Byte percent) => this.macroBoard.SetBrightness(percent);
|
|
|
|
/// <inheritdoc/>
|
|
public virtual void SetButtonImage(Int32 keyId, KeyBitmap bitmapData) => this.macroBoard.SetButtonImage(keyId, bitmapData);
|
|
|
|
/// <inheritdoc/>
|
|
public void SetFullScreenImage(KeyBitmap bitmapData) => throw new NotImplementedException();
|
|
|
|
/// <inheritdoc/>
|
|
public virtual void SetWindowImage(KeyBitmap bitmapData) => throw new NotImplementedException();
|
|
|
|
/// <inheritdoc/>
|
|
public virtual void SetPartialWindowImage(Int32 x_pos, Int32 y_pos, KeyBitmap bitmapData) => throw new NotImplementedException();
|
|
|
|
/// <inheritdoc/>
|
|
public virtual void ShowLogo() => this.macroBoard.ShowLogo();
|
|
|
|
/// <inheritdoc/>
|
|
public String GetFirmwareVersion() => this.macroBoard.GetFirmwareVersion();
|
|
|
|
/// <inheritdoc/>
|
|
public String GetSerialNumber() => this.macroBoard.GetFirmwareVersion();
|
|
|
|
// <summary>
|
|
// Virtual KeyStateChanged event handler.
|
|
// </summary>
|
|
// <param name="sender">The sender of the original event.</param>
|
|
// <param name="e">The event arguments.</param>
|
|
//protected virtual void OnKeyStateChanged(Object sender, KeyEventArgs e) => KeyStateChanged?.Invoke(this, e);
|
|
|
|
private void MacroBoard_EncoderTurn(Object sender, KnobTurnEvent e) => this.EncoderTurn?.Invoke(this, e);
|
|
private void MacroBoard_EncoderReleased(Object sender, KnobPressEvent e) => this.EncoderReleased?.Invoke(this, e);
|
|
private void MacroBoard_EncoderPressed(Object sender, KnobPressEvent e) => this.EncoderPressed?.Invoke(this, e);
|
|
private void MacroBoard_TouchFlipEvent(Object sender, TouchFlipEvent e) => this.TouchFlipEvent?.Invoke(this, e);
|
|
private void MacroBoard_TouchbarPressed(Object sender, TouchTipEvent e) => this.TouchbarPressed?.Invoke(this, e);
|
|
private void MacroBoard_TouchbarTouched(Object sender, TouchTipEvent e) => this.TouchbarTouched?.Invoke(this, e);
|
|
private void MacroBoard_ButtonReleased(Object sender, KeyPressEvent e) => this.ButtonReleased?.Invoke(this, e);
|
|
private void MacroBoard_ButtonPressed(Object sender, KeyPressEvent e) => this.ButtonPressed?.Invoke(this, e);
|
|
|
|
/// <summary>
|
|
/// Virtual ConnectionStateChanged event handler.
|
|
/// </summary>
|
|
/// <param name="sender">The sender of the original event.</param>
|
|
/// <param name="e">The event arguments.</param>
|
|
protected virtual void OnConnectionStateChanged(Object sender, ConnectionEventArgs e) => ConnectionStateChanged?.Invoke(this, e);
|
|
|
|
/// <summary>
|
|
/// Protected implementation of Dispose pattern.
|
|
/// </summary>
|
|
/// <param name="disposing">True when called from <see cref="Dispose()"/> and false when called from the finalizer.</param>
|
|
protected virtual void Dispose(Boolean disposing) {
|
|
if(this.disposed) {
|
|
return;
|
|
}
|
|
|
|
if(disposing && !this.leaveOpen) {
|
|
this.macroBoard?.Dispose();
|
|
}
|
|
|
|
this.disposed = true;
|
|
}
|
|
}
|
|
}
|