2018-07-15 00:53:37 +00:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using PKHeX.Core;
|
2019-09-29 16:47:06 +00:00
|
|
|
|
using PKHeX.Drawing.Properties;
|
2018-07-15 00:53:37 +00:00
|
|
|
|
|
2019-09-29 16:47:06 +00:00
|
|
|
|
namespace PKHeX.Drawing
|
2018-07-15 00:53:37 +00:00
|
|
|
|
{
|
2019-10-04 02:09:02 +00:00
|
|
|
|
public sealed class SpriteBuilder : ISpriteBuilder<Image>
|
2018-07-15 00:53:37 +00:00
|
|
|
|
{
|
2018-08-19 23:45:20 +00:00
|
|
|
|
public static bool ShowEggSpriteAsItem { get; set; } = true;
|
|
|
|
|
|
2019-04-23 05:24:29 +00:00
|
|
|
|
private const int ItemShiftX = 22;
|
|
|
|
|
private const int ItemShiftY = 15;
|
|
|
|
|
private const int ItemMaxSize = 15;
|
|
|
|
|
private const int EggItemShiftX = 9;
|
|
|
|
|
private const int EggItemShiftY = 2;
|
|
|
|
|
private const double UnknownFormTransparency = 0.5;
|
|
|
|
|
private const double ShinyTransparency = 0.7;
|
|
|
|
|
private const double EggUnderLayerTransparency = 0.33;
|
|
|
|
|
|
2018-07-15 00:53:37 +00:00
|
|
|
|
public void Initialize(SaveFile sav)
|
|
|
|
|
{
|
|
|
|
|
if (sav.Generation != 3)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Game = sav.Version;
|
|
|
|
|
if (Game == GameVersion.FRLG)
|
|
|
|
|
Game = sav.Personal == PersonalTable.FR ? GameVersion.FR : GameVersion.LG;
|
|
|
|
|
}
|
2018-07-22 02:20:11 +00:00
|
|
|
|
|
2018-07-15 00:53:37 +00:00
|
|
|
|
private GameVersion Game;
|
|
|
|
|
|
|
|
|
|
private static int GetDeoxysForm(GameVersion game)
|
|
|
|
|
{
|
2019-09-29 16:47:06 +00:00
|
|
|
|
return game switch
|
2018-07-15 00:53:37 +00:00
|
|
|
|
{
|
2019-09-29 16:47:06 +00:00
|
|
|
|
GameVersion.FR => 1, // Attack
|
|
|
|
|
GameVersion.LG => 2, // Defense
|
|
|
|
|
GameVersion.E => 3, // Speed
|
|
|
|
|
_ => 0
|
|
|
|
|
};
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Image GetSprite(int species, int form, int gender, int heldItem, bool isEgg, bool isShiny, int generation = -1, bool isBoxBGRed = false)
|
|
|
|
|
{
|
|
|
|
|
if (species == 0)
|
|
|
|
|
return Resources._0;
|
|
|
|
|
|
|
|
|
|
if (generation == 3 && species == 386) // Deoxys, special consideration for Gen3 save files
|
|
|
|
|
form = GetDeoxysForm(Game);
|
|
|
|
|
|
|
|
|
|
var baseImage = GetBaseImage(species, form, gender, isShiny, generation);
|
|
|
|
|
|
2018-10-21 04:05:15 +00:00
|
|
|
|
return GetSprite(baseImage, species, heldItem, isEgg, isShiny, generation, isBoxBGRed);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-27 15:53:09 +00:00
|
|
|
|
public Image GetSprite(Image baseSprite, int species, int heldItem, bool isEgg, bool isShiny, int generation = -1, bool isBoxBGRed = false)
|
2018-10-21 04:05:15 +00:00
|
|
|
|
{
|
2018-07-15 00:53:37 +00:00
|
|
|
|
if (isEgg)
|
2018-10-27 15:53:09 +00:00
|
|
|
|
baseSprite = LayerOverImageEgg(baseSprite, species, heldItem != 0);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
if (isShiny)
|
2018-10-27 15:53:09 +00:00
|
|
|
|
baseSprite = LayerOverImageShiny(baseSprite, isBoxBGRed);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
if (heldItem > 0)
|
2018-10-27 15:53:09 +00:00
|
|
|
|
baseSprite = LayerOverImageItem(baseSprite, heldItem, generation);
|
|
|
|
|
return baseSprite;
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Image GetBaseImage(int species, int form, int gender, bool shiny, int generation)
|
|
|
|
|
{
|
|
|
|
|
var img = FormConverter.IsTotemForm(species, form)
|
|
|
|
|
? GetBaseImageTotem(species, form, gender, shiny, generation)
|
|
|
|
|
: GetBaseImageDefault(species, form, gender, shiny, generation);
|
|
|
|
|
return img ?? GetBaseImageFallback(species, form, gender, shiny, generation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Image GetBaseImageTotem(int species, int form, int gender, bool shiny, int generation)
|
|
|
|
|
{
|
|
|
|
|
var baseform = FormConverter.GetTotemBaseForm(species, form);
|
2019-04-23 05:24:29 +00:00
|
|
|
|
var baseImage = GetBaseImageDefault(species, baseform, gender, shiny, generation);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
return ImageUtil.ToGrayscale(baseImage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Image GetBaseImageDefault(int species, int form, int gender, bool shiny, int generation)
|
|
|
|
|
{
|
2019-10-04 01:23:40 +00:00
|
|
|
|
var file = SpriteName.GetResourceStringSprite(species, form, gender, generation, shiny);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
return (Image)Resources.ResourceManager.GetObject(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Image GetBaseImageFallback(int species, int form, int gender, bool shiny, int generation)
|
|
|
|
|
{
|
|
|
|
|
if (shiny) // try again without shiny
|
|
|
|
|
{
|
2019-04-23 05:24:29 +00:00
|
|
|
|
var img = GetBaseImageDefault(species, form, gender, false, generation);
|
|
|
|
|
if (img != null)
|
|
|
|
|
return img;
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try again without form
|
2019-04-23 05:24:29 +00:00
|
|
|
|
var baseImage = (Image)Resources.ResourceManager.GetObject($"_{species}");
|
2018-07-15 00:53:37 +00:00
|
|
|
|
if (baseImage == null) // failed again
|
|
|
|
|
return Resources.unknown;
|
2019-04-23 05:24:29 +00:00
|
|
|
|
return ImageUtil.LayerImage(baseImage, Resources.unknown, 0, 0, UnknownFormTransparency);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Image LayerOverImageItem(Image baseImage, int item, int generation)
|
|
|
|
|
{
|
|
|
|
|
Image itemimg = (Image)Resources.ResourceManager.GetObject($"item_{item}") ?? Resources.helditem;
|
|
|
|
|
if (generation >= 2 && generation <= 4 && 328 <= item && item <= 419) // gen2/3/4 TM
|
|
|
|
|
itemimg = Resources.item_tm;
|
|
|
|
|
|
2019-04-23 05:24:29 +00:00
|
|
|
|
// Redraw item in bottom right corner; since images are cropped, try to not have them at the edge
|
|
|
|
|
int x = ItemShiftX + ((ItemMaxSize - itemimg.Width) / 2);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
if (x + itemimg.Width > baseImage.Width)
|
|
|
|
|
x = baseImage.Width - itemimg.Width;
|
2019-04-23 05:24:29 +00:00
|
|
|
|
int y = ItemShiftY + (ItemMaxSize - itemimg.Height);
|
2018-07-15 01:43:46 +00:00
|
|
|
|
return ImageUtil.LayerImage(baseImage, itemimg, x, y);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Image LayerOverImageShiny(Image baseImage, bool isBoxBGRed)
|
|
|
|
|
{
|
|
|
|
|
// Add shiny star to top left of image.
|
|
|
|
|
var rare = isBoxBGRed ? Resources.rare_icon_alt : Resources.rare_icon;
|
2019-04-23 05:24:29 +00:00
|
|
|
|
return ImageUtil.LayerImage(baseImage, rare, 0, 0, ShinyTransparency);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 05:59:00 +00:00
|
|
|
|
private static Image LayerOverImageEgg(Image baseImage, int species, bool hasItem)
|
2018-08-19 23:45:20 +00:00
|
|
|
|
{
|
2018-10-11 05:59:00 +00:00
|
|
|
|
if (ShowEggSpriteAsItem && !hasItem)
|
2018-08-19 23:45:20 +00:00
|
|
|
|
return LayerOverImageEggAsItem(baseImage, species);
|
|
|
|
|
return LayerOverImageEggTransparentSpecies(baseImage, species);
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 19:36:46 +00:00
|
|
|
|
private static Image GetEggSprite(int species) => species == 490 ? Resources._490_e : Resources.egg;
|
2018-08-19 23:45:20 +00:00
|
|
|
|
|
|
|
|
|
private static Image LayerOverImageEggTransparentSpecies(Image baseImage, int species)
|
2018-07-15 00:53:37 +00:00
|
|
|
|
{
|
|
|
|
|
// Partially transparent species.
|
2018-08-19 23:45:20 +00:00
|
|
|
|
baseImage = ImageUtil.ChangeOpacity(baseImage, EggUnderLayerTransparency);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
// Add the egg layer over-top with full opacity.
|
2018-08-19 23:45:20 +00:00
|
|
|
|
var egg = GetEggSprite(species);
|
2018-07-15 01:43:46 +00:00
|
|
|
|
return ImageUtil.LayerImage(baseImage, egg, 0, 0);
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
2018-08-19 23:45:20 +00:00
|
|
|
|
|
|
|
|
|
private static Image LayerOverImageEggAsItem(Image baseImage, int species)
|
|
|
|
|
{
|
|
|
|
|
var egg = GetEggSprite(species);
|
2019-04-23 05:24:29 +00:00
|
|
|
|
return ImageUtil.LayerImage(baseImage, egg, EggItemShiftX, EggItemShiftY); // similar to held item, since they can't have any
|
2018-08-19 23:45:20 +00:00
|
|
|
|
}
|
2018-07-15 00:53:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|