epervier-old/birb/modules/gui/menus/flowbox.lua

149 lines
4.4 KiB
Lua
Raw Normal View History

2019-04-10 18:01:46 +02:00
-- flowbox :: flexible box menu, that handle in grid the 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.
]]
local cwd = (...):gsub('%.flowbox$', '') .. "."
2019-04-10 18:01:46 +02:00
local Menu = require(cwd .. "parent")
local FlowBox = Menu:extend()
2020-12-04 13:16:32 +01:00
local View2D = require(cwd .. "views.view2D")
2019-04-10 18:01:46 +02:00
-- INIT FUNCTIONS
-- Initialize and configure the flowbox
2021-12-04 13:18:54 +01:00
function FlowBox:new(name, x, y, w, h, slots_hor, slots_vert)
2020-12-04 12:57:24 +01:00
self.view = View2D(slots_hor, slots_vert)
2021-12-04 13:18:54 +01:00
FlowBox.super.new(self, name, x, y, w, h)
2020-12-04 12:57:24 +01:00
self:setRealSize()
end
function FlowBox:setRealSize()
-- On fait en sorte que la hauteur et la largeur
-- soit un multiple du nombre de slot et de leur dimensions
2020-12-04 12:57:24 +01:00
self:updateWidgetSize()
self.w = self.view.colNumber * self.widgetSize.w
self.h = self.view.lineNumber * self.widgetSize.h
end
2021-01-29 21:54:34 +01:00
function FlowBox:resetView()
self.view:reset()
end
2019-04-11 16:58:42 +02:00
-- UPDATE FUNCTIONS
-- Update the menu and its view
function FlowBox:updateWidgetSize()
2020-12-04 12:57:24 +01:00
self.widgetSize.h = math.floor( self.h / self.view.lineNumber )
self.widgetSize.w = math.floor( self.w / self.view.colNumber )
end
function FlowBox:update(dt)
2020-12-04 12:57:24 +01:00
self.view:updateFirstSlot(self.widget:getSelected())
end
2019-04-11 16:58:42 +02:00
-- CURSOR FUNCTIONS
-- Handle the cursor in a 2D menu
2020-12-04 12:57:24 +01:00
function FlowBox:moveCursor2D(new_col, new_line)
local lastcol, lastline = self.view:getCoord(self.widget:lenght())
2020-12-04 12:57:24 +01:00
new_line = utils.math.wrapAndLimit(new_line, 0, lastline)
if (new_line == lastline) then
2020-12-04 12:57:24 +01:00
new_col = utils.math.wrapAndLimit(new_col, 0, lastcol)
else
2020-12-04 12:57:24 +01:00
new_col = utils.math.wrapAndLimit(new_col, 0, (self.view.colNumber - 1))
end
2020-12-04 12:57:24 +01:00
self.widget:moveCursor((new_line * self.view.colNumber) + new_col + 1)
end
2019-04-11 16:58:42 +02:00
-- KEYS FUNCTIONS
-- Handle the keyboard/controller inputs
2020-12-04 12:57:24 +01:00
function FlowBox:moveByKeys(key)
local col, line = self.view:getCoord(self.widget:getSelected())
if key == 'left' then
2020-12-04 12:57:24 +01:00
self:moveCursor2D(col - 1, line)
2021-12-04 13:18:54 +01:00
self:playNavigationSound()
end
if key == 'right' then
2020-12-04 12:57:24 +01:00
self:moveCursor2D(col + 1, line)
2021-12-04 13:18:54 +01:00
self:playNavigationSound()
end
if key == 'up' then
2020-12-04 12:57:24 +01:00
self:moveCursor2D(col, line - 1)
2021-12-04 13:18:54 +01:00
self:playNavigationSound()
end
if key == 'down' then
2020-12-04 12:57:24 +01:00
self:moveCursor2D(col, line + 1)
2021-12-04 13:18:54 +01:00
self:playNavigationSound()
end
end
2020-12-04 12:57:24 +01:00
-- POSITION FUNCTIONS
-- Get a widget by a position.
2020-12-04 12:57:24 +01:00
function FlowBox:getWidgetAtPoint(x, y)
local col = math.floor(x / self.widgetSize.w)
local line = math.floor(y / self.widgetSize.h)
return self.view:getFromCoord(col, line)
end
2019-04-10 18:01:46 +02:00
-- DRAW FUNCTIONS
-- Draw the menu and its content
2021-12-04 13:18:54 +01:00
function FlowBox:drawTexture()
2020-12-04 12:57:24 +01:00
self.view:updateFirstSlot(self.widget:getSelected())
2021-12-04 13:18:54 +01:00
local widgety = self.canvas.padding
local widgetx = self.canvas.padding
2020-12-04 12:57:24 +01:00
local listWidget = self.widget:getList(self.view.firstSlot, self.view.slotNumber)
for _, widget in ipairs(listWidget) do
widget:drawWidget(widgetx, widgety, self.w, self.widgetSize.h)
-- On calcule la position du prochain widget
widgetx = widgetx + self.widgetSize.w
if widgetx >= (self.x + self.w) then
widgetx = self.x
2020-12-04 12:57:24 +01:00
widgety = widgety + self.widgetSize.h
end
end
end
2020-12-04 12:57:24 +01:00
function FlowBox:getGraphicalCursorPosition()
self.view:updateFirstSlot(self.widget:getSelected())
local _, beginline = self.view:getCoord(self.view.firstSlot)
local w, h = self:getWidgetSize()
local col, line = self.view:getCoord(self.widget:getSelected())
local x = (col) * h
local y = (line - beginline) * h
2022-07-27 21:57:34 +02:00
return x, y, w, h
end
return FlowBox