Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit 1ef81d8

Browse files
committed
Corrrection
1 parent 1c3a712 commit 1ef81d8

File tree

9 files changed

+282
-156
lines changed

9 files changed

+282
-156
lines changed

Jeux de la vie.Avalonia/App.axaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public partial class App : Application
88
{
99
public override void Initialize()
1010
{
11+
Paramètres_de_lapplication.Charger();
12+
1113
AvaloniaXamlLoader.Load(this);
1214
}
1315

Jeux de la vie.Avalonia/Controls/Grille_de_jeu_Control.axaml.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
using System.Drawing.Imaging;
55
using System.IO;
66
using SystemBitmap = System.Drawing.Bitmap;
7-
using Color = System.Drawing.Color;
87
using Graphics = System.Drawing.Graphics;
98
using SolidBrush = System.Drawing.SolidBrush;
109
using Avalonia.Threading;
1110
using System.Collections.Generic;
1211
using System;
12+
using Avalonia.Media;
13+
using Jeux_de_la_vie.Avalonia.Extensions;
1314

1415
namespace Jeux_de_la_vie.Avalonia.Controls
1516
{
@@ -20,29 +21,21 @@ public Grille_de_jeu_Control()
2021
{
2122
InitializeComponent();
2223

23-
//grille_source = new Bitmap();
24-
taille_cellule = new Size(4, 4);
2524
Taille_ligne = 1;
26-
Couleur_cellule = Color.Black;
27-
Couleur_tableau = Color.White;
25+
Couleur_cellule = Colors.Black;
26+
Couleur_tableau = Colors.White;
2827
grille_source = new SystemBitmap(1, 1);
29-
30-
Nouveau_tableau(100, 100);
3128
}
3229
#endregion
3330

3431
#region Variables
35-
private Size taille_cellule;
3632
private SystemBitmap grille_source;
3733
private Color Couleur_cellule;
3834
private Color Couleur_tableau;
3935
private bool En_cours_dutilisation;
4036
#endregion
4137

4238
#region Properties
43-
private double taille_vertical => Taille_ligne * 2 + taille_cellule.Height * Grille_de_jeu.Size.Height;
44-
private double taille_horizontal => Taille_ligne * 2 + taille_cellule.Width * Grille_de_jeu.Size.Width;
45-
4639
private Bitmap Grille_de_jeu
4740
{
4841
get => Grille_de_jeu_Img.Source as Bitmap ?? new Bitmap(new MemoryStream());
@@ -75,9 +68,16 @@ public void Ajouter_tableau(string nom_du_fichier)
7568

7669
public void Nouveau_tableau(int taille_vertical, int taille_horizontal)
7770
{
71+
if (taille_vertical < 1)
72+
taille_vertical = 1;
73+
74+
if (taille_horizontal < 1)
75+
taille_horizontal = 1;
76+
7877
grille_source = new SystemBitmap(taille_vertical, taille_horizontal);
78+
7979
using (var gfx = Graphics.FromImage(grille_source))
80-
using (var brush = new SolidBrush(Couleur_tableau))
80+
using (var brush = new SolidBrush(Couleur_tableau.ToNativeColor()))
8181
{
8282
gfx.FillRectangle(brush, 0, 0, taille_vertical, taille_horizontal);
8383
}
@@ -89,8 +89,7 @@ public void Nouveau_tableau(int taille_vertical, int taille_horizontal)
8989

9090
public void Définir_couleur(Color tableau, Color celulle)
9191
{
92-
93-
Color pixel;
92+
System.Drawing.Color pixel;
9493

9594
for (int x = 0; x < grille_source.Width; x++)
9695
for (int y = 0; y < grille_source.Height; y++)
@@ -99,7 +98,7 @@ public void Nouveau_tableau(int taille_vertical, int taille_horizontal)
9998

10099
grille_source.SetPixel(
101100
x, y,
102-
pixel == Couleur_cellule ? celulle : tableau);
101+
pixel == Couleur_cellule.ToNativeColor() ? celulle.ToNativeColor() : tableau.ToNativeColor());
103102
}
104103

105104
Couleur_tableau = tableau;
@@ -116,7 +115,7 @@ public void Nouveau_tableau(int taille_vertical, int taille_horizontal)
116115
grille_source.SetPixel(
117116
position_vertical,
118117
position_horizontal,
119-
cellule ? Couleur_cellule : Couleur_tableau);
118+
cellule ? Couleur_cellule.ToNativeColor() : Couleur_tableau.ToNativeColor());
120119

121120
En_cours_dutilisation = false;
122121
Recharger_grille();
@@ -134,27 +133,14 @@ public void Nouveau_tableau(int taille_vertical, int taille_horizontal)
134133
for (int x = 0; x < tableau.GetLength(0); x++)
135134
for (int y = 0; y < tableau.GetLength(1); y++)
136135
grille_source.SetPixel(x, y,
137-
tableau[x, y] ? Couleur_cellule : Couleur_tableau);
136+
tableau[x, y] ? Couleur_cellule.ToNativeColor() : Couleur_tableau.ToNativeColor());
138137

139138
En_cours_dutilisation = false;
140139
Recharger_grille();
141140

142141
return true;
143142
}
144143

