2021-05-01 20:49:58 +02:00
using System ;
using System.Collections.Generic ;
using Illumilib.System ;
namespace Illumilib {
/// <summary>
/// The class that houses all Illumilib methods.
/// This class does not need to be instantiated.
/// </summary>
public static class IllumilibLighting {
2021-05-01 22:35:45 +02:00
/// <summary>
/// The maximum width that a keyboard can have, in amount of keys
/// </summary>
public const int KeyboardWidth = 22 ;
/// <summary>
/// The maximum height that a keyboard can have, in amount of keys
/// </summary>
public const int KeyboardHeight = 6 ;
2022-01-19 12:12:36 +01:00
private static Dictionary < LightingType , LightingSystem > systems ;
2021-05-01 21:17:05 +02:00
/// <summary>
/// A property that returns whether Illumilib is currently initialized
/// </summary>
public static bool Initialized = > systems ! = null ;
2021-05-01 20:49:58 +02:00
/// <summary>
/// Initializes Illumilib, starting all of the supported lighting systems.
/// Any lighting systems that are not supported, or for which devices are not present, will be ignored.
/// </summary>
2021-05-02 19:34:21 +02:00
/// <returns>Whether at least one lighting system was successfully initialized</returns>
2021-05-01 21:17:05 +02:00
/// <exception cref="InvalidOperationException">Thrown if Illumilib has already been <see cref="Initialized"/></exception>
2021-05-02 19:34:21 +02:00
public static bool Initialize ( ) {
2021-05-01 21:17:05 +02:00
if ( Initialized )
2021-05-01 20:49:58 +02:00
throw new InvalidOperationException ( "Illumilib has already been initialized" ) ;
2022-01-19 12:12:36 +01:00
systems = new Dictionary < LightingType , LightingSystem > ( ) ;
2021-05-03 00:53:54 +02:00
foreach ( var system in new LightingSystem [ ] { new LogitechLighting ( ) , new RazerLighting ( ) , new CorsairLighting ( ) } ) {
2021-05-02 19:34:21 +02:00
if ( system . Initialize ( ) )
2022-01-19 12:12:36 +01:00
systems . Add ( system . Type , system ) ;
2021-05-01 20:49:58 +02:00
}
2021-05-02 19:34:21 +02:00
return systems . Count > 0 ;
2021-05-01 20:49:58 +02:00
}
/// <summary>
/// Disposes Illumilib, disposing all of the underlying lighting systems
/// </summary>
public static void Dispose ( ) {
2021-05-01 21:17:05 +02:00
if ( ! Initialized )
return ;
2022-01-19 12:12:36 +01:00
foreach ( var system in systems . Values )
2022-01-19 11:51:59 +01:00
system . Dispose ( ) ;
2021-05-01 20:49:58 +02:00
systems = null ;
}
2022-01-19 12:12:36 +01:00
/// <summary>
/// Returns whether the given <see cref="LightingType"/> has been initialized successfully and is enabled.
/// </summary>
/// <param name="type">The <see cref="LightingType"/> to query.</param>
/// <returns>Whether the given <see cref="LightingType"/> has been initialized and is enabled.</returns>
public static bool IsEnabled ( LightingType type ) {
EnsureInitialized ( ) ;
return systems . ContainsKey ( type ) ;
}
2021-05-01 20:49:58 +02:00
/// <summary>
/// Sets the lighting for all keyboards and mice to the given color
/// </summary>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
public static void SetAllLighting ( float r , float g , float b ) {
2022-01-19 11:51:59 +01:00
EnsureInitialized ( ) ;
2022-01-19 12:12:36 +01:00
foreach ( var system in systems . Values )
2022-01-19 11:51:59 +01:00
system . SetAllLighting ( r , g , b ) ;
2021-05-01 20:49:58 +02:00
}
/// <summary>
2021-05-01 22:35:45 +02:00
/// Sets the lighting for all keyboards to the given color
2021-05-01 20:49:58 +02:00
/// </summary>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
public static void SetKeyboardLighting ( float r , float g , float b ) {
2022-01-19 11:51:59 +01:00
EnsureInitialized ( ) ;
2022-01-19 12:12:36 +01:00
foreach ( var system in systems . Values )
2022-01-19 11:51:59 +01:00
system . SetKeyboardLighting ( r , g , b ) ;
2021-05-01 20:49:58 +02:00
}
/// <summary>
2021-05-01 22:35:45 +02:00
/// Sets the lighting for the given x, y position on the keyboard to the given color.
2021-05-03 00:53:54 +02:00
/// The position is zero-based, with 0, 0 being the key in the top left corner of the keyboard.
2021-05-01 20:49:58 +02:00
/// </summary>
2021-05-01 22:35:45 +02:00
/// <param name="x">The zero-based x position of the key</param>
/// <param name="y">The zero-based y position of the key</param>
2021-05-01 20:49:58 +02:00
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
2021-05-01 22:35:45 +02:00
/// <exception cref="ArgumentOutOfRangeException">Thrown if the positions are out of range in relation to <see cref="KeyboardWidth"/> and <see cref="KeyboardHeight"/></exception>
public static void SetKeyboardLighting ( int x , int y , float r , float g , float b ) {
2022-01-19 11:51:59 +01:00
EnsureInitialized ( ) ;
2021-05-01 22:35:45 +02:00
if ( x < 0 | | x > = KeyboardWidth )
throw new ArgumentOutOfRangeException ( nameof ( x ) ) ;
if ( y < 0 | | y > = KeyboardHeight )
throw new ArgumentOutOfRangeException ( nameof ( y ) ) ;
2022-01-19 12:12:36 +01:00
foreach ( var system in systems . Values )
2022-01-19 11:51:59 +01:00
system . SetKeyboardLighting ( x , y , r , g , b ) ;
2021-05-01 22:35:45 +02:00
}
/// <summary>
/// Sets the lighting in the given area on the keyboard to the given color.
2021-05-03 00:53:54 +02:00
/// The position is zero-based, with 0, 0 being the key in the top left corner of the keyboard.
2021-05-01 22:35:45 +02:00
/// The position is the top left corner of the rectangle that represents the area to set colors in.
/// </summary>
/// <param name="x">The zero-based x position of the key</param>
/// <param name="y">The zero-based y position of the key</param>
/// <param name="width">The width of the area to set the color in</param>
/// <param name="height">The height of the area to set the color in</param>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if the positions are out of range in relation to <see cref="KeyboardWidth"/> and <see cref="KeyboardHeight"/></exception>
public static void SetKeyboardLighting ( int x , int y , int width , int height , float r , float g , float b ) {
2022-01-19 11:51:59 +01:00
EnsureInitialized ( ) ;
2021-05-01 22:35:45 +02:00
if ( x < 0 | | x + width > KeyboardWidth )
throw new ArgumentOutOfRangeException ( nameof ( x ) ) ;
if ( y < 0 | | y + height > KeyboardHeight )
throw new ArgumentOutOfRangeException ( nameof ( y ) ) ;
2022-01-19 12:12:36 +01:00
foreach ( var system in systems . Values )
2022-01-19 11:51:59 +01:00
system . SetKeyboardLighting ( x , y , width , height , r , g , b ) ;
2021-05-01 20:49:58 +02:00
}
/// <summary>
/// Sets the lighting for the specified <see cref="KeyboardKeys"/> to the given color.
/// Only a single key can be specified at a time.
/// </summary>
2021-05-01 20:54:17 +02:00
/// <param name="key">The key value to set the lighting for</param>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
2021-05-01 22:35:45 +02:00
public static void SetKeyboardLighting ( KeyboardKeys key , float r , float g , float b ) {
2022-01-19 11:51:59 +01:00
EnsureInitialized ( ) ;
2022-01-19 12:12:36 +01:00
foreach ( var system in systems . Values )
2022-01-19 11:51:59 +01:00
system . SetKeyboardLighting ( key , r , g , b ) ;
2021-05-01 22:35:45 +02:00
}
/// <summary>
/// Sets the lighting for all mice to the given color
/// </summary>
/// <param name="r">The color's red value, between 0 and 1</param>
/// <param name="g">The color's green value, between 0 and 1</param>
/// <param name="b">The color's blue value, between 0 and 1</param>
public static void SetMouseLighting ( float r , float g , float b ) {
2022-01-19 11:51:59 +01:00
EnsureInitialized ( ) ;
2022-01-19 12:12:36 +01:00
foreach ( var system in systems . Values )
2022-01-19 11:51:59 +01:00
system . SetMouseLighting ( r , g , b ) ;
2021-05-01 20:49:58 +02:00
}
2022-01-19 11:51:59 +01:00
private static void EnsureInitialized ( ) {
2021-05-01 21:17:05 +02:00
if ( ! Initialized )
2021-05-01 20:49:58 +02:00
throw new InvalidOperationException ( "Illumilib has not been initialized yet" ) ;
}
}
}