Compare commits

...

3 Commits

Author SHA1 Message Date
mars 7604a23824 Load Atlas and Font images from paths 2023-04-20 21:19:17 -04:00
mars 886ad221a0 Implement Atlas anchors 2023-04-20 21:12:37 -04:00
mars e4b65b2fd5 Add MultiMenu positioning and backgrounds 2023-04-20 20:59:27 -04:00
7 changed files with 68 additions and 32 deletions

View File

@ -58,8 +58,7 @@ end
---@param y number The Y position to draw at. ---@param y number The Y position to draw at.
function Animation:draw(index, x, y) function Animation:draw(index, x, y)
local frame = self.frames[index] local frame = self.frames[index]
local mesh = frame.atlas.meshes[frame.sprite] frame.atlas:draw(frame.sprite, x, y)
love.graphics.draw(mesh, x, y)
end end
return Animation return Animation

View File

@ -6,10 +6,14 @@ local Object = require "lib/classic"
local Atlas = Object:extend() local Atlas = Object:extend()
---Creates a new atlas. ---Creates a new atlas.
---@param image love.Image The source image for the atlas. ---@param image_path string The path to the source image for the atlas.
---@param sw integer The width of each sprite. ---@param sw integer The width of each sprite.
---@param sh integer The height of each sprite. ---@param sh integer The height of each sprite.
function Atlas:new(image, sw, sh) ---@param ax integer? The anchor X coordinate of each sprite. Defaults to 0.
---@param ay integer? The anchor Y coordinate of each sprite. Defaults to 0.
function Atlas:new(image_path, sw, sh, ax, ay)
local image = love.graphics.newImage(image_path)
---The source image of the atlas. ---The source image of the atlas.
self.image = image self.image = image
@ -19,6 +23,12 @@ function Atlas:new(image, sw, sh)
---The height of each atlas's sprite. ---The height of each atlas's sprite.
self.sh = sh self.sh = sh
---The anchor X coordinate of each sprite.
self.ax = ax or 0
---The anchor Y coordinate of each sprite.
self.ay = ay or 0
---The width of the atlas in texels. ---The width of the atlas in texels.
self.iw = image:getPixelWidth() self.iw = image:getPixelWidth()
@ -61,4 +71,13 @@ function Atlas:new(image, sw, sh)
end end
end end
---Draws a sprite at a given location.
---@param sprite integer The index of the sprite to draw.
---@param x integer The X coordinate to draw at.
---@param y integer The Y coordinate to draw at.
function Atlas:draw(sprite, x, y)
local mesh = self.meshes[sprite]
love.graphics.draw(mesh, x - self.ax, y - self.ay)
end
return Atlas return Atlas

View File

@ -5,12 +5,12 @@ local Atlas = require "atlas"
local Font = Atlas:extend() local Font = Atlas:extend()
---Creates a new font. ---Creates a new font.
---@param image love.Image The source image for the atlas. ---@param image_path string The path to the source image for the atlas.
---@param sw integer The width of each sprite. ---@param sw integer The width of each sprite.
---@param sh integer The height of each sprite. ---@param sh integer The height of each sprite.
---@param characters string[] A list of characters with indices corresponding to each sprite. ---@param characters string[] A list of characters with indices corresponding to each sprite.
function Font:new(image, sw, sh, characters) function Font:new(image_path, sw, sh, characters)
Font.super.new(self, image, sw, sh) Font.super.new(self, image_path, sw, sh)
---A map of characters to sprites. ---A map of characters to sprites.
self.characters = {} self.characters = {}
@ -32,7 +32,7 @@ function Font:draw(text, x, y)
local sprite = self.characters[c] local sprite = self.characters[c]
if sprite then if sprite then
local x = (i - 1) * self.sw + x local x = (i - 1) * self.sw + x
love.graphics.draw(self.meshes[sprite], x, y) self.super.draw(self, sprite, x, y)
end end
end end
end end

View File

