lib/gamnit: fix offsets in textures to be lazy
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 12 Dec 2015 00:26:08 +0000 (19:26 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Mon, 14 Dec 2015 16:00:01 +0000 (11:00 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gamnit/textures.nit

index 04efeb1..ee66213 100644 (file)
@@ -62,29 +62,15 @@ abstract class Texture
        # Prepare a subtexture from this texture
        fun subtexture(left, top, width, height: Numeric): GamnitSubtexture
        do
-               # We want only floats
-               left = left.to_f
-               top = top.to_f
-               width = width.to_f
-               height = height.to_f
-
                # Setup the subtexture
-               var subtex = new GamnitSubtexture(root)
-               subtex.width = width
-               subtex.height = height
-
-               subtex.offset_left = offset_left + left / root.width
-               subtex.offset_top = offset_top + top / root.height
-               subtex.offset_right = offset_left + width / root.width
-               subtex.offset_bottom = offset_top + height / root.height
-
+               var subtex = new GamnitSubtexture(root, self, left.to_f, top.to_f, width.to_f, height.to_f)
                return subtex
        end
 
-       private fun offset_left: Float do return 0.0
-       private fun offset_top: Float do return 0.0
-       private fun offset_right: Float do return 1.0
-       private fun offset_bottom: Float do return 1.0
+       fun offset_left: Float do return 0.0
+       fun offset_top: Float do return 0.0
+       fun offset_right: Float do return 1.0
+       fun offset_bottom: Float do return 1.0
 end
 
 # Texture with its own pixels
@@ -167,12 +153,27 @@ class GamnitSubtexture
 
        redef var root
 
+       # Parent texture, from which this texture was created
+       var parent: Texture
+
+       # Left border of this texture compared to `parent`
+       var left: Float
+
+       # Top border of this texture compared to `parent`
+       var top: Float
+
+       private fun set_wh(width, height: Float)
+       is autoinit do
+               self.width = width
+               self.height = height
+       end
+
        redef fun load(force) do root.load(force)
 
-       redef var offset_left = 0.0
-       redef var offset_top = 0.0
-       redef var offset_right = 1.0
-       redef var offset_bottom = 1.0
+       redef var offset_left = parent.offset_left + left / root.width is lazy
+       redef var offset_top = parent.offset_top + top / root.height is lazy
+       redef var offset_right = offset_left + width / root.width is lazy
+       redef var offset_bottom = offset_top + height / root.height is lazy
 end
 
 redef class Sys