[*] Style improvements, make constructors callable by :.

That is the recommended style for class constructors.
This commit is contained in:
Lorenzo Cogotti 2022-10-25 13:26:30 +02:00
parent 43e9b8f8f7
commit 84d234fca6
12 changed files with 65 additions and 58 deletions

View File

@ -5,7 +5,7 @@
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
-- --
--- Button widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onHit}(), @{yui.Widget.WidgetCallbacks|onLeave}(). -- Button widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onHit}(), @{yui.Widget.WidgetCallbacks|onLeave}().
local BASE = (...):gsub('button$', '') local BASE = (...):gsub('button$', '')
@ -16,7 +16,9 @@ local core = require(BASE..'core')
local shadowtext = require 'lib.gear.shadowtext' local shadowtext = require 'lib.gear.shadowtext'
local T = require('lib.moonspeak').translate local T = require('lib.moonspeak').translate
local Button = setmetatable({}, Widget) local Button = setmetatable({
__call = function(cls, args) return cls:new(args) end
}, Widget)
Button.__index = Button Button.__index = Button
--- Attributes accepted by the @{Button} widget beyond the standard @{yui.Widget.WidgetAttributes|attributes} --- Attributes accepted by the @{Button} widget beyond the standard @{yui.Widget.WidgetAttributes|attributes}
@ -33,8 +35,8 @@ Button.__index = Button
--- Button constructor --- Button constructor
-- @param args (@{ButtonAttributes}) widget attributes -- @param args (@{ButtonAttributes}) widget attributes
function Button.new(args) function Button:new(args)
local self = setmetatable(args, Button) self = setmetatable(args, self)
self.text = self.text or "" self.text = self.text or ""
self.align = self.align or 'center' self.align = self.align or 'center'
@ -57,7 +59,7 @@ local function hit(button)
end end
end end
function Button:onPointerInput(px,py, clicked) function Button:onPointerInput(_,_, clicked)
self:grabFocus() self:grabFocus()
if clicked then hit(self) end if clicked then hit(self) end
end end

View File

@ -4,7 +4,7 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- Checkbox widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}(). -- Checkbox widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}().
local BASE = (...):gsub('checkbox$', '') local BASE = (...):gsub('checkbox$', '')
@ -14,7 +14,9 @@ local core = require(BASE..'core')
local shadowtext = require 'lib.gear.shadowtext' local shadowtext = require 'lib.gear.shadowtext'
local T = require('lib.moonspeak').translate local T = require('lib.moonspeak').translate
local Checkbox = setmetatable({}, Widget) local Checkbox = setmetatable({
__call = function(cls, args) return cls:new(args) end
}, Widget)
Checkbox.__index = Checkbox Checkbox.__index = Checkbox
@ -25,15 +27,15 @@ Checkbox.__index = Checkbox
-- @field text (string) text displayed inside the Checkbox -- @field text (string) text displayed inside the Checkbox
-- @field[opt='center'] valign (string) vertical alignment 'top', 'bottom', 'center' -- @field[opt='center'] valign (string) vertical alignment 'top', 'bottom', 'center'
-- @field[opt='center'] align (string) horizontal alignment, 'left', 'center', 'right' -- @field[opt='center'] align (string) horizontal alignment, 'left', 'center', 'right'
-- @field cornerRadius (number) radius for rounded corners -- @field cornerRadius (number) radius for rounded corner
-- @field notranslate (boolean) don't translate text -- @field notranslate (boolean) don't translate text
-- @table CheckboxAttributes -- @table CheckboxAttributes
--- Checkbox constructor --- Checkbox constructor
-- @param args (@{CheckboxAttributes}) widget attributes -- @param args (@{CheckboxAttributes}) widget attributes
function Checkbox.new(args) function Checkbox:new(args)
local self = setmetatable(args, Checkbox) self = setmetatable(args, self)
self.text = self.text or "" self.text = self.text or ""
self.text = self.notranslate and self.text or T(self.text) self.text = self.notranslate and self.text or T(self.text)
@ -45,7 +47,7 @@ function Checkbox.new(args)
return self return self
end end
function Checkbox:onPointerInput(px,py, clicked) function Checkbox:onPointerInput(_,_, clicked)
self:grabFocus() self:grabFocus()
if clicked then if clicked then
self.checked = not self.checked self.checked = not self.checked
@ -75,7 +77,7 @@ function Checkbox:draw()
love.graphics.line(x+h*.2,y+h*.55, x+h*.45,y+h*.75, x+h*.8,y+h*.2) love.graphics.line(x+h*.2,y+h*.55, x+h*.45,y+h*.75, x+h*.8,y+h*.2)
end end
-- Most of the times checkboxes have no text, so test for performance -- Most checkboxes have no text, so test for performance
if self.text ~= "" then if self.text ~= "" then
love.graphics.setFont(font) love.graphics.setFont(font)
y = y + core.verticalOffsetForAlign(self.valign, font, self.h) y = y + core.verticalOffsetForAlign(self.valign, font, self.h)

