Compare commits

...
Author SHA1 Message Date
辉鸭蛋 1d4c755b4d Merge branch 'main' into feat-TreeListView 2024-09-24 01:02:59 +08:00
辉鸭蛋 89e92d57b2 has some bug 2024-09-16 14:44:57 +08:00
4 changed files with 165 additions and 44 deletions

No files matched your search

@@ -0,0 +1,79 @@
using System;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Linq;
using BetterGenshinImpact.GameTask.AutoPathing.Model;
using Wpf.Ui.Controls;
using static Vanara.PInvoke.Gdi32;
namespace BetterGenshinImpact.Model;
public class FileTreeModel<T>(string rootPath) : ITreeModel where T : class
{
public string? Name { get; set; }
public DirectoryInfo? Directory { get; set; }
public T? Data { get; set; }
public IEnumerable GetChildren(object? parent)
{
parent ??= new FileTreeModel<T>(rootPath)
{
Directory = new DirectoryInfo(rootPath)
};
if (parent is FileTreeModel<T> { Directory: not null } model)
{
// 返回子目录
foreach (var dir in model.Directory.GetDirectories())
{
yield return new FileTreeModel<T>(rootPath)
{
Name = dir.Name,
Directory = dir
};
}
// 返回文件
foreach (var file in model.Directory.GetFiles())
{
var m = new FileTreeModel<T>(rootPath)
{
Name = file.Name,
};
if (typeof(T) == typeof(PathingTask))
{
m.Data = PathingTask.BuildFromFilePath(file.FullName) as T;
}
yield return m;
}
}
}
public bool HasChildren(object parent)
{
if (parent is FileTreeModel<T> { Directory: not null } model)
{
try
{
// 检查是否有子目录或文件
return model.Directory.GetDirectories().Any() || model.Directory.GetFiles().Any();
}
catch (UnauthorizedAccessException)
{
// 捕获没有权限的异常并返回 false
return false;
}
catch (Exception ex)
{
// 处理其他可能的异常
Console.WriteLine($"An error occurred: {ex.Message}");
return false;
}
}
return false;
}
}
@@ -0,0 +1,18 @@
using BetterGenshinImpact.GameTask.AutoPathing.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using Wpf.Ui.Controls;
namespace BetterGenshinImpact.Model;
[ObservableObject]
public partial class PathingTaskTreeObject : TreeObject<PathingTaskTreeObject>
{
[ObservableProperty]
private string? _name;
[ObservableProperty]
private bool _isFolder;
[ObservableProperty]
private PathingTask? _task;
}
@@ -67,14 +67,40 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="80" />
<!-- <ColumnDefinition Width="100" /> -->
<ColumnDefinition Width="20" />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<Grid x:Name="Col1" Grid.Column="0" />
<Grid x:Name="Col2" Grid.Column="1" />
<Grid x:Name="Col3" Grid.Column="2" />
<!-- <Grid x:Name="Col4" Grid.Column="3" /> -->
</Grid>
<ui:ListView Grid.Row="5"
<ui:TreeListView Grid.Row="5" Model="{Binding FileItems}">
<ui:GridView>
<ui:GridView.Columns>
<ui:GridViewColumn Width="{Binding ElementName=Col1, Path=ActualWidth}" Header="名称">
<ui:GridViewColumn.CellTemplate>
<DataTemplate>
<ui:TreeRowExpander Content="{Binding Name}" />
</DataTemplate>
</ui:GridViewColumn.CellTemplate>
</ui:GridViewColumn>
<ui:GridViewColumn Width="{Binding ElementName=Col2, Path=ActualWidth}"
DisplayMemberBinding="{Binding Data.Info.TypeDesc}"
Header="类型" />
</ui:GridView.Columns>
</ui:GridView>
<ListBox.ContextMenu>
<ContextMenu>
<!-- <MenuItem Command="{Binding StartCommand}" -->
<!-- CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" -->
<!-- Header="执行任务" /> -->
<MenuItem Command="{Binding RefreshCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"
Header="刷新" />
</ContextMenu>
</ListBox.ContextMenu>
</ui:TreeListView>
<!--<ui:ListView Grid.Row="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding PathItems}"
@@ -87,7 +113,7 @@
<GridViewColumn Width="{Binding ElementName=Col2, Path=ActualWidth}"
DisplayMemberBinding="{Binding Info.TypeDesc}"
Header="类型" />
<!--<GridViewColumn Width="{Binding ElementName=Col3, Path=ActualWidth}" Header="操作">
~1~<GridViewColumn Width="{Binding ElementName=Col3, Path=ActualWidth}" Header="操作">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
@@ -98,7 +124,7 @@
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>-->
</GridViewColumn>@1@
</GridView>
</ListView.View>
<ListBox.ContextMenu>
@@ -117,6 +143,6 @@
<Setter Property="Background" Value="Transparent" />
</Style>
</ListView.Style>
</ui:ListView>
</ui:ListView>-->
</Grid>
</Page>
@@ -1,24 +1,16 @@
using BetterGenshinImpact.Core.Config;
using BetterGenshinImpact.Core.Script;
using BetterGenshinImpact.Core.Script.Group;
using BetterGenshinImpact.Core.Script.Project;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.GameTask.AutoPathing;
using BetterGenshinImpact.GameTask.AutoPathing.Model;
using BetterGenshinImpact.GameTask.Model.Enum;
using BetterGenshinImpact.Model;
using BetterGenshinImpact.Service.Interface;
using BetterGenshinImpact.View.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Wpf.Ui.Controls;
using Wpf.Ui.Violeta.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
@@ -28,43 +20,56 @@ public partial class MapPathingViewModel(IScriptService scriptService) : Observa
public static readonly string PathJsonPath = Global.Absolute(@"User\AutoPathing");
[ObservableProperty]
private ObservableCollection<PathingTask> _pathItems = [];
private TreeCollection<PathingTaskTreeObject> _pathItems = Load();
[ObservableProperty]
private FileTreeModel<PathingTask> _fileItems = new(PathJsonPath);
private MapViewer? _mapViewer;
private void InitScriptListViewData()
{
_pathItems.Clear();
var fileInfos = LoadScriptFolder(PathJsonPath);
foreach (var f in fileInfos)
{
try
{
_pathItems.Add(PathingTask.BuildFromFilePath(f.FullName));
}
catch (Exception e)
{
Toast.Warning($"地图追踪任务 {f.Name} 载入失败:{e.Message}");
}
}
_pathItems.Root.Name = PathJsonPath;
_pathItems.Root.IsFolder = true;
LoadFolderRecursive(PathJsonPath, _pathItems.Root);
}
private IEnumerable<FileInfo> LoadScriptFolder(string folder)
private static TreeCollection<PathingTaskTreeObject> Load()
{
if (!Directory.Exists(folder))
TreeCollection<PathingTaskTreeObject> tree = [];
tree.Root.Name = PathJsonPath;
tree.Root.IsFolder = true;
LoadFolderRecursive(PathJsonPath, tree.Root);
return tree;
}
private static void LoadFolderRecursive(string folder, PathingTaskTreeObject parent)
{
var directoryInfo = new DirectoryInfo(folder);
foreach (var dir in directoryInfo.GetDirectories())
{
Directory.CreateDirectory(folder);
var dirNode = new PathingTaskTreeObject { Name = dir.Name, IsFolder = true };
parent.Children.Add(dirNode);
LoadFolderRecursive(dir.FullName, dirNode);
}
var files = Directory.GetFiles(folder, "*.*",
SearchOption.AllDirectories);
return files.Select(file => new FileInfo(file)).ToList();
foreach (var file in directoryInfo.GetFiles())
{
var fileNode = new PathingTaskTreeObject
{
Name = file.Name,
IsFolder = false,
Task = PathingTask.BuildFromFilePath(file.FullName)
};
parent.Children.Add(fileNode);
}
}
public void OnNavigatedTo()
{
InitScriptListViewData();
// InitScriptListViewData();
}
public void OnNavigatedFrom()
@@ -99,13 +104,6 @@ public partial class MapPathingViewModel(IScriptService scriptService) : Observa
return;
}
// new TaskRunner(DispatcherTimerOperationEnum.UseCacheImageWithTriggerEmpty)
// .FireAndForget(async () =>
// {
// TaskTriggerDispatcher.Instance().AddTrigger("AutoPick", null);
// await new PathExecutor(CancellationContext.Instance.Cts).Pathing(item);
// });
var fileInfo = new FileInfo(item.FullPath);
var project = ScriptGroupProject.BuildPathingProject(fileInfo.Name, fileInfo.DirectoryName!);
await scriptService.RunMulti([project]);
@@ -139,8 +137,8 @@ public partial class MapPathingViewModel(IScriptService scriptService) : Observa
}
[RelayCommand]
public void OnRefresh(PathingTask? item)
public void OnRefresh(object? item)
{
InitScriptListViewData();
_fileItems = new(PathJsonPath);
}
}