Refactor dragdrop managing logic

Relocate class out of main form for reusability/less lines in main.cs
refactored to be a bit more oop
This commit is contained in:
Kurt 2017-05-16 08:56:13 -07:00
parent 6f64c9be8e
commit 2bfa98db09
4 changed files with 195 additions and 202 deletions

View file

@ -0,0 +1,94 @@
using PKHeX.Core;
namespace PKHeX.WinForms
{
public class DragDropManager
{
private readonly SaveFile SAV;
public bool LeftMouseIsDown;
public bool RightMouseIsDown;
public bool DragDropInProgress;
public object Cursor;
public string CurrentPath;
public DragLocation Source = new DragLocation();
public DragLocation Destination = new DragLocation();
public DragDropManager(SaveFile sav)
{
SAV = sav;
Source.Data = SAV.BlankPKM.EncryptedPartyData;
}
public class DragLocation
{
public object Parent;
public byte[] Data;
public int Offset = -1;
public int Slot = -1;
public int Box = -1;
public bool IsParty => 30 <= Slot && Slot < 36;
public bool IsValid => Slot > -1 && (Box > -1 || IsParty);
}
public bool SameBox => Source.Box > -1 && Source.Box == Destination.Box;
public bool SameSlot => Source.Slot == Destination.Slot && Source.Box == Destination.Box;
// PKM Get Set
public PKM GetPKM(bool src)
{
var slot = src ? Source : Destination;
int o = slot.Offset;
return slot.IsParty ? SAV.getPartySlot(o) : SAV.getStoredSlot(o);
}
public void SetPKM(PKM pk, bool src)
{
var slot = src ? Source : Destination;
int o = slot.Offset;
if (!slot.IsParty)
{ SAV.setStoredSlot(pk, o); return; }
if (src)
{
if (pk.Species == 0) // Empty Slot
{
SAV.deletePartySlot(Source.Slot - 30);
return;
}
}
else
{
if (30 + SAV.PartyCount < slot.Slot)
{
o = SAV.getPartyOffset(SAV.PartyCount);
slot.Slot = 30 + SAV.PartyCount;
}
}
if (pk.Stat_HPMax == 0) // Without Stats (Box)
{
pk.setStats(pk.getStats(SAV.Personal.getFormeEntry(pk.Species, pk.AltForm)));
pk.Stat_Level = pk.CurrentLevel;
}
SAV.setPartySlot(pk, o);
}
public bool? WasDragParticipant(object form, int index)
{
if (Destination.Box != index && Source.Box != index)
return null; // form was not watching box
return Source.Parent == form || Destination.Parent == form; // form already updated?
}
public void Reset()
{
LeftMouseIsDown = RightMouseIsDown = DragDropInProgress = false;
Cursor = CurrentPath = null;
Source = new DragLocation();
Destination = new DragLocation();
}
}
}

View file

