diff --git a/main.lua b/main.lua index 30bab03..d8f497f 100644 --- a/main.lua +++ b/main.lua @@ -3,6 +3,7 @@ local Animator = require "animator" local Atlas = require "atlas" local Font = require "font" local MultiMenu = require "multimenu" +local NineSlice = require "nine_slice" local Units = require "units" local push = require "lib/push" @@ -30,6 +31,20 @@ function love.load() BlockyFont = Font(blockyFont, 8, 8, blockFontChars) + local uiAtlas = Atlas(love.graphics.newImage("assets/flat-ui.png"), 32, 32) + + Window = NineSlice(uiAtlas, { + tl = 26, + top = 27, + tr = 28, + left = 50, + center = 51, + right = 52, + bl = 74, + bottom = 75, + br = 76, + }) + local options = {} for idx, unit in ipairs(Units) do @@ -55,6 +70,7 @@ end function love.draw() push:start() + Window:draw(100, 100, 5, 5) BlockyFont:draw("Hello, world!") ActiveUnit:draw() diff --git a/nine_slice.lua b/nine_slice.lua new file mode 100644 index 0000000..0b3a7a3 --- /dev/null +++ b/nine_slice.lua @@ -0,0 +1,74 @@ +local Object = require "lib/classic" + +local NineSlice = Object:extend() + +function NineSlice:new(atlas, sprites) + self.atlas = atlas + self.sprites = sprites +end + +function NineSlice:draw(x, y, width, height) + local function draw(sprite, tx, ty) + local atlas = self.atlas + local sw = atlas.sw + local sh = atlas.sh + local gx = x + tx * sw + local gy = y + ty * sh + + if sprite then + love.graphics.draw(atlas.meshes[sprite], gx, gy) + else + love.graphics.rectangle("fill", gx, gy, sw, sh) + end + end + + --common variables + local right = width - 1 + local bottom = height - 1 + local sprites = self.sprites + + if width == 1 and height == 1 then + draw(sprites.only_center, 0, 0) + elseif width == 1 then + draw(sprites.vert_top, 0, 0) + draw(sprites.vert_bottom, 0, bottom) + + for cy = 1, bottom - 1 do + draw(sprites.vert_center, 0, cy) + end + elseif height == 1 then + draw(sprites.hori_left, 0, 0) + draw(sprites.hori_right, right, 0) + + for cx = 1, right - 1 do + draw(sprites.hori_center, cx, 0) + end + else + --draw corners + draw(sprites.tl, 0, 0) + draw(sprites.tr, right, 0) + draw(sprites.bl, 0, bottom) + draw(sprites.br, right, bottom) + + --horizontal edges + for cx = 1, right - 1 do + draw(sprites.top, cx, 0) + draw(sprites.bottom, cx, bottom) + end + + --vertical edges + for cy = 1, bottom - 1 do + draw(sprites.left, 0, cy) + draw(sprites.right, right, cy) + end + + --draw center + for cx = 1, right - 1 do + for cy = 1, bottom - 1 do + draw(sprites.center, cx, cy) + end + end + end +end + +return NineSlice