-
Notifications
You must be signed in to change notification settings - Fork 13
/
PatternManager.cs
115 lines (101 loc) · 3.61 KB
/
PatternManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using UnityEngine;
using Nes3D.Utils;
using System.Threading;
namespace Nes3D.Engine3D
{
public static class PatternManager
{
public static KeyedCollectionEx<RawTile2D, Tile2D> tiles = new KeyedCollectionEx<RawTile2D, Tile2D>(delegate (Tile2D tile) { return tile.rawData; });
public static PatternCollection patterns;
public static PatternCollection tempPatterns;
public static void Init()
{
patterns = new PatternCollection(new PatternEquality());
tempPatterns = new PatternCollection(new PatternEquality());
SettingManager.Ins.PropertyChanged += OnSettingChanged;
}
static private void OnSettingChanged(object sender, PropertyChangedEventArgs args)
{
if (ReferenceEquals(args.PropertyName, SettingManager.RenderModeId))
{
if (patterns.Count > 0)
Misc.RunInMainThread(() =>
{
Debug.Log("Change Render Mode To " + SettingManager.Ins.RenderMode);
foreach (Pattern p in patterns)
p.Release();
foreach (Pattern p in tempPatterns)
p.Release();
GC.Collect();
Thread.Sleep(100);
foreach (Pattern p in patterns)
p.Build();
foreach (Pattern p in tempPatterns)
p.Build();
});
}
}
public static void Reset()
{
foreach (var p in patterns)
p.Release();
foreach (Pattern p in tempPatterns)
p.Release();
patterns.Clear();
tempPatterns.Clear();
tiles.Clear();
}
//local variable
private static List<Pattern> temp = new List<Pattern>();
public static void Add(Pattern pattern)
{
Pattern tempPattern;
temp.Clear();
if (pattern.permanent)
{
if (patterns.TryGetValue(pattern, out tempPattern))
Remove(tempPattern);
foreach (Pattern s in tempPatterns)
if (pattern.tiles2D.Contains(s.tiles2D))
temp.Add(s);
foreach (Pattern s in temp)
Remove(s);
patterns.Add(pattern);
}
else
{
foreach (Pattern s in tempPatterns)
if (s.tiles2D.Contains(pattern.tiles2D))
temp.Add(s);
foreach (Pattern s in temp)
Remove(s);
tempPatterns.Add(pattern);
}
}
public static void Remove(Pattern p)
{
if (p.permanent)
{
if (patterns.Contains(p))
{
foreach (Tile2D tile in p.tiles2D)
tile.patternSet.Remove(p);
patterns.Remove(p);
}
}
else
{
if (tempPatterns.Contains(p))
{
foreach (Tile2D tile in p.tiles2D)
tile.tempPatternSet.Remove(p);
tempPatterns.Remove(p);
}
}
}
}
}