Zmiana rozmiaru planszy

This commit is contained in:
Kacper Donat 2019-10-28 17:04:42 +01:00
parent 9b0313201b
commit db989e9801
7 changed files with 93 additions and 20 deletions

View File

@ -7,12 +7,11 @@
<Style x:Key="Header" TargetType="TextBlock">
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Medium"/>
<Setter Property="Margin" Value="0 0 0 0"/>
<Setter Property="Margin" Value="0 0 18 0"/>
</Style>
<Style x:Key="Value" TargetType="TextBlock">
<Setter Property="FontSize" Value="24" />
<Setter Property="FontWeight" Value="Black"/>
<Setter Property="Margin" Value="18 0 0 0"></Setter>
</Style>
</Application.Resources>
</Application>

View File

@ -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<IField> observer)
{
_observers.Add(observer);

View File

@ -18,5 +18,7 @@ namespace Life.Automaton
void Transform(Rule rule);
void Notify();
void Resize(int width, int height);
}
}

View File

@ -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)

View File

@ -30,23 +30,27 @@
<GridSplitter Grid.Column="1" Width="3" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"/>
<StackPanel Orientation="Vertical" Margin="8" Grid.Column="2" HorizontalAlignment="Stretch">
<DockPanel HorizontalAlignment="Stretch">
<TextBlock Style="{StaticResource Header}" VerticalAlignment="Center">Iteracja</TextBlock>
<DockPanel HorizontalAlignment="Stretch" Margin="0 0 0 8">
<TextBlock Style="{StaticResource Header}" VerticalAlignment="Center">Iteration</TextBlock>
<TextBlock Style="{StaticResource Value}" Text="{Binding Iteration}" VerticalAlignment="Stretch"/>
<Button VerticalAlignment="Center" Command="{Binding StepCommand}" HorizontalAlignment="Right">
<TextBlock Margin="4">krok</TextBlock>
<TextBlock Margin="4">step</TextBlock>
</Button>
</DockPanel>
<DockPanel HorizontalAlignment="Stretch" Margin="0 0 0 8">
<TextBlock Style="{StaticResource Header}" VerticalAlignment="Center" DockPanel.Dock="Left">Size</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<TextBox Text="{Binding Width, Mode=TwoWay}"/>
<TextBlock Text=" x "/>
<TextBox Text="{Binding Height, Mode=TwoWay}"/>
</StackPanel>
<Button VerticalAlignment="Center" Command="{Binding ChangeSizeCommand}" DockPanel.Dock="Right" HorizontalAlignment="Right">
<TextBlock Margin="2">change</TextBlock>
</Button>
</DockPanel>
<DockPanel HorizontalAlignment="Stretch">
<TextBlock Style="{StaticResource Header}" VerticalAlignment="Center">Rozmiar</TextBlock>
<StackPanel Orientation="Horizontal" HorizontalAlignment="center" VerticalAlignment="Center">
<TextBox Text="{Binding Field.Size.Width, Mode=OneWay}"/>
<TextBlock Text=" x "/>
<TextBox Text="{Binding Field.Size.Height, Mode=OneWay}"/>
</StackPanel>
<Button VerticalAlignment="Center" Command="{Binding StepCommand}" HorizontalAlignment="Right">
<TextBlock Margin="4">Zapisz</TextBlock>
</Button>
<TextBlock Style="{StaticResource Header}" VerticalAlignment="Center" DockPanel.Dock="Left">Cell Size</TextBlock>
<Slider Minimum="8" Maximum="32" Value="{Binding Size}" VerticalAlignment="Center" />
</DockPanel>
</StackPanel>
</Grid>

View File

@ -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<object>
{
Action = HandleSizeChange
};
public ICommand LoadCommand => new DelegateCommand<object>
{
@ -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();

View File

@ -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;