This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProfileSelector.cs
More file actions
302 lines (266 loc) · 12.5 KB
/
ProfileSelector.cs
File metadata and controls
302 lines (266 loc) · 12.5 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AstroModLoader
{
public partial class ProfileSelector : Form
{
private Form1 OurParentForm;
public ModProfile SelectedProfile = null;
public ProfileSelector()
{
InitializeComponent();
this.statusLabel.Text = "";
this.listBox1.DrawItem += ListBox1_DrawItem;
}
private int GetMaxListBoxEntryHeight(Font font)
{
int maxHeight = -1;
foreach (KeyValuePair<string, ModProfile> entry in listBox1.Items)
{
int testingHeight = TextRenderer.MeasureText(entry.Key, font).Height;
if (testingHeight > maxHeight) maxHeight = testingHeight;
}
return maxHeight;
}
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) return;
string decidedText = ((KeyValuePair<string, ModProfile>)listBox1.Items[e.Index]).Key;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State ^ DrawItemState.Selected, AMLPalette.ForeColor, AMLPalette.HighlightColor);
}
Rectangle decidedNewBounds = new Rectangle(e.Bounds.Location.X + (AMLPalette.BorderPenWidth / 2), e.Bounds.Location.Y + (AMLPalette.BorderPenWidth / 2), e.Bounds.Width - AMLPalette.BorderPenWidth, e.Bounds.Height);
e.Graphics.FillRectangle(new SolidBrush(e.BackColor), decidedNewBounds);
e.Graphics.DrawString(decidedText, e.Font, new SolidBrush(AMLPalette.ForeColor), decidedNewBounds, StringFormat.GenericDefault);
if ((e.State & DrawItemState.Focus) == DrawItemState.Focus && (e.State & DrawItemState.NoFocusRect) != DrawItemState.NoFocusRect)
{
ControlPaint.DrawFocusRectangle(e.Graphics, decidedNewBounds, e.ForeColor, e.BackColor);
}
}
private void RefreshTheme()
{
this.listBox1.Select();
}
private void RefreshBox()
{
string currentlySelected = listBox1.SelectedValue as string;
if (OurParentForm == null) return;
if (OurParentForm.ModManager.ProfileList == null) OurParentForm.ModManager.ProfileList = new Dictionary<string, ModProfile>();
if (OurParentForm.ModManager.ProfileList.Count > 0)
{
listBox1.DisplayMember = "Key";
listBox1.ValueMember = "Key";
listBox1.DataSource = new BindingSource(OurParentForm.ModManager.ProfileList, null);
}
else
{
listBox1.DataSource = null;
}
double desiredItemHeight = GetMaxListBoxEntryHeight(listBox1.Font);
if (desiredItemHeight > 5)
{
int desiredFullHeight = (int)(Math.Ceiling(listBox1.Height / desiredItemHeight) * desiredItemHeight);
int maxEntries = listBox1.Height / (int)desiredItemHeight;
if (maxEntries <= OurParentForm.ModManager.ProfileList.Count)
{
desiredFullHeight = (int)desiredItemHeight * (OurParentForm.ModManager.ProfileList.Count + 1);
}
listBox1.Height = desiredFullHeight;
listBox1.ItemHeight = (int)desiredItemHeight;
statusLabel.Location = new Point(statusLabel.Location.X, listBox1.Location.Y + listBox1.Height + (int)desiredItemHeight);
this.Height = statusLabel.Location.Y + ((int)desiredItemHeight * 7);
}
this.RefreshTheme();
this.listBox1.Refresh();
for (int i = 0; i < this.listBox1.Items.Count; i++)
{
object thisItem = this.listBox1.Items[i];
if (thisItem is KeyValuePair<string, ModProfile> thisKVP)
{
if (thisKVP.Key == currentlySelected)
{
this.listBox1.SelectedIndex = i;
break;
}
}
}
}
private void ProfileSelector_Load(object sender, EventArgs e)
{
if (this.Owner is Form1 parentForm)
{
OurParentForm = this.Owner as Form1;
this.Text = OurParentForm.Text;
RefreshBox();
}
this.RefreshTheme();
AMLPalette.RefreshTheme(this);
this.AdjustFormPosition();
}
private void ForceRefreshSelectedProfile()
{
string kosherKey = listBox1.SelectedValue as string;
if (string.IsNullOrEmpty(kosherKey) || !OurParentForm.ModManager.ProfileList.ContainsKey(kosherKey))
{
SelectedProfile = null;
return;
}
SelectedProfile = OurParentForm.ModManager.ProfileList[kosherKey];
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
statusLabel.Text = "";
ForceRefreshSelectedProfile();
}
private async Task ForceLoadSelectedProfile()
{
if (SelectedProfile == null)
{
this.ShowBasicButton("Please select a profile to load it.", "OK", null, null);
return;
}
OurParentForm.ModManager.ApplyProfile(SelectedProfile);
await OurParentForm.ModManager.FullUpdate();
OurParentForm.FullRefresh();
statusLabel.Text = "Successfully loaded from profile.";
ForceRefreshSelectedProfile();
}
private async void listBox1_DoubleClick(object sender, EventArgs e)
{
await ForceLoadSelectedProfile();
}
private async void okButton_Click(object sender, EventArgs e)
{
await ForceLoadSelectedProfile();
}
private void cancelButton_Click(object sender, EventArgs e)
{
SelectedProfile = null;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void newProfileButton_Click(object sender, EventArgs e)
{
if (OurParentForm == null) return;
TextPrompt profileNamePrompt = new TextPrompt();
profileNamePrompt.StartPosition = FormStartPosition.Manual;
profileNamePrompt.Location = new Point((this.Location.X + this.Width / 2) - (profileNamePrompt.Width / 2), (this.Location.Y + this.Height / 2) - (profileNamePrompt.Height / 2));
profileNamePrompt.Text = OurParentForm.Text;
profileNamePrompt.AllowBrowse = false;
profileNamePrompt.Width -= 100;
profileNamePrompt.DisplayText = "Name this profile:";
if (profileNamePrompt.ShowDialog(this) == DialogResult.OK)
{
if (string.IsNullOrEmpty(profileNamePrompt.OutputText)) return;
if (OurParentForm.ModManager.ProfileList.ContainsKey(profileNamePrompt.OutputText))
{
int dialogResult = this.ShowBasicButton("Do you want to overwrite this profile?", "Yes", "No", null);
if (dialogResult == 0)
{
OurParentForm.ModManager.ProfileList[profileNamePrompt.OutputText] = OurParentForm.ModManager.GenerateProfile();
OurParentForm.ModManager.SyncConfigToDisk();
RefreshBox();
statusLabel.Text = "Successfully overwrote profile.";
}
else
{
statusLabel.Text = "";
}
return;
}
OurParentForm.ModManager.ProfileList.Add(profileNamePrompt.OutputText, OurParentForm.ModManager.GenerateProfile());
OurParentForm.ModManager.SyncConfigToDisk();
RefreshBox();
statusLabel.Text = "Successfully created profile.";
}
}
private void ForceDeleteProfilePrompt()
{
if (listBox1.SelectedValue == null)
{
this.ShowBasicButton("Please select a profile to delete it.", "OK", null, null);
return;
}
int dialogResult = this.ShowBasicButton("Are you sure you want to delete this profile?", "Yes", "No", null);
if (dialogResult == 0)
{
OurParentForm.ModManager.ProfileList.Remove(listBox1.SelectedValue as string);
OurParentForm.ModManager.SyncConfigToDisk();
RefreshBox();
statusLabel.Text = "Successfully deleted profile.";
}
ForceRefreshSelectedProfile();
}
private void ForceExportProfile()
{
if (listBox1.SelectedValue == null || SelectedProfile == null)
{
this.ShowBasicButton("Please select a profile to export it as a .zip file.", "OK", null, null);
return;
}
var dialog = new SaveFileDialog();
dialog.Filter = "ZIP files (*.zip)|*.zip|All files (*.*)|*.*";
dialog.Title = "Export a profile";
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
string targetFolderPath = Path.Combine(Path.GetTempPath(), "AstroModLoader", "export");
Directory.CreateDirectory(targetFolderPath);
ModProfile creatingProfile = new ModProfile();
creatingProfile.ProfileData = new Dictionary<string, Mod>();
creatingProfile.Name = listBox1.SelectedValue as string;
creatingProfile.Info = "Exported by " + AMLUtils.UserAgent + " at " + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffK");
List<KeyValuePair<string, Mod>> plannedOrdering = new List<KeyValuePair<string, Mod>>();
foreach (KeyValuePair<string, Mod> entry in SelectedProfile.ProfileData)
{
if (entry.Value.Enabled) plannedOrdering.Add(entry);
}
plannedOrdering = new List<KeyValuePair<string, Mod>>(plannedOrdering.OrderBy(o => o.Value.Priority).ToList());
for (int i = 0; i < plannedOrdering.Count; i++)
{
plannedOrdering[i].Value.Priority = i + 1;
creatingProfile.ProfileData[plannedOrdering[i].Key] = plannedOrdering[i].Value;
// Copy mod pak to the zip as well
string onePathOnDisk = OurParentForm.ModManager.GetPathOnDisk(plannedOrdering[i].Value, plannedOrdering[i].Key);
if (!string.IsNullOrEmpty(onePathOnDisk)) File.Copy(onePathOnDisk, Path.Combine(targetFolderPath, Path.GetFileName(onePathOnDisk)));
}
File.WriteAllBytes(Path.Combine(targetFolderPath, "profile1.json"), Encoding.UTF8.GetBytes(AMLUtils.SerializeObject(creatingProfile)));
ZipFile.CreateFromDirectory(targetFolderPath, dialog.FileName);
Directory.Delete(targetFolderPath, true);
RefreshBox();
statusLabel.Text = "Successfully exported profile.";
}
}
private async void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete) ForceDeleteProfilePrompt();
if (e.KeyCode == Keys.Enter) await ForceLoadSelectedProfile();
if (e.KeyCode == Keys.X) ForceExportProfile();
}
private void deleteProfileButton_Click(object sender, EventArgs e)
{
ForceDeleteProfilePrompt();
}
private void saveButton_Click(object sender, EventArgs e)
{
if (SelectedProfile == null)
{
this.ShowBasicButton("Please select a profile to save to it.", "OK", null, null);
return;
}
OurParentForm.ModManager.ProfileList[listBox1.SelectedValue as string] = OurParentForm.ModManager.GenerateProfile();
OurParentForm.ModManager.SyncConfigToDisk();
RefreshBox();
statusLabel.Text = "Successfully saved to profile.";
ForceRefreshSelectedProfile();
}
}
}