Added dialog windows and control settings.

Lots of comments and changes in UI code in general.
This commit is contained in:
2025-10-13 00:28:12 -04:00
parent 4ecaaf1b50
commit 777b70d1ab
20 changed files with 607 additions and 62 deletions

View File

@@ -21,7 +21,7 @@ local Bar = make_class(UIElement)
------------------------------------------------------------------------------
function Bar:_init(callback, val_fn, min, max, w, h, color, sel_color, press_col, float, x, y)
UIElement._init(self, x, y, float)
UIElement._init(self, x, y, float, false)
self.callback = callback
self.val_fn = val_fn
self.min = min

View File

@@ -11,6 +11,7 @@ local UIElement = require 'src.ui.element'
-- Class definitions
------------------------------------------------------------------------------
---@class Button: UIElement
local Button = make_class(UIElement)
@@ -18,8 +19,12 @@ local Button = make_class(UIElement)
-- Class methods
------------------------------------------------------------------------------
---@param x number
---@param y number
---@param callback function
---@param float boolean
function Button:_init(x, y, callback, float)
UIElement._init(self, x, y, float)
UIElement._init(self, x, y, float, false)
self.selected = false
self.pressed = false
self.was_pressed = false

117
src/ui/dialog.lua Normal file
View File

@@ -0,0 +1,117 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local love = require 'love'
local make_class = require 'src.utils.classes'
local dialog_manager = require 'src.ui.dlgmngr'
local VBox = require 'src.ui.vbox'
local Sprite = require 'src.graphics.sprite'
local TextButton = require 'src.ui.textbtn'
local HBox = require 'src.ui.hbox'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local Dialog = make_class(VBox)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function Dialog:_init(bckg, spacing, padding, close_btn, accept_btn, font)
VBox._init(self, nil, nil, nil, nil, spacing, false)
self.spacing = spacing ~= nil and spacing or 10
self.padding = padding ~= nil and padding or 15
self.background = Sprite(bckg)
self.shown = false
local close = nil
if close_btn ~= nil then
close = close_btn
else
close = TextButton(
'Close',
font,
nil,
nil,
function()
self:hide()
end)
end
local accept = nil
if accept_btn ~= nil then accept = accept_btn end
-- Create a container for the close and accept button and add them.
local box = HBox(nil, nil, nil, nil, 10, true)
box:add(close)
if accept ~= nil then box:add(accept) end
self.elements[9001] = box
end
function Dialog:add(element)
table.insert(self.elements, element)
end
function Dialog:load()
self.background:load()
self:set_dimensions()
VBox.load(self)
end
function Dialog:set_dimensions()
self.w = self.background.sprite:getWidth()
self.h = self.background.sprite:getHeight()
-- Center the dialog on the screen.
self.x = (320 - self.w) / 2
self.y = (200 - self.h) / 2
self.background.x = self.x
self.background.y = self.y
-- Add padding.
self.x = self.x + self.padding
self.y = self.y + self.padding / 2
end
function Dialog:draw()
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle('line', self.x - self.padding - 1, self.y - (self.padding / 2) - 1, self.w + 2, self.h + 2)
love.graphics.setColor()
self.background:draw()
VBox.draw(self)
end
function Dialog:show()
if not self.shown then
dialog_manager:add(self)
self.shown = true
end
end
function Dialog:hide()
dialog_manager:remove(self)
self.shown = false
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return Dialog

104
src/ui/dlgmngr.lua Normal file
View File

