// 解析ボタン
var anlyzeButton = new Button(10, 50, "解析");
anlyzeButton.Show();
asd.Engine.AddObject2D(anlyzeButton.getBackTexture());
asd.Engine.AddObject2D(anlyzeButton.getTextObject());
buttons.Add(anlyzeButton);
anlyzeButton.SetAction(() =>
{
});
// セーブボタン
var saveButton = new Button(10, 90, "セーブ");
saveButton.Show();
asd.Engine.AddObject2D(saveButton.getBackTexture());
asd.Engine.AddObject2D(saveButton.getTextObject());
buttons.Add(saveButton);
saveButton.SetAction(() =>
{
FileAccess.Save(rowNumberSquare, colNumberSquare);
});
// ロードボタン
var loadButton = new Button(10, 130, "ロード");
loadButton.Show();
asd.Engine.AddObject2D(loadButton.getBackTexture());
asd.Engine.AddObject2D(loadButton.getTextObject());
buttons.Add(loadButton);
loadButton.SetAction(() =>
{
FileAccess.Load(ref rowNumberSquare, ref colNumberSquare);
});
入力データを保存、読み取りするコードを書いていきます。
[JsonObject("dataModel")]
class Data
{
[JsonProperty("rowdata")]
public List<List<int>> RowData { get; set; }
[JsonProperty("coldata")]
public List<List<int>> ColData { get; set; }
}
class FileAccess
{
private const string _filename = "save.dat";
public static void Save(List<List<NumberSquare>> row, List<List<NumberSquare>> col)
{
if (File.Exists(_filename) == true)
{
File.Delete(_filename);
}
Data data = new Data();
data.RowData = new List<List<int>>();
foreach(var r in row)
{
List<int> list = new List<int>();
foreach(var s in r)
{
if(s.GetValue() != 0)
{
list.Add(s.GetValue());
}
}
data.RowData.Add(list);
}
data.ColData = new List<List<int>>();
foreach (var c in col)
{
List<int> list = new List<int>();
foreach (var s in c)
{
if (s.GetValue() != 0)
{
list.Add(s.GetValue());
}
}
data.ColData.Add(list);
}
var str = JsonConvert.SerializeObject(data);
using (var stream = new StreamWriter(_filename, true))
{
stream.Write(str);
}
}
public static void Load(ref List<List<NumberSquare>> rowList, ref List<List<NumberSquare>> colList)
{
foreach(var r in rowList)
{
foreach(var s in r)
{
asd.Engine.RemoveObject2D(s.getBackTexture());
asd.Engine.RemoveObject2D(s.getTextObject());
}
}
foreach (var c in colList)
{
foreach (var s in c)
{
asd.Engine.RemoveObject2D(s.getBackTexture());
asd.Engine.RemoveObject2D(s.getTextObject());
}
}
string str = string.Empty;
using (var stream = new StreamReader(_filename, true))
{
str = stream.ReadToEnd();
}
var data = JsonConvert.DeserializeObject<Data>(str);
rowList = new List<List<NumberSquare>>();
{
int row = 0;
foreach (var r in data.RowData)
{
List<NumberSquare> list = new List<NumberSquare>();
int col = -1;
foreach (var v in r)
{
var s = new NumberSquare(row, col);
s.SetValue(v.ToString());
asd.Engine.AddObject2D(s.getBackTexture());
asd.Engine.AddObject2D(s.getTextObject());
list.Add(s);
col--;
}
{
var s = new NumberSquare(row, col);
s.SetValue("0");
asd.Engine.AddObject2D(s.getBackTexture());
asd.Engine.AddObject2D(s.getTextObject());
list.Add(s);
}
rowList.Add(list);
row++;
}
}
colList = new List<List<NumberSquare>>();
{
int col = 0;
foreach (var c in data.ColData)
{
List<NumberSquare> list = new List<NumberSquare>();
int row = -1;
foreach (var v in c)
{
var s = new NumberSquare(row, col);
s.SetValue(v.ToString());
asd.Engine.AddObject2D(s.getBackTexture());
asd.Engine.AddObject2D(s.getTextObject());
list.Add(s);
row--;
}
{
var s = new NumberSquare(row, col);
s.SetValue("0");
asd.Engine.AddObject2D(s.getBackTexture());
asd.Engine.AddObject2D(s.getTextObject());
list.Add(s);
}
colList.Add(list);
col++;
}
}
}
}