2022-09-12 09:34:46 +02:00
--- Implements a clickable button widget
--
-- @classmod yui.Button
-- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini
--
--
2022-10-25 13:26:30 +02:00
-- Button widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onHit}(), @{yui.Widget.WidgetCallbacks|onLeave}().
2022-09-12 09:34:46 +02:00
2022-08-24 11:18:48 +02:00
local BASE = ( ... ) : gsub ( ' button$ ' , ' ' )
2022-08-15 23:41:17 +02:00
2022-08-16 00:23:52 +02:00
local Widget = require ( BASE .. ' widget ' )
local core = require ( BASE .. ' core ' )
2022-08-15 23:41:17 +02:00
local shadowtext = require ' lib.gear.shadowtext '
local T = require ( ' lib.moonspeak ' ) . translate
2022-10-25 13:26:30 +02:00
local Button = setmetatable ( {
__call = function ( cls , args ) return cls : new ( args ) end
} , Widget )
2022-08-15 23:41:17 +02:00
Button.__index = Button
2022-09-12 09:34:46 +02:00
--- Attributes accepted by the @{Button} widget beyond the standard @{yui.Widget.WidgetAttributes|attributes}
-- and @{yui.Widget.WidgetCallbacks|callbacks}.
--
-- and @{yui.Widget.WidgetCallbacks|callbacks}.
-- @field text (string) text displayed inside the button
-- @field[opt='center'] valign (string) vertical alignment 'top', 'bottom', 'center'
-- @field[opt='center'] align (string) horizontal alignment, 'left', 'center', 'right'
-- @field cornerRadius (number) radius for rounded corners
-- @field notranslate (boolean) don't translate text
-- @table ButtonAttributes
2022-08-15 23:41:17 +02:00
2022-09-12 09:34:46 +02:00
--- Button constructor
-- @param args (@{ButtonAttributes}) widget attributes
2022-10-25 13:26:30 +02:00
function Button : new ( args )
self = setmetatable ( args , self )
2022-08-15 23:41:17 +02:00
self.text = self.text or " "
self.align = self.align or ' center '
self.valign = self.valign or ' center '
self.active = false
if not self.notranslate then
self.text = T ( self.text )
end
return self
end
local function hit ( button )
if not button.active then
button.active = true
button : onHit ( )
button.ui . timer : after ( 0.15 , function ( ) button.active = false end )
end
end
2022-10-25 13:26:30 +02:00
function Button : onPointerInput ( _ , _ , clicked )
2022-08-15 23:41:17 +02:00
self : grabFocus ( )
if clicked then hit ( self ) end
end
function Button : onActionInput ( action )
if action.confirm then hit ( self ) end
end
function Button : draw ( )
local x , y , w , h = self.x , self.y , self.w , self.h
2022-10-25 19:06:57 +02:00
local color , font , cornerRadius = core.themeForWidget ( self )
local c = core.colorForWidgetState ( self , color )
2022-08-15 23:41:17 +02:00
2022-10-25 19:06:57 +02:00
core.drawBox ( x , y , w , h , c , cornerRadius )
2022-08-15 23:41:17 +02:00
love.graphics . setColor ( c.fg )
love.graphics . setFont ( font )
y = y + core.verticalOffsetForAlign ( self.valign , font , h )
shadowtext.printf ( self.text , x + 2 , y , w - 4 , self.align )
end
return Button