From 22003d5c069ce91709a847c644379b46d01a4ab4 Mon Sep 17 00:00:00 2001 From: mat1jaczyyy Date: Thu, 8 Oct 2020 13:08:44 +0200 Subject: [PATCH] Device: Properly fix handling ProjectLoaded event --- Apollo/Core/Program.cs | 5 ++--- Apollo/Devices/Fade.cs | 9 +++------ Apollo/Devices/Output.cs | 7 ++----- Apollo/Elements/Device.cs | 23 +++++++++++++++++++++++ Apollo/Elements/Project.cs | 1 - 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/Apollo/Core/Program.cs b/Apollo/Core/Program.cs index d9c39242..79473da0 100644 --- a/Apollo/Core/Program.cs +++ b/Apollo/Core/Program.cs @@ -55,8 +55,6 @@ public static void DebugLog(string text) { public delegate void ProjectLoadedEventHandler(); public static event ProjectLoadedEventHandler ProjectLoaded; - public static void ClearProjectLoaded() => ProjectLoaded = null; - static Project _project; public static Project Project { get => _project; @@ -73,8 +71,9 @@ public static Project Project { _project.WriteCrashBackup(); ProjectLoaded?.Invoke(); - ProjectLoaded = null; } + + ProjectLoaded = null; } } diff --git a/Apollo/Devices/Fade.cs b/Apollo/Devices/Fade.cs index b3af93ea..625691c8 100644 --- a/Apollo/Devices/Fade.cs +++ b/Apollo/Devices/Fade.cs @@ -280,16 +280,13 @@ public Fade(Time time = null, double gate = 1, FadePlaybackType playmode = FadeP _colors = colors?? new List() {new Color(), new Color(0)}; _positions = positions?? new List() {0, 1}; - _types = types ?? new List() {FadeType.Linear}; + _types = types?? new List() {FadeType.Linear}; Expanded = expanded; - if (Program.Project == null) Program.ProjectLoaded += Initialize; - else Initialize(); + Initialize(); } - public void Initialize() { - if (Disposed) return; - + protected override void Initialized() { Generate(); Preferences.FPSLimitChanged += Generate; diff --git a/Apollo/Devices/Output.cs b/Apollo/Devices/Output.cs index 98ba6126..b0d7bd63 100644 --- a/Apollo/Devices/Output.cs +++ b/Apollo/Devices/Output.cs @@ -66,14 +66,11 @@ public Output(int target = -1): base("output") { if (target < 0) target = Track.Get(this).ParentIndex.Value; _target = target; - if (Program.Project == null) Program.ProjectLoaded += Initialize; - else if (Program.Project.TrackOperation) Program.Project.TrackOperationFinished += Initialize; + if (Program.Project?.TrackOperation == true) Program.Project.TrackOperationFinished += Initialize; else Initialize(); } - void Initialize() { - if (Disposed) return; - + protected override void Initialized() { Program.Project.Tracks[_target].ParentIndexChanged += IndexChanged; Program.Project.Tracks[_target].Disposing += IndexRemoved; } diff --git a/Apollo/Elements/Device.cs b/Apollo/Elements/Device.cs index 7211779b..336b8105 100644 --- a/Apollo/Elements/Device.cs +++ b/Apollo/Elements/Device.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Runtime.Serialization; +using Apollo.Core; using Apollo.Helpers; using Apollo.Rendering; using Apollo.Selection; @@ -56,6 +57,28 @@ protected Device(string identifier, string name = null) { Name = name?? this.GetType().ToString().Split(".").Last(); } + bool ListeningToProjectLoaded = false; + + protected virtual void Initialized() {} + + public void Initialize() { + if (!Disposed) { + if (Program.Project == null) { + Program.ProjectLoaded += Initialize; + ListeningToProjectLoaded = true; + return; + } + + if (Track.Get(this) != null) + Initialized(); + } + + if (ListeningToProjectLoaded) { + Program.ProjectLoaded -= Initialize; + ListeningToProjectLoaded = false; + } + } + public void InvokeExit(List n) { if (!(n is StopSignal) && !n.Any()) return; diff --git a/Apollo/Elements/Project.cs b/Apollo/Elements/Project.cs index 517d8c0b..04db147f 100644 --- a/Apollo/Elements/Project.cs +++ b/Apollo/Elements/Project.cs @@ -240,7 +240,6 @@ public void Remove(int index, bool dispose = true) { public Project(int bpm = 150, int[] macros = null, List tracks = null, string author = "", long basetime = 0, long started = 0, UndoManager undo = null, string path = "") { TimeSpent.Start(); - Program.ClearProjectLoaded(); BPM = bpm; Macros = macros?? new int[4] {1, 1, 1, 1};