Repository navigation
Expand file tree
/
Copy pathRecentColours.cs
More file actions
66 lines (54 loc) · 1.83 KB
/
Copy pathRecentColours.cs
File metadata and controls
66 lines (54 loc) · 1.83 KB
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
// RecentColours.cs
// Copyright Karel Kroeze, 2018-2018
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Verse;
namespace ColourPicker {
public class RecentColours {
private const int max = 20;
private static List<Color> _colors = new List<Color>();
static RecentColours() {
Read();
}
public Color this[int index] => _colors[index];
public int Count => _colors.Count;
public void Add(Color color) {
_colors.RemoveAll(c => c == color);
_colors.Insert(0, color);
while (_colors.Count > max) {
_colors.RemoveAt(_colors.Count - 1);
}
Write();
}
private static void Read() {
string path = Path.Combine(GenFilePaths.ConfigFolderPath, "ColourPicker.xml");
if (!File.Exists(path)) {
return;
}
try {
Scribe.loader.InitLoading(path);
ExposeData();
} catch (Exception ex) {
Log.Error("ColourPicker :: Error loading recent colours from file:" + ex);
} finally {
Scribe.loader.FinalizeLoading();
}
}
private static void Write() {
try {
string path = Path.Combine( GenFilePaths.ConfigFolderPath, "ColourPicker.xml" );
Scribe.saver.InitSaving(path, "ColourPicker");
ExposeData();
} catch (Exception ex) {
Log.Error("ColourPicker :: Error saving recent colours to file:" + ex);
} finally {
Scribe.saver.FinalizeSaving();
}
}
private static void ExposeData() {
Scribe_Collections.Look(ref _colors, "RecentColors");
}
}
}