Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
InheritanceDefinition.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  /// Inheritance only applies to specific keys (which can be remapped).
10  /// </summary>
11  public class InheritanceDefinition : IParameterCollectionInheritanceInternal, System.Collections.IEnumerable
12  {
13  private ParameterCollection source;
14  private Dictionary<ParameterKey, ParameterKey> keyMapping;
15  private ParameterCollection.OnUpdateValueDelegate updateValueDelegate;
16 
18  {
19  this.source = source;
20  this.keyMapping = new Dictionary<ParameterKey, ParameterKey>();
21  }
22 
23  public void Add(ParameterKey keySource, ParameterKey keyTarget)
24  {
25  keyMapping.Add(keySource, keyTarget);
26  }
27 
28  public void Add(ParameterKey key)
29  {
30  keyMapping.Add(key, key);
31  }
32 
33  ParameterKey MapKey(ParameterKey key)
34  {
35  ParameterKey mappedKey;
36  if (keyMapping.TryGetValue(key, out mappedKey))
37  return mappedKey;
38 
39  return key;
40  }
41 
42  ParameterCollection IParameterCollectionInheritanceInternal.GetParameterCollection()
43  {
44  return source;
45  }
46 
47  ParameterCollection.OnUpdateValueDelegate IParameterCollectionInheritanceInternal.GetUpdateValueDelegate(ParameterCollection.OnUpdateValueDelegate original)
48  {
49  if (updateValueDelegate == null)
50  {
51  updateValueDelegate = (source, key, value) =>
52  {
53  if (keyMapping.ContainsKey(key))
54  {
55  original(source, keyMapping[key], value);
56  }
57  };
58  }
59  return updateValueDelegate;
60  }
61 
62  int IParameterCollectionInheritanceInternal.GetInternalValueCount()
63  {
64  return keyMapping.Count;
65  }
66 
67  ParameterCollection.InternalValue IParameterCollectionInheritanceInternal.GetInternalValue(ParameterKey key)
68  {
69  return source.GetInternalValue(keyMapping[key]);
70  }
71 
72  IEnumerable<KeyValuePair<ParameterKey, ParameterCollection.InternalValue>> IParameterCollectionInheritanceInternal.GetInternalValues()
73  {
74  return source.InternalValues.Where(x => x.Value != null && keyMapping.ContainsKey(x.Key)).Select(x => new KeyValuePair<ParameterKey, ParameterCollection.InternalValue>(keyMapping[x.Key], x.Value));
75  }
76 
77  public IEnumerable<ParameterDynamicValue> DynamicValues
78  {
79  get
80  {
81  foreach (var dynamicValue in source.DynamicValues)
82  {
83  var dynamicValueCopy = dynamicValue.Clone();
84  dynamicValueCopy.Dependencies = dynamicValue.Dependencies.Select(MapKey).ToArray();
85  dynamicValueCopy.Target = MapKey(dynamicValue.Target);
86  yield return dynamicValueCopy;
87  }
88  }
89  }
90 
91  public System.Collections.IEnumerator GetEnumerator()
92  {
93  throw new System.NotImplementedException();
94  }
95  }
96 }
Key of an effect parameter.
Definition: ParameterKey.cs:15
Inheritance only applies to specific keys (which can be remapped).
void Add(ParameterKey keySource, ParameterKey keyTarget)
A container to handle a hierarchical collection of effect variables.