Skip to content

Commit b2ef4c0

Browse files
committed
Add grid filtering and owner block count display.
- Introduced filter functionality for grids, allowing dynamic filtering by name. - Updated SortedView to allow filter function. - Added owner block count tracking and display per grid. - Updated UI elements to show grid block count and owner details.
1 parent 8eaebd5 commit b2ef4c0

7 files changed

Lines changed: 128 additions & 39 deletions

File tree

‎Torch.Server/ViewModels/Entities/EntityViewModel.cs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
4-
using System.Windows;
5-
using System.Windows.Controls;
63
using NLog;
7-
using Sandbox.Game;
84
using Sandbox.Game.Entities;
9-
using Sandbox.Game.Entities.Character;
10-
using Sandbox.Game.Entities.Cube;
11-
using Sandbox.Game.World;
125
using Torch.API.Managers;
136
using Torch.Collections;
147
using Torch.Server.Managers;
158
using Torch.Utils;
16-
using VRage.Game.Entity;
17-
using VRage.Game.ModAPI;
189
using VRage.ModAPI;
1910
using VRageMath;
2011

‎Torch.Server/ViewModels/Entities/GridViewModel.cs‎

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Runtime.CompilerServices;
54
using Sandbox.Definitions;
65
using Sandbox.Game.Entities;
76
using Sandbox.Game.Entities.Cube;
8-
using Sandbox.ModAPI;
9-
using Torch.API.Managers;
7+
using Sandbox.Game.World;
108
using Torch.Collections;
119
using Torch.Server.ViewModels.Blocks;
1210
using VRage.Game;
@@ -50,18 +48,19 @@ public MtObservableSortedDictionary<MyCubeBlockDefinition, MtObservableSortedDic
5048
Blocks { get; } =
5149
new MtObservableSortedDictionary<MyCubeBlockDefinition, MtObservableSortedDictionary<long, BlockViewModel>>(
5250
CubeBlockDefinitionComparer.Default);
53-
51+
5452
public GridViewModel()
5553
{
54+
5655
}
57-
56+
5857
public GridViewModel(MyCubeGrid grid, EntityTreeViewModel tree) : base(grid, tree)
5958
{
6059
//DescriptiveName = $"{grid.DisplayName} ({grid.BlocksCount} blocks)";
6160
Blocks.Add(_fillerDefinition, new MtObservableSortedDictionary<long, BlockViewModel>());
6261
}
6362

64-
private void Grid_OnBlockRemoved(Sandbox.Game.Entities.Cube.MySlimBlock obj)
63+
private void Grid_OnBlockRemoved(MySlimBlock obj)
6564
{
6665
if (obj.FatBlock != null)
6766
RemoveBlock(obj.FatBlock);
@@ -75,16 +74,62 @@ private void RemoveBlock(MyCubeBlock block)
7574
return;
7675
if (group.Remove(block.EntityId) && group.Count == 0 && Blocks.Count > 1)
7776
Blocks.Remove(block.BlockDefinition);
77+
78+
long ownerId = block.OwnerId;
79+
if (block.OwnerId == 0)
80+
ownerId = block.IDModule.Owner;
81+
82+
if (ownerId != 0)
83+
{
84+
var playerIdent = MySession.Static.Players.TryGetIdentity(ownerId);
85+
if (playerIdent != null)
86+
{
87+
var name = playerIdent.DisplayName;
88+
if (Owners.ContainsKey(name))
89+
Owners[name]--;
90+
if (Owners[name] == 0)
91+
Owners.Remove(name);
92+
}
93+
}
7894
}
7995

8096
private void AddBlock(MyTerminalBlock block)
8197
{
8298
if (!Blocks.TryGetValue(block.BlockDefinition, out var group))
8399
group = Blocks[block.BlockDefinition] = new MtObservableSortedDictionary<long, BlockViewModel>();
84100
group.Add(block.EntityId, new BlockViewModel(block, Tree));
101+
102+
long ownerId = block.OwnerId;
103+
if (block.OwnerId == 0)
104+
ownerId = block.IDModule.Owner;
105+
106+
if (ownerId != 0)
107+
{
108+
var playerIdent = MySession.Static.Players.TryGetIdentity(ownerId);
109+
if (playerIdent != null)
110+
{
111+
var name = playerIdent.DisplayName;
112+
if (!Owners.TryGetValue(name, out int count))
113+
Owners[name] = 1;
114+
else
115+
Owners[name] = count + 1;
116+
}
117+
}
118+
}
119+
120+
public int BlockCount
121+
{
122+
get
123+
{
124+
if (Grid != null)
125+
return Grid.BlocksCount;
126+
return 0;
127+
}
85128
}
86129

