-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dlg.xaml.cs
123 lines (110 loc) · 3.58 KB
/
Dlg.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MyPropertyDlg3
{
/// <summary>
/// Lógica interna para Dlg.xaml
/// </summary>
public partial class Dlg : Window
{
public static Options options = new Options();
public Dlg()
{
InitializeComponent();
}
private void okButton_Click(object sender, RoutedEventArgs e)
{
options.ball_radius = float.Parse(cbBallRadius.Text);
options.hole_radius = float.Parse(cbHoleRadius.Text);
options.wall_friction = int.Parse(cbWallFriction.Text);
options.table_friction = int.Parse(cbTableFriction.Text);
options.Save();
DialogResult = true;
Close();
}
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
options.Load();
}
// set combos sels
private void cbBallRadius_Loaded(object sender, RoutedEventArgs e)
{
cbBallRadius.Text = options.ball_radius.ToString();
}
private void cbHoleRadius_Loaded(object sender, RoutedEventArgs e)
{
cbHoleRadius.Text = options.hole_radius.ToString();
}
private void cbTableFriction_Loaded(object sender, RoutedEventArgs e)
{
cbTableFriction.Text = options.table_friction.ToString();
}
private void cbWallFriction_Loaded(object sender, RoutedEventArgs e)
{
cbWallFriction.Text = options.wall_friction.ToString();
}
}
public struct Options
{
public Options() { }
public float ball_radius { get; set; } = 10f;
public float hole_radius { get; set; } = 25f;
public int table_friction { get; set; } = 5;
public int wall_friction { get; set; } = 10;
public void Save()
{
string? path = System.IO.Path.ChangeExtension(Environment.ProcessPath, ".json");
if (path != null)
{
string json;
File.WriteAllText(path,json = JsonSerializer.Serialize<Options>(this));
}
}
public void Load()
{
string? path = System.IO.Path.ChangeExtension(Environment.ProcessPath, ".json");
if (path != null)
{
try
{
string? json = File.ReadAllText(path);
if (json != null)
{
Options options;
options = JsonSerializer.Deserialize<Options>(json);
ball_radius = options.ball_radius;
hole_radius = options.hole_radius;
wall_friction = options.wall_friction;
table_friction = options.table_friction;
}
}
catch (Exception ex)
{
}
}
}
public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}
}