First-class slices, fixes #6

This commit is contained in:
airstruck
2016-02-08 10:52:37 -05:00
parent 3ff76864d3
commit a4a34ce787
3 changed files with 127 additions and 64 deletions

View File

@@ -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 ()