-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
185 lines (159 loc) · 5.42 KB
/
MainForm.cs
File metadata and controls
185 lines (159 loc) · 5.42 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
using System.Drawing;
using System.Windows.Forms;
using Krassheiten.SystemGameManager.Controller;
using Krassheiten.SystemGameManager.Service;
using Krassheiten.SystemGameManager.View;
using System.Text.Json;
namespace Krassheiten.SystemGameManager;
public class MainForm : Form
{
private readonly Button btnLoadInfo;
private readonly Label statusLabel;
private readonly GameViewService gameViewService;
private readonly PcInfoView pcInfoView;
private readonly GameInfoView gameInfoView;
private readonly GameAudioView gameAudioView;
private GameAudioController? gameAudioController;
public MainForm()
{
Text = $"System & Game Manager (v{GetVersionFromReleases()})";
StartPosition = FormStartPosition.CenterScreen;
MinimumSize = new Size(980, 640);
Width = 1180;
Height = 760;
BackColor = Color.FromArgb(245, 247, 250);
DoubleBuffered = true;
gameViewService = new GameViewService();
pcInfoView = new PcInfoView();
gameInfoView = new GameInfoView(gameViewService.Artwork, OpenGameDirectory);
gameAudioView = new GameAudioView();
var toolbar = new Panel()
{
Dock = DockStyle.Top,
Height = 60,
Padding = new Padding(12),
BackColor = Color.White
};
btnLoadInfo = CreatePrimaryButton("Infos laden", 125);
btnLoadInfo.Dock = DockStyle.Left;
statusLabel = new Label()
{
Text = "Bereit",
Dock = DockStyle.Fill,
Padding = new Padding(14, 7, 0, 0),
TextAlign = ContentAlignment.MiddleLeft,
ForeColor = Color.FromArgb(55, 65, 81)
};
var tabs = new TabControl()
{
Dock = DockStyle.Fill,
Padding = new Point(18, 8)
};
tabs.TabPages.Add(pcInfoView.CreateTab());
tabs.TabPages.Add(gameInfoView.CreateTab());
tabs.TabPages.Add(gameAudioView.CreateTab());
btnLoadInfo.Click += BtnLoadInfo_Click;
Shown += async (_, _) => await LoadInfoAsync();
toolbar.Controls.Add(statusLabel);
toolbar.Controls.Add(btnLoadInfo);
Controls.Add(tabs);
Controls.Add(toolbar);
pcInfoView.ShowLoadingState();
gameInfoView.ShowLoadingState();
gameAudioView.ShowLoadingState();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
gameAudioController?.Dispose();
gameViewService.Dispose();
}
base.Dispose(disposing);
}
private async void BtnLoadInfo_Click(object? sender, EventArgs e)
{
await LoadInfoAsync();
}
private async Task LoadInfoAsync()
{
btnLoadInfo.Enabled = false;
statusLabel.Text = "Lade Informationen...";
pcInfoView.ShowLoadingState();
gameInfoView.ShowLoadingState();
gameAudioView.ShowLoadingState();
try
{
var viewData = await Task.Run(BuildViewData);
pcInfoView.ShowSystemText(viewData.SystemText);
gameInfoView.Populate(viewData.GameManager);
gameAudioView.RefreshGames();
gameAudioController ??= new GameAudioController();
statusLabel.Text = "Informationen geladen.";
}
catch (Exception ex)
{
pcInfoView.ShowError(ex.Message);
gameInfoView.ShowErrorState(ex.Message);
gameAudioView.ShowErrorState(ex.Message);
statusLabel.Text = "Fehler beim Laden.";
}
finally
{
btnLoadInfo.Enabled = true;
}
}
private void OpenGameDirectory(string path)
{
if (!gameViewService.TryOpenDirectory(path, out var errorMessage))
{
MessageBox.Show(errorMessage, "Hinweis", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private MainViewData BuildViewData()
{
var pcInfo = new PcInfoController();
_ = new GameInfoController();
return new MainViewData(
pcInfoView.BuildSystemText(pcInfo),
gameViewService.BuildViewData());
}
private static Button CreatePrimaryButton(string text, int width)
{
var button = new Button()
{
Text = text,
Width = width,
Height = 34,
FlatStyle = FlatStyle.Flat,
BackColor = Color.FromArgb(79, 70, 229),
ForeColor = Color.White,
Cursor = Cursors.Hand
};
button.FlatAppearance.BorderSize = 0;
button.FlatAppearance.MouseDownBackColor = Color.FromArgb(67, 56, 202);
button.FlatAppearance.MouseOverBackColor = Color.FromArgb(99, 102, 241);
return button;
}
private static string GetVersionFromReleases()
{
try
{
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
if (version != null)
{
if (version.Revision > 0)
{
return $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
}
return $"{version.Major}.{version.Minor}.{version.Build}";
}
}
catch
{
// Fehler ignorieren
}
return "Unknown";
}
private sealed record MainViewData(string SystemText, GameViewService.GameManagerViewData GameManager);
}