Skip to content

Commit dfbd264

Browse files
Added basic code category display
1 parent df4ee7f commit dfbd264

5 files changed

Lines changed: 124 additions & 79 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:cb="using:HedgeModManager.UI.Controls.Basic"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:cc="using:HedgeModManager.UI.Controls.Codes"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:vmc="using:HedgeModManager.UI.ViewModels.Codes"
8+
mc:Ignorable="d" d:DesignWidth="720" d:DesignHeight="390"
9+
x:Class="HedgeModManager.UI.Controls.Codes.CodeCategory"
10+
x:DataType="vmc:CodeCategoryViewModel">
11+
<StackPanel Orientation="Horizontal" Margin="0,4,0,0">
12+
<TextBlock Text="&#x21B3;" FontWeight="Bold" />
13+
<StackPanel Margin="16,0,0,0">
14+
<TextBlock Text="{Binding Name}" Margin="0,0,0,2" FontWeight="Bold" />
15+
<ItemsControl ItemsSource="{Binding Categories}">
16+
<ItemsControl.ItemTemplate>
17+
<DataTemplate>
18+
<cc:CodeCategory />
19+
</DataTemplate>
20+
</ItemsControl.ItemTemplate>
21+
</ItemsControl>
22+
<ItemsControl ItemsSource="{Binding Codes}">
23+
<ItemsControl.ItemTemplate>
24+
<DataTemplate>
25+
<cb:CheckBox Text="{Binding Code.Name}" IsChecked="{Binding Enabled}" />
26+
</DataTemplate>
27+
</ItemsControl.ItemTemplate>
28+
</ItemsControl>
29+
</StackPanel>
30+
</StackPanel>
31+
</UserControl>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Avalonia.Controls;
2+
using Avalonia.Markup.Xaml;
3+
4+
namespace HedgeModManager.UI.Controls.Codes;
5+
6+
public partial class CodeCategory : UserControl
7+
{
8+
public CodeCategory()
9+
{
10+
AvaloniaXamlLoader.Load(this);
11+
}
12+
}

Source/HedgeModManager.UI/Controls/Codes/Codes.axaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
Text="Layout for this tab is not final."
1919
HorizontalAlignment="Center" />
2020
<ScrollViewer Grid.Row="1" Margin="12,4,12,12">
21-
<ItemsControl ItemsSource="{Binding CodesList, RelativeSource={RelativeSource AncestorType=cc:Codes}}">
22-
<ItemsControl.ItemTemplate>
23-
<DataTemplate>
24-
<cb:CheckBox Text="{Binding Code.Name}" IsChecked="{Binding Enabled}" />
25-
</DataTemplate>
26-
</ItemsControl.ItemTemplate>
27-
</ItemsControl>
21+
<StackPanel>
22+
<ItemsControl ItemsSource="{Binding Cateories, RelativeSource={RelativeSource AncestorType=cc:Codes}}">
23+
<ItemsControl.ItemTemplate>
24+
<DataTemplate>
25+
<cc:CodeCategory />
26+
</DataTemplate>
27+
</ItemsControl.ItemTemplate>
28+
</ItemsControl>
29+
</StackPanel>
2830
</ScrollViewer>
2931
</Grid>
3032
</UserControl>

Source/HedgeModManager.UI/Controls/Codes/Codes.axaml.cs

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Avalonia.Interactivity;
33
using Avalonia.Markup.Xaml;
44
using HedgeModManager.CodeCompiler;
5+
using HedgeModManager.Foundation;
56
using HedgeModManager.UI.ViewModels;
67
using HedgeModManager.UI.ViewModels.Codes;
78
using System.Collections.ObjectModel;
@@ -22,49 +23,55 @@ public Codes()
2223
AvaloniaXamlLoader.Load(this);
2324
}
2425

