PKHeX/PKHeX.WinForms/Controls/SAV Editor/PartyEditor.cs

135 lines
4.3 KiB
C#
Raw Normal View History

2019-11-16 01:34:18 +00:00
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PKHeX.Core;
using PKHeX.Drawing.PokeSprite;
2019-11-16 01:34:18 +00:00
namespace PKHeX.WinForms.Controls;
public partial class PartyEditor : UserControl, ISlotViewer<PictureBox>
2019-11-16 01:34:18 +00:00
{
public IList<PictureBox> SlotPictureBoxes { get; private set; } = Array.Empty<PictureBox>();
public SaveFile SAV => M?.SE.SAV ?? throw new ArgumentNullException(nameof(SAV));
2019-11-16 01:34:18 +00:00
public int BoxSlotCount { get; private set; }
public SlotChangeManager? M { get; set; }
public bool FlagIllegal { get; set; }
2019-11-16 01:34:18 +00:00
public PartyEditor()
{
InitializeComponent();
}
2019-11-16 01:34:18 +00:00
internal bool InitializeGrid()
{
const int width = 2;
const int height = 3;
if (!PartyPokeGrid.InitializeGrid(width, height, SpriteUtil.Spriter))
return false;
PartyPokeGrid.HorizontallyCenter(this);
InitializeSlots();
return true;
}
2019-11-16 01:34:18 +00:00
private void InitializeSlots()
{
SlotPictureBoxes = PartyPokeGrid.Entries;
BoxSlotCount = SlotPictureBoxes.Count;
foreach (var pb in SlotPictureBoxes)
2019-11-16 01:34:18 +00:00
{
pb.MouseEnter += (o, args) => BoxSlot_MouseEnter(pb, args);
pb.MouseLeave += (o, args) => BoxSlot_MouseLeave(pb, args);
pb.MouseClick += BoxSlot_MouseClick;
pb.MouseMove += BoxSlot_MouseMove;
pb.MouseDown += BoxSlot_MouseDown;
pb.MouseUp += BoxSlot_MouseUp;
pb.DragEnter += BoxSlot_DragEnter;
pb.DragDrop += BoxSlot_DragDrop;
pb.QueryContinueDrag += BoxSlot_QueryContinueDrag;
pb.GiveFeedback += (sender, e) => e.UseDefaultCursors = false;
pb.AllowDrop = true;
2019-11-16 01:34:18 +00:00
}
}
2019-11-16 01:34:18 +00:00
public void NotifySlotOld(ISlotInfo previous)
{
if (previous is not SlotInfoParty p)
return;
2019-11-16 01:34:18 +00:00
var pb = SlotPictureBoxes[p.Slot];
pb.BackgroundImage = null;
}
2019-11-16 01:34:18 +00:00
public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pk)
{
int index = GetViewIndex(slot);
if (index < 0)
return;
2019-11-16 01:34:18 +00:00
if (type == SlotTouchType.Delete)
2019-11-16 01:34:18 +00:00
{
ResetSlots();
return;
2019-11-16 01:34:18 +00:00
}
var pb = SlotPictureBoxes[index];
SlotUtil.UpdateSlot(pb, slot, pk, SAV, FlagIllegal, type);
}
2019-11-16 01:34:18 +00:00
public int GetViewIndex(ISlotInfo slot)
{
if (slot is not SlotInfoParty p)
return -1;
return p.Slot;
}
2019-11-16 01:34:18 +00:00
public ISlotInfo GetSlotData(PictureBox view)
{
int slot = GetSlot(view);
return new SlotInfoParty(slot);
}
2019-11-16 01:34:18 +00:00
private int GetSlot(PictureBox sender) => SlotPictureBoxes.IndexOf(sender);
public int ViewIndex => -999;
2019-11-16 01:34:18 +00:00
public void Setup(SlotChangeManager m)
{
M = m;
FlagIllegal = M.SE.FlagIllegal;
}
2019-11-16 01:34:18 +00:00
public void ResetSlots()
{
//pokeGrid1.SetBackground(SAV.WallpaperImage(0));
M?.Hover.Stop();
2019-11-16 01:34:18 +00:00
foreach (var pb in SlotPictureBoxes)
{
var slot = (SlotInfoParty) GetSlotData(pb);
SlotUtil.UpdateSlot(pb, slot, slot.Read(SAV), SAV, FlagIllegal);
}
if (M?.Env.Slots.Publisher.Previous is SlotInfoParty p)
SlotPictureBoxes[p.Slot].BackgroundImage = SlotUtil.GetTouchTypeBackground(M.Env.Slots.Publisher.PreviousType);
}
// Drag & Drop Handling
private void BoxSlot_MouseEnter(object? sender, EventArgs e) => M?.MouseEnter(sender, e);
private void BoxSlot_MouseLeave(object? sender, EventArgs e) => M?.MouseLeave(sender, e);
private void BoxSlot_MouseClick(object? sender, MouseEventArgs e) => M?.MouseClick(sender, e);
private void BoxSlot_MouseUp(object? sender, MouseEventArgs e) => M?.MouseUp(sender, e);
private void BoxSlot_MouseDown(object? sender, MouseEventArgs e) => M?.MouseDown(sender, e);
private void BoxSlot_MouseMove(object? sender, MouseEventArgs e) => M?.MouseMove(sender, e);
private void BoxSlot_DragEnter(object? sender, DragEventArgs e) => M?.DragEnter(sender, e);
private void BoxSlot_QueryContinueDrag(object? sender, QueryContinueDragEventArgs e) => M?.QueryContinueDrag(sender, e);
private void BoxSlot_DragDrop(object? sender, DragEventArgs e) => M?.DragDrop(sender, e);
public bool InitializeFromSAV(SaveFile sav)
{
Visible = sav.HasParty;
return InitializeGrid();
2019-11-16 01:34:18 +00:00
}
}