Added text buttons and button groups.
This commit is contained in:
100
src/ui/btngrp.lua
Normal file
100
src/ui/btngrp.lua
Normal file
@@ -0,0 +1,100 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- 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'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local ButtonGroup = make_class(Drawable, Asset)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function ButtonGroup:_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 = {}
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup: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))
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:load()
|
||||
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
|
||||
if v.is_a[Asset] then
|
||||
v:load()
|
||||
end
|
||||
|
||||
y = y + ((i > 1 and (self.btns[i - 1].h + self.s)) or 0)
|
||||
|
||||
v.x = self.x
|
||||
v.y = y
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:unload()
|
||||
for _, v in pairs(self.btns) do
|
||||
if v.is_a[Asset] then
|
||||
v:unload()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:update(dt)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:update(dt)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:draw()
|
||||
for _, v in pairs(self.btns) do
|
||||
v:draw()
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:mousemoved(x, y, dx, dy)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:mousemoved(x, y, dx, dy)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:mousepressed(x, y, btn)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:mousepressed(x, y, btn)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ButtonGroup:mousereleased(x, y, btn)
|
||||
for _, v in pairs(self.btns) do
|
||||
v:mousereleased(x, y, btn)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return ButtonGroup
|
||||
89
src/ui/button.lua
Normal file
89
src/ui/button.lua
Normal file
@@ -0,0 +1,89 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
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'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local Button = make_class(Drawable, Asset)
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function Button:_init(x, y, callback)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.w = nil
|
||||
self.h = nil
|
||||
self.selected = false
|
||||
self.pressed = false
|
||||
self.was_pressed = false
|
||||
self.callback = callback
|
||||
end
|
||||
|
||||
|
||||
function Button:set_dimensions()
|
||||
self.w = 0
|
||||
self.h = 0
|
||||
end
|
||||
|
||||
|
||||
function Button: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)
|
||||
|
||||
-- If the button was pressed then check if the pointer is no longer inside the button.
|
||||
if self.pressed then
|
||||
-- If it's not, then mark the button as "was pressed".
|
||||
self.pressed = collisions.point_in_square(x, y, self.x, self.y, self.w, self.h)
|
||||
self.was_pressed = true
|
||||
end
|
||||
|
||||
-- If the button was pressed and the pointer is inside the button right now.
|
||||
if self.was_pressed and collisions.point_in_square(x, y, self.x, self.y, self.w, self.h) then
|
||||
-- Then mark the button as pressed again.
|
||||
self.pressed = true
|
||||
self.was_pressed = false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Button:mousepressed(x, y, btn)
|
||||
-- If the pointer was inside the button when it was pressed then mark the button as pressed.
|
||||
if btn == 0 then
|
||||
self.pressed = collisions.point_in_square(x, y, self.x, self.y, self.w, self.h)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Button:mousereleased(_, _, btn)
|
||||
if btn == 0 then
|
||||
if self.pressed then
|
||||
-- If the button was pressed then execute the callback if any and unmark as pressed.
|
||||
if self.callback ~= nil then
|
||||
self.callback()
|
||||
end
|
||||
|
||||
self.pressed = false
|
||||
end
|
||||
|
||||
-- The button no longer was pressed.
|
||||
self.was_pressed = false
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return Button
|
||||
@@ -27,7 +27,9 @@ end
|
||||
|
||||
|
||||
function Font:load()
|
||||
self.f = love.graphics.newFont(self.file_name, self.size)
|
||||
if not self:is_loaded() then
|
||||
self.f = love.graphics.newFont(self.file_name, self.size)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -36,6 +38,11 @@ function Font:unload()
|
||||
end
|
||||
|
||||
|
||||
function Font:is_loaded()
|
||||
return self.f ~= nil
|
||||
end
|
||||
|
||||
|
||||
function Font:set()
|
||||
if self.f ~= nil then love.graphics.setFont(self.f) end
|
||||
end
|
||||
@@ -46,6 +53,16 @@ function Font:unset()
|
||||
end
|
||||
|
||||
|
||||
function Font:get_width(text)
|
||||
if self.f ~= nil then return self.f:getWidth(text) else return nil end
|
||||
end
|
||||
|
||||
|
||||
function Font:get_height(text)
|
||||
if self.f ~= nil then return self.f:getHeight(text) else return nil end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
73
src/ui/textbtn.lua
Normal file
73
src/ui/textbtn.lua
Normal file
@@ -0,0 +1,73 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local make_class = require 'src.utils.classes'
|
||||
local Button = require 'src.ui.button'
|
||||
local Color = require 'src.utils.color'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Class definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
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)
|
||||
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)
|
||||
end
|
||||
|
||||
|
||||
function TextButton: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 TextButton:unload()
|
||||
self.font:unload()
|
||||
end
|
||||
|
||||
|
||||
function TextButton:is_loaded()
|
||||
return self.font:is_loaded() and self.w ~= nil and self.h ~= nil
|
||||
end
|
||||
|
||||
|
||||
function TextButton:set_dimensions()
|
||||
self.w = self.font:get_width(self.text)
|
||||
self.h = self.font:get_height(self.text)
|
||||
end
|
||||
|
||||
|
||||
function TextButton:draw()
|
||||
local color = (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()
|
||||
love.graphics.print(self.text, self.x, self.y)
|
||||
self.font:unset()
|
||||
love.graphics.setColor()
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return TextButton
|
||||
Reference in New Issue
Block a user