145-
[Obsolete]
146-
public bool[,] Exporter_le_tableau()
147-
{
148-
var tableau = new bool[grille_source.Height, grille_source.Width];
149-
var couleur_cellule = Couleur_cellule.ToArgb();
150-
151-
for (int y = 0; y < grille_source.Height; y++)
152-
for (int x = 0; x < grille_source.Width; x++)
153-
tableau[y, x] = grille_source.GetPixel(x, y).ToArgb() == couleur_cellule;
154-
155-
return tableau;
156-
}
157-
158144
private void Recharger_grille() => Dispatcher.UIThread.Post(() =>
159145
{
160146
if (En_cours_dutilisation)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Avalonia.Media;
2+
using Avalonia.Media.Immutable;
3+
4+
namespace Jeux_de_la_vie.Avalonia.Extensions
5+
{
6+
internal static class Avalonia_color_extension
7+
{
8+
public static System.Drawing.Color ToNativeColor(this Color color) =>
9+
System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
10+
public static Color ToColor(this IBrush brush)
11+
{
12+
if (brush is SolidColorBrush solidColorBrush)
13+
return solidColorBrush.Color;
14+
else if (brush is ImmutableSolidColorBrush immutableSolidColorBrush)
15+
return immutableSolidColorBrush.Color;
16+
else
17+
return Colors.White;
18+
}
19+
}
20+
}

Jeux de la vie.Avalonia/Paramètres_de_l'application.cs

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System;
2-
using System.ComponentModel;
1+
using Avalonia.Media;
2+
using System;
33
using System.Drawing;
44
using System.IO;
55
using System.Text.Json;
@@ -8,37 +8,75 @@ namespace Jeux_de_la_vie.Avalonia
88
{
99
internal class Paramètres_de_lapplication
1010
{
11-
public Size Taille_tableau { get; set; } = new(100, 100);
12-
public int Couleur_tableau { get; set; } = Color.White.ToArgb();
13-
public int Couleur_celulle { get; set; } = Color.Black.ToArgb();
14-
public string? Dernier_tableau;
11+
#region Variables
12+
/// <summary> Y </summary>
13+
public static uint Taille_tableau_vertical
14+
{
15+
get => instance._Taille_tableau_vertical;
16+
set => instance._Taille_tableau_vertical = value;
17+
}
18+
/// <summary> X </summary>
19+
public static uint Taille_tableau_horizontal
20+
{
21+
get => instance._Taille_tableau_horizontal;
22+
set => instance._Taille_tableau_horizontal = value;
23+
}
24+
public static uint Couleur_tableau
25+
{
26+
get => instance._Couleur_tableau;
27+
set => instance._Couleur_tableau = value;
28+
}
29+
public static uint Couleur_celulle
30+
{
31+
get => instance._Couleur_celulle;
32+
set => instance._Couleur_celulle = value;
33+
}
34+
public static string? Dernier_tableau
35+
{
36+
get => instance._Dernier_tableau;
37+
set => instance._Dernier_tableau = value;
38+
}
1539

1640
public static event EventHandler? Paramètre_changer;
41+
#endregion
1742

18-
public static Paramètres_de_lapplication Actuelle
19-
{
20-
get
21-
{
22-
if (paramètres_de_lapplication_actuelle == null)
23-
paramètres_de_lapplication_actuelle = Charger();
43+
#region Properties
44+
public uint _Taille_tableau_vertical { get; set; } = 100;
45+
public uint _Taille_tableau_horizontal { get; set; } = 100;
46+
public uint _Couleur_tableau { get; set; } = Colors.White.ToUint32();
47+
public uint _Couleur_celulle { get; set; } = Colors.Black.ToUint32();
48+
public string? _Dernier_tableau { get; set; }
2449

25-
return paramètres_de_lapplication_actuelle ?? new();
26-
}
27-
}
28-
private static Paramètres_de_lapplication? paramètres_de_lapplication_actuelle;
50+
private static Paramètres_de_lapplication instance { get; set; } = new();
51+
#endregion
2952

30-
private static Paramètres_de_lapplication? Charger()
53+
#region Methods
54+
/// <summary>
55+
/// Charge le fichier de sauvegarde
56+
/// </summary>
57+
/// <returns></returns>
58+
public static bool Charger()
3159
{
3260
try
3361
{
3462
var chemin_du_fichier_de_sauvegarde = Obtenir_le_chemin_du_fichier_de_sauvegarde();
3563

64+
if (!File.Exists(chemin_du_fichier_de_sauvegarde))
65+
return false;
66+
3667
var flux = File.OpenRead(chemin_du_fichier_de_sauvegarde);
37-
return JsonSerializer.Deserialize<Paramètres_de_lapplication>(flux);
68+
var nouvelle_instance = JsonSerializer.Deserialize<Paramètres_de_lapplication>(flux);
69+
70+
if (nouvelle_instance == null)
71+
return false;
72+
else
73+
instance = nouvelle_instance;
74+
75+
return true;
3876
}
3977
catch
4078
{
41-
return null;
79+
return false;
4280
}
4381
}
4482

@@ -51,7 +89,7 @@ public static bool Sauvegarder()
5189
try
5290
{
5391
var chemin_du_fichier_de_sauvegarde = Obtenir_le_chemin_du_fichier_de_sauvegarde();
54-
var json = JsonSerializer.Serialize(Actuelle, new JsonSerializerOptions()
92+
var json = JsonSerializer.Serialize(instance, new JsonSerializerOptions()
5593
{
5694
WriteIndented = true,
5795
});
@@ -77,5 +115,6 @@ private static string Obtenir_le_chemin_du_fichier_de_sauvegarde()
77115

78116
return path;
79117
}
118+
#endregion
80119
}
81120
}

Jeux de la vie.Avalonia/Windows/MainWindow.axaml.cs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ public partial class MainWindow : Window
1515
public MainWindow()
1616
{
1717
InitializeComponent();
18-
Initialisation();
1918

20-
tableau_initial = new bool[
21-
Paramètres_de_lapplication.Actuelle.Taille_tableau.Width,
22-
Paramètres_de_lapplication.Actuelle.Taille_tableau.Height];
19+
tableau_initial = new bool[1,1];
20+
jeu_de_la_vie = new();
2321

22+
Initialisation();
2423
}
2524
#endregion
2625

@@ -54,37 +53,14 @@ public async void Charger_tableau(object sender, RoutedEventArgs e)
5453
// Charger le fichier selectionner si il existe
5554
if (resulta != null && resulta.Length > 0)
5655
{
57-
if (File.Exists(resulta[0]))
58-
{
59-
switch (Path.GetExtension(resulta[0]).ToLower())
60-
{
61-
case ".bmp":
62-
var tableau_bmp = Charger_BMP(resulta[0]);
63-
Ajouter_tableau(tableau_bmp, 0, 0, true);
64-
break;
65-
66-
case ".csv":
67-
var tableau_csv = Charger_CSV(resulta[0]);
68-
Ajouter_tableau(tableau_csv, 0, 0, true);
69-
break;
70-
71-
default:
72-
Debug.WriteLine("L'extension n'est pas connus");
73-
break;
74-
}
75-
}
76-
else
77-
Debug.WriteLine("Le fichier n'existe pas");
56+
if (Essayer_de_charger_un_fichier(resulta[0], out var tableau))
57+
Ajouter_tableau(tableau!, 0, 0, true);
7858
}
7959
}
8060

8161
public void Modifier_tableau(object sender, RoutedEventArgs e)
8262
{
83-
var fenétre_propriété_du_tableau = new Propriété_du_tableau_Window()
84-
{
85-
Lignes = Grille_de_jeu.Lignes,
86-
Colonnes = Grille_de_jeu.Colonnes,
87-
};
63+
var fenétre_propriété_du_tableau = new Propriété_du_tableau_Window();
8864
fenétre_propriété_du_tableau.Show();
8965
}
9066

Jeux de la vie.Avalonia/Windows/Propriété_du_tableau_Window.axaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
Orientation="Horizontal" Spacing="10">
3434
<TextBox x:Name="Taille_lignes_TextBox"
3535
Text="100"
36-
UseFloatingWatermark="True" Watermark="Lignes"/>
36+
UseFloatingWatermark="True" Watermark="Lignes"
37+
KeyUp="Taille_lignes_TextBox_KeyUp"/>
3738
<TextBlock Text="X" VerticalAlignment="Center"/>
3839
<TextBox x:Name="Taille_colonnes_TextBox"
3940
Text="80"
40-
UseFloatingWatermark="True" Watermark="Colonnes"/>
41+
UseFloatingWatermark="True" Watermark="Colonnes"
42+
KeyUp="Taille_colonnes_TextBox_KeyUp"/>
4143
</StackPanel>
4244

4345
<!-- Taille du tableau -->
@@ -55,7 +57,8 @@
5557
<TextBox x:Name="Couleur_cellule_TextBox"
5658
MinWidth="80" HorizontalAlignment="Left"
5759
Text="#FAFAFA"
58-
UseFloatingWatermark="True" Watermark="Cellule"/>
60+
UseFloatingWatermark="True" Watermark="Cellule"
61+
KeyUp="Couleur_cellule_TextBox_KeyUo"/>
5962
</StackPanel>
6063
<StackPanel Grid.Row="8" Margin="8" Orientation="Horizontal" Spacing="10">
6164
<Border x:Name="Couleur_tableau_Border"
@@ -66,7 +69,8 @@
6669
<TextBox x:Name="Couleur_tableau_TextBox"
6770
MinWidth="80" HorizontalAlignment="Left"
6871
Text="#141516"
69-
UseFloatingWatermark="True" Watermark="Tableau"/>
72+
UseFloatingWatermark="True" Watermark="Tableau"
73+
KeyUp="Couleur_tableau_TextBox_KeyUp"/>
7074
</StackPanel>
7175

7276
<!-- Boutons de navigation -->

0 commit comments

Comments
 (0)