112 lines
2.8 KiB
C#
112 lines
2.8 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Runtime.Serialization;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Windows.Input;
|
|
using Life.Automaton;
|
|
using Life.Misc;
|
|
using Microsoft.Win32;
|
|
|
|
namespace Life.ViewModel
|
|
{
|
|
public class AutomatonViewModel : BaseViewModel
|
|
{
|
|
private int _size;
|
|
private int _separation;
|
|
private IAutomaton _automaton;
|
|
private IFormatter _formatter = new BinaryFormatter();
|
|
|
|
public int Size
|
|
{
|
|
get => _size;
|
|
set
|
|
{
|
|
_size = value;
|
|
OnPropertyChanged(nameof(Size));
|
|
}
|
|
}
|
|
|
|
public int Separation
|
|
{
|
|
get => _separation;
|
|
set
|
|
{
|
|
_separation = value;
|
|
OnPropertyChanged(nameof(Separation));
|
|
}
|
|
}
|
|
|
|
public IAutomaton Automaton
|
|
{
|
|
get => _automaton;
|
|
set
|
|
{
|
|
_automaton = value;
|
|
OnPropertyChanged(nameof(Field));
|
|
OnPropertyChanged(nameof(Iteration));
|
|
}
|
|
}
|
|
|
|
public IField Field => Automaton.Field;
|
|
public int Iteration => Automaton.Iteration;
|
|
|
|
public ICommand CellClicked => new DelegateCommand<Position>
|
|
{
|
|
Action = HandleCellClick
|
|
};
|
|
|
|
public ICommand LoadCommand => new DelegateCommand<object>
|
|
{
|
|
Action = HandleLoadCommand
|
|
};
|
|
|
|
public ICommand SaveCommand => new DelegateCommand<object>
|
|
{
|
|
Action = HandleSaveCommand
|
|
};
|
|
|
|
public ICommand StepCommand => new DelegateCommand<object>
|
|
{
|
|
Action = Step
|
|
};
|
|
|
|
private void Step(object param)
|
|
{
|
|
Automaton.Advance();
|
|
|
|
OnPropertyChanged(nameof(Iteration));
|
|
}
|
|
|
|
private void HandleCellClick(Position position)
|
|
{
|
|
Field[position.X, position.Y].IsAlive = !Field[position.X, position.Y].IsAlive;
|
|
Field.Notify();
|
|
}
|
|
|
|
private void HandleLoadCommand(object _)
|
|
{
|
|
OpenFileDialog dialog = new OpenFileDialog();
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
using var file = dialog.OpenFile();
|
|
var field = _formatter.Deserialize(file) as IField;
|
|
|
|
Automaton.ReplaceField(field);
|
|
|
|
OnPropertyChanged(nameof(Field));
|
|
}
|
|
}
|
|
|
|
private void HandleSaveCommand(object _)
|
|
{
|
|
SaveFileDialog dialog = new SaveFileDialog();
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
using var file = dialog.OpenFile();
|
|
_formatter.Serialize(file, Field);
|
|
}
|
|
}
|
|
}
|
|
} |