This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.xaml.cs
295 lines (257 loc) · 10.3 KB
/
Home.xaml.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
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
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Xml;
using ModernWpf;
namespace Index
{
public partial class Home : Page
{
private readonly QuinticEase ease = new()
{
EasingMode = EasingMode.EaseInOut
};
public Home()
{
try
{
InitializeComponent();
Trace.WriteLine("Home initialized");
Boot();
}
catch(Exception ex)
{
MessageBox.Show($"Error while preparing Home.xaml.cs\n\n{ex}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Boot()
{
try
{
var row = 0;
var colomn = 0;
Int16 indexid = 1;
foreach (var game in Data.games)
{
if (Data.gametable.Select($"id = {game.Metadata.ID}").Length == 0)
{
Trace.WriteLine("Found new game " + game.Metadata.Name + game.Images.Banners[0].ToString());
object[] data = {
game.Metadata.ID,
indexid,
game.Metadata.Name,
game.Metadata.Description,
game.Images.Banners[0],
game.Download.InfoHash
};
var newrow = Data.gametable.NewRow();
var dex = 0;
foreach (var dat in data)
{
if (dat is null)
{
newrow[dex] = "Data unavailable";
dex++;
break;
}
newrow[dex] = dat;
dex++;
}
Data.gametable.Rows.Add(newrow);
var cnvs = (Grid)XamlReader.Load(XmlReader.Create(new StringReader(XamlWriter.Save(game1))));
cnvs.Name = $"game{indexid}";
if (colomn == 3)
{
RowDefinition ne = new()
{
Height = new GridLength(140)
};
Games.RowDefinitions.Add(ne);
colomn = 0;
row++;
}
Grid.SetColumn(cnvs, colomn);
Grid.SetRow(cnvs, row);
Games.Children.Add(cnvs);
Refresh(game.Metadata.ID, cnvs);
indexid++;
colomn++;
}
else
{
Trace.WriteLine("Found indexed game " + game.Metadata.Name);
}
}
}
catch(Exception ex)
{
MessageBox.Show($"Error while filling gametable data\n\n{ex}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Refresh(Int16 id, FrameworkElement cnvs)
{
DataRow row = null;
try
{
row = Data.gametable.Select($"id = '{id}'")[0];
}
catch
{
MessageBox.Show($"Error while finding gametable row\nIndexID: {id}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
try
{
var text = (Label)cnvs.FindDescendantByName($"gameName");
text.Content = row[(int)GameTable.Name].ToString();
var desc = (TextBlock)cnvs.FindDescendantByName($"gameDesc");
desc.Text = row[(int)GameTable.Description].ToString();
}
catch(Exception ex)
{
MessageBox.Show($"Error while setting text\nIndexID: {id} GameID: {row[2]}\n\n{ex}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
try
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(row[(int)GameTable.Banner].ToString());
bitmap.EndInit();
var img = (Image)cnvs.FindDescendantByName($"image");
img.Source = bitmap;
cnvs.Visibility = Visibility.Visible;
DoubleAnimation da = new()
{
From = cnvs.Opacity,
To = 1.0,
Duration = new Duration(TimeSpan.FromSeconds(.25)),
EasingFunction = ease
};
cnvs.BeginAnimation(OpacityProperty, da);
cnvs.PreviewMouseLeftButtonUp += GameClick;
cnvs.MouseEnter += GameEnter;
cnvs.MouseLeave += GameLeave;
cnvs.Cursor = Cursors.Hand;
}
catch(Exception ex)
{
MessageBox.Show($"Error while loading image\nIndexID: {id}\nImage: {row[(int)GameTable.Banner]}\n\n{ex}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void GameClick(object sender, MouseEventArgs e)
{
try
{
var window = Application.Current.MainWindow;
var element = (FrameworkElement)sender;
var indexid = Int16.Parse(Regex.Match(element.Name, @"\d+").Value);
var row = Data.gametable.Select($"indexid = {indexid}")[0];
(window as Main).Game.Navigate(new Game(Int16.Parse(row[(int)GameTable.ID].ToString())));
(window as Main).Game.Visibility = Visibility.Visible;
DoubleAnimation da = new()
{
From = (window as Main).Game.Opacity,
To = 1.0,
Duration = new Duration(TimeSpan.FromSeconds(.25)),
EasingFunction = ease
};
(window as Main).Game.BeginAnimation(OpacityProperty, da);
}
catch(Exception ex)
{
MessageBox.Show($"Error while preparing game page:\n\n{ex}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private readonly Duration dur = new(TimeSpan.FromMilliseconds(500));
private readonly Duration dur1 = new(TimeSpan.FromMilliseconds(750));
private void GameEnter(object sender, MouseEventArgs e)
{
try
{
var window = Application.Current.MainWindow;
var element = (FrameworkElement)sender;
var indexid = Int16.Parse(Regex.Match(element.Name, @"\d+").Value);
var row = Data.gametable.Select($"indexid = {indexid}")[0];
var button = (Canvas)element.FindDescendantByName("gameButton");
var text = (Label)element.FindDescendantByName("gameName");
var desc = (TextBlock)element.FindDescendantByName("gameDesc");
ThicknessAnimation da = new()
{
From = text.Margin,
To = new Thickness(0, 10, 0, 0),
Duration = dur,
EasingFunction = ease
};
ThicknessAnimation da1 = new()
{
From = desc.Margin,
To = new Thickness(0, 40, 0, 0),
Duration = dur,
EasingFunction = ease
};
DoubleAnimation da2 = new()
{
From = button.OpacityMask.Opacity,
To = .25,
Duration = dur1,
EasingFunction = ease
};
text.BeginAnimation(MarginProperty, da);
desc.BeginAnimation(MarginProperty, da1);
button.BeginAnimation(OpacityProperty, da2);
}
catch (Exception ex)
{
MessageBox.Show($"Error while animating game enter:\n\n{ex}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void GameLeave(object sender, MouseEventArgs e)
{
try
{
var window = Application.Current.MainWindow;
var element = (FrameworkElement)sender;
var indexid = Int16.Parse(Regex.Match(element.Name, @"\d+").Value);
var row = Data.gametable.Select($"indexid = {indexid}")[0];
var button = (Canvas)element.FindDescendantByName("gameButton");
var text = (Label)element.FindDescendantByName("gameName");
var desc = (TextBlock)element.FindDescendantByName("gameDesc");
ThicknessAnimation da = new()
{
From = text.Margin,
To = new Thickness(0, 120, 0, 0),
Duration = dur,
EasingFunction = ease
};
ThicknessAnimation da1 = new()
{
From = desc.Margin,
To = new Thickness(0, 150, 0, 0),
Duration = dur,
EasingFunction = ease
};
DoubleAnimation da2 = new()
{
From = button.OpacityMask.Opacity,
To = 1,
Duration = dur1,
EasingFunction = ease
};
text.BeginAnimation(MarginProperty, da);
desc.BeginAnimation(MarginProperty, da1);
button.BeginAnimation(OpacityProperty, da2);
}
catch (Exception ex)
{
MessageBox.Show($"Error while animating game leave:\n\n{ex}", "Index", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}