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))
202 NUILog.Debug("mapped " + cSharpTypeName + " to dAli type " + daliType);
208 NUILog.Debug("Failed to find a mapping between C# property" + cSharpTypeName + " and DALi type");
210 return PropertyType.None;
215 /// Called directly from DALi C++ type registry to create a control (view) using no marshalling.
217 /// <returns>Pointer to the control (views) handle.</returns>
218 /// <param name="cPtrControlName">C pointer to the control (view) name.</param>
219 private static IntPtr CreateControl(IntPtr cPtrControlName)
221 string controlName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(cPtrControlName);
223 NUILog.Debug("Create controlled called from C++ create a " + controlName);
225 Func<CustomView> controlConstructor;
227 // find the control constructor
228 if (Instance._constructorMap.TryGetValue(controlName, out controlConstructor))
230 // Create the control
231 CustomView newControl = controlConstructor();
232 return newControl.GetPtrfromView(); // return pointer to handle
236 throw new global::System.InvalidOperationException("C# View not registererd with ViewRegistry" + controlName);
240 private static IntPtr GetProperty(IntPtr controlPtr, IntPtr propertyName)
242 string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
243 return Instance.GetPropertyValue(controlPtr, name);
246 private static void SetProperty(IntPtr controlPtr, IntPtr propertyName, IntPtr propertyValue)
248 string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
250 NUILog.Debug("SetControlProperty called for:" + name);
252 Instance.SetPropertyValue(controlPtr, name, propertyValue);
255 /// <since_tizen> 3 </since_tizen>
256 public static CustomViewRegistry Instance
260 if (instance == null)
262 instance = new CustomViewRegistry();
269 /// The function which registers a view and all it's scriptable properties with DALi's type registry.
270 /// Means the view can be created or configured from a JSON script.
272 /// The function uses introspection to scan a views C# properties, then selects the ones with
273 ///[ScriptableProperty] attribute to be registered.
274 /// Example of a Spin view registering itself:
277 /// ViewRegistry registers control type with DALi type registery
278 /// also uses introspection to find any properties that need to be registered with type registry
279 /// ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
283 /// <since_tizen> 3 </since_tizen>
284 public void Register(Func<CustomView> createFunction, System.Type viewType)
286 // add the mapping between the view name and it's create function
287 _constructorMap.Add(viewType.ToString(), createFunction);
289 // Call into DALi C++ to register the control with the type registry
290 TypeRegistration.RegisterControl(viewType.ToString(), _createCallback);
292 // Cycle through each property in the class
293 foreach (System.Reflection.PropertyInfo propertyInfo in viewType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
295 if (propertyInfo.CanRead)
297 IEnumerable<Attribute> ie_attrs = propertyInfo.GetCustomAttributes<Attribute>();
298 List<Attribute> li_attrs = new List<Attribute>(ie_attrs);
299 System.Attribute[] attrs = li_attrs.ToArray();
301 foreach (System.Attribute attr in attrs)
303 // If the Scriptable attribute exists, then register it with the type registry.
304 if (attr is ScriptableProperty)
306 NUILog.Debug("Got a DALi JSON scriptable property = " + propertyInfo.Name + ", of type " + propertyInfo.PropertyType.Name);
308 // first get the attribute type, ( default, or animatable)
309 ScriptableProperty scriptableProp = attr as ScriptableProperty;
311 // 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)
312 int propertyIndex = _propertyRangeManager.GetPropertyIndex(viewType.ToString(), viewType, scriptableProp.type);
314 // get the enum for the property type... E.g. registering a string property returns Tizen.NUI.PropertyType.String
315 Tizen.NUI.PropertyType propertyType = GetDaliPropertyType(propertyInfo.PropertyType.Name);
317 // Example RegisterProperty("spin","maxValue", 50001, FLOAT, set, get );
318 // Native call to register the property
319 TypeRegistration.RegisterProperty(viewType.ToString(), propertyInfo.Name, propertyIndex, propertyType, _setPropertyCallback, _getPropertyCallback);
322 NUILog.Debug("property name = " + propertyInfo.Name);
328 /// Gets a property value from a view.
330 private IntPtr GetPropertyValue(IntPtr refObjectPtr, string propertyName)
332 // Get the C# control that maps to the C++ control
333 View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
336 // call the get property function
337 System.Object val = view.GetType().GetProperty(propertyName).GetAccessors()[0].Invoke(view, null);
339 PropertyValue value = PropertyValue.CreateFromObject(val);
341 return (IntPtr)PropertyValue.getCPtr(value);
350 /// Sets a property value on a view.
352 private void SetPropertyValue(IntPtr refObjectPtr, string propertyName, IntPtr propertyValuePtr)
354 // Get the C# control that maps to the C++ control
355 NUILog.Debug("SetPropertyValue refObjectPtr = {0:X}" + refObjectPtr);
357 PropertyValue propValue = new PropertyValue(propertyValuePtr, false);
359 // Get the C# control that maps to the C++ control
360 View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
363 System.Reflection.PropertyInfo propertyInfo = view.GetType().GetProperty(propertyName);
364 // We know the property name, we know it's type, we just need to convert from a DALi property value to native C# type
365 System.Type type = propertyInfo.PropertyType;
368 if (type.Equals(typeof(Int32)))
371 ok = propValue.Get(out value);
374 propertyInfo.SetValue(view, value);
377 else if (type.Equals(typeof(bool)))
380 ok = propValue.Get(out value);
383 propertyInfo.SetValue(view, value);
386 else if (type.Equals(typeof(float)))
389 ok = propValue.Get(out value);
392 propertyInfo.SetValue(view, value);
395 else if (type.Equals(typeof(string)))
398 ok = propValue.Get(out value);
401 propertyInfo.SetValue(view, value);
404 else if (type.Equals(typeof(Vector2)))
406 Vector2 value = new Vector2();
407 ok = propValue.Get(value);
410 propertyInfo.SetValue(view, value);
413 else if (type.Equals(typeof(Vector3)))
415 Vector3 value = new Vector3();
416 ok = propValue.Get(value);
419 propertyInfo.SetValue(view, value);
422 else if (type.Equals(typeof(Vector4)))
424 Vector4 value = new Vector4();
425 ok = propValue.Get(value);
429 propertyInfo.SetValue(view, value);
432 else if (type.Equals(typeof(Position)))
434 Position value = new Position();
435 ok = propValue.Get(value);
438 propertyInfo.SetValue(view, value);
441 else if (type.Equals(typeof(Size)))
443 Size value = new Size();
444 ok = propValue.Get(value);
447 propertyInfo.SetValue(view, new Size(value.Width, value.Height, value.Depth));
450 else if (type.Equals(typeof(Color)))
452 // Colors are stored as Vector4's in DALi
453 Color value = new Color();
454 ok = propValue.Get(value);
457 propertyInfo.SetValue(view, (Color)value);
460 else if (type.Equals(typeof(PropertyMap)))
462 PropertyMap map = new PropertyMap();
463 ok = propValue.Get(map);
466 propertyInfo.SetValue( view, map );
469 else if (type.Equals(typeof(PropertyArray)))
471 PropertyArray array = new PropertyArray();
472 ok = propValue.Get(array);
475 propertyInfo.SetValue( view, array );
480 throw new global::System.InvalidOperationException("SetPropertyValue Unimplemented type for Property Value for " + type.FullName );
484 throw new global::System.InvalidOperationException("SetPropertyValue propValue.Get failed");
489 throw new global::System.InvalidOperationException("failed to find the control to write a property to: cptr = " + refObjectPtr);