Clean up / fix external drag in (#2383)

* stash

* Consolidate some logic
This commit is contained in:
Kurt 2019-09-03 19:54:41 -07:00 committed by GitHub
parent 11bc9f064f
commit 6d8ff992e2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 58 deletions

View file

@ -1,21 +0,0 @@
using System;
namespace PKHeX.Core
{
/// <summary>
/// <see cref="ISlotInfo"/> originating from outside of a save file (e.g. a saved file on a hard drive).
/// </summary>
public sealed class ExternalSlotInfo : ISlotInfo
{
private readonly PKM Data;
public ExternalSlotInfo(PKM pkm) => Data = pkm;
public int Slot { get; } = -1;
public bool Equals(ISlotInfo other) => false;
public bool CanWriteTo(SaveFile SAV) => false;
public WriteBlockedMessage CanWriteTo(SaveFile SAV, PKM pkm) => WriteBlockedMessage.InvalidDestination;
public bool WriteTo(SaveFile sav, PKM pkm, PKMImportSetting setting = PKMImportSetting.UseDefault) => throw new InvalidOperationException();
public PKM Read(SaveFile sav) => Data;
}
}

View file

@ -8,5 +8,9 @@ namespace PKHeX.Core
{
public ISlotInfo Slot;
public ISlotViewer<T> View;
public PKM ReadCurrent() => Slot.Read(View.SAV);
public bool CanWriteTo() => Slot.CanWriteTo(View.SAV);
public WriteBlockedMessage CanWriteTo(PKM pkm) => Slot.CanWriteTo(View.SAV, pkm);
}
}

View file

@ -6,7 +6,7 @@ namespace PKHeX.WinForms.Controls
{
public class DragManager
{
public SlotChangeInfo<Cursor> Info { get; private set; }
public SlotChangeInfo<Cursor, PictureBox> Info { get; private set; }
public event DragEventHandler RequestExternalDragDrop;
public void RequestDD(object sender, DragEventArgs e) => RequestExternalDragDrop?.Invoke(sender, e);
@ -22,7 +22,7 @@ namespace PKHeX.WinForms.Controls
public void Initialize()
{
Info = new SlotChangeInfo<Cursor>();
Info = new SlotChangeInfo<Cursor, PictureBox>();
}
public void Reset() => Info.Reset();
@ -31,16 +31,16 @@ namespace PKHeX.WinForms.Controls
public bool CanStartDrag => Info.LeftMouseIsDown && !Cursor.Position.Equals(MouseDownPosition);
}
public class SlotChangeInfo<T>
public class SlotChangeInfo<TCursor, TImageSource>
{
public bool LeftMouseIsDown { get; set; }
public bool DragDropInProgress { get; set; }
public T Cursor { get; set; }
public TCursor Cursor { get; set; }
public string CurrentPath { get; set; }
public ISlotInfo Source { get; set; }
public ISlotInfo Destination { get; set; }
public SlotViewInfo<TImageSource> Source { get; set; }
public SlotViewInfo<TImageSource> Destination { get; set; }
public SlotChangeInfo()
{
@ -55,5 +55,6 @@ namespace PKHeX.WinForms.Controls
}
public bool SameLocation => Source?.Equals(Destination) ?? false;
public bool DragIsParty => Source?.Slot is SlotInfoParty || Destination?.Slot is SlotInfoParty;
}
}

View file