@ -22,7 +22,6 @@ namespace PKHeX.WinForms
{ {
#region Initialize Form #region Initialize Form
new Thread(() => new SplashScreen().ShowDialog()).Start(); new Thread(() => new SplashScreen().ShowDialog()).Start();
DragInfo.slotPkmSource = SAV.BlankPKM.EncryptedPartyData;
InitializeComponent(); InitializeComponent();
// Check for Updates // Check for Updates
@ -1034,6 +1033,7 @@ namespace PKHeX.WinForms
PKM pk = preparePKM(); PKM pk = preparePKM();
populateFields(SAV.BlankPKM); populateFields(SAV.BlankPKM);
SAV = sav; SAV = sav;
DragInfo = new DragDropManager(SAV);
string title = $"PKH{(HaX ? "a" : "e")}X ({Resources.ProgramVersion}) - " + $"{SAV.GetType().Name}: "; string title = $"PKH{(HaX ? "a" : "e")}X ({Resources.ProgramVersion}) - " + $"{SAV.GetType().Name}: ";
if (path != null) // Actual save file if (path != null) // Actual save file
@ -2859,7 +2859,6 @@ namespace PKHeX.WinForms
Tip2.SetToolTip(TB_SID, IDstr); Tip2.SetToolTip(TB_SID, IDstr);
pkm.PID = Util.getHEXval(TB_PID.Text); pkm.PID = Util.getHEXval(TB_PID.Text);
var PSV = pkm.PSV;
Tip3.SetToolTip(TB_PID, $"PSV: {pkm.PSV:d4}"); Tip3.SetToolTip(TB_PID, $"PSV: {pkm.PSV:d4}");
} }
private void update_ID(object sender, EventArgs e) private void update_ID(object sender, EventArgs e)
@ -4248,8 +4247,9 @@ namespace PKHeX.WinForms
} }
// Drag and drop related functions // Drag and drop related functions
private static Image OriginalBackground; public static DragDropManager DragInfo;
private static Image CurrentBackground; private Image OriginalBackground;
private Image CurrentBackground;
private void pbBoxSlot_MouseEnter(object sender, EventArgs e) private void pbBoxSlot_MouseEnter(object sender, EventArgs e)
{ {
var pb = (PictureBox) sender; var pb = (PictureBox) sender;
@ -4269,29 +4269,29 @@ namespace PKHeX.WinForms
} }
private void pbBoxSlot_MouseClick(object sender, MouseEventArgs e) private void pbBoxSlot_MouseClick(object sender, MouseEventArgs e)
{ {
if (!DragInfo.slotDragDropInProgress) if (!DragInfo.DragDropInProgress)
clickSlot(sender, e); clickSlot(sender, e);
} }
private void pbBoxSlot_MouseUp(object sender, MouseEventArgs e) private void pbBoxSlot_MouseUp(object sender, MouseEventArgs e)
{ {
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
DragInfo.slotLeftMouseIsDown = false; DragInfo.LeftMouseIsDown = false;
if (e.Button == MouseButtons.Right) if (e.Button == MouseButtons.Right)
DragInfo.slotRightMouseIsDown = false; DragInfo.RightMouseIsDown = false;
} }
private void pbBoxSlot_MouseDown(object sender, MouseEventArgs e) private void pbBoxSlot_MouseDown(object sender, MouseEventArgs e)
{ {
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
DragInfo.slotLeftMouseIsDown = true; DragInfo.LeftMouseIsDown = true;
if (e.Button == MouseButtons.Right) if (e.Button == MouseButtons.Right)
DragInfo.slotRightMouseIsDown = true; DragInfo.RightMouseIsDown = true;
} }
private void pbBoxSlot_MouseMove(object sender, MouseEventArgs e) private void pbBoxSlot_MouseMove(object sender, MouseEventArgs e)
{ {
if (DragInfo.slotDragDropInProgress) if (DragInfo.DragDropInProgress)
return; return;
if (!DragInfo.slotLeftMouseIsDown) if (!DragInfo.LeftMouseIsDown)
return; return;
// The goal is to create a temporary PKX file for the underlying Pokemon // The goal is to create a temporary PKX file for the underlying Pokemon
@ -4308,19 +4308,19 @@ namespace PKHeX.WinForms
return; return;
// Set flag to prevent re-entering. // Set flag to prevent re-entering.
DragInfo.slotDragDropInProgress = true; DragInfo.DragDropInProgress = true;
DragInfo.slotSource = this; DragInfo.Source.Parent = this;
DragInfo.slotSourceSlotNumber = slot; DragInfo.Source.Slot = slot;
DragInfo.slotSourceBoxNumber = box; DragInfo.Source.Box = box;
DragInfo.slotSourceOffset = getPKXOffset(DragInfo.slotSourceSlotNumber); DragInfo.Source.Offset = getPKXOffset(DragInfo.Source.Slot);
// Prepare Data // Prepare Data
DragInfo.slotPkmSource = SAV.getData(DragInfo.slotSourceOffset, SAV.SIZE_STORED); DragInfo.Source.Data = SAV.getData(DragInfo.Source.Offset, SAV.SIZE_STORED);
// Make a new file name based off the PID // Make a new file name based off the PID
bool encrypt = ModifierKeys == Keys.Control; bool encrypt = ModifierKeys == Keys.Control;
byte[] dragdata = SAV.decryptPKM(DragInfo.slotPkmSource); byte[] dragdata = SAV.decryptPKM(DragInfo.Source.Data);
Array.Resize(ref dragdata, SAV.SIZE_STORED); Array.Resize(ref dragdata, SAV.SIZE_STORED);
PKM pkx = SAV.getPKM(dragdata); PKM pkx = SAV.getPKM(dragdata);
string fn = pkx.FileName; fn = fn.Substring(0, fn.LastIndexOf('.')); string fn = pkx.FileName; fn = fn.Substring(0, fn.LastIndexOf('.'));
@ -4338,22 +4338,22 @@ namespace PKHeX.WinForms
// Thread Blocks on DoDragDrop // Thread Blocks on DoDragDrop
DragInfo.CurrentPath = newfile; DragInfo.CurrentPath = newfile;
DragDropEffects result = pb.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move); DragDropEffects result = pb.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move);
if (!DragInfo.SourceValid || result != DragDropEffects.Link) // not dropped to another box slot, restore img if (!DragInfo.Source.IsValid || result != DragDropEffects.Link) // not dropped to another box slot, restore img
pb.Image = img; pb.Image = img;
else // refresh image else // refresh image
getQuickFiller(pb, SAV.getStoredSlot(DragInfo.slotSourceOffset)); getQuickFiller(pb, SAV.getStoredSlot(DragInfo.Source.Offset));
pb.BackgroundImage = null; pb.BackgroundImage = null;
if (DragInfo.SameBox && DragInfo.DestinationValid) if (DragInfo.SameBox && DragInfo.Destination.IsValid)
{ {
if (SAV.getIsTeamSet(box, DragInfo.slotDestinationSlotNumber) ^ SAV.getIsTeamSet(box, DragInfo.slotSourceSlotNumber)) if (SAV.getIsTeamSet(box, DragInfo.Destination.Slot) ^ SAV.getIsTeamSet(box, DragInfo.Source.Slot))
getQuickFiller(SlotPictureBoxes[DragInfo.slotDestinationSlotNumber], SAV.getStoredSlot(DragInfo.slotDestinationOffset)); getQuickFiller(SlotPictureBoxes[DragInfo.Destination.Slot], SAV.getStoredSlot(DragInfo.Destination.Offset));
else else
SlotPictureBoxes[DragInfo.slotDestinationSlotNumber].Image = img; SlotPictureBoxes[DragInfo.Destination.Slot].Image = img;
} }
if (result == DragDropEffects.Copy) // viewed in tabs, apply 'view' highlight if (result == DragDropEffects.Copy) // viewed in tabs, apply 'view' highlight
getSlotColor(DragInfo.slotSourceSlotNumber, Resources.slotView); getSlotColor(DragInfo.Source.Slot, Resources.slotView);
} }
catch (Exception x) catch (Exception x)
{ {
@ -4371,28 +4371,28 @@ namespace PKHeX.WinForms
if (File.Exists(newfile) && DragInfo.CurrentPath == null) if (File.Exists(newfile) && DragInfo.CurrentPath == null)
File.Delete(newfile); File.Delete(newfile);
}).Start(); }).Start();
if (DragInfo.SourceParty || DragInfo.DestinationParty) if (DragInfo.Source.IsParty || DragInfo.Destination.IsParty)
setParty(); setParty();
} }
private void pbBoxSlot_DragDrop(object sender, DragEventArgs e) private void pbBoxSlot_DragDrop(object sender, DragEventArgs e)
{ {
DragInfo.slotDestination = this; DragInfo.Destination.Parent = this;
DragInfo.slotDestinationSlotNumber = getSlot(sender); DragInfo.Destination.Slot = getSlot(sender);
DragInfo.slotDestinationOffset = getPKXOffset(DragInfo.slotDestinationSlotNumber); DragInfo.Destination.Offset = getPKXOffset(DragInfo.Destination.Slot);
DragInfo.slotDestinationBoxNumber = DragInfo.DestinationParty ? -1 : CB_BoxSelect.SelectedIndex; DragInfo.Destination.Box = DragInfo.Destination.IsParty ? -1 : CB_BoxSelect.SelectedIndex;
// Check for In-Dropped files (PKX,SAV,ETC) // Check for In-Dropped files (PKX,SAV,ETC)
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (Directory.Exists(files[0])) { loadBoxesFromDB(files[0]); return; } if (Directory.Exists(files[0])) { loadBoxesFromDB(files[0]); return; }
if (DragInfo.SameSlot) if (DragInfo.SameSlot)
return; return;
if (SAV.getIsSlotLocked(DragInfo.slotDestinationBoxNumber, DragInfo.slotDestinationSlotNumber)) if (SAV.getIsSlotLocked(DragInfo.Destination.Box, DragInfo.Destination.Slot))
{ {
DragInfo.slotDestinationSlotNumber = -1; // Invalidate DragInfo.Destination.Slot = -1; // Invalidate
WinFormsUtil.Alert("Unable to set to locked slot."); WinFormsUtil.Alert("Unable to set to locked slot.");
return; return;
} }
if (DragInfo.slotSourceOffset < 0) // file if (DragInfo.Source.Offset < 0) // file
{ {
if (files.Length <= 0) if (files.Length <= 0)
return; return;
@ -4417,50 +4417,50 @@ namespace PKHeX.WinForms
{ Console.WriteLine(c); Console.WriteLine(concat); return; } { Console.WriteLine(c); Console.WriteLine(concat); return; }
} }
DragInfo.setPKMtoDestination(SAV, pk); DragInfo.SetPKM(pk, false);
getQuickFiller(SlotPictureBoxes[DragInfo.slotDestinationSlotNumber], pk); getQuickFiller(SlotPictureBoxes[DragInfo.Destination.Slot], pk);
getSlotColor(DragInfo.slotDestinationSlotNumber, Resources.slotSet); getSlotColor(DragInfo.Destination.Slot, Resources.slotSet);
Console.WriteLine(c); Console.WriteLine(c);
} }
else else
{ {
PKM pkz = DragInfo.getPKMfromSource(SAV); PKM pkz = DragInfo.GetPKM(true);
if (!DragInfo.SourceValid) { } // not overwritable, do nothing if (!DragInfo.Source.IsValid) { } // not overwritable, do nothing
else if (ModifierKeys == Keys.Alt && DragInfo.DestinationValid) // overwrite delete old slot else if (ModifierKeys == Keys.Alt && DragInfo.Destination.IsValid) // overwrite delete old slot
{ {
// Clear from slot // Clear from slot
if (DragInfo.SameBox) if (DragInfo.SameBox)
getQuickFiller(SlotPictureBoxes[DragInfo.slotSourceSlotNumber], SAV.BlankPKM); // picturebox getQuickFiller(SlotPictureBoxes[DragInfo.Source.Slot], SAV.BlankPKM); // picturebox
DragInfo.setPKMtoSource(SAV, SAV.BlankPKM); DragInfo.SetPKM(SAV.BlankPKM, true);
} }
else if (ModifierKeys != Keys.Control && DragInfo.DestinationValid) else if (ModifierKeys != Keys.Control && DragInfo.Destination.IsValid)
{ {
// Load data from destination // Load data from destination
PKM pk = ((PictureBox) sender).Image != null PKM pk = ((PictureBox) sender).Image != null
? DragInfo.getPKMfromDestination(SAV) ? DragInfo.GetPKM(false)
: SAV.BlankPKM; : SAV.BlankPKM;
// Set destination pokemon image to source picture box // Set destination pokemon image to source picture box
if (DragInfo.SameBox) if (DragInfo.SameBox)
getQuickFiller(SlotPictureBoxes[DragInfo.slotSourceSlotNumber], pk); getQuickFiller(SlotPictureBoxes[DragInfo.Source.Slot], pk);
// Set destination pokemon data to source slot // Set destination pokemon data to source slot
DragInfo.setPKMtoSource(SAV, pk); DragInfo.SetPKM(pk, true);
} }
else if (DragInfo.SameBox) else if (DragInfo.SameBox)
getQuickFiller(SlotPictureBoxes[DragInfo.slotSourceSlotNumber], pkz); getQuickFiller(SlotPictureBoxes[DragInfo.Source.Slot], pkz);
// Copy from temp to destination slot. // Copy from temp to destination slot.
DragInfo.setPKMtoDestination(SAV, pkz); DragInfo.SetPKM(pkz, false);
getQuickFiller(SlotPictureBoxes[DragInfo.slotDestinationSlotNumber], pkz); getQuickFiller(SlotPictureBoxes[DragInfo.Destination.Slot], pkz);
e.Effect = DragDropEffects.Link; e.Effect = DragDropEffects.Link;
Cursor = DefaultCursor; Cursor = DefaultCursor;
} }
if (DragInfo.SourceParty || DragInfo.DestinationParty) if (DragInfo.Source.IsParty || DragInfo.Destination.IsParty)
setParty(); setParty();
if (DragInfo.slotSource == null) // another instance or file if (DragInfo.Source.Parent == null) // another instance or file
{ {
notifyBoxViewerRefresh(); notifyBoxViewerRefresh();
DragInfo.Reset(); DragInfo.Reset();
@ -4473,120 +4473,18 @@ namespace PKHeX.WinForms
else if (e.Data != null) // within else if (e.Data != null) // within
e.Effect = DragDropEffects.Move; e.Effect = DragDropEffects.Move;
if (DragInfo.slotDragDropInProgress) if (DragInfo.DragDropInProgress)
Cursor = (Cursor)DragInfo.Cursor; Cursor = (Cursor)DragInfo.Cursor;
} }
private void pbBoxSlot_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) private void pbBoxSlot_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{ {
if (e.Action == DragAction.Cancel || e.Action == DragAction.Drop) if (e.Action == DragAction.Cancel || e.Action == DragAction.Drop)
{ {
DragInfo.slotLeftMouseIsDown = false; DragInfo.LeftMouseIsDown = false;
DragInfo.slotRightMouseIsDown = false; DragInfo.RightMouseIsDown = false;
DragInfo.slotDragDropInProgress = false; DragInfo.DragDropInProgress = false;
} }
} }
public static class DragInfo
{
public static bool slotLeftMouseIsDown;
public static bool slotRightMouseIsDown;
public static bool slotDragDropInProgress;
public static byte[] slotPkmSource;
public static byte[] slotPkmDestination;
public static object slotSource;
public static int slotSourceOffset = -1;
public static int slotSourceSlotNumber = -1;
public static int slotSourceBoxNumber = -1;
public static object slotDestination;
public static int slotDestinationOffset = -1;
public static int slotDestinationSlotNumber = -1;
public static int slotDestinationBoxNumber = -1;
public static object Cursor;
public static string CurrentPath;
public static bool SameBox => slotSourceBoxNumber > -1 && slotSourceBoxNumber == slotDestinationBoxNumber;
public static bool SameSlot => slotSourceSlotNumber == slotDestinationSlotNumber && slotSourceBoxNumber == slotDestinationBoxNumber;
public static bool SourceValid => slotSourceSlotNumber > -1 && (slotSourceBoxNumber > -1 || SourceParty);
public static bool DestinationValid => slotDestinationSlotNumber > -1 && (slotDestinationBoxNumber > -1 || DestinationParty);
public static bool SourceParty => 30 <= slotSourceSlotNumber && slotSourceSlotNumber < 36;
public static bool DestinationParty => 30 <= slotDestinationSlotNumber && slotDestinationSlotNumber < 36;
// PKM Get Set
public static PKM getPKMfromSource(SaveFile SAV)
{
int o = slotSourceOffset;
return SourceParty ? SAV.getPartySlot(o) : SAV.getStoredSlot(o);
}
public static PKM getPKMfromDestination(SaveFile SAV)
{
int o = slotDestinationOffset;
return DestinationParty ? SAV.getPartySlot(o) : SAV.getStoredSlot(o);
}
public static void setPKMtoSource(SaveFile SAV, PKM pk)
{
int o = slotSourceOffset;
if (!SourceParty)
{ SAV.setStoredSlot(pk, o); return; }
if (pk.Species == 0) // Empty Slot
{ SAV.deletePartySlot(slotSourceSlotNumber-30); return; }
if (pk.Stat_HPMax == 0) // Without Stats (Box)
{
pk.setStats(pk.getStats(SAV.Personal.getFormeEntry(pk.Species, pk.AltForm)));
pk.Stat_Level = pk.CurrentLevel;
}
SAV.setPartySlot(pk, o);
}
public static void setPKMtoDestination(SaveFile SAV, PKM pk)
{
int o = slotDestinationOffset;
if (!DestinationParty)
{ SAV.setStoredSlot(pk, o); return; }
if (30 + SAV.PartyCount < slotDestinationSlotNumber)
{
o = SAV.getPartyOffset(SAV.PartyCount);
slotDestinationSlotNumber = 30 + SAV.PartyCount;
}
if (pk.Stat_HPMax == 0) // Without Stats (Box/File)
{
pk.setStats(pk.getStats(SAV.Personal.getFormeEntry(pk.Species, pk.AltForm)));
pk.Stat_Level = pk.CurrentLevel;
}
SAV.setPartySlot(pk, o);
}
public static void Reset()
{
slotLeftMouseIsDown = false;
slotRightMouseIsDown = false;
slotDragDropInProgress = false;
slotPkmSource = null;
slotSourceOffset = slotSourceSlotNumber = slotSourceBoxNumber = -1;
slotPkmDestination = null;
slotDestinationOffset = slotSourceBoxNumber = slotDestinationBoxNumber = -1;
Cursor = null;
CurrentPath = null;
slotSource = null;
slotDestination = null;
}
public static bool? WasDragParticipant(object form, int index)
{
if (slotDestinationBoxNumber != index && slotSourceBoxNumber != index)
return null; // form was not watching box
return slotSource == form || slotDestination == form; // form already updated?
}
}
#endregion #endregion
} }
} }