@@ -0,0 +1,104 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local make_class = require 'src.utils.classes'
local Drawable = require 'src.graphics.drawable'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local DialogManager = make_class(Drawable)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function DialogManager:_init()
Drawable._init(self)
self.dialogs = {}
self.count = 0
end
function DialogManager:add(dialog)
self.dialogs[dialog] = true
self.count = self.count + 1
end
function DialogManager:remove(dialog)
self.dialogs[dialog] = nil
self.count = math.max(self.count - 1, 0)
end
function DialogManager:empty()
return self.count == 0
end
function DialogManager:update(dt)
for d, _ in pairs(self.dialogs) do
d:update(dt)
end
end
function DialogManager:draw()
for d, _ in pairs(self.dialogs) do
d:draw()
end
end
function DialogManager:keypressed(key, code, isrepeat)
for d, _ in pairs(self.dialogs) do
d:keypressed(key, code, isrepeat)
end
end
function DialogManager:textinput(text)
for d, _ in pairs(self.dialogs) do
d:textinput(text)
end
end
function DialogManager:keyreleased(key, code)
for d, _ in pairs(self.dialogs) do
d:keyreleased(key, code)
end
end
function DialogManager:mousemoved(x, y, dx, dy)
for d, _ in pairs(self.dialogs) do
d:mousemoved(x, y, dx, dy)
end
end
function DialogManager:mousepressed(x, y, btn)
for d, _ in pairs(self.dialogs) do
d:mousepressed(x, y, btn)
end
end
function DialogManager:mousereleased(x, y, btn)
for d, _ in pairs(self.dialogs) do
d:mousereleased(x, y, btn)
end
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return DialogManager()

View File

@@ -6,11 +6,18 @@ local make_class = require 'src.utils.classes'
local Asset = require 'src.utils.asset'
local Drawable = require 'src.graphics.drawable'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local focused_element = nil
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
---@class UIElement: Drawable, Asset
local UIElement = make_class(Drawable, Asset)
@@ -18,13 +25,18 @@ local UIElement = make_class(Drawable, Asset)
-- Class methods
------------------------------------------------------------------------------
function UIElement:_init(x, y, float)
---@param x integer
---@param y integer
---@param float boolean
---@param focusable boolean
function UIElement:_init(x, y, float, focusable)
Asset._init(self)
self.x = x
self.y = y
self.w = nil
self.h = nil
self.float_right = float and float or false
self.focusable = focusable ~= nil and focusable or false
end
@@ -34,7 +46,6 @@ function UIElement:set_dimensions()
end
function UIElement:load()
end
@@ -48,6 +59,24 @@ function UIElement:is_loaded()
end
function UIElement:focus()
if self.focusable then
focused_element = self
end
end
function UIElement:blur()
if self.focusable and focused_element == self then
focused_element = nil
end
end
function UIElement:is_focused()
return self.focusable and focused_element == self
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------

View File

@@ -10,6 +10,7 @@ local Asset = require 'src.utils.asset'
-- Class definitions
------------------------------------------------------------------------------
---@class Font: Asset
-- Font is a simple wrapper around Löve's own Font class to allow for
-- easy loading/unloading and automatically checking if it's is valid
-- before using it.
@@ -20,6 +21,8 @@ local Font = make_class(Asset)
-- Class methods
------------------------------------------------------------------------------
---@param file_name string
---@param size number
function Font:_init(file_name, size)
self.file_name = string.format('assets/%s', file_name)
self.size = (size ~= nil and size) or 20

View File

@@ -10,6 +10,7 @@ local Layout = require 'src.ui.layout'
-- Class definitions
------------------------------------------------------------------------------
---@class HBox: Layout
local HBox = make_class(Layout)
@@ -17,8 +18,8 @@ local HBox = make_class(Layout)
-- Class methods
------------------------------------------------------------------------------
function HBox:_init(x, y, w, h, spacing)
Layout._init(self, x, y, w, h, spacing)
function HBox:_init(x, y, w, h, spacing, float)
Layout._init(self, x, y, w, h, spacing, float)
end

View File

@@ -12,6 +12,7 @@ local Color = require 'src.utils.color'
-- Class definitions
------------------------------------------------------------------------------
---@class Label: UIElement
local Label = make_class(UIElement)
@@ -19,8 +20,14 @@ local Label = make_class(UIElement)
-- Class methods
------------------------------------------------------------------------------
---@param x number
---@param y number
---@param text string
---@param font Font
---@param color Color
---@param float boolean
function Label:_init(x, y, text, font, color, float)
UIElement._init(self, x, y, float)
UIElement._init(self, x, y, float, false)
self.text = text
self.font = font
self.color = color ~= nil and color or Color(255, 255, 255)
@@ -34,9 +41,7 @@ end
function Label:load()
if not self.font:is_loaded() then
self.font:load()
end
self.font:load()
-- 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