25-
//public void LogCodes(int level, CodeCategoryViewModel codeCategory)
26-
//{
27-
// if (codeCategory != null)
28-
// {
29-
// Logger.Debug($"{new string(' ', level * 4)}{codeCategory.Name}/");
30-
// foreach (var category in codeCategory.Cateories)
31-
// LogCodes(level + 1, category);
32-
// foreach (var code in codeCategory.Codes)
33-
// Logger.Debug($"{new string(' ', level * 4)}{code.Code.Name}");
34-
// }
35-
//}
36-
37-
//public ObservableCollection<CodeCategoryViewModel> MergeCategories(ObservableCollection<CodeCategoryViewModel> root)
38-
//{
39-
// var categoryGroups = root.GroupBy(x => x.Name);
40-
// var categories = new ObservableCollection<CodeCategoryViewModel>();
41-
//
42-
// // Merge codes
43-
// foreach (var group in categoryGroups)
44-
// {
45-
// var category = group.First();
46-
// categories.Add(category);
47-
//
48-
// if (group.Count() == 1)
49-
// continue;
50-
//
51-
// // Move Codes
52-
// group.Skip(1)
53-
// .SelectMany(x => x.Codes)
54-
// .ToList()
55-
// .ForEach(category.Codes.Add);
56-
//
57-
// // Move sub categories
58-
// group.Skip(1)
59-
// .SelectMany(x => x.Cateories)
60-
// .ToList()
61-
// .ForEach(category.Cateories.Add);
62-
//
63-
// //Merge sub categories
64-
// category.Cateories = MergeCategories(category.Cateories);
65-
// }
66-
// return categories;
67-
//}
26+
public CodeCategoryViewModel CreateCategory(string path)
27+
{
28+
var split = path.Split('/');
29+
CodeCategoryViewModel? currentCategory = null;
30+
if (!(split.Length == 1 && string.IsNullOrEmpty(split[0])))
31+
{
32+
for (int i = 0; i < split.Length; ++i)
33+
{
34+
if (currentCategory == null)
35+
{
36+
currentCategory = Cateories.FirstOrDefault(x => x.Name == split[i]);
37+
if (currentCategory == null)
38+
{
39+
currentCategory = new(null, split[i]);
40+
Cateories.Add(currentCategory);
41+
}
42+
}
43+
else
44+
{
45+
var nextCategory = currentCategory.Categories.FirstOrDefault(x => x.Name == split[i]);
46+
if (nextCategory == null)
47+
{
48+
nextCategory = new(currentCategory, split[i]);
49+
currentCategory.Categories.Add(nextCategory);
50+
}
51+
currentCategory = nextCategory;
52+
}
53+
}
54+
}
55+
if (currentCategory == null)
56+
{
57+
currentCategory = new(null, "Uncategorized");
58+
Cateories.Add(currentCategory);
59+
}
60+
61+
return currentCategory;
62+
}
63+
64+
public void LogCodes(int level, CodeCategoryViewModel codeCategory)
65+
{
66+
if (codeCategory != null)
67+
{
68+
Logger.Debug($"{new string(' ', level * 2)}{codeCategory.Name}/");
69+
foreach (var category in codeCategory.Categories)
70+
LogCodes(level + 1, category);
71+
foreach (var code in codeCategory.Codes)
72+
Logger.Debug($"{new string(' ', (level + 1) * 2)}{code.Code.Name}");
73+
}
74+
}
6875

