9Patch

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: 9Patch

Post by darkfrei »

notcl4y wrote: Fri Jun 30, 2023 6:45 am
darkfrei wrote: Fri Jun 30, 2023 4:55 am where are you calculates new middleW and middleH after rescaling?
Maybe turn those into getMiddle() function?
Just use the update function with new width and height of patched area.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
notcl4y
Citizen
Posts: 85
Joined: Fri Nov 25, 2022 12:23 pm

Re: 9Patch

Post by notcl4y »

darkfrei wrote: Fri Jun 30, 2023 6:57 am Just use the update function with new width and height of patched area.
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.

Here's the changed code of the draw() function

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
I'm sorry if I misunderstood.

Code: Select all

loves_lua = "not so",
wants_to = true
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

Re: 9Patch

Post by darkfrei »

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.
Removed code, fixed W and H values:

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
But you can save all not changed values in the patch and update the patch on some game changes.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
notcl4y
Citizen
Posts: 85
Joined: Fri Nov 25, 2022 12:23 pm

Re: 9Patch

Post by notcl4y »

darkfrei wrote: Fri Jun 30, 2023 10:25 am
Removed code, fixed W and H values:

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
But you can save all not changed values in the patch and update the patch on some game changes.
Ok, thanks

Code: Select all

loves_lua = "not so",
wants_to = true
Post Reply

Who is online

Users browsing this forum: No registered users and 71 guests