refactor formspec

This commit is contained in:
Andrey Stepanov 2025-01-12 23:42:22 +05:00
parent a74ab51323
commit cf81ed8e18
Signed by: Koldun
GPG Key ID: 53DE683337F5D25F
2 changed files with 127 additions and 134 deletions

260
init.lua
View File

@ -36,10 +36,8 @@ core.register_on_leaveplayer(
local pfp = {
padding = 0.1,
group_btn_w = 0.7,
group_btn_step = 0.9,
section_bg = "#22222266",
body_scale = 0.8,
}
@ -54,7 +52,7 @@ local function draw_group_btn(button, active, x)
local formspec = {
"image_button[",
x, ",0;",
pfp.group_btn_w, ",", pfp.group_btn_w, ";",
"0.8,0.8;",
button.name, "_", suffix, ".png;",
button.name, ";",
";",
@ -71,75 +69,134 @@ local function draw_group_btn(button, active, x)
end
local function group_default_formspec(perplayer_formspec)
local fp = perplayer_formspec
local form_width = (fp.formw - fp.form_header_x * 2) / 3
local formspec = {
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,0;",
form_width - pfp.padding, ",3.2;",
"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 group_buttons = {
{
name = "triage_card",
label = S("Triage Card"),
get_formspec = group_default_formspec,
},
{
name = "examine_patient",
label = S("Examine patient"),
get_formspec = group_default_formspec,
},
{
name = "bandage_fracture",
label = S("Bandage & Fracture"),
get_formspec = group_default_formspec,
},
{
name = "medication",
label = S("Medication"),
get_formspec = group_default_formspec,
},
{
name = "advanced_treatment",
label = S("Advanced treatment"),
get_formspec = group_default_formspec,
},
}
local function draw_groups(active_group, perplayer_formspec)
local formspec = {}
for i, button in ipairs(group_buttons) do
local is_active = false
if active_group == button.name then
is_active = true
end
table.insert(formspec, draw_group_btn(button, is_active, pfp.padding+(i-1)*pfp.group_btn_step))
end
local container = {
"container[",
"0,", pfp.group_btn_step,
"]"
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,
"body", S("Body")
),
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")
)
}
table.insert(
formspec,
table.concat(container)
)
table.insert(
formspec,
group_default_formspec(perplayer_formspec)
)
table.insert(
formspec,
"container_end[]"
)
return table.concat(formspec)
end
@ -148,14 +205,11 @@ ui.register_page(
"health",
{
get_formspec = function(player, perplayer_formspec)
local active_group = get_context(player:get_player_name()).active_group or group_buttons[1].name
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 body_center = section_width * 1.5
local leg_w = pfp.body_scale * 0.8
local leg_offset = pfp.body_scale * 0.02
local formspec = {
perplayer_formspec.standard_inv_bg,
"container[",
@ -189,70 +243,8 @@ ui.register_page(
"darkgray",
"]",
"container[0,0.35]",
draw_groups(
active_group,
perplayer_formspec
),
"image_button[",
body_center - pfp.body_scale/2, ",0;",
pfp.body_scale, ",", pfp.body_scale, ";",
"head.png;head;;",
"false;false;",
"]",
"tooltip[",
"head;",
S("Head"),
"]",
"image_button[",
body_center - pfp.body_scale/2, ",", pfp.body_scale, ";",
pfp.body_scale, ",", pfp.body_scale*2, ";",
"body.png;body;;",
"false;false;",
"]",
"tooltip[",
"body;",
S("Body"),
"]",
"image_button[",
body_center - pfp.body_scale*1.5, ",", pfp.body_scale, ";",
pfp.body_scale, ",", pfp.body_scale*1.8, ";",
"right_hand.png;right_hand;;",
"false;false;",
"]",
"tooltip[",
"right_hand;",
S("Right Hand"),
"]",
"image_button[",
body_center + pfp.body_scale/2, ",", pfp.body_scale, ";",
pfp.body_scale, ",", pfp.body_scale*1.8, ";",
"left_hand.png;left_hand;;",
"false;false;",
"]",
"tooltip[",
"left_hand;",
S("Left Hand"),
"]",
"image_button[",
body_center - leg_w - leg_offset, ",", pfp.body_scale*3, ";",
leg_w, ",", pfp.body_scale*1.8, ";",
"right_leg.png;right_leg;;",
"false;false;",
"]",
"tooltip[",
"right_leg;",
S("Right Leg"),
"]",
"image_button[",
body_center + leg_offset, ",", pfp.body_scale*3, ";",
leg_w, ",", pfp.body_scale*1.8, ";",
"left_leg.png;left_leg;;",
"false;false;",
"]",
"tooltip[",
"left_leg;",
S("Left Leg"),
"]",
draw_groups(active_group_name, section_width),
draw_body(section_width * 1.5, 0.8),
"box[",
section_width * 2, ",0;",
section_width, ",9;",
@ -286,9 +278,9 @@ core.register_on_player_receive_fields(
return
end
for _, btn in ipairs(group_buttons) do
if fields[btn.name] then
get_context(player:get_player_name()).active_group = btn.name
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

View File

@ -14,3 +14,4 @@ Right Hand=Правая рука
Left Hand=Левая рука
Right Leg=Правая нога
Left Leg=Левая нога
No entries on this triage card.=Нет информации.