Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using GameOfLife.CellClasses;
- namespace GameOfLife
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- const int GridWidth = 100;
- const int GridHeight = GridWidth;
- const int QuadLength = 12;
- private RenderTargetBitmap buffer;
- private DrawingVisual drawingVisual;
- private Color BackgroundColor;
- private Color ForegroundColor;
- private Color GridColor;
- private CellGrid grid;
- public MainWindow()
- {
- InitializeComponent();
- grid = new CellGrid(GridWidth, GridHeight);
- drawingVisual = new DrawingVisual();
- buffer = new RenderTargetBitmap(GridWidth * QuadLength, GridHeight * QuadLength, 96, 96, PixelFormats.Pbgra32);
- display.Source = buffer;
- BackgroundColor = Colors.Wheat;
- ForegroundColor = Colors.Black;
- GridColor = Colors.Green;
- Draw();
- }
- private void Draw()
- {
- if (drawingVisual == null)
- return;
- using (DrawingContext drawingContext = drawingVisual.RenderOpen())
- {
- drawingContext.DrawRectangle(new SolidColorBrush(GridColor), null, new Rect(0, 0, GridWidth * QuadLength, GridHeight * QuadLength));
- for (int x = 0; x < GridWidth; x++)
- {
- for (int y = 0; y < GridHeight; y++)
- {
- if (grid[x, y].Alive)
- {
- drawingContext.DrawRectangle(new SolidColorBrush(ForegroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
- }
- else
- {
- drawingContext.DrawRectangle(new SolidColorBrush(BackgroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
- }
- }
- }
- }
- buffer.Render(drawingVisual);
- }
- private void Draw(Point cell)
- {
- if (drawingVisual == null)
- return;
- using (DrawingContext drawingContext = drawingVisual.RenderOpen())
- {
- int x = (int)cell.X;
- int y = (int)cell.Y;
- drawingContext.DrawRectangle(new SolidColorBrush(GridColor), null, new Rect(x * QuadLength, y * QuadLength, QuadLength, QuadLength));
- if (grid[x, y].Alive)
- {
- drawingContext.DrawRectangle(new SolidColorBrush(ForegroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
- }
- else
- {
- drawingContext.DrawRectangle(new SolidColorBrush(BackgroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
- }
- }
- buffer.Render(drawingVisual);
- }
- private void SetCell(ColorTypes type, Point p)
- {
- Cell i = grid[(int)p.X, (int)p.Y];
- i.Alive = (type == ColorTypes.Primary);
- grid[(int)p.X, (int)p.Y] = i;
- Draw(p);
- }
- private void PassGeneration_Click(object sender, RoutedEventArgs e)
- {
- grid.PassGeneration(RuleSet.Conway);
- Draw();
- }
- private void Invert_Click(object sender, RoutedEventArgs e)
- {
- grid.GenerateNegetive();
- Draw();
- }
- private void display_MouseMove(object sender, MouseEventArgs e)
- {
- Point p = e.GetPosition(display);
- p.X = Math.Floor(p.X / QuadLength);
- p.Y = Math.Floor(p.Y / QuadLength);
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- SetCell(ColorTypes.Primary, p);
- }
- else if (e.RightButton == MouseButtonState.Pressed)
- {
- SetCell(ColorTypes.Secondry, p);
- }
- }
- private void display_MouseDown(object sender, MouseButtonEventArgs e)
- {
- Point p = e.GetPosition(display);
- p.X = Math.Floor(p.X / QuadLength);
- p.Y = Math.Floor(p.Y / QuadLength);
- if (e.LeftButton == MouseButtonState.Pressed)
- {
- SetCell(ColorTypes.Primary, p);
- }
- else if (e.RightButton == MouseButtonState.Pressed)
- {
- SetCell(ColorTypes.Secondry, p);
- }
- }
- enum ColorTypes
- {
- Primary,
- Secondry
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement