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 /// <since_tizen> 3 </since_tizen>
79 public readonly ScriptableType type;
81 /// <since_tizen> 3 </since_tizen>
82 public ScriptableProperty(ScriptableType type = ScriptableType.Default)
88 /// Rhe enum of ScriptableType
90 /// <since_tizen> 3 </since_tizen>
91 public enum ScriptableType
94 /// Read Writable, non-animatable property, event thread only.
96 /// <since_tizen> 3 </since_tizen>
97 Default, // Read Writable, non-animatable property, event thread only
98 // Animatable // Animatable property, Currently disabled, UK
103 /// View the Registry singleton.
104 /// Used for registering controls and any scriptable properties they have (see ScriptableProperty).
106 /// Internal Design from C# to C++
108 /// - Each custom C# view should have it's static constructor called before any JSON file is loaded.
109 /// Static constructors for a class will only run once ( they are run per control type, not per instance).
110 /// Example of running a static constructor:
111 /// System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (typeof(Spin).TypeHandle);
112 /// Inside the static constructor the control should register it's type with the ViewRegistry
117 /// ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
120 /// The control should also provide a CreateInstance function, which gets passed to the ViewRegistry.
121 /// // Eventually it will be called if DALi Builderfinds a Spin control in a JSON file.
122 /// static CustomView CreateInstance()
124 /// return new Spin();
129 /// The DALi C++ equivalent of this is
131 /// TypeRegistration mType( typeid(Toolkit::Spin), typeid(Toolkit::Control), CreateInstance );
136 /// <since_tizen> 3 </since_tizen>
137 public sealed class CustomViewRegistry
141 /// Lookup table to match C# types to DALi types, used for the automatic property registration.
143 private static readonly Dictionary<string, Tizen.NUI.PropertyType> _daliPropertyTypeLookup
144 = new Dictionary<string, Tizen.NUI.PropertyType>
146 { "float", PropertyType.Float },
147 { "int", PropertyType.Integer },
148 { "Int32", PropertyType.Integer },
149 { "Boolean", PropertyType.Boolean },
150 { "string", PropertyType.String },
151 { "Vector2", PropertyType.Vector2 },
152 { "Vector3", PropertyType.Vector3 },
153 { "Vector4", PropertyType.Vector4 },
154 { "Size", PropertyType.Vector2 },
155 { "Position",PropertyType.Vector3 },
156 { "Color", PropertyType.Vector4 },
157 { "PropertyArray", PropertyType.Array },
158 { "PropertyMap", PropertyType.Map },
159 // { "Matrix3", PropertyType.MATRIX3 }, commented out until we need to use Matrices from JSON
160 // { "Matrix", PropertyType.MATRIX },
164 /// ViewRegistry is a singleton.
166 private static CustomViewRegistry instance = null;
168 private CreateControlDelegate _createCallback;
169 private SetPropertyDelegate _setPropertyCallback;
170 private GetPropertyDelegate _getPropertyCallback;
171 private PropertyRangeManager _propertyRangeManager;
174 /// Maps the name of a custom view to a create instance function
175 /// For example, given a string "Spin", we can get a function used to create the Spin View.
177 private Dictionary<String, Func<CustomView>> _constructorMap;
179 private CustomViewRegistry()
181 _createCallback = new CreateControlDelegate(CreateControl);
182 _getPropertyCallback = new GetPropertyDelegate(GetProperty);
183 _setPropertyCallback = new SetPropertyDelegate(SetProperty);
185 _constructorMap = new Dictionary<string, Func<CustomView>>();
186 _propertyRangeManager = new PropertyRangeManager();
189 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
190 private delegate IntPtr CreateControlDelegate(IntPtr cPtrControlName);
192 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
193 private delegate IntPtr GetPropertyDelegate(IntPtr controlPtr, IntPtr propertyName);
195 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
196 private delegate void SetPropertyDelegate(IntPtr controlPtr, IntPtr propertyName, IntPtr propertyValue);
198 /// <since_tizen> 3 </since_tizen>
199 public static CustomViewRegistry Instance
203 if (instance == null)
205 instance = new CustomViewRegistry();
212 /// The function which registers a view and all it's scriptable properties with DALi's type registry.
213 /// Means the view can be created or configured from a JSON script.
215 /// The function uses introspection to scan a views C# properties, then selects the ones with
216 ///[ScriptableProperty] attribute to be registered.
217 /// Example of a Spin view registering itself:
220 /// ViewRegistry registers control type with DALi type registery
221 /// also uses introspection to find any properties that need to be registered with type registry
222 /// ViewRegistry.Instance.Register(CreateInstance, typeof(Spin) );
226 /// <since_tizen> 3 </since_tizen>
227 public void Register(Func<CustomView> createFunction, System.Type viewType)
229 // add the mapping between the view name and it's create function
230 _constructorMap.Add(viewType.ToString(), createFunction);
232 // Call into DALi C++ to register the control with the type registry
233 TypeRegistration.RegisterControl(viewType.ToString(), _createCallback);
235 // Cycle through each property in the class
236 foreach (System.Reflection.PropertyInfo propertyInfo in viewType.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public))
238 if (propertyInfo.CanRead)
240 IEnumerable<Attribute> ie_attrs = propertyInfo.GetCustomAttributes<Attribute>();
241 List<Attribute> li_attrs = new List<Attribute>(ie_attrs);
242 System.Attribute[] attrs = li_attrs.ToArray();
244 foreach (System.Attribute attr in attrs)
246 // If the Scriptable attribute exists, then register it with the type registry.
247 if (attr is ScriptableProperty)
249 NUILog.Debug("Got a DALi JSON scriptable property = " + propertyInfo.Name + ", of type " + propertyInfo.PropertyType.Name);
251 // first get the attribute type, ( default, or animatable)
252 ScriptableProperty scriptableProp = attr as ScriptableProperty;
254 // 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)
255 int propertyIndex = _propertyRangeManager.GetPropertyIndex(viewType.ToString(), viewType, scriptableProp.type);
257 // get the enum for the property type... E.g. registering a string property returns Tizen.NUI.PropertyType.String
258 Tizen.NUI.PropertyType propertyType = GetDaliPropertyType(propertyInfo.PropertyType.Name);
260 // Example RegisterProperty("spin","maxValue", 50001, FLOAT, set, get );
261 // Native call to register the property
262 TypeRegistration.RegisterProperty(viewType.ToString(), propertyInfo.Name, propertyIndex, propertyType, _setPropertyCallback, _getPropertyCallback);
265 NUILog.Debug("property name = " + propertyInfo.Name);
271 /// Called directly from DALi C++ type registry to create a control (view) using no marshalling.
273 /// <returns>Pointer to the control (views) handle.</returns>
274 /// <param name="cPtrControlName">C pointer to the control (view) name.</param>
275 private static IntPtr CreateControl(IntPtr cPtrControlName)
277 string controlName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(cPtrControlName);
279 NUILog.Debug("Create controlled called from C++ create a " + controlName);
281 Func<CustomView> controlConstructor;
283 // find the control constructor
284 if (Instance._constructorMap.TryGetValue(controlName, out controlConstructor))
286 // Create the control
287 CustomView newControl = controlConstructor();
288 if (newControl != null)
290 return newControl.GetPtrfromView(); // return pointer to handle
299 throw new global::System.InvalidOperationException("C# View not registererd with ViewRegistry" + controlName);
303 private static IntPtr GetProperty(IntPtr controlPtr, IntPtr propertyName)
305 string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
306 return Instance.GetPropertyValue(controlPtr, name);
309 private static void SetProperty(IntPtr controlPtr, IntPtr propertyName, IntPtr propertyValue)
311 string name = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(propertyName);
313 NUILog.Debug("SetControlProperty called for:" + name);
315 Instance.SetPropertyValue(controlPtr, name, propertyValue);
318 private Tizen.NUI.PropertyType GetDaliPropertyType(string cSharpTypeName)
320 Tizen.NUI.PropertyType daliType;
321 if (_daliPropertyTypeLookup.TryGetValue(cSharpTypeName, out daliType))
323 NUILog.Debug("mapped " + cSharpTypeName + " to dAli type " + daliType);
329 NUILog.Debug("Failed to find a mapping between C# property" + cSharpTypeName + " and DALi type");
331 return PropertyType.None;
336 /// Gets a property value from a view.
338 private IntPtr GetPropertyValue(IntPtr refObjectPtr, string propertyName)
340 // Get the C# control that maps to the C++ control
341 View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
344 // call the get property function
345 System.Object val = view.GetType().GetProperty(propertyName).GetAccessors()[0].Invoke(view, null);
347 PropertyValue value = PropertyValue.CreateFromObject(val);
349 return (IntPtr)PropertyValue.getCPtr(value);
358 /// Sets a property value on a view.
360 private void SetPropertyValue(IntPtr refObjectPtr, string propertyName, IntPtr propertyValuePtr)
362 // Get the C# control that maps to the C++ control
363 NUILog.Debug("SetPropertyValue refObjectPtr = {0:X}" + refObjectPtr);
365 PropertyValue propValue = new PropertyValue(propertyValuePtr, false);
367 // Get the C# control that maps to the C++ control
368 View view = Registry.GetManagedBaseHandleFromRefObject(refObjectPtr) as View;
371 System.Reflection.PropertyInfo propertyInfo = view.GetType().GetProperty(propertyName);
372 // We know the property name, we know it's type, we just need to convert from a DALi property value to native C# type
373 System.Type type = propertyInfo.PropertyType;
376 if (type.Equals(typeof(Int32)))
379 ok = propValue.Get(out value);
382 propertyInfo.SetValue(view, value);
385 else if (type.Equals(typeof(bool)))
388 ok = propValue.Get(out value);
391 propertyInfo.SetValue(view, value);
394 else if (type.Equals(typeof(float)))
397 ok = propValue.Get(out value);
400 propertyInfo.SetValue(view, value);
403 else if (type.Equals(typeof(string)))
406 ok = propValue.Get(out value);
409 propertyInfo.SetValue(view, value);
412 else if (type.Equals(typeof(Vector2)))
414 Vector2 value = new Vector2();
415 ok = propValue.Get(value);
418 propertyInfo.SetValue(view, value);
421 else if (type.Equals(typeof(Vector3)))
423 Vector3 value = new Vector3();
424 ok = propValue.Get(value);
427 propertyInfo.SetValue(view, value);
430 else if (type.Equals(typeof(Vector4)))
432 Vector4 value = new Vector4();
433 ok = propValue.Get(value);
437 propertyInfo.SetValue(view, value);
440 else if (type.Equals(typeof(Position)))
442 Position value = new Position();
443 ok = propValue.Get(value);
446 propertyInfo.SetValue(view, value);
449 else if (type.Equals(typeof(Size)))
451 Size value = new Size();
452 ok = propValue.Get(value);
455 propertyInfo.SetValue(view, new Size(value.Width, value.Height, value.Depth));
458 else if (type.Equals(typeof(Color)))
460 // Colors are stored as Vector4's in DALi
461 Color value = new Color();
462 ok = propValue.Get(value);
465 propertyInfo.SetValue(view, (Color)value);
468 else if (type.Equals(typeof(PropertyMap)))
470 PropertyMap map = new PropertyMap();
471 ok = propValue.Get(map);
474 propertyInfo.SetValue(view, map);
477 else if (type.Equals(typeof(PropertyArray)))
479 PropertyArray array = new PropertyArray();
480 ok = propValue.Get(array);
483 propertyInfo.SetValue(view, array);
488 throw new global::System.InvalidOperationException("SetPropertyValue Unimplemented type for Property Value for " + type.FullName);
492 throw new global::System.InvalidOperationException("SetPropertyValue propValue.Get failed");
497 throw new global::System.InvalidOperationException("failed to find the control to write a property to: cptr = " + refObjectPtr);