@ -22,7 +22,6 @@ namespace PKHeX.WinForms.Controls
public readonly DragManager Drag = new DragManager();
public SaveDataEditor<PictureBox> Env { get; set; }
private SaveFile SAV => SE.SAV;
public readonly List<BoxEditor> Boxes = new List<BoxEditor>();
public readonly SlotHoverHandler Hover = new SlotHoverHandler();
@ -57,6 +56,7 @@ namespace PKHeX.WinForms.Controls
{
if (e.Button == MouseButtons.Left)
Drag.Info.LeftMouseIsDown = false;
Drag.Info.Source = null;
}
public void MouseDown(object sender, MouseEventArgs e)
@ -78,7 +78,7 @@ namespace PKHeX.WinForms.Controls
public void DragEnter(object sender, DragEventArgs e)
{
if (e.AllowedEffect == (DragDropEffects.Copy | DragDropEffects.Link)) // external file
if (e.AllowedEffect.HasFlag(DragDropEffects.Copy)) // external file
e.Effect = DragDropEffects.Copy;
else if (e.Data != null) // within
e.Effect = DragDropEffects.Move;
@ -87,6 +87,13 @@ namespace PKHeX.WinForms.Controls
Drag.SetCursor(((Control)sender).FindForm(), Drag.Info.Cursor);
}
private static SlotViewInfo<T> GetSlotInfo<T>(T pb) where T : Control
{
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<T>>(pb);
var src = view.GetSlotData(pb);
return new SlotViewInfo<T> { Slot = src, View = view };
}
public void MouseMove(object sender, MouseEventArgs e)
{
if (!Drag.CanStartDrag)
@ -96,9 +103,8 @@ namespace PKHeX.WinForms.Controls
PictureBox pb = (PictureBox)sender;
if (pb.Image == null)
return;
var view = WinFormsUtil.FindFirstControlOfType<ISlotViewer<PictureBox>>(pb);
var src = view.GetSlotData(pb);
if (!src.CanWriteTo(SAV))
var src = GetSlotInfo(pb);
if (!src.CanWriteTo())
return;
bool encrypt = Control.ModifierKeys == Keys.Control;
HandleMovePKM(pb, encrypt);
@ -107,9 +113,8 @@ namespace PKHeX.WinForms.Controls
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.CanWriteTo(SAV))
var info = GetSlotInfo(pb);
if (!info.CanWriteTo())
{
SystemSounds.Asterisk.Play();
e.Effect = DragDropEffects.Copy;
@ -118,13 +123,10 @@ namespace PKHeX.WinForms.Controls
}
var mod = SlotUtil.GetDropModifier();
Drag.Info.Destination = src;
Drag.Info.Destination = info;
HandleDropPKM(pb, e, mod);
}
private static ISlotViewer<T> GetViewParent<T>(T pb) where T : Control
=> WinFormsUtil.FindFirstControlOfType<ISlotViewer<T>>(pb);
private void HandleMovePKM(PictureBox pb, bool encrypt)
{
// Create a temporary PKM file to perform a drag drop operation.
@ -133,13 +135,13 @@ namespace PKHeX.WinForms.Controls
Drag.Info.DragDropInProgress = true;
// Prepare Data
Drag.Info.Source = GetViewParent(pb).GetSlotData(pb);
Drag.Info.Source.Read(SAV);
Drag.Info.Source = GetSlotInfo(pb);
// Make a new file name based off the PID
string newfile = CreateDragDropPKM(pb, encrypt, out bool external);
// drop finished, clean up
Drag.Info.Source = null;
Drag.Reset();
Drag.ResetCursor(pb.FindForm());
@ -148,7 +150,7 @@ namespace PKHeX.WinForms.Controls
// Keep it to 10 seconds; Discord upload only stores the file path until you click Upload.
int delay = external ? 10_000 : 0;
DeleteAsync(newfile, delay);
if (Drag.Info.Source is SlotInfoParty || Drag.Info.Destination is SlotInfoParty)
if (Drag.Info.DragIsParty)
SE.SetParty();
}
@ -162,7 +164,7 @@ namespace PKHeX.WinForms.Controls
private string CreateDragDropPKM(PictureBox pb, bool encrypt, out bool external)
{
// Make File
PKM pk = Drag.Info.Source.Read(SAV);
PKM pk = Drag.Info.Source.ReadCurrent();
string newfile = FileUtil.GetPKMTempFileName(pk, encrypt);
try
{
@ -202,7 +204,7 @@ namespace PKHeX.WinForms.Controls
if (result == DragDropEffects.Copy) // viewed in tabs or cloned
{
if (Drag.Info.Destination == null) // apply 'view' highlight
Env.Slots.Get(Drag.Info.Source);
Env.Slots.Get(Drag.Info.Source.Slot);
return false;
}
return true;
@ -220,7 +222,6 @@ namespace PKHeX.WinForms.Controls
e.Effect = mod == DropModifier.Clone ? DragDropEffects.Copy : DragDropEffects.Link;
// file
Drag.Info.Destination = GetViewParent(pb).GetSlotData(pb);
if (Drag.Info.SameLocation)
{
e.Effect = DragDropEffects.Link;
@ -231,7 +232,7 @@ namespace PKHeX.WinForms.Controls
if (Drag.Info.Source == null) // external source
{
bool badDest = !dest.CanWriteTo(SAV);
bool badDest = !dest.CanWriteTo();
if (!TryLoadFiles(files, e, badDest))
WinFormsUtil.Alert(MessageStrings.MsgSaveSlotBadData);
}
@ -254,7 +255,7 @@ namespace PKHeX.WinForms.Controls
if (files.Count == 0)
return false;
var sav = SAV;
var sav = Drag.Info.Destination.View.SAV;
var path = files[0];
var temp = FileUtil.GetSingleFromPath(path, sav);
if (temp == null)
@ -294,15 +295,15 @@ namespace PKHeX.WinForms.Controls
}
}
Env.Slots.Set(Drag.Info.Destination, pk);
Env.Slots.Set(Drag.Info.Destination.Slot, pk);
Debug.WriteLine(c);
return true;
}
private bool TrySetPKMDestination(PictureBox pb, DropModifier mod)
{
PKM pk = Drag.Info.Source.Read(SAV);
var msg = Drag.Info.Destination.CanWriteTo(SAV, pk);
PKM pk = Drag.Info.Source.ReadCurrent();
var msg = Drag.Info.Destination.CanWriteTo(pk);
if (msg != WriteBlockedMessage.None)
return false;
@ -310,7 +311,7 @@ namespace PKHeX.WinForms.Controls
TrySetPKMSource(pb, mod);
// Copy from temp to destination slot.
Env.Slots.Set(Drag.Info.Destination, pk);
Env.Slots.Set(Drag.Info.Destination.Slot, pk);
Drag.ResetCursor(pb.FindForm());
return true;
}
@ -322,17 +323,17 @@ namespace PKHeX.WinForms.Controls
if (sender.Image == null || mod == DropModifier.Overwrite)
{
Env.Slots.Delete(Drag.Info.Source);
Env.Slots.Delete(Drag.Info.Source.Slot);
return true;
}
var pk = Drag.Info.Destination.Read(SAV);
Env.Slots.Set(Drag.Info.Source, pk);
var pk = Drag.Info.Destination.ReadCurrent();
Env.Slots.Set(Drag.Info.Source.Slot, pk);
return true;
}
// Utility
public void SwapBoxes(int index, int other)
public void SwapBoxes(int index, int other, SaveFile SAV)
{
if (index == other)
return;

View file

@ -1127,7 +1127,6 @@ namespace PKHeX.WinForms
try
{
File.WriteAllBytes(newfile, data);
C_SAV.M.Drag.Info.Source = new ExternalSlotInfo(pk);
var pb = (PictureBox)sender;
if (pb.Image != null)

View file

@ -74,13 +74,13 @@ namespace PKHeX.WinForms
{
int index = Boxes.FindIndex(z => z == ((Button)s).Parent);
int other = (index + Boxes.Count - 1) % Boxes.Count;
m.SwapBoxes(index, other);
m.SwapBoxes(index, other, p.SAV);
};
box.B_BoxRight.Click += (s, e) =>
{
int index = Boxes.FindIndex(z => z == ((Button)s).Parent);
int other = (index + 1) % Boxes.Count;
m.SwapBoxes(index, other);
m.SwapBoxes(index, other, p.SAV);
};
}
}