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, :develAPI, :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
94 # Check if the property uses devel API
95 # Currently we ignore devel properties for now
96 if data[6].include? "Devel"
98 puts("Ignoring DEVEL API property: " + shortenum);
103 # store the :name, :type, :writable, :animatable, :constrainInput, :enum
104 property = $propertyStruct.new;
106 property.name = propertyName
107 property.type = data[2]
108 property.writable = (data[3]=="true")
109 property.animatable = (data[4] == "true")
110 property.constrainInput = (data[5]=="true")
111 property.enum = shortenum
112 property.develAPI = develAPI;
116 # Extracts data from Toolkit property definition
117 def extractToolkitPropertyInfo( propertyMacro )
119 # Extract the property name, type
120 property = $propertyStruct.new;
122 # Split the macro definition by comma and quotes, close bracket and delete any empty segments
123 data = propertyMacro.split(/[\s,")]/).reject { |s| s.empty? }
125 if(data[1] == "PropertyRegistration")
127 # Properties defined in Control using PropertyRegistration
128 # const PropertyRegistration Control::Impl::PROPERTY_1(typeRegistration, "styleName", Toolkit::Control::Property::STYLE_NAME, Property::STRING, &Control::Impl::SetProperty, &Control::Impl::GetProperty);
130 # Creates an array of strings that looks like this:
132 # PropertyRegistration 1
133 # Control::Impl::PROPERTY_1 2
136 # Toolkit::Control::Property::STYLE_NAME 5
138 # &Control::Impl::SetProperty 7
139 # &Control::Impl::GetProperty 8
142 property.name = data[4]
144 propertyType = data[6].rpartition("::")
145 property.type = propertyType[2]
147 propertyEnum = data[5].rpartition("::")
148 property.enum = propertyEnum[2]
152 # Properties defined in macro DALI_PROPERTY_REGISTRATION or DALI_ANIMATABLE_PROPERTY_REGISTRATION or DALI_CHILD_PROPERTY_REGISTRATION
153 # or DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT:
154 # DALI_PROPERTY_REGISTRATION(Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE)
155 # DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA)
157 # Creates an array of strings that looks like this:
158 # DALI_PROPERTY_REGISTRATION( 0
166 property.name = data[3]
169 if property.name == "image"
170 property.name = "imageMap"
173 if( data[0] == "DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT(" )
174 # TODO: Need to work out the property type from the value
175 property.type = "VECTOR4"
177 property.type = data[4]
180 property.enum = data[data.length-1]
184 # e.g. turn styleName into StyleName
185 property.name[0] = property.name[0].capitalize
187 property.writable = true
188 property.animatable = false
189 property.constrainInput = false
190 property.childProperty = false;
192 # check to see if it's a child property
193 if( data[0] == "DALI_CHILD_PROPERTY_REGISTRATION(" )
194 #puts(" #{property.name} is child property ")
195 property.childProperty = true;
197 if( data[0] == "DALI_ANIMATABLE_PROPERTY_REGISTRATION(" )
198 #puts(" #{property.name} is animatable")
199 property.animatable = true;
205 def writePropertiesToCSharpFile( daliClass )
207 # open the CSharp file autogenerated by SWIG
208 swigFiles = $daliSwigPath + "/automatic/csharp/"
210 # some C++ classes are renamed for C#
211 className = getCSharpName( daliClass.name )
213 fileName =(swigFiles + className ) + ".cs"
216 # it's possible some classes in dali-core aren't being wrapped by swig, so if the swig generated file
217 # doesn't exist just return
218 if( ! File.exist?(fileName) )
219 $filesNotWrapped.push("#{daliClass.name}.cs ")
223 File.open(fileName, 'r+') do |file|
226 file.each { last_line = file.pos unless file.eof? }
228 # we seek to the end of the file... minus 3 characters which lets us overwrite the 2 closing brackets
229 # so we can insert the getter/setter stuff into the file.
230 file.seek( last_line-3, IO::SEEK_SET)
232 for property in daliClass.properties
234 if( (!property.childProperty) && (!property.develAPI))
235 file.write( property.csharpGetter );
236 file.write( property.csharpSetter );
241 file.write("\n}\n\n}"); # re-insert the closing brackets we over-wrote
244 puts("Injected #{daliClass.properties.length} C# Properties from #{daliClass.name} into #{className}.cs".blueBackground)
248 def writeChildPropertiesToCSharpFile( daliClass )
250 # open the CSharp file autogenerated by SWIG
251 swigFiles = $daliSwigPath + "/automatic/csharp/"
253 # Add all the child properties to Control
254 fileName = (swigFiles+"View") + ".cs"
256 if( ! File.exist?(fileName) )
260 File.open(fileName, 'r+') do |file|
263 file.each { last_line = file.pos unless file.eof? }
265 # we seek to the end of the file... minus 3 characters which lets us overwrite the 2 closing brackets
266 # so we can insert the getter/setter stuff into the file.
267 file.seek( last_line-3, IO::SEEK_SET)
269 $childPropertyCount = 0
271 for property in daliClass.properties
273 if (property.childProperty)
274 file.write( property.csharpGetter );
275 file.write( property.csharpSetter );
276 $childPropertyCount += 1
281 file.write("\n}\n\n}"); # re-insert the closing brackets we over-wrote
284 puts("Injected #{$childPropertyCount} C# Child Properties into #{"View"}.cs".blueBackground)
288 # Write the CSharp data to the generated .cs file
291 for daliClass in $daliClassArray
293 #puts ( daliClass.name )
296 hasChildProperties = false
298 for property in daliClass.properties
299 propertyInfo = getCSharpType( property.type )
301 if( propertyInfo.length() < 2 )
302 # some types aren't supported yet like Rotation
306 $totalProperties+=1 # keep track of total
308 propertyType = propertyInfo[1] # e.g. bool or int
309 propertyArg = propertyInfo[2] # e.g. ref or out
310 tempDeclaration = propertyInfo[3] # e.g. bool temp;
312 csharpClassName = getCSharpName(daliClass.name);
315 propertyName = "#{csharpClassName}.Property.#{property.enum}"
317 if property.childProperty
318 propertyName = "#{csharpClassName}.ChildProperty.#{property.enum}"
319 hasChildProperties = true
322 property.csharpGetter =" public #{propertyType} #{property.name} \n"\
326 " #{tempDeclaration}\n"\
327 " GetProperty( #{propertyName}).Get( #{propertyArg} temp );\n"\
332 #text.SetProperty(TextLabel.Property.HORIZONTAL_ALIGNMENT, new Property.Value("CENTER"));
333 property.csharpSetter = " set \n" \
335 " SetProperty( #{propertyName}, new Dali.Property.Value( value ) );\n" \
339 property.csharpSetter = "}" # close the opening property declaration
342 # write normal properties to the class's own csharp file
343 writePropertiesToCSharpFile( daliClass )
344 # write child properties to View.cs ( on Control has child properties)
345 if (hasChildProperties)
346 writeChildPropertiesToCSharpFile( daliClass )
352 def getDaliClassItem( className )
354 # puts( "getDaliClassItem "+ className )
355 index = $daliClassArray.index{ |a| a.name == className }
358 # create a new item along with a array for it's properites
359 classItem = $daliClassStruct.new( className, Array.new )
360 $daliClassArray.push( classItem )
361 $totalDaliClasses+=1 # for stats
363 # puts("class found " + className )
364 classItem = $daliClassArray[ index ]
375 pn = Pathname.new(Dir.pwd)
378 $rootPath = fullPath.slice(0..( fullPath.index('/dali-toolkit')))
379 $daliCorePath = $rootPath + "dali-core/dali" # source code path
380 $daliSwigPath = $rootPath + "dali-toolkit/plugins/dali-swig"
381 $daliToolkitPath = $rootPath + "dali-toolkit/dali-toolkit" # source code path
383 puts("--------------------------------------------")
384 puts("Injecting DALi properties into SWIG generated C# files ")
390 def writeDaliCoreProperties
392 puts("Scanning for DALI_PROPERTY macro in dali-core");
393 puts("Scanning folder: #{$daliCorePath}\n\n");
395 # Executed a recursive grep over dali-core for the DALI_PROPERTY macro
396 result =`grep --include \\*.cpp -r "DALI_PROPERTY( \" #{$daliCorePath}`
399 # We now have a list of lines that look like this:
400 # dali/internal/event/animation/path-impl.cpp:DALI__PROPERTY( "points", ARRAY,true,false,false,Dali::Path::Property::POINTS )
402 lines = result.split(/\n+/);
406 # Split the line into file name and property macro, splt 2 means just create two strings as we don't want to split
407 # property Dali::Path::Property::POINTS string as well
409 data = line.split(":",2)
413 # Get the class name from the filename ( e.g. image-actor-impl.cpp => image-actor)
414 className = File.basename(fileName,"-impl.cpp").capitalize
416 # convert it from image-actor to ImageActor
417 className = className.split(/ |\_|\-/).map(&:capitalize).join
419 # Get the property information ( name, type, read/writeable)
420 propertyInfo = extractPropertyInfo( macro );
422 # get or create a new DALi class item which stores the property information
423 classItem = getDaliClassItem( className )
425 classItem.properties.push( propertyInfo )
432 def writeDaliToolkitProperties
435 puts("\nScanning for PROPERTY_REGISTRATION macros in dali-toolkit");
436 puts("Scanning folder: #{$daliToolkitPath}\n\n");
438 $daliClassArray.clear;
440 # Executed a recursive grep over dali-toolkit for following macros
441 # DALI_PROPERTY_REGISTRATION
442 # DALI_ANIMATABLE_PROPERTY_REGISTRATION
443 # DALI_CHILD_PROPERTY_REGISTRATION
444 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}`
447 puts("Error parsing #{$daliToolkitPath} no properties found")
450 # create an array to store each DALi class and it's assoc
451 classArray = Array.new
453 # We now have a list of lines that look like this:
454 # text-controls/text-label-impl.cpp:DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multiLine", BOOLEAN, MULTI_LINE )
455 lines = result.split(/\n+/);
459 # Split the line into file name and property macro, split 2 means just create two strings
460 data = line.split(":",2)
464 # Get the class name from the filename ( e.g. image-actor-impl.cpp => image-actor)
465 className = File.basename(fileName,"-impl.cpp").capitalize
467 # convert it from image-actor to ImageActor
468 className = className.split(/ |\_|\-/).map(&:capitalize).join
473 # Get the property information ( name, type, read/writeable)
474 propertyInfo = extractToolkitPropertyInfo( macro );
476 # get or create a new DALi class item which stores the property information
477 classItem = getDaliClassItem( className )
479 classItem.properties.push( propertyInfo )
487 # helper class to color the background
489 def blueBackground; "\e[44m#{self}\e[0m" end
494 puts("\nFiles that have not been wrapped file by SWIG ( not included in dali.i file):")
495 for i in $filesNotWrapped
499 puts("Done. Injected #{$totalProperties} properties into #{$totalDaliClasses} DALi C# classes".blueBackground)
505 writeDaliCoreProperties()
507 writeDaliToolkitProperties()