using OpenMacroBoard.SDK.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
namespace OpenMacroBoard.SDK
{
///
/// Represents a grid-like keyboard layout for macro boards.
///
public class GridKeyLayout : IKeyLayout
{
///
/// Initializes a new instance of the class.
///
/// Number of keys in the x-coordinate (horizontal)
/// Number of keys in the y-coordinate (vertical)
/// Square key size (pixels)
/// Distance between keys (pixels)
public GridKeyLayout(int countX, int countY, int keySize, int gapSize)
{
#pragma warning disable SA1503, IDE0011 // Braces should not be omitted
if (countX <= 0) throw new ArgumentOutOfRangeException(nameof(countX));
if (countY <= 0) throw new ArgumentOutOfRangeException(nameof(countY));
if (keySize <= 0) throw new ArgumentOutOfRangeException(nameof(keySize));
if (gapSize <= 0) throw new ArgumentOutOfRangeException(nameof(gapSize));
#pragma warning restore SA1503, IDE0011
CountX = countX;
CountY = countY;
KeySize = keySize;
GapSize = gapSize;
Count = countX * countY;
Area = this.GetFullArea();
}
///
/// Gets the number of keys on this layout.
///
public int Count { get; }
///
public int KeySize { get; }
///
public int GapSize { get; }
///
public OmbRectangle Area { get; }
///
public int CountX { get; }
///
public int CountY { get; }
///
/// Gets the dimensions of the key with a given .
///
/// The index of the key.
/// The dimensions of the requested key.
/// Is thrown if the is out of range.
public OmbRectangle this[int keyIndex]
{
get
{
if (keyIndex < 0 || keyIndex >= Count)
{
throw new ArgumentOutOfRangeException(nameof(keyIndex));
}
// split id into x and y component
var y = keyIndex / CountX;
var x = keyIndex % CountX;
var fullSize = KeySize + GapSize;
return new OmbRectangle(fullSize * x, fullSize * y, KeySize, KeySize);
}
}
///
public IEnumerator GetEnumerator()
{
for (int i = 0; i < Count; i++)
{
yield return this[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}