Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
InheritanceIndexer.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Collections.Generic;
4 using System.Linq;
5 
6 namespace SiliconStudio.Paradox.Effects
7 {
8  /// <summary>
9  /// Applies IndexedOf(index) on each key.
10  /// </summary>
11  public class InheritanceIndexer : IParameterCollectionInheritanceInternal
12  {
13  private ParameterCollection source;
14  private int keyIndex;
15  private Dictionary<ParameterKey, ParameterKey> keyMapping = new Dictionary<ParameterKey, ParameterKey>();
16  private ParameterCollection.OnUpdateValueDelegate updateValueDelegate;
17 
18  public InheritanceIndexer(ParameterCollection source, int keyIndex)
19  {
20  this.source = source;
21  this.keyIndex = keyIndex;
22  }
23 
24  ParameterCollection IParameterCollectionInheritanceInternal.GetParameterCollection()
25  {
26  return source;
27  }
28 
29  ParameterKey MapKey(ParameterKey key, bool createIfMissing = true)
30  {
31  ParameterKey mappedKey;
32  bool keyFound = keyMapping.TryGetValue(key, out mappedKey);
33 
34  if (keyFound)
35  return mappedKey;
36 
37  if (!createIfMissing)
38  return key;
39 
40  keyMapping.Add(key, mappedKey = key.AppendKey(keyIndex));
41  return mappedKey;
42  }
43 
44  ParameterCollection.OnUpdateValueDelegate IParameterCollectionInheritanceInternal.GetUpdateValueDelegate(ParameterCollection.OnUpdateValueDelegate original)
45  {
46  if (updateValueDelegate == null)
47  {
48  updateValueDelegate = (source, key, value) =>
49  {
50  original(source, MapKey(key), value);
51  };
52  }
53  return updateValueDelegate;
54  }
55 
56  int IParameterCollectionInheritanceInternal.GetInternalValueCount()
57  {
58  return ((IParameterCollectionInheritanceInternal)source).GetInternalValueCount();
59  }
60 
61 
62  ParameterCollection.InternalValue IParameterCollectionInheritanceInternal.GetInternalValue(ParameterKey key)
63  {
64  return source.GetInternalValue(MapKey(key));
65  }
66 
67  IEnumerable<KeyValuePair<ParameterKey, ParameterCollection.InternalValue>> IParameterCollectionInheritanceInternal.GetInternalValues()
68  {
69  return source.InternalValues.Where(x => x.Value != null).Select(x => new KeyValuePair<ParameterKey, ParameterCollection.InternalValue>(MapKey(x.Key), x.Value));
70  }
71 
72  public IEnumerable<ParameterDynamicValue> DynamicValues
73  {
74  get
75  {
76  foreach (var dynamicValue in source.DynamicValues)
77  {
78  var dynamicValueCopy = dynamicValue.Clone();
79 
80  // Remap keys (only if existing)
81  dynamicValueCopy.Dependencies = dynamicValue.Dependencies.Select(x => MapKey(x, false)).ToArray();
82  dynamicValueCopy.Target = MapKey(dynamicValue.Target);
83 
84  yield return dynamicValueCopy;
85  }
86  }
87  }
88  }
89 }
Applies IndexedOf(index) on each key.
InheritanceIndexer(ParameterCollection source, int keyIndex)
A container to handle a hierarchical collection of effect variables.