View File

@ -4,18 +4,19 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- Multi-choice widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}(). -- Multi-choice widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}().
local BASE = (...):gsub('choice$', '') local BASE = (...):gsub('choice$', '')
local Widget = require(BASE..'widget') local Widget = require(BASE..'widget')
local core = require(BASE..'core') local core = require(BASE..'core')
local clamp = require('lib.gear.algo').clamp
local shadowtext = require 'lib.gear.shadowtext' local shadowtext = require 'lib.gear.shadowtext'
local T = require('lib.moonspeak').translate local T = require('lib.moonspeak').translate
local Choice = setmetatable({ local Choice = setmetatable({
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
}, Widget) }, Widget)
Choice.__index = Choice Choice.__index = Choice
@ -33,8 +34,8 @@ Choice.__index = Choice
--- Choice constructor --- Choice constructor
-- @param args (@{ChoiceAttributes}) widget attributes -- @param args (@{ChoiceAttributes}) widget attributes
function Choice.new(args) function Choice:new(args)
local self = setmetatable(args, Choice) self = setmetatable(args, self)
self.align = self.align or 'center' self.align = self.align or 'center'
self.valign = self.valign or 'center' self.valign = self.valign or 'center'
@ -70,7 +71,7 @@ end
function Choice:checkIndex() function Choice:checkIndex()
if self.nowrap then if self.nowrap then
self.index = math.min(math.max(self.index, 1), #self.choices) self.index = clamp(self.index, 1, #self.choices)
else else
if self.index < 1 then if self.index < 1 then
self.index = #self.choices self.index = #self.choices
@ -108,7 +109,7 @@ function Choice:onActionInput(action)
return true return true
end end
function Choice:onPointerInput(px,py, clicked) function Choice:onPointerInput(px,_, clicked)
self:grabFocus() self:grabFocus()
if not clicked then if not clicked then
return return

View File

@ -4,7 +4,7 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- A widget container, lays down its child widgets into multiple columns. -- A widget container, lays down its child widgets into multiple columns.
local BASE = (...):gsub('columns$', '') local BASE = (...):gsub('columns$', '')
@ -12,17 +12,17 @@ local Layout = require(BASE..'layout')
-- Advance position to next column -- Advance position to next column
-- given current position, widget dimensions and padding -- given current position, widget dimensions and padding
local function columnadvance(x,y, ww,wh, padding) local function columnadvance(x,y, ww,_, padding)
return x + ww + padding, y return x + ww + padding, y
end end
local Columns = setmetatable({ local Columns = setmetatable({
advance = columnadvance, advance = columnadvance,
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
}, Layout) }, Layout)
Columns.__index = Columns Columns.__index = Columns
function Columns.new(args) return setmetatable(Layout.new(args), Columns) end function Columns:new(args) return setmetatable(Layout:new(args), self) end
return Columns return Columns

View File

@ -4,7 +4,7 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- Input widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}(). -- Input widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}().
local BASE = (...):gsub('input$', '') local BASE = (...):gsub('input$', '')
@ -17,7 +17,7 @@ local utf8 = require 'utf8'
-- NOTE: Input manages keyboard directly. -- NOTE: Input manages keyboard directly.
local Input = setmetatable({ local Input = setmetatable({
grabkeyboard = true, grabkeyboard = true,
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
}, Widget) }, Widget)
Input.__index = Input Input.__index = Input
@ -37,8 +37,8 @@ end
--- Input constructor --- Input constructor
-- @param args (@{InputAttributes}) widget attributes -- @param args (@{InputAttributes}) widget attributes
function Input.new(args) function Input:new(args)
local self = setmetatable(args, Input) self = setmetatable(args, self)
self.text = self.text or "" self.text = self.text or ""
self.color = self.color or core.theme.color self.color = self.color or core.theme.color
@ -90,7 +90,7 @@ function Input:textinput(text)
end end
end end
function Input:keypressed(key, code, isrepeat) function Input:keypressed(key, _, isrepeat)
if isrepeat == nil then if isrepeat == nil then
-- LOVE sends 3 types of keypressed() events, -- LOVE sends 3 types of keypressed() events,
-- 1. with isrepeat = true -- 1. with isrepeat = true
@ -111,7 +111,7 @@ function Input:keypressed(key, code, isrepeat)
self:onChange(self.text) self:onChange(self.text)
elseif key == 'delete' and self.cursor ~= utf8.len(self.text)+1 then elseif key == 'delete' and self.cursor ~= utf8.len(self.text)+1 then
local a,b = split(self.text, self.cursor) local a,b = split(self.text, self.cursor)
local _,b = split(b, 2) _,b = split(b, 2)
self.text = table.concat { a, b } self.text = table.concat { a, b }

