// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using System.Linq;
namespace RGB.NET.Core;
///
/// Offers some extensions and helper-methods for the work with the surface.
///
public static class SurfaceExtensions
{
#region Methods
///
/// Initializes the specifiec device provider and attaches all devices.
///
/// The surface to attach the devices to.
/// The device provider to load.
/// -flags to filter the devices to load.
/// Specifies if exceptions should be thrown or silently be ignored.
public static void Load(this RGBSurface surface, IRGBDeviceProvider deviceProvider, RGBDeviceType loadFilter = RGBDeviceType.All, bool throwExceptions = false)
{
if (!deviceProvider.IsInitialized)
deviceProvider.Initialize(loadFilter, throwExceptions);
surface.Attach(deviceProvider.Devices);
}
///
/// Attaches the specified devices to the surface.
///
/// The surface the devices are attached to.
/// The devices to attach.
public static void Attach(this RGBSurface surface, IEnumerable devices)
{
foreach (IRGBDevice device in devices)
surface.Attach(device);
}
///
/// Detaches the specified devices from the surface.
///
/// The surface the devices are detached from.
/// The devices to detach.
public static void Detach(this RGBSurface surface, IEnumerable devices)
{
foreach (IRGBDevice device in devices)
surface.Detach(device);
}
///
/// Gets all devices of a specific type.
///
/// The type of devices to get.
/// A collection of devices with the specified type.
public static IEnumerable GetDevices(this RGBSurface surface)
where T : class
=> surface.Devices.Where(x => x is T).Cast();
///
/// Gets all devices of the specified .
///
/// The surface to get the devices from.
/// The of the devices to get.
/// A collection of devices matching the specified .
public static IEnumerable GetDevices(this RGBSurface surface, RGBDeviceType deviceType)
=> surface.Devices.Where(d => deviceType.HasFlag(d.DeviceInfo.DeviceType));
///
/// Automatically aligns all devices to prevent overlaps.
///
public static void AlignDevices(this RGBSurface surface)
{
float posX = 0;
foreach (IRGBDevice device in surface.Devices)
{
device.Location += new Point(posX, 0);
posX += device.ActualSize.Width + 1;
}
}
#endregion
}