2019-04-10 18:01:46 +02:00
|
|
|
-- listbox : add a vertical list of widgets.
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Copyright © 2019 Kazhnuz
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
local cwd = (...):gsub('%.listbox$', '') .. "."
|
|
|
|
local Menu = require(cwd .. "parent")
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
local ListBox = Menu:extend()
|
|
|
|
|
2020-12-04 13:16:32 +01:00
|
|
|
local View1D = require(cwd .. "views.view1D")
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialize and configure functions.
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
function ListBox:new(name, x, y, w, h, slotNumber)
|
2020-12-04 12:57:24 +01:00
|
|
|
self.view = View1D(slotNumber)
|
2021-12-04 13:18:54 +01:00
|
|
|
ListBox.super.new(self, name, x, y, w, h)
|
2020-12-04 12:57:24 +01:00
|
|
|
self.h = slotNumber * self.widgetSize.h -- On fait en sorte que la hauteur
|
2019-03-16 12:27:38 +01:00
|
|
|
-- soit un multiple du nombre de slot et de leur hauteur
|
2022-07-27 21:57:34 +02:00
|
|
|
self.lateralFunc = nil
|
|
|
|
self.packAtEnd = false
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
-- Update the menu every step.
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function ListBox:updateWidgetSize()
|
2020-12-04 12:57:24 +01:00
|
|
|
self.widgetSize.h = math.floor( self.h / self.view.slotNumber )
|
|
|
|
self.widgetSize.w = self.w
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function ListBox:update(dt)
|
2020-12-04 12:57:24 +01:00
|
|
|
self.view:updateFirstSlot(self.widget:getSelected())
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2021-01-29 21:54:34 +01:00
|
|
|
function ListBox:resetView()
|
|
|
|
self.view:reset()
|
|
|
|
end
|
|
|
|
|
2022-07-27 21:57:34 +02:00
|
|
|
function ListBox:addLateralAction(func)
|
|
|
|
self.lateralFunc = func
|
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- KEYBOARD FUNCTIONS
|
|
|
|
-- Handle input from keyboard/controllers.
|
|
|
|
|
2020-12-04 12:57:24 +01:00
|
|
|
function ListBox:moveByKeys(key)
|
2019-03-16 12:27:38 +01:00
|
|
|
if key == 'up' then
|
2020-12-04 12:57:24 +01:00
|
|
|
self.widget:moveCursor(-1)
|
2021-12-04 13:18:54 +01:00
|
|
|
self:playNavigationSound()
|
|
|
|
self.canvas.needRedraw = true
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
if key == 'down' then
|
2020-12-04 12:57:24 +01:00
|
|
|
self.widget:moveCursor(1)
|
2021-12-04 13:18:54 +01:00
|
|
|
self:playNavigationSound()
|
|
|
|
self.canvas.needRedraw = true
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
2022-07-27 21:57:34 +02:00
|
|
|
|
|
|
|
if (self.lateralFunc ~= nil and (key == 'left' or key == 'right')) then
|
|
|
|
self.widget:lateralAction(self.lateralFunc, key)
|
|
|
|
end
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2020-12-04 12:57:24 +01:00
|
|
|
-- POSITION FUNCTIONS
|
|
|
|
-- Get a widget by a position.
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2020-12-04 12:57:24 +01:00
|
|
|
function ListBox:getWidgetAtPoint(x, y)
|
|
|
|
return (self.view.firstSlot + math.floor(y / self.widgetSize.h))
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2019-04-10 18:01:46 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- draw the menu and the rest of content.
|
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
function ListBox:getListPart(relativeNumber)
|
2022-07-27 21:57:34 +02:00
|
|
|
if (self.packAtEnd) then relativeNumber = relativeNumber + math.max(0, self.view.slotNumber - self.widget:lenght()) end
|
2021-12-04 13:18:54 +01:00
|
|
|
return 0, (relativeNumber) * self.widgetSize.h, self.w, self.widgetSize.h
|
|
|
|
end
|
2020-12-04 12:57:24 +01:00
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
function ListBox:drawTexture()
|
|
|
|
self.view:updateFirstSlot(self.widget:getSelected())
|
2020-12-04 12:57:24 +01:00
|
|
|
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
|
|
|
|
|
2021-12-04 13:18:54 +01:00
|
|
|
for i, widget in ipairs(listWidget) do
|
|
|
|
local x, y, w, h = self:getListPart(i - 1)
|
|
|
|
self:drawWidgetBackground(x + self.canvas.padding, y + self.canvas.padding, w, h)
|
|
|
|
widget:drawWidget(x + self.canvas.padding, y + self.canvas.padding, w, h)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-04 12:57:24 +01:00
|
|
|
function ListBox:getGraphicalCursorPosition()
|
2021-12-04 13:18:54 +01:00
|
|
|
local x, y, w, h = self:getListPart(self.widget:getSelected() - self.view.firstSlot)
|
2020-12-04 12:57:24 +01:00
|
|
|
|
2022-07-27 21:57:34 +02:00
|
|
|
return self:getListPart(self.widget:getSelected() - self.view.firstSlot)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return ListBox
|