2017-01-12 01:55:42 +00:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using PKHeX.Core;
|
2017-05-12 04:34:18 +00:00
|
|
|
|
using PKHeX.WinForms.Properties;
|
2017-01-12 01:55:42 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms
|
|
|
|
|
{
|
|
|
|
|
public static class PKMUtil
|
|
|
|
|
{
|
2017-07-16 21:05:29 +00:00
|
|
|
|
public static 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;
|
|
|
|
|
}
|
|
|
|
|
private static GameVersion Game;
|
|
|
|
|
|
|
|
|
|
private static int GetDeoxysForm()
|
|
|
|
|
{
|
|
|
|
|
switch (Game)
|
|
|
|
|
{
|
2018-05-12 15:13:39 +00:00
|
|
|
|
default: return 0;
|
2018-02-22 04:35:07 +00:00
|
|
|
|
case GameVersion.FR: return 1; // Attack
|
|
|
|
|
case GameVersion.LG: return 2; // Defense
|
|
|
|
|
case GameVersion.E: return 3; // Speed
|
2017-07-16 21:05:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static Image GetBallSprite(int ball)
|
2017-01-12 01:55:42 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string str = PKX.GetResourceStringBall(ball);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
return (Image)Resources.ResourceManager.GetObject(str) ?? Resources._ball4; // Poké Ball (default)
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static Image GetSprite(int species, int form, int gender, int item, bool isegg, bool shiny, int generation = -1, bool isBoxBGRed = false)
|
2017-01-12 01:55:42 +00:00
|
|
|
|
{
|
|
|
|
|
if (species == 0)
|
2017-01-12 06:28:35 +00:00
|
|
|
|
return Resources._0;
|
2017-01-12 01:55:42 +00:00
|
|
|
|
|
2017-07-16 21:05:29 +00:00
|
|
|
|
if (generation == 3 && species == 386) // Deoxys, special consideration for Gen3 save files
|
|
|
|
|
form = GetDeoxysForm();
|
|
|
|
|
|
2018-01-03 01:17:30 +00:00
|
|
|
|
string file = PKX.GetResourceStringSprite(species, form, gender, generation, shiny);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
|
|
|
|
|
// Redrawing logic
|
|
|
|
|
Image baseImage = (Image)Resources.ResourceManager.GetObject(file);
|
2017-11-10 06:00:36 +00:00
|
|
|
|
if (FormConverter.IsTotemForm(species, form))
|
2017-11-10 05:52:11 +00:00
|
|
|
|
{
|
|
|
|
|
form = FormConverter.GetTotemBaseForm(species, form);
|
2018-01-03 01:17:30 +00:00
|
|
|
|
file = PKX.GetResourceStringSprite(species, form, gender, generation, shiny);
|
2017-11-10 05:52:11 +00:00
|
|
|
|
baseImage = (Image)Resources.ResourceManager.GetObject(file);
|
|
|
|
|
baseImage = ImageUtil.ToGrayscale(baseImage);
|
|
|
|
|
}
|
2017-01-12 01:55:42 +00:00
|
|
|
|
if (baseImage == null)
|
|
|
|
|
{
|
2018-01-03 01:17:30 +00:00
|
|
|
|
if (shiny) // try again without shiny
|
|
|
|
|
{
|
|
|
|
|
file = PKX.GetResourceStringSprite(species, form, gender, generation);
|
|
|
|
|
baseImage = (Image)Resources.ResourceManager.GetObject(file);
|
|
|
|
|
}
|
|
|
|
|
if (baseImage == null)
|
2018-02-22 04:35:07 +00:00
|
|
|
|
baseImage = (Image)Resources.ResourceManager.GetObject($"_{species}");
|
2017-01-12 06:28:35 +00:00
|
|
|
|
baseImage = baseImage != null ? ImageUtil.LayerImage(baseImage, Resources.unknown, 0, 0, .5) : Resources.unknown;
|
2017-01-12 01:55:42 +00:00
|
|
|
|
}
|
|
|
|
|
if (isegg)
|
|
|
|
|
{
|
2017-02-05 07:42:55 +00:00
|
|
|
|
// Partially transparent species.
|
|
|
|
|
baseImage = ImageUtil.ChangeOpacity(baseImage, 0.33);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
// Add the egg layer over-top with full opacity.
|
2017-06-21 05:19:04 +00:00
|
|
|
|
var egg = species == 490 ? (Image) Resources.ResourceManager.GetObject("_490_e") : Resources.egg;
|
|
|
|
|
baseImage = ImageUtil.LayerImage(baseImage, egg, 0, 0, 1);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
}
|
|
|
|
|
if (shiny)
|
|
|
|
|
{
|
|
|
|
|
// Add shiny star to top left of image.
|
2017-03-09 05:50:34 +00:00
|
|
|
|
var rare = isBoxBGRed ? Resources.rare_icon_alt : Resources.rare_icon;
|
|
|
|
|
baseImage = ImageUtil.LayerImage(baseImage, rare, 0, 0, 0.7);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
}
|
|
|
|
|
if (item > 0)
|
|
|
|
|
{
|
2017-09-30 05:58:25 +00:00
|
|
|
|
Image itemimg = (Image)Resources.ResourceManager.GetObject($"item_{item}") ?? Resources.helditem;
|
2017-01-12 01:55:42 +00:00
|
|
|
|
if (generation >= 2 && generation <= 4 && 328 <= item && item <= 419) // gen2/3/4 TM
|
|
|
|
|
itemimg = Resources.item_tm;
|
|
|
|
|
|
|
|
|
|
// Redraw
|
2017-03-09 05:50:34 +00:00
|
|
|
|
int x = 22 + (15 - itemimg.Width)/2;
|
2017-11-18 06:19:23 +00:00
|
|
|
|
if (x + itemimg.Width > baseImage.Width)
|
|
|
|
|
x = baseImage.Width - itemimg.Width;
|
2017-03-09 05:50:34 +00:00
|
|
|
|
int y = 15 + (15 - itemimg.Height);
|
|
|
|
|
baseImage = ImageUtil.LayerImage(baseImage, itemimg, x, y, 1);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
}
|
|
|
|
|
return baseImage;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static Image GetRibbonSprite(string name)
|
2017-01-12 01:55:42 +00:00
|
|
|
|
{
|
2017-01-12 06:28:35 +00:00
|
|
|
|
return Resources.ResourceManager.GetObject(name.Replace("CountG3", "G3").ToLower()) as Image;
|
|
|
|
|
}
|
2018-03-18 18:25:57 +00:00
|
|
|
|
public static Image GetTypeSprite(int type, int generation = PKX.Generation)
|
2017-01-12 06:28:35 +00:00
|
|
|
|
{
|
2018-03-18 18:25:57 +00:00
|
|
|
|
if (generation <= 2)
|
|
|
|
|
type = (int)((MoveType)type).GetMoveTypeGeneration(generation);
|
2017-09-30 05:58:25 +00:00
|
|
|
|
return Resources.ResourceManager.GetObject($"type_icon_{type:00}") as Image;
|
2017-01-12 01:55:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-18 19:34:23 +00:00
|
|
|
|
private static Image GetSprite(MysteryGift gift)
|
2017-01-12 06:28:35 +00:00
|
|
|
|
{
|
|
|
|
|
if (gift.Empty)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Image img;
|
2017-06-21 05:19:04 +00:00
|
|
|
|
if (gift.IsEgg && gift.Species == 490) // Manaphy Egg
|
|
|
|
|
img = (Image)(Resources.ResourceManager.GetObject("_490_e") ?? Resources.unknown);
|
|
|
|
|
else if (gift.IsPokémon)
|
2017-11-18 19:34:23 +00:00
|
|
|
|
img = GetSprite(gift.Species, gift.Form, gift.Gender, gift.HeldItem, gift.IsEgg, gift.IsShiny, gift.Format);
|
2017-01-12 06:28:35 +00:00
|
|
|
|
else if (gift.IsItem)
|
2017-10-01 03:57:32 +00:00
|
|
|
|
{
|
|
|
|
|
int item = gift.ItemID;
|
|
|
|
|
if (Legal.ZCrystalDictionary.TryGetValue(item, out int value))
|
|
|
|
|
item = value;
|
|
|
|
|
img = (Image)(Resources.ResourceManager.GetObject("item_" + item) ?? Resources.unknown);
|
|
|
|
|
}
|
2017-01-12 06:28:35 +00:00
|
|
|
|
else
|
|
|
|
|
img = Resources.unknown;
|
|
|
|
|
|
|
|
|
|
if (gift.GiftUsed)
|
|
|
|
|
img = ImageUtil.LayerImage(new Bitmap(img.Width, img.Height), img, 0, 0, 0.3);
|
|
|
|
|
return img;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static Image GetSprite(PKM pkm, bool isBoxBGRed = false)
|
2017-03-08 16:46:25 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return GetSprite(pkm.Species, pkm.AltForm, pkm.Gender, pkm.SpriteItem, pkm.IsEgg, pkm.IsShiny, pkm.Format, isBoxBGRed);
|
2017-03-08 16:46:25 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static Image GetSprite(SaveFile SAV)
|
2017-01-12 06:28:35 +00:00
|
|
|
|
{
|
|
|
|
|
string file = "tr_00";
|
|
|
|
|
if (SAV.Generation == 6 && (SAV.ORAS || SAV.ORASDEMO))
|
2017-09-30 05:58:25 +00:00
|
|
|
|
file = $"tr_{SAV.MultiplayerSpriteID:00}";
|
2017-01-12 06:28:35 +00:00
|
|
|
|
return Resources.ResourceManager.GetObject(file) as Image;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static Image GetWallpaper(SaveFile SAV, int box)
|
2017-01-12 01:55:42 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
string s = BoxWallpaper.GetWallpaper(SAV, box);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
return (Bitmap)(Resources.ResourceManager.GetObject(s) ?? Resources.box_wp16xy);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private static Image GetSprite(PKM pkm, SaveFile SAV, int box, int slot, bool flagIllegal = false)
|
2017-02-05 04:39:42 +00:00
|
|
|
|
{
|
|
|
|
|
if (!pkm.Valid)
|
|
|
|
|
return null;
|
|
|
|
|
|
2017-03-13 05:23:39 +00:00
|
|
|
|
bool inBox = slot >= 0 && slot < 30;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
var sprite = pkm.Species != 0 ? pkm.Sprite(isBoxBGRed: inBox && BoxWallpaper.IsWallpaperRed(SAV, box)) : null;
|
2017-03-09 05:50:34 +00:00
|
|
|
|
|
|
|
|
|
if (slot <= -1) // from tabs
|
|
|
|
|
return sprite;
|
|
|
|
|
|
|
|
|
|
if (flagIllegal)
|
2017-02-05 04:39:42 +00:00
|
|
|
|
{
|
2018-05-22 01:28:54 +00:00
|
|
|
|
if (box >= 0)
|
2017-04-15 00:49:13 +00:00
|
|
|
|
pkm.Box = box;
|
2017-11-07 03:31:24 +00:00
|
|
|
|
var la = new LegalityAnalysis(pkm, SAV.Personal);
|
2017-11-18 06:19:23 +00:00
|
|
|
|
if (la.ParsedInvalid && pkm.Species != 0)
|
2017-02-05 04:39:42 +00:00
|
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.warn, 0, 14, 1);
|
|
|
|
|
}
|
2017-03-09 05:50:34 +00:00
|
|
|
|
if (inBox) // in box
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (SAV.IsSlotLocked(box, slot))
|
2017-03-09 05:50:34 +00:00
|
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.locked, 26, 0, 1);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
else if (SAV.IsSlotInBattleTeam(box, slot))
|
2017-03-09 05:50:34 +00:00
|
|
|
|
sprite = ImageUtil.LayerImage(sprite, Resources.team, 21, 0, 1);
|
|
|
|
|
}
|
2017-02-05 04:39:42 +00:00
|
|
|
|
|
|
|
|
|
return sprite;
|
|
|
|
|
}
|
2017-01-12 06:28:35 +00:00
|
|
|
|
|
|
|
|
|
// Extension Methods
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static Image WallpaperImage(this SaveFile SAV, int box) => GetWallpaper(SAV, box);
|
2017-11-18 19:34:23 +00:00
|
|
|
|
public static Image Sprite(this MysteryGift gift) => GetSprite(gift);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public static Image Sprite(this SaveFile SAV) => GetSprite(SAV);
|
|
|
|
|
public static Image Sprite(this PKM pkm, bool isBoxBGRed = false) => GetSprite(pkm, isBoxBGRed);
|
2017-02-05 04:39:42 +00:00
|
|
|
|
public static Image Sprite(this PKM pkm, SaveFile SAV, int box, int slot, bool flagIllegal = false)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
=> GetSprite(pkm, SAV, box, slot, flagIllegal);
|
2017-01-12 01:55:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|