6976
public void RefreshUI()
7077
{
@@ -75,7 +82,7 @@ public void RefreshUI()
7582
return;
7683

7784
MainViewModel.Codes
78-
.Where(x => x.Type != Foundation.CodeType.Library && x is CSharpCode)
85+
.Where(x => x.Type != CodeType.Library && x is CSharpCode)
7986
.Cast<CSharpCode>()
8087
.Select(x => new CodeEntryViewModel(x))
8188
.ToList()
@@ -94,41 +101,29 @@ private void OnLoaded(object? sender, RoutedEventArgs e)
94101
MainViewModel.PropertyChanged += OnMainViewModelPropertyChanged;
95102
RefreshUI();
96103

97-
//// Generate categories
98-
//Cateories.Clear();
99-
//MainViewModel.Codes
100-
// .Select(x => x as CSharpCode)
101-
// .Where(x => x != null)
102-
// .DistinctBy(x => x!.Category)
103-
// .Select(x => new CodeCategoryViewModel(null, x!.Category))
104-
// .ToList()
105-
// .ForEach(Cateories.Add);
106-
//
107-
//// Merge categories
108-
//var cateories = MergeCategories(Cateories);
109-
//
110-
//// Test log
111-
//foreach (var category in cateories)
112-
// LogCodes(0, category);
104+
// Generate categories
105+
Cateories.Clear();
106+
foreach (var code in CodesList)
107+
{
108+
var category = CreateCategory(code.Code.Category);
109+
category.Codes.Add(code);
110+
}
111+
112+
// Test log
113+
Logger.Debug("Codes:");
114+
foreach (var category in Cateories)
115+
LogCodes(1, category);
113116

114117
// Add buttons
115118
if (MainViewModel.CurrentTabInfo != null)
116119
{
117120
MainViewModel.CurrentTabInfo.Buttons.Clear();
118-
//MainViewModel.CurrentTabInfo.Buttons.Add(new("Common.Button.SavePlay", Buttons.Y, async (b) =>
119-
//{
120-
// await MainViewModel.SaveAndRun();
121-
//}));
122121
MainViewModel.CurrentTabInfo.Buttons.Add(new("Codes.Button.UpdateCodes", ButtonsOLD.X, async (b) =>
123122
{
124123
b.IsEnabled = false;
125124
await MainViewModel.UpdateCodesAsync(true, false);
126125
b.IsEnabled = true;
127126
}));
128-
//MainViewModel.CurrentTabInfo.Buttons.Add(new("Common.Button.Select", Buttons.A, (b) =>
129-
//{
130-
// Logger.Information("Select Pressed");
131-
//}));
132127
}
133128
}
134129

Source/HedgeModManager.UI/ViewModels/Codes/CodeCategoryViewModel.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace HedgeModManager.UI.ViewModels.Codes;
55

66
public partial class CodeCategoryViewModel : ViewModelBase
77
{
8-
private CodeCategoryViewModel? _parent;
98

109
[ObservableProperty] private string _name = "Unnamed Category";
1110
[ObservableProperty] private bool _expanded = false;
12-
[ObservableProperty] private ObservableCollection<CodeCategoryViewModel> _cateories = [];
11+
[ObservableProperty] private CodeCategoryViewModel? _parent;
12+
[ObservableProperty] private ObservableCollection<CodeCategoryViewModel> _categories = [];
1313
[ObservableProperty] private ObservableCollection<CodeEntryViewModel> _codes = [];
1414

1515
public CodeCategoryViewModel(CodeCategoryViewModel? parent, string name)
@@ -22,7 +22,7 @@ public CodeCategoryViewModel(CodeCategoryViewModel? parent, string name)
2222
{
2323
string newCategoryName = name.Substring(firstIndex + 1);
2424
Name = name.Substring(0, firstIndex);
25-
Cateories.Add(new CodeCategoryViewModel(this, newCategoryName));
25+
Categories.Add(new CodeCategoryViewModel(this, newCategoryName));
2626
}
2727
}
2828

@@ -31,6 +31,11 @@ public string GetPathToRoot(CodeCategoryViewModel? parent, string str)
3131
if (parent == null)
3232
return str;
3333

34-
return GetPathToRoot(parent._parent, parent.Name + "/" + str);
34+
return GetPathToRoot(parent.Parent, parent.Name + "/" + str);
35+
}
36+
37+
public override string ToString()
38+
{
39+
return GetPathToRoot(Parent, Name);
3540
}
3641
}

0 commit comments

Comments
 (0)