I'm trying to implement a library for my love2d project which should be able to scale images with the nine patch approach. (If you haven't heard of it: http://radleymarx.com/blog/simple-guide-to-9-patch/.) I thought that the easiest way to do that in love2d would be with spriteBatches, where I create quads for the sections of the image that should/shouldn't be scaled. Anyway, I can't really get this to work.
1st Problem: How do I set the scaling of quads after I already created them?
2nd Problem: I want the ability to repeat quads instead of scaling them if needed, but i got no idea how this could work. But this is optional yet, first of all I want the scaling itself to work.
This is my code so far:
function sign(x) --math function to sign a value.
return x>0 and 1 or x<0 and -1 or 0
end
function convert(image, borderWidth, borderHeight) --convert a normal image into a spritebatch with generated quads.
local rW = image:getWidth() --reference width
local rH = image:getHeight() --reference height
local bW = borderWidth --border width
local bH = borderHeight --border height
local cW = rW - 2 * bW --content width
local cH = rH - 2 * bH --content height
local spriteBatch = love.graphics.newSpriteBatch(image, 9)
local id = {}
--generate the 9 quads with some neat calculations
for i = 1, 3 do
for j = 1, 3 do
local x = sign(i - 1) * bW + math.floor(i / 3) * cW
local y = sign(j - 1) * bH + math.floor(j / 3) * cH
local w = math.abs(i - 2) * bW + math.abs(math.abs(i - 2) - 1) * cW
local h = math.abs(j - 2) * bH + math.abs(math.abs(j - 2) - 1) * cH
id[j + (i * 3) - 3] = spriteBatch:add(love.graphics.newQuad(x, y, w, h, rW, rH), x, y)
end
end
--draw the spriteBatch with applied scaling
--[[
name = spriteBatch object,
width = width of final image,
height = height of final image,
scale = boolean value to switch between scaling and repeating the quads
]]
spriteBatch.draw = function(name, width, height, scale)
--How do I change the scaling of the already existing quads to achieve the wanted width/height?
--(How could I let the quads which would be scaled repeat themselves instead of scaling if needed?)
end
return spriteBatch
end
I know that i could use the "patchy" library which aleady exists (https://github.com/excessive/patchy), but I am still learning and I think it makes more sense for me to create such a library myself to get used to spriteBatches and get some practice.