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 = { local pfp = {
padding = 0.1, padding = 0.1,
group_btn_w = 0.7,
group_btn_step = 0.9, group_btn_step = 0.9,
section_bg = "#22222266", section_bg = "#22222266",
body_scale = 0.8,
} }
@ -54,7 +52,7 @@ local function draw_group_btn(button, active, x)
local formspec = { local formspec = {
"image_button[", "image_button[",
x, ",0;", x, ",0;",
pfp.group_btn_w, ",", pfp.group_btn_w, ";", "0.8,0.8;",
button.name, "_", suffix, ".png;", button.name, "_", suffix, ".png;",
button.name, ";", button.name, ";",
";", ";",
@ -71,75 +69,134 @@ local function draw_group_btn(button, active, x)
end end
local function group_default_formspec(perplayer_formspec) local groups = {
local fp = perplayer_formspec {
local form_width = (fp.formw - fp.form_header_x * 2) / 3 name = "triage_card",
local formspec = { 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[", "box[",
"0,0;", "0,", pfp.group_btn_step, ";",
form_width - pfp.padding, ",3.2;", groups_width - pfp.padding, ",3.2;",
pfp.section_bg, 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) return table.concat(formspec)
end 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_body(center_x, scale)
local function draw_groups(active_group, perplayer_formspec) local leg_w = scale * 0.8
local formspec = {} local leg_offset = scale * 0.02
local formspec = {
for i, button in ipairs(group_buttons) do draw_body_part(
local is_active = false center_x - scale/2, 0,
if active_group == button.name then scale, scale,
is_active = true "head", S("Head")
end ),
table.insert(formspec, draw_group_btn(button, is_active, pfp.padding+(i-1)*pfp.group_btn_step)) draw_body_part(
end center_x - scale/2, scale,
local container = { scale, scale*2,
"container[", "body", S("Body")
"0,", pfp.group_btn_step, ),
"]" 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) return table.concat(formspec)
end end
@ -148,14 +205,11 @@ ui.register_page(
"health", "health",
{ {
get_formspec = function(player, perplayer_formspec) 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 fp = perplayer_formspec
local form_width = fp.formw - fp.form_header_x * 2 local form_width = fp.formw - fp.form_header_x * 2
local section_width = form_width / 3 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 = { local formspec = {
perplayer_formspec.standard_inv_bg, perplayer_formspec.standard_inv_bg,
"container[", "container[",
@ -189,70 +243,8 @@ ui.register_page(
"darkgray", "darkgray",
"]", "]",
"container[0,0.35]", "container[0,0.35]",
draw_groups( draw_groups(active_group_name, section_width),
active_group, draw_body(section_width * 1.5, 0.8),
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"),
"]",
"box[", "box[",
section_width * 2, ",0;", section_width * 2, ",0;",
section_width, ",9;", section_width, ",9;",
@ -286,9 +278,9 @@ core.register_on_player_receive_fields(
return return
end end
for _, btn in ipairs(group_buttons) do for _, group in ipairs(groups) do
if fields[btn.name] then if fields[group.name] then
get_context(player:get_player_name()).active_group = btn.name get_context(player:get_player_name()).active_group = group.name
ui.set_inventory_formspec(player, "health") ui.set_inventory_formspec(player, "health")
return return
end end

View File

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