-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
MaterialPassCollection.cs
55 lines (46 loc) · 1.67 KB
/
MaterialPassCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.ObjectModel;
using Stride.Core.Collections;
using Stride.Core.Serialization;
using Stride.Core.Serialization.Serializers;
namespace Stride.Rendering
{
/// <summary>
/// A collection of <see cref="MaterialPass"/>.
/// </summary>
[DataSerializer(typeof(ListAllSerializer<MaterialPassCollection, MaterialPass>))]
public sealed class MaterialPassCollection : FastCollection<MaterialPass>
{
private readonly Material material;
internal MaterialPassCollection(Material material)
{
this.material = material;
}
/// <inheritdoc/>
protected override void InsertItem(int index, MaterialPass item)
{
if (item.Material != null)
throw new InvalidOperationException($"A {nameof(MaterialPass)} can only belong to a single {nameof(Material)}");
base.InsertItem(index, item);
item.Material = material;
}
/// <inheritdoc/>
protected override void RemoveItem(int index)
{
this[index].Material = null;
base.RemoveItem(index);
}
/// <inheritdoc/>
protected override void SetItem(int index, MaterialPass item)
{
// Note: Changing CollectionChanged is not thread-safe
var oldItem = this[index];
if (oldItem != null)
oldItem.Material = null;
if (item.Material != null)
throw new InvalidOperationException($"A {nameof(MaterialPass)} can only belong to a single {nameof(Material)}");
base.SetItem(index, item);
item.Material = material;
}
}
}