-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompatibilityStatsWindow.cs
More file actions
356 lines (308 loc) · 12.7 KB
/
CompatibilityStatsWindow.cs
File metadata and controls
356 lines (308 loc) · 12.7 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Playnite.SDK;
namespace ControllerCompatibility
{
public partial class CompatibilityStatsWindow : Window
{
private CompatibilityDatabase compatibilityDb;
private ControllerDetectionService controllerService;
private static readonly ILogger logger = LogManager.GetLogger();
public CompatibilityStatsWindow(CompatibilityDatabase db, ControllerDetectionService controller)
{
InitializeComponent();
compatibilityDb = db;
controllerService = controller;
Loaded += (s, e) =>
{
try
{
RefreshStats();
}
catch (Exception ex)
{
logger.Error($"Error in refresh stats: {ex.Message}");
logger.Error($"Stack trace: {ex.StackTrace}");
// Show error message to user
MessageBox.Show($"Error loading compatibility stats: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
};
}
private void RefreshStats()
{
try
{
logger.Info("Refresh stats start");
if (compatibilityDb == null)
{
logger.Error("Compatibility DB is null");
throw new Exception("Compatibility database is null");
}
if (controllerService == null)
{
logger.Error("Controller service is null");
throw new Exception("Controller service is null");
}
var allGames = compatibilityDb.GetAllCompatibilityInfo();
logger.Info($"Got all games: {allGames.Count}");
var connectedControllers = controllerService.GetConnectedControllers();
logger.Info($"Got controllers: {connectedControllers.Count}");
// Calculate statistics
var stats = new Dictionary<ControllerSupportLevel, int>();
foreach (ControllerSupportLevel level in Enum.GetValues(typeof(ControllerSupportLevel)))
{
stats[level] = allGames.Count(g => g.SupportLevel == level);
}
logger.Info("Calculated stats");
// Update pie chart
DrawPieChart(stats);
// Update statistics panel
UpdateStatsPanel(stats, allGames.Count);
// Update controllers panel
UpdateControllersPanel(connectedControllers);
logger.Info("Refresh stats complete");
}
catch (Exception ex)
{
logger.Error($"Error in refresh stats: {ex.Message}");
logger.Error($"Stack trace: {ex.StackTrace}");
throw; // Re-throw to be caught by the Loaded handler
}
}
private void DrawPieChart(Dictionary<ControllerSupportLevel, int> stats)
{
PieChartCanvas.Children.Clear();
var total = stats.Values.Sum();
if (total == 0) return;
var centerX = PieChartCanvas.ActualWidth / 2;
var centerY = PieChartCanvas.ActualHeight / 2;
var radius = Math.Min(centerX, centerY) - 20;
double startAngle = 0;
foreach (var kvp in stats.Where(s => s.Value > 0))
{
var sweepAngle = (kvp.Value / (double)total) * 360;
// Create pie slice
var path = new Path
{
Fill = GetCompatibilityColor(kvp.Key),
Stroke = Brushes.White,
StrokeThickness = 2
};
var pathGeometry = new PathGeometry();
var pathFigure = new PathFigure
{
StartPoint = new Point(centerX, centerY)
};
// Calculate arc points
var startPoint = GetPointOnCircle(centerX, centerY, radius, startAngle);
var endPoint = GetPointOnCircle(centerX, centerY, radius, startAngle + sweepAngle);
// Create arc segment
var isLargeArc = sweepAngle > 180;
var arcSegment = new ArcSegment
{
Point = endPoint,
Size = new Size(radius, radius),
SweepDirection = SweepDirection.Clockwise,
IsLargeArc = isLargeArc
};
pathFigure.Segments.Add(new LineSegment(startPoint, true));
pathFigure.Segments.Add(arcSegment);
pathFigure.Segments.Add(new LineSegment(new Point(centerX, centerY), true));
pathGeometry.Figures.Add(pathFigure);
path.Data = pathGeometry;
PieChartCanvas.Children.Add(path);
// Add percentage label
var labelAngle = startAngle + sweepAngle / 2;
var labelPoint = GetPointOnCircle(centerX, centerY, radius * 0.7, labelAngle);
var percentage = (kvp.Value / (double)total * 100).ToString("0.#") + "%";
var label = new TextBlock
{
Text = percentage,
FontSize = 12,
FontWeight = FontWeights.Bold,
Foreground = Brushes.White,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
Canvas.SetLeft(label, labelPoint.X - 15);
Canvas.SetTop(label, labelPoint.Y - 10);
PieChartCanvas.Children.Add(label);
startAngle += sweepAngle;
}
// Add legend
AddLegend(stats);
}
private void AddLegend(Dictionary<ControllerSupportLevel, int> stats)
{
var legendPanel = new StackPanel
{
Orientation = Orientation.Vertical,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10)
};
foreach (var kvp in stats.Where(s => s.Value > 0))
{
var legendItem = new StackPanel
{
Orientation = Orientation.Horizontal,
Margin = new Thickness(0, 2, 0, 2)
};
var colorRect = new Rectangle
{
Width = 16,
Height = 16,
Fill = GetCompatibilityColor(kvp.Key),
Stroke = Brushes.White,
StrokeThickness = 1,
Margin = new Thickness(0, 0, 8, 0)
};
var label = new TextBlock
{
Text = $"{GetCompatibilityName(kvp.Key)}: {kvp.Value}",
VerticalAlignment = VerticalAlignment.Center,
FontSize = 11
};
legendItem.Children.Add(colorRect);
legendItem.Children.Add(label);
legendPanel.Children.Add(legendItem);
}
Canvas.SetLeft(legendPanel, 10);
Canvas.SetTop(legendPanel, 10);
PieChartCanvas.Children.Add(legendPanel);
}
private void UpdateStatsPanel(Dictionary<ControllerSupportLevel, int> stats, int totalGames)
{
StatsPanel.Children.Clear();
var totalText = new TextBlock
{
Text = $"Total Games: {totalGames}",
FontSize = 14,
FontWeight = FontWeights.Bold,
Margin = new Thickness(0, 0, 0, 10)
};
StatsPanel.Children.Add(totalText);
foreach (var kvp in stats.Where(s => s.Value > 0))
{
var percentage = totalGames > 0 ? (kvp.Value / (double)totalGames * 100).ToString("0.#") : "0";
var statText = new TextBlock
{
Text = $"{GetCompatibilityName(kvp.Key)}: {kvp.Value} ({percentage}%)",
Margin = new Thickness(0, 2, 0, 2),
FontSize = 12
};
StatsPanel.Children.Add(statText);
}
// Add controller-ready percentage
var controllerReady = stats[ControllerSupportLevel.Full] + stats[ControllerSupportLevel.Partial];
var readyPercentage = totalGames > 0 ? (controllerReady / (double)totalGames * 100).ToString("0.#") : "0";
var readyText = new TextBlock
{
Text = $"Controller Ready: {controllerReady} ({readyPercentage}%)",
FontSize = 12,
FontWeight = FontWeights.Bold,
Foreground = Brushes.Green,
Margin = new Thickness(0, 10, 0, 0)
};
StatsPanel.Children.Add(readyText);
}
private void UpdateControllersPanel(List<DetectedController> controllers)
{
ControllersPanel.Children.Clear();
if (controllers.Any())
{
foreach (var controller in controllers)
{
var controllerPanel = new StackPanel
{
Orientation = Orientation.Horizontal,
Margin = new Thickness(0, 2, 0, 2)
};
var icon = new TextBlock
{
Text = GetControllerEmoji(controller.Type),
FontSize = 16,
Margin = new Thickness(0, 0, 8, 0),
VerticalAlignment = VerticalAlignment.Center
};
var name = new TextBlock
{
Text = controller.Name,
VerticalAlignment = VerticalAlignment.Center,
FontSize = 12
};
controllerPanel.Children.Add(icon);
controllerPanel.Children.Add(name);
ControllersPanel.Children.Add(controllerPanel);
}
}
else
{
var noControllers = new TextBlock
{
Text = "No controllers detected",
FontSize = 12,
Foreground = Brushes.Gray,
FontStyle = FontStyles.Italic
};
ControllersPanel.Children.Add(noControllers);
}
}
private Point GetPointOnCircle(double centerX, double centerY, double radius, double angleDegrees)
{
var angleRadians = angleDegrees * Math.PI / 180;
return new Point(
centerX + radius * Math.Cos(angleRadians - Math.PI / 2),
centerY + radius * Math.Sin(angleRadians - Math.PI / 2)
);
}
private Brush GetCompatibilityColor(ControllerSupportLevel level)
{
return level switch
{
ControllerSupportLevel.Full => Brushes.Green,
ControllerSupportLevel.Partial => Brushes.Orange,
ControllerSupportLevel.Community => Brushes.CornflowerBlue,
ControllerSupportLevel.None => Brushes.Red,
ControllerSupportLevel.Unknown => Brushes.Gray,
_ => Brushes.Gray
};
}
private string GetCompatibilityName(ControllerSupportLevel level)
{
return level switch
{
ControllerSupportLevel.Full => "Full Support",
ControllerSupportLevel.Partial => "Partial Support",
ControllerSupportLevel.Community => "Community Configs",
ControllerSupportLevel.None => "No Support",
ControllerSupportLevel.Unknown => "Unknown",
_ => "Unknown"
};
}
private string GetControllerEmoji(ControllerType type)
{
return type switch
{
ControllerType.Xbox => "X",
ControllerType.PlayStation => "P",
ControllerType.Nintendo => "N",
ControllerType.Steam => "S",
_ => "C"
};
}
private void RefreshButton_Click(object sender, RoutedEventArgs e)
{
RefreshStats();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}