Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor LightingStateManager #1841

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Project-Aurora/Project-Aurora/ConfigUI.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ partial class ConfigUI : Window, INotifyPropertyChanged
private bool settingsloaded = false;
private bool shownHiddenMessage = false;

private string saved_preview_key = "";
private string saved_preview_key = null;
public string preview_key => saved_preview_key;

private Timer virtual_keyboard_timer;
private Stopwatch recording_stopwatch = new Stopwatch();
Expand All @@ -66,7 +67,9 @@ public Profiles.Application FocusedApplication
set
{
SetValue(FocusedApplicationProperty, value);
Global.LightingStateManager.PreviewProfileKey = value != null ? value.Config.ID : string.Empty;
saved_preview_key = value != null ? value.Config.ID : null;

Global.LightingStateManager.FocusedApplicationChanged(saved_preview_key);
}
}

Expand Down Expand Up @@ -334,8 +337,6 @@ private void minimizeApp()
shownHiddenMessage = true;
}

Global.LightingStateManager.PreviewProfileKey = string.Empty;

//Hide Window
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, (System.Windows.Threading.DispatcherOperationCallback)delegate (object o)
{
Expand All @@ -347,13 +348,12 @@ private void minimizeApp()

private void Window_Activated(object sender, EventArgs e)
{
Global.LightingStateManager.PreviewProfileKey = saved_preview_key;
Global.LightingStateManager.FocusedApplicationChanged(saved_preview_key);
}

private void Window_Deactivated(object sender, EventArgs e)
{
saved_preview_key = Global.LightingStateManager.PreviewProfileKey;
Global.LightingStateManager.PreviewProfileKey = string.Empty;
Global.LightingStateManager.FocusedApplicationChanged(null);
}

private Image profile_add;
Expand Down
3 changes: 2 additions & 1 deletion Project-Aurora/Project-Aurora/Controls/ColorZones.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ private void effect_settings_button_Click(object sender, RoutedEventArgs e)
if (cz_list.SelectedItem != null)
{
EffectSettingsWindow effect_settings = new EffectSettingsWindow(((ColorZone)cz_list.SelectedItem).effect_config);
effect_settings.preview_key = Global.LightingStateManager.PreviewProfileKey;

effect_settings.preview_key = ((ConfigUI)System.Windows.Application.Current.MainWindow).preview_key;
effect_settings.EffectConfigUpdated += Effect_settings_EffectConfigUpdated;

effect_settings.ShowDialog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ private void accept_button_Click(object sender, RoutedEventArgs e)

private void Window_Activated(object sender, EventArgs e)
{
Global.LightingStateManager.PreviewProfileKey = preview_key;
Global.LightingStateManager.FocusedApplicationChanged(null);
}

private void Window_Deactivated(object sender, EventArgs e)
{
Global.LightingStateManager.PreviewProfileKey = null;
Global.LightingStateManager.FocusedApplicationChanged(preview_key);
}

private void effect_angle_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
Expand Down
2 changes: 0 additions & 2 deletions Project-Aurora/Project-Aurora/Profiles/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class LightEventConfig : INotifyPropertyChanged

public LightEvent Event { get; set; }

public int? UpdateInterval { get; set; } = null;

public string IconURI { get; set; }

public HashSet<Type> ExtraAvailableLayers { get; } = new HashSet<Type>();
Expand Down
10 changes: 9 additions & 1 deletion Project-Aurora/Project-Aurora/Profiles/Desktop/Event_Desktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Event_Desktop() : base()
public override void UpdateLights(EffectFrame frame)
{
var layers = new Queue<EffectLayer>(Application.Profile.Layers.Where(l => l.Enabled).Reverse().Select(l => l.Render(_game_state)));

//Scripts before interactive and shortcut assistant layers
//ProfilesManager.DesktopProfile.UpdateEffectScripts(layers);

Expand All @@ -42,6 +42,14 @@ public override void UpdateLights(EffectFrame frame)
}

frame.AddLayers(layers.ToArray());

}
public override void UpdateOverlayLights(EffectFrame frame)
{
var overlayLayers = new Queue<EffectLayer>(Application.Profile.OverlayLayers.Where(l => l.Enabled).Reverse().Select(l => l.Render(_game_state)));

frame.AddOverlayLayers(overlayLayers.ToArray());

}

public override void SetGameState(IGameState new_game_state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public EliteDangerous()
Name = "Elite: Dangerous",
ID = "EliteDangerous",
ProcessNames = new[] { "EliteDangerous64.exe" },
UpdateInterval = 16,
SettingsType = typeof(EliteDangerousSettings),
ProfileType = typeof(EliteDangerousProfile),
OverviewControlType = typeof(Control_EliteDangerous),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,6 @@ private static SlotNode GetItem(_AllPlayer p, int slot)

private async void UpdateData(object sender, ElapsedEventArgs e)
{
if (!Global.LightingStateManager.RunningProcessMonitor.IsProcessRunning("league of legends.exe"))
{
allGameData = null;
return;
}


string jsonData = "";
try
Expand Down
6 changes: 0 additions & 6 deletions Project-Aurora/Project-Aurora/Profiles/LightEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@

namespace Aurora.Profiles
{
public enum LightEventType
{
Normal,
Underlay,
Overlay
}

public interface ILightEvent : IInit
{
Expand Down
208 changes: 208 additions & 0 deletions Project-Aurora/Project-Aurora/Profiles/LightingEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using Aurora.EffectsEngine;
using Aurora.Settings;
using Aurora.Settings.Layers;
using Aurora.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;


namespace Aurora.Profiles
{
public interface ILightingEngine
{
void ActiveProfileChanged(ILightEvent profile);
void RefreshOverLayerProfiles(List<ILightEvent> profiles);
}

public class LightingEngine : ILightingEngine, IInit
{
private ILightEvent ActiveProfile;
private List<ILightEvent> OverlayProfiles = new List<ILightEvent>();

private Timer updateTimer;

private int timerInterval = 33;

private long currentTick;

public event EventHandler PreUpdate;
public event EventHandler PostUpdate;

private List<TimedListObject> TimedLayers = new List<TimedListObject>();

public LightingEngine(ILightEvent profile)
{
ActiveProfile = profile;
}
public bool Initialized { get; private set; }

public bool Initialize()
{
if (Initialized)
return true;

this.InitUpdate();

// Listen for profile keybind triggers
Global.InputEvents.KeyDown += CheckProfileKeybinds;

Initialized = true;
return Initialized;
}

private void InitUpdate()
{
updateTimer = new System.Threading.Timer(g => {
Stopwatch watch = new Stopwatch();
watch.Start();
if (Global.isDebug)
Update();
else
{
try
{
Update();
}
catch (Exception exc)
{
Global.logger.Error("ProfilesManager.Update() Exception, " + exc);
}
}

watch.Stop();
currentTick += timerInterval + watch.ElapsedMilliseconds;
updateTimer?.Change(Math.Max(timerInterval, 0), Timeout.Infinite);
}, null, 0, System.Threading.Timeout.Infinite);
GC.KeepAlive(updateTimer);
}

private void RefreshLightningFrame()
{
EffectsEngine.EffectFrame newFrame = new EffectsEngine.EffectFrame();

if (ActiveProfile.IsEnabled)
{
ActiveProfile.UpdateLights(newFrame);
}
foreach (ILightEvent prof in OverlayProfiles)
{
if (prof.IsOverlayEnabled)
prof.UpdateOverlayLights(newFrame);
}
if (ActiveProfile.IsEnabled)
{
ActiveProfile.UpdateOverlayLights(newFrame);
}

var timedLayers = TimedLayers.ToList();
var layers = new Queue<EffectLayer>(timedLayers.Select(l => ((Layer)l.item).Render(null)));
newFrame.AddOverlayLayers(layers.ToArray());

Global.effengine.PushFrame(newFrame);
}

private void Update()
{
PreUpdate?.Invoke(this, null);

//Blackout. TODO: Cleanup this a bit. Maybe push blank effect frame to keyboard incase it has existing stuff displayed
if ((Global.Configuration.time_based_dimming_enabled &&
Utils.Time.IsCurrentTimeBetween(Global.Configuration.time_based_dimming_start_hour, Global.Configuration.time_based_dimming_start_minute, Global.Configuration.time_based_dimming_end_hour, Global.Configuration.time_based_dimming_end_minute)))
{
return;
}

Global.dev_manager.InitializeOnce();

timerInterval = 1000 / Global.Configuration.FrameRate;


RefreshLightningFrame();


PostUpdate?.Invoke(this, null);
}

public void Dispose()
{
updateTimer.Dispose();
updateTimer = null;
}

public void ActiveProfileChanged(ILightEvent profile)
{
if (Global.Configuration.ProfileChangeAnimation)
{
AddOverlayForDuration(new Layer("Profile Close Helper Layer", new GradientFillLayerHandler()
{
Properties = new GradientFillLayerHandlerProperties()
{
_FillEntireKeyboard = true,
_GradientConfig = new LayerEffectConfig(Color.FromArgb(0, 0, 0, 0), Color.FromArgb(255, 0, 0, 0)) { AnimationType = AnimationType.Translate_XY, speed = 10 }

}
}), 600);
Task.Factory.StartNew(() =>
{
Thread.Sleep(450);

ActiveProfile = profile;
AddOverlayForDuration(new Layer("Profile Open Helper Layer", new GradientFillLayerHandler()
{
Properties = new GradientFillLayerHandlerProperties()
{
_FillEntireKeyboard = true,
_GradientConfig = new LayerEffectConfig(Color.FromArgb(255, 0, 0, 0), Color.FromArgb(0, 0, 0, 0)) { AnimationType = AnimationType.Translate_XY, speed = 10 }
}
}), 900);

});
}
else
{
ActiveProfile = profile;
}
}

public void RefreshOverLayerProfiles(List<ILightEvent> profiles)
{
OverlayProfiles = profiles;
}

/// <summary>KeyDown handler that checks the current application's profiles for keybinds.
/// In the case of multiple profiles matching the keybind, it will pick the next one as specified in the Application.Profile order.</summary>
public void CheckProfileKeybinds(object sender, SharpDX.RawInput.KeyboardInputEventArgs e)
{
ILightEvent profile = ActiveProfile;

// Check profile is valid and do not switch profiles if the user is trying to enter a keybind
if (profile is Application && Controls.Control_Keybind._ActiveKeybind == null)
{

// Find all profiles that have their keybinds pressed
List<ApplicationProfile> possibleProfiles = new List<ApplicationProfile>();
foreach (var prof in (profile as Application).Profiles)
if (prof.TriggerKeybind.IsPressed())
possibleProfiles.Add(prof);

// If atleast one profile has it's key pressed
if (possibleProfiles.Count > 0)
{
// The target profile is the NEXT valid profile after the currently selected one (or the first valid one if the currently selected one doesn't share this keybind)
int trg = (possibleProfiles.IndexOf((profile as Application).Profile) + 1) % possibleProfiles.Count;
(profile as Application).SwitchToProfile(possibleProfiles[trg]);
}
}
}
public void AddOverlayForDuration(Layer overlay_event, int duration)
{
TimedLayers.Add(new TimedListObject(overlay_event, duration, TimedLayers));
}

}
}
Loading