1- using System . Windows . Controls ;
1+ using System ;
2+ using System . Collections ;
3+ using System . Collections . Generic ;
4+ using System . Windows . Controls ;
5+ using NLog ;
6+ using Sandbox . Game . Entities ;
27using Sandbox . Game . World ;
38using Torch . API . Managers ;
49using Torch . Collections ;
510using Torch . Server . Managers ;
11+ using Torch . Utils ;
612using VRage . Game . Entity ;
713using VRage . Game . ModAPI ;
814using VRage . ModAPI ;
@@ -14,6 +20,8 @@ public class EntityViewModel : ViewModel
1420 {
1521 protected EntityTreeViewModel Tree { get ; }
1622
23+ private static Logger Log = LogManager . GetCurrentClassLogger ( ) ;
24+
1725 private IMyEntity _backing ;
1826 public IMyEntity Entity
1927 {
@@ -43,6 +51,75 @@ public virtual string Name
4351 }
4452 }
4553
54+ private string _descriptiveName ;
55+ public string DescriptiveName
56+ {
57+ get => _descriptiveName ?? ( _descriptiveName = GetSortedName ( EntityTreeViewModel . SortEnum . Name ) ) ;
58+ set => _descriptiveName = value ;
59+ }
60+
61+ public virtual string GetSortedName ( EntityTreeViewModel . SortEnum sort )
62+ {
63+ switch ( sort )
64+ {
65+ case EntityTreeViewModel . SortEnum . Name :
66+ return Name ;
67+ case EntityTreeViewModel . SortEnum . Size :
68+ return $ "{ Name } ({ Entity . WorldVolume . Radius * 2 : N} m)";
69+ case EntityTreeViewModel . SortEnum . Speed :
70+ return $ "{ Name } ({ Entity . Physics ? . LinearVelocity . Length ( ) ?? 0 : N} m/s)";
71+ case EntityTreeViewModel . SortEnum . BlockCount :
72+ if ( Entity is MyCubeGrid grid )
73+ return $ "{ Name } ({ grid . BlocksCount } blocks)";
74+ return Name ;
75+ case EntityTreeViewModel . SortEnum . DistFromCenter :
76+ return $ "{ Name } ({ Entity . GetPosition ( ) . Length ( ) : N} m)";
77+ case EntityTreeViewModel . SortEnum . Owner :
78+ if ( Entity is MyCubeGrid g )
79+ return $ "{ Name } ({ g . GetGridOwnerName ( ) } )";
80+ return Name ;
81+ default :
82+ throw new ArgumentOutOfRangeException ( nameof ( sort ) , sort , null ) ;
83+ }
84+ }
85+
86+ public virtual int CompareToSort ( EntityViewModel other , EntityTreeViewModel . SortEnum sort )
87+ {
88+ switch ( sort )
89+ {
90+ case EntityTreeViewModel . SortEnum . Name :
91+ return string . Compare ( Name , other . Name , StringComparison . InvariantCultureIgnoreCase ) ;
92+ case EntityTreeViewModel . SortEnum . Size :
93+ return Entity . WorldVolume . Radius . CompareTo ( other . Entity . WorldVolume . Radius ) ;
94+ case EntityTreeViewModel . SortEnum . Speed :
95+ if ( Entity . Physics == null )
96+ {
97+ if ( other . Entity . Physics == null )
98+ return 0 ;
99+ return - 1 ;
100+ }
101+ if ( other . Entity . Physics == null )
102+ return 1 ;
103+ return Entity . Physics . LinearVelocity . LengthSquared ( ) . CompareTo ( other . Entity . Physics . LinearVelocity . LengthSquared ( ) ) ;
104+ case EntityTreeViewModel . SortEnum . BlockCount :
105+ {
106+ if ( Entity is MyCubeGrid ga && other . Entity is MyCubeGrid gb )
107+ return ga . BlocksCount . CompareTo ( gb . BlocksCount ) ;
108+ goto case EntityTreeViewModel . SortEnum . Name ;
109+ }
110+ case EntityTreeViewModel . SortEnum . DistFromCenter :
111+ return Entity . GetPosition ( ) . LengthSquared ( ) . CompareTo ( other . Entity . GetPosition ( ) . LengthSquared ( ) ) ;
112+ case EntityTreeViewModel . SortEnum . Owner :
113+ {
114+ if ( Entity is MyCubeGrid ga && other . Entity is MyCubeGrid gb )
115+ return string . Compare ( ga . GetGridOwnerName ( ) , gb . GetGridOwnerName ( ) , StringComparison . InvariantCultureIgnoreCase ) ;
116+ goto case EntityTreeViewModel . SortEnum . Name ;
117+ }
118+ default :
119+ throw new ArgumentOutOfRangeException ( nameof ( sort ) , sort , null ) ;
120+ }
121+ }
122+
46123 public virtual string Position
47124 {
48125 get => Entity ? . GetPosition ( ) . ToString ( ) ;
@@ -76,5 +153,20 @@ public EntityViewModel()
76153 {
77154
78155 }
156+
157+ public class Comparer : IComparer < EntityViewModel >
158+ {
159+ private EntityTreeViewModel . SortEnum _sort ;
160+
161+ public Comparer ( EntityTreeViewModel . SortEnum sort )
162+ {
163+ _sort = sort ;
164+ }
165+
166+ public int Compare ( EntityViewModel x , EntityViewModel y )
167+ {
168+ return x . CompareToSort ( y , _sort ) ;
169+ }
170+ }
79171 }
80172}
0 commit comments