/// <summary>Construct an instance, building the positions dictionary.</summary> internal ArtisanGoodTextureManager(Texture2D texture, ArtisanGood good, int category) { this.spriteSheet = texture; this.artisanGoodIndex = (int)good; List <Tuple <int, string> > names = new List <Tuple <int, string> >(); //Get all item ids and names for a given category foreach (KeyValuePair <int, string> item in Game1.objectInformation) { string[] parts = item.Value.Split('/'); if (parts[3] == $"Basic {category}") { if (good != ArtisanGood.Honey || HoneyFlowerTypes.Contains(parts[0].Replace(" ", ""))) { names.Add(new Tuple <int, string>(item.Key, parts[0])); } } } if (good == ArtisanGood.Honey) { names.Add(new Tuple <int, string>((int)HoneyType.Wild, "Wild")); } //Sort names alphabetically names.Sort((T1, T2) => T1.Item2.CompareTo(T2.Item2)); //Get sprite positions, assuming alphabetical order and 4 sprites in each row int x = 0; int y = 0; foreach (Tuple <int, string> item in names) { this.positions[item.Item1] = new Rectangle(x, y, 16, 16); x += 16; if (x == 16 * 4) { x = 0; y += 16; } } }
internal ArtisanGoodTextureProvider(Texture2D texture, List <string> names, ArtisanGood good) { this.spriteSheet = texture; this.good = good; //Get sprite positions assuming names go left to right int x = 0; int y = 0; foreach (string item in names) { this.positions[item] = new Rectangle(x, y, 16, 16); x += 16; if (x >= texture.Width) { x = 0; y += 16; } } }
/// <summary>Tries to load a texture given the <see cref="IContentSource"/>, the path to the texture, the list of source names for it, and the good type.</summary> private static bool TryLoadTextureProvider(IContentSource contentSource, string imagePath, List <string> source, ArtisanGood good, IMonitor monitor, out ArtisanGoodTextureProvider provider) { provider = null; if (imagePath == null) { return(false); } IManifest manifest = contentSource.GetManifest(); if (source == null || source.Count == 0 || source.Any(item => item == null)) { monitor.Log($"Couldn't load {good} from {manifest.Name} ({manifest.UniqueID}) because it has an invalid source list ({artisanGoodToSourceType[good]}).", LogLevel.Warn); monitor.Log($"{artisanGoodToSourceType[good]} must not be null, must not be empty, and cannot have null items inside it.", LogLevel.Warn); } else { try { provider = new ArtisanGoodTextureProvider(contentSource.Load <Texture2D>(imagePath), source, good); return(true); } catch (Exception) { monitor.Log($"Couldn't load {good} from {manifest.Name} ({manifest.UniqueID}) because the {good} texture file path is invalid ({imagePath}).", LogLevel.Warn); } } return(false); }