Skip to content

Instantly share code, notes, and snippets.

@radda-ui
Created December 10, 2024 08:25
Show Gist options
  • Save radda-ui/a398a21707643932578b33b82c15a735 to your computer and use it in GitHub Desktop.
Save radda-ui/a398a21707643932578b33b82c15a735 to your computer and use it in GitHub Desktop.
-- [[
-- # Free-to-use License
-- Copyright (c) 2024 Salem Raddaoui
-- Creation Date: December 10, 2024, 9:21 AM
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- 1. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- 2. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- End of License
--]]
local NinePatch = {}
NinePatch.__index = NinePatch
function NinePatch.new(filename)
local self = setmetatable({}, NinePatch)
-- Load the image and its data
self.image = love.graphics.newImage(filename)
self.imageData = love.image.newImageData(filename)
-- Store original dimensions
self.width = self.image:getWidth()
self.height = self.image:getHeight()
-- Parse 9-patch markers
self:parseMarkers()
-- Create the quads for each region
self:createQuads()
return self
end
function NinePatch:parseMarkers()
-- Find horizontal stretchable region from top strip
self.stretchX = {start = 0, ending = 0}
local foundStart = false
for x = 1, self.width - 1 do
local r, g, b, a = self.imageData:getPixel(x, 0)
if not foundStart and r == 0 and g == 0 and b == 0 and a == 1 then
self.stretchX.start = x
foundStart = true
elseif foundStart and (r ~= 0 or g ~= 0 or b ~= 0 or a ~= 1) then
self.stretchX.ending = x - 1
break
end
end
-- Find vertical stretchable region from left strip
self.stretchY = {start = 0, ending = 0}
foundStart = false
for y = 1, self.height - 1 do
local r, g, b, a = self.imageData:getPixel(0, y)
if not foundStart and r == 0 and g == 0 and b == 0 and a == 1 then
self.stretchY.start = y
foundStart = true
elseif foundStart and (r ~= 0 or g ~= 0 or b ~= 0 or a ~= 1) then
self.stretchY.ending = y - 1
break
end
end
-- Calculate content regions (excluding the 1px borders)
self.regions = {
topLeft = {x = 1, y = 1, w = self.stretchX.start - 1, h = self.stretchY.start - 1},
topCenter = {x = self.stretchX.start, y = 1, w = self.stretchX.ending - self.stretchX.start, h = self.stretchY.start - 1},
topRight = {x = self.stretchX.ending, y = 1, w = self.width - self.stretchX.ending - 1, h = self.stretchY.start - 1},
middleLeft = {x = 1, y = self.stretchY.start, w = self.stretchX.start - 1, h = self.stretchY.ending - self.stretchY.start},
middleCenter = {x = self.stretchX.start, y = self.stretchY.start, w = self.stretchX.ending - self.stretchX.start, h = self.stretchY.ending - self.stretchY.start},
middleRight = {x = self.stretchX.ending, y = self.stretchY.start, w = self.width - self.stretchX.ending - 1, h = self.stretchY.ending - self.stretchY.start},
bottomLeft = {x = 1, y = self.stretchY.ending, w = self.stretchX.start - 1, h = self.height - self.stretchY.ending - 1},
bottomCenter = {x = self.stretchX.start, y = self.stretchY.ending, w = self.stretchX.ending - self.stretchX.start, h = self.height - self.stretchY.ending - 1},
bottomRight = {x = self.stretchX.ending, y = self.stretchY.ending, w = self.width - self.stretchX.ending - 1, h = self.height - self.stretchY.ending - 1}
}
end
function NinePatch:createQuads()
self.quads = {}
for name, region in pairs(self.regions) do
self.quads[name] = love.graphics.newQuad(
region.x, region.y, region.w, region.h,
self.width, self.height
)
end
end
function NinePatch:draw(x, y, width, height)
-- Calculate scale factors
local scaleX = (width - self.regions.topLeft.w - self.regions.topRight.w) / self.regions.topCenter.w
local scaleY = (height - self.regions.topLeft.h - self.regions.bottomLeft.h) / self.regions.middleLeft.h
-- Pre-calculate some common positions
local centerX = x + self.regions.topLeft.w
local rightX = x + width - self.regions.topRight.w
local middleY = y + self.regions.topLeft.h
local bottomY = y + height - self.regions.bottomLeft.h
-- Use a single draw call for each region using the pre-created quads
love.graphics.draw(self.image, self.quads.topLeft, x, y)
love.graphics.draw(self.image, self.quads.topCenter, centerX, y, 0, scaleX, 1)
love.graphics.draw(self.image, self.quads.topRight, rightX, y)
love.graphics.draw(self.image, self.quads.middleLeft, x, middleY, 0, 1, scaleY)
love.graphics.draw(self.image, self.quads.middleCenter, centerX, middleY, 0, scaleX, scaleY)
love.graphics.draw(self.image, self.quads.middleRight, rightX, middleY, 0, 1, scaleY)
love.graphics.draw(self.image, self.quads.bottomLeft, x, bottomY)
love.graphics.draw(self.image, self.quads.bottomCenter, centerX, bottomY, 0, scaleX, 1)
love.graphics.draw(self.image, self.quads.bottomRight, rightX, bottomY)
end
return NinePatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment