focustense / StardewUI

UI/widget library for Stardew modding
MIT License
6 stars 1 forks source link

Mod-provided sprite assets #7

Closed focustense closed 1 month ago

focustense commented 1 month ago

Any Sprite asset can be used in a binding, and there are implicit conversions for things that often translate to sprites like Texture2D, Tuple<Texture2D, Rectangle> and ParsedItemData, but it's hard to represent the entire sprite structure with just implicit conversions, and there isn't an easy way for a mod to actually register a Sprite asset.

Plan: Use JSON data to register "sprite sheets", with a single texture reference for each, either pointing implicitly to a texture file of the same name or to an external, named game asset.

e.g. for a vanilla sprite sheet:

{
  "Texture": "LooseSprites/Cursors",
  "Sprites": [
    "BackpackTab": {
      "SourceRect": "0,368,16,16",
      "FixedEdges": "4,4,4,1",
      "Scale": 4
    }
    "SocialTab": {
      "SourceRect": "32,368,16,16",
      "FixedEdges": "4,4,4,1",
      "Scale": 4
    }
  ]
}

For a modded sprite sheet, the format is the same except that we omit the Texture and have two file names that differ only by extension, e.g.

When registered through the API - which is much easier than doing a manual asset handler because only StardewUI has the actual Sprite type and knows how to convert this data - it will register a handler for both the Texture2D asset as well as the Sprite asset that points to that texture and is invalidated when the texture is invalidated.

e.g. ViewEngine.AddSprites("Mods/MyMod/Sprites", "assets/sprites") would allow all sprites in the directory to be referenced with the asset prefix, so in the example above, we could then use an asset binding @Mods/MyMod/Sprites/Cursors/BackpackTab (mod reference to vanilla texture) or @Mods/MyMod/Sprites/UI/ExampleSprite to refer to an ExampleSprite key in the assets/sprites/ui.json which uses pixels from the assets/sprites/ui.png image.