Skip to content

Commit

Permalink
MainForm - Add support droping folder in
Browse files Browse the repository at this point in the history
  • Loading branch information
NessieHax committed Aug 16, 2024
1 parent 5b6703f commit 3245609
Showing 1 changed file with 76 additions and 20 deletions.
96 changes: 76 additions & 20 deletions PCK-Studio/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,17 +1254,6 @@ private void treeViewMain_DragEnter(object sender, DragEventArgs e)

private void treeViewMain_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) && e.Data.GetData(DataFormats.FileDrop) is string[] files)
{
ImportFiles(files);
return;
}

string dataFormat = typeof(TreeNode).FullName;

if (!e.Data.GetDataPresent(dataFormat))
return;

// Retrieve the client coordinates of the drop location.
Point dragLocation = new Point(e.X, e.Y);
Point targetPoint = treeViewMain.PointToClient(dragLocation);
Expand All @@ -1274,9 +1263,30 @@ private void treeViewMain_DragDrop(object sender, DragEventArgs e)

// Retrieve the node at the drop location.
TreeNode targetNode = treeViewMain.GetNodeAt(targetPoint);

if (e.Data.GetDataPresent(DataFormats.FileDrop) && e.Data.GetData(DataFormats.FileDrop) is string[] filesDropped)
{
IEnumerable<string> files = filesDropped.Where(File.Exists);
IEnumerable<string> directoryFiles = filesDropped
.Where(f => (File.GetAttributes(f) & FileAttributes.Directory) != 0)
.SelectMany(dir => Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories));

string baseDirectory = Path.GetDirectoryName(filesDropped.First());

IEnumerable<string> importPaths = files.Concat(directoryFiles);

ImportFiles(baseDirectory, importPaths, string.IsNullOrWhiteSpace(targetNode?.FullPath) ? string.Empty : targetNode?.FullPath);
return;
}

string dataFormat = typeof(TreeNode).FullName;

if (targetNode is null)
return;

if (!e.Data.GetDataPresent(dataFormat))
return;

bool isTargetPckFile = targetNode.IsTagOfType<PckAsset>();
TreeNode draggedNode = e.Data.GetData(dataFormat) as TreeNode;
if (draggedNode == null)
Expand Down Expand Up @@ -1356,33 +1366,79 @@ IEnumerable<TreeNode> GetAllChildNodes(TreeNodeCollection root)
return childNodes;
}


private void ImportFiles(string[] files)
private void ImportFiles(string baseDirectory, IEnumerable<string> files, string prefix)
{
int fileCount = files.Count();
int addedCount = 0;
int skippedFiles = 0;
int skipAttempts = 3;
int typeDuplication = 0;
PckAssetType lastSelectedAssetType = PckAssetType.SkinFile;
bool askForAssetType = true;
foreach (var filepath in files)
{
using AddFilePrompt addFile = new AddFilePrompt(Path.GetFileName(filepath));
string assetPath = Path.Combine(prefix + filepath.Substring(baseDirectory.Length)).TrimStart('/', '\\');
PckAssetType assetType = lastSelectedAssetType;

if (askForAssetType)
{
using AddFilePrompt addFile = new AddFilePrompt(assetPath);
if (addFile.ShowDialog(this) != DialogResult.OK)
{
skippedFiles++;
skipAttempts--;
if (skipAttempts > 0)
continue;

if (currentPCK.Contains(addFile.Filepath, addFile.Filetype))
int remainingFileCount = fileCount - addedCount - skippedFiles;
DialogResult abortFurtherImport = MessageBox.Show($"Do you wan't to abort further file imports?\n{remainingFileCount} file(s) left.", "Abort further import", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (abortFurtherImport == DialogResult.Yes)
{
skippedFiles += remainingFileCount;
break;
}
skipAttempts = 3;
continue;
}

assetType = addFile.Filetype;
assetPath = addFile.Filepath;

if (lastSelectedAssetType == assetType)
typeDuplication++;
lastSelectedAssetType = addFile.Filetype;
if (typeDuplication > 1)
{
DialogResult useSameTypeForRest = MessageBox.Show($"Do you want to import all remaining files as {lastSelectedAssetType}?", "Import all as", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (useSameTypeForRest == DialogResult.Yes)
{
askForAssetType = false;
}
}
}

if (currentPCK.Contains(filepath, assetType))
{
MessageBox.Show(this, $"'{addFile.Filepath}' of type {addFile.Filetype} already exists.\nSkiping file.", "File already exists", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
if (askForAssetType)
MessageBox.Show(this, $"'{assetPath}' of type {assetType} already exists.\nSkiping file.", "File already exists", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
Debug.WriteLine($"'{assetPath}' of type {assetType} already exists.\nSkiping file.");
continue;
}
PckAsset importedAsset = currentPCK.CreateNewAsset(addFile.Filepath, addFile.Filetype, () => File.ReadAllBytes(filepath));
PckAsset importedAsset = currentPCK.CreateNewAsset(assetPath, assetType, () => File.ReadAllBytes(filepath));
string propertyFile = filepath + ".txt";
if (File.Exists(propertyFile))
{
importedAsset.DeserializeProperties(File.ReadAllLines(propertyFile));
}
addedCount++;

BuildMainTreeView();
}
Trace.TraceInformation("[{0}] Imported {1} file(s).", nameof(ImportFiles), addedCount);
Trace.TraceInformation("[{0}] Skipped {1} file(s).", nameof(ImportFiles), skippedFiles);
if (addedCount > 0)
{
wasModified = true;
BuildMainTreeView();
}
Trace.TraceInformation("[{0}] Imported {1} file(s).", nameof(ImportFiles), addedCount);
}

#endregion
Expand Down

0 comments on commit 3245609

Please sign in to comment.