87-
private void Grid_OnBlockAdded(Sandbox.Game.Entities.Cube.MySlimBlock obj)
130+
public MtObservableSortedDictionary<string,int> Owners { get; } = new MtObservableSortedDictionary<string, int>();
131+
132+
private void Grid_OnBlockAdded(MySlimBlock obj)
88133
{
89134
var block = obj.FatBlock as MyTerminalBlock;
90135
if (block != null)

‎Torch.Server/ViewModels/EntityTreeViewModel.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public enum SortEnum
3737
public Dispatcher ControlDispatcher => _control.Dispatcher;
3838

3939
public SortedView<GridViewModel> SortedGrids { get; }
40+
public SortedView<GridViewModel> FilteredSortedGrids { get; }
4041
public SortedView<CharacterViewModel> SortedCharacters { get; }
4142
public SortedView<FloatingObjectViewModel> SortedFloatingObjects { get; }
4243
public SortedView<VoxelMapViewModel> SortedVoxelMaps { get; }
@@ -67,6 +68,7 @@ public EntityTreeViewModel(UserControl control)
6768
_control = control;
6869
var comparer = new EntityViewModel.Comparer(_currentSort);
6970
SortedGrids = new SortedView<GridViewModel>(Grids.Values, comparer);
71+
FilteredSortedGrids = new SortedView<GridViewModel>(Grids.Values, comparer);
7072
SortedCharacters = new SortedView<CharacterViewModel>(Characters.Values, comparer);
7173
SortedFloatingObjects = new SortedView<FloatingObjectViewModel>(FloatingObjects.Values, comparer);
7274
SortedVoxelMaps = new SortedView<VoxelMapViewModel>(VoxelMaps.Values, comparer);

‎Torch.Server/Views/Entities/GridView.xaml‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,44 @@
2222
<RowDefinition Height="*"/>
2323
</Grid.RowDefinitions>
2424
<StackPanel Grid.Row="0" Orientation="Horizontal">
25+
<Button Content="Repair" Margin="3" Width="125" Tag="{Binding ElementName=EntityIDText, Path=Text}" Click="RepairGrid"/>
26+
</StackPanel>
27+
<StackPanel Grid.Row="1" Orientation="Horizontal">
2528
<Label Content="Name" Width="125"/>
2629
<TextBox Text="{Binding Name}" Margin="3"/>
2730
</StackPanel>
28-
<StackPanel Grid.Row="1" Orientation="Horizontal">
31+
<StackPanel Grid.Row="2" Orientation="Horizontal">
2932
<Label Content="Position" Width="125"/>
3033
<TextBox Text="{Binding Position}" Margin="3" />
3134
</StackPanel>
32-
<StackPanel Grid.Row="2" Orientation="Horizontal">
35+
<StackPanel Grid.Row="3" Orientation="Horizontal">
3336
<Label Content="EntityID" Width="125"/>
3437
<TextBox x:Name="EntityIDText" Text="{Binding Id, Mode=OneWay}" Margin="3" />
3538
</StackPanel>
36-
<StackPanel Grid.Row="3" Orientation="Horizontal">
39+
<StackPanel Grid.Row="4" Orientation="Horizontal">
3740
<Label Content="Speed" Width="125" />
3841
<TextBox Text="{Binding Speed, Mode=OneWay}" Margin="3" />
3942
</StackPanel>
40-
<StackPanel Grid.Row="4" Orientation="Horizontal">
43+
<StackPanel Grid.Row="5" Orientation="Horizontal">
4144
<Label Content="Distance From Center" Width="125" />
4245
<TextBox Text="{Binding PrettyDistance, Mode=OneWay}" Margin="3" />
4346
</StackPanel>
44-
<StackPanel Grid.Row="5" Orientation="Horizontal">
45-
<Button Content="Repair" Margin="3" Width="125" Tag="{Binding ElementName=EntityIDText, Path=Text}" Click="RepairGrid"/>
47+
<StackPanel Grid.Row="6" Orientation="Horizontal">
48+
<Label Content="Blocks" Width="125" />
49+
<TextBox Text="{Binding BlockCount, Mode=OneWay}" Margin="3" />
50+
</StackPanel>
51+
<StackPanel Grid.Row="7" Grid.ColumnSpan="2" Orientation="Horizontal">
52+
<TextBlock Text="Owners and Blocks" Width="125" ToolTip="Expand the blocklist on the grid entry to populate."/>
53+
54+
<DataGrid x:Name="DictGrid" AutoGenerateColumns="False" CanUserAddRows="False"
55+
IsReadOnly="True" ItemsSource="{Binding Owners}"
56+
Margin="3" ToolTip="Expand the blocklist on the grid entry to populate.">
57+
<DataGrid.Columns>
58+
<DataGridTextColumn Header="Name" Binding="{Binding Key, Mode=OneWay}"/>
59+
<DataGridTextColumn Header="Count" Binding="{Binding Value, Mode=OneWay}"/>
60+
</DataGrid.Columns>
61+
</DataGrid>
4662
</StackPanel>
47-
4863

4964
<ScrollViewer Grid.Row="6" Margin="3" VerticalScrollBarVisibility="Auto">
5065
<local:EntityControlsView DataContext="{Binding}"/>

‎Torch.Server/Views/EntitiesControl.xaml‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,26 @@
9999
</Grid>
100100
</TabItem>
101101

102-
<TabItem Header="Grids">
102+
<TabItem Header="Grids" IsSelected="True">
103103
<Grid>
104104
<Grid.ColumnDefinitions>
105105
<ColumnDefinition Width="5*"/>
106106
<ColumnDefinition Width="5*"/>
107107
</Grid.ColumnDefinitions>
108108
<Grid Grid.Column="0">
109109
<Grid.RowDefinitions>
110-
<RowDefinition />
110+
<RowDefinition Height="Auto"/>
111+
<RowDefinition Height="*" />
112+
<RowDefinition Height="Auto" />
111113
<RowDefinition Height="Auto" />
112114
<RowDefinition Height="Auto" />
113115
</Grid.RowDefinitions>
114-
<TreeView Grid.Row="0" Grid.Column="0" Margin="3" DockPanel.Dock="Top"
116+
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
117+
<TextBlock Text="Filter" Margin="3"/>
118+
<TextBox Name="FilterBox" ToolTip="Filter" MinWidth="150"/>
119+
</StackPanel>
120+
121+
<TreeView Grid.Row="1" Grid.Column="0" Margin="3" DockPanel.Dock="Top"
115122
SelectedItemChanged="TreeView_Grids_OnSelectionItemChanged"
116123
TreeViewItem.Expanded="TreeViewItem_OnExpanded" Name="EntityTree_Grids">
117124
<TreeView.Resources>
@@ -138,15 +145,15 @@
138145
</HierarchicalDataTemplate>
139146
</TreeView.Resources>
140147

141-
<TreeViewItem ItemsSource="{Binding Path=SortedGrids}">
148+
<TreeViewItem ItemsSource="{Binding Path=FilteredSortedGrids}">
142149
<TreeViewItem.Header>
143-
<TextBlock Text="{Binding Grids.Count, StringFormat=Grids ({0})}" />
150+
<TextBlock Text="{Binding FilteredSortedGrids.FilteredCount, StringFormat=Grids ({0})}" />
144151
</TreeViewItem.Header>
145152
</TreeViewItem>
146153
</TreeView>
147154

148-
<ComboBox Grid.Row="1" Margin="3" Name="SortCombo_GridTab" SelectionChanged="SortCombo_SelectionChanged" />
149-
<StackPanel Grid.Column="0" Grid.Row="2" DockPanel.Dock="Bottom">
155+
<ComboBox Grid.Row="2" Margin="3" Name="SortCombo_GridTab" SelectionChanged="SortCombo_SelectionChanged" />
156+
<StackPanel Grid.Column="0" Grid.Row="3" DockPanel.Dock="Bottom">
150157
<Button Tag="Grids" Content="Delete Selected" Click="MultiDelete_OnClick" IsEnabled="{Binding CurrentEntity.CanDelete}" Margin="3" />
151158
<Button Content="Stop" Click="Stop_OnClick" IsEnabled="{Binding CurrentEntity.CanStop}" Margin="3" />
152159
</StackPanel>

‎Torch.Server/Views/EntitiesControl.xaml.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public EntitiesControl()
4545
SortCombo_Characters.ItemsSource = Enum.GetNames(typeof(EntityTreeViewModel.SortEnum));
4646
SortCombo_Voxels.ItemsSource = Enum.GetNames(typeof(EntityTreeViewModel.SortEnum));
4747
SortCombo_FloatingObjects.ItemsSource = Enum.GetNames(typeof(EntityTreeViewModel.SortEnum));
48+
49+
FilterBox.TextChanged += (sender, args) =>
50+
{
51+
string filter = FilterBox.Text;
52+
Entities.FilteredSortedGrids.Filter = grid => grid.Name.Contains(filter, StringComparison.OrdinalIgnoreCase);
53+
};
4854
}
4955