@ -1,4 +1,3 @@
local Animation = require "animation"
local Animator = require "animator" local Animator = require "animator"
local Atlas = require "atlas" local Atlas = require "atlas"
local Font = require "font" local Font = require "font"
@ -18,8 +17,6 @@ local pushOpts = {
push:setupScreen(gameWidth, gameHeight, windowWidth, windowHeight, pushOpts) push:setupScreen(gameWidth, gameHeight, windowWidth, windowHeight, pushOpts)
function love.load() function love.load()
local blockyFont = love.graphics.newImage("assets/small-blocky-font.png")
local blockFontChars = { local blockFontChars = {
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "×", "+", ",", "-", ".", "/", --first line " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "×", "+", ",", "-", ".", "/", --first line
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", --second line "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", --second line
@ -29,9 +26,9 @@ function love.load()
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "{", "|", "}", "~", --fourth line "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "{", "|", "}", "~", --fourth line
} }
BlockyFont = Font(blockyFont, 8, 8, blockFontChars) BlockyFont = Font("assets/small-blocky-font.png", 8, 8, blockFontChars)
local uiAtlas = Atlas(love.graphics.newImage("assets/flat-ui.png"), 32, 32) local uiAtlas = Atlas("assets/flat-ui.png", 32, 32)
Window = NineSlice(uiAtlas, { Window = NineSlice(uiAtlas, {
tl = 26, tl = 26,
@ -59,25 +56,25 @@ function love.load()
options[#options + 1] = { options[#options + 1] = {
text = unit.name, text = unit.name,
callback = function() StateMenu = MultiMenu(BlockyFont, state_menu) end, callback = function()
StateMenu = MultiMenu(BlockyFont, state_menu, 32, 32, Window)
end,
} }
end end
UnitMenu = MultiMenu(BlockyFont, options) UnitMenu = MultiMenu(BlockyFont, options, 16, 16, Window)
ActiveUnit = Animator(Units[1].states, "idle") ActiveUnit = Animator(Units[1].states, "idle")
end end
function love.draw() function love.draw()
push:start() push:start()
Window:draw(100, 100, 5, 5)
BlockyFont:draw("Hello, world!") BlockyFont:draw("Hello, world!")
ActiveUnit:draw() ActiveUnit:draw(240, 180)
UnitMenu:draw()
if StateMenu then if StateMenu then
StateMenu:draw() StateMenu:draw()
else
UnitMenu:draw()
end end
push:finish() push:finish()

View File

@ -6,34 +6,55 @@ local MultiMenu = Object:extend()
---Creates a new multimenu. ---Creates a new multimenu.
---@param font Font ---@param font Font
---@param options table ---@param options table
function MultiMenu:new(font, options) function MultiMenu:new(font, options, x, y, bg)
self.font = font self.font = font
self.options = options self.options = options
self.selected = 1 self.selected = 1
self.x = x
self.y = y
self.bg = bg
self.padding = 16
local inner_width = 0
local inner_height = #options * self.font.sh
for idx, option in ipairs(options) do
local text_width = string.len(option.text) * self.font.sw
inner_width = math.max(inner_width, text_width)
end
local bg_width = inner_width + self.padding * 2
local bg_height = inner_height + self.padding * 2
self.bg_width = math.ceil(bg_width / bg.atlas.sw)
self.bg_height = math.ceil(bg_height / bg.atlas.sh)
end end
function MultiMenu:draw() function MultiMenu:draw()
-- option drawing constants -- option drawing constants
local x = 20 local x = self.x
local y_anchor = 50 local y = self.y
local spacing = 10 local spacing = self.font.sh
y_anchor = y_anchor - spacing local padding = self.padding
--draw the background
self.bg:draw(x, y, self.bg_width, self.bg_height)
-- draw all the options -- draw all the options
for idx, option in ipairs(self.options) do for idx, option in ipairs(self.options) do
local y = y_anchor + idx * spacing local ty = y + (idx - 1) * spacing
self.font:draw(option.text, x, y) self.font:draw(option.text, x + padding, ty + padding)
end end
-- cursor metric constants -- cursor metric constants
local cursor_width = 4 local cursor_width = 4
local cursor_depth = 8 local cursor_depth = 8
local cursor_spacing = 8 local cursor_spacing = 4
local cursor_offset = 4 local cursor_offset = 4
-- calculate the cursor's position -- calculate the cursor's position
local cursor_x = x - cursor_spacing local cursor_x = x - cursor_spacing + padding
local cursor_y = y_anchor + cursor_offset + spacing * self.selected local cursor_y = y + cursor_offset + (self.selected - 1) * spacing + padding
-- draw the cursor -- draw the cursor
love.graphics.polygon("fill", love.graphics.polygon("fill",

View File

@ -16,7 +16,7 @@ function NineSlice:draw(x, y, width, height)
local gy = y + ty * sh local gy = y + ty * sh
if sprite then if sprite then
love.graphics.draw(atlas.meshes[sprite], gx, gy) atlas:draw(sprite, gx, gy)
else else
love.graphics.rectangle("fill", gx, gy, sw, sh) love.graphics.rectangle("fill", gx, gy, sw, sh)
end end

View File

@ -11,8 +11,8 @@ local function rowState(atlas, row, len, on_finish, next)
return spannedState(atlas, start, len, on_finish, next) return spannedState(atlas, start, len, on_finish, next)
end end
local leafRanger = Atlas(love.graphics.newImage("assets/leaf-ranger.png"), 288, 128) local leafRanger = Atlas("assets/leaf-ranger.png", 288, 128, 144, 128)
local bringerOfDeath = Atlas(love.graphics.newImage("assets/bringer-of-death.png"), 140, 93) local bringerOfDeath = Atlas("assets/bringer-of-death.png", 140, 93, 105, 93)
return { return {
{ {