e548f613de0a1b9b04e083970f8f1ca169f663e4
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / property-wrapper.rb
1 #!/usr/bin/env ruby
2 require 'pathname'
3 require 'scanf'
4
5 # Script does the following:
6 # - greps dali-core for DALI_PROPERTY macro which holds all the information about a property ( type, read only etc)
7 # - uses the filename of the macro to detect the class the properties belong to. E.g. actor-impl.cpp  = Actor
8 # - Scans each property macro and builds a list of DALi classes with an array of properties
9 # - Generates the csharp get/set  code for each property
10 # - Pastes the property get / set code into the DALi csharp files
11
12 # Given a DALi C++ property type this table stores the
13 # information needed to produce a csharp getter / setter
14 $typeTable =  [
15         ["BOOLEAN",     "bool",             "ref",  "bool temp = false;"],
16         ["FLOAT",       "float",            "ref",  "float temp = 0.0f;"],
17         ["INTEGER",     "int",              "ref",  "int temp = 0;"],
18         ["VECTOR2",     "Vector2",          "",     "Vector2 temp = new Vector2(0.0f,0.0f);"],
19         ["VECTOR3",     "Vector3",          "",     "Vector3 temp = new Vector3(0.0f,0.0f,0.0f);"],
20         ["VECTOR4",     "Vector4",          "",     "Vector4 temp = new Vector4(0.0f,0.0f,0.0f,0.0f);"],
21         ["MATRIX3",     "Matrix3",          "",     "Matrix3 temp = new Matrix3();"],
22         ["MATRIX",      "Matrix",           "",     "Matrix temp = new Matrix();"  ],
23         ["RECTANGLE",   "RectInteger",      "",     "RectInteger temp = new RectInteger(0,0,0,0);"],
24         ["ROTATION",    "Quaternion",       "",    "Quaternion temp = new Quaternion();"],
25         ["STRING",      "string",           "out",  "string temp;"],
26         ["ARRAY",       "Dali.Property.Array",   "",     "Dali.Property.Array temp = new Dali.Property.Array();"],
27         ["MAP",         "Dali.Property.Map",     "",     "Dali.Property.Map temp = new Dali.Property.Map();"],
28     ]
29
30 # Some csharp classes are renamed ( e.g. C++ Control is called View in C#)
31 $renameMap = [
32               ["Control", "View"]
33              ]
34
35 $daliSwigPath = String.new;
36
37 def getCSharpName( cppClassName )
38
39     entry = $renameMap.select{ |a| a.first == cppClassName }
40     if( entry.empty?)
41       return cppClassName
42     end
43     return entry[0][1]
44 end
45
46 # use the table above to get information for a specific type
47 def getCSharpType( type )
48
49     entry = $typeTable.select{ |a| a.first == type }
50     if( entry.empty? )
51       return nil
52     end
53     return entry[0]
54 end
55
56
57 # Property struct stores the information about a property after parsing the C++ DALI_PROPERTY macro
58 $propertyStruct = Struct.new("Property", :name, :type, :writable, :animatable,:constrainInput, :enum, :shortenum,  :csharpGetter, :csharpSetter, :childProperty,)
59
60 # daliClass struct stores a class name and an array of properties
61 $daliClassStruct = Struct.new("DaliClass", :name, :properties )
62
63 # class array stores all the dali classes ( actor, image etc)
64 $daliClassArray = Array.new
65
66 # list of files not generated by swig that we have tried to inject properties into
67 $filesNotWrapped= Array.new
68
69 # stats
70 $totalProperties = 0
71 $totalDaliClasses = 0
72
73 # global paths
74 $rootPath = ""
75 $daliCorePath = ""
76 $daliSwigPath = ""
77
78 # Extracts data  DALI__PROPERTY( "points", ARRAY,true,false,false,Dali::Path::Property::POINTS )
79 def extractPropertyInfo( propertyMacro )
80
81     # want to extract the property name, type + read only status
82     # split the DALI_PROPERTY macro definition by comma and quotes, and delete any empty segments
83     data = propertyMacro.split(/[\s,"]/).reject { |s| s.empty? }
84
85     propertyName = data[1]
86
87     # e.g. turn viewMatrix into ViewMatrix
88     propertyName[0] = propertyName[0].capitalize
89
90     # extract the property enum name Dali::Path::Property::POINTS -> POINTS
91     shortenum =  data[6].split(":").last
92
93     # store the :name, :type, :writable, :animatable, :constrainInput, :enum
94     property = $propertyStruct.new;
95
96     property.name = propertyName
97     property.type = data[2]
98     property.writable = (data[3]=="true")
99     property.animatable = (data[4] == "true")
100     property.constrainInput = (data[5]=="true")
101     property.enum = shortenum
102
103     return property;
104 end
105
106 # Extracts data from Toolkit property definition
107 def extractToolkitPropertyInfo( propertyMacro )
108
109     # Extract the property name, type
110     property = $propertyStruct.new;
111
112     # Split the macro definition by comma and quotes, close bracket and delete any empty segments
113     data = propertyMacro.split(/[\s,")]/).reject { |s| s.empty? }
114
115     if(data[1] == "PropertyRegistration")
116
117       # Properties defined in Control using PropertyRegistration
118       # const PropertyRegistration Control::Impl::PROPERTY_1(typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty);
119
120       # Creates an array of strings that looks like this:
121       # const 0
122       # PropertyRegistration 1
123       # Control::Impl::PROPERTY_1 2
124       # typeRegistration 3
125       # styleName 4
126       # Toolkit::Control::Property::STYLE_NAME 5
127       # Property::STRING 6
128       # &Control::Impl::SetProperty 7
129       # &Control::Impl::GetProperty 8
130       #
131
132       property.name = data[4]
133
134       propertyType = data[6].rpartition("::")
135       property.type = propertyType[2]
136
137       propertyEnum = data[5].rpartition("::")
138       property.enum = propertyEnum[2]
139
140     else
141
142       # Properties defined in macro DALI_PROPERTY_REGISTRATION or DALI_ANIMATABLE_PROPERTY_REGISTRATION or DALI_CHILD_PROPERTY_REGISTRATION
143       # or DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT:
144       # DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE)
145       # DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA)
146
147       # Creates an array of strings that looks like this:
148       # DALI_PROPERTY_REGISTRATION( 0
149       # Toolkit 1
150       # PageTurnView 2
151       # pageSize 3
152       # VECTOR2 4
153       # PAGE_SIZE 5
154       #
155
156       property.name = data[3]
157
158       #puts property.name
159       if property.name == "image"
160         property.name = "imageMap"
161       end
162
163       if( data[0] == "DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(" )
164         # TODO: Need to work out the property type from the value
165         property.type = "VECTOR4"
166       else
167         property.type = data[4]
168       end
169
170       property.enum = data[data.length-1]
171
172     end
173
174     # e.g. turn styleName into StyleName
175     property.name[0] = property.name[0].capitalize
176
177     property.writable = true
178     property.animatable = false
179     property.constrainInput = false
180     property.childProperty = false;
181
182     # check to see if it's a child property
183     if( data[0] == "DALI_CHILD_PROPERTY_REGISTRATION(" )
184       #puts(" #{property.name} is child property ")
185       property.childProperty = true;
186     end
187     if( data[0] == "DALI_ANIMATABLE_PROPERTY_REGISTRATION(" )
188       #puts("  #{property.name} is animatable")
189       property.animatable = true;
190     end
191
192     return property;
193 end
194
195 def writePropertiesToCSharpFile( daliClass )
196
197    # open the CSharp file autogenerated by SWIG
198   swigFiles =  $daliSwigPath + "/automatic/csharp/"
199
200   # some C++ classes are renamed for C#
201   className = getCSharpName( daliClass.name )
202
203   fileName =(swigFiles + className ) + ".cs"
204
205
206   # it's possible some classes in dali-core aren't being wrapped by swig, so if the swig generated file
207   # doesn't exist just return
208   if( ! File.exist?(fileName) )
209     $filesNotWrapped.push("#{daliClass.name}.cs ")
210     return
211   end
212
213   File.open(fileName, 'r+') do |file|
214
215   last_line  =0
216   file.each { last_line = file.pos unless file.eof? }
217
218   # we seek to the end of the file... minus 3 characters which lets us overwrite the 2 closing brackets
219   # so we can insert the getter/setter stuff into the file.
220   file.seek( last_line-3, IO::SEEK_SET)
221
222   for property in daliClass.properties
223
224     if (!property.childProperty)
225       file.write( property.csharpGetter );
226       file.write( property.csharpSetter );
227     end
228
229   end
230
231   file.write("\n}\n\n}");  # re-insert the closing brackets we over-wrote
232   end
233
234   puts("Injected #{daliClass.properties.length} C# Properties from #{daliClass.name} into #{className}.cs".blueBackground)
235
236 end
237
238 def writeChildPropertiesToCSharpFile( daliClass )
239
240   # open the CSharp file autogenerated by SWIG
241   swigFiles =  $daliSwigPath + "/automatic/csharp/"
242
243   # Add all the child properties to Control
244   fileName = (swigFiles+"View") + ".cs"
245
246   if( ! File.exist?(fileName) )
247     return
248   end
249
250   File.open(fileName, 'r+') do |file|
251
252   last_line  =0
253   file.each { last_line = file.pos unless file.eof? }
254
255   # we seek to the end of the file... minus 3 characters which lets us overwrite the 2 closing brackets
256   # so we can insert the getter/setter stuff into the file.
257   file.seek( last_line-3, IO::SEEK_SET)
258
259   $childPropertyCount = 0
260
261   for property in daliClass.properties
262
263     if (property.childProperty)
264       file.write( property.csharpGetter );
265       file.write( property.csharpSetter );
266       $childPropertyCount += 1
267     end
268
269   end
270
271   file.write("\n}\n\n}");  # re-insert the closing brackets we over-wrote
272   end
273
274   puts("Injected #{$childPropertyCount} C# Child Properties into #{"View"}.cs".blueBackground)
275
276 end
277
278 # Write the CSharp data to the generated .cs file
279 def writeCSharpData
280
281     for daliClass in  $daliClassArray
282
283         #puts ( daliClass.name )
284
285
286         hasChildProperties = false
287
288         for property in daliClass.properties
289             propertyInfo = getCSharpType( property.type )
290
291             if( propertyInfo.length() < 2 )
292                 # some types aren't supported yet like Rotation
293                 next
294             end
295
296             $totalProperties+=1  #  keep track of total
297
298             propertyType = propertyInfo[1]    # e.g. bool or int
299             propertyArg =  propertyInfo[2]  # e.g. ref or out
300             tempDeclaration = propertyInfo[3] # e.g. bool temp;
301
302             csharpClassName = getCSharpName(daliClass.name);
303
304
305             propertyName = "#{csharpClassName}.Property.#{property.enum}"
306
307             if property.childProperty
308               propertyName = "#{csharpClassName}.ChildProperty.#{property.enum}"
309               hasChildProperties = true
310             end
311
312             property.csharpGetter ="  public #{propertyType} #{property.name} \n"\
313                      "  { \n"\
314                      "    get \n" \
315                      "    {\n"\
316                      "      #{tempDeclaration}\n"\
317                      "      GetProperty( #{propertyName}).Get( #{propertyArg} temp );\n"\
318                      "      return temp;\n"\
319                      "    }\n"
320
321             if property.writable
322                   #text.SetProperty(TextLabel.Property.HORIZONTAL_ALIGNMENT, new Property.Value("CENTER"));
323                   property.csharpSetter = "    set \n" \
324                          "    { \n"\
325                          "      SetProperty( #{propertyName}, new Dali.Property.Value( value ) );\n" \
326                          "    }\n"\
327                          "  }\n"
328             else
329                    property.csharpSetter = "}"  # close the opening property declaration
330             end
331         end
332         # write normal properties to the class's own csharp file
333         writePropertiesToCSharpFile( daliClass )
334         # write child properties to View.cs ( on Control has child properties)
335         if (hasChildProperties)
336           writeChildPropertiesToCSharpFile( daliClass )
337         end
338     end
339
340 end
341
342 def getDaliClassItem( className )
343
344    # puts( "getDaliClassItem  "+ className )
345     index = $daliClassArray.index{ |a| a.name == className }
346
347     if( index == nil)
348         # create a new item along with a array for it's properites
349         classItem = $daliClassStruct.new( className, Array.new )
350         $daliClassArray.push( classItem )
351         $totalDaliClasses+=1  # for stats
352     else
353         # puts("class found " + className )
354         classItem = $daliClassArray[ index ]
355     end
356
357     return classItem;
358
359 end
360
361
362
363 def init
364
365     pn = Pathname.new(Dir.pwd)
366     fullPath = pn.to_s
367
368     $rootPath = fullPath.slice(0..( fullPath.index('/dali-toolkit')))
369     $daliCorePath = $rootPath + "dali-core/dali"   # source code path
370     $daliSwigPath = $rootPath + "dali-toolkit/plugins/dali-swig"
371     $daliToolkitPath = $rootPath + "dali-toolkit/dali-toolkit"  # source code path
372
373     puts("--------------------------------------------")
374     puts("Injecting DALi properties into SWIG generated C# files ")
375     puts("")
376
377
378 end
379
380 def writeDaliCoreProperties
381
382     puts("Scanning for DALI_PROPERTY macro in dali-core");
383     puts("Scanning folder: #{$daliCorePath}\n\n");
384
385     # Executed a recursive grep over dali-core for the DALI_PROPERTY macro
386     result =`grep --include \\*.cpp  -r "DALI_PROPERTY( \" #{$daliCorePath}`
387
388
389     # We now have a list of lines that look like this:
390     # dali/internal/event/animation/path-impl.cpp:DALI__PROPERTY( "points", ARRAY,true,false,false,Dali::Path::Property::POINTS )
391
392     lines = result.split(/\n+/);
393     for line in lines
394
395
396       # Split the line into file name and property macro, splt 2 means just create two strings as we don't want to split
397       # property Dali::Path::Property::POINTS string as well
398
399       data  = line.split(":",2)
400       fileName = data[0];
401       macro = data[1];
402
403       # Get the class name from the filename  ( e.g. image-actor-impl.cpp => image-actor)
404       className = File.basename(fileName,"-impl.cpp").capitalize
405
406       # convert it from image-actor to ImageActor
407       className = className.split(/ |\_|\-/).map(&:capitalize).join
408
409       # Get the property information ( name, type, read/writeable)
410       propertyInfo = extractPropertyInfo( macro );
411
412       # get or create a new DALi class item which stores the property information
413       classItem  = getDaliClassItem( className )
414
415       classItem.properties.push( propertyInfo )
416
417     end
418
419     writeCSharpData()
420 end
421
422 def writeDaliToolkitProperties
423
424
425     puts("\nScanning for PROPERTY_REGISTRATION macros in dali-toolkit");
426     puts("Scanning folder: #{$daliToolkitPath}\n\n");
427
428     $daliClassArray.clear;
429
430     # Executed a recursive grep over dali-toolkit for following macros
431     # DALI_PROPERTY_REGISTRATION
432     # DALI_ANIMATABLE_PROPERTY_REGISTRATION
433     # DALI_CHILD_PROPERTY_REGISTRATION
434     result =`grep --include \\*.cpp  -w 'Control::Impl::SetProperty\\|DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(\\|DALI_CHILD_PROPERTY_REGISTRATION(\\|DALI_ANIMATABLE_PROPERTY_REGISTRATION(\\|DALI_PROPERTY_REGISTRATION' -r #{$daliToolkitPath}`
435
436     if( result == "" )
437       puts("Error parsing #{$daliToolkitPath} no properties found")
438       return
439     end
440     # create an array to store each DALi class and it's assoc
441     classArray = Array.new
442
443     # We now have a list of lines that look like this:
444     # text-controls/text-label-impl.cpp:DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
445     lines = result.split(/\n+/);
446     for line in lines
447
448
449       # Split the line into file name and property macro, split 2 means just create two strings
450       data  = line.split(":",2)
451       fileName = data[0];
452       macro = data[1];
453
454       # Get the class name from the filename  ( e.g. image-actor-impl.cpp => image-actor)
455       className = File.basename(fileName,"-impl.cpp").capitalize
456
457       # convert it from image-actor to ImageActor
458       className = className.split(/ |\_|\-/).map(&:capitalize).join
459
460       #puts(className);
461       #puts(fileName);
462
463         # Get the property information ( name, type, read/writeable)
464       propertyInfo = extractToolkitPropertyInfo( macro );
465
466       # get or create a new DALi class item which stores the property information
467       classItem  = getDaliClassItem( className )
468
469       classItem.properties.push( propertyInfo )
470
471     end
472
473     writeCSharpData()
474
475 end
476
477 # helper class to color the background
478 class String
479 def blueBackground;   "\e[44m#{self}\e[0m" end
480 end
481
482 def writeStats
483
484   puts("\nFiles that have not been wrapped file by SWIG ( not included in dali.i file):")
485   for i in $filesNotWrapped
486     puts(i)
487   end
488
489   puts("Done. Injected #{$totalProperties} properties into #{$totalDaliClasses} DALi C# classes".blueBackground)
490
491 end
492
493 init()
494
495 writeDaliCoreProperties()
496
497 writeDaliToolkitProperties()
498
499 writeStats()
500