From 84d234fca65961b7b44c11cdad5c48065a2b4e06 Mon Sep 17 00:00:00 2001 From: Lorenzo Cogotti Date: Tue, 25 Oct 2022 13:26:30 +0200 Subject: [PATCH] [*] Style improvements, make constructors callable by :. That is the recommended style for class constructors. --- button.lua | 12 +++++++----- checkbox.lua | 16 +++++++++------- choice.lua | 13 +++++++------ columns.lua | 8 ++++---- input.lua | 12 ++++++------ label.lua | 7 +++---- layout.lua | 9 +++++---- rows.lua | 8 ++++---- slider.lua | 14 ++++++++------ spacer.lua | 6 +++--- ui.lua | 16 ++++++++-------- widget.lua | 2 +- 12 files changed, 65 insertions(+), 58 deletions(-) diff --git a/button.lua b/button.lua index 6a64cbb..4d98d86 100644 --- a/button.lua +++ b/button.lua @@ -5,7 +5,7 @@ -- @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$', '') @@ -16,7 +16,9 @@ local core = require(BASE..'core') local shadowtext = require 'lib.gear.shadowtext' 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 --- Attributes accepted by the @{Button} widget beyond the standard @{yui.Widget.WidgetAttributes|attributes} @@ -33,8 +35,8 @@ Button.__index = Button --- Button constructor -- @param args (@{ButtonAttributes}) widget attributes -function Button.new(args) - local self = setmetatable(args, Button) +function Button:new(args) + self = setmetatable(args, self) self.text = self.text or "" self.align = self.align or 'center' @@ -57,7 +59,7 @@ local function hit(button) end end -function Button:onPointerInput(px,py, clicked) +function Button:onPointerInput(_,_, clicked) self:grabFocus() if clicked then hit(self) end end diff --git a/checkbox.lua b/checkbox.lua index 1d1b513..7316149 100644 --- a/checkbox.lua +++ b/checkbox.lua @@ -4,7 +4,7 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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$', '') @@ -14,7 +14,9 @@ local core = require(BASE..'core') local shadowtext = require 'lib.gear.shadowtext' 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 @@ -25,15 +27,15 @@ Checkbox.__index = Checkbox -- @field text (string) text displayed inside the Checkbox -- @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 cornerRadius (number) radius for rounded corner -- @field notranslate (boolean) don't translate text -- @table CheckboxAttributes --- Checkbox constructor -- @param args (@{CheckboxAttributes}) widget attributes -function Checkbox.new(args) - local self = setmetatable(args, Checkbox) +function Checkbox:new(args) + self = setmetatable(args, self) self.text = self.text or "" self.text = self.notranslate and self.text or T(self.text) @@ -45,7 +47,7 @@ function Checkbox.new(args) return self end -function Checkbox:onPointerInput(px,py, clicked) +function Checkbox:onPointerInput(_,_, clicked) self:grabFocus() if clicked then 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) 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 love.graphics.setFont(font) y = y + core.verticalOffsetForAlign(self.valign, font, self.h) diff --git a/choice.lua b/choice.lua index df17f7f..2f820ce 100644 --- a/choice.lua +++ b/choice.lua @@ -4,18 +4,19 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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 Widget = require(BASE..'widget') local core = require(BASE..'core') +local clamp = require('lib.gear.algo').clamp local shadowtext = require 'lib.gear.shadowtext' local T = require('lib.moonspeak').translate local Choice = setmetatable({ - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end }, Widget) Choice.__index = Choice @@ -33,8 +34,8 @@ Choice.__index = Choice --- Choice constructor -- @param args (@{ChoiceAttributes}) widget attributes -function Choice.new(args) - local self = setmetatable(args, Choice) +function Choice:new(args) + self = setmetatable(args, self) self.align = self.align or 'center' self.valign = self.valign or 'center' @@ -70,7 +71,7 @@ end function Choice:checkIndex() if self.nowrap then - self.index = math.min(math.max(self.index, 1), #self.choices) + self.index = clamp(self.index, 1, #self.choices) else if self.index < 1 then self.index = #self.choices @@ -108,7 +109,7 @@ function Choice:onActionInput(action) return true end -function Choice:onPointerInput(px,py, clicked) +function Choice:onPointerInput(px,_, clicked) self:grabFocus() if not clicked then return diff --git a/columns.lua b/columns.lua index 25115a4..153ba09 100644 --- a/columns.lua +++ b/columns.lua @@ -4,7 +4,7 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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$', '') @@ -12,17 +12,17 @@ local Layout = require(BASE..'layout') -- Advance position to next column -- 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 end local Columns = setmetatable({ advance = columnadvance, - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end }, Layout) 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 diff --git a/input.lua b/input.lua index 77106b0..eae7c8c 100644 --- a/input.lua +++ b/input.lua @@ -4,7 +4,7 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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$', '') @@ -17,7 +17,7 @@ local utf8 = require 'utf8' -- NOTE: Input manages keyboard directly. local Input = setmetatable({ grabkeyboard = true, - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end }, Widget) Input.__index = Input @@ -37,8 +37,8 @@ end --- Input constructor -- @param args (@{InputAttributes}) widget attributes -function Input.new(args) - local self = setmetatable(args, Input) +function Input:new(args) + self = setmetatable(args, self) self.text = self.text or "" self.color = self.color or core.theme.color @@ -90,7 +90,7 @@ function Input:textinput(text) end end -function Input:keypressed(key, code, isrepeat) +function Input:keypressed(key, _, isrepeat) if isrepeat == nil then -- LOVE sends 3 types of keypressed() events, -- 1. with isrepeat = true @@ -111,7 +111,7 @@ function Input:keypressed(key, code, isrepeat) self:onChange(self.text) elseif key == 'delete' and self.cursor ~= utf8.len(self.text)+1 then local a,b = split(self.text, self.cursor) - local _,b = split(b, 2) + _,b = split(b, 2) self.text = table.concat { a, b } diff --git a/label.lua b/label.lua index 6f185e3..7cc68b4 100644 --- a/label.lua +++ b/label.lua @@ -3,7 +3,6 @@ -- @classmod yui.Label -- @copyright 2022, The DoubleFourteen Code Forge -- @author Lorenzo Cogotti, Andrea Pasquini --- local BASE = (...):gsub('label$', '') @@ -16,7 +15,7 @@ local T = require('lib.moonspeak').translate -- Labels don't accept focus local Label = setmetatable({ nofocus = true, - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end }, Widget) Label.__index = Label @@ -30,8 +29,8 @@ Label.__index = Label --- Label constructor -- @param args (@{LabelAttributes}) widget attributes -function Label.new(args) - local self = setmetatable(args, Label) +function Label:new(args) + self = setmetatable(args, self) self.text = self.text or "" self.text = self.notranslate and self.text or T(self.text) diff --git a/layout.lua b/layout.lua index 102f8c8..82b4f72 100644 --- a/layout.lua +++ b/layout.lua @@ -6,7 +6,8 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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. @@ -22,7 +23,7 @@ local rectunion = gear.rect.union local pointinrect = gear.rect.pointinside local Layout = setmetatable({ - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end }, Widget) Layout.__index = Layout @@ -167,8 +168,8 @@ end --- Layout constructor -- @param args (@{LayoutAttributes}) widget attributes -function Layout.new(args) - local self = setmetatable(args, Layout) +function Layout:new(args) + self = setmetatable(args, self) self.padding = self.padding or 0 self.stack = {} diff --git a/rows.lua b/rows.lua index b6de635..74ef25a 100644 --- a/rows.lua +++ b/rows.lua @@ -4,7 +4,7 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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$', '') @@ -12,17 +12,17 @@ local Layout = require(BASE..'layout') -- Advance position to next row, -- 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 end local Rows = setmetatable({ advance = rowadvance, - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end }, Layout) 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 diff --git a/slider.lua b/slider.lua index bd37f6d..d9c1dc9 100644 --- a/slider.lua +++ b/slider.lua @@ -4,14 +4,16 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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 Widget = require(BASE..'widget') 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 --- Attributes accepted by the @{Slider} widget beyond the standard @{yui.Widget.WidgetAttributes|attributes} @@ -21,15 +23,15 @@ Slider.__index = Slider -- @field max (number) max value of the slider -- @field vertical (boolean) true for vertical slider, false or nil for horizontal slider -- @field value (number) default value --- @field step (number) number of slider's steps +-- @field step (number) number of slider's steps -- @field cornerRadius (number) radius for rounded corners -- @table SliderAttributes --- Slider constructor -- @param args (@{SliderAttributes}) widget attributes -function Slider.new(args) - local self = setmetatable(args, Slider) +function Slider:new(args) + self = setmetatable(args, self) self.color = self.color or core.theme.color self.cornerRadius = self.cornerRadius or core.theme.cornerRadius @@ -41,7 +43,7 @@ function Slider.new(args) return self end -function Slider:onPointerInput(px,py, clicked, down) +function Slider:onPointerInput(px,py, _, down) self:grabFocus() if not down then return diff --git a/spacer.lua b/spacer.lua index d7f2985..1a13623 100644 --- a/spacer.lua +++ b/spacer.lua @@ -4,7 +4,7 @@ -- @copyright 2022, The DoubleFourteen Code Forge -- @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$', '') @@ -13,7 +13,7 @@ local Widget = require(BASE..'widget') -- Spacers don't accept focus local Spacer = setmetatable({ nofocus = true, - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end }, Widget) Spacer.__index = Spacer @@ -21,6 +21,6 @@ Spacer.__index = Spacer --- Attributes accepted by the @{Spacer} widget @{yui.Widget.WidgetAttributes|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 diff --git a/ui.lua b/ui.lua index 09a9d37..a362e21 100644 --- a/ui.lua +++ b/ui.lua @@ -6,7 +6,7 @@ -- --- An Ui manages a hierarchy of Widgets. --- The @{Ui} draws its widgets according to their layout and position, manages input focus, and +-- The @{Ui} draws its widgets according to their layout and position, manages input focus, and -- dispatches events to the appropriate widgets depending on their class and activity status. local BASE = (...):gsub('ui$', '') @@ -71,9 +71,9 @@ end --- Ui constructor -- @param args (@{UiAttributes}) widget attributes -function Ui.new(args) - local self = setmetatable(args, Ui) - assert(#self == 1, "Ui.new() must have exactly one root widget.") +function Ui:new(args) + self = setmetatable(args, self) + assert(#self == 1, "Ui:new() must have exactly one root widget.") self.device = self.device or require(BASE..'device.love').new() self.x = self.x or 0 @@ -83,7 +83,7 @@ function Ui.new(args) local root = self[1] 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 root.x,root.y = self.x,self.y @@ -91,9 +91,9 @@ function Ui.new(args) if isinstance(root, Layout) then root:layoutWidgets() else - 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(not root.nofocus, "Ui.new() single root Widget can't be nofocus.") + 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(not root.nofocus, "Ui:new() single root Widget can't be nofocus.") end self.w,self.h = root.w,root.h diff --git a/widget.lua b/widget.lua index c8769da..f82a3b3 100644 --- a/widget.lua +++ b/widget.lua @@ -18,7 +18,7 @@ local rectunion = require('lib.gear.rect').union local Widget = { - __call = function(cls, args) return cls.new(args) end + __call = function(cls, args) return cls:new(args) end } Widget.__index = Widget