Skip to content

Commit

Permalink
Make Folder Navigation a bit better
Browse files Browse the repository at this point in the history
  • Loading branch information
romibi committed Jan 3, 2022
1 parent 1c63f34 commit 208200d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 21 deletions.
12 changes: 10 additions & 2 deletions ScrapPackedExplorer/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="ch.romibi.Scrap.Packed.Explorer.MainWindow"
<Window x:Class="ch.romibi.Scrap.Packed.Explorer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Expand All @@ -10,12 +10,20 @@
<DockPanel HorizontalAlignment="Center" Height="78" LastChildFill="False" VerticalAlignment="Top" Width="800">
<Button Name="OpenButton" Content="Open" Width="100" Click="OpenButton_Click"/>
</DockPanel>
<TreeView Margin="0,78,0,0" Name="FileTree">
<TreeView Margin="0,78,400,0" Name="FileTree">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TreeEntry}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>

<ListView Margin="400,78,0,0" Name="TreeContent" ItemsSource="{Binding Items}" MouseDoubleClick="TreeContent_MouseDoubleClick">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" Width="100"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
125 changes: 108 additions & 17 deletions ScrapPackedExplorer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ch.romibi.Scrap.Packed.PackerLib.DataTypes;

namespace ch.romibi.Scrap.Packed.Explorer
{
Expand All @@ -33,13 +34,41 @@ public MainWindow()
private void RefreshTreeView()
{

TreeEntry root = new TreeEntry() { Name = loadedPackedFile.fileName };
TreeEntry root = new TreeEntry(null) { Name = loadedPackedFile.fileName };

foreach (string file in loadedPackedFile.GetFileNames()) {
root.AddFilename(file);
foreach (PackedFileIndexData file in loadedPackedFile.GetFileIndexDataList())
{
root.AddFileData(file);
}

root.Sort();
FileTree.Items.Add(root);

TreeContent.Items.Clear();
//TreeContent.Items.Add(new TreeEntry() { Name = ".." });
foreach (var item in root.Items) {
TreeContent.Items.Add(item);
}

}

private void TreeContent_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var clickedItem = ((FrameworkElement)e.OriginalSource).DataContext as TreeEntry;
if (clickedItem != null)
{
if (!clickedItem.IsDirectory)
return;
TreeContent.Items.Clear();
if(!(clickedItem.Parent is null))
{
var navigateUpItem = new TreeEntry(clickedItem.Parent.Parent) { Name = "..", Items = clickedItem.Parent.Items };
TreeContent.Items.Add(navigateUpItem);
}
foreach(var item in clickedItem.Items)
{
TreeContent.Items.Add(item);
}
}
}


Expand All @@ -55,36 +84,98 @@ private void OpenButton_Click(object sender, RoutedEventArgs e)

}

public class TreeEntry
public class TreeEntry : IComparable
{
public TreeEntry()
public TreeEntry(TreeEntry p_Parent)
{
this.Items = new ObservableCollection<TreeEntry>();
Items = new ObservableCollection<TreeEntry>();
IndexData = null;
Parent = p_Parent;
}

public string Name { get; set; }

public void AddFilename(string p_FileName)
public TreeEntry Parent { get; set; }

public PackedFileIndexData IndexData { get; set; }
public bool IsFile {
get {
return !IsDirectory;
}
}

public bool IsDirectory {
get {
return IndexData is null;
}
}

public void AddFileData(PackedFileIndexData p_File, string p_SubdirFilename = "")
{
if (p_FileName.Contains("/"))
string fileName;
if (p_SubdirFilename.Length == 0)
{
var nextDir = p_FileName.Split("/")[0];
fileName = p_File.FilePath;
}
else
{
fileName = p_SubdirFilename;
}

if (fileName.Contains("/"))
{
var nextDir = fileName.Split("/")[0];
TreeEntry subDir = null;
foreach(TreeEntry entry in Items)
foreach (TreeEntry entry in Items)
{
if (entry.Name.Equals(nextDir)) {
if (entry.Name.Equals(nextDir))
{
subDir = entry;
break;
}
}
if (subDir == null) {
subDir = new TreeEntry() { Name = nextDir };
if (subDir == null)
{
subDir = new TreeEntry(this) { Name = nextDir };
Items.Add(subDir);
}
subDir.AddFilename(p_FileName.Substring(nextDir.Length + 1));
} else
subDir.AddFileData(p_File, fileName.Substring(nextDir.Length + 1));
}
else
{
Items.Add(new TreeEntry() { Name = p_FileName });
Items.Add(new TreeEntry(this) { Name = fileName, IndexData = p_File });
}
}

public int CompareTo(object o)
{
TreeEntry a = this;
TreeEntry b = (TreeEntry)o;
if (a.IsDirectory == b.IsDirectory)
{
return a.Name.CompareTo(b.Name);
}
else
{
if (a.IsDirectory)
return -1;
else
return 1;
}

}

public void Sort()
{
var sorted = Items.OrderBy(x => x).ToList();

for (int i = 0; i < sorted.Count(); i++)
Items.Move(Items.IndexOf(sorted[i]), i);

foreach (var item in Items) {
if (item.IsDirectory) {
item.Sort();
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions ScrapPackedLibrary/DataTypes/PackedMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ch.romibi.Scrap.Packed.PackerLib.DataTypes
{
class PackedMetaData
public class PackedMetaData
{
private const UInt32 DATA_LENGTH_STATIC = 12; // 4 bytes each: "PFBK", version (all 0s), number of files

Expand Down Expand Up @@ -34,7 +34,7 @@ private UInt32 CalculateFirstFileOffset()
}
}

class PackedFileIndexData
public class PackedFileIndexData
{
private const UInt32 DATA_LENGTH_STATIC = 12; // 4 bytes each: path length, file size, offset

Expand Down
5 changes: 5 additions & 0 deletions ScrapPackedLibrary/ScrapPackedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public List<string> GetFileNames()
return list;
}

public List<PackedFileIndexData> GetFileIndexDataList()
{
return metaData.fileList;
}

public void Add(string p_externalPath, string p_packedPath)
{
FileAttributes fileAttributes = File.GetAttributes(p_externalPath);
Expand Down

0 comments on commit 208200d

Please sign in to comment.