Sudoku/Complete/Models/NumberBrushes.cs

41 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Media;
using SudokuWeek4.Models;
namespace SudokuWeek4
{
internal static class NumberBrushes
{
// Create a dictionary for the cached brushes (per cell)
private static readonly Dictionary<string, DrawingBrush> cachedBrushes = new Dictionary<string, DrawingBrush>();
public static DrawingBrush GetBrush(int number, Color colorBrush)
{
// Check if the index is out of bounds
if (number < 0 || number > BoardModel.GameFieldSize)
throw new IndexOutOfRangeException();
// Get the cached brush (if exists)
string key = number + colorBrush.ToString();
DrawingBrush brush;
if (cachedBrushes.TryGetValue(key, out brush))
return brush;
// Create a new brush (not in cache)
FormattedText formtxt = new FormattedText(number.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Times New Roman"), 100, Brushes.Black);
Geometry textGeometry = formtxt.BuildGeometry(new Point(0, 0));
brush = new DrawingBrush(new GeometryDrawing(new SolidColorBrush(colorBrush), null, textGeometry));
brush.Stretch = Stretch.Uniform;
cachedBrushes[key] = brush;
return brush;
}
}
}