Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SettingsEntry.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;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Linq;
7 
8 using SiliconStudio.ActionStack;
9 using SiliconStudio.Core.IO;
10 
11 namespace SiliconStudio.Presentation.Settings
12 {
13  /// <summary>
14  /// An internal object that represent a value for a settings key into a <see cref="SettingsProfile"/>.
15  /// </summary>
16  internal abstract class SettingsEntry
17  {
18  protected readonly SettingsProfile Profile;
19  protected bool ShouldNotify;
20  private object value;
21 
22  /// <summary>
23  /// Initializes a new instance of the <see cref="SettingsEntry"/> class.
24  /// </summary>
25  /// <param name="profile">The profile this <see cref="SettingsEntry"/>belongs to.</param>
26  /// <param name="name">The name associated to this <see cref="SettingsEntry"/>.</param>
27  protected SettingsEntry(SettingsProfile profile, UFile name)
28  {
29  if (profile == null) throw new ArgumentNullException("profile");
30  if (name == null) throw new ArgumentNullException("name");
31  Profile = profile;
32  Name = name;
33  }
34 
35  /// <summary>
36  /// Gets the name of this <see cref="SettingsEntry"/>.
37  /// </summary>
38  internal UFile Name { get; private set; }
39 
40  /// <summary>
41  /// Gets or sets the value of this <see cref="SettingsEntry"/>.
42  /// </summary>
43  internal object Value { get { return value; } set { UpdateValue(value); } }
44 
45  /// <summary>
46  /// Creates a new instance of a class derived from <see cref="SettingsEntry"/> that matches the type of the given value.
47  /// </summary>
48  /// <param name="profile">The profile the <see cref="SettingsEntry"/> to create belongs to.</param>
49  /// <param name="name">The name associated to the <see cref="SettingsEntry"/> to create.</param>
50  /// <param name="value">The value to associate to the <see cref="SettingsEntry"/> to create.</param>
51  /// <returns>A new instance of a <see cref="SettingsEntry"/> class.</returns>
52  internal static SettingsEntry CreateFromValue(SettingsProfile profile, UFile name, object value)
53  {
54  if (profile == null) throw new ArgumentNullException("profile");
55  if (name == null) throw new ArgumentNullException("name");
56  if (value != null)
57  {
58  Type type = value.GetType();
59  // Check if we have a list interface...
60  if (value is IList || type.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>)))
61  {
62  var listItems = (IEnumerable)value;
63  var genericListInterface = type.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>));
64  if (genericListInterface != null)
65  {
66  var itemType = genericListInterface.GetGenericArguments()[0];
67  var result = (SettingsEntry)Activator.CreateInstance(typeof(SettingsEntryList<>).MakeGenericType(itemType), profile, name, listItems);
68  return result;
69  }
70  return new SettingsEntryList<object>(profile, name, listItems);
71  }
72  }
73 
74  return new SettingsEntryValue(profile, name, value);
75  }
76 
77  /// <summary>
78  /// Gets the value of this entry converted to a serializable type.
79  /// </summary>
80  /// <returns></returns>
81  internal abstract object GetSerializableValue();
82 
83  private void UpdateValue(object newValue)
84  {
85  var oldValue = value;
86  bool changed = !Equals(oldValue, newValue);
87  if (changed && ShouldNotify && !Profile.IsDiscarding)
88  {
89  var actionItem = new PropertyChangedActionItem("Value", this, oldValue);
90  Profile.ActionStack.Add(actionItem);
91  Profile.NotifyEntryChanged(Name);
92  }
93  value = newValue;
94  }
95  }
96 }
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
Defines a normalized file path. See UPath for details. This class cannot be inherited.
Definition: UFile.cs:13