5056
private void TreeView_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)

‎Torch/Collections/SortedView.cs‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
using System.Collections.Generic;
44
using System.Collections.Specialized;
55
using System.ComponentModel;
6-
using System.Linq;
76
using System.Runtime.CompilerServices;
8-
using System.Text;
9-
using System.Threading;
10-
using System.Threading.Tasks;
11-
using Havok;
127
using NLog;
138

149
namespace Torch.Collections
@@ -19,6 +14,7 @@ public class SortedView<T>: IReadOnlyCollection<T>, INotifyCollectionChanged, IN
1914
private readonly MtObservableCollectionBase<T> _backing;
2015
private IComparer<T> _comparer;
2116
private readonly List<T> _store;
17+
private Predicate<T> _filter;
2218

2319
public SortedView(MtObservableCollectionBase<T> backing, IComparer<T> comparer)
2420
{
@@ -55,7 +51,6 @@ private void backing_CollectionChanged(object sender, NotifyCollectionChangedEve
5551
default:
5652
throw new ArgumentOutOfRangeException();
5753
}
58-
5954
}
6055

6156
public IEnumerator<T> GetEnumerator()
@@ -70,6 +65,16 @@ IEnumerator IEnumerable.GetEnumerator()
7065

7166
public int Count => _backing.Count;
7267