View File

@@ -10,6 +10,7 @@ local UIElement = require 'src.ui.element'
-- Class definitions
------------------------------------------------------------------------------
---@class Layout: UIElement
local Layout = make_class(UIElement)
@@ -18,7 +19,7 @@ local Layout = make_class(UIElement)
------------------------------------------------------------------------------
function Layout:_init(x, y, w, h, spacing, float)
UIElement._init(self, x, y, float)
UIElement._init(self, x, y, float, false)
self.x = x ~= nil and x or 0
self.y = y ~= nil and y or 0
self.w = w
@@ -68,6 +69,27 @@ function Layout:draw()
end
function Layout:keypressed(key, code, isrepeat)
for _, v in pairs(self.elements) do
v:keypressed(key, code, isrepeat)
end
end
function Layout:textinput(text)
for _, v in pairs(self.elements) do
v:textinput(text)
end
end
function Layout:keyreleased(key, code)
for _, v in pairs(self.elements) do
v:keyreleased(key, code)
end
end
function Layout:mousemoved(x, y, dx, dy)
for _, v in pairs(self.elements) do
v:mousemoved(x, y, dx, dy)

118
src/ui/textinpt.lua Normal file
View File

@@ -0,0 +1,118 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local love = require 'love'
local make_class = require 'src.utils.classes'
local collisions = require 'src.utils.colls'
local UIElement = require 'src.ui.element'
local Color = require 'src.utils.color'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
---@class CharInput: UIElement
local TextInput = make_class(UIElement)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
---@param text string
---@param font Font
---@param x number
---@param y number
---@param callback function
---@param value function
---@param float boolean
---@param base_col Color
---@param sel_color Color
---@param press_col Color
function TextInput:_init(text, font, x, y, callback, value, float, base_col, sel_color, press_col)
UIElement._init(self, x, y, float, true)
self.callback = callback
self.value = value
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)
self.selected = false
self.pressed = false
end
function TextInput:load()
self.font:load()
-- 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 TextInput:unload()
self.font:unload()
end
function TextInput:is_loaded()
return self.font:is_loaded() and self.w ~= nil and self.h ~= nil
end
function TextInput:set_dimensions()
self.w = self.font:get_width(self.text .. 'm')
self.h = self.font:get_height(self.text)
end
function TextInput:draw()
local color = self.callback == nil and self.disbl_col or (((self.pressed or self:is_focused()) 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()
if self:is_focused() then
love.graphics.print(self.text .. ': ?', self.x, self.y)
else
love.graphics.print(self.text .. ': ' .. self.value(), self.x, self.y)
end
self.font:unset()
love.graphics.setColor()
end
function TextInput:mousemoved(x, y)
-- Select if the pointer is inside the button's bounding rect.
self.selected = collisions.point_in_square(x, y, self.x, self.y, self.w, self.h)
end
function TextInput:mousepressed(_, _, btn)
-- If the pointer was inside the button when it was pressed then mark the button as pressed.
if btn == 0 and self.selected then
self:focus()
end
end
function TextInput:keypressed(key)
if self:is_focused() then
if #key == 1 then
if self.callback ~= nil then self.callback(key) end
self:blur()
end
end
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return TextInput

View File

@@ -10,6 +10,7 @@ local Layout = require 'src.ui.layout'
-- Class definitions
------------------------------------------------------------------------------
---@class VBox: Layout
local VBox = make_class(Layout)
@@ -17,8 +18,8 @@ local VBox = make_class(Layout)
-- Class methods
------------------------------------------------------------------------------
function VBox:_init(x, y, w, h, spacing)
Layout._init(self, x, y, w, h, spacing)
function VBox:_init(x, y, w, h, spacing, float)
Layout._init(self, x, y, w, h, spacing, float)
end