Skip to content

Commit

Permalink
Some XAML optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Jedidja Bourgeois committed Sep 10, 2012
1 parent 571521f commit bab2399
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions LifeInMetro/CellMapDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Windows.UI;
using System.Collections.Generic;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
Expand All @@ -11,12 +12,22 @@ public class CellMapDisplay
private readonly int numberCellsAcross;
private readonly int numberCellsDown;

private readonly Dictionary<int, Rectangle> cellIndexDisplayMap;

private readonly SolidColorBrush blackBrush;
private readonly SolidColorBrush whiteBrush;

public CellMapDisplay(Canvas canvas, int numberCellsAcross, int numberCellsDown, int cellSize)
{
this.canvas = canvas;
this.numberCellsAcross = numberCellsAcross;
this.numberCellsDown = numberCellsDown;

cellIndexDisplayMap = new Dictionary<int, Rectangle>();

blackBrush = new SolidColorBrush(Colors.Black);
whiteBrush = new SolidColorBrush(Colors.White);

for (int y = 0; y < numberCellsDown; y++)
{
for (int x = 0; x < numberCellsAcross; x++)
Expand All @@ -30,15 +41,17 @@ public CellMapDisplay(Canvas canvas, int numberCellsAcross, int numberCellsDown,
Canvas.SetLeft(cell, x * cellSize);
Canvas.SetTop(cell, y * cellSize);
canvas.Children.Add(cell);

cellIndexDisplayMap[y * numberCellsAcross + x] = cell;
}
}
}

public void DrawCell(int x, int y, bool on)
{
var cellRectangle = canvas.Children[y * numberCellsAcross + x] as Rectangle;
var cellRectangle = cellIndexDisplayMap[y * numberCellsAcross + x];

cellRectangle.Fill = on ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Black);
cellRectangle.Fill = on ? whiteBrush : blackBrush;
}
}
}

0 comments on commit bab2399

Please sign in to comment.