View File

@ -3,7 +3,6 @@
-- @classmod yui.Label -- @classmod yui.Label
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
--
local BASE = (...):gsub('label$', '') local BASE = (...):gsub('label$', '')
@ -16,7 +15,7 @@ local T = require('lib.moonspeak').translate
-- Labels don't accept focus -- Labels don't accept focus
local Label = setmetatable({ local Label = setmetatable({
nofocus = true, nofocus = true,
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
}, Widget) }, Widget)
Label.__index = Label Label.__index = Label
@ -30,8 +29,8 @@ Label.__index = Label
--- Label constructor --- Label constructor
-- @param args (@{LabelAttributes}) widget attributes -- @param args (@{LabelAttributes}) widget attributes
function Label.new(args) function Label:new(args)
local self = setmetatable(args, Label) self = setmetatable(args, self)
self.text = self.text or "" self.text = self.text or ""
self.text = self.notranslate and self.text or T(self.text) self.text = self.notranslate and self.text or T(self.text)

View File

@ -6,7 +6,8 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- @{Layout} is an internal class, serving as a container of widgets and helping to define their organization and display order. -- @{Layout} is an internal class, serving as a container of widgets and
-- defining their organization and display order.
-- It is useful for arrangement customization. -- It is useful for arrangement customization.
@ -22,7 +23,7 @@ local rectunion = gear.rect.union
local pointinrect = gear.rect.pointinside local pointinrect = gear.rect.pointinside
local Layout = setmetatable({ local Layout = setmetatable({
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
}, Widget) }, Widget)
Layout.__index = Layout Layout.__index = Layout
@ -167,8 +168,8 @@ end
--- Layout constructor --- Layout constructor
-- @param args (@{LayoutAttributes}) widget attributes -- @param args (@{LayoutAttributes}) widget attributes
function Layout.new(args) function Layout:new(args)
local self = setmetatable(args, Layout) self = setmetatable(args, self)
self.padding = self.padding or 0 self.padding = self.padding or 0
self.stack = {} self.stack = {}

View File

@ -4,7 +4,7 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- A widget container, lays down its child widgets into multiple rows. -- A widget container, lays down its child widgets into multiple rows.
local BASE = (...):gsub('rows$', '') local BASE = (...):gsub('rows$', '')
@ -12,17 +12,17 @@ local Layout = require(BASE..'layout')
-- Advance position to next row, -- Advance position to next row,
-- given current position, widget dimensions and padding. -- given current position, widget dimensions and padding.
local function rowadvance(x,y, ww,wh, padding) local function rowadvance(x,y, _,wh, padding)
return x, y + wh + padding return x, y + wh + padding
end end
local Rows = setmetatable({ local Rows = setmetatable({
advance = rowadvance, advance = rowadvance,
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
}, Layout) }, Layout)
Rows.__index = Rows Rows.__index = Rows
function Rows.new(args) return setmetatable(Layout.new(args), Rows) end function Rows:new(args) return setmetatable(Layout:new(args), Rows) end
return Rows return Rows

View File

