Beispiel #1
0
        public void ChangeGlyph(GlyphFontViewModel glyphFontViewModel, int glyphIndex)
        {
            if (glyphFontViewModel == null)
            {
                throw new ArgumentNullException(nameof(glyphFontViewModel));
            }
            if (!_glyphFontStore.GlyphFonts.Contains(glyphFontViewModel))
            {
                throw new Exception("Chosen GlyphFont is not known in the GlyphFontStore.");
            }
            if (!glyphFontViewModel.IsValid)
            {
                throw new Exception("Cannot use an invalid GlyphFont.");
            }
            if (glyphIndex >= glyphFontViewModel.GlyphCount)
            {
                throw new Exception($"GlyphFont has no glyph at index {glyphIndex}.");
            }
            if (glyphFontViewModel.Equals(_glyphFontViewModel) && _glyphIndex == glyphIndex)
            {
                return;
            }

            var @event = new GlyphChangedEvent(_glyphFontViewModel, glyphFontViewModel, _glyphIndex, glyphIndex);

            _glyphFontViewModel = glyphFontViewModel;
            _glyphIndex         = glyphIndex;
            MessageBus.Publish(@event);
        }
        public static Int32Rect GetGlyphCropRectangle(this GlyphFontViewModel glyphFontViewModel, int glyphIndex)
        {
            var x = glyphIndex % glyphFontViewModel.ColumnCount;
            var y = glyphIndex / glyphFontViewModel.ColumnCount;

            return(new Int32Rect(x * glyphFontViewModel.GlyphSize.X, y * glyphFontViewModel.GlyphSize.Y, glyphFontViewModel.GlyphSize.X, glyphFontViewModel.GlyphSize.Y));
        }
Beispiel #3
0
        public void ChangeGlyph(GlyphFontViewModel glyphFontViewModel)
        {
            if (glyphFontViewModel == null)
            {
                throw new ArgumentNullException(nameof(glyphFontViewModel));
            }

            ChangeGlyph(glyphFontViewModel, _glyphIndex >= _glyphFontViewModel.GlyphCount ? 0 : _glyphIndex);
        }
Beispiel #4
0
 public void DetectGlyphFonts()
 {
     GlyphFonts = new HashSet <GlyphFontViewModel>();
     foreach (var file in Directory.GetFiles("Fonts", "*.png"))
     {
         var filename = Path.GetFullPath(file);
         var match    = FontFileRegex.Match(Path.GetFileNameWithoutExtension(filename));
         if (match.Success)
         {
             try
             {
                 var fontBitmap = BitmapUtils.LoadAndCleanGlyphFontBitmap(filename);
                 var width      = int.Parse(match.Groups["width"].Value);
                 var height     = int.Parse(match.Groups["height"].Value);
                 var fontName   = match.Groups["name"].Value;
                 if (fontBitmap.PixelWidth / width != 16 || fontBitmap.PixelHeight / height != 16 ||
                     fontBitmap.PixelWidth % width != 0 || fontBitmap.PixelHeight % height != 0)
                 {
                     GlyphFonts.Add(GlyphFontViewModel.CreateInvalid(filename,
                                                                     $"All fonts must fit exactly 16x16 characters: expected image of {16 * width}x{16 * height} but is {fontBitmap.PixelWidth}x{fontBitmap.PixelHeight}."));
                 }
                 else
                 {
                     var glyphFont = GlyphFontViewModel.Create(filename, fontName, new Point(width, height), fontBitmap);
                     GlyphFonts.Add(glyphFont);
                 }
             }
             catch (Exception e)
             {
                 GlyphFonts.Add(GlyphFontViewModel.CreateInvalid(filename, "Could not load font from file: " + e.Message));
             }
         }
         else
         {
             GlyphFonts.Add(GlyphFontViewModel.CreateInvalid(filename, "Filename must match pattern <name>_<charwidth>x<charheight>.png"));
         }
     }
     MessageBus.Publish(new GlyphFontListLoadedEvent(GlyphFonts.ToArray()));
 }