Refactoring

max IVs use flawless method directly with 6 IVs specified
move color blend/stat calc to ImageUtil
move potential string to commonedits
expose color properties for pkmeditor
don't constantly create/dispose brushes for drawing legal moves
This commit is contained in:
Kurt
2018-07-14 15:08:14 -07:00
parent 74f36b79af
commit 8c2b74c149
6 changed files with 69 additions and 83 deletions

View File

@@ -688,5 +688,20 @@ public static void SetDefaultNickname(this PKM pk, LegalityAnalysis la = null)
else
pk.SetNickname();
}
private static readonly string[] PotentialUnicode = { "★☆☆☆", "★★☆☆", "★★★☆", "★★★★" };
private static readonly string[] PotentialNoUnicode = { "+", "++", "+++", "++++" };
/// <summary>
/// Gets the Potential evaluation of the input <see cref="pk"/>.
/// </summary>
/// <param name="pk">Pokémon to analyze.</param>
/// <param name="unicode">Returned value is unicode or not</param>
/// <returns>Potential string</returns>
public static string GetPotentialString(this PKM pk, bool unicode = true)
{
var arr = unicode ? PotentialUnicode : PotentialNoUnicode;
return arr[pk.PotentialRating];
}
}
}

View File

@@ -11,7 +11,7 @@
namespace PKHeX.WinForms.Controls
{
public partial class PKMEditor : UserControl, IMainEditor
public sealed partial class PKMEditor : UserControl, IMainEditor
{
public PKMEditor()
{
@@ -28,6 +28,9 @@ public PKMEditor()
TB_OT.Font = (Font)TB_Nickname.Font.Clone();
TB_OTt2.Font = (Font)TB_Nickname.Font.Clone();
if (TextBrush == null) TextBrush = new SolidBrush(CB_Move1.ForeColor);
if (BackBrush == null) BackBrush = new SolidBrush(CB_Move1.BackColor);
// Commonly reused Control arrays
Moves = new[] { CB_Move1, CB_Move2, CB_Move3, CB_Move4 };
Relearn = new[] { CB_RelearnMove1, CB_RelearnMove2, CB_RelearnMove3, CB_RelearnMove4 };
@@ -76,7 +79,6 @@ private void SavePartyStats(PKM pk)
private LegalityAnalysis Legality;
private string[] gendersymbols = { "♂", "♀", "-" };
private readonly Image mixedHighlight = ImageUtil.ChangeOpacity(Resources.slotSet, 0.5);
private static readonly Color InvalidSelectionColor = Color.DarkSalmon;
public event EventHandler LegalityChanged;
public event EventHandler UpdatePreviewSprite;
@@ -112,7 +114,7 @@ public bool VerifiedPKM()
if (ModifierKeys == (Keys.Control | Keys.Shift | Keys.Alt))
return true; // Override
var cb = Array.Find(ValidationRequired, c => c.BackColor == InvalidSelectionColor && c.Items.Count != 0);
var cb = Array.Find(ValidationRequired, c => c.BackColor == InvalidSelection && c.Items.Count != 0);
if (cb != null)
tabMain.SelectedTab = WinFormsUtil.FindFirstControlOfType<TabPage>(cb);
else if (!Stats.Valid)
@@ -292,6 +294,14 @@ private void UpdateSprite()
}
// General Use Functions //
private Color InvalidSelection { get; set; } = Color.DarkSalmon;
private Color MarkBlue { get; set; } = Color.FromArgb(000, 191, 255);
private Color MarkPink { get; set; } = Color.FromArgb(255, 117, 179);
private Color MarkDefault { get; set; } = Color.Black;
private Brush LegalMove { get; set; } = Brushes.PaleGreen;
private Brush TextBrush { get; set; }
private Brush BackBrush { get; set; }
private Color GetGenderColor(int gender)
{
if (gender == 0) // male
@@ -418,18 +428,18 @@ private void SetMarkings()
if (GetMarkingColor(markings[i], out Color c))
pba[i].Image = ImageUtil.ChangeAllColorTo(pba[i].Image, c);
}
private static bool GetMarkingColor(int markval, out Color c)
private bool GetMarkingColor(int markval, out Color c)
{
switch (markval)
{
case 1:
c = Color.FromArgb(000, 191, 255);
c = MarkBlue;
return true;
case 2:
c = Color.FromArgb(255, 117, 179);
c = MarkPink;
return true;
default:
c = Color.Black;
c = MarkDefault;
return false;
}
}
@@ -1332,7 +1342,7 @@ private void ValidateComboBox(object sender)
if (cb.Text.Length == 0 && cb.Items.Count > 0)
cb.SelectedIndex = 0;
else if (cb.SelectedValue == null)
cb.BackColor = InvalidSelectionColor;
cb.BackColor = InvalidSelection;
else
cb.ResetBackColor();
}
@@ -1392,18 +1402,14 @@ private void ValidateMovePaint(object sender, DrawItemEventArgs e)
var i = (ComboItem)((ComboBox)sender).Items[e.Index];
var moves = Legality.AllSuggestedMovesAndRelearn;
bool vm = moves?.Contains(i.Value) == true && !HaX;
bool valid = moves?.Contains(i.Value) == true && !HaX;
bool current = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
Brush tBrush = current ? SystemBrushes.HighlightText : new SolidBrush(e.ForeColor);
Brush brush = current ? SystemBrushes.Highlight : vm ? Brushes.PaleGreen : new SolidBrush(e.BackColor);
Brush tBrush = current ? SystemBrushes.HighlightText : TextBrush;
Brush brush = current ? SystemBrushes.Highlight : valid ? LegalMove : BackBrush;
e.Graphics.FillRectangle(brush, e.Bounds);
e.Graphics.DrawString(i.Text, e.Font, tBrush, e.Bounds, StringFormat.GenericDefault);
if (current) return;
tBrush.Dispose();
if (!vm)
brush.Dispose();
}
private void ValidateLocation(object sender, EventArgs e)
{

View File

@@ -251,45 +251,22 @@ public void UpdateStats()
private void LoadBST(PersonalInfo pi)
{
var stats = new[] {pi.HP, pi.ATK, pi.DEF, pi.SPE, pi.SPA, pi.SPD};
var stats = pi.Stats;
for (int i = 0; i < stats.Length; i++)
{
MT_Base[i].Text = stats[i].ToString("000");
MT_Base[i].BackColor = MapColor(stats[i]);
MT_Base[i].BackColor = ImageUtil.ColorBaseStat(stats[i]);
}
var bst = pi.BST;
TB_BST.Text = bst.ToString("000");
TB_BST.BackColor = MapColor((int)(Math.Max(0, bst - 175) / 3f));
}
private static Color MapColor(int v)
{
const float maxval = 180; // shift the green cap down
float x = 100f * v / maxval;
if (x > 100)
x = 100;
double red = 255f * (x > 50 ? 1 - 2 * (x - 50) / 100.0 : 1.0);
double green = 255f * (x > 50 ? 1.0 : 2 * x / 100.0);
return Blend(Color.FromArgb((int)red, (int)green, 0), Color.White, 0.4);
}
private static Color Blend(Color color, Color backColor, double amount)
{
byte r = (byte)(color.R * amount + backColor.R * (1 - amount));
byte g = (byte)(color.G * amount + backColor.G * (1 - amount));
byte b = (byte)(color.B * amount + backColor.B * (1 - amount));
return Color.FromArgb(r, g, b);
TB_BST.BackColor = ImageUtil.ColorBaseStat((int)(Math.Max(0, bst - 175) / 3f));
}
public void UpdateRandomIVs(object sender, EventArgs e)
{
ChangingFields = true;
if (ModifierKeys.HasFlag(Keys.Control)) // Max IVs
{
int[] IVs = { pkm.MaxIV, pkm.MaxIV, pkm.MaxIV, pkm.MaxIV, pkm.MaxIV, pkm.MaxIV };
LoadIVs(IVs);
}
else
LoadIVs(pkm.SetRandomIVs());
int? flawless = ModifierKeys.HasFlag(Keys.Control) ? (int?)6 : null;
var IVs = pkm.SetRandomIVs(flawless);
LoadIVs(IVs);
}
public void UpdateCharacteristic() => UpdateCharacteristic(pkm.Characteristic);
@@ -429,16 +406,4 @@ public void InitializeDataSources()
CB_HPType.DataSource = Util.GetCBList(GameInfo.Strings.types.Skip(1).Take(16).ToArray(), null);
}
}
public static partial class Extensions
{
private static readonly string[] PotentialUnicode = {"★☆☆☆", "★★☆☆", "★★★☆", "★★★★"};
private static readonly string[] PotentialNoUnicode = {"+", "++", "+++", "++++"};
public static string GetPotentialString(this PKM pkm, bool unicode = true)
{
var arr = unicode ? PotentialUnicode : PotentialNoUnicode;
return arr[pkm.PotentialRating];
}
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using PKHeX.Core;
using PKHeX.WinForms.Properties;
@@ -54,44 +53,26 @@ private void PopEntry(int index)
row.Cells[r++].Value = PKMUtil.GetSprite(s, f, 0, 0, false, false, SAV.Generation);
row.Cells[r++].Value = species[index];
row.Cells[r++].Value = s > 721 || Legal.PastGenAlolanNatives.Contains(s);
row.Cells[r].Style.BackColor = MapColor((int)((Math.Max(p.BST - 175, 0)) / 3f));
row.Cells[r].Style.BackColor = ImageUtil.ColorBaseStat((int)((Math.Max(p.BST - 175, 0)) / 3f));
row.Cells[r++].Value = p.BST.ToString("000");
row.Cells[r++].Value = PKMUtil.GetTypeSprite(p.Type1, SAV.Generation);
row.Cells[r++].Value = p.Type1 == p.Type2 ? Resources.slotTrans : PKMUtil.GetTypeSprite(p.Type2, SAV.Generation);
row.Cells[r].Style.BackColor = MapColor(p.HP);
row.Cells[r].Style.BackColor = ImageUtil.ColorBaseStat(p.HP);
row.Cells[r++].Value = p.HP.ToString("000");
row.Cells[r].Style.BackColor = MapColor(p.ATK);
row.Cells[r].Style.BackColor = ImageUtil.ColorBaseStat(p.ATK);
row.Cells[r++].Value = p.ATK.ToString("000");
row.Cells[r].Style.BackColor = MapColor(p.DEF);
row.Cells[r].Style.BackColor = ImageUtil.ColorBaseStat(p.DEF);
row.Cells[r++].Value = p.DEF.ToString("000");
row.Cells[r].Style.BackColor = MapColor(p.SPA);
row.Cells[r].Style.BackColor = ImageUtil.ColorBaseStat(p.SPA);
row.Cells[r++].Value = p.SPA.ToString("000");
row.Cells[r].Style.BackColor = MapColor(p.SPD);
row.Cells[r].Style.BackColor = ImageUtil.ColorBaseStat(p.SPD);
row.Cells[r++].Value = p.SPD.ToString("000");
row.Cells[r].Style.BackColor = MapColor(p.SPE);
row.Cells[r].Style.BackColor = ImageUtil.ColorBaseStat(p.SPE);
row.Cells[r++].Value = p.SPE.ToString("000");
row.Cells[r++].Value = abilities[p.Abilities[0]];
row.Cells[r++].Value = abilities[p.Abilities[1]];
row.Cells[r].Value = abilities[p.Abilities.Length <= 2 ? 0 : p.Abilities[2]];
DGV.Rows.Add(row);
}
private static Color MapColor(int v)
{
const float maxval = 180; // shift the green cap down
float x = 100f * v / maxval;
if (x > 100)
x = 100;
double red = 255f * (x > 50 ? 1 - 2 * (x - 50) / 100.0 : 1.0);
double green = 255f * (x > 50 ? 1.0 : 2 * x / 100.0);
return Blend(Color.FromArgb((int)red, (int)green, 0), Color.White, 0.4);
}
private static Color Blend(Color color, Color backColor, double amount)
{
byte r = (byte)(color.R * amount + backColor.R * (1 - amount));
byte g = (byte)(color.G * amount + backColor.G * (1 - amount));
byte b = (byte)(color.B * amount + backColor.B * (1 - amount));
return Color.FromArgb(r, g, b);
}
}
}

