From cb7bc35e7b781ff537d55d0fccf978ab9c918e53 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 3 Mar 2021 18:23:36 -0800 Subject: [PATCH] Add dragdrop for nhi files Drop nhi on the item editor and it'll load the first item in the nhi Drop nhi anywhere else and it'll load the items if it's compatible. --- NHSE.WinForms/Subforms/PlayerItemEditor.cs | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/NHSE.WinForms/Subforms/PlayerItemEditor.cs b/NHSE.WinForms/Subforms/PlayerItemEditor.cs index 97c5fda..ee1fa58 100644 --- a/NHSE.WinForms/Subforms/PlayerItemEditor.cs +++ b/NHSE.WinForms/Subforms/PlayerItemEditor.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Windows.Forms; using NHSE.Core; using NHSE.Injection; @@ -29,6 +30,10 @@ public PlayerItemEditor(IReadOnlyList array, int width, int height, bool s DialogResult = DialogResult.Cancel; LoadItems = () => Editor.LoadItems(); B_Inject.Visible = sysbot; + + EnableDragDrop(this, ItemEditor_DragEnter, PlayerItemEditor_DragDrop); + EnableDragDrop(PAN_Items, ItemEditor_DragEnter, PlayerItemEditor_DragDrop); + EnableDragDrop(ItemEditor, ItemEditor_DragEnter, PlayerItemEditor_DragDrop); } private void B_Cancel_Click(object sender, EventArgs e) => Close(); @@ -121,5 +126,50 @@ static void AfterWrite(InjectionResult r) var sysbot = new SysBotUI(ai, sb, aiUSB, ub); sysbot.Show(); } + + private void ItemEditor_DragEnter(object sender, DragEventArgs e) + { + if (e.Data.GetDataPresent(DataFormats.FileDrop)) + e.Effect = DragDropEffects.Copy; + } + + private void PlayerItemEditor_DragDrop(object sender, DragEventArgs e) + { + string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); + + if (files.Length != 1 || Directory.Exists(files[0])) + return; + + string path = files[0]; // open first D&D + var fi = new FileInfo(path); + if (fi.Length > ItemArray.TotalSize || fi.Length % Item.SIZE != 0) + return; + + System.Media.SystemSounds.Asterisk.Play(); + var data = File.ReadAllBytes(path); + if (sender == ItemEditor) + { + var item = new Item(BitConverter.ToUInt64(data, 0)); + ItemEditor.LoadItem(item); + } + else + { + bool skipOccupiedSlots = (ModifierKeys & Keys.Alt) != 0; + ImportItemData(data, skipOccupiedSlots); + } + } + + public void EnableDragDrop(Control parent, DragEventHandler enter, DragEventHandler drop) + { + parent.AllowDrop = true; + parent.DragEnter += enter; + parent.DragDrop += drop; + foreach (var control in parent.Controls.OfType()) + { + control.AllowDrop = true; + control.DragEnter += enter; + control.DragDrop += drop; + } + } } }