using System; using System.Collections.Generic; using System.Drawing; using Blacklight.Core; using Glass; using Glass.Blacklight; using Glass.Text; using Konis.Blacklight; using Prism.Glass; namespace Konis.Testbed { /// /// Makes the pictures of sprites a list shows, drawing from the sheets that particles draw from. /// public class SpritePreviews { private const int BackdropCell = 8; private readonly BlacklightRenderer renderer; private readonly SpriteResolver sprites; private readonly Glass.Material backdrop; private readonly Dictionary attributes = new Dictionary(); /// /// Makes previews that register their sheets with , and take each sheet from /// , so a sheet that particles already use isn't uploaded again. The checkerboard /// behind a picture is a texture of , which must belong /// to. /// public SpritePreviews(WindowHandle window, BlacklightRenderer renderer, SpriteResolver sprites) { if (window == null) { throw new ArgumentNullException(nameof(window)); } this.renderer = renderer ?? throw new ArgumentNullException(nameof(renderer)); this.sprites = sprites ?? throw new ArgumentNullException(nameof(sprites)); var size = new Size(SpriteCell.PictureSide, SpriteCell.PictureSide); Texture texture = window.CreateTexture( new DecodedImage(PickerArt.Backdrop(size.Width, size.Height, BackdropCell), size), generateMipmaps: false); backdrop = new TextureMaterial { Texture = new BlacklightGlassTexture { Texture = texture } }; } // Glass finds a sheet by its attribute, so each sheet gets one that no other can equal. private Glass.SpriteSheetAttribute AttributeOf(SpriteSheetDef sheet) { if (!attributes.TryGetValue(sheet, out Glass.SpriteSheetAttribute attribute)) { attribute = new Glass.SpriteSheetAttribute( sheet.SpriteWidth, sheet.SpriteHeight, sheet.BorderSize, "Konis.Testbed.Preview." + attributes.Count); renderer.Register( attribute, new SpriteSheetRenderer { Source = attribute, SpriteSheet = sprites.SheetOf(sheet) }); attributes.Add(sheet, attribute); } return attribute; } /// /// Returns a material that draws the first frame of . /// /// The def is null. /// The def or its image can't work. public SpriteMaterial MaterialFor(SpriteDef def) { if (def == null) { throw new ArgumentNullException(nameof(def)); } def.Validate(); SheetSpriteDef frame = def.Frames[0]; return SpriteMaterial.CreateSprite( AttributeOf(frame.Sheet), new Glass.Point(frame.Column, frame.Row), debugName: def.Name); } /// /// Returns a cell that shows the picture of with its label beneath, in . /// public SpriteCell CellFor(SpriteChoice choice, TextMaterialStyle captionStyle) { if (choice == null) { throw new ArgumentNullException(nameof(choice)); } var caption = new Label(captionStyle, choice.Label, Constraints.Unbounded); return new SpriteCell(backdrop, MaterialFor(choice.Def), caption); } } }