From db989e98010a36bedce33fed7fcb6ea4274c543b Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Mon, 28 Oct 2019 17:04:42 +0100 Subject: [PATCH] Zmiana rozmiaru planszy --- Life/App.xaml | 3 +- Life/Automaton/BasicField.cs | 16 +++++++++- Life/Automaton/IField.cs | 2 ++ Life/AutomatonField.xaml.cs | 6 ++-- Life/MainWindow.xaml | 28 +++++++++-------- Life/ViewModel/AutomatonViewModel.cs | 45 ++++++++++++++++++++++++++++ Life/ViewModel/FieldViewModel.cs | 13 ++++++-- 7 files changed, 93 insertions(+), 20 deletions(-) diff --git a/Life/App.xaml b/Life/App.xaml index 28bcbde..0dc76a5 100644 --- a/Life/App.xaml +++ b/Life/App.xaml @@ -7,12 +7,11 @@ diff --git a/Life/Automaton/BasicField.cs b/Life/Automaton/BasicField.cs index f2afbda..ac32053 100644 --- a/Life/Automaton/BasicField.cs +++ b/Life/Automaton/BasicField.cs @@ -24,7 +24,7 @@ namespace Life.Automaton private Cell[,] _cells; - public Size Size { get; } + public Size Size { get; private set; } public Cell this[int x, int y] { @@ -52,6 +52,20 @@ namespace Life.Automaton _observers.ForEach(observer => observer.OnNext(this)); } + public void Resize(int width, int height) + { + var cells = new Cell[width, height]; + + for (int x = 0; x < width; x++) + for (int y = 0; y < height; y++) + cells[x, y] = (x >= Size.Width || y >= Size.Height) ? new Cell() : _cells[x, y]; + + Size = new Size(width, height); + _cells = cells; + + Notify(); + } + public IDisposable Subscribe(IObserver observer) { _observers.Add(observer); diff --git a/Life/Automaton/IField.cs b/Life/Automaton/IField.cs index 6b25e1d..1107314 100644 --- a/Life/Automaton/IField.cs +++ b/Life/Automaton/IField.cs @@ -18,5 +18,7 @@ namespace Life.Automaton void Transform(Rule rule); void Notify(); + + void Resize(int width, int height); } } \ No newline at end of file diff --git a/Life/AutomatonField.xaml.cs b/Life/AutomatonField.xaml.cs index afe1d5a..a7dd19f 100644 --- a/Life/AutomatonField.xaml.cs +++ b/Life/AutomatonField.xaml.cs @@ -64,13 +64,13 @@ namespace Life #region Dependency Properties public static readonly DependencyProperty SizeProperty = DependencyProperty.Register( - "Size", + nameof(Size), typeof(int), typeof(AutomatonField), new PropertyMetadata(OnSizeChanged) ); public static readonly DependencyProperty SeparationProperty = DependencyProperty.Register( - "Separation", + nameof(Separation), typeof(int), typeof(AutomatonField), new PropertyMetadata(OnSeparationChanged) ); @@ -80,7 +80,7 @@ namespace Life typeof(IField), typeof(AutomatonField), new PropertyMetadata(OnFieldChanged) ); - + public static readonly DependencyProperty ClickCommandProperty = DependencyProperty.Register( nameof(ClickCommand), typeof(ICommand), typeof(AutomatonField) diff --git a/Life/MainWindow.xaml b/Life/MainWindow.xaml index 8d9aa62..0352c0b 100644 --- a/Life/MainWindow.xaml +++ b/Life/MainWindow.xaml @@ -30,23 +30,27 @@ - - Iteracja + + Iteration + + + Size + + + + + + - Rozmiar - - - - - - + Cell Size + diff --git a/Life/ViewModel/AutomatonViewModel.cs b/Life/ViewModel/AutomatonViewModel.cs index 4cd47c9..cdfec2e 100644 --- a/Life/ViewModel/AutomatonViewModel.cs +++ b/Life/ViewModel/AutomatonViewModel.cs @@ -13,9 +13,23 @@ namespace Life.ViewModel { private int _size; private int _separation; + private int _width; + private int _height; private IAutomaton _automaton; private IFormatter _formatter = new BinaryFormatter(); + public AutomatonViewModel() + { + PropertyChanged += (sender, args) => + { + if (args.PropertyName == nameof(Field)) + { + Width = Field.Size.Width; + Height = Field.Size.Height; + } + }; + } + public int Size { get => _size; @@ -35,6 +49,26 @@ namespace Life.ViewModel OnPropertyChanged(nameof(Separation)); } } + + public int Width + { + get => _width; + set + { + _width = value; + OnPropertyChanged(nameof(Width)); + } + } + + public int Height + { + get => _height; + set + { + _height = value; + OnPropertyChanged(nameof(Height)); + } + } public IAutomaton Automaton { @@ -54,6 +88,11 @@ namespace Life.ViewModel { Action = HandleCellClick }; + + public ICommand ChangeSizeCommand => new DelegateCommand + { + Action = HandleSizeChange + }; public ICommand LoadCommand => new DelegateCommand { @@ -83,6 +122,12 @@ namespace Life.ViewModel Field.Notify(); } + private void HandleSizeChange(object obj) + { + Field.Resize(Width, Height); + OnPropertyChanged(nameof(Field)); + } + private void HandleLoadCommand(object _) { OpenFileDialog dialog = new OpenFileDialog(); diff --git a/Life/ViewModel/FieldViewModel.cs b/Life/ViewModel/FieldViewModel.cs index 6ee5bfe..62751ac 100644 --- a/Life/ViewModel/FieldViewModel.cs +++ b/Life/ViewModel/FieldViewModel.cs @@ -7,9 +7,11 @@ namespace Life.ViewModel { public class FieldViewModel : BaseViewModel { + private IField _field; + + private Size _oldSize; private int _width; private int _height; - private IField _field; private int _size; private int _separation; private IDisposable _fieldObserverDisposal; @@ -46,7 +48,8 @@ namespace Life.ViewModel { Action = field => Sync() }); - + + _oldSize = value.Size; _field = value; ResetFields(); } @@ -107,6 +110,12 @@ namespace Life.ViewModel public void Sync() { + if (!Equals(_oldSize, Field.Size)) + { + _oldSize = Field.Size; + ResetFields(); + } + foreach (var cell in Cells) { cell.IsAlive = Field[cell.Position.X, cell.Position.Y].IsAlive;