mirror of
https://github.com/airstruck/luigi.git
synced 2025-11-18 12:25:06 +00:00
First-class slices, fixes #6
This commit is contained in:
@@ -27,6 +27,10 @@ function SpriteBatch:constructor (image)
|
||||
self.sprites = {}
|
||||
end
|
||||
|
||||
function SpriteBatch:clear ()
|
||||
self.sprites = {}
|
||||
end
|
||||
|
||||
--[[
|
||||
id = SpriteBatch:add( quad, x, y, r, sx, sy )
|
||||
|
||||
|
||||
117
luigi/mosaic.lua
Normal file
117
luigi/mosaic.lua
Normal file
@@ -0,0 +1,117 @@
|
||||
local ROOT = (...):gsub('[^.]*$', '')
|
||||
|
||||
local Backend = require(ROOT .. 'backend')
|
||||
local Base = require(ROOT .. 'base')
|
||||
|
||||
local Mosaic = Base:extend()
|
||||
|
||||
local imageCache = {}
|
||||
local sliceCache = {}
|
||||
|
||||
local function loadImage (path)
|
||||
if not imageCache[path] then
|
||||
imageCache[path] = Backend.Image(path)
|
||||
end
|
||||
|
||||
return imageCache[path]
|
||||
end
|
||||
|
||||
function Mosaic.fromWidget (widget)
|
||||
local mosaic = widget.mosaic
|
||||
if mosaic and mosaic.slicePath == widget.slices then
|
||||
return mosaic
|
||||
end
|
||||
if widget.slices then
|
||||
widget.mosaic = Mosaic(widget.slices)
|
||||
return widget.mosaic
|
||||
end
|
||||
end
|
||||
|
||||
function Mosaic:constructor (path)
|
||||
local slices = self:loadSlices(path)
|
||||
self.batch = Backend.SpriteBatch(slices.image)
|
||||
self.slices = slices
|
||||
self.slicePath = path
|
||||
end
|
||||
|
||||
function Mosaic:setRectangle (x, y, w, h)
|
||||
if self.x == x and self.y == y and self.width == w and self.height == h then
|
||||
self.needsRefresh = false
|
||||
return
|
||||
end
|
||||
self.needsRefresh = true
|
||||
self.x, self.y, self.width, self.height = x, y, w, h
|
||||
end
|
||||
|
||||
function Mosaic:loadSlices (path)
|
||||
local slices = sliceCache[path]
|
||||
|
||||
if not slices then
|
||||
slices = {}
|
||||
sliceCache[path] = slices
|
||||
local image = loadImage(path)
|
||||
local iw, ih = image:getWidth(), image:getHeight()
|
||||
local w, h = math.floor(iw / 3), math.floor(ih / 3)
|
||||
local Quad = Backend.Quad
|
||||
|
||||
slices.image = image
|
||||
slices.width = w
|
||||
slices.height = h
|
||||
|
||||
slices.topLeft = Quad(0, 0, w, h, iw, ih)
|
||||
slices.topCenter = Quad(w, 0, w, h, iw, ih)
|
||||
slices.topRight = Quad(iw - w, 0, w, h, iw, ih)
|
||||
|
||||
slices.middleLeft = Quad(0, h, w, h, iw, ih)
|
||||
slices.middleCenter = Quad(w, h, w, h, iw, ih)
|
||||
slices.middleRight = Quad(iw - w, h, w, h, iw, ih)
|
||||
|
||||
slices.bottomLeft = Quad(0, ih - h, w, h, iw, ih)
|
||||
slices.bottomCenter = Quad(w, ih - h, w, h, iw, ih)
|
||||
slices.bottomRight = Quad(iw - w, ih - h, w, h, iw, ih)
|
||||
end
|
||||
|
||||
return slices
|
||||
end
|
||||
|
||||
function Mosaic:draw ()
|
||||
local batch = self.batch
|
||||
|
||||
if not self.needsRefresh then
|
||||
Backend.draw(batch)
|
||||
return
|
||||
end
|
||||
|
||||
self.needsRefresh = false
|
||||
|
||||
local x, y, w, h = self.x, self.y, self.width, self.height
|
||||
local slices = self.slices
|
||||
local sliceWidth, sliceHeight = slices.width, slices.height
|
||||
|
||||
local xScale = (w - sliceWidth * 2) / sliceWidth
|
||||
local yScale = (h - sliceHeight * 2) / sliceHeight
|
||||
|
||||
batch:clear()
|
||||
|
||||
batch:add(slices.middleCenter, x + sliceWidth, y + sliceHeight, 0,
|
||||
xScale, yScale)
|
||||
|
||||
batch:add(slices.topCenter, x + sliceWidth, y, 0,
|
||||
xScale, 1)
|
||||
batch:add(slices.bottomCenter, x + sliceWidth, y + h - sliceHeight, 0,
|
||||
xScale, 1)
|
||||
|
||||
batch:add(slices.middleLeft, x, y + sliceHeight, 0,
|
||||
1, yScale)
|
||||
batch:add(slices.middleRight, x + w - sliceWidth, y + sliceHeight, 0,
|
||||
1, yScale)
|
||||
|
||||
batch:add(slices.topLeft, x, y)
|
||||
batch:add(slices.topRight, x + w - sliceWidth, y)
|
||||
batch:add(slices.bottomLeft, x, y + h - sliceHeight)
|
||||
batch:add(slices.bottomRight, x + w - sliceWidth, y + h - sliceHeight)
|
||||
|
||||
Backend.draw(batch)
|
||||
end
|
||||
|
||||
return Mosaic
|
||||
@@ -3,12 +3,13 @@ local ROOT = (...):gsub('[^.]*$', '')
|
||||
local Backend = require(ROOT .. 'backend')
|
||||
local Base = require(ROOT .. 'base')
|
||||
local Event = require(ROOT .. 'event')
|
||||
local Mosaic = require(ROOT .. 'mosaic')
|
||||
local Text = Backend.Text
|
||||
|
||||
local Painter = Base:extend()
|
||||
|
||||
local imageCache = {}
|
||||
local sliceCache = {}
|
||||
-- local sliceCache = {}
|
||||
|
||||
local function intersectScissor (x, y, w, h)
|
||||
local sx, sy, sw, sh = Backend.getScissor()
|
||||
@@ -39,72 +40,13 @@ function Painter:loadImage (path)
|
||||
return imageCache[path]
|
||||
end
|
||||
|
||||
-- TODO: make slices a seperate drawable
|
||||
|
||||
function Painter:loadSlices (path)
|
||||
local slices = sliceCache[path]
|
||||
|
||||
if not slices then
|
||||
slices = {}
|
||||
sliceCache[path] = slices
|
||||
local image = self:loadImage(path)
|
||||
local iw, ih = image:getWidth(), image:getHeight()
|
||||
local w, h = math.floor(iw / 3), math.floor(ih / 3)
|
||||
local Quad = Backend.Quad
|
||||
|
||||
slices.image = image
|
||||
slices.width = w
|
||||
slices.height = h
|
||||
|
||||
slices.topLeft = Quad(0, 0, w, h, iw, ih)
|
||||
slices.topCenter = Quad(w, 0, w, h, iw, ih)
|
||||
slices.topRight = Quad(iw - w, 0, w, h, iw, ih)
|
||||
|
||||
slices.middleLeft = Quad(0, h, w, h, iw, ih)
|
||||
slices.middleCenter = Quad(w, h, w, h, iw, ih)
|
||||
slices.middleRight = Quad(iw - w, h, w, h, iw, ih)
|
||||
|
||||
slices.bottomLeft = Quad(0, ih - h, w, h, iw, ih)
|
||||
slices.bottomCenter = Quad(w, ih - h, w, h, iw, ih)
|
||||
slices.bottomRight = Quad(iw - w, ih - h, w, h, iw, ih)
|
||||
end
|
||||
|
||||
return slices
|
||||
end
|
||||
|
||||
function Painter:paintSlices ()
|
||||
local widget = self.widget
|
||||
local path = widget.slices
|
||||
if not path then return end
|
||||
|
||||
local mosaic = Mosaic.fromWidget(widget)
|
||||
if not mosaic then return end
|
||||
local x, y, w, h = widget:getRectangle(true)
|
||||
|
||||
local slices = self:loadSlices(path)
|
||||
|
||||
local batch = Backend.SpriteBatch(slices.image)
|
||||
|
||||
local xScale = (w - slices.width * 2) / slices.width
|
||||
local yScale = (h - slices.height * 2) / slices.height
|
||||
|
||||
batch:add(slices.middleCenter, x + slices.width, y + slices.height, 0,
|
||||
xScale, yScale)
|
||||
|
||||
batch:add(slices.topCenter, x + slices.width, y, 0,
|
||||
xScale, 1)
|
||||
batch:add(slices.bottomCenter, x + slices.width, y + h - slices.height, 0,
|
||||
xScale, 1)
|
||||
|
||||
batch:add(slices.middleLeft, x, y + slices.height, 0,
|
||||
1, yScale)
|
||||
batch:add(slices.middleRight, x + w - slices.width, y + slices.height, 0,
|
||||
1, yScale)
|
||||
|
||||
batch:add(slices.topLeft, x, y)
|
||||
batch:add(slices.topRight, x + w - slices.width, y)
|
||||
batch:add(slices.bottomLeft, x, y + h - slices.height)
|
||||
batch:add(slices.bottomRight, x + w - slices.width, y + h - slices.height)
|
||||
|
||||
Backend.draw(batch)
|
||||
mosaic:setRectangle(x, y, w, h)
|
||||
mosaic:draw()
|
||||
end
|
||||
|
||||
function Painter:paintBackground ()
|
||||
|
||||
Reference in New Issue
Block a user