View File

@@ -85,7 +85,7 @@ private void LoadFields()
CB_OTFeel.SelectedIndex = pkm.OT_Feeling;
CB_Handler.Items.Clear();
CB_Handler.Items.AddRange(new object[] {$"{pkm.OT_Name} ({args[2]})"}); // OTNAME : OT
CB_Handler.Items.Add($"{pkm.OT_Name} ({args[2]})"); // OTNAME : OT
if (!string.IsNullOrEmpty(pkm.HT_Name))
CB_Handler.Items.Add(pkm.HT_Name);

View File

@@ -123,5 +123,24 @@ private static void SetAllColorToGrayScale(byte[] data)
data[i + 2] = greyS;
}
}
public static Color ColorBaseStat(int v)
{
const float maxval = 180; // shift the green cap down
float x = 100f * v / maxval;
if (x > 100)
x = 100;
double red = 255f * (x > 50 ? 1 - 2 * (x - 50) / 100.0 : 1.0);
double green = 255f * (x > 50 ? 1.0 : 2 * x / 100.0);
return Blend(Color.FromArgb((int)red, (int)green, 0), Color.White, 0.4);
}
public static Color Blend(Color color, Color backColor, double amount)
{
byte r = (byte)(color.R * amount + backColor.R * (1 - amount));
byte g = (byte)(color.G * amount + backColor.G * (1 - amount));
byte b = (byte)(color.B * amount + backColor.B * (1 - amount));
return Color.FromArgb(r, g, b);
}
}
}