classroom/gui.lua

124 lines
2.8 KiB
Lua

local infos = {
{
title = "Shout?",
type = "priv",
privs = { shout = true },
},
{
title = "Fly?",
type = "priv",
privs = { fly = true },
},
{
title = "Fast?",
type = "priv",
privs = { fast = true },
},
}
local function get_group(context)
if context and context.groupname then
return classroom.get_group_students(context.groupname)
else
return classroom.get_students()
end
end
sfinv.register_page("edu", {
title = "Classroom",
get = function(self, player, context)
local fs = {
"tablecolumns[color;text"
}
context.select_toggle = context.select_toggle or "all"
for i, col in pairs(infos) do
fs[#fs + 1] = ";color;text,align=center"
if i == 1 then
fs[#fs + 1] = ",padding=2"
end
end
fs[#fs + 1] = "]"
fs[#fs + 1] = "tabheader[0.3,1.1;group;All,Group 1;1]"
fs[#fs + 1] = "table[0,0.8;5,8;kids;,Name"
for _, col in pairs(infos) do
fs[#fs + 1] = ",," .. col.title
end
for _, student in pairs(get_group(context)) do
fs[#fs + 1] = ",,"
fs[#fs + 1] = minetest.formspec_escape(student)
for _, col in pairs(infos) do
local color, value
if col.type == "priv" then
local has_priv = minetest.check_player_privs(student, col.privs)
color = has_priv and "green" or "red"
value = has_priv and "Yes" or "No"
end
fs[#fs + 1] = ","
fs[#fs + 1] = color
fs[#fs + 1] = ","
fs[#fs + 1] = minetest.formspec_escape(value)
end
end
fs[#fs + 1] = ";2]"
fs[#fs + 1] = "container[5.25,0]"
local x = 0
local y = 0.8
for aname, action in pairs(classroom.get_actions()) do
fs[#fs + 1] = "button["
fs[#fs + 1] = tostring(x)
fs[#fs + 1] = ","
fs[#fs + 1] = tostring(y)
fs[#fs + 1] = ";1,1;action_"
fs[#fs + 1] = aname
fs[#fs + 1] = ";"
fs[#fs + 1] = minetest.formspec_escape(action.title)
fs[#fs + 1] = "]"
fs[#fs + 1] = "tooltip[action_"
fs[#fs + 1] = aname
fs[#fs + 1] = ";"
fs[#fs + 1] = minetest.formspec_escape(action.description)
fs[#fs + 1] = "]"
if x < 1.5 then
x = x + 0.9
else
x = 0
y = y + 0.8
end
end
fs[#fs + 1] = "container_end[]"
return sfinv.make_formspec(player, context, table.concat(fs, ""), false)
end,
on_player_receive_fields = function(self, player, context, fields)
for aname, action in pairs(classroom.get_actions()) do
if fields["action_" .. aname] then
local selector
if context.select_toggle == "all" then
selector = "*"
elseif context.select_toggle == "group" then
selector = "group:" .. context.groupname
elseif context.select_toggle == "selected" then
error("Unimplemented")
else
error("Unknown selector")
end
classroom.run_action(aname, player, selector)
sfinv.set_player_inventory_formspec(player)
return true
end
end
end,
})