Added labels and flushed-right UI elements.

This commit is contained in:
2025-10-05 22:45:55 -04:00
parent 479ff952cf
commit 2dea06f8c7
6 changed files with 155 additions and 33 deletions

View File

@@ -12,6 +12,7 @@ local Cursor = require 'src.ui.cursor'
local Font = require 'src.ui.font'
local SoundEffect = require 'src.sound.sfx'
local VBox = require 'src.ui.vbox'
local Color = require 'src.utils.color'
------------------------------------------------------------------------------
@@ -33,9 +34,11 @@ function MainMenu:_init(name, index)
-- Create sprites and buttons.
self.background = Sprite('imgs/concb03.png')
self.btn_font = Font('fonts/Concrete.ttf')
self.title_font = Font('fonts/BBrick.ttf', 35)
-- Create UI elements.
self.btns = VBox(15, 15)
self.btns = VBox(15, 5)
self.btns:add_label('LoveDOS', self.title_font, Color(215, 0, 0), true)
self.btns:add_text_button('New Game', self.btn_font)
self.btns:add_text_button('Load Game', self.btn_font)
self.btns:add_text_button('Options', self.btn_font)
@@ -51,6 +54,7 @@ function MainMenu:_init(name, index)
-- Register all assets.
assets:register(self.name, self.btn_font)
assets:register(self.name, self.title_font)
assets:register(self.name, self.background)
assets:register(self.name, self.cursor)
assets:register(self.name, self.bgm)
@@ -75,6 +79,7 @@ end
function MainMenu:draw()
if assets:done(self.name) then
self.background:draw()
self.btns:draw()
self.cursor:draw()
self.fade:draw()

View File

@@ -4,26 +4,22 @@
local make_class = require 'src.utils.classes'
local collisions = require 'src.utils.colls'
local Asset = require 'src.utils.asset'
local Drawable = require 'src.graphics.drawable'
local UIElement = require 'src.ui.element'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local Button = make_class(Drawable, Asset)
local Button = make_class(UIElement)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function Button:_init(x, y, callback)
self.x = x
self.y = y
self.w = nil
self.h = nil
function Button:_init(x, y, callback, float)
UIElement._init(self, x, y, float)
self.selected = false
self.pressed = false
self.was_pressed = false

41
src/ui/element.lua Normal file
View File

@@ -0,0 +1,41 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local make_class = require 'src.utils.classes'
local Asset = require 'src.utils.asset'
local Drawable = require 'src.graphics.drawable'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local UIElement = make_class(Drawable, Asset)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function UIElement:_init(x, y, float)
Asset._init(self)
self.x = x
self.y = y
self.w = nil
self.h = nil
self.float_right = float and float or false
end
function UIElement:set_dimensions()
self.w = 0
self.h = 0
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return UIElement

69
src/ui/label.lua Normal file
View File

@@ -0,0 +1,69 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local love = require 'love'
local make_class = require 'src.utils.classes'
local UIElement = require 'src.ui.element'
local Color = require 'src.utils.color'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local Label = make_class(UIElement)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function Label:_init(x, y, text, font, color, float)
UIElement._init(self, x, y, float)
self.text = text
self.font = font
self.color = color ~= nil and color or Color(255, 255, 255)
end
function Label:set_dimensions()
self.w = self.font:get_width(self.text)
self.h = self.font:get_height(self.text)
end
function Label:load()
if not self.font:is_loaded() then
self.font:load()
end
-- If the label's size has not been computed then do it.
if self.w == nil or self.h == nil then self:set_dimensions() end
end
function Label:unload()
self.font:unload()
end
function Label:is_loaded()
return self.font:is_loaded() and self.w ~= nil and self.h ~= nil
end
function Label:draw()
love.graphics.setColor(self.color.r, self.color.g, self.color.b)
self.font:set()
love.graphics.print(self.text, self.x, self.y)
self.font:unset()
love.graphics.setColor()
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return Label

View File

