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
12 # Given a DALi C++ property type this table stores the
13 # information needed to produce a csharp getter / setter
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();"],
30 # Some csharp classes are renamed ( e.g. C++ Control is called View in C#)
35 $daliSwigPath = String.new;
37 def getCSharpName( cppClassName )
39 entry = $renameMap.select{ |a| a.first == cppClassName }
46 # use the table above to get information for a specific type
47 def getCSharpType( type )
49 entry = $typeTable.select{ |a| a.first == type }
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,)
60 # daliClass struct stores a class name and an array of properties
61 $daliClassStruct = Struct.new("DaliClass", :name, :properties )
63 # class array stores all the dali classes ( actor, image etc)
64 $daliClassArray = Array.new
66 # list of files not generated by swig that we have tried to inject properties into
67 $filesNotWrapped= Array.new
78 # Extracts data DALI__PROPERTY( "points", ARRAY,true,false,false,Dali::Path::Property::POINTS )
79 def extractPropertyInfo( propertyMacro )
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? }
85 propertyName = data[1]
87 # e.g. turn viewMatrix into ViewMatrix
88 propertyName[0] = propertyName[0].capitalize
90 # extract the property enum name Dali::Path::Property::POINTS -> POINTS
91 shortenum = data[6].split(":").last
93 # store the :name, :type, :writable, :animatable, :constrainInput, :enum
94 property = $propertyStruct.new;
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
106 # Extracts data from Toolkit property definition
107 def extractToolkitPropertyInfo( propertyMacro )
109 # Extract the property name, type
110 property = $propertyStruct.new;
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? }
115 if(data[1] == "PropertyRegistration")
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);
120 # Creates an array of strings that looks like this:
122 # PropertyRegistration 1
123 # Control::Impl::PROPERTY_1 2
126 # Toolkit::Control::Property::STYLE_NAME 5
128 # &Control::Impl::SetProperty 7
129 # &Control::Impl::GetProperty 8
132 property.name = data[4]
134 propertyType = data[6].rpartition("::")
135 property.type = propertyType[2]
137 propertyEnum = data[5].rpartition("::")
138 property.enum = propertyEnum[2]
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)
147 # Creates an array of strings that looks like this:
148 # DALI_PROPERTY_REGISTRATION( 0
156 property.name = data[3]
159 if property.name == "image"
160 property.name = "imageMap"
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"
167 property.type = data[4]
170 property.enum = data[data.length-1]
174 # e.g. turn styleName into StyleName
175 property.name[0] = property.name[0].capitalize
177 property.writable = true
178 property.animatable = false
179 property.constrainInput = false
180 property.childProperty = false;
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;
187 if( data[0] == "DALI_ANIMATABLE_PROPERTY_REGISTRATION(" )
188 #puts(" #{property.name} is animatable")
189 property.animatable = true;
195 def writePropertiesToCSharpFile( daliClass )
197 # open the CSharp file autogenerated by SWIG
198 swigFiles = $daliSwigPath + "/automatic/csharp/"
200 # some C++ classes are renamed for C#
201 className = getCSharpName( daliClass.name )
203 fileName =(swigFiles + className ) + ".cs"
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 ")
213 File.open(fileName, 'r+') do |file|
216 file.each { last_line = file.pos unless file.eof? }
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)
222 for property in daliClass.properties
224 if (!property.childProperty)
225 file.write( property.csharpGetter );
226 file.write( property.csharpSetter );
231 file.write("\n}\n\n}"); # re-insert the closing brackets we over-wrote
234 puts("Injected #{daliClass.properties.length} C# Properties from #{daliClass.name} into #{className}.cs".blueBackground)
238 def writeChildPropertiesToCSharpFile( daliClass )
240 # open the CSharp file autogenerated by SWIG
241 swigFiles = $daliSwigPath + "/automatic/csharp/"
243 # Add all the child properties to Control
244 fileName = (swigFiles+"View") + ".cs"
246 if( ! File.exist?(fileName) )
250 File.open(fileName, 'r+') do |file|
253 file.each { last_line = file.pos unless file.eof? }
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)
259 $childPropertyCount = 0
261 for property in daliClass.properties
263 if (property.childProperty)
264 file.write( property.csharpGetter );
265 file.write( property.csharpSetter );
266 $childPropertyCount += 1
271 file.write("\n}\n\n}"); # re-insert the closing brackets we over-wrote
274 puts("Injected #{$childPropertyCount} C# Child Properties into #{"View"}.cs".blueBackground)
278 # Write the CSharp data to the generated .cs file
281 for daliClass in $daliClassArray
283 #puts ( daliClass.name )
286 hasChildProperties = false
288 for property in daliClass.properties
289 propertyInfo = getCSharpType( property.type )
291 if( propertyInfo.length() < 2 )
292 # some types aren't supported yet like Rotation
296 $totalProperties+=1 # keep track of total
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;
302 csharpClassName = getCSharpName(daliClass.name);
305 propertyName = "#{csharpClassName}.Property.#{property.enum}"
307 if property.childProperty
308 propertyName = "#{csharpClassName}.ChildProperty.#{property.enum}"
309 hasChildProperties = true
312 property.csharpGetter =" public #{propertyType} #{property.name} \n"\
316 " #{tempDeclaration}\n"\
317 " GetProperty( #{propertyName}).Get( #{propertyArg} temp );\n"\
322 #text.SetProperty(TextLabel.Property.HORIZONTAL_ALIGNMENT, new Property.Value("CENTER"));
323 property.csharpSetter = " set \n" \
325 " SetProperty( #{propertyName}, new Dali.Property.Value( value ) );\n" \
329 property.csharpSetter = "}" # close the opening property declaration
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 )
342 def getDaliClassItem( className )
344 # puts( "getDaliClassItem "+ className )
345 index = $daliClassArray.index{ |a| a.name == className }
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
353 # puts("class found " + className )
354 classItem = $daliClassArray[ index ]
365 pn = Pathname.new(Dir.pwd)
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
373 puts("--------------------------------------------")
374 puts("Injecting DALi properties into SWIG generated C# files ")
380 def writeDaliCoreProperties
382 puts("Scanning for DALI_PROPERTY macro in dali-core");
383 puts("Scanning folder: #{$daliCorePath}\n\n");
385 # Executed a recursive grep over dali-core for the DALI_PROPERTY macro
386 result =`grep --include \\*.cpp -r "DALI_PROPERTY( \" #{$daliCorePath}`
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 )
392 lines = result.split(/\n+/);
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
399 data = line.split(":",2)
403 # Get the class name from the filename ( e.g. image-actor-impl.cpp => image-actor)
404 className = File.basename(fileName,"-impl.cpp").capitalize
406 # convert it from image-actor to ImageActor
407 className = className.split(/ |\_|\-/).map(&:capitalize).join
409 # Get the property information ( name, type, read/writeable)
410 propertyInfo = extractPropertyInfo( macro );
412 # get or create a new DALi class item which stores the property information
413 classItem = getDaliClassItem( className )
415 classItem.properties.push( propertyInfo )
422 def writeDaliToolkitProperties
425 puts("\nScanning for PROPERTY_REGISTRATION macros in dali-toolkit");
426 puts("Scanning folder: #{$daliToolkitPath}\n\n");
428 $daliClassArray.clear;
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}`
437 puts("Error parsing #{$daliToolkitPath} no properties found")
440 # create an array to store each DALi class and it's assoc
441 classArray = Array.new
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+/);
449 # Split the line into file name and property macro, split 2 means just create two strings
450 data = line.split(":",2)
454 # Get the class name from the filename ( e.g. image-actor-impl.cpp => image-actor)
455 className = File.basename(fileName,"-impl.cpp").capitalize
457 # convert it from image-actor to ImageActor
458 className = className.split(/ |\_|\-/).map(&:capitalize).join
463 # Get the property information ( name, type, read/writeable)
464 propertyInfo = extractToolkitPropertyInfo( macro );
466 # get or create a new DALi class item which stores the property information
467 classItem = getDaliClassItem( className )
469 classItem.properties.push( propertyInfo )
477 # helper class to color the background
479 def blueBackground; "\e[44m#{self}\e[0m" end
484 puts("\nFiles that have not been wrapped file by SWIG ( not included in dali.i file):")
485 for i in $filesNotWrapped
489 puts("Done. Injected #{$totalProperties} properties into #{$totalDaliClasses} DALi C# classes".blueBackground)
495 writeDaliCoreProperties()
497 writeDaliToolkitProperties()