2017-05-23 04:55:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-07-02 02:43:51 +00:00
|
|
|
|
using System.Diagnostics;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2018-09-16 19:44:00 +00:00
|
|
|
|
using System.Linq;
|
2018-05-11 23:58:29 +00:00
|
|
|
|
using System.Media;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using PKHeX.Core;
|
|
|
|
|
using PKHeX.WinForms.Properties;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms.Controls
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manager class for moving slots.
|
|
|
|
|
/// </summary>
|
2017-12-16 22:11:41 +00:00
|
|
|
|
public sealed class SlotChangeManager : IDisposable
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
// Disposeables
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public readonly SAVEditor SE;
|
|
|
|
|
private Image OriginalBackground;
|
|
|
|
|
private Image CurrentBackground;
|
2018-01-30 01:52:21 +00:00
|
|
|
|
|
|
|
|
|
public Image ColorizedColor { get; private set; }
|
|
|
|
|
public int ColorizedBox { get; private set; } = -1;
|
|
|
|
|
public int ColorizedSlot { get; private set; } = -1;
|
2018-07-23 03:04:28 +00:00
|
|
|
|
|
2018-07-22 04:55:37 +00:00
|
|
|
|
public bool GlowHover { get; set; } = true;
|
2018-07-23 03:04:28 +00:00
|
|
|
|
public Color GlowInitial { get; set; } = Color.White;
|
|
|
|
|
public Color GlowFinal { get; set; } = Color.LightSkyBlue;
|
2018-07-28 02:59:14 +00:00
|
|
|
|
public readonly BitmapAnimator HoverWorker;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
|
|
|
|
|
private SaveFile SAV => SE.SAV;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public SlotChangeInfo DragInfo;
|
|
|
|
|
public readonly List<BoxEditor> Boxes = new List<BoxEditor>();
|
2018-05-05 15:07:22 +00:00
|
|
|
|
public readonly List<ISlotViewer<PictureBox>> OtherSlots = new List<ISlotViewer<PictureBox>>();
|
2017-05-31 02:17:37 +00:00
|
|
|
|
public event DragEventHandler RequestExternalDragDrop;
|
2018-05-13 15:14:46 +00:00
|
|
|
|
private readonly ToolTip ShowSet = new ToolTip {InitialDelay = 200, IsBalloon = false};
|
2018-05-11 23:58:29 +00:00
|
|
|
|
private readonly SoundPlayer Sounds = new SoundPlayer();
|
2018-05-13 15:14:46 +00:00
|
|
|
|
private PictureBox HoveredSlot;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
public SlotChangeManager(SAVEditor se)
|
|
|
|
|
{
|
2018-07-28 02:59:14 +00:00
|
|
|
|
HoverWorker = new BitmapAnimator(Resources.slotHover);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
SE = se;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-01-30 01:52:21 +00:00
|
|
|
|
public void Reset() { DragInfo = new SlotChangeInfo(SAV); ColorizedBox = ColorizedSlot = -1; }
|
2018-05-11 23:38:09 +00:00
|
|
|
|
public bool CanStartDrag => DragInfo.LeftMouseIsDown && !Cursor.Position.Equals(MouseDownPosition);
|
|
|
|
|
private Point MouseDownPosition { get; set; }
|
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void SetCursor(Cursor z, object sender)
|
|
|
|
|
{
|
|
|
|
|
if (SE != null)
|
2017-12-16 22:11:41 +00:00
|
|
|
|
DragInfo.Cursor = ((Control)sender).FindForm().Cursor = z;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void MouseEnter(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var pb = (PictureBox)sender;
|
|
|
|
|
if (pb.Image == null)
|
|
|
|
|
return;
|
2018-05-13 15:14:46 +00:00
|
|
|
|
BeginHoverSlot(pb);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-05-13 15:14:46 +00:00
|
|
|
|
private void BeginHoverSlot(PictureBox pb)
|
|
|
|
|
{
|
2018-05-11 23:58:29 +00:00
|
|
|
|
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
|
|
|
|
|
var data = view.GetSlotData(pb);
|
|
|
|
|
var pk = SAV.GetStoredSlot(data.Offset);
|
2018-05-13 15:14:46 +00:00
|
|
|
|
HoveredSlot = pb;
|
2018-05-11 23:58:29 +00:00
|
|
|
|
|
2018-07-23 03:04:28 +00:00
|
|
|
|
OriginalBackground = pb.BackgroundImage;
|
|
|
|
|
|
|
|
|
|
Bitmap hover;
|
|
|
|
|
if (GlowHover)
|
|
|
|
|
{
|
2018-07-28 02:59:14 +00:00
|
|
|
|
HoverWorker.Stop();
|
2018-07-23 03:04:28 +00:00
|
|
|
|
|
2018-07-28 02:59:14 +00:00
|
|
|
|
var bgr = new[] { GlowInitial.B, GlowInitial.G, GlowInitial.R };
|
|
|
|
|
SpriteUtil.GetSpriteGlow(pk, bgr, out var glowdata, out var GlowBase);
|
2018-07-23 03:04:28 +00:00
|
|
|
|
hover = ImageUtil.LayerImage(GlowBase, Resources.slotHover, 0, 0);
|
2018-07-28 02:59:14 +00:00
|
|
|
|
HoverWorker.GlowToColor = GlowFinal;
|
|
|
|
|
HoverWorker.GlowFromColor = GlowInitial;
|
|
|
|
|
HoverWorker.Start(pb, GlowBase, glowdata, OriginalBackground);
|
2018-07-23 03:04:28 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
hover = Resources.slotHover;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pb.BackgroundImage = CurrentBackground = OriginalBackground == null ? hover : ImageUtil.LayerImage(OriginalBackground, hover, 0, 0);
|
|
|
|
|
|
2018-05-13 03:04:58 +00:00
|
|
|
|
if (Settings.Default.HoverSlotShowText)
|
|
|
|
|
ShowSimulatorSetTooltip(pb, pk);
|
|
|
|
|
if (Settings.Default.HoverSlotPlayCry)
|
|
|
|
|
PlayCry(pk);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-05-13 15:14:46 +00:00
|
|
|
|
private void EndHoverSlot()
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2018-07-23 03:04:28 +00:00
|
|
|
|
if (HoveredSlot != null)
|
2018-07-28 16:56:13 +00:00
|
|
|
|
HoverCancel();
|
2018-05-13 15:14:46 +00:00
|
|
|
|
ShowSet.RemoveAll();
|
2018-05-12 00:05:42 +00:00
|
|
|
|
Sounds.Stop();
|
2018-05-13 15:14:46 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-07-28 16:56:13 +00:00
|
|
|
|
public void HoverCancel()
|
|
|
|
|
{
|
|
|
|
|
HoverWorker.Stop();
|
|
|
|
|
HoveredSlot = null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-13 15:14:46 +00:00
|
|
|
|
public void RefreshHoverSlot(ISlotViewer<PictureBox> parent)
|
|
|
|
|
{
|
|
|
|
|
if (HoveredSlot == null || !parent.SlotPictureBoxes.Contains(HoveredSlot))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
BeginHoverSlot(HoveredSlot);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-05-13 15:14:46 +00:00
|
|
|
|
public void MouseLeave(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
EndHoverSlot();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
var pb = (PictureBox)sender;
|
|
|
|
|
if (pb.BackgroundImage != CurrentBackground)
|
|
|
|
|
return;
|
|
|
|
|
pb.BackgroundImage = OriginalBackground;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void MouseClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!DragInfo.DragDropInProgress)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SE.ClickSlot(sender, e);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void MouseUp(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
|
|
|
|
DragInfo.LeftMouseIsDown = false;
|
|
|
|
|
if (e.Button == MouseButtons.Right)
|
|
|
|
|
DragInfo.RightMouseIsDown = false;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void MouseDown(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
2018-05-11 23:38:09 +00:00
|
|
|
|
{
|
2017-05-23 04:55:05 +00:00
|
|
|
|
DragInfo.LeftMouseIsDown = true;
|
2018-05-11 23:38:09 +00:00
|
|
|
|
MouseDownPosition = Cursor.Position;
|
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
if (e.Button == MouseButtons.Right)
|
|
|
|
|
DragInfo.RightMouseIsDown = true;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Action != DragAction.Cancel && e.Action != DragAction.Drop)
|
|
|
|
|
return;
|
|
|
|
|
DragInfo.LeftMouseIsDown = false;
|
|
|
|
|
DragInfo.RightMouseIsDown = false;
|
|
|
|
|
DragInfo.DragDropInProgress = false;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void DragEnter(object sender, DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.AllowedEffect == (DragDropEffects.Copy | DragDropEffects.Link)) // external file
|
|
|
|
|
e.Effect = DragDropEffects.Copy;
|
|
|
|
|
else if (e.Data != null) // within
|
|
|
|
|
e.Effect = DragDropEffects.Move;
|
|
|
|
|
|
|
|
|
|
if (DragInfo.DragDropInProgress)
|
|
|
|
|
SetCursor((Cursor)DragInfo.Cursor, sender);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-05-11 23:38:09 +00:00
|
|
|
|
public void MouseMove(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!CanStartDrag)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Abort if there is no Pokemon in the given slot.
|
|
|
|
|
PictureBox pb = (PictureBox)sender;
|
|
|
|
|
if (pb.Image == null)
|
|
|
|
|
return;
|
|
|
|
|
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
|
|
|
|
|
var src = view.GetSlotData(pb);
|
|
|
|
|
if (!src.Editable || SAV.IsSlotLocked(src.Box, src.Slot))
|
|
|
|
|
return;
|
|
|
|
|
bool encrypt = Control.ModifierKeys == Keys.Control;
|
2018-05-12 15:41:23 +00:00
|
|
|
|
HandleMovePKM(pb, encrypt);
|
2018-05-11 23:38:09 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-05-11 23:38:09 +00:00
|
|
|
|
public void DragDrop(object sender, DragEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PictureBox pb = (PictureBox)sender;
|
|
|
|
|
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
|
|
|
|
|
var src = view.GetSlotData(pb);
|
|
|
|
|
if (!src.Editable || SAV.IsSlotLocked(src.Box, src.Slot))
|
|
|
|
|
{
|
2018-05-11 23:58:29 +00:00
|
|
|
|
SystemSounds.Asterisk.Play();
|
2018-05-11 23:38:09 +00:00
|
|
|
|
e.Effect = DragDropEffects.Copy;
|
|
|
|
|
DragInfo.Reset();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool overwrite = Control.ModifierKeys == Keys.Alt;
|
|
|
|
|
bool clone = Control.ModifierKeys == Keys.Control;
|
|
|
|
|
DragInfo.Destination = src;
|
|
|
|
|
HandleDropPKM(sender, e, overwrite, clone);
|
|
|
|
|
}
|
2018-05-05 15:07:22 +00:00
|
|
|
|
|
2018-05-11 23:58:29 +00:00
|
|
|
|
private void ShowSimulatorSetTooltip(Control pb, PKM pk)
|
|
|
|
|
{
|
|
|
|
|
if (pk.Species == 0)
|
2018-05-21 01:33:38 +00:00
|
|
|
|
{
|
2018-07-27 02:34:27 +00:00
|
|
|
|
ShowSet.RemoveAll();
|
|
|
|
|
return;
|
2018-05-21 01:33:38 +00:00
|
|
|
|
}
|
2018-12-17 19:17:19 +00:00
|
|
|
|
var text = GetLocalizedPreviewText(pk, Settings.Default.Language);
|
2018-09-16 19:44:00 +00:00
|
|
|
|
ShowSet.SetToolTip(pb, text);
|
2018-05-11 23:58:29 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-05-11 23:58:29 +00:00
|
|
|
|
private void PlayCry(PKM pk)
|
|
|
|
|
{
|
|
|
|
|
if (pk.Species == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-12-17 19:17:19 +00:00
|
|
|
|
string path = GetCryPath(pk, Main.CryPath);
|
2018-05-11 23:58:29 +00:00
|
|
|
|
if (!File.Exists(path))
|
2018-12-17 19:17:19 +00:00
|
|
|
|
return;
|
2018-05-11 23:58:29 +00:00
|
|
|
|
|
|
|
|
|
Sounds.SoundLocation = path;
|
2018-12-17 19:17:19 +00:00
|
|
|
|
try { Sounds.Play(); }
|
|
|
|
|
catch { }
|
2018-05-11 23:58:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-12 15:13:39 +00:00
|
|
|
|
private static ISlotViewer<T> GetViewParent<T>(T pb) where T : Control
|
2018-05-05 15:07:22 +00:00
|
|
|
|
=> WinFormsUtil.FindFirstControlOfType<ISlotViewer<T>>(pb);
|
|
|
|
|
|
2018-05-12 15:41:23 +00:00
|
|
|
|
public void HandleMovePKM(PictureBox pb, bool encrypt)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
// Create a temporary PKM file to perform a drag drop operation.
|
|
|
|
|
|
|
|
|
|
// Set flag to prevent re-entering.
|
|
|
|
|
DragInfo.DragDropInProgress = true;
|
|
|
|
|
|
|
|
|
|
// Prepare Data
|
2018-05-05 15:07:22 +00:00
|
|
|
|
DragInfo.Source = GetViewParent(pb).GetSlotData(pb);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
DragInfo.Source.OriginalData = SAV.GetData(DragInfo.Source.Offset, SAV.SIZE_STORED);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
// Make a new file name based off the PID
|
2018-05-12 15:41:23 +00:00
|
|
|
|
string newfile = CreateDragDropPKM(pb, encrypt, out bool external);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
DragInfo.Reset();
|
|
|
|
|
SetCursor(SE.GetDefaultCursor, pb);
|
|
|
|
|
|
|
|
|
|
// Browser apps need time to load data since the file isn't moved to a location on the user's local storage.
|
|
|
|
|
// Tested 10ms -> too quick, 100ms was fine. 500ms should be safe?
|
2018-12-17 19:17:19 +00:00
|
|
|
|
// Keep it to 10 seconds; Discord upload only stores the file path until you click Upload.
|
|
|
|
|
int delay = external ? 10_000 : 0;
|
2017-07-02 02:43:51 +00:00
|
|
|
|
DeleteAsync(newfile, delay);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
if (DragInfo.Source.IsParty || DragInfo.Destination.IsParty)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SE.SetParty();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-07-02 02:43:51 +00:00
|
|
|
|
private async void DeleteAsync(string path, int delay)
|
|
|
|
|
{
|
2018-05-25 04:22:44 +00:00
|
|
|
|
await Task.Delay(delay).ConfigureAwait(true);
|
2017-07-02 02:43:51 +00:00
|
|
|
|
if (File.Exists(path) && DragInfo.CurrentPath == null)
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-05-12 15:41:23 +00:00
|
|
|
|
private string CreateDragDropPKM(PictureBox pb, bool encrypt, out bool external)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
byte[] dragdata = SAV.DecryptPKM(DragInfo.Source.OriginalData);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
Array.Resize(ref dragdata, SAV.SIZE_STORED);
|
|
|
|
|
|
|
|
|
|
// Make File
|
2018-09-16 19:44:00 +00:00
|
|
|
|
PKM pkx = SAV.GetPKM(dragdata);
|
|
|
|
|
string newfile = FileUtil.GetPKMTempFileName(pkx, encrypt);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
TryMakeDragDropPKM(pb, encrypt, pkx, newfile, out external);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception x)
|
|
|
|
|
{
|
|
|
|
|
WinFormsUtil.Error("Drag & Drop Error", x);
|
|
|
|
|
external = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newfile;
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private bool TryMakeDragDropPKM(PictureBox pb, bool encrypt, PKM pkx, string newfile, out bool external)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
File.WriteAllBytes(newfile, encrypt ? pkx.EncryptedBoxData : pkx.DecryptedBoxData);
|
|
|
|
|
var img = (Bitmap)pb.Image;
|
|
|
|
|
SetCursor(new Cursor(img.GetHicon()), pb);
|
2018-07-28 16:56:13 +00:00
|
|
|
|
HoverCancel();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
pb.Image = null;
|
|
|
|
|
pb.BackgroundImage = Resources.slotDrag;
|
|
|
|
|
// Thread Blocks on DoDragDrop
|
|
|
|
|
DragInfo.CurrentPath = newfile;
|
|
|
|
|
DragDropEffects result = pb.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move);
|
|
|
|
|
external = !DragInfo.Source.IsValid || result != DragDropEffects.Link;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (external || DragInfo.SameSlot || result != DragDropEffects.Link) // not dropped to another box slot, restore img
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
pb.Image = img;
|
|
|
|
|
pb.BackgroundImage = OriginalBackground;
|
2018-05-05 15:07:22 +00:00
|
|
|
|
SetCursor(SE.GetDefaultCursor, pb);
|
|
|
|
|
return false;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result == DragDropEffects.Copy) // viewed in tabs or cloned
|
|
|
|
|
{
|
|
|
|
|
if (!DragInfo.Destination.IsValid) // apply 'view' highlight
|
|
|
|
|
SetColor(DragInfo.Source.Box, DragInfo.Source.Slot, Resources.slotView);
|
|
|
|
|
external = false;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return true;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void SetSlotSprite(SlotChange loc, PKM pk, BoxEditor x = null) => (x ?? SE.Box).SetSlotFiller(pk, loc.Box, loc.Slot);
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void HandleDropPKM(object sender, DragEventArgs e, bool overwrite, bool clone)
|
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
var pb = (PictureBox)sender;
|
|
|
|
|
DragInfo.Destination = GetViewParent(pb).GetSlotData(pb);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
// Check for In-Dropped files (PKX,SAV,ETC)
|
|
|
|
|
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
|
|
|
|
|
if (Directory.Exists(files[0])) { SE.LoadBoxes(out string _, files[0]); return; }
|
|
|
|
|
if (DragInfo.SameSlot)
|
|
|
|
|
{
|
|
|
|
|
e.Effect = DragDropEffects.Link;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (SAV.IsSlotLocked(DragInfo.Destination.Box, DragInfo.Destination.Slot))
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2018-12-17 19:17:19 +00:00
|
|
|
|
AlertInvalidate(MessageStrings.MsgSaveSlotLocked);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-05 15:07:22 +00:00
|
|
|
|
bool noEgg = DragInfo.Destination.IsParty && SE.SAV.IsPartyAllEggs(DragInfo.Destination.Slot) && !SE.HaX;
|
2017-05-24 01:35:32 +00:00
|
|
|
|
if (DragInfo.Source.Offset < 0) // external source
|
|
|
|
|
{
|
2017-06-22 03:24:42 +00:00
|
|
|
|
if (!TryLoadFiles(files, e, noEgg))
|
2018-12-17 19:17:19 +00:00
|
|
|
|
AlertInvalidate(MessageStrings.MsgSaveSlotBadData);
|
2017-06-22 03:24:42 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!TrySetPKMDestination(sender, e, overwrite, clone, noEgg))
|
|
|
|
|
{
|
2018-12-17 19:17:19 +00:00
|
|
|
|
AlertInvalidate(MessageStrings.MsgSaveSlotEmpty);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return;
|
2017-05-24 01:35:32 +00:00
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
if (DragInfo.Source.Parent == null) // internal file
|
|
|
|
|
DragInfo.Reset();
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-06-22 03:24:42 +00:00
|
|
|
|
private void AlertInvalidate(string msg)
|
|
|
|
|
{
|
|
|
|
|
DragInfo.Destination.Slot = -1; // Invalidate
|
|
|
|
|
WinFormsUtil.Alert(msg);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-09-16 19:44:00 +00:00
|
|
|
|
private bool TryLoadFiles(IReadOnlyList<string> files, DragEventArgs e, bool noEgg)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2018-09-16 19:44:00 +00:00
|
|
|
|
if (files.Count == 0)
|
2017-07-11 01:57:03 +00:00
|
|
|
|
return false;
|
2018-09-16 19:44:00 +00:00
|
|
|
|
|
|
|
|
|
var temp = FileUtil.GetSingleFromPath(files[0], SAV);
|
|
|
|
|
if (temp == null)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2017-05-31 02:17:37 +00:00
|
|
|
|
RequestExternalDragDrop?.Invoke(this, e); // pass thru
|
2018-05-25 02:05:13 +00:00
|
|
|
|
return true; // treat as handled
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
PKM pk = PKMConverter.ConvertToType(temp, SAV.PKMType, out string c);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
if (pk == null)
|
|
|
|
|
{
|
|
|
|
|
WinFormsUtil.Error(c);
|
2017-07-02 02:43:51 +00:00
|
|
|
|
Debug.WriteLine(c);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return false;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 03:24:42 +00:00
|
|
|
|
if (noEgg && (pk.Species == 0 || pk.IsEgg))
|
|
|
|
|
return false;
|
|
|
|
|
|
2018-07-24 22:49:00 +00:00
|
|
|
|
if (PKMConverter.IsIncompatibleGB(pk.Format, SAV.Japanese, pk.Japanese))
|
|
|
|
|
{
|
|
|
|
|
c = PKMConverter.GetIncompatibleGBMessage(pk, SAV.Japanese);
|
|
|
|
|
WinFormsUtil.Error(c);
|
|
|
|
|
Debug.WriteLine(c);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-21 04:32:33 +00:00
|
|
|
|
var errata = SAV.IsPKMCompatible(pk);
|
|
|
|
|
if (errata.Count > 0)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
string concat = string.Join(Environment.NewLine, errata);
|
2018-12-17 19:17:19 +00:00
|
|
|
|
if (DialogResult.Yes != WinFormsUtil.Prompt(MessageBoxButtons.YesNo, concat, MessageStrings.MsgContinue))
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2017-07-02 02:43:51 +00:00
|
|
|
|
Debug.WriteLine(c);
|
|
|
|
|
Debug.WriteLine(concat);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return false;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetPKM(pk, false, Resources.slotSet);
|
2017-07-02 02:43:51 +00:00
|
|
|
|
Debug.WriteLine(c);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return true;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-06-22 03:24:42 +00:00
|
|
|
|
private bool TrySetPKMDestination(object sender, DragEventArgs e, bool overwrite, bool clone, bool noEgg)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
PKM pkz = GetPKM(true);
|
2017-06-22 03:24:42 +00:00
|
|
|
|
if (noEgg && (pkz.Species == 0 || pkz.IsEgg))
|
|
|
|
|
return false;
|
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
if (DragInfo.Source.IsValid)
|
2017-06-22 03:24:42 +00:00
|
|
|
|
TrySetPKMSource(sender, overwrite, clone);
|
2017-05-23 04:55:05 +00:00
|
|
|
|
|
|
|
|
|
// Copy from temp to destination slot.
|
|
|
|
|
SetPKM(pkz, false, null);
|
|
|
|
|
|
|
|
|
|
e.Effect = clone ? DragDropEffects.Copy : DragDropEffects.Link;
|
|
|
|
|
SetCursor(SE.GetDefaultCursor, sender);
|
2017-06-22 03:24:42 +00:00
|
|
|
|
return true;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private bool TrySetPKMSource(object sender, bool overwrite, bool clone)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (overwrite && DragInfo.Destination.IsValid) // overwrite delete old slot
|
|
|
|
|
{
|
|
|
|
|
// Clear from slot
|
|
|
|
|
SetPKM(SAV.BlankPKM, true, null);
|
2018-07-27 02:34:27 +00:00
|
|
|
|
return true;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
if (!clone && DragInfo.Destination.IsValid)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
|
|
|
|
// Load data from destination
|
|
|
|
|
PKM pk = ((PictureBox)sender).Image != null
|
|
|
|
|
? GetPKM(false)
|
|
|
|
|
: SAV.BlankPKM;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
// Set destination pokemon data to source slot
|
|
|
|
|
SetPKM(pk, true, null);
|
2018-07-27 02:34:27 +00:00
|
|
|
|
return true;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
return false;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetColor(int box, int slot, Image img)
|
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
foreach (var boxview in Boxes)
|
|
|
|
|
updateView(boxview);
|
|
|
|
|
foreach (var other in OtherSlots)
|
|
|
|
|
updateView(other);
|
|
|
|
|
|
|
|
|
|
void updateView(ISlotViewer<PictureBox> view)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
if (view.ViewIndex == ColorizedBox && ColorizedSlot >= 0)
|
|
|
|
|
view.SlotPictureBoxes[ColorizedSlot].BackgroundImage = null;
|
|
|
|
|
if (view.ViewIndex == box && slot >= 0)
|
|
|
|
|
view.SlotPictureBoxes[slot].BackgroundImage = img;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-05-05 15:07:22 +00:00
|
|
|
|
|
2018-01-30 01:52:21 +00:00
|
|
|
|
ColorizedBox = box;
|
|
|
|
|
ColorizedSlot = slot;
|
|
|
|
|
ColorizedColor = img;
|
2018-07-23 03:04:28 +00:00
|
|
|
|
|
|
|
|
|
OriginalBackground = img;
|
|
|
|
|
if (HoverWorker != null)
|
|
|
|
|
HoverWorker.OriginalBackground = img;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PKM Get Set
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private PKM GetPKM(bool src) => GetPKM(src ? DragInfo.Source : DragInfo.Destination);
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public PKM GetPKM(SlotChange slot)
|
|
|
|
|
{
|
|
|
|
|
int o = slot.Offset;
|
|
|
|
|
if (o < 0)
|
|
|
|
|
return slot.PKM;
|
2017-11-22 02:32:23 +00:00
|
|
|
|
|
|
|
|
|
if (slot.IsParty)
|
|
|
|
|
return SAV.GetPartySlot(o);
|
|
|
|
|
|
|
|
|
|
var pk = SAV.GetStoredSlot(o);
|
|
|
|
|
pk.Slot = slot.Slot;
|
|
|
|
|
pk.Box = slot.Box;
|
|
|
|
|
return pk;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void SetPKM(PKM pk, bool src, Image img) => SetPKM(pk, src ? DragInfo.Source : DragInfo.Destination, src, img);
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
public void SetPKM(PKM pk, SlotChange slot, bool src, Image img)
|
|
|
|
|
{
|
|
|
|
|
if (slot.IsParty)
|
|
|
|
|
{
|
|
|
|
|
SetPKMParty(pk, src, slot);
|
|
|
|
|
if (img == Resources.slotDel)
|
2018-05-05 15:07:22 +00:00
|
|
|
|
slot.Slot = SAV.PartyCount;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
SetColor(slot.Box, slot.Slot, img ?? Resources.slotSet);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int o = slot.Offset;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SAV.SetStoredSlot(pk, o);
|
2018-05-05 15:07:22 +00:00
|
|
|
|
if (slot.Type == StorageSlotType.Box)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
foreach (var boxview in Boxes)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
if (boxview.CurrentBox != slot.Box)
|
|
|
|
|
continue;
|
|
|
|
|
Debug.WriteLine($"Setting to {boxview.Parent.Name}'s [{boxview.CurrentBox + 1:d2}]|{boxview.CurrentBoxName} at Slot {slot.Slot + 1}.");
|
2017-05-23 04:55:05 +00:00
|
|
|
|
SetSlotSprite(slot, pk, boxview);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
SetColor(slot.Box, slot.Slot, img ?? Resources.slotSet);
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2017-05-23 04:55:05 +00:00
|
|
|
|
private void SetPKMParty(PKM pk, bool src, SlotChange slot)
|
|
|
|
|
{
|
|
|
|
|
int o = slot.Offset;
|
|
|
|
|
if (src)
|
|
|
|
|
{
|
|
|
|
|
if (pk.Species == 0) // Empty Slot
|
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
SAV.DeletePartySlot(slot.Slot);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SE.SetParty();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-05-05 15:07:22 +00:00
|
|
|
|
if (SAV.PartyCount < slot.Slot)
|
2017-05-23 04:55:05 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
o = SAV.GetPartyOffset(SAV.PartyCount);
|
2018-05-05 15:07:22 +00:00
|
|
|
|
slot.Slot = SAV.PartyCount;
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SAV.SetPartySlot(pk, o);
|
|
|
|
|
SE.SetParty();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 06:37:55 +00:00
|
|
|
|
// Utility
|
|
|
|
|
public void SwapBoxes(int index, int other)
|
|
|
|
|
{
|
|
|
|
|
if (index == other)
|
|
|
|
|
return;
|
|
|
|
|
SAV.SwapBox(index, other);
|
2018-09-16 19:44:00 +00:00
|
|
|
|
UpdateBoxViewAtBoxIndexes(index, other);
|
2018-01-24 06:37:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2018-07-23 00:26:19 +00:00
|
|
|
|
Sounds.Dispose();
|
2018-12-11 04:32:08 +00:00
|
|
|
|
HoverWorker.Dispose();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SE?.Dispose();
|
|
|
|
|
OriginalBackground?.Dispose();
|
|
|
|
|
CurrentBackground?.Dispose();
|
2018-01-30 01:52:21 +00:00
|
|
|
|
ColorizedColor?.Dispose();
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
2018-09-16 19:44:00 +00:00
|
|
|
|
|
|
|
|
|
private void UpdateBoxViewAtBoxIndexes(params int[] boxIndexes)
|
|
|
|
|
{
|
|
|
|
|
foreach (var box in Boxes)
|
|
|
|
|
{
|
|
|
|
|
var current = box.CurrentBox;
|
|
|
|
|
if (!boxIndexes.Contains(current))
|
|
|
|
|
continue;
|
|
|
|
|
box.ResetSlots();
|
|
|
|
|
box.ResetBoxNames(current);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 19:17:19 +00:00
|
|
|
|
private static string GetCryPath(PKM pk, string cryFolder)
|
|
|
|
|
{
|
|
|
|
|
var name = PKX.GetResourceStringSprite(pk.Species, pk.AltForm, pk.Gender, pk.Format).Replace('_', '-').Substring(1);
|
|
|
|
|
var path = Path.Combine(cryFolder, $"{name}.wav");
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
path = Path.Combine(cryFolder, $"{pk.Species}.wav");
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetLocalizedPreviewText(PKM pk, string language)
|
2018-09-16 19:44:00 +00:00
|
|
|
|
{
|
|
|
|
|
var set = new ShowdownSet(pk);
|
|
|
|
|
if (pk.Format <= 2) // Nature preview from IVs
|
|
|
|
|
set.Nature = Experience.GetNatureVC(pk.EXP);
|
2018-12-17 19:17:19 +00:00
|
|
|
|
return set.LocalizedText(language);
|
2018-09-16 19:44:00 +00:00
|
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|