Re: 9Patch
Posted: Fri Jun 30, 2023 6:57 am
I put some of the update() function contents into the draw() function and it worked, except for when you want the width and height not to be the same.
Code: Select all
function lib.draw(patch, x, y, width, height)
local imageW, imageH = patch.image:getDimensions()
local scaleX = (width-2*patch.edgeW)/(imageW-2*patch.edgeW)
local scaleY = (width-2*patch.edgeH)/(imageH-2*patch.edgeH)
patch.w = width
patch.h = height
local x1, x2, x3 = x, x+patch.edgeW, x+patch.w-patch.edgeW
local y1, y2, y3 = y, y+patch.edgeH, y+patch.h-patch.edgeH
love.graphics.draw(patch.image, patch.quads[1], x1, y1)
love.graphics.draw(patch.image, patch.quads[2], x2, y1, 0, scaleX, 1)
love.graphics.draw(patch.image, patch.quads[3], x3, y1)
love.graphics.draw(patch.image, patch.quads[4], x1, y2, 0, 1, scaleY)
love.graphics.draw(patch.image, patch.quads[5], x2, y2, 0, scaleX, scaleY)
love.graphics.draw(patch.image, patch.quads[6], x3, y2, 0, 1, scaleY)
love.graphics.draw(patch.image, patch.quads[7], x1, y3)
love.graphics.draw(patch.image, patch.quads[8], x2, y3, 0, scaleX, 1)
love.graphics.draw(patch.image, patch.quads[9], x3, y3)
end
Removed code, fixed W and H values:notcl4y wrote: ↑Fri Jun 30, 2023 9:57 am I put some of the update() function contents into the draw() function and it worked, except for when you want the width and height not to be the same.
What I'm trying to make is a 9patch that can be drawn and its width and height depends on the arguments of its draw() function, not its properties.
Code: Select all
-- drawPatch
function lib.draw(patch, x, y, width, height)
local imageW, imageH = patch.image:getDimensions()
-- edited:
local imageMiddleW = imageW-2*patch.edgeW -- actually you can store this values in the patch
local imageMiddleH = imageH-2*patch.edgeH
local patchMiddleW = width-2*patch.edgeW
local patchMiddleH = height-2*patch.edgeH
local scaleX = patchMiddleW/imageMiddleW
local scaleY = patchMiddleH/imageMiddleH
local x1, x2, x3 = x, x+patch.edgeW, x+patch.edgeW + patchMiddleW
local y1, y2, y3 = y, y+patch.edgeH, y+patch.edgeH + patchMiddleH
-- not edited:
love.graphics.draw(patch.image, patch.quads[1], x1, y1)
love.graphics.draw(patch.image, patch.quads[2], x2, y1, 0, scaleX, 1)
love.graphics.draw(patch.image, patch.quads[3], x3, y1)
love.graphics.draw(patch.image, patch.quads[4], x1, y2, 0, 1, scaleY)
love.graphics.draw(patch.image, patch.quads[5], x2, y2, 0, scaleX, scaleY)
love.graphics.draw(patch.image, patch.quads[6], x3, y2, 0, 1, scaleY)
love.graphics.draw(patch.image, patch.quads[7], x1, y3)
love.graphics.draw(patch.image, patch.quads[8], x2, y3, 0, scaleX, 1)
love.graphics.draw(patch.image, patch.quads[9], x3, y3)
end
Ok, thanksdarkfrei wrote: ↑Fri Jun 30, 2023 10:25 am
Removed code, fixed W and H values:But you can save all not changed values in the patch and update the patch on some game changes.Code: Select all
-- drawPatch function lib.draw(patch, x, y, width, height) local imageW, imageH = patch.image:getDimensions() -- edited: local imageMiddleW = imageW-2*patch.edgeW -- actually you can store this values in the patch local imageMiddleH = imageH-2*patch.edgeH local patchMiddleW = width-2*patch.edgeW local patchMiddleH = height-2*patch.edgeH local scaleX = patchMiddleW/imageMiddleW local scaleY = patchMiddleH/imageMiddleH local x1, x2, x3 = x, x+patch.edgeW, x+patch.edgeW + patchMiddleW local y1, y2, y3 = y, y+patch.edgeH, y+patch.edgeH + patchMiddleH -- not edited: love.graphics.draw(patch.image, patch.quads[1], x1, y1) love.graphics.draw(patch.image, patch.quads[2], x2, y1, 0, scaleX, 1) love.graphics.draw(patch.image, patch.quads[3], x3, y1) love.graphics.draw(patch.image, patch.quads[4], x1, y2, 0, 1, scaleY) love.graphics.draw(patch.image, patch.quads[5], x2, y2, 0, scaleX, scaleY) love.graphics.draw(patch.image, patch.quads[6], x3, y2, 0, 1, scaleY) love.graphics.draw(patch.image, patch.quads[7], x1, y3) love.graphics.draw(patch.image, patch.quads[8], x2, y3, 0, scaleX, 1) love.graphics.draw(patch.image, patch.quads[9], x3, y3) end