From 72aee7540da02b2baab61a95fdefbce16446b65c Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 7 Oct 2019 19:10:38 -0700 Subject: [PATCH] Enable nullable for drawing project Found one NRE: daycare slot graying Found one missing reference: GO icon (due to netcore3 refactor) --- PKHeX.Drawing/ImageUtil.cs | 14 +++------- PKHeX.Drawing/PKHeX.Drawing.csproj | 1 + PKHeX.Drawing/Sprites/SpriteBuilder.cs | 13 +++++----- PKHeX.Drawing/Sprites/SpriteUtil.cs | 26 +++++++++---------- .../Controls/PKM Editor/PKMEditor.Designer.cs | 7 ++--- .../Controls/SAV Editor/SAVEditor.cs | 3 ++- 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/PKHeX.Drawing/ImageUtil.cs b/PKHeX.Drawing/ImageUtil.cs index 59716b7ba..a623035dc 100644 --- a/PKHeX.Drawing/ImageUtil.cs +++ b/PKHeX.Drawing/ImageUtil.cs @@ -12,16 +12,14 @@ namespace PKHeX.Drawing { public static Bitmap LayerImage(Image baseLayer, Image overLayer, int x, int y, double transparency) { - if (baseLayer == null) - return overLayer as Bitmap; overLayer = ChangeOpacity(overLayer, transparency); return LayerImage(baseLayer, overLayer, x, y); } - public static Bitmap LayerImage(Image baseLayer, Image overLayer, int x, int y) + public static Bitmap LayerImage(Image? baseLayer, Image overLayer, int x, int y) { - if (baseLayer == null) - return overLayer as Bitmap; + if (baseLayer is null) + return (Bitmap)overLayer; Bitmap img = new Bitmap(baseLayer); using (Graphics gr = Graphics.FromImage(img)) gr.DrawImage(overLayer, x, y, overLayer.Width, overLayer.Height); @@ -30,8 +28,6 @@ namespace PKHeX.Drawing public static Bitmap ChangeOpacity(Image img, double trans) { - if (img == null) - return null; if (img.PixelFormat.HasFlag(PixelFormat.Indexed)) return (Bitmap)img; @@ -48,8 +44,6 @@ namespace PKHeX.Drawing public static Bitmap ChangeAllColorTo(Image img, Color c) { - if (img == null) - return null; if (img.PixelFormat.HasFlag(PixelFormat.Indexed)) return (Bitmap)img; @@ -66,8 +60,6 @@ namespace PKHeX.Drawing public static Bitmap ToGrayscale(Image img) { - if (img == null) - return null; if (img.PixelFormat.HasFlag(PixelFormat.Indexed)) return (Bitmap)img; diff --git a/PKHeX.Drawing/PKHeX.Drawing.csproj b/PKHeX.Drawing/PKHeX.Drawing.csproj index 935b4a496..5565a0101 100644 --- a/PKHeX.Drawing/PKHeX.Drawing.csproj +++ b/PKHeX.Drawing/PKHeX.Drawing.csproj @@ -3,6 +3,7 @@ netcoreapp3.0;net46 8 + enable diff --git a/PKHeX.Drawing/Sprites/SpriteBuilder.cs b/PKHeX.Drawing/Sprites/SpriteBuilder.cs index b416579cf..5beb8253f 100644 --- a/PKHeX.Drawing/Sprites/SpriteBuilder.cs +++ b/PKHeX.Drawing/Sprites/SpriteBuilder.cs @@ -49,7 +49,6 @@ namespace PKHeX.Drawing form = GetDeoxysForm(Game); var baseImage = GetBaseImage(species, form, gender, isShiny, generation); - return GetSprite(baseImage, species, heldItem, isEgg, isShiny, generation, isBoxBGRed); } @@ -72,17 +71,19 @@ namespace PKHeX.Drawing return img ?? GetBaseImageFallback(species, form, gender, shiny, generation); } - private static Image GetBaseImageTotem(int species, int form, int gender, bool shiny, int generation) + private static Image? GetBaseImageTotem(int species, int form, int gender, bool shiny, int generation) { var baseform = FormConverter.GetTotemBaseForm(species, form); var baseImage = GetBaseImageDefault(species, baseform, gender, shiny, generation); + if (baseImage == null) + return null; return ImageUtil.ToGrayscale(baseImage); } - private static Image GetBaseImageDefault(int species, int form, int gender, bool shiny, int generation) + private static Image? GetBaseImageDefault(int species, int form, int gender, bool shiny, int generation) { var file = SpriteName.GetResourceStringSprite(species, form, gender, generation, shiny); - return (Image)Resources.ResourceManager.GetObject(file); + return (Image?)Resources.ResourceManager.GetObject(file); } private static Image GetBaseImageFallback(int species, int form, int gender, bool shiny, int generation) @@ -95,7 +96,7 @@ namespace PKHeX.Drawing } // try again without form - var baseImage = (Image)Resources.ResourceManager.GetObject($"_{species}"); + var baseImage = (Image?)Resources.ResourceManager.GetObject($"_{species}"); if (baseImage == null) // failed again return Resources.unknown; return ImageUtil.LayerImage(baseImage, Resources.unknown, 0, 0, UnknownFormTransparency); @@ -103,7 +104,7 @@ namespace PKHeX.Drawing private static Image LayerOverImageItem(Image baseImage, int item, int generation) { - Image itemimg = (Image)Resources.ResourceManager.GetObject($"item_{item}") ?? Resources.helditem; + 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; diff --git a/PKHeX.Drawing/Sprites/SpriteUtil.cs b/PKHeX.Drawing/Sprites/SpriteUtil.cs index baac66ee3..927164296 100644 --- a/PKHeX.Drawing/Sprites/SpriteUtil.cs +++ b/PKHeX.Drawing/Sprites/SpriteUtil.cs @@ -11,7 +11,7 @@ namespace PKHeX.Drawing public static Image GetBallSprite(int ball) { string resource = SpriteName.GetResourceStringBall(ball); - return (Image)Resources.ResourceManager.GetObject(resource) ?? Resources._ball4; // Poké Ball (default) + return (Bitmap?)Resources.ResourceManager.GetObject(resource) ?? Resources._ball4; // Poké Ball (default) } public static Image GetSprite(int species, int form, int gender, int item, bool isegg, bool shiny, int generation = -1, bool isBoxBGRed = false) @@ -19,16 +19,16 @@ namespace PKHeX.Drawing return Spriter.GetSprite(species, form, gender, item, isegg, shiny, generation, isBoxBGRed); } - public static Image GetRibbonSprite(string name) + public static Image? GetRibbonSprite(string name) { var resource = name.Replace("CountG3", "G3").ToLower(); - return (Bitmap)Resources.ResourceManager.GetObject(resource); + return (Bitmap?)Resources.ResourceManager.GetObject(resource); } - public static Image GetRibbonSprite(string name, int max, int value) + public static Image? GetRibbonSprite(string name, int max, int value) { var resource = GetRibbonSpriteName(name, max, value); - return (Bitmap)Resources.ResourceManager.GetObject(resource); + return (Bitmap?)Resources.ResourceManager.GetObject(resource); } private static string GetRibbonSpriteName(string name, int max, int value) @@ -52,17 +52,17 @@ namespace PKHeX.Drawing }; } - public static Image GetTypeSprite(int type, int generation = PKX.Generation) + public static Image? GetTypeSprite(int type, int generation = PKX.Generation) { if (generation <= 2) type = (int)((MoveType)type).GetMoveTypeGeneration(generation); - return (Bitmap)Resources.ResourceManager.GetObject($"type_icon_{type:00}"); + return (Bitmap?)Resources.ResourceManager.GetObject($"type_icon_{type:00}"); } private static Image GetSprite(MysteryGift gift) { if (gift.Empty) - return null; + return Resources._0; var img = GetBaseImage(gift); if (gift.GiftUsed) @@ -101,7 +101,7 @@ namespace PKHeX.Drawing return img; } - private static Image GetSprite(SaveFile sav) + private static Image? GetSprite(SaveFile sav) { string file = "tr_00"; if (sav is SAV6AO) @@ -112,17 +112,17 @@ namespace PKHeX.Drawing private static Image GetWallpaper(SaveFile sav, int box) { string s = BoxWallpaper.GetWallpaperResourceName(sav.Version, sav.GetBoxWallpaper(box)); - return (Bitmap)Resources.ResourceManager.GetObject(s) ?? Resources.box_wp16xy; + return (Bitmap?)Resources.ResourceManager.GetObject(s) ?? Resources.box_wp16xy; } private static Image GetSprite(PKM pk, SaveFile sav, int box, int slot, bool flagIllegal = false) { if (!pk.Valid) - return null; + return Resources._0; bool inBox = (uint)slot < MaxSlotCount; bool empty = pk.Species == 0; - var sprite = empty ? null : pk.Sprite(isBoxBGRed: inBox && BoxWallpaper.IsWallpaperRed(sav.Version, sav.GetBoxWallpaper(box))); + var sprite = empty ? Resources._0 : pk.Sprite(isBoxBGRed: inBox && BoxWallpaper.IsWallpaperRed(sav.Version, sav.GetBoxWallpaper(box))); if (!empty && flagIllegal) { @@ -198,7 +198,7 @@ namespace PKHeX.Drawing // Extension Methods public static Image WallpaperImage(this SaveFile sav, int box) => GetWallpaper(sav, box); public static Image Sprite(this MysteryGift gift) => GetSprite(gift); - public static Image Sprite(this SaveFile sav) => GetSprite(sav); + public static Image? Sprite(this SaveFile sav) => GetSprite(sav); public static Image Sprite(this PKM pk, bool isBoxBGRed = false) => GetSprite(pk, isBoxBGRed); public static Image Sprite(this PKM pk, SaveFile sav, int box, int slot, bool flagIllegal = false) diff --git a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.Designer.cs b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.Designer.cs index 193d429cf..fe7bfd91a 100644 --- a/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.Designer.cs +++ b/PKHeX.WinForms/Controls/PKM Editor/PKMEditor.Designer.cs @@ -1821,7 +1821,7 @@ this.Tab_Attacks.Location = new System.Drawing.Point(4, 22); this.Tab_Attacks.Name = "Tab_Attacks"; this.Tab_Attacks.Padding = new System.Windows.Forms.Padding(3); - this.Tab_Attacks.Size = new System.Drawing.Size(272, 539); + this.Tab_Attacks.Size = new System.Drawing.Size(192, 74); this.Tab_Attacks.TabIndex = 3; this.Tab_Attacks.Text = "Attacks"; this.Tab_Attacks.UseVisualStyleBackColor = true; @@ -2208,7 +2208,7 @@ this.Tab_OTMisc.Location = new System.Drawing.Point(4, 22); this.Tab_OTMisc.Name = "Tab_OTMisc"; this.Tab_OTMisc.Padding = new System.Windows.Forms.Padding(3); - this.Tab_OTMisc.Size = new System.Drawing.Size(192, 74); + this.Tab_OTMisc.Size = new System.Drawing.Size(272, 539); this.Tab_OTMisc.TabIndex = 4; this.Tab_OTMisc.Text = "OT/Misc"; this.Tab_OTMisc.UseVisualStyleBackColor = true; @@ -2380,7 +2380,8 @@ // // PB_MarkGO // - this.PB_MarkGO.InitialImage = null; + this.PB_MarkGO.Image = global::PKHeX.WinForms.Properties.Resources.horohoro; + this.PB_MarkGO.InitialImage = global::PKHeX.WinForms.Properties.Resources.horohoro; this.PB_MarkGO.Location = new System.Drawing.Point(110, 15); this.PB_MarkGO.Name = "PB_MarkGO"; this.PB_MarkGO.Size = new System.Drawing.Size(20, 20); diff --git a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs index 6c709fd41..694fd32a0 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SAVEditor.cs @@ -289,7 +289,8 @@ namespace PKHeX.WinForms.Controls else { L_SlotOccupied[i].Text = $"{i + 1}: ✘"; - pb.Image = ImageUtil.ChangeOpacity(pb.Image, 0.6); + if (pb.Image != null) + pb.Image = ImageUtil.ChangeOpacity(pb.Image, 0.6); } }