-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
181 lines (155 loc) · 6.16 KB
/
Form1.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
using SenkiSavedataEditor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SenkiSaveEditor
{
public partial class Form1 : Form
{
private TextBox[] GeneralInfoTextBoxes;
private TextBox[] StatsTextBoxes;
private Savedata.CharacterInfo _currentCharInfo;
private int _currentCharIndex = 0;
private string _savedatafile;
public Form1()
{
InitializeComponent();
GeneralInfoTextBoxes = new TextBox[] { txtGe0, txtGe1, txtGe2, txtGe3, txtGe4, txtGe5, txtGe6, txtGe7, txtGe8, txtGe9, txtGe10 };
StatsTextBoxes = new TextBox[] { txtSt0, txtSt1, txtSt2, txtSt3, txtSt4, txtSt5 };
foreach (var c in Savedata.CharacterDescriptions)
lstChar.Items.Add(c);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
SetUpGeneralInfoTextBoxesTagsAndEvents();
SetUpStatsTextBoxesTagsAndEvents();
}
private void SetUpGeneralInfoTextBoxesTagsAndEvents()
{
for (int i = 0; i < GeneralInfoTextBoxes.Length; i++)
{
GeneralInfoTextBoxes[i].TextChanged += GeneralInfoTextBox_TextChanged;
GeneralInfoTextBoxes[i].Leave += GeneralInfoTextBox_Leave;
GeneralInfoTextBoxes[i].Tag = i;
}
}
private void SetUpStatsTextBoxesTagsAndEvents()
{
for (int i = 0; i < StatsTextBoxes.Length; i++)
{
StatsTextBoxes[i].TextChanged += StatsTextBox_TextChanged;
StatsTextBoxes[i].Leave += StatsTextBox_Leave;
StatsTextBoxes[i].Tag = i;
}
}
private void GeneralInfoTextBox_TextChanged(object sender, EventArgs e)
{
TextBox giTextBox = (TextBox)sender;
if (!int.TryParse(giTextBox.Text, out int newValue))
return;
int GeIdx = (int)giTextBox.Tag;
_currentCharInfo.GeneralInfo[GeIdx] = newValue;
Savedata.SetCharacterInfo(_currentCharIndex, _currentCharInfo);
}
private void GeneralInfoTextBox_Leave(object sender, EventArgs e)
{
TextBox giTextBox = (TextBox)sender;
int GeIdx = (int)giTextBox.Tag;
giTextBox.Text = _currentCharInfo.GeneralInfo[GeIdx].ToString();
}
private void StatsTextBox_TextChanged(object sender, EventArgs e)
{
TextBox statTextBox = (TextBox)sender;
if (!ushort.TryParse(statTextBox.Text, out ushort newValue))
return;
int StIdx = (int)statTextBox.Tag;
_currentCharInfo.Stats[StIdx] = newValue;
Savedata.SetCharacterInfo(_currentCharIndex, _currentCharInfo);
}
private void StatsTextBox_Leave(object sender, EventArgs e)
{
TextBox statTextBox = (TextBox)sender;
int StIdx = (int)statTextBox.Tag;
statTextBox.Text = _currentCharInfo.Stats[StIdx].ToString();
}
private void UpdateGeStTextBoxes()
{
var charInfo = Savedata.GetCharacterInfo(_currentCharIndex);
_currentCharInfo = charInfo;
for (int i = 0; i < GeneralInfoTextBoxes.Length; i++)
GeneralInfoTextBoxes[i].Text = _currentCharInfo.GeneralInfo[i].ToString();
for (int i = 0; i < StatsTextBoxes.Length; i++)
StatsTextBoxes[i].Text = _currentCharInfo.Stats[i].ToString();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "SAVEDATA|SAVEDATA|*.*|*.*";
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;
if (!Savedata.LoadFromFile(openFileDialog1.FileName))
return;
_savedatafile = openFileDialog1.FileName;
saveAsToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = true;
tabControl1.Visible = true;
label2.Text = _savedatafile;
lstChar.SelectedIndex = -1;
TextBoxesSetEnabledState(false);
TextBoxesClearAll();
}
private void TextBoxesSetEnabledState(bool v)
{
foreach (var g in GeneralInfoTextBoxes)
g.Enabled = v;
foreach (var s in StatsTextBoxes)
s.Enabled = v;
}
private void TextBoxesClearAll()
{
foreach (var g in GeneralInfoTextBoxes)
g.Clear();
foreach (var s in StatsTextBoxes)
s.Clear();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "SAVEDATA|SAVEDATA|*.*|*.*";
sfd.FileName = "SAVEDATA";
if (sfd.ShowDialog() != DialogResult.OK)
return;
if (!Savedata.WriteToFile(sfd.FileName))
return;
MessageBox.Show("Saved as:\n" + sfd.FileName, "Saved", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
_savedatafile = sfd.FileName;
label2.Text = sfd.FileName;
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if(Savedata.WriteToFile(_savedatafile))
MessageBox.Show("Done.", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new AboutForm().ShowDialog();
}
private void lstChar_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstChar.SelectedIndex == -1)
return;
_currentCharIndex = lstChar.SelectedIndex;
TextBoxesSetEnabledState(true);
UpdateGeStTextBoxes();
}
}
}