68+
public int FilteredCount
69+
{
70+
get
71+
{
72+
if (_filter == null)
73+
return Count;
74+
return _store.Count;
75+
}
76+
}
77+
7378
private void InsertSorted(IEnumerable items)
7479
{
7580
foreach (var t in items)
@@ -100,6 +105,8 @@ private int InsertSorted(T item, IComparer<T> comparer = null)
100105
if (index < 0)
101106
index = ~index;
102107
_store.Insert(index, item);
108+
OnPropertyChanged(nameof(Count));
109+
OnPropertyChanged(nameof(FilteredCount));
103110
return index;
104111
}
105112

@@ -115,15 +122,31 @@ public void Sort(IComparer<T> comparer = null)
115122

116123
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
117124
}
125+
126+
public Predicate<T> Filter
127+
{
128+
get => _filter;
129+
set
130+
{
131+
_filter = value;
132+
Refresh();
133+
}
134+
}
118135

119136
public void Refresh()
120137
{
121138
_store.Clear();
122139
//_store.AddRange(_backing);
123140
_store.EnsureCapacity(_backing.Count);
124-
foreach (var e in _backing)
125-
_store.Add(e);
126-
Sort();
141+
foreach (var item in _backing)
142+
{
143+
if (_filter == null || _filter(item))
144+
_store.Add(item);
145+
}
146+
Sort(); // maintain sort
147+
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
148+
OnPropertyChanged(nameof(Count));
149+
OnPropertyChanged(nameof(FilteredCount));
127150
}
128151

129152
public void SetComparer(IComparer<T> comparer, bool resort = true)

0 commit comments

Comments
 (0)