-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
501 lines (449 loc) · 20.9 KB
/
MainWindow.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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 ZC.PeriodicTableLearner.Resources.Models;
using ZC.PeriodicTableLearner.Resources.Extensions;
using System.Windows.Media.Animation;
namespace ZC.PeriodicTableLearner
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public const int CASE_WIDTH = 75;
public const int CASE_HEIGHT = 75;
public const int FONT_SIZE = 24;
public static List<Element> allElements = new List<Element>();
public static Element[,] elements = new Element[9, 18];
public Color colorCache = Color.FromArgb(160, 255, 255, 255);
public static MainWindow thisMainWindow = null;
public (ChemicalGroup c, Element e) lastFilter = default;
public static bool hasClicked;
public MainWindow()
{
InitializeComponent();
ChemicalGroup.CreateAll();
foreach (ChemicalGroup chemicalGroup in ChemicalGroup.ChemicalGroups)
{
foreach (Element element in chemicalGroup.Elements)
allElements.Add(element);
}
allElements = allElements.OrderBy(element => element.AtomicNumber).ToList();
int idx = 0;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 18; j++)
{
if (i >= 5 && i <= 6 && j == 2)
{
for (int k = 0; k < 15; k++)
{
elements[i + 2, j + k] = allElements[idx];
idx++;
}
}
else if ((i == j && j == 0) || (i == 0 && j == 17) || (i >= 1 && i <= 2 && j <= 1) || (i >= 1 && i <= 2 && j >= 12) || (i >= 3 && i <= 4) ||
(i >= 5 && i <= 6 && j != 2))
{
elements[i, j] = allElements[idx];
idx++;
}
}
}
idx = 0;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 18; j++)
{
if (i >= 5 && i <= 6 && j == 2)
{
CreateSerie(idx, j, i);
idx += 15;
}
else if (elements[i, j] == default)
{
continue;
}
else
{
CreateCase(elements[i, j], j + (i >= 7 && i <= 8 ? 0.5 : 0), i + (i >= 7 && i <= 8 ? 0.1 : 0));
idx++;
}
}
}
CreateColumns();
CreateLegend();
thisMainWindow = this;
}
/// <summary>
/// On left click, apply the cache over the elements that are not in the specified ChemicalGroup
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Label_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Label label = sender as Label;
ChemicalGroup chemicalGroup = ChemicalGroup.GetChemicalGroupFromName(label.Content.ToString());
if (thisMainWindow.lastFilter.c != default && thisMainWindow.lastFilter.c.Equals(chemicalGroup))
hasClicked = !hasClicked;
thisMainWindow.ToggleFilter(chemicalGroup);
}
/// <summary>
/// On mouse enter, apply the cache over the elements that are not in the specified ChemicalGroup
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Label_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!hasClicked)
{
Label label = sender as Label;
thisMainWindow.ToggleFilter(ChemicalGroup.GetChemicalGroupFromName(label.Content.ToString()));
}
}
/// <summary>
/// On mouse leave, remove the cache over the elements that are not in the specified ChemicalGroup
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Label_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
if (!hasClicked)
{
thisMainWindow.ToggleFilter();
}
}
/// <summary>
/// Toggle the visibility of the cache
/// </summary>
/// <param name="chemicalGroup">The chemical group to filter</param>
/// <param name="element">The element to filter</param>
public void ToggleFilter(ChemicalGroup chemicalGroup = default, Element element = default)
{
foreach (Rectangle rectangle in mainGrid.Children.OfType<Rectangle>())
{
if (chemicalGroup == default && element == default)
{
AnimateVisibility(rectangle, false, rectangle.IsVisible ? 1 : 0);
continue;
}
if (rectangle.Tag == default) continue;
(ChemicalGroup c, Element e) tagOfO = ((ChemicalGroup c, Element e))rectangle.Tag;
AnimateVisibility(rectangle, !(tagOfO.c.Equals(chemicalGroup) || tagOfO.e.Equals(element)), rectangle.IsVisible ? 1 : 0);
}
lastFilter = lastFilter.Equals((chemicalGroup, element)) && !hasClicked ? default : (chemicalGroup, element);
}
/// <summary>
/// Animate the visibility toggle
/// </summary>
/// <param name="element">The element to change visibility</param>
/// <param name="isVisible">If the element finishes visible or not</param>
/// <param name="currentState">The current state of the element, 1 for visible, 0 otherwise</param>
private void AnimateVisibility(UIElement element, bool isVisible, int currentState)
{
DoubleAnimation animation = new DoubleAnimation
{
Duration = TimeSpan.FromSeconds(0.2),
From = currentState,
To = isVisible ? 1 : 0,
FillBehavior = FillBehavior.HoldEnd
};
element.BeginAnimation(OpacityProperty, animation);
if (!isVisible)
animation.Completed += (s, e) => element.Visibility = Visibility.Hidden;
else element.Visibility = Visibility.Visible;
}
/// <summary>
/// Create the legends of chemical groups
/// </summary>
public void CreateLegend()
{
int height = 40;
int nbrColumn = 8;
double width = (mainWindow.Width - 20 - (nbrColumn * 10) - (nbrColumn * 20)) / nbrColumn;
double marginLeft = 10 + (1 * width);
for (int i = 0; i < ChemicalGroup.ChemicalGroups.Count; i++)
{
if (i == 6) marginLeft = 10 + (1.5 * width);
int rtl, rtr, rbl, rbr;
rtl = rtr = rbl = rbr = 3;
Label lblName = new Label()
{
Content = ChemicalGroup.ChemicalGroups[i].Name,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Width = width,
Height = 30,
FontSize = 12,
FontWeight = FontWeights.Bold,
FontFamily = new FontFamily("Arial"),
Padding = new Thickness(0, 0, 0, 0)
};
lblName.MouseLeftButtonUp += Label_MouseLeftButtonUp;
lblName.MouseEnter += Label_MouseEnter;
lblName.MouseLeave += Label_MouseLeave;
Border border = new Border()
{
Width = width,
Height = 30,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(marginLeft, 750 + ((i >= 6 ? 1 : 0) * height), 0, 0),
CornerRadius = new CornerRadius(rtl, rtr, rbr, rbl),
Child = lblName,
BorderThickness = new Thickness(1),
BorderBrush = Brushes.Black,
Background = ChemicalGroup.ChemicalGroups[i].GroupColor
};
mainGrid.Children.Add(border);
marginLeft += width + 10 + 20;
}
}
/// <summary>
/// Create columns index
/// </summary>
public void CreateColumns()
{
int height;
string letter;
int number = 0;
for (int i = 0; i < 18; i++)
{
letter = i >= 2 && i <= 11 ? "B" : "A";
height = i >= 1 && i < 17 ? (i >= 2 && i <= 11 ? 3 : 1) : 0;
number++;
if (i == 10) number = 1;
if (i >= 7 && i <= 9) number = 8;
Label lblColumnTitle = new Label()
{
Content = number.ToRoman() + letter,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + i * CASE_WIDTH + i * 2, 10 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH,
Height = 24,
FontSize = FONT_SIZE * 2 / 3,
FontWeight = FontWeights.Bold,
FontFamily = new FontFamily("Arial"),
Padding = new Thickness(2, 1, 0, 0)
};
mainGrid.Children.Add(lblColumnTitle);
}
}
/// <summary>
/// Create the table case of the series of element
/// </summary>
/// <param name="first">The first element of the series</param>
/// <param name="width">The width in the table</param>
/// <param name="height">The height in the table</param>
public void CreateSerie(int first, double width, double height)
{
CreateTableCase();
CreateSerieTitle();
void CreateTableCase()
{
int rtl, rtr, rbl, rbr;
rtl = rtr = rbl = rbr = 5;
Rectangle rect = new Rectangle()
{
Width = CASE_WIDTH,
Height = CASE_HEIGHT,
Fill = allElements[first].ChemicalGroup.GroupColor,
};
Border border = new Border()
{
Width = CASE_WIDTH,
Height = CASE_HEIGHT,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
CornerRadius = new CornerRadius(rtl, rtr, rbr, rbl),
Child = rect,
BorderThickness = new Thickness(1),
BorderBrush = Brushes.Black
};
Rectangle cache = new Rectangle()
{
Tag = (allElements[first].ChemicalGroup, allElements[first]),
Visibility = Visibility.Hidden,
Width = CASE_WIDTH,
Height = CASE_HEIGHT,
Fill = new SolidColorBrush(colorCache),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
};
Label lblNoAtom = new Label()
{
Content = allElements[first].AtomicNumber.ToString() + "-" + (allElements[first].AtomicNumber + 14).ToString(),
HorizontalContentAlignment = HorizontalAlignment.Left,
VerticalContentAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH,
Height = CASE_HEIGHT / 3,
FontSize = FONT_SIZE * 1 / 2,
FontFamily = new FontFamily("Arial"),
Padding = new Thickness(3, 2, 0, 0)
};
_ = mainGrid.Children.Add(border);
_ = mainGrid.Children.Add(lblNoAtom);
_ = mainGrid.Children.Add(cache);
}
void CreateSerieTitle()
{
width -= 0.6;
height += 2.1;
Label lblSerieTitle = new Label()
{
Content = allElements[first].AtomicNumber == 57 ? "Lanthanide" : "Actinide",
HorizontalContentAlignment = HorizontalAlignment.Right,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH + 5,
Height = CASE_HEIGHT,
FontSize = FONT_SIZE * 1 / 2,
FontFamily = new FontFamily("Arial Black"),
Padding = new Thickness(2, 1, 0, 0)
};
_ = mainGrid.Children.Add(lblSerieTitle);
}
}
/// <summary>
/// Create the case for an element
/// </summary>
/// <param name="element">The element to create the case for</param>
/// <param name="width">The width in the table</param>
/// <param name="height">The height in the table</param>
public void CreateCase(Element element, double width, double height)
{
int rtl, rtr, rbl, rbr;
rtl = rtr = rbl = rbr = 6;
int i = (int)Math.Floor(width);
int j = (int)Math.Floor(height);
if (i - 1 >= 0 && elements[j, i - 1] != default && elements[j, i - 1].ChemicalGroup.Equals(elements[j, i].ChemicalGroup)) rtl = rbl = 1;
if (i + 1 <= 17 && elements[j, i + 1] != default && elements[j, i + 1].ChemicalGroup.Equals(elements[j, i].ChemicalGroup)) rtr = rbr = 1;
if (j - 1 >= 0 && elements[j - 1, i] != default && elements[j - 1, i].ChemicalGroup.Equals(elements[j, i].ChemicalGroup)) rtl = rtr = 1;
if (j + 1 <= 6 && elements[j + 1, i] != default && elements[j + 1, i].ChemicalGroup.Equals(elements[j, i].ChemicalGroup)) rbl = rbr = 1;
Rectangle rect = new Rectangle()
{
Tag = (element.ChemicalGroup, element),
Width = CASE_WIDTH,
Height = CASE_HEIGHT,
Fill = element.ChemicalGroup.GroupColor,
};
Border border = new Border()
{
Width = CASE_WIDTH,
Height = CASE_HEIGHT,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
CornerRadius = new CornerRadius(rtl, rtr, rbr, rbl),
Child = rect,
BorderThickness = new Thickness(1),
BorderBrush = Brushes.Black
};
Rectangle cache = new Rectangle()
{
Tag = (element.ChemicalGroup, element),
Visibility = Visibility.Hidden,
Width = CASE_WIDTH,
Height = CASE_HEIGHT,
Fill = new SolidColorBrush(colorCache),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
};
Label lblSymbol = new Label()
{
Content = element.Symbol,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 29 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH,
Height = CASE_HEIGHT,
FontSize = FONT_SIZE,
FontFamily = new FontFamily("Arial Black"),
Padding = new Thickness(0, 0, 0, 0)
};
Label lblName = new Label()
{
Content = element.Name,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 74 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH,
Height = CASE_HEIGHT / 3,
FontSize = FONT_SIZE * 1 / 2,
FontFamily = new FontFamily("Arial"),
Padding = new Thickness(0, 0, 0, 0)
};
Label lblAtomicWeight = new Label()
{
Content = element.AtomicWeight,
HorizontalContentAlignment = HorizontalAlignment.Center,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 86 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH,
Height = CASE_HEIGHT / 3,
FontSize = (FONT_SIZE * 1 / 2) - 2,
FontFamily = new FontFamily("Arial"),
Padding = new Thickness(0, 0, 0, 0)
};
Label lblNoAtom = new Label()
{
Content = element.AtomicNumber.ToString(),
HorizontalContentAlignment = HorizontalAlignment.Left,
VerticalContentAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH,
Height = CASE_HEIGHT / 3,
FontSize = (FONT_SIZE * 1 / 2) + 4,
FontFamily = new FontFamily("Arial"),
Padding = new Thickness(3, 2, 0, 0)
};
Label lblElecNeg = new Label()
{
Content = element.ElectroNegativity.ToString(),
HorizontalContentAlignment = HorizontalAlignment.Right,
VerticalContentAlignment = VerticalAlignment.Top,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(10 + width * CASE_WIDTH + width * 2, 32 + height * CASE_HEIGHT + height * 2, 0, 0),
Width = CASE_WIDTH,
Height = CASE_HEIGHT / 3,
FontSize = FONT_SIZE * 1 / 2,
FontFamily = new FontFamily("Arial"),
Padding = new Thickness(0, 2, 3, 0)
};
_ = mainGrid.Children.Add(border);
_ = mainGrid.Children.Add(lblNoAtom);
_ = mainGrid.Children.Add(lblSymbol);
_ = mainGrid.Children.Add(lblElecNeg);
_ = mainGrid.Children.Add(lblName);
_ = mainGrid.Children.Add(lblAtomicWeight);
_ = mainGrid.Children.Add(cache);
}
}
}