-
Notifications
You must be signed in to change notification settings - Fork 4
/
DeckState.cs
155 lines (146 loc) · 4.65 KB
/
DeckState.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CropperDeck {
public class DeckState {
public static string DirectoryName = "config";
public static string GetPath(string name) {
return $"{DirectoryName}/{name}.json";
}
public string resourceType = "YAL's Deck";
public int resourceVersion = 101;
public string Name;
public int Width = 1024, Height = 600;
public List<ColumnData> Columns = new List<ColumnData>();
public List<CropMargins> Margins = new List<CropMargins>();
public List<CropMargins> HiddenMargins = new List<CropMargins>();
public DeckColors CustomColors = new DeckColors();
public DeckRestoreMode RestoreMode = DeckRestoreMode.Ask;
public DeckState() {
}
public void InitMissing() {
if (CustomColors == null) CustomColors = new DeckColors();
}
public void Acquire(MainForm form) {
Name = form.DeckName;
Width = form.Width;
Height = form.Height;
CustomColors = form.CustomColors;
RestoreMode = form.RestoreMode;
var saveTitles = RestoreMode != DeckRestoreMode.Never;
foreach (var m in form.CropMargins) Margins.Add(m);
foreach (var col in form.GetDeckColumns()) {
var cm = col.CropMargins;
var cmName = cm.Name;
if (!MarginsContainName(Margins, cmName)
&& !MarginsContainName(CropMargins.Special, cmName)
&& !MarginsContainName(HiddenMargins, cmName)
) {
HiddenMargins.Add(cm);
}
var cd = new ColumnData();
cd.Name = col.ColumnName;
cd.Icon = col.IconName;
cd.CropStyle = cmName;
if (saveTitles) cd.LastTitle = col.Window != null ? col.Window.Title : col.LastTitle;
if (col.TfWidth.Text != "") {
cd.Width = col.Width;
} else cd.Width = null;
Columns.Add(cd);
}
// OK!
}
public void Apply(MainForm form) {
form.ResizeAndCenter(Width, Height);
form.PanCtr.Controls.Clear();
form.CropMargins = Margins.Count == 0 ? CropMargins.Default.ToList() : Margins.ToList();
form.RestoreMode = RestoreMode;
form.PanCtr.SuspendLayout();
foreach (var colState in Columns) {
var col = new DeckColumn(form);
CropMargins cm;
do {
cm = Margins.Where(m => m.Name == colState.CropStyle).FirstOrDefault();
if (cm.Name != null) break;
cm = CropMargins.Special.Where(m => m.Name == colState.CropStyle).FirstOrDefault();
if (cm.Name != null) break;
cm = HiddenMargins.Where(m => m.Name == colState.CropStyle).FirstOrDefault();
if (cm.Name != null) break;
cm = CropMargins.Zero;
} while (false);
col.CropMargins = cm;
col.TfName.Text = col.TlName.Text = col.ColumnName = colState.Name;
if (colState.Icon != "") {
col.IconName = colState.Icon;
if (File.Exists(col.IconPath)) {
col.TbConfig.Image = DeckColumn.LoadImage(col.IconPath);
}
}
if (colState.Width.HasValue) {
col.Width = colState.Width.Value;
col.TfWidth.Text = col.Width.ToString();
}
col.LastTitle = colState.LastTitle;
form.PanCtr.Controls.Add(col);
form.PanCtr.Controls.SetChildIndex(col, 0);
}
form.PanCtr.ResumeLayout();
form.CustomColors = CustomColors;
form.RebuildQuickAccess();
form.SyncCustomColors();
}
public void Save() {
if (!Directory.Exists(DirectoryName)) Directory.CreateDirectory(DirectoryName);
var text = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(GetPath(Name), text, Encoding.UTF8);
}
static bool MarginsContainName(List<CropMargins> list, string cmName) {
return list.Any(m => m.Name == cmName);
}
static DeckState CreateDefault(string name) {
var config = new DeckState();
config.Name = name;
foreach (var m in CropMargins.Default) config.Margins.Add(m);
config.Columns.Add(new ColumnData() {
Name = "Example",
Width = 300,
CropStyle = CropMargins.Zero.Name,
});
return config;
}
public static DeckState Load(string name) {
var path = GetPath(name);
if (File.Exists(path)) {
try {
var text = File.ReadAllText(path);
var state = JsonConvert.DeserializeObject<DeckState>(text);
state.InitMissing();
return state;
} catch (Exception e) {
MessageBox.Show(e.ToString(), "Error loading configuration!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
return CreateDefault(name);
}
}
public enum DeckRestoreMode {
Ask = 0,
Always = 1,
Never = 2,
}
public class ColumnData {
public string Name;
public string Icon;
public string CropStyle;
public int? Width;
public string LastTitle;
public ColumnData() { }
}
}