contrib/inkscape_tools: add alternative code generation for gamnit target
authorAlexis Laferrière <alexis.laf@xymus.net>
Sun, 19 Apr 2015 16:38:16 +0000 (12:38 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 21 Apr 2015 13:38:40 +0000 (09:38 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/inkscape_tools/src/svg_to_png_and_nit.nit

index 6b2d70f..80ba5c4 100644 (file)
@@ -157,6 +157,85 @@ end
        end
 end
 
+# Nit module targeting the Gamnit framework
+#
+# Gamnit's `Texture` already manage the lazy loading, no need to do it here.
+class GamnitImageSetSrc
+       super ImageSetSrc
+
+       private fun attributes: Array[String]
+       do
+               # Separate the images from the arrays of images
+               var single_images = new Array[Image]
+               var arrays_of_images = new HashMap[String, Array[Image]]
+
+               for image in images do
+                       var nit_name = image.name
+                       var last_char = nit_name.chars.last
+                       if last_char.to_s.is_numeric then
+
+                               # Is an array
+                               nit_name = nit_name.substring(0, nit_name.length-1)
+                               if not arrays_of_images.keys.has(nit_name) then
+                                       # Create a new array
+                                       var array = new Array[Image]
+                                       arrays_of_images[nit_name] = array
+                               end
+
+                               arrays_of_images[nit_name].add image
+                       else
+                               # Is a single image
+                               single_images.add image
+                       end
+               end
+
+               # Attributes of the class
+               var attributes = new Array[String]
+               attributes.add "\tprivate var main_image = new Texture(\"images/{document.drawing_name}.png\")\n"
+
+               # Add single images to Nit source file
+               for image in single_images do
+                       # Adapt coordinates to new top left and scale
+                       var coordinates = document.coordinates(image)
+
+                       attributes.add "\tvar {image.name}: Texture = main_image.subtexture({coordinates})\n"
+               end
+
+               # Add array of images too
+               for name, images in arrays_of_images do
+
+                       var lines = new Array[String]
+                       for image in images do
+                               var coordinates = document.coordinates(image)
+                               lines.add "\t\tmain_image.subtexture({coordinates})"
+                       end
+
+                       attributes.add """
+       var {{{name}}} = new Array[Texture].with_items(
+{{{lines.join(",\n")}}})
+"""
+               end
+
+               return attributes
+       end
+
+       redef fun rendering
+       do
+               add """
+# File generated by svg_to_png_and_nit, do not modify, redef instead
+
+import gamnit::display
+
+class {{{document.nit_class_name}}}
+
+"""
+               add_all attributes
+               add """
+end
+"""
+       end
+end
+
 redef class Int
        # Magic adaption of this coordinates to the given `margin` and `scale`
        fun adapt(margin: Int, scale: Float): Int
@@ -177,10 +256,11 @@ end
 var opt_out_src = new OptionString("Path to output source file (folder or file)", "--src", "-s")
 var opt_assets = new OptionString("Path to assert dir where to put PNG files", "--assets", "-a")
 var opt_scale = new OptionFloat("Apply scaling to exported images (default at 1.0 of 90dpi)", 1.0, "--scale", "-x")
+var opt_gamnit = new OptionBool("Target the Gamnit framework (by default it targets Mnit)", "--gamnit", "-g")
 var opt_help = new OptionBool("Print this help message", "--help", "-h")
 
 var opt_context = new OptionContext
-opt_context.add_option(opt_out_src, opt_assets, opt_scale, opt_help)
+opt_context.add_option(opt_out_src, opt_assets, opt_scale, opt_gamnit, opt_help)
 
 opt_context.parse(args)
 var rest = opt_context.rest
@@ -298,7 +378,12 @@ for drawing in drawings do
        var document = new Document(drawing_name, scale, min_x, max_x, min_y, max_y)
 
        # Nit class
-       var nit_src = new MnitImageSetSrc(document, images)
+       var nit_src: ImageSetSrc
+       if opt_gamnit.value then
+               nit_src = new GamnitImageSetSrc(document, images)
+       else
+               nit_src = new MnitImageSetSrc(document, images)
+       end
 
        if not src_path.file_extension == "nit" then
                src_path = src_path/drawing_name+".nit"