medical/gui.lua

289 lines
7.6 KiB
Lua

--[[
Copyright 2025 Andrey Stepanov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
local S = minetest.get_translator("medical")
local ui = unified_inventory
local _contexts = {}
local function get_context(name)
local context = _contexts[name] or {}
_contexts[name] = context
return context
end
minetest.register_on_leaveplayer(
function(player)
_contexts[player:get_player_name()] = nil
end
)
local pfp = {
padding = 0.1,
group_btn_step = 0.9,
section_bg = "#22222266",
}
local function draw_group_btn(button, active, x)
local suffix = nil
if active == true then
suffix = "active"
else
suffix = "inactive"
end
local formspec = {
"image_button[",
x, ",0;",
"0.8,0.8;",
button.name, "_", suffix, ".png;",
button.name, ";",
";",
"false;",
"false;",
"]",
"tooltip[",
button.name, ";",
button.label,
"]"
}
return table.concat(formspec)
end
local groups = {
{
name = "triage_card",
label = S("Triage Card"),
get_formspec = function()
local formspec = {
"label[",
pfp.padding, ",", pfp.padding+0.2, ";",
S("No entries on this triage card."),
"]"
}
return table.concat(formspec)
end
},
{
name = "examine_patient",
label = S("Examine patient"),
},
{
name = "bandage_fracture",
label = S("Bandage / Fracture"),
},
{
name = "medication",
label = S("Medication"),
},
{
name = "advanced_treatment",
label = S("Advanced treatment"),
},
}
local function draw_groups(active_group_name, groups_width)
local formspec = {}
local active_group = nil
for i, group in ipairs(groups) do
local is_active = false
if active_group_name == group.name then
is_active = true
active_group = group
end
table.insert(formspec, draw_group_btn(group, is_active, pfp.padding+(i-1)*pfp.group_btn_step))
end
local groups_bg = {
"box[",
"0,", pfp.group_btn_step, ";",
groups_width - pfp.padding, ",3.2;",
pfp.section_bg,
"]"
}
table.insert(formspec, table.concat(groups_bg))
if active_group and active_group.get_formspec then
local container = {
"container[",
"0,", pfp.group_btn_step,
"]"
}
table.insert(
formspec,
table.concat(container)
)
table.insert(
formspec,
active_group.get_formspec()
)
table.insert(
formspec,
"container_end[]"
)
end
return table.concat(formspec)
end
local function draw_body_part(x, y, w, h, name, caption)
local formspec = {
"image_button[",
x, ",", y, ";",
w, ",", h, ";",
name, ".png;", name, ";;",
"false;false;",
"]",
"tooltip[",
name, ";",
caption,
"]"
}
return table.concat(formspec)
end
local function draw_body(center_x, scale)
local leg_w = scale * 0.8
local leg_offset = scale * 0.02
local formspec = {
draw_body_part(
center_x - scale/2, 0,
scale, scale,
"head", S("Head")
),
draw_body_part(
center_x - scale/2, scale,
scale, scale*2,
"torso", S("Torso")
),
draw_body_part(
center_x - scale*1.5, scale,
scale, scale*1.8,
"right_hand", S("Right Hand")
),
draw_body_part(
center_x + scale/2, scale,
scale, scale*1.8,
"left_hand", S("Left Hand")
),
draw_body_part(
center_x - leg_w - leg_offset, scale*3,
leg_w, scale*1.8,
"right_leg", S("Right Leg")
),
draw_body_part(
center_x + leg_offset, scale*3,
leg_w, scale*1.8,
"left_leg", S("Left Leg")
)
}
return table.concat(formspec)
end
ui.register_page(
"health",
{
get_formspec = function(player, perplayer_formspec)
local active_group_name = get_context(player:get_player_name()).active_group or groups[1].name
local fp = perplayer_formspec
local form_width = fp.formw - fp.form_header_x * 2
local section_width = form_width / 3
local formspec = {
perplayer_formspec.standard_inv_bg,
"container[",
fp.form_header_x, ",", fp.form_header_y, ";",
"]",
"box[",
"0,0;",
form_width, ",", 0.4, ";",
"orange",
"]",
"label[",
pfp.padding, ",0.2;",
player:get_player_name(),
"]",
"container[0,0.65]",
"label[",
pfp.padding, ",0;",
S("EXAMINE & TREATMENT"),
"]",
"label[",
section_width + pfp.padding, ",0;",
S("STATUS"),
"]",
"label[",
section_width * 2 + pfp.padding, ",0;",
S("OVERVIEW"),
"]",
"box[",
"0,0.14;",
form_width, ",0.02;",
"darkgray",
"]",
"container[0,0.35]",
draw_groups(active_group_name, section_width),
draw_body(section_width * 1.5, 0.8),
"box[",
section_width * 2, ",0;",
section_width, ",9;",
pfp.section_bg,
"]",
"container_end[]",
"container_end[]",
"container_end[]"
}
return {
formspec = table.concat(formspec),
draw_inventory = true,
draw_item_list = false
}
end
}
)
ui.register_button(
"health",
{
type = "image",
image = "redcross.png",
tooltip = S("Medical Menu")
}
)
minetest.register_on_player_receive_fields(
function(player, formname, fields)
if formname ~= "" then
return
end
for _, group in ipairs(groups) do
if fields[group.name] then
get_context(player:get_player_name()).active_group = group.name
ui.set_inventory_formspec(player, "health")
return
end
end
end
)