Add glow animation to slothover

This commit is contained in:
Kurt 2018-07-22 20:04:28 -07:00
parent 3c4ba0d52b
commit 922cbdf583
3 changed files with 132 additions and 11 deletions

View file

@ -0,0 +1,92 @@
using System;
using System.Drawing;
using System.Timers;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
namespace PKHeX.WinForms.Controls
{
public sealed class BitmapAnimator : Timer
{
public BitmapAnimator(Bitmap baseImage, Bitmap extraLayer = null)
{
GlowBase = baseImage;
ExtraLayer = extraLayer;
Elapsed += TimerElapsed;
}
private readonly Bitmap GlowBase;
private readonly Bitmap ExtraLayer;
private Bitmap[] GlowCache;
private int GlowInterval;
private int GlowCounter;
public int GlowFps { get; set; } = 60;
public Color GlowToColor { get; set; } = Color.LightSkyBlue;
public Color GlowFromColor { get; set; } = Color.White;
private readonly object Lock = new object();
public new void Start() => throw new ArgumentException();
public new void Stop()
{
lock (Lock)
StopTimer();
}
private void StopTimer()
{
Enabled = false;
pb.BackgroundImage = OriginalBackground;
GlowBase.Dispose();
}
private PictureBox pb;
public Image OriginalBackground;
public void Start(PictureBox pbox, Image original)
{
pb = pbox;
OriginalBackground = original;
GlowCache = new Bitmap[GlowFps];
GlowInterval = 1000 / GlowFps;
Interval = GlowInterval;
Enabled = true;
}
private void TimerElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
lock (Lock)
{
if (!Enabled)
return; // timer canceled, was waiting to proceed
GlowCounter = (GlowCounter + 1) % (GlowInterval * 2); // loop backwards
int frameIndex = GlowCounter >= GlowInterval ? (GlowInterval * 2) - GlowCounter : GlowCounter;
try
{
var frame = GetFrame(frameIndex);
pb.BackgroundImage = ImageUtil.LayerImage(OriginalBackground, frame, 0, 0, 1);
}
catch
{
StopTimer();
}
}
}
private Bitmap GetFrame(int frameIndex)
{
var frame = GlowCache[frameIndex];
if (frame != null)
return frame;
var elapsedFraction = (double)frameIndex / GlowInterval;
var frameColor = GetFrameColor(elapsedFraction);
frame = ImageUtil.ChangeAllColorTo(GlowBase, frameColor);
if (ExtraLayer != null)
frame = ImageUtil.LayerImage(frame, ExtraLayer, 0, 0, 1);
return GlowCache[frameIndex] = frame;
}
private Color GetFrameColor(double elapsedFraction) => ImageUtil.Blend(GlowToColor, GlowFromColor, elapsedFraction);
}
}

View file

@ -24,7 +24,11 @@ namespace PKHeX.WinForms.Controls
public Image ColorizedColor { get; private set; }
public int ColorizedBox { get; private set; } = -1;
public int ColorizedSlot { get; private set; } = -1;
public bool GlowHover { get; set; } = true;
public Color GlowInitial { get; set; } = Color.White;
public Color GlowFinal { get; set; } = Color.LightSkyBlue;
private BitmapAnimator HoverWorker;
private SaveFile SAV => SE.SAV;
public SlotChangeInfo DragInfo;
@ -54,22 +58,15 @@ namespace PKHeX.WinForms.Controls
var pb = (PictureBox)sender;
if (pb.Image == null)
return;
OriginalBackground = pb.BackgroundImage;
var hover = GlowHover ? GetGlowSprite(pb) : Resources.slotHover;
pb.BackgroundImage = CurrentBackground = pb.BackgroundImage == null ? hover : ImageUtil.LayerImage(pb.BackgroundImage, hover, 0, 0);
BeginHoverSlot(pb);
}
private Bitmap GetGlowSprite(PictureBox pb)
private Bitmap GetGlowSprite(PictureBox pb, PKM pk)
{
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
var data = view.GetSlotData(pb);
var pk = SAV.GetStoredSlot(data.Offset);
var baseSprite = SpriteUtil.GetSprite(pk.Species, pk.AltForm, pk.Gender, 0, pk.IsEgg, false, pk.Format);
var pixels = ImageUtil.GetPixelData((Bitmap)baseSprite);
ImageUtil.GlowEdges(pixels, new byte[] { 255, 255, 255 }, pb.Image.Width);
var hover = ImageUtil.GetBitmap(pixels, pb.Image.Width, pb.Image.Height);
return ImageUtil.LayerImage(Resources.slotHover, hover, 0, 0);
ImageUtil.GlowEdges(pixels, new[] {GlowInitial.B, GlowInitial.G, GlowInitial.R}, pb.Image.Width);
return ImageUtil.GetBitmap(pixels, pb.Image.Width, pb.Image.Height);
}
private void BeginHoverSlot(PictureBox pb)
{
@ -78,6 +75,25 @@ namespace PKHeX.WinForms.Controls
var pk = SAV.GetStoredSlot(data.Offset);
HoveredSlot = pb;
OriginalBackground = pb.BackgroundImage;
Bitmap hover;
if (GlowHover)
{
HoverWorker?.Stop();
var GlowBase = GetGlowSprite(pb, pk);
hover = ImageUtil.LayerImage(GlowBase, Resources.slotHover, 0, 0);
HoverWorker = new BitmapAnimator(GlowBase, Resources.slotHover) { GlowFromColor = GlowInitial, GlowToColor = GlowFinal };
HoverWorker.Start(pb, OriginalBackground);
}
else
{
hover = Resources.slotHover;
}
pb.BackgroundImage = CurrentBackground = OriginalBackground == null ? hover : ImageUtil.LayerImage(OriginalBackground, hover, 0, 0);
if (Settings.Default.HoverSlotShowText)
ShowSimulatorSetTooltip(pb, pk);
if (Settings.Default.HoverSlotPlayCry)
@ -85,7 +101,13 @@ namespace PKHeX.WinForms.Controls
}
private void EndHoverSlot()
{
HoveredSlot = null;
if (HoveredSlot != null)
{
HoverWorker?.Stop();
HoverWorker = null;
HoveredSlot.BackgroundImage = OriginalBackground;
HoveredSlot = null;
}
ShowSet.RemoveAll();
Sounds.Stop();
}
@ -433,6 +455,10 @@ namespace PKHeX.WinForms.Controls
ColorizedBox = box;
ColorizedSlot = slot;
ColorizedColor = img;
OriginalBackground = img;
if (HoverWorker != null)
HoverWorker.OriginalBackground = img;
}
// PKM Get Set

View file

@ -178,6 +178,9 @@
<Compile Include="Controls\PKM Editor\TrainerID.Designer.cs">
<DependentUpon>TrainerID.cs</DependentUpon>
</Compile>
<Compile Include="Controls\SAV Editor\BitmapAnimator.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\SAV Editor\BoxEditor.cs">
<SubType>UserControl</SubType>
</Compile>