mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-31 17:53:32 +00:00
Added LUA timer module
This commit is contained in:
parent
094b7abfc4
commit
b91ad6fcdd
@ -518,6 +518,8 @@
|
|||||||
<Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" />
|
<Compile Include="Profiles\Lua\Modules\LuaLayerModule.cs" />
|
||||||
<Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" />
|
<Compile Include="Profiles\Lua\Modules\LuaMouseModule.cs" />
|
||||||
<Compile Include="Profiles\Lua\Modules\LuaProfileModule.cs" />
|
<Compile Include="Profiles\Lua\Modules\LuaProfileModule.cs" />
|
||||||
|
<Compile Include="Profiles\Lua\Modules\Timer\LuaTimer.cs" />
|
||||||
|
<Compile Include="Profiles\Lua\Modules\LuaTimerModule.cs" />
|
||||||
<Compile Include="Profiles\Lua\Wrappers\LuaDrawWrapper.cs" />
|
<Compile Include="Profiles\Lua\Wrappers\LuaDrawWrapper.cs" />
|
||||||
<Compile Include="Profiles\Lua\Wrappers\LuaLayerWrapper.cs" />
|
<Compile Include="Profiles\Lua\Wrappers\LuaLayerWrapper.cs" />
|
||||||
<Compile Include="Profiles\ProfileModel.cs" />
|
<Compile Include="Profiles\ProfileModel.cs" />
|
||||||
|
|||||||
@ -1,160 +1,160 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using Artemis.DeviceProviders.Corsair.Utilities;
|
using Artemis.DeviceProviders.Corsair.Utilities;
|
||||||
using Artemis.Properties;
|
using Artemis.Properties;
|
||||||
using Artemis.Utilities;
|
using Artemis.Utilities;
|
||||||
using CUE.NET;
|
using CUE.NET;
|
||||||
using CUE.NET.Brushes;
|
using CUE.NET.Brushes;
|
||||||
using CUE.NET.Devices.Generic;
|
using CUE.NET.Devices.Generic;
|
||||||
using CUE.NET.Devices.Generic.Enums;
|
using CUE.NET.Devices.Generic.Enums;
|
||||||
using CUE.NET.Exceptions;
|
using CUE.NET.Exceptions;
|
||||||
using CUE.NET.Helper;
|
using CUE.NET.Helper;
|
||||||
using Ninject.Extensions.Logging;
|
using Ninject.Extensions.Logging;
|
||||||
using Point = System.Drawing.Point;
|
using Point = System.Drawing.Point;
|
||||||
|
|
||||||
namespace Artemis.DeviceProviders.Corsair
|
namespace Artemis.DeviceProviders.Corsair
|
||||||
{
|
{
|
||||||
public class CorsairKeyboard : KeyboardProvider
|
public class CorsairKeyboard : KeyboardProvider
|
||||||
{
|
{
|
||||||
private CUE.NET.Devices.Keyboard.CorsairKeyboard _keyboard;
|
private CUE.NET.Devices.Keyboard.CorsairKeyboard _keyboard;
|
||||||
private ImageBrush _keyboardBrush;
|
private ImageBrush _keyboardBrush;
|
||||||
|
|
||||||
public CorsairKeyboard(ILogger logger)
|
public CorsairKeyboard(ILogger logger)
|
||||||
{
|
{
|
||||||
Logger = logger;
|
Logger = logger;
|
||||||
Name = "Corsair RGB Keyboard";
|
Name = "Corsair RGB Keyboard";
|
||||||
CantEnableText = "Couldn't connect to your Corsair keyboard.\n" +
|
CantEnableText = "Couldn't connect to your Corsair keyboard.\n" +
|
||||||
"Please check your cables and/or drivers (could be outdated) and that Corsair Utility Engine is running.\n" +
|
"Please check your cables and/or drivers (could be outdated) and that Corsair Utility Engine is running.\n" +
|
||||||
"In CUE, make sure \"Enable SDK\" is checked under Global Settings.\n\n" +
|
"In CUE, make sure \"Enable SDK\" is checked under Global Settings.\n\n" +
|
||||||
"If needed, you can select a different keyboard in Artemis under settings.";
|
"If needed, you can select a different keyboard in Artemis under settings.";
|
||||||
}
|
}
|
||||||
|
|
||||||
public ILogger Logger { get; set; }
|
public ILogger Logger { get; set; }
|
||||||
|
|
||||||
public override bool CanEnable()
|
public override bool CanEnable()
|
||||||
{
|
{
|
||||||
return CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard);
|
return CueSDK.IsSDKAvailable(CorsairDeviceType.Keyboard);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Enables the SDK and sets updatemode to manual as well as the color of the background to black.
|
/// Enables the SDK and sets updatemode to manual as well as the color of the background to black.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public override void Enable()
|
public override void Enable()
|
||||||
{
|
{
|
||||||
if (!CueSDK.IsInitialized)
|
if (!CueSDK.IsInitialized)
|
||||||
CueSDK.Initialize();
|
CueSDK.Initialize();
|
||||||
|
|
||||||
CueSDK.UpdateMode = UpdateMode.Manual;
|
CueSDK.UpdateMode = UpdateMode.Manual;
|
||||||
_keyboard = CueSDK.KeyboardSDK;
|
_keyboard = CueSDK.KeyboardSDK;
|
||||||
switch (_keyboard.DeviceInfo.Model)
|
switch (_keyboard.DeviceInfo.Model)
|
||||||
{
|
{
|
||||||
case "K95 RGB":
|
case "K95 RGB":
|
||||||
Height = 7;
|
Height = 7;
|
||||||
Width = 25;
|
Width = 25;
|
||||||
Slug = "corsair-k95-rgb";
|
Slug = "corsair-k95-rgb";
|
||||||
PreviewSettings = new PreviewSettings(676, 190, new Thickness(0, -15, 0, 0), Resources.k95);
|
PreviewSettings = new PreviewSettings(676, 190, new Thickness(0, -15, 0, 0), Resources.k95);
|
||||||
break;
|
break;
|
||||||
case "K70 RGB":
|
case "K70 RGB":
|
||||||
case "K70 RGB RAPIDFIRE":
|
case "K70 RGB RAPIDFIRE":
|
||||||
case "K70 LUX RGB":
|
case "K70 LUX RGB":
|
||||||
Height = 7;
|
Height = 7;
|
||||||
Width = 21;
|
Width = 21;
|
||||||
Slug = "corsair-k70-rgb";
|
Slug = "corsair-k70-rgb";
|
||||||
PreviewSettings = new PreviewSettings(676, 210, new Thickness(0, -25, 0, 0), Resources.k70);
|
PreviewSettings = new PreviewSettings(676, 210, new Thickness(0, -25, 0, 0), Resources.k70);
|
||||||
break;
|
break;
|
||||||
case "K65 RGB":
|
case "K65 RGB":
|
||||||
case "CGK65 RGB":
|
case "CGK65 RGB":
|
||||||
case "K65 LUX RGB":
|
case "K65 LUX RGB":
|
||||||
case "K65 RGB RAPIDFIRE":
|
case "K65 RGB RAPIDFIRE":
|
||||||
Height = 7;
|
Height = 7;
|
||||||
Width = 18;
|
Width = 18;
|
||||||
Slug = "corsair-k65-rgb";
|
Slug = "corsair-k65-rgb";
|
||||||
PreviewSettings = new PreviewSettings(610, 240, new Thickness(0, -30, 0, 0), Resources.k65);
|
PreviewSettings = new PreviewSettings(610, 240, new Thickness(0, -30, 0, 0), Resources.k65);
|
||||||
break;
|
break;
|
||||||
case "STRAFE RGB":
|
case "STRAFE RGB":
|
||||||
Height = 7;
|
Height = 7;
|
||||||
Width = 22;
|
Width = 22;
|
||||||
Slug = "corsair-strafe-rgb";
|
Slug = "corsair-strafe-rgb";
|
||||||
PreviewSettings = new PreviewSettings(665, 215, new Thickness(0, -5, 0, 0), Resources.strafe);
|
PreviewSettings = new PreviewSettings(665, 215, new Thickness(0, -5, 0, 0), Resources.strafe);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.Debug("Corsair SDK reported device as: {0}", _keyboard.DeviceInfo.Model);
|
Logger.Debug("Corsair SDK reported device as: {0}", _keyboard.DeviceInfo.Model);
|
||||||
_keyboard.Brush = _keyboardBrush ?? (_keyboardBrush = new ImageBrush());
|
_keyboard.Brush = _keyboardBrush ?? (_keyboardBrush = new ImageBrush());
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Disable()
|
public override void Disable()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (CueSDK.IsInitialized)
|
if (CueSDK.IsInitialized)
|
||||||
CueSDK.Reinitialize();
|
CueSDK.Reinitialize();
|
||||||
}
|
}
|
||||||
catch (WrapperException e)
|
catch (WrapperException e)
|
||||||
{
|
{
|
||||||
// This occurs when releasing the SDK after sleep, ignore it
|
// This occurs when releasing the SDK after sleep, ignore it
|
||||||
if (e.Message != "The previously loaded Keyboard got disconnected.")
|
if (e.Message != "The previously loaded Keyboard got disconnected.")
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Properly resizes any size bitmap to the keyboard by creating a rectangle whose size is dependent on the bitmap
|
/// Properly resizes any size bitmap to the keyboard by creating a rectangle whose size is dependent on the bitmap
|
||||||
/// size.
|
/// size.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="bitmap"></param>
|
/// <param name="bitmap"></param>
|
||||||
public override void DrawBitmap(Bitmap bitmap)
|
public override void DrawBitmap(Bitmap bitmap)
|
||||||
{
|
{
|
||||||
using (var image = ImageUtilities.ResizeImage(bitmap, Width, Height))
|
using (var image = ImageUtilities.ResizeImage(bitmap, Width, Height))
|
||||||
{
|
{
|
||||||
// For STRAFE, stretch the image on row 2.
|
// For STRAFE, stretch the image on row 2.
|
||||||
if (_keyboard.DeviceInfo.Model == "STRAFE RGB")
|
if (_keyboard.DeviceInfo.Model == "STRAFE RGB")
|
||||||
{
|
{
|
||||||
using (var strafeBitmap = new Bitmap(22, 8))
|
using (var strafeBitmap = new Bitmap(22, 8))
|
||||||
{
|
{
|
||||||
using (var g = Graphics.FromImage(strafeBitmap))
|
using (var g = Graphics.FromImage(strafeBitmap))
|
||||||
{
|
{
|
||||||
g.DrawImage(image, new Point(0, 0));
|
g.DrawImage(image, new Point(0, 0));
|
||||||
g.DrawImage(image, new Rectangle(0, 3, 22, 7), new Rectangle(0, 2, 22, 7),
|
g.DrawImage(image, new Rectangle(0, 3, 22, 7), new Rectangle(0, 2, 22, 7),
|
||||||
GraphicsUnit.Pixel);
|
GraphicsUnit.Pixel);
|
||||||
|
|
||||||
_keyboardBrush.Image = strafeBitmap;
|
_keyboardBrush.Image = strafeBitmap;
|
||||||
_keyboard.Update();
|
_keyboard.Update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_keyboardBrush.Image = image;
|
_keyboardBrush.Image = image;
|
||||||
_keyboard.Update();
|
_keyboard.Update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override KeyMatch? GetKeyPosition(Keys keyCode)
|
public override KeyMatch? GetKeyPosition(Keys keyCode)
|
||||||
{
|
{
|
||||||
var widthMultiplier = Width/_keyboard.Brush.RenderedRectangle.Width;
|
var widthMultiplier = Width/_keyboard.Brush.RenderedRectangle.Width;
|
||||||
var heightMultiplier = Height/_keyboard.Brush.RenderedRectangle.Height;
|
var heightMultiplier = Height/_keyboard.Brush.RenderedRectangle.Height;
|
||||||
|
|
||||||
CorsairLed cueLed = null;
|
CorsairLed cueLed = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
cueLed = _keyboard.Leds.FirstOrDefault(k => k.Id.ToString() == keyCode.ToString()) ??
|
cueLed = _keyboard.Leds.FirstOrDefault(k => k.Id.ToString() == keyCode.ToString()) ??
|
||||||
_keyboard.Leds.FirstOrDefault(k => k.Id == KeyMap.FormsKeys[keyCode]);
|
_keyboard.Leds.FirstOrDefault(k => k.Id == KeyMap.FormsKeys[keyCode]);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
// ignored
|
// ignored
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cueLed == null)
|
if (cueLed == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var center = cueLed.LedRectangle.GetCenter();
|
var center = cueLed.LedRectangle.GetCenter();
|
||||||
return new KeyMatch(keyCode, (int) (center.X*widthMultiplier), (int) (center.Y*heightMultiplier));
|
return new KeyMatch(keyCode, (int) (center.X*widthMultiplier), (int) (center.Y*heightMultiplier));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -21,8 +21,8 @@ namespace Artemis.Managers
|
|||||||
private readonly DeviceManager _deviceManager;
|
private readonly DeviceManager _deviceManager;
|
||||||
private readonly IKernel _kernel;
|
private readonly IKernel _kernel;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private List<LuaModule> _luaModules;
|
|
||||||
private readonly Script _luaScript;
|
private readonly Script _luaScript;
|
||||||
|
private List<LuaModule> _luaModules;
|
||||||
private FileSystemWatcher _watcher;
|
private FileSystemWatcher _watcher;
|
||||||
|
|
||||||
public LuaManager(IKernel kernel, ILogger logger, DeviceManager deviceManager)
|
public LuaManager(IKernel kernel, ILogger logger, DeviceManager deviceManager)
|
||||||
@ -52,17 +52,15 @@ namespace Artemis.Managers
|
|||||||
|
|
||||||
// Get new instances of all modules
|
// Get new instances of all modules
|
||||||
_luaModules = _kernel.Get<List<LuaModule>>();
|
_luaModules = _kernel.Get<List<LuaModule>>();
|
||||||
ProfileModule = (LuaProfileModule)_luaModules.First(m => m.ModuleName == "Profile");
|
ProfileModule = (LuaProfileModule) _luaModules.First(m => m.ModuleName == "Profile");
|
||||||
EventsModule = (LuaEventsModule)_luaModules.First(m => m.ModuleName == "Events");
|
EventsModule = (LuaEventsModule) _luaModules.First(m => m.ModuleName == "Events");
|
||||||
|
|
||||||
// Setup new state
|
// Setup new state
|
||||||
_luaScript.Options.DebugPrint = LuaPrint;
|
_luaScript.Options.DebugPrint = LuaPrint;
|
||||||
|
|
||||||
// Insert each module into the script's globals
|
// Insert each module into the script's globals
|
||||||
foreach (var luaModule in _luaModules)
|
foreach (var luaModule in _luaModules)
|
||||||
{
|
|
||||||
_luaScript.Globals[luaModule.ModuleName] = luaModule;
|
_luaScript.Globals[luaModule.ModuleName] = luaModule;
|
||||||
}
|
|
||||||
|
|
||||||
// If there is no LUA script, don't bother executing the string
|
// If there is no LUA script, don't bother executing the string
|
||||||
if (ProfileModel.LuaScript.IsNullOrEmpty())
|
if (ProfileModel.LuaScript.IsNullOrEmpty())
|
||||||
@ -74,6 +72,7 @@ namespace Artemis.Managers
|
|||||||
{
|
{
|
||||||
lock (_luaScript)
|
lock (_luaScript)
|
||||||
{
|
{
|
||||||
|
UpdateLuaSource(ProfileModel);
|
||||||
_luaScript.DoString(ProfileModel.LuaScript);
|
_luaScript.DoString(ProfileModel.LuaScript);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +115,6 @@ namespace Artemis.Managers
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (EventsModule != null)
|
if (EventsModule != null)
|
||||||
{
|
|
||||||
lock (EventsModule.InvokeLock)
|
lock (EventsModule.InvokeLock)
|
||||||
{
|
{
|
||||||
lock (_luaScript)
|
lock (_luaScript)
|
||||||
@ -124,13 +122,51 @@ namespace Artemis.Managers
|
|||||||
_luaScript.DoString("");
|
_luaScript.DoString("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
lock (_luaScript)
|
lock (_luaScript)
|
||||||
{
|
{
|
||||||
_luaScript.DoString("");
|
_luaScript.DoString("");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Safely call a function on the active script
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="function"></param>
|
||||||
|
/// <param name="args"></param>
|
||||||
|
public void Call(DynValue function, DynValue[] args = null)
|
||||||
|
{
|
||||||
|
if (EventsModule == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
lock (EventsModule.InvokeLock)
|
||||||
|
{
|
||||||
|
lock (_luaScript)
|
||||||
|
{
|
||||||
|
if (args != null)
|
||||||
|
_luaScript.Call(function, args);
|
||||||
|
else
|
||||||
|
_luaScript.Call(function);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (ArgumentException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.Message);
|
||||||
|
}
|
||||||
|
catch (InternalErrorException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage);
|
||||||
|
}
|
||||||
|
catch (SyntaxErrorException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage);
|
||||||
|
}
|
||||||
|
catch (ScriptRuntimeException e)
|
||||||
|
{
|
||||||
|
_logger.Error("[{0}-LUA]: Error: {1}", ProfileModel.Name, e.DecoratedMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,6 +179,20 @@ namespace Artemis.Managers
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates a profile's LUA script to be compatible with the latest version of Artemis, if needed.
|
||||||
|
/// This function obviously won't fix completely custom profiles but it'll fix copied LUA.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="profileModel"></param>
|
||||||
|
private static void UpdateLuaSource(ProfileModel profileModel)
|
||||||
|
{
|
||||||
|
// 1.7.1.0 - Events cleanup
|
||||||
|
profileModel.LuaScript = profileModel.LuaScript.Replace("function updateHandler(profile, eventArgs)",
|
||||||
|
"function updateHandler(eventArgs)");
|
||||||
|
profileModel.LuaScript = profileModel.LuaScript.Replace("function drawHandler(profile, eventArgs)",
|
||||||
|
"function drawHandler(eventArgs)");
|
||||||
|
}
|
||||||
|
|
||||||
#region Editor
|
#region Editor
|
||||||
|
|
||||||
public void OpenEditor()
|
public void OpenEditor()
|
||||||
|
|||||||
43
Artemis/Artemis/Profiles/Lua/Modules/LuaTimerModule.cs
Normal file
43
Artemis/Artemis/Profiles/Lua/Modules/LuaTimerModule.cs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using Artemis.Managers;
|
||||||
|
using Artemis.Profiles.Lua.Modules.Timer;
|
||||||
|
using MoonSharp.Interpreter;
|
||||||
|
|
||||||
|
namespace Artemis.Profiles.Lua.Modules
|
||||||
|
{
|
||||||
|
[MoonSharpUserData]
|
||||||
|
public class LuaTimerModule : LuaModule
|
||||||
|
{
|
||||||
|
private readonly List<LuaTimer> _timers;
|
||||||
|
|
||||||
|
public LuaTimerModule(LuaManager luaManager) : base(luaManager)
|
||||||
|
{
|
||||||
|
_timers = new List<LuaTimer>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ModuleName => "Timer";
|
||||||
|
|
||||||
|
public override void Dispose()
|
||||||
|
{
|
||||||
|
foreach (var luaTimer in _timers)
|
||||||
|
luaTimer.Dispose();
|
||||||
|
|
||||||
|
_timers.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LuaTimer SetTimer(DynValue function, int interval, int timesToExecute, params DynValue[] args)
|
||||||
|
{
|
||||||
|
var luaTimer = new LuaTimer(this, function, interval, timesToExecute, args);
|
||||||
|
_timers.Add(luaTimer);
|
||||||
|
return luaTimer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveTimer(LuaTimer luaTimer)
|
||||||
|
{
|
||||||
|
if (_timers.Contains(luaTimer))
|
||||||
|
_timers.Remove(luaTimer);
|
||||||
|
|
||||||
|
luaTimer.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
59
Artemis/Artemis/Profiles/Lua/Modules/Timer/LuaTimer.cs
Normal file
59
Artemis/Artemis/Profiles/Lua/Modules/Timer/LuaTimer.cs
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Timers;
|
||||||
|
using MoonSharp.Interpreter;
|
||||||
|
|
||||||
|
namespace Artemis.Profiles.Lua.Modules.Timer
|
||||||
|
{
|
||||||
|
[MoonSharpUserData]
|
||||||
|
public class LuaTimer : IDisposable
|
||||||
|
{
|
||||||
|
private readonly DynValue[] _args;
|
||||||
|
private readonly DynValue _function;
|
||||||
|
private readonly System.Timers.Timer _timer;
|
||||||
|
private readonly LuaTimerModule _timerModule;
|
||||||
|
private readonly int _timesToExecute;
|
||||||
|
private int _timesExecuted;
|
||||||
|
|
||||||
|
public LuaTimer(LuaTimerModule timerModule, DynValue function, int interval, int timesToExecute,
|
||||||
|
params DynValue[] args)
|
||||||
|
{
|
||||||
|
_timerModule = timerModule;
|
||||||
|
_function = function;
|
||||||
|
_timesToExecute = timesToExecute;
|
||||||
|
_args = args;
|
||||||
|
_timesExecuted = 0;
|
||||||
|
|
||||||
|
// Setup timer
|
||||||
|
_timer = new System.Timers.Timer(interval);
|
||||||
|
_timer.Elapsed += TimerOnElapsed;
|
||||||
|
_timer.Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_timer?.Stop();
|
||||||
|
_timer?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Stop()
|
||||||
|
{
|
||||||
|
_timerModule.RemoveTimer(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TimerOnElapsed(object sender, ElapsedEventArgs e)
|
||||||
|
{
|
||||||
|
if (_args != null)
|
||||||
|
_timerModule.LuaManager.Call(_function, _args);
|
||||||
|
else
|
||||||
|
_timerModule.LuaManager.Call(_function);
|
||||||
|
|
||||||
|
// Don't keep track of execution if times is set to 0 (infinite)
|
||||||
|
if (_timesToExecute <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_timesExecuted++;
|
||||||
|
if (_timesExecuted >= _timesToExecute)
|
||||||
|
_timerModule.RemoveTimer(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -14,7 +14,7 @@
|
|||||||
-- changes are applied to the profile and the script is restarted.
|
-- changes are applied to the profile and the script is restarted.
|
||||||
|
|
||||||
-- This event is raised after every profile update, before drawing.
|
-- This event is raised after every profile update, before drawing.
|
||||||
function updateHandler(profile, eventArgs)
|
function updateHandler(eventArgs)
|
||||||
-- In this example we only want to update once per frame when the keyboard is
|
-- In this example we only want to update once per frame when the keyboard is
|
||||||
-- updated. If you don't do this the updateHandler will trigger on every
|
-- updated. If you don't do this the updateHandler will trigger on every
|
||||||
-- device's update.
|
-- device's update.
|
||||||
@ -26,7 +26,7 @@ function updateHandler(profile, eventArgs)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- This event is raised after every profile draw, after updating.
|
-- This event is raised after every profile draw, after updating.
|
||||||
function drawHandler(profile, eventArgs)
|
function drawHandler(eventArgs)
|
||||||
-- In this example we only want to draw to the keyboard. Each device has it's
|
-- In this example we only want to draw to the keyboard. Each device has it's
|
||||||
-- own drawing event
|
-- own drawing event
|
||||||
if eventArgs.DeviceType != "keyboard" then
|
if eventArgs.DeviceType != "keyboard" then
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user