-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreferenceTerm.cs
More file actions
180 lines (154 loc) · 5.44 KB
/
Copy pathPreferenceTerm.cs
File metadata and controls
180 lines (154 loc) · 5.44 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
namespace OpenAPE
{
public class TypeSwitch
{
Dictionary<Type, Action<object>> matches = new Dictionary<Type, Action<object>>();
public TypeSwitch Case<T>(Action<T> action)
{
matches.Add(typeof(T), (x) => action((T) x));
return this;
}
public void Switch(object x)
{
matches[x.GetType()](x);
}
}
/// <summary>
/// The PreferenceTerm class.
/// Contains a representation of a single preference term.
/// </summary>
[Serializable]
public class PreferenceTerm
{
/// <summary>
/// The base URI for common terms.
/// </summary>
/// <remarks>
/// This is used to simplify getting a specific preference.
/// </remarks>
private const string CommonTermBaseUri = "http://registry.gpii.eu/common/";
/// <summary>
/// The base URI for application terms.
/// </summary>
/// <remarks>
/// This is used to simplify getting a specific preference.
/// </remarks>
private const string ApplicationTermBaseUri = "http://registry.gpii.eu/sh2018ar/";
/// <summary>
/// Create a new preference term from serialization.
/// </summary>
/// <param name="key">The key that is used.</param>
/// <param name="value">The value that is used.</param>
/// <param name="uri">The uri that is used.</param>
[JsonConstructor]
private PreferenceTerm(string key, string value, string uri)
{
Key = key;
Value = value;
Uri = uri;
}
/// <summary>
/// Create a new preference term with the given key and value.
/// </summary>
/// <param name="key">The key that is used.</param>
/// <param name="value">The value that is used.</param>
public PreferenceTerm(string key, string value)
{
if (key.Contains(CommonTermBaseUri))
{
Key = key.Replace(CommonTermBaseUri, "");
Uri = CommonTermBaseUri;
}
else if (key.Contains(ApplicationTermBaseUri))
{
Key = key.Replace(ApplicationTermBaseUri, "");
Uri = ApplicationTermBaseUri;
}
else
{
Debug.Log("Unknown preference term URI encountered in key: " + key);
}
Value = value;
}
/// <summary>
/// The key of this preference term.
/// </summary>
public string Key { get; }
/// <summary>
/// The Uri of this preference term.
/// </summary>
/// <remark>
/// E.g. "http://registry.gpii.eu/common/"
/// </remark>
public string Uri { get; }
/// <summary>
/// The value of this preference term.
/// </summary>
public string Value { set; get; }
/// <summary>
/// The Type of this preference term.
/// </summary>
/// <returns>A Type, i.e. bool.</returns>
public Type GetTypeOfValue()
{
switch (Key)
{
case "grayScale":
return typeof(bool);
case "fontSize":
return typeof(int);
case "volumeTTS":
return typeof(float);
case "iconLocation":
return typeof(CircleButtonGroupManager.ViewType);
}
return typeof(string);
}
/// <summary>
/// The value of this preference term as a typed instance.
/// </summary>
/// <typeparam name="T">The expected type.</typeparam>
/// <remarks>You might want to use GetTypeOfValue() if you are not sure what type to expect.</remarks>
/// <returns>The value converted to T.</returns>
public T GetValueTyped<T>()
{
if (typeof(T) == typeof(bool))
{
return (T) Convert.ChangeType(bool.Parse(Value), typeof(T));
}
if (typeof(T) == typeof(int))
{
return (T) Convert.ChangeType(int.Parse(Value), typeof(T));
}
if (typeof(T) == typeof(float))
{
return (T) Convert.ChangeType(float.Parse(Value), typeof(T));
}
if (typeof(T) == typeof(CircleButtonGroupManager.ViewType))
{
switch (Value)
{
case "NORMAL":
return (T) Convert.ChangeType(CircleButtonGroupManager.ViewType.NORMAL, typeof(T));
case "RIGHT":
return (T) Convert.ChangeType(CircleButtonGroupManager.ViewType.RIGHT, typeof(T));
case "TOPRIGHT":
return (T) Convert.ChangeType(CircleButtonGroupManager.ViewType.TOPRIGHT, typeof(T));
}
}
return (T) Convert.ChangeType(Value, typeof(T));
}
/// <summary>
/// Returns a string representation of this preference term.
/// </summary>
/// <returns>A printable string.</returns>
public override string ToString()
{
return "{\"" + Uri + Key + "\" : \"" + Value + "}";
}
}
}