2014-04-18 15:39:15 +02:00
|
|
|
--Minetest
|
|
|
|
--Copyright (C) 2014 sapier
|
2023-09-02 23:02:02 +02:00
|
|
|
--Copyright (C) 2023 Gregor Parzefall
|
2014-04-18 15:39:15 +02:00
|
|
|
--
|
2015-09-21 20:03:35 +02:00
|
|
|
--This program is free software; you can redistribute it and/or modify
|
2014-04-18 15:39:15 +02:00
|
|
|
--it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
--the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
--(at your option) any later version.
|
|
|
|
--
|
2015-09-21 20:03:35 +02:00
|
|
|
--This program is distributed in the hope that it will be useful,
|
2014-04-18 15:39:15 +02:00
|
|
|
--but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
--GNU Lesser General Public License for more details.
|
|
|
|
--
|
|
|
|
--You should have received a copy of the GNU Lesser General Public License along
|
2015-09-21 20:03:35 +02:00
|
|
|
--with this program; if not, write to the Free Software Foundation, Inc.,
|
2014-04-18 15:39:15 +02:00
|
|
|
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
local BASE_SPACING = 0.1
|
2024-02-22 16:44:49 +01:00
|
|
|
local function get_scroll_btn_width()
|
|
|
|
return core.settings:get_bool("enable_touch") and 0.8 or 0.5
|
|
|
|
end
|
2014-04-18 15:39:15 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
local function buttonbar_formspec(self)
|
2014-04-18 15:39:15 +02:00
|
|
|
if self.hidden then
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
local formspec = {
|
|
|
|
"style_type[box;noclip=true]",
|
|
|
|
string.format("box[%f,%f;%f,%f;%s]", self.pos.x, self.pos.y, self.size.x,
|
|
|
|
self.size.y, self.bgcolor),
|
|
|
|
"style_type[box;noclip=false]",
|
|
|
|
}
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
local btn_size = self.size.y - 2*BASE_SPACING
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
-- Spacing works like CSS Flexbox with `justify-content: space-evenly;`.
|
|
|
|
-- `BASE_SPACING` is used as the minimum spacing, like `gap` in CSS Flexbox.
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
-- The number of buttons per page is always calculated as if the scroll
|
|
|
|
-- buttons were visible.
|
2024-02-22 16:44:49 +01:00
|
|
|
local avail_space = self.size.x - 2*BASE_SPACING - 2*get_scroll_btn_width()
|
2023-09-02 23:02:02 +02:00
|
|
|
local btns_per_page = math.floor((avail_space - BASE_SPACING) / (btn_size + BASE_SPACING))
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
self.num_pages = math.ceil(#self.buttons / btns_per_page)
|
|
|
|
self.cur_page = math.min(self.cur_page, self.num_pages)
|
|
|
|
local first_btn = (self.cur_page - 1) * btns_per_page + 1
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
local show_scroll_btns = self.num_pages > 1
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
-- In contrast, the button spacing calculation takes hidden scroll buttons
|
|
|
|
-- into account.
|
|
|
|
local real_avail_space = show_scroll_btns and avail_space or self.size.x
|
|
|
|
local btn_spacing = (real_avail_space - btns_per_page * btn_size) / (btns_per_page + 1)
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
local btn_start_x = self.pos.x + btn_spacing
|
|
|
|
if show_scroll_btns then
|
2024-02-22 16:44:49 +01:00
|
|
|
btn_start_x = btn_start_x + BASE_SPACING + get_scroll_btn_width()
|
2014-04-18 15:39:15 +02:00
|
|
|
end
|
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
for i = first_btn, first_btn + btns_per_page - 1 do
|
|
|
|
local btn = self.buttons[i]
|
|
|
|
if btn == nil then
|
|
|
|
break
|
2014-04-18 15:39:15 +02:00
|
|
|
end
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
local btn_pos = {
|
|
|
|
x = btn_start_x + (i - first_btn) * (btn_size + btn_spacing),
|
|
|
|
y = self.pos.y + BASE_SPACING,
|
|
|
|
}
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
table.insert(formspec, string.format("image_button[%f,%f;%f,%f;%s;%s;%s;true;false]tooltip[%s;%s]",
|
|
|
|
btn_pos.x, btn_pos.y, btn_size, btn_size, btn.image, btn.name,
|
|
|
|
btn.caption, btn.name, btn.tooltip))
|
|
|
|
end
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
if show_scroll_btns then
|
|
|
|
local btn_prev_pos = {
|
|
|
|
x = self.pos.x + BASE_SPACING,
|
|
|
|
y = self.pos.y + BASE_SPACING,
|
|
|
|
}
|
|
|
|
local btn_next_pos = {
|
2024-02-22 16:44:49 +01:00
|
|
|
x = self.pos.x + self.size.x - BASE_SPACING - get_scroll_btn_width(),
|
2023-09-02 23:02:02 +02:00
|
|
|
y = self.pos.y + BASE_SPACING,
|
|
|
|
}
|
|
|
|
|
|
|
|
table.insert(formspec, string.format("style[%s,%s;noclip=true]",
|
|
|
|
self.btn_prev_name, self.btn_next_name))
|
|
|
|
|
|
|
|
table.insert(formspec, string.format("button[%f,%f;%f,%f;%s;<]",
|
2024-02-22 16:44:49 +01:00
|
|
|
btn_prev_pos.x, btn_prev_pos.y, get_scroll_btn_width(), btn_size,
|
2023-09-02 23:02:02 +02:00
|
|
|
self.btn_prev_name))
|
|
|
|
|
|
|
|
table.insert(formspec, string.format("button[%f,%f;%f,%f;%s;>]",
|
2024-02-22 16:44:49 +01:00
|
|
|
btn_next_pos.x, btn_next_pos.y, get_scroll_btn_width(), btn_size,
|
2023-09-02 23:02:02 +02:00
|
|
|
self.btn_next_name))
|
2014-04-18 15:39:15 +02:00
|
|
|
end
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
return table.concat(formspec)
|
2014-04-18 15:39:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function buttonbar_buttonhandler(self, fields)
|
2023-09-02 23:02:02 +02:00
|
|
|
if fields[self.btn_prev_name] and self.cur_page > 1 then
|
|
|
|
self.cur_page = self.cur_page - 1
|
2014-04-18 15:39:15 +02:00
|
|
|
return true
|
|
|
|
end
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
if fields[self.btn_next_name] and self.cur_page < self.num_pages then
|
|
|
|
self.cur_page = self.cur_page + 1
|
2014-04-18 15:39:15 +02:00
|
|
|
return true
|
|
|
|
end
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
for _, btn in ipairs(self.buttons) do
|
|
|
|
if fields[btn.name] then
|
2014-04-18 15:39:15 +02:00
|
|
|
return self.userbuttonhandler(fields)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local buttonbar_metatable = {
|
|
|
|
handle_buttons = buttonbar_buttonhandler,
|
|
|
|
handle_events = function(self, event) end,
|
|
|
|
get_formspec = buttonbar_formspec,
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2014-04-18 15:39:15 +02:00
|
|
|
hide = function(self) self.hidden = true end,
|
|
|
|
show = function(self) self.hidden = false end,
|
|
|
|
|
|
|
|
delete = function(self) ui.delete(self) end,
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2014-10-12 19:50:33 +02:00
|
|
|
add_button = function(self, name, caption, image, tooltip)
|
2023-09-02 23:02:02 +02:00
|
|
|
if caption == nil then caption = "" end
|
|
|
|
if image == nil then image = "" end
|
|
|
|
if tooltip == nil then tooltip = "" end
|
|
|
|
|
|
|
|
table.insert(self.buttons, {
|
|
|
|
name = name,
|
|
|
|
caption = caption,
|
|
|
|
image = image,
|
|
|
|
tooltip = tooltip,
|
|
|
|
})
|
|
|
|
end,
|
2014-04-18 15:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buttonbar_metatable.__index = buttonbar_metatable
|
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
function buttonbar_create(name, pos, size, bgcolor, cbf_buttonhandler)
|
|
|
|
assert(type(name) == "string" )
|
|
|
|
assert(type(pos) == "table" )
|
|
|
|
assert(type(size) == "table" )
|
|
|
|
assert(type(bgcolor) == "string" )
|
|
|
|
assert(type(cbf_buttonhandler) == "function")
|
2014-04-18 15:39:15 +02:00
|
|
|
|
|
|
|
local self = {}
|
|
|
|
self.type = "addon"
|
2023-09-02 23:02:02 +02:00
|
|
|
self.name = name
|
2014-04-18 15:39:15 +02:00
|
|
|
self.pos = pos
|
|
|
|
self.size = size
|
2023-09-02 23:02:02 +02:00
|
|
|
self.bgcolor = bgcolor
|
2014-04-18 15:39:15 +02:00
|
|
|
self.userbuttonhandler = cbf_buttonhandler
|
2023-09-02 23:02:02 +02:00
|
|
|
|
|
|
|
self.hidden = false
|
2014-04-18 15:39:15 +02:00
|
|
|
self.buttons = {}
|
2023-09-02 23:02:02 +02:00
|
|
|
self.num_pages = 1
|
|
|
|
self.cur_page = 1
|
|
|
|
|
|
|
|
self.btn_prev_name = "btnbar_prev_" .. self.name
|
|
|
|
self.btn_next_name = "btnbar_next_" .. self.name
|
2015-09-21 20:03:35 +02:00
|
|
|
|
2023-09-02 23:02:02 +02:00
|
|
|
setmetatable(self, buttonbar_metatable)
|
2014-04-18 15:39:15 +02:00
|
|
|
|
|
|
|
ui.add(self)
|
|
|
|
return self
|
|
|
|
end
|