Refactor stuff, clean up managers

This commit is contained in:
John Gross
2017-06-24 17:25:22 -07:00
parent 4962c753cd
commit 4b4a069adb
51 changed files with 607 additions and 450 deletions

View File

@@ -1,14 +1,16 @@
using System.Linq;
using NLog;
using Sandbox.Game.Entities;
using Sandbox.ModAPI;
using Torch.Server.ViewModels.Blocks;
namespace Torch.Server.ViewModels.Entities
{
public class GridViewModel : EntityViewModel
public class GridViewModel : EntityViewModel, ILazyLoad
{
private MyCubeGrid Grid => (MyCubeGrid)Entity;
public MTObservableCollection<BlockViewModel> Blocks { get; } = new MTObservableCollection<BlockViewModel>();
private static readonly Logger Log = LogManager.GetLogger(nameof(GridViewModel));
/// <inheritdoc />
public string DescriptiveName => $"{Name} ({Grid.BlocksCount} blocks)";
@@ -17,17 +19,8 @@ namespace Torch.Server.ViewModels.Entities
public GridViewModel(MyCubeGrid grid, EntityTreeViewModel tree) : base(grid, tree)
{
TorchBase.Instance.InvokeBlocking(() =>
{
foreach (var block in grid.GetFatBlocks().Where(b => b is IMyTerminalBlock))
{
Blocks.Add(new BlockViewModel((IMyTerminalBlock)block, tree));
}
});
Blocks.Sort(b => b.Block.GetType().AssemblyQualifiedName);
grid.OnBlockAdded += Grid_OnBlockAdded;
grid.OnBlockRemoved += Grid_OnBlockRemoved;
Log.Debug($"Creating model {Grid.DisplayName}");
Blocks.Add(new BlockViewModel(null, Tree));
}
private void Grid_OnBlockRemoved(Sandbox.Game.Entities.Cube.MySlimBlock obj)
@@ -48,5 +41,27 @@ namespace Torch.Server.ViewModels.Entities
Blocks.Sort(b => b.Block.GetType().AssemblyQualifiedName);
OnPropertyChanged(nameof(Name));
}
private bool _load;
public void Load()
{
if (_load)
return;
Log.Debug($"Loading model {Grid.DisplayName}");
_load = true;
Blocks.Clear();
TorchBase.Instance.InvokeBlocking(() =>
{
foreach (var block in Grid.GetFatBlocks().Where(b => b is IMyTerminalBlock))
{
Blocks.Add(new BlockViewModel((IMyTerminalBlock)block, Tree));
}
});
Blocks.Sort(b => b.Block.GetType().AssemblyQualifiedName);
Grid.OnBlockAdded += Grid_OnBlockAdded;
Grid.OnBlockRemoved += Grid_OnBlockRemoved;
}
}
}