/
CustomViewRegistry.cs
executable file
·501 lines (458 loc) · 19.4 KB
/
CustomViewRegistry.cs
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System.Reflection;
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Tizen.NUI.BaseComponents;
namespace Tizen.NUI
{
/// <summary>
/// Adds this attribute to any property belonging to a view (control) you want to be scriptable from JSON.
/// </summary>
/// <remarks>
/// Example:
///
/// class MyView : public CustomView
/// {
/// [ScriptableProperty()]
/// public int MyProperty
/// {
/// get
/// {
/// return _myProperty;
/// }
/// set
/// {
/// _myProperty = value;
/// }
/// }
/// }
///
/// Internally the following occurs for property registration ( this only occurs once per Type, not per Instance):
///
/// - The controls static constructor should call ViewRegistry.Register() (only called once for the lifecycle of the app).
/// - Within Register() the code will introspect the Controls properties, looking for the ScriptableProperty() attribute.
/// - For every property with the ScriptableProperty() attribute, TypeRegistration.RegisterProperty is called.
/// - TypeRegistration.RegisterProperty calls in to DALi C++ Code Dali::CSharpTypeRegistry::RegisterProperty().
/// - DALi C++ now knows the existance of the property and will try calling SetProperty, if it finds the property in a JSON file (loaded using builder).
///
/// The DALi C# example:
///
/// class MyView : public CustomView
/// {
///
/// [ScriptableProperty()]
/// public double Hours
/// {
/// get { return seconds / 3600; }
/// set { seconds = value * 3600; }
/// }
/// }
///
/// Equivalent code in DALi C++:
/// in MyControl.h
/// class MyControl : public Control
/// {
/// struct Property
/// {
/// enum
/// {
/// HOURS = Control::CONTROL_PROPERTY_END_INDEX + 1
/// }
/// }
/// }
///
/// in MyControl-impl.cpp
///
/// DALI_TYPE_REGISTRATION_BEGIN( Toolkit::MyControl, Toolkit::Control, Create );
/// DALI_PROPERTY_REGISTRATION( Toolkit, MyControl, "Hours", INTEGER, DISABLED )
/// DALI_TYPE_REGISTRATION_END()
/// </remarks>
///
///
/// <since_tizen> 3 </since_tizen>
public class ScriptableProperty : System.Attribute
{
/// <since_tizen> 3 </since_tizen>
public readonly ScriptableType type;
/// <since_tizen> 3 </since_tizen>
public ScriptableProperty(ScriptableType type = ScriptableType.Default)
{
this.type = type;
}
/// <summary>
/// Rhe enum of ScriptableType
/// </summary>
/// <since_tizen> 3 </since_tizen>
public enum ScriptableType
{
/// <summary>
/// Read Writable, non-animatable property, event thread only.
/// </summary>
/// <since_tizen> 3 </since_tizen>
Default, // Read Writable, non-animatable property, event thread only
// Animatable // Animatable property, Currently disabled, UK
}
}
/// <summary>
/// View the Registry singleton.
/// Used for registering controls and any scriptable properties they have (see ScriptableProperty).
///
/// Internal Design from C# to C++
///
/// - Each custom C# view should have it's static constructor called before any JSON file is loaded.
/// Static constructors for a class will only run once ( they are run per control type, not per instance).
/// Example of running a static constructor:
/// System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(Spin).TypeHandle);
/// Inside the static constructor the control should register it's type with the ViewRegistry
/// For example:
///
/// static Spin()
/// {
/// ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
/// }
///
/// The control should also provide a CreateInstance function, which gets passed to the ViewRegistry.
/// // Eventually it will be called if DALi Builderfinds a Spin control in a JSON file.
/// static CustomView CreateInstance()
/// {
/// return new Spin();
/// }
///
///
///
/// The DALi C++ equivalent of this is
///
/// TypeRegistration mType( typeid(Toolkit::Spin), typeid(Toolkit::Control), CreateInstance );
///
///
///
/// </summary>
/// <since_tizen> 3 </since_tizen>
public sealed class CustomViewRegistry
{
/// <summary>
/// Lookup table to match C# types to DALi types, used for the automatic property registration.
/// </summary>
private static readonly Dictionary<string, Tizen.NUI.PropertyType> _daliPropertyTypeLookup
= new Dictionary<string, Tizen.NUI.PropertyType>
{
{ "float", PropertyType.Float },
{ "int", PropertyType.Integer },
{ "Int32", PropertyType.Integer },
{ "Boolean", PropertyType.Boolean },
{ "string", PropertyType.String },
{ "Vector2", PropertyType.Vector2 },
{ "Vector3", PropertyType.Vector3 },
{ "Vector4", PropertyType.Vector4 },
{ "Size", PropertyType.Vector2 },
{ "Position",PropertyType.Vector3 },
{ "Color", PropertyType.Vector4 },
{ "PropertyArray", PropertyType.Array },
{ "PropertyMap", PropertyType.Map },
// { "Matrix3", PropertyType.MATRIX3 }, commented out until we need to use Matrices from JSON
// { "Matrix", PropertyType.MATRIX },
};
/// <summary>
/// ViewRegistry is a singleton.
/// </summary>
private static CustomViewRegistry instance = null;
private CreateControlDelegate _createCallback;
private SetPropertyDelegate _setPropertyCallback;
private GetPropertyDelegate _getPropertyCallback;
private PropertyRangeManager _propertyRangeManager;
///<summary>
/// Maps the name of a custom view to a create instance function
/// For example, given a string "Spin", we can get a function used to create the Spin View.
///</summary>
private Dictionary<String, Func<CustomView>> _constructorMap;
private CustomViewRegistry()
{
_createCallback = new CreateControlDelegate(CreateControl);
_getPropertyCallback = new GetPropertyDelegate(GetProperty);
_setPropertyCallback = new SetPropertyDelegate(SetProperty);
_constructorMap = new Dictionary<string, Func<CustomView>>();
_propertyRangeManager = new PropertyRangeManager();
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate IntPtr CreateControlDelegate(IntPtr cPtrControlName);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate IntPtr GetPropertyDelegate(IntPtr controlPtr, IntPtr propertyName);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void SetPropertyDelegate(IntPtr controlPtr, IntPtr propertyName, IntPtr propertyValue);
/// <since_tizen> 3 </since_tizen>
public static CustomViewRegistry Instance
{
get
{
if (instance == null)
{
instance = new CustomViewRegistry();
}
return instance;
}
}
/// <summary>
/// The function which registers a view and all it's scriptable properties with DALi's type registry.
/// Means the view can be created or configured from a JSON script.
///
/// The function uses introspection to scan a views C# properties, then selects the ones with
///[ScriptableProperty] attribute to be registered.
/// Example of a Spin view registering itself:
/// static Spin()
/// {
/// ViewRegistry registers control type with DALi type registery
/// also uses introspection to find any properties that need to be registered with type registry
/// ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
/// }
///
/// </summary>
/// <since_tizen> 3 </since_tizen>
public void Register(Func<CustomView> createFunction, System.Type viewType)
{
// add the mapping between the view name and it's create function
_constructorMap.Add(viewType.ToString(), createFunction);
// Call into DALi C++ to register the control with the type registry
TypeRegistration.RegisterControl(viewType.ToString(), _createCallback);
// Cycle through each property in the class
foreach (System.Reflection.PropertyInfo propertyInfo in viewType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
{
if (propertyInfo.CanRead)
{
IEnumerable<Attribute> ie_attrs = propertyInfo.GetCustomAttributes<Attribute>();
List<Attribute> li_attrs = new List<Attribute>(ie_attrs);
System.Attribute[] attrs = li_attrs.ToArray();
foreach (System.Attribute attr in attrs)
{
// If the Scriptable attribute exists, then register it with the type registry.
if (attr is ScriptableProperty)
{
NUILog.Debug("Got a DALi JSON scriptable property = " + propertyInfo.Name + ", of type " + propertyInfo.PropertyType.Name);
// first get the attribute type, ( default, or animatable)
ScriptableProperty scriptableProp = attr as ScriptableProperty;
// we get the start property index, based on the type and it's heirachy, e.g. DateView (70,000)-> Spin (60,000) -> View (50,000)
int propertyIndex = _propertyRangeManager.GetPropertyIndex(viewType.ToString(), viewType, scriptableProp.type);
// get the enum for the property type... E.g. registering a string property returns Tizen.NUI.PropertyType.String
Tizen.NUI.PropertyType propertyType = GetDaliPropertyType(propertyInfo.PropertyType.Name);
// Example RegisterProperty("spin","maxValue", 50001, FLOAT, set, get );
// Native call to register the property
TypeRegistration.RegisterProperty(viewType.ToString(), propertyInfo.Name, propertyIndex, propertyType, _setPropertyCallback, _getPropertyCallback);
}
}
NUILog.Debug("property name = " + propertyInfo.Name);
}
}
}
/// <summary>
/// Called directly from DALi C++ type registry to create a control (view) using no marshalling.
/// </summary>
/// <returns>Pointer to the control (views) handle.</returns>
/// <param name="cPtrControlName">C pointer to the control (view) name.</param>
private static IntPtr CreateControl(IntPtr cPtrControlName)
{
string controlName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(cPtrControlName);
NUILog.Debug("Create controlled called from C++ create a " + controlName);
Func<CustomView> controlConstructor;
// find the control constructor
if (Instance._constructorMap.TryGetValue(controlName, out controlConstructor))
{
// Create the control
CustomView newControl = controlConstructor();
if (newControl != null)
{
return newControl.GetPtrfromView(); // return pointer to handle
}
else
{
return IntPtr.Zero;
}
}
else
{
throw new global::System.InvalidOperationException("C# View not registererd with ViewRegistry" + controlName);
}
}
private static IntPtr GetProperty(IntPtr controlPtr, IntPtr propertyName)
{
string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
return Instance.GetPropertyValue(controlPtr, name);
}
private static void SetProperty(IntPtr controlPtr, IntPtr propertyName, IntPtr propertyValue)
{
string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
NUILog.Debug("SetControlProperty called for:" + name);
Instance.SetPropertyValue(controlPtr, name, propertyValue);
}
private Tizen.NUI.PropertyType GetDaliPropertyType(string cSharpTypeName)
{
Tizen.NUI.PropertyType daliType;
if (_daliPropertyTypeLookup.TryGetValue(cSharpTypeName, out daliType))
{
NUILog.Debug("mapped " + cSharpTypeName + " to dAli type " + daliType);
return daliType;
}
else
{
NUILog.Debug("Failed to find a mapping between C# property" + cSharpTypeName + " and DALi type");
return PropertyType.None;
}
}
/// <summary>
/// Gets a property value from a view.
/// </summary>
private IntPtr GetPropertyValue(IntPtr refObjectPtr, string propertyName)
{
// Get the C# control that maps to the C++ control
View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
if (view != null)
{
// call the get property function
System.Object val = view.GetType().GetProperty(propertyName).GetAccessors()[0].Invoke(view, null);
PropertyValue value = PropertyValue.CreateFromObject(val);
return (IntPtr)PropertyValue.getCPtr(value);
}
else
{
return IntPtr.Zero;
}
}
/// <summary>
/// Sets a property value on a view.
/// </summary>
private void SetPropertyValue(IntPtr refObjectPtr, string propertyName, IntPtr propertyValuePtr)
{
// Get the C# control that maps to the C++ control
NUILog.Debug("SetPropertyValue refObjectPtr = {0:X}" + refObjectPtr);
PropertyValue propValue = new PropertyValue(propertyValuePtr, false);
// Get the C# control that maps to the C++ control
View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
if (view != null)
{
System.Reflection.PropertyInfo propertyInfo = view.GetType().GetProperty(propertyName);
// We know the property name, we know it's type, we just need to convert from a DALi property value to native C# type
System.Type type = propertyInfo.PropertyType;
bool ok = false;
if (type.Equals(typeof(Int32)))
{
int value = 0;
ok = propValue.Get(out value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(bool)))
{
bool value = false;
ok = propValue.Get(out value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(float)))
{
float value = 0;
ok = propValue.Get(out value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(string)))
{
string value = "";
ok = propValue.Get(out value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(Vector2)))
{
Vector2 value = new Vector2();
ok = propValue.Get(value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(Vector3)))
{
Vector3 value = new Vector3();
ok = propValue.Get(value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(Vector4)))
{
Vector4 value = new Vector4();
ok = propValue.Get(value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(Position)))
{
Position value = new Position();
ok = propValue.Get(value);
if (ok)
{
propertyInfo.SetValue(view, value);
}
}
else if (type.Equals(typeof(Size)))
{
Size value = new Size();
ok = propValue.Get(value);
if (ok)
{
propertyInfo.SetValue(view, new Size(value.Width, value.Height, value.Depth));
};
}
else if (type.Equals(typeof(Color)))
{
// Colors are stored as Vector4's in DALi
Color value = new Color();
ok = propValue.Get(value);
if (ok)
{
propertyInfo.SetValue(view, (Color)value);
};
}
else if (type.Equals(typeof(PropertyMap)))
{
PropertyMap map = new PropertyMap();
ok = propValue.Get(map);
if (ok)
{
propertyInfo.SetValue(view, map);
}
}
else if (type.Equals(typeof(PropertyArray)))
{
PropertyArray array = new PropertyArray();
ok = propValue.Get(array);
if (ok)
{
propertyInfo.SetValue(view, array);
}
}
else
{
throw new global::System.InvalidOperationException("SetPropertyValue Unimplemented type for Property Value for " + type.FullName);
}
if (!ok)
{
throw new global::System.InvalidOperationException("SetPropertyValue propValue.Get failed");
}
}
else
{
throw new global::System.InvalidOperationException("failed to find the control to write a property to: cptr = " + refObjectPtr);
}
}
}
}