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

136 lines
4.7 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;
namespace PKHeX.WinForms.Controls
{
public partial class PartyEditor : UserControl, ISlotViewer<PictureBox>
{
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; }
2019-11-16 01:34:18 +00:00
public bool FlagIllegal { get; set; }
public PartyEditor()
{
InitializeComponent();
}
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)
{
pb.MouseEnter += (o, args) => BoxSlot_MouseEnter(pb, args);
pb.MouseLeave += (o, args) => BoxSlot_MouseLeave(pb, args);
2019-11-16 01:34:18 +00:00
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;
}
}
public void NotifySlotOld(ISlotInfo previous)
{
2020-12-23 05:24:41 +00:00
if (previous is not SlotInfoParty p)
2019-11-16 01:34:18 +00:00
return;
var pb = SlotPictureBoxes[p.Slot];
pb.BackgroundImage = null;
}
public void NotifySlotChanged(ISlotInfo slot, SlotTouchType type, PKM pkm)
{
int index = GetViewIndex(slot);
if (index < 0)
return;
if (type == SlotTouchType.Delete)
{
ResetSlots();
return;
}
var pb = SlotPictureBoxes[index];
SlotUtil.UpdateSlot(pb, slot, pkm, SAV, FlagIllegal, type);
}
public int GetViewIndex(ISlotInfo slot)
{
2020-12-23 05:24:41 +00:00
if (slot is not SlotInfoParty p)
2019-11-16 01:34:18 +00:00
return -1;
return p.Slot;
}
public ISlotInfo GetSlotData(PictureBox view)
{
int slot = GetSlot(view);
return new SlotInfoParty(slot);
}
private int GetSlot(PictureBox sender) => SlotPictureBoxes.IndexOf(sender);
public int ViewIndex => -999;
public void Setup(SlotChangeManager m)
{
M = m;
FlagIllegal = M.SE.FlagIllegal;
}
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)
2019-11-16 01:34:18 +00:00
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);
2019-11-16 01:34:18 +00:00
public bool InitializeFromSAV(SaveFile sav)
{
Visible = sav.HasParty;
return InitializeGrid();
}
2019-11-16 01:34:18 +00:00
}
}