Add NineSlice and draw in main

This commit is contained in:
mars 2023-04-20 13:15:38 -04:00
parent 6f4b3b50e4
commit e731fc4c38
2 changed files with 90 additions and 0 deletions

View File

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

74
nine_slice.lua Normal file
View File

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