Files
Aaru/Aaru.Gui/Controls/BlockMap.cs

122 lines
4.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
2020-02-27 00:33:26 +00:00
namespace Aaru.Gui.Controls
{
public class BlockMap : ColoredGrid
{
ulong _sectors;
public uint blocksToRead;
public uint sectorsToRead;
public BlockMap()
{
ColoredSectors = new ObservableCollection<ColoredBlock>();
ColoredSectors.CollectionChanged += OnColoredSectorsChanged;
}
public ulong Sectors
{
get => _sectors;
set
{
_sectors = value;
CalculateBoundaries();
Invalidate();
}
}
public ulong SectorsPerBlock { get; private set; }
public uint SectorsToRead
{
get => sectorsToRead;
set
{
sectorsToRead = value;
blocksToRead = (uint)(sectorsToRead / SectorsPerBlock);
2020-02-29 18:03:35 +00:00
if(sectorsToRead % SectorsPerBlock > 0)
blocksToRead++;
}
}
public ObservableCollection<ColoredBlock> ColoredSectors { get; }
void OnColoredSectorsChanged(object sender, NotifyCollectionChangedEventArgs args)
{
switch(args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach(object item in args.NewItems)
{
2020-02-29 18:03:35 +00:00
if(!(item is ColoredBlock block))
continue;
for(ulong i = 0; i < blocksToRead; i++)
2020-02-29 18:03:35 +00:00
ColoredBlocks.Add(new ColoredBlock((block.Block / SectorsPerBlock) + i, block.Color));
}
break;
case NotifyCollectionChangedAction.Move: break;
case NotifyCollectionChangedAction.Remove:
foreach(object item in args.OldItems)
{
2020-02-29 18:03:35 +00:00
if(!(item is ColoredBlock block))
continue;
for(ulong i = 0; i < blocksToRead; i++)
2020-02-29 18:03:35 +00:00
ColoredBlocks.Remove(ColoredBlocks.FirstOrDefault(t => t.Block == (block.Block /
SectorsPerBlock) + i));
}
break;
case NotifyCollectionChangedAction.Replace:
foreach(object item in args.OldItems)
{
2020-02-29 18:03:35 +00:00
if(!(item is ColoredBlock block))
continue;
for(ulong i = 0; i < blocksToRead; i++)
2020-02-29 18:03:35 +00:00
ColoredBlocks.Remove(ColoredBlocks.FirstOrDefault(t => t.Block == (block.Block /
SectorsPerBlock) + i));
}
foreach(object item in args.NewItems)
{
2020-02-29 18:03:35 +00:00
if(!(item is ColoredBlock block))
continue;
for(ulong i = 0; i < blocksToRead; i++)
2020-02-29 18:03:35 +00:00
ColoredBlocks.Add(new ColoredBlock((block.Block / SectorsPerBlock) + i, block.Color));
}
break;
case NotifyCollectionChangedAction.Reset:
ColoredBlocks.Clear();
2020-02-29 18:03:35 +00:00
break;
default: throw new ArgumentOutOfRangeException();
}
}
void CalculateBoundaries()
{
SectorsPerBlock = Blocks == 0 ? 0 : _sectors / Blocks;
ColoredBlocks.Clear();
2020-02-29 18:03:35 +00:00
if(SectorsPerBlock > 0)
return;
SectorsPerBlock = 1;
2020-02-29 18:03:35 +00:00
for(ulong i = Sectors; i < Blocks; i++)
ColoredBlocks.Add(new ColoredBlock(i, GridColor));
}
2020-02-29 18:03:35 +00:00
public void Clear() => ColoredSectors.Clear();
}
}