[NUI] Add Tizen.NUI.XamlBuild module
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.XamlBuild / src / public / XamlBuild / CompiledConverters / ExtentsTypeConverter.cs
1 using Mono.Cecil;
2 using Mono.Cecil.Cil;
3 using System;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Globalization;
7 using System.Linq;
8 using System.Reflection;
9
10 using Tizen.NUI;
11 using Tizen.NUI.Xaml.Build.Tasks;
12
13 namespace Tizen.NUI.Xaml.Core.XamlC
14 {
15     internal class ExtentsTypeConverter : ICompiledTypeConverter
16     {
17         IEnumerable<Instruction> GenerateIL(ModuleDefinition module, params ushort[] args)
18         {
19             foreach (var d in args)
20                 yield return Instruction.Create(OpCodes.Ldc_I4, d);
21
22             yield return Instruction.Create(OpCodes.Newobj, module.ImportCtorReference((XamlTask.nuiAssemblyName, XamlTask.nuiNameSpace, "Extents"),
23                 parameterTypes: args.Select(a => ("mscorlib", "System", "UInt16")).ToArray()));
24         }
25
26         public IEnumerable<Instruction> ConvertFromString(string value, ILContext context, BaseNode node)
27         {
28             var module = context.Body.Method.Module;
29
30             if (!string.IsNullOrEmpty(value))
31             {
32                 var thickness = value.Split(',');
33
34                 foreach (var thick in thickness)
35                 {
36                     if (thick.EndsWith("dp") || thick.EndsWith("px"))
37                     {
38                         return null;
39                     }
40                 }
41
42                 if (4 == thickness.Length)
43                 {
44                     ushort start, end, top, bottom;
45
46                     if (ushort.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out start) &&
47                         ushort.TryParse(thickness[1], NumberStyles.Number, CultureInfo.InvariantCulture, out end) &&
48                         ushort.TryParse(thickness[2], NumberStyles.Number, CultureInfo.InvariantCulture, out top) &&
49                         ushort.TryParse(thickness[2], NumberStyles.Number, CultureInfo.InvariantCulture, out bottom))
50
51                         return GenerateIL(module, start, end, top, bottom);
52                 }
53                 else if (1 == thickness.Length)
54                 {
55                     ushort v;
56                     ushort.TryParse(thickness[0], NumberStyles.Number, CultureInfo.InvariantCulture, out v);
57                     return GenerateIL(module, v, v, v, v);
58                 }
59             }
60
61             throw new XamlParseException($"Cannot convert \"{value}\" into Position", node);
62         }
63     }
64 }