Jypeli  5
The simple game programming library
PropertySet.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 
5 namespace Jypeli.LevelEditor
6 {
7  [Save]
8  public class PropertySet
9  {
10  [Save] public List<Property> Items { get; private set; }
11 
12  public PropertySet()
13  {
14  Items = new List<Property>();
15  }
16 
17  private void Add( string name, object value )
18  {
19  Items.Add( new Property( name, value ) );
20  }
21 
22  public void Add( PropertySet propSet )
23  {
24  for ( int i = 0; i < propSet.Items.Count; i++ )
25  {
26  SetValue( propSet.Items[i].Name, propSet.Items[i].Value );
27  }
28  }
29 
30  public void Remove( string propName )
31  {
32  Remove( item => item.Name == propName );
33  }
34 
35  private void Remove( Predicate<Property> pred )
36  {
37  for ( int i = Items.Count - 1; i >= 0; i-- )
38  {
39  if ( pred( Items[i] ) )
40  Items.RemoveAt( i );
41  }
42  }
43 
44  public int IndexOf( string propName )
45  {
46  for ( int i = 0; i < Items.Count; i++ )
47  {
48  if ( Items[i].Name == propName )
49  return i;
50  }
51 
52  return -1;
53  }
54 
55  public bool Contains( string propName )
56  {
57  return IndexOf( propName ) >= 0;
58  }
59 
60  public object GetValue( string propName )
61  {
62  int propIndex = IndexOf( propName );
63  if ( propIndex < 0 ) throw new KeyNotFoundException();
64  return Items[propIndex].Value;
65  }
66 
67  public T GetValue<T>( string propName )
68  {
69  return (T)GetValue( propName );
70  }
71 
72  public object TryGetValue( string propName )
73  {
74  try
75  {
76  return GetValue( propName );
77  }
78  catch ( KeyNotFoundException )
79  {
80  return null;
81  }
82  }
83 
84  public void SetValue( string propName, object propValue )
85  {
86  int propIndex = IndexOf( propName );
87 
88  if ( propIndex >= 0 )
89  {
90  Property property = Items[propIndex];
91  property.Value = propValue;
92  Items[propIndex] = property;
93  }
94  else
95  Add( propName, propValue );
96  }
97 
98  public void Apply( ref GameObject obj )
99  {
100  Type type = obj.GetType();
101 
102  for ( int i = 0; i < Items.Count; i++ )
103  {
104  if ( Items[i].Name == "Type" )
105  continue;
106 
107  BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty;
108  PropertyInfo prop = type.GetProperty( Items[i].Name, flags );
109 
110  if ( prop != null )
111  prop.SetValue( obj, Items[i].Value, null );
112  }
113  }
114 
115  public static PropertySet Merge( params PropertySet[] sets )
116  {
117  PropertySet result = new PropertySet();
118 
119  for ( int i = 0; i < sets.Length; i++ )
120  {
121  result.Add( sets[i] );
122  }
123 
124  return result;
125  }
126  }
127 
128  [Save]
129  public struct Property
130  {
131  [Save] public string Name;
132  [Save] public object Value;
133 
134  public Property( string name, object value )
135  {
136  Name = name;
137  Value = value;
138  }
139  }
140 }
object TryGetValue(string propName)
Definition: PropertySet.cs:72
int IndexOf(string propName)
Definition: PropertySet.cs:44
object GetValue(string propName)
Definition: PropertySet.cs:60
static PropertySet Merge(params PropertySet[] sets)
Definition: PropertySet.cs:115
void Remove(string propName)
Definition: PropertySet.cs:30
bool Contains(string propName)
Definition: PropertySet.cs:55
void SetValue(string propName, object propValue)
Definition: PropertySet.cs:84
void Add(PropertySet propSet)
Definition: PropertySet.cs:22
void Apply(ref GameObject obj)
Definition: PropertySet.cs:98
Property(string name, object value)
Definition: PropertySet.cs:134
Pelialueella liikkuva olio. Käytä fysiikkapeleissä PhysicsObject-olioita.
Definition: __GameObject.cs:54