2 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Collections.Generic;
6 using Tizen.NUI.BaseComponents;
11 /// Adds this attribute to any property belonging to a view (control) you want to be scriptable from JSON.
16 /// class MyView : public CustomView
18 /// [ScriptableProperty()]
19 /// public int MyProperty
23 /// return _myProperty;
27 /// _myProperty = value;
32 /// Internally the following occurs for property registration ( this only occurs once per Type, not per Instance):
34 /// - The controls static constructor should call ViewRegistry.Register() (only called once for the lifecycle of the app).
35 /// - Within Register() the code will introspect the Controls properties, looking for the ScriptableProperty() attribute.
36 /// - For every property with the ScriptableProperty() attribute, TypeRegistration.RegisterProperty is called.
37 /// - TypeRegistration.RegisterProperty calls in to DALi C++ Code Dali::CSharpTypeRegistry::RegisterProperty().
38 /// - 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).
40 /// The DALi C# example:
42 /// class MyView : public CustomView
45 /// [ScriptableProperty()]
46 /// public double Hours
48 /// get { return seconds / 3600; }
49 /// set { seconds = value * 3600; }
53 /// Equivalent code in DALi C++:
55 /// class MyControl : public Control
61 /// HOURS = Control::CONTROL_PROPERTY_END_INDEX + 1
66 /// in MyControl-impl.cpp
68 /// DALI_TYPE_REGISTRATION_BEGIN( Toolkit::MyControl, Toolkit::Control, Create );
69 /// DALI_PROPERTY_REGISTRATION( Toolkit, MyControl, "Hours", INTEGER, DISABLED )
70 /// DALI_TYPE_REGISTRATION_END()
74 /// <since_tizen> 3 </since_tizen>
75 public class ScriptableProperty : System.Attribute
78 /// Rhe enum of ScriptableType
80 /// <since_tizen> 3 </since_tizen>
81 public enum ScriptableType
84 /// Read Writable, non-animatable property, event thread only.
86 /// <since_tizen> 3 </since_tizen>
87 Default, // Read Writable, non-animatable property, event thread only
88 // Animatable // Animatable property, Currently disabled, UK
90 /// <since_tizen> 3 </since_tizen>
91 public readonly ScriptableType type;
94 /// <since_tizen> 3 </since_tizen>
95 public ScriptableProperty(ScriptableType type = ScriptableType.Default)
102 /// View the Registry singleton.
103 /// Used for registering controls and any scriptable properties they have (see ScriptableProperty).
105 /// Internal Design from C# to C++
107 /// - Each custom C# view should have it's static constructor called before any JSON file is loaded.
108 /// Static constructors for a class will only run once ( they are run per control type, not per instance).
109 /// Example of running a static constructor:
110 /// System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(Spin).TypeHandle);
111 /// Inside the static constructor the control should register it's type with the ViewRegistry
116 /// ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
119 /// The control should also provide a CreateInstance function, which gets passed to the ViewRegistry.
120 /// // Eventually it will be called if DALi Builderfinds a Spin control in a JSON file.
121 /// static CustomView CreateInstance()
123 /// return new Spin();
128 /// The DALi C++ equivalent of this is
130 /// TypeRegistration mType( typeid(Toolkit::Spin), typeid(Toolkit::Control), CreateInstance );
135 /// <since_tizen> 3 </since_tizen>
136 public sealed class CustomViewRegistry
139 /// ViewRegistry is a singleton.
141 private static CustomViewRegistry instance = null;
143 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
144 private delegate IntPtr CreateControlDelegate(IntPtr cPtrControlName);
146 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
147 private delegate IntPtr GetPropertyDelegate(IntPtr controlPtr, IntPtr propertyName);
149 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
150 private delegate void SetPropertyDelegate(IntPtr controlPtr, IntPtr propertyName, IntPtr propertyValue);
152 private CreateControlDelegate _createCallback;
153 private SetPropertyDelegate _setPropertyCallback;
154 private GetPropertyDelegate _getPropertyCallback;
155 private PropertyRangeManager _propertyRangeManager;
158 /// Maps the name of a custom view to a create instance function
159 /// For example, given a string "Spin", we can get a function used to create the Spin View.
161 private Dictionary<String, Func<CustomView>> _constructorMap;
164 /// Lookup table to match C# types to DALi types, used for the automatic property registration.
166 private static readonly Dictionary<string, Tizen.NUI.PropertyType> _daliPropertyTypeLookup
167 = new Dictionary<string, Tizen.NUI.PropertyType>
169 { "float", PropertyType.Float },
170 { "int", PropertyType.Integer },
171 { "Int32", PropertyType.Integer },
172 { "Boolean", PropertyType.Boolean },
173 { "string", PropertyType.String },
174 { "Vector2", PropertyType.Vector2 },
175 { "Vector3", PropertyType.Vector3 },
176 { "Vector4", PropertyType.Vector4 },
177 { "Size", PropertyType.Vector2 },
178 { "Position",PropertyType.Vector3 },
179 { "Color", PropertyType.Vector4 },
180 { "PropertyArray", PropertyType.Array },
181 { "PropertyMap", PropertyType.Map },
182 // { "Matrix3", PropertyType.MATRIX3 }, commented out until we need to use Matrices from JSON
183 // { "Matrix", PropertyType.MATRIX },
187 private CustomViewRegistry()
189 _createCallback = new CreateControlDelegate(CreateControl);
190 _getPropertyCallback = new GetPropertyDelegate(GetProperty);
191 _setPropertyCallback = new SetPropertyDelegate(SetProperty);
193 _constructorMap = new Dictionary<string, Func<CustomView>>();
194 _propertyRangeManager = new PropertyRangeManager();
197 private Tizen.NUI.PropertyType GetDaliPropertyType(string cSharpTypeName)
199 Tizen.NUI.PropertyType daliType;
200 if (_daliPropertyTypeLookup.TryGetValue(cSharpTypeName, out daliType))
203 Tizen.Log.Debug("NUI", "mapped " + cSharpTypeName + " to dAli type " + daliType);
210 Tizen.Log.Debug("NUI", "Failed to find a mapping between C# property" + cSharpTypeName + " and DALi type");
212 return PropertyType.None;
217 /// Called directly from DALi C++ type registry to create a control (view) using no marshalling.
219 /// <returns>Pointer to the control (views) handle.</returns>
220 /// <param name="cPtrControlName">C pointer to the control (view) name.</param>
221 private static IntPtr CreateControl(IntPtr cPtrControlName)
223 string controlName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(cPtrControlName);
225 Tizen.Log.Debug("NUI", "Create controlled called from C++ create a " + controlName);
227 Func<CustomView> controlConstructor;
229 // find the control constructor
230 if (Instance._constructorMap.TryGetValue(controlName, out controlConstructor))
232 // Create the control
233 CustomView newControl = controlConstructor();
234 return newControl.GetPtrfromView(); // return pointer to handle
238 throw new global::System.InvalidOperationException("C# View not registererd with ViewRegistry" + controlName);
242 private static IntPtr GetProperty(IntPtr controlPtr, IntPtr propertyName)
244 string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
245 return Instance.GetPropertyValue(controlPtr, name);
248 private static void SetProperty(IntPtr controlPtr, IntPtr propertyName, IntPtr propertyValue)
250 string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
252 Tizen.Log.Debug("NUI", "SetControlProperty called for:" + name);
254 Instance.SetPropertyValue(controlPtr, name, propertyValue);
258 /// <since_tizen> 3 </since_tizen>
259 public static CustomViewRegistry Instance
263 if (instance == null)
265 instance = new CustomViewRegistry();
272 /// The function which registers a view and all it's scriptable properties with DALi's type registry.
273 /// Means the view can be created or configured from a JSON script.
275 /// The function uses introspection to scan a views C# properties, then selects the ones with
276 ///[ScriptableProperty] attribute to be registered.
277 /// Example of a Spin view registering itself:
280 /// ViewRegistry registers control type with DALi type registery
281 /// also uses introspection to find any properties that need to be registered with type registry
282 /// ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
286 /// <since_tizen> 3 </since_tizen>
287 public void Register(Func<CustomView> createFunction, System.Type viewType)
289 // add the mapping between the view name and it's create function
290 _constructorMap.Add(viewType.ToString(), createFunction);
292 // Call into DALi C++ to register the control with the type registry
293 TypeRegistration.RegisterControl(viewType.ToString(), _createCallback);
295 // Cycle through each property in the class
296 foreach (System.Reflection.PropertyInfo propertyInfo in viewType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
298 if (propertyInfo.CanRead)
300 IEnumerable<Attribute> ie_attrs = propertyInfo.GetCustomAttributes<Attribute>();
301 List<Attribute> li_attrs = new List<Attribute>(ie_attrs);
302 System.Attribute[] attrs = li_attrs.ToArray();
304 foreach (System.Attribute attr in attrs)
306 // If the Scriptable attribute exists, then register it with the type registry.
307 if (attr is ScriptableProperty)
310 Tizen.Log.Debug("NUI", "Got a DALi JSON scriptable property = " + propertyInfo.Name + ", of type " + propertyInfo.PropertyType.Name);
312 // first get the attribute type, ( default, or animatable)
313 ScriptableProperty scriptableProp = attr as ScriptableProperty;
315 // 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)
316 int propertyIndex = _propertyRangeManager.GetPropertyIndex(viewType.ToString(), viewType, scriptableProp.type);
318 // get the enum for the property type... E.g. registering a string property returns Tizen.NUI.PropertyType.String
319 Tizen.NUI.PropertyType propertyType = GetDaliPropertyType(propertyInfo.PropertyType.Name);
321 // Example RegisterProperty("spin","maxValue", 50001, FLOAT, set, get );
322 // Native call to register the property
323 TypeRegistration.RegisterProperty(viewType.ToString(), propertyInfo.Name, propertyIndex, propertyType, _setPropertyCallback, _getPropertyCallback);
327 Tizen.Log.Debug("NUI", "property name = " + propertyInfo.Name);
334 /// Gets a property value from a view.
336 private IntPtr GetPropertyValue(IntPtr refObjectPtr, string propertyName)
338 // Get the C# control that maps to the C++ control
339 View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
342 // call the get property function
343 System.Object val = view.GetType().GetProperty(propertyName).GetAccessors()[0].Invoke(view, null);
345 PropertyValue value = PropertyValue.CreateFromObject(val);
347 return (IntPtr)PropertyValue.getCPtr(value);
356 /// Sets a property value on a view.
358 private void SetPropertyValue(IntPtr refObjectPtr, string propertyName, IntPtr propertyValuePtr)
360 // Get the C# control that maps to the C++ control
362 Tizen.Log.Debug("NUI", "SetPropertyValue refObjectPtr = {0:X}" + refObjectPtr);
364 PropertyValue propValue = new PropertyValue(propertyValuePtr, false);
366 // Get the C# control that maps to the C++ control
367 View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
370 System.Reflection.PropertyInfo propertyInfo = view.GetType().GetProperty(propertyName);
371 // We know the property name, we know it's type, we just need to convert from a DALi property value to native C# type
372 System.Type type = propertyInfo.PropertyType;
375 if (type.Equals(typeof(Int32)))
378 ok = propValue.Get(out value);
381 propertyInfo.SetValue(view, value);
384 else if (type.Equals(typeof(bool)))
387 ok = propValue.Get(out value);
390 propertyInfo.SetValue(view, value);
393 else if (type.Equals(typeof(float)))
396 ok = propValue.Get(out value);
399 propertyInfo.SetValue(view, value);
402 else if (type.Equals(typeof(string)))
405 ok = propValue.Get(out value);
408 propertyInfo.SetValue(view, value);
411 else if (type.Equals(typeof(Vector2)))
413 Vector2 value = new Vector2();
414 ok = propValue.Get(value);
417 propertyInfo.SetValue(view, value);
420 else if (type.Equals(typeof(Vector3)))
422 Vector3 value = new Vector3();
423 ok = propValue.Get(value);
426 propertyInfo.SetValue(view, value);
429 else if (type.Equals(typeof(Vector4)))
431 Vector4 value = new Vector4();
432 ok = propValue.Get(value);
436 propertyInfo.SetValue(view, value);
439 else if (type.Equals(typeof(Position)))
441 Position value = new Position();
442 ok = propValue.Get(value);
445 propertyInfo.SetValue(view, value);
448 else if (type.Equals(typeof(Size)))
450 Size value = new Size();
451 ok = propValue.Get(value);
454 propertyInfo.SetValue(view, new Size(value.Width, value.Height, value.Depth));
457 else if (type.Equals(typeof(Color)))
459 // Colors are stored as Vector4's in DALi
460 Color value = new Color();
461 ok = propValue.Get(value);
464 propertyInfo.SetValue(view, (Color)value);
467 else if (type.Equals(typeof(PropertyMap)))
469 PropertyMap map = new PropertyMap();
470 ok = propValue.Get(map);
473 propertyInfo.SetValue( view, map );
476 else if (type.Equals(typeof(PropertyArray)))
478 PropertyArray array = new PropertyArray();
479 ok = propValue.Get(array);
482 propertyInfo.SetValue( view, array );
487 throw new global::System.InvalidOperationException("SetPropertyValue Unimplemented type for Property Value for " + type.FullName );
491 throw new global::System.InvalidOperationException("SetPropertyValue propValue.Get failed");
496 throw new global::System.InvalidOperationException("failed to find the control to write a property to: cptr = " + refObjectPtr);