Tanoda
Config.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9namespace Leap {
10
11 using System;
12 using System.Collections.Generic;
13 using LeapInternal;
14
20 public class Config {
21 private Connection _connection;
22 private Dictionary<UInt32, object> _transactions = new Dictionary<UInt32, object>();
23
30 public Config(Connection.Key connectionKey) {
31 _connection = Connection.GetConnection(connectionKey);
32 _connection.LeapConfigChange += handleConfigChange;
33 _connection.LeapConfigResponse += handleConfigResponse;
34 }
35 public Config(int connectionId) : this(new Connection.Key(connectionId)) { }
36
37 private void handleConfigChange(object sender, ConfigChangeEventArgs eventArgs) {
38 object actionDelegate;
39 if (_transactions.TryGetValue(eventArgs.RequestId, out actionDelegate)) {
40 Action<bool> changeAction = actionDelegate as Action<bool>;
41 changeAction(eventArgs.Succeeded);
42 _transactions.Remove(eventArgs.RequestId);
43 }
44 }
45
46 private void handleConfigResponse(object sender, SetConfigResponseEventArgs eventArgs) {
47 object actionDelegate = new object();
48 if (_transactions.TryGetValue(eventArgs.RequestId, out actionDelegate)) {
49 switch (eventArgs.DataType) {
50 case ValueType.TYPE_BOOLEAN:
51 Action<bool> boolAction = actionDelegate as Action<bool>;
52 boolAction((int)eventArgs.Value != 0);
53 break;
54 case ValueType.TYPE_FLOAT:
55 Action<float> floatAction = actionDelegate as Action<float>;
56 floatAction((float)eventArgs.Value);
57 break;
58 case ValueType.TYPE_INT32:
59 Action<Int32> intAction = actionDelegate as Action<Int32>;
60 intAction((Int32)eventArgs.Value);
61 break;
62 case ValueType.TYPE_STRING:
63 Action<string> stringAction = actionDelegate as Action<string>;
64 stringAction((string)eventArgs.Value);
65 break;
66 default:
67 break;
68 }
69 _transactions.Remove(eventArgs.RequestId);
70 }
71 }
72
82 public bool Get<T>(string key, Action<T> onResult) {
83 uint requestId = _connection.GetConfigValue(key);
84 if (requestId > 0) {
85 _transactions.Add(requestId, onResult);
86 return true;
87 }
88 return false;
89 }
90
100 public bool Set<T>(string key, T value, Action<bool> onResult) where T : IConvertible {
101 uint requestId = _connection.SetConfigValue<T>(key, value);
102
103 if (requestId > 0) {
104 _transactions.Add(requestId, onResult);
105 return true;
106 }
107 return false;
108 }
109
110 [Obsolete("Use the generic Set<T> method instead.")]
111 public ValueType Type(string key) {
112 return ValueType.TYPE_UNKNOWN;
113 }
114
115 [Obsolete("Use the generic Get<T> method instead.")]
116 public bool GetBool(string key) {
117 return false;
118 }
119
120 [Obsolete("Use the generic Set<T> method instead.")]
121 public bool SetBool(string key, bool value) {
122 return false;
123 }
124
125 [Obsolete("Use the generic Get<T> method instead.")]
126 public bool GetInt32(string key) {
127 return false;
128 }
129
130 [Obsolete("Use the generic Set<T> method instead.")]
131 public bool SetInt32(string key, int value) {
132 return false;
133 }
134
135 [Obsolete("Use the generic Get<T> method instead.")]
136 public bool GetFloat(string key) {
137 return false;
138 }
139
140 [Obsolete("Use the generic Set<T> method instead.")]
141 public bool SetFloat(string key, float value) {
142 return false;
143 }
144
145 [Obsolete("Use the generic Get<T> method instead.")]
146 public bool GetString(string key) {
147 return false;
148 }
149
150 [Obsolete("Use the generic Set<T> method instead.")]
151 public bool SetString(string key, string value) {
152 return false;
153 }
154
155 [Obsolete]
156 public bool Save() {
157 return false;
158 }
159
164 public enum ValueType {
165 TYPE_UNKNOWN = 0,
166 TYPE_BOOLEAN = 1,
167 TYPE_INT32 = 2,
168 TYPE_FLOAT = 6,
169 TYPE_STRING = 8,
170 }
171 }
172}
Dispatched when a configuration change is completed.
Definition: Events.cs:128
The Config class provides access to Leap Motion system configuration information.
Definition: Config.cs:20
bool SetString(string key, string value)
Definition: Config.cs:151
ValueType Type(string key)
Definition: Config.cs:111
bool Get< T >(string key, Action< T > onResult)
Requests a configuration value.
Definition: Config.cs:82
bool SetBool(string key, bool value)
Definition: Config.cs:121
bool GetString(string key)
Definition: Config.cs:146
Config(Connection.Key connectionKey)
Creates a new Config object for setting runtime configuration settings.
Definition: Config.cs:30
bool Save()
Definition: Config.cs:156
Config(int connectionId)
Definition: Config.cs:35
bool SetInt32(string key, int value)
Definition: Config.cs:131
bool GetBool(string key)
Definition: Config.cs:116
bool GetInt32(string key)
Definition: Config.cs:126
bool Set< T >(string key, T value, Action< bool > onResult)
Sets a configuration value.
Definition: Config.cs:100
bool SetFloat(string key, float value)
Definition: Config.cs:141
bool GetFloat(string key)
Definition: Config.cs:136
ValueType
Enumerates the possible data types for configuration values.
Definition: Config.cs:164
uint GetConfigValue(string config_key)
Definition: Connection.cs:751
static Connection GetConnection(int connectionId=0)
Definition: Connection.cs:30
EventHandler< ConfigChangeEventArgs > LeapConfigChange
Definition: Connection.cs:108
EventHandler< SetConfigResponseEventArgs > LeapConfigResponse
Definition: Connection.cs:107