Files
sfall/MPClient/EditorWindow.cs
T

70 lines
2.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace FalloutClient {
2019-04-04 10:22:02 +08:00
public enum DataType : int { None = 0, Int = 1, Float = 2, String = 3 }
public partial class EditorWindow : Form
{
private readonly DataType[] types;
private readonly string[] values;
private bool save;
2019-04-05 09:17:43 +08:00
private EditorWindow(string[] names, DataType[] types, string[] values, bool isMap) {
2019-04-04 10:22:02 +08:00
this.types = types;
this.values = values;
InitializeComponent();
dataGridView1.SuspendLayout();
2019-04-04 10:22:02 +08:00
if (names == null)
2019-04-05 09:17:43 +08:00
for (int i = 0; i < types.Length; i++) {
string element = i.ToString();
if (isMap && (i % 2) == 0) element += " - Key";
dataGridView1.Rows.Add(element, types[i].ToString(), values[i]);
}
2019-04-04 10:22:02 +08:00
else
2019-04-05 09:17:43 +08:00
for (int i = 0; i < types.Length; i++) dataGridView1.Rows.Add("[0x" + (i * 4).ToString("x").ToUpper() + "]" + names[i], types[i].ToString(), values[i]);
dataGridView1.ResumeLayout();
}
2019-04-05 09:17:43 +08:00
public static string[] ShowEditor(string[] names, DataType[] types, string[] values, bool isMap = false) {
EditorWindow editor = new EditorWindow(names, types, values, isMap);
editor.ShowDialog();
2019-04-04 10:22:02 +08:00
if (editor.save)
return editor.values;
else
return null;
}
private bool CheckInput(DataType type, string str) {
2019-04-04 10:22:02 +08:00
switch (type) {
case DataType.Int:
int i;
return int.TryParse(str, out i);
case DataType.Float:
float f;
return float.TryParse(str, out f);
}
return true;
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
2019-04-04 10:22:02 +08:00
string str = (string)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (CheckInput(types[e.RowIndex], str))
values[e.RowIndex] = str;
else
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = values[e.RowIndex];
}
private void bCancel_Click(object sender, EventArgs e) {
Close();
}
private void bSave_Click(object sender, EventArgs e) {
2019-04-04 10:22:02 +08:00
save = true;
Close();
}
}
}