@@ -19,13 +19,14 @@ local TextButton = make_class(Button)
-- Class methods
------------------------------------------------------------------------------
function TextButton:_init(text, font, x, y, callback, base_col, sel_color, press_col)
Button._init(self, x, y, callback)
function TextButton:_init(text, font, x, y, callback, float, base_col, sel_color, press_col)
Button._init(self, x, y, callback, float)
self.font = font
self.text = text
self.base_col = base_col ~= nil and base_col or Color(255, 255, 255)
self.sel_color = sel_color ~= nil and sel_color or Color(215, 0, 0)
self.press_col = press_col ~= nil and press_col or Color(99, 99, 139)
self.disbl_col = Color(95, 63, 75)
end
@@ -56,7 +57,7 @@ end
function TextButton:draw()
local color = (self.pressed and self.press_col) or ((self.selected and self.sel_color) or self.base_col)
local color = self.callback == nil and self.disbl_col or ((self.pressed and self.press_col) or ((self.selected and self.sel_color) or self.base_col))
love.graphics.setColor(color.r, color.g, color.b)
self.font:set()

View File

@@ -2,10 +2,11 @@
-- Imports
------------------------------------------------------------------------------
local make_class = require 'src.utils.classes'
local Asset = require 'src.utils.asset'
local Drawable = require 'src.graphics.drawable'
local TextButton = require 'src.ui.textbtn'
local make_class = require 'src.utils.classes'
local Asset = require 'src.utils.asset'
local Drawable = require 'src.graphics.drawable'
local Label = require 'src.ui.label'
local TextButton = require 'src.ui.textbtn'
------------------------------------------------------------------------------
@@ -20,37 +21,46 @@ local VBox = make_class(Drawable, Asset)
------------------------------------------------------------------------------
function VBox:_init(x, y, spacing)
self.x = x ~= nil and x or 0
self.y = y ~= nil and y or 0
self.s = spacing ~= nil and spacing or 10
self.btns = {}
self.x = x ~= nil and x or 0
self.y = y ~= nil and y or 0
self.s = spacing ~= nil and spacing or 10
self.elements = {}
end
function VBox:add_text_button(text, font, callback, base_col, sel_color, press_col)
table.insert(self.btns, TextButton(text, font, 0, 0, callback, base_col, sel_color, press_col))
function VBox:add_text_button(text, font, callback, float, base_col, sel_color, press_col)
table.insert(self.elements, TextButton(text, font, 0, 0, callback, float, base_col, sel_color, press_col))
end
function VBox:add_label(text, font, color, float)
table.insert(self.elements, Label(0, 0, text, font, color, float))
end
function VBox:load()
local y = self.y
local _y = self.y
-- Load required assets if needed and then compute the coordinates of each button.
for i, v in pairs(self.btns) do
for _, v in pairs(self.elements) do
if v.is_a[Asset] then
v:load()
end
y = y + ((i > 1 and (self.btns[i - 1].h + self.s)) or 0)
if v.float_right then
v.x = 320 - self.x - v.w
else
v.x = self.x
end
v.x = self.x
v.y = y
v.y = _y
_y = _y + v.h + self.s
end
end
function VBox:unload()
for _, v in pairs(self.btns) do
for _, v in pairs(self.elements) do
if v.is_a[Asset] then
v:unload()
end
@@ -59,35 +69,35 @@ end
function VBox:update(dt)
for _, v in pairs(self.btns) do
for _, v in pairs(self.elements) do
v:update(dt)
end
end
function VBox:draw()
for _, v in pairs(self.btns) do
for _, v in pairs(self.elements) do
v:draw()
end
end
function VBox:mousemoved(x, y, dx, dy)
for _, v in pairs(self.btns) do
for _, v in pairs(self.elements) do
v:mousemoved(x, y, dx, dy)
end
end
function VBox:mousepressed(x, y, btn)
for _, v in pairs(self.btns) do
for _, v in pairs(self.elements) do
v:mousepressed(x, y, btn)
end
end
function VBox:mousereleased(x, y, btn)
for _, v in pairs(self.btns) do
for _, v in pairs(self.elements) do
v:mousereleased(x, y, btn)
end
end