Skip to content

Commit

Permalink
Device: Properly fix handling ProjectLoaded event
Browse files Browse the repository at this point in the history
  • Loading branch information
mat1jaczyyy committed Oct 8, 2020
1 parent a95d311 commit 22003d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
5 changes: 2 additions & 3 deletions Apollo/Core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -73,8 +71,9 @@ public static Project Project {
_project.WriteCrashBackup();

ProjectLoaded?.Invoke();
ProjectLoaded = null;
}

ProjectLoaded = null;
}
}

Expand Down
9 changes: 3 additions & 6 deletions Apollo/Devices/Fade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,16 +280,13 @@ public Fade(Time time = null, double gate = 1, FadePlaybackType playmode = FadeP

_colors = colors?? new List<Color>() {new Color(), new Color(0)};
_positions = positions?? new List<double>() {0, 1};
_types = types ?? new List<FadeType>() {FadeType.Linear};
_types = types?? new List<FadeType>() {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;
Expand Down
7 changes: 2 additions & 5 deletions Apollo/Devices/Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions Apollo/Elements/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Runtime.Serialization;

using Apollo.Core;
using Apollo.Helpers;
using Apollo.Rendering;
using Apollo.Selection;
Expand Down Expand Up @@ -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<Signal> n) {
if (!(n is StopSignal) && !n.Any()) return;

Expand Down
1 change: 0 additions & 1 deletion Apollo/Elements/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ public void Remove(int index, bool dispose = true) {

public Project(int bpm = 150, int[] macros = null, List<Track> 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};
Expand Down

0 comments on commit 22003d5

Please sign in to comment.