@ -4,14 +4,16 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- Slider widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}(). -- Slider widget receives the following callbacks: @{yui.Widget.WidgetCallbacks|onEnter}(), @{yui.Widget.WidgetCallbacks|onChange}(), @{yui.Widget.WidgetCallbacks|onLeave}().
local BASE = (...):gsub('slider$', '') local BASE = (...):gsub('slider$', '')
local Widget = require(BASE..'widget') local Widget = require(BASE..'widget')
local core = require(BASE..'core') local core = require(BASE..'core')
local Slider = setmetatable({}, Widget) local Slider = setmetatable({
__call = function(cls, args) return cls:new(args) end
}, Widget)
Slider.__index = Slider Slider.__index = Slider
--- Attributes accepted by the @{Slider} widget beyond the standard @{yui.Widget.WidgetAttributes|attributes} --- Attributes accepted by the @{Slider} widget beyond the standard @{yui.Widget.WidgetAttributes|attributes}
@ -28,8 +30,8 @@ Slider.__index = Slider
--- Slider constructor --- Slider constructor
-- @param args (@{SliderAttributes}) widget attributes -- @param args (@{SliderAttributes}) widget attributes
function Slider.new(args) function Slider:new(args)
local self = setmetatable(args, Slider) self = setmetatable(args, self)
self.color = self.color or core.theme.color self.color = self.color or core.theme.color
self.cornerRadius = self.cornerRadius or core.theme.cornerRadius self.cornerRadius = self.cornerRadius or core.theme.cornerRadius
@ -41,7 +43,7 @@ function Slider.new(args)
return self return self
end end
function Slider:onPointerInput(px,py, clicked, down) function Slider:onPointerInput(px,py, _, down)
self:grabFocus() self:grabFocus()
if not down then if not down then
return return

View File

@ -4,7 +4,7 @@
-- @copyright 2022, The DoubleFourteen Code Forge -- @copyright 2022, The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti, Andrea Pasquini -- @author Lorenzo Cogotti, Andrea Pasquini
-- --
--- Spacer widget insert a space between two widget. -- Spacer widget insert a space between two widget.
local BASE = (...):gsub('spacer$', '') local BASE = (...):gsub('spacer$', '')
@ -13,7 +13,7 @@ local Widget = require(BASE..'widget')
-- Spacers don't accept focus -- Spacers don't accept focus
local Spacer = setmetatable({ local Spacer = setmetatable({
nofocus = true, nofocus = true,
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
}, Widget) }, Widget)
Spacer.__index = Spacer Spacer.__index = Spacer
@ -21,6 +21,6 @@ Spacer.__index = Spacer
--- Attributes accepted by the @{Spacer} widget @{yui.Widget.WidgetAttributes|attributes}. --- Attributes accepted by the @{Spacer} widget @{yui.Widget.WidgetAttributes|attributes}.
-- --
-- @param args @{yui.Widget.WidgetAttributes|Widgetattributes} widget attributes -- @param args @{yui.Widget.WidgetAttributes|Widgetattributes} widget attributes
function Spacer.new(args) return setmetatable(args, Spacer) end function Spacer:new(args) return setmetatable(args, self) end
return Spacer return Spacer

14
ui.lua
View File

@ -71,9 +71,9 @@ end
--- Ui constructor --- Ui constructor
-- @param args (@{UiAttributes}) widget attributes -- @param args (@{UiAttributes}) widget attributes
function Ui.new(args) function Ui:new(args)
local self = setmetatable(args, Ui) self = setmetatable(args, self)
assert(#self == 1, "Ui.new() must have exactly one root widget.") assert(#self == 1, "Ui:new() must have exactly one root widget.")
self.device = self.device or require(BASE..'device.love').new() self.device = self.device or require(BASE..'device.love').new()
self.x = self.x or 0 self.x = self.x or 0
@ -83,7 +83,7 @@ function Ui.new(args)
local root = self[1] local root = self[1]
if not isinstance(root, Widget) then if not isinstance(root, Widget) then
error("Ui.new() bad root Widget type: "..type(root)..".") error("Ui:new() bad root Widget type: "..type(root)..".")
end end
root.x,root.y = self.x,self.y root.x,root.y = self.x,self.y
@ -91,9 +91,9 @@ function Ui.new(args)
if isinstance(root, Layout) then if isinstance(root, Layout) then
root:layoutWidgets() root:layoutWidgets()
else else
assert(type(root.w) == 'number', "Ui.new() root Widget must have a numeric width.") assert(type(root.w) == 'number', "Ui:new() root Widget must have a numeric width.")
assert(type(root.h) == 'number', "Ui.new() root Widget must have a numeric height.") assert(type(root.h) == 'number', "Ui:new() root Widget must have a numeric height.")
assert(not root.nofocus, "Ui.new() single root Widget can't be nofocus.") assert(not root.nofocus, "Ui:new() single root Widget can't be nofocus.")
end end
self.w,self.h = root.w,root.h self.w,self.h = root.w,root.h

View File

@ -18,7 +18,7 @@
local rectunion = require('lib.gear.rect').union local rectunion = require('lib.gear.rect').union
local Widget = { local Widget = {
__call = function(cls, args) return cls.new(args) end __call = function(cls, args) return cls:new(args) end
} }
Widget.__index = Widget Widget.__index = Widget