View file

@ -165,6 +165,7 @@
<Reference Include="System.Deployment" /> <Reference Include="System.Deployment" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="MainWindow\DragDropManager.cs" />
<Compile Include="MainWindow\Main.cs"> <Compile Include="MainWindow\Main.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>

View file

@ -157,23 +157,23 @@ namespace PKHeX.WinForms
private void pbBoxSlot_MouseUp(object sender, MouseEventArgs e) private void pbBoxSlot_MouseUp(object sender, MouseEventArgs e)
{ {
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
DragInfo.slotLeftMouseIsDown = false; DragInfo.LeftMouseIsDown = false;
if (e.Button == MouseButtons.Right) if (e.Button == MouseButtons.Right)
DragInfo.slotRightMouseIsDown = false; DragInfo.RightMouseIsDown = false;
} }
private void pbBoxSlot_MouseDown(object sender, MouseEventArgs e) private void pbBoxSlot_MouseDown(object sender, MouseEventArgs e)
{ {
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
DragInfo.slotLeftMouseIsDown = true; DragInfo.LeftMouseIsDown = true;
if (e.Button == MouseButtons.Right) if (e.Button == MouseButtons.Right)
DragInfo.slotRightMouseIsDown = true; DragInfo.RightMouseIsDown = true;
} }
private void pbBoxSlot_MouseMove(object sender, MouseEventArgs e) private void pbBoxSlot_MouseMove(object sender, MouseEventArgs e)
{ {
if (DragInfo.slotDragDropInProgress) if (DragInfo.DragDropInProgress)
return; return;
if (!DragInfo.slotLeftMouseIsDown) if (!DragInfo.LeftMouseIsDown)
return; return;
// The goal is to create a temporary PKX file for the underlying Pokemon // The goal is to create a temporary PKX file for the underlying Pokemon
@ -190,18 +190,18 @@ namespace PKHeX.WinForms
return; return;
// Set flag to prevent re-entering. // Set flag to prevent re-entering.
DragInfo.slotDragDropInProgress = true; DragInfo.DragDropInProgress = true;
DragInfo.slotSource = this; DragInfo.Source.Parent = this;
DragInfo.slotSourceSlotNumber = slot; DragInfo.Source.Slot = slot;
DragInfo.slotSourceBoxNumber = box; DragInfo.Source.Box = box;
DragInfo.slotSourceOffset = getPKXOffset(DragInfo.slotSourceSlotNumber); DragInfo.Source.Offset = getPKXOffset(DragInfo.Source.Slot);
// Prepare Data // Prepare Data
DragInfo.slotPkmSource = SAV.getData(DragInfo.slotSourceOffset, SAV.SIZE_STORED); DragInfo.Source.Data = SAV.getData(DragInfo.Source.Offset, SAV.SIZE_STORED);
// Make a new file name based off the PID // Make a new file name based off the PID
byte[] dragdata = SAV.decryptPKM(DragInfo.slotPkmSource); byte[] dragdata = SAV.decryptPKM(DragInfo.Source.Data);
Array.Resize(ref dragdata, SAV.SIZE_STORED); Array.Resize(ref dragdata, SAV.SIZE_STORED);
PKM pkx = SAV.getPKM(dragdata); PKM pkx = SAV.getPKM(dragdata);
string filename = pkx.FileName; string filename = pkx.FileName;
@ -218,18 +218,18 @@ namespace PKHeX.WinForms
// Thread Blocks on DoDragDrop // Thread Blocks on DoDragDrop
DragInfo.CurrentPath = newfile; DragInfo.CurrentPath = newfile;
DragDropEffects result = pb.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move); DragDropEffects result = pb.DoDragDrop(new DataObject(DataFormats.FileDrop, new[] { newfile }), DragDropEffects.Move);
if (!DragInfo.SourceValid || result != DragDropEffects.Link) // not dropped to another box slot, restore img if (!DragInfo.Source.IsValid || result != DragDropEffects.Link) // not dropped to another box slot, restore img
pb.Image = img; pb.Image = img;
else // refresh image else // refresh image
getQuickFiller(pb, SAV.getStoredSlot(DragInfo.slotSourceOffset)); getQuickFiller(pb, SAV.getStoredSlot(DragInfo.Source.Offset));
pb.BackgroundImage = null; pb.BackgroundImage = null;
if (DragInfo.SameBox && DragInfo.DestinationValid) if (DragInfo.SameBox && DragInfo.Destination.IsValid)
{ {
if (SAV.getIsTeamSet(box, DragInfo.slotDestinationSlotNumber) ^ SAV.getIsTeamSet(box, DragInfo.slotSourceSlotNumber)) if (SAV.getIsTeamSet(box, DragInfo.Destination.Slot) ^ SAV.getIsTeamSet(box, DragInfo.Source.Slot))
getQuickFiller(SlotPictureBoxes[DragInfo.slotDestinationSlotNumber], SAV.getStoredSlot(DragInfo.slotDestinationOffset)); getQuickFiller(SlotPictureBoxes[DragInfo.Destination.Slot], SAV.getStoredSlot(DragInfo.Destination.Offset));
else else
SlotPictureBoxes[DragInfo.slotDestinationSlotNumber].Image = img; SlotPictureBoxes[DragInfo.Destination.Slot].Image = img;
} }
} }
catch (Exception x) catch (Exception x)
@ -251,21 +251,21 @@ namespace PKHeX.WinForms
} }
private void pbBoxSlot_DragDrop(object sender, DragEventArgs e) private void pbBoxSlot_DragDrop(object sender, DragEventArgs e)
{ {
DragInfo.slotDestination = this; DragInfo.Destination.Parent = this;
DragInfo.slotDestinationSlotNumber = getSlot(sender); DragInfo.Destination.Slot = getSlot(sender);
DragInfo.slotDestinationOffset = getPKXOffset(DragInfo.slotDestinationSlotNumber); DragInfo.Destination.Offset = getPKXOffset(DragInfo.Destination.Slot);
DragInfo.slotDestinationBoxNumber = CB_BoxSelect.SelectedIndex; DragInfo.Destination.Box = CB_BoxSelect.SelectedIndex;
// Check for In-Dropped files (PKX,SAV,ETC) // Check for In-Dropped files (PKX,SAV,ETC)
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (Directory.Exists(files[0])) { return; } if (Directory.Exists(files[0])) { return; }
if (SAV.getIsSlotLocked(DragInfo.slotDestinationBoxNumber, DragInfo.slotDestinationSlotNumber)) if (SAV.getIsSlotLocked(DragInfo.Destination.Box, DragInfo.Destination.Slot))
{ {
DragInfo.slotDestinationSlotNumber = -1; // Invalidate DragInfo.Destination.Slot = -1; // Invalidate
WinFormsUtil.Alert("Unable to set to locked slot."); WinFormsUtil.Alert("Unable to set to locked slot.");
return; return;
} }
if (DragInfo.slotSourceOffset < 0) // file if (DragInfo.Source.Offset < 0) // file
{ {
if (files.Length <= 0) if (files.Length <= 0)
return; return;
@ -291,49 +291,49 @@ namespace PKHeX.WinForms
{ Console.WriteLine(c); Console.WriteLine(concat); return; } { Console.WriteLine(c); Console.WriteLine(concat); return; }
} }
DragInfo.setPKMtoDestination(SAV, pk); DragInfo.SetPKM(pk, false);
getQuickFiller(SlotPictureBoxes[DragInfo.slotDestinationSlotNumber], pk); getQuickFiller(SlotPictureBoxes[DragInfo.Destination.Slot], pk);
Console.WriteLine(c); Console.WriteLine(c);
} }
else else
{ {
PKM pkz = DragInfo.getPKMfromSource(SAV); PKM pkz = DragInfo.GetPKM(true);
if (!DragInfo.SourceValid) { } // not overwritable, do nothing if (!DragInfo.Source.IsValid) { } // not overwritable, do nothing
else if (ModifierKeys == Keys.Alt && DragInfo.DestinationValid) // overwrite else if (ModifierKeys == Keys.Alt && DragInfo.Destination.IsValid) // overwrite
{ {
// Clear from slot // Clear from slot
if (DragInfo.SameBox) if (DragInfo.SameBox)
getQuickFiller(SlotPictureBoxes[DragInfo.slotSourceSlotNumber], SAV.BlankPKM); // picturebox getQuickFiller(SlotPictureBoxes[DragInfo.Source.Slot], SAV.BlankPKM); // picturebox
DragInfo.setPKMtoSource(SAV, SAV.BlankPKM); DragInfo.SetPKM(SAV.BlankPKM, true);
} }
else if (ModifierKeys != Keys.Control && DragInfo.DestinationValid) // move else if (ModifierKeys != Keys.Control && DragInfo.Destination.IsValid) // move
{ {
// Load data from destination // Load data from destination
PKM pk = ((PictureBox)sender).Image != null PKM pk = ((PictureBox)sender).Image != null
? DragInfo.getPKMfromDestination(SAV) ? DragInfo.GetPKM(false)
: SAV.BlankPKM; : SAV.BlankPKM;
// Set destination pokemon image to source picture box // Set destination pokemon image to source picture box
if (DragInfo.SameBox) if (DragInfo.SameBox)
getQuickFiller(SlotPictureBoxes[DragInfo.slotSourceSlotNumber], pk); getQuickFiller(SlotPictureBoxes[DragInfo.Source.Slot], pk);
// Set destination pokemon data to source slot // Set destination pokemon data to source slot
DragInfo.setPKMtoSource(SAV, pk); DragInfo.SetPKM(pk, true);
} }
else if (DragInfo.SameBox) // clone else if (DragInfo.SameBox) // clone
getQuickFiller(SlotPictureBoxes[DragInfo.slotSourceSlotNumber], pkz); getQuickFiller(SlotPictureBoxes[DragInfo.Source.Slot], pkz);
// Copy from temp to destination slot. // Copy from temp to destination slot.
DragInfo.setPKMtoDestination(SAV, pkz); DragInfo.SetPKM(pkz, false);
getQuickFiller(SlotPictureBoxes[DragInfo.slotDestinationSlotNumber], pkz); getQuickFiller(SlotPictureBoxes[DragInfo.Destination.Slot], pkz);
e.Effect = DragDropEffects.Link; e.Effect = DragDropEffects.Link;
Cursor = DefaultCursor; Cursor = DefaultCursor;
} }
if (DragInfo.SourceParty || DragInfo.DestinationParty) if (DragInfo.Source.IsParty || DragInfo.Destination.IsParty)
parent.setParty(); parent.setParty();
if (DragInfo.slotSource == null) // another instance or file if (DragInfo.Source.Parent == null) // another instance or file
{ {
parent.notifyBoxViewerRefresh(); parent.notifyBoxViewerRefresh();
DragInfo.Reset(); DragInfo.Reset();
@ -346,16 +346,16 @@ namespace PKHeX.WinForms
else if (e.Data != null) // within else if (e.Data != null) // within
e.Effect = DragDropEffects.Move; e.Effect = DragDropEffects.Move;
if (DragInfo.slotDragDropInProgress) if (DragInfo.DragDropInProgress)
Cursor = (Cursor)DragInfo.Cursor; Cursor = (Cursor)DragInfo.Cursor;
} }
private void pbBoxSlot_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) private void pbBoxSlot_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{ {
if (e.Action == DragAction.Cancel || e.Action == DragAction.Drop) if (e.Action == DragAction.Cancel || e.Action == DragAction.Drop)
{ {
DragInfo.slotLeftMouseIsDown = false; DragInfo.LeftMouseIsDown = false;
DragInfo.slotRightMouseIsDown = false; DragInfo.RightMouseIsDown = false;
DragInfo.slotDragDropInProgress = false; DragInfo.DragDropInProgress = false;
} }
} }