2022-04-11 18:47:26 +02:00
|
|
|
require("common.class")
|
2022-04-27 12:29:54 +02:00
|
|
|
local ContainerField = require("api.page.containerfield")
|
2022-06-29 00:35:52 +02:00
|
|
|
local ServiceField = require("api.page.servicefield")
|
2022-04-11 18:47:26 +02:00
|
|
|
|
|
|
|
---@class ListField: ContainerField, ServiceField
|
2022-04-12 23:45:28 +02:00
|
|
|
---@field selectedIndex integer
|
2022-04-15 04:54:30 +02:00
|
|
|
---@field locked boolean
|
|
|
|
---@field PADDING number[]
|
2022-04-11 18:47:26 +02:00
|
|
|
local ListField = {
|
2022-05-06 01:02:30 +02:00
|
|
|
__name = "ListField",
|
2022-04-15 04:54:30 +02:00
|
|
|
MARGIN = {0, 0, 0, 0}, --{left, top, right, bottom}
|
2022-04-11 18:47:26 +02:00
|
|
|
PADDING = {0, 0, 0, 0}, --{left, top, right, bottom}
|
|
|
|
}
|
|
|
|
|
|
|
|
---Create a new ListField instance
|
2022-05-06 01:02:30 +02:00
|
|
|
---@param params? ListField # initial parameters
|
2022-04-11 18:47:26 +02:00
|
|
|
---@return ListField
|
2022-05-06 01:02:30 +02:00
|
|
|
function ListField.new(params)
|
|
|
|
params = params or {}
|
2022-04-11 18:47:26 +02:00
|
|
|
|
|
|
|
--set instance members
|
2022-05-06 01:02:30 +02:00
|
|
|
params.selectedIndex = params.selectedIndex or 1
|
|
|
|
params.locked = params.locked or false
|
2022-04-11 18:47:26 +02:00
|
|
|
|
2022-05-06 01:02:30 +02:00
|
|
|
local self = CreateInstance(ListField, params, ContainerField, ServiceField)
|
2022-04-11 18:47:26 +02:00
|
|
|
|
2022-05-06 01:02:30 +02:00
|
|
|
local minW = self.MARGIN[1] + self.PADDING[1] + self.PADDING[3] + self.MARGIN[3]
|
|
|
|
local minH = self.MARGIN[2] + self.PADDING[2] + self.PADDING[4] + self.MARGIN[4]
|
|
|
|
self.aabbW = math.max(self.aabbW, minW)
|
|
|
|
self.aabbH = math.max(self.aabbH, minH)
|
2022-04-15 04:54:30 +02:00
|
|
|
|
2022-05-06 01:02:30 +02:00
|
|
|
return self
|
2022-04-12 23:45:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param obj? any # message object for the field
|
|
|
|
function ListField:focus(obj)
|
2022-06-29 00:35:52 +02:00
|
|
|
if self.focused then
|
2022-04-12 23:45:28 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if obj message received about direction of cursor movement
|
|
|
|
if obj and obj.direction then
|
|
|
|
if obj.direction > 0 then
|
|
|
|
self.selectedIndex = 1
|
|
|
|
elseif obj.direction < 0 then
|
|
|
|
self.selectedIndex = #self.content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- else try to figure out by comparing current selected index
|
|
|
|
if self.selectedIndex < 1 then
|
|
|
|
self.selectedIndex = 1
|
|
|
|
elseif self.selectedIndex > #self.content then
|
|
|
|
self.selectedIndex = #self.content
|
|
|
|
end
|
|
|
|
|
|
|
|
local field = self.content[self.selectedIndex]
|
|
|
|
if field and field.focus then
|
|
|
|
field:focus()
|
|
|
|
end
|
|
|
|
|
|
|
|
ServiceField.focus(self)
|
|
|
|
end
|
|
|
|
|
2022-06-29 00:35:52 +02:00
|
|
|
function ListField:unfocus()
|
|
|
|
if not self.focused then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local field = self.content[self.selectedIndex]
|
|
|
|
if field and field.unfocus then
|
|
|
|
field:unfocus()
|
|
|
|
end
|
|
|
|
|
|
|
|
ServiceField.unfocus(self)
|
|
|
|
end
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
---Add field to list container
|
|
|
|
---@param field Field
|
|
|
|
function ListField:addField(field)
|
|
|
|
--update size
|
2022-04-12 23:45:28 +02:00
|
|
|
self.aabbH = self.aabbH + field.aabbH
|
|
|
|
local fieldAabbW = self.PADDING[1] + field.aabbW + self.PADDING[3]
|
2022-04-11 18:47:26 +02:00
|
|
|
if self.aabbW < fieldAabbW then
|
|
|
|
self.aabbW = fieldAabbW
|
|
|
|
end
|
|
|
|
|
|
|
|
--add field to container
|
|
|
|
ContainerField.addField(self, field)
|
|
|
|
end
|
|
|
|
|
2022-04-12 23:45:28 +02:00
|
|
|
---Refresh content parameters
|
|
|
|
function ListField:refreshFields()
|
|
|
|
local aabbH = self.MARGIN[2] + self.PADDING[2] + self.PADDING[4] + self.MARGIN[4]
|
|
|
|
for _, child in ipairs(self.content) do
|
|
|
|
--update size
|
|
|
|
aabbH = aabbH + child.aabbH
|
|
|
|
local fieldAabbW = self.PADDING[1] + child.aabbW + self.PADDING[3]
|
|
|
|
if self.aabbW < fieldAabbW then
|
|
|
|
self.aabbW = fieldAabbW
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.aabbH = aabbH
|
|
|
|
|
|
|
|
ContainerField.refreshFields(self)
|
2022-04-11 18:47:26 +02:00
|
|
|
end
|
|
|
|
|
2022-04-12 23:45:28 +02:00
|
|
|
---@param button integer # options are under the `game` table prefixed with `BUTTON`
|
|
|
|
---@return boolean # true if further button input processing should be stopped, otherwise false
|
|
|
|
function ListField:handleButtonInput(button)
|
|
|
|
local field = self.content[self.selectedIndex]
|
2022-04-15 04:54:30 +02:00
|
|
|
if field:handleButtonInput(button) then
|
|
|
|
return true
|
2022-04-12 23:45:28 +02:00
|
|
|
end
|
|
|
|
|
2022-04-15 04:54:30 +02:00
|
|
|
if self.locked then
|
2022-06-29 00:35:52 +02:00
|
|
|
return false
|
2022-04-15 04:54:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local direction = 0
|
|
|
|
|
|
|
|
if button == game.BUTTON_BTA then
|
2022-04-12 23:45:28 +02:00
|
|
|
direction = -1
|
|
|
|
elseif button == game.BUTTON_BTB then
|
|
|
|
direction = 1
|
2022-04-11 18:47:26 +02:00
|
|
|
end
|
|
|
|
|
2022-04-12 23:45:28 +02:00
|
|
|
if direction ~= 0 then
|
2022-06-29 00:35:52 +02:00
|
|
|
field:unfocus()
|
2022-04-12 23:45:28 +02:00
|
|
|
|
|
|
|
self.selectedIndex = self.selectedIndex + direction
|
|
|
|
|
|
|
|
if self.selectedIndex < 1 or self.selectedIndex > #self.content then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
field = self.content[self.selectedIndex]
|
|
|
|
field:focus()
|
2022-06-29 00:35:52 +02:00
|
|
|
|
|
|
|
return true
|
2022-04-12 23:45:28 +02:00
|
|
|
end
|
|
|
|
|
2022-06-29 00:35:52 +02:00
|
|
|
return false
|
2022-04-12 23:45:28 +02:00
|
|
|
end
|
2022-04-11 18:47:26 +02:00
|
|
|
|
2022-04-15 04:54:30 +02:00
|
|
|
---@param deltaTime number # frametime in seconds
|
|
|
|
function ListField:drawContent(deltaTime)
|
|
|
|
gfx.Translate(self.PADDING[1] + self.MARGIN[1], self.PADDING[2] + self.MARGIN[2])
|
|
|
|
for _, child in ipairs(self.content) do
|
|
|
|
child:render(deltaTime)
|
|
|
|
gfx.Translate(0, child.aabbH)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
return ListField
|