1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using GameOfLife.CellClasses;
  15.  
  16. namespace GameOfLife
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         const int GridWidth = 100;
  24.         const int GridHeight = GridWidth;
  25.         const int QuadLength = 12;
  26.  
  27.         private RenderTargetBitmap buffer;
  28.         private DrawingVisual drawingVisual;
  29.         private Color BackgroundColor;
  30.         private Color ForegroundColor;
  31.         private Color GridColor;
  32.  
  33.         private CellGrid grid;
  34.  
  35.         public MainWindow()
  36.         {
  37.             InitializeComponent();
  38.             grid = new CellGrid(GridWidth, GridHeight);
  39.             drawingVisual = new DrawingVisual();
  40.             buffer = new RenderTargetBitmap(GridWidth * QuadLength, GridHeight * QuadLength, 96, 96, PixelFormats.Pbgra32);
  41.             display.Source = buffer;
  42.  
  43.  
  44.             BackgroundColor = Colors.Wheat;
  45.             ForegroundColor = Colors.Black;
  46.             GridColor = Colors.Green;
  47.  
  48.             Draw();
  49.         }
  50.  
  51.         private void Draw()
  52.         {
  53.             if (drawingVisual == null)
  54.                 return;
  55.             using (DrawingContext drawingContext = drawingVisual.RenderOpen())
  56.             {
  57.                 drawingContext.DrawRectangle(new SolidColorBrush(GridColor), null, new Rect(0, 0, GridWidth * QuadLength, GridHeight * QuadLength));
  58.                 for (int x = 0; x < GridWidth; x++)
  59.                 {
  60.                     for (int y = 0; y < GridHeight; y++)
  61.                     {
  62.                         if (grid[x, y].Alive)
  63.                         {
  64.                             drawingContext.DrawRectangle(new SolidColorBrush(ForegroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
  65.                         }
  66.                         else
  67.                         {
  68.                             drawingContext.DrawRectangle(new SolidColorBrush(BackgroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.  
  74.             buffer.Render(drawingVisual);
  75.         }
  76.         private void Draw(Point cell)
  77.         {
  78.             if (drawingVisual == null)
  79.                 return;
  80.             using (DrawingContext drawingContext = drawingVisual.RenderOpen())
  81.             {
  82.                 int x = (int)cell.X;
  83.                 int y = (int)cell.Y;
  84.                 drawingContext.DrawRectangle(new SolidColorBrush(GridColor), null, new Rect(x * QuadLength, y * QuadLength, QuadLength, QuadLength));
  85.                 if (grid[x, y].Alive)
  86.                 {
  87.                     drawingContext.DrawRectangle(new SolidColorBrush(ForegroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
  88.                 }
  89.                 else
  90.                 {
  91.                     drawingContext.DrawRectangle(new SolidColorBrush(BackgroundColor), null, new Rect((x * QuadLength) + 1, (y * QuadLength) + 1, QuadLength - 1, QuadLength - 1));
  92.                 }
  93.             }
  94.  
  95.             buffer.Render(drawingVisual);
  96.         }
  97.         private void SetCell(ColorTypes type, Point p)
  98.         {
  99.             Cell i = grid[(int)p.X, (int)p.Y];
  100.             i.Alive = (type == ColorTypes.Primary);
  101.             grid[(int)p.X, (int)p.Y] = i;
  102.             Draw(p);
  103.         }
  104.  
  105.         private void PassGeneration_Click(object sender, RoutedEventArgs e)
  106.         {
  107.             grid.PassGeneration(RuleSet.Conway);
  108.             Draw();
  109.         }
  110.         private void Invert_Click(object sender, RoutedEventArgs e)
  111.         {
  112.             grid.GenerateNegetive();
  113.             Draw();
  114.         }
  115.         private void display_MouseMove(object sender, MouseEventArgs e)
  116.         {
  117.             Point p = e.GetPosition(display);
  118.             p.X = Math.Floor(p.X / QuadLength);
  119.             p.Y = Math.Floor(p.Y / QuadLength);
  120.            
  121.             if (e.LeftButton == MouseButtonState.Pressed)
  122.             {
  123.                 SetCell(ColorTypes.Primary, p);
  124.             }
  125.             else if (e.RightButton == MouseButtonState.Pressed)
  126.             {
  127.                 SetCell(ColorTypes.Secondry, p);
  128.             }
  129.         }
  130.         private void display_MouseDown(object sender, MouseButtonEventArgs e)
  131.         {
  132.             Point p = e.GetPosition(display);
  133.             p.X = Math.Floor(p.X / QuadLength);
  134.             p.Y = Math.Floor(p.Y / QuadLength);
  135.  
  136.             if (e.LeftButton == MouseButtonState.Pressed)
  137.             {
  138.                 SetCell(ColorTypes.Primary, p);
  139.             }
  140.             else if (e.RightButton == MouseButtonState.Pressed)
  141.             {
  142.                 SetCell(ColorTypes.Secondry, p);
  143.             }
  144.         }
  145.  
  146.         enum ColorTypes
  147.         {
  148.             Primary,
  149.             Secondry
  150.         }
  151.     }
  152. }