Added a layer of abstraction between RGB.NET and Artemis

This means Artemis can add extra functionality
It also avoid having to reference RGB.NET for basic rendering
Implemented rendering scaling
This commit is contained in:
SpoinkyNL committed 2019-10-30 23:20:06 +01:00
1 parent 4126f5622e
commit f72f145c7e
41 files changed
+725 -494

No files matched your search

@@ -1,11 +1,14 @@
using System.Threading.Tasks;
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Artemis.Storage.Entities;
using Artemis.Storage.Repositories.Interfaces;
using Newtonsoft.Json;
using Stylet;
namespace Artemis.Core.Plugins.Models
{
public class PluginSetting<T>
public class PluginSetting<T> : PropertyChangedBase
{
// ReSharper disable once NotAccessedField.Local
private readonly PluginInfo _pluginInfo;
@@ -20,6 +23,15 @@ namespace Artemis.Core.Plugins.Models
Name = pluginSettingEntity.Name;
Value = JsonConvert.DeserializeObject<T>(pluginSettingEntity.Value);
// PropertyChanged is for bindings, but we can use it here to create a easy to use SettingsChanged event
PropertyChanged += OnPropertyChanged;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Value))
OnSettingChanged();
}
/// <summary>
@@ -69,5 +81,12 @@ namespace Artemis.Core.Plugins.Models
_pluginSettingEntity.Value = JsonConvert.SerializeObject(Value);
await _pluginSettingRepository.SaveAsync();
}
public event EventHandler<EventArgs> SettingChanged;
protected virtual void OnSettingChanged()
{
SettingChanged?.Invoke(this, EventArgs.Empty);
}
}
}
@@ -6,6 +6,10 @@ using Newtonsoft.Json;
namespace Artemis.Core.Plugins.Models
{
/// <summary>
/// <para>This contains all the settings for your plugin. To access a setting use <see cref="GetSetting{T}" />.</para>
/// <para>To use this class, inject it into the constructor of your plugin.</para>
/// </summary>
public class PluginSettings
{
private readonly PluginInfo _pluginInfo;
@@ -19,17 +23,27 @@ namespace Artemis.Core.Plugins.Models
_settingEntities = pluginSettingRepository.GetByPluginGuid(_pluginInfo.Guid).ToDictionary(se => se.Name);
}
/// <summary>
/// Gets the setting with the provided name. If the setting does not exist yet, it is created.
/// </summary>
/// <typeparam name="T">The type of the setting, can be any serializable type</typeparam>
/// <param name="name">The name of the setting</param>
/// <param name="defaultValue">The default value to use if the setting does not exist yet</param>
/// <returns></returns>
public PluginSetting<T> GetSetting<T>(string name, T defaultValue = default(T))
{
if (_settingEntities.ContainsKey(name))
lock (_settingEntities)
{
if (_settingEntities.ContainsKey(name))
return new PluginSetting<T>(_pluginInfo, _pluginSettingRepository, _settingEntities[name]);
var settingEntity = new PluginSettingEntity {Name = name, PluginGuid = _pluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)};
_pluginSettingRepository.Add(settingEntity);
_pluginSettingRepository.Save();
_settingEntities.Add(name, settingEntity);
return new PluginSetting<T>(_pluginInfo, _pluginSettingRepository, _settingEntities[name]);
var settingEntity = new PluginSettingEntity {Name = name, PluginGuid = _pluginInfo.Guid, Value = JsonConvert.SerializeObject(defaultValue)};
_pluginSettingRepository.Add(settingEntity);
_pluginSettingRepository.Save();
_settingEntities.Add(name, settingEntity);
return new PluginSetting<T>(_pluginInfo, _pluginSettingRepository, _settingEntities[name]);
}
}
}
}