contrib/inkscape_tools: update support for gamnit
[nit.git] / contrib / inkscape_tools / src / svg_to_png_and_nit.nit
index 6b2d70f..d762c11 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 root_texture = 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 = root_texture.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::textures
+
+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,12 @@ 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_pow2 = new OptionBool("Round the image size to the next power of 2", "--pow2")
 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_pow2, opt_help)
 
 opt_context.parse(args)
 var rest = opt_context.rest
@@ -248,8 +329,10 @@ for drawing in drawings do
        end
        svg_file.close
 
-       assert page_width != -1
-       assert page_height != -1
+       if page_width == -1 or page_height == -1 then
+               stderr.write "Source drawing file '{drawing}' doesn't look like an SVG file\n"
+               exit 1
+       end
 
        # Query Inkscape
        var prog = "inkscape"
@@ -298,7 +381,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"
@@ -309,12 +397,14 @@ for drawing in drawings do
        nit_src.write_to(src_file)
        src_file.close
 
-       # Find closest power of 2
-       var dx = max_x - min_x
-       max_x = dx.next_pow2 + min_x
+       # Find next power of 2
+       if opt_pow2.value then
+               var dx = max_x - min_x
+               max_x = dx.next_pow2 + min_x
 
-       var dy = max_y - min_y
-       max_y = dy.next_pow2 + min_y
+               var dy = max_y - min_y
+               max_y = dy.next_pow2 + min_y
+       end
 
        # Inkscape's --export-area inverts the Y axis. It uses the lower left corner of
        # the drawing area where as queries return coordinates from the top left.