attempt to fix scrolling in formspec mode 2 and below

This commit is contained in:
Sokomine 2022-09-11 03:33:59 +02:00
parent db5a67b333
commit 19625b6d3a
3 changed files with 56 additions and 18 deletions

View File

@ -27,15 +27,21 @@ end
-- older formspecs (before v3) do not offer a scroll container and have to scroll manually;
-- we maintain a player-name-based counter in order to see if this line ought to be shown
yl_speak_up.old_fs_version_show_line = function(pname)
yl_speak_up.old_fs_version_show_line = function(pname, anz_lines)
-- the player is using a new enough version for scroll_container to work
if(not(pname)) then
return true
end
if(not(anz_lines) or anz_lines < 1) then
anz_lines = 1
end
local max_number_of_buttons = yl_speak_up.max_number_of_buttons
local start_index = yl_speak_up.speak_to[pname].option_index
local counter = yl_speak_up.speak_to[pname].counter
yl_speak_up.speak_to[pname].counter = counter + 1
if(not(counter)) then
counter = 1
end
yl_speak_up.speak_to[pname].counter = counter + anz_lines
if counter < start_index or counter >= start_index + max_number_of_buttons then
return false
end
@ -46,26 +52,35 @@ end
-- show an edit option in the main menu of the NPC;
-- helper function for yl_speak_up.fs_talkdialog(..)
-- and yl_speak_up.show_fs_decorated
-- (optional) anz_lines: trade offers take up more room
-- (optional) multi_line_content: for trade offers; draw them directly
yl_speak_up.add_edit_button_fs_talkdialog = function(formspec, h, button_name, tooltip, label,
show_main_not_alternate, alternate_label, is_exit_button,
pname)
pname, anz_lines, multi_line_content)
if(not(anz_lines) or anz_lines < 1) then
anz_lines = 1
end
-- do not show this button at all if there is no alternate text and the condition is false
if(not(alternate_label) and not(show_main_not_alternate)) then
return h
end
local button_dimensions = "0.5,"..(h+1)..";53.8,0.9;"
local button_dimensions = "0.5,"..(h+1)..";53.8,"..tostring(0.9*anz_lines)..";"
local label_start_pos = "0.7"
-- older formspecs (before v4) do not offer a scroll container and have to scroll manually
if(pname) then
if(not(yl_speak_up.old_fs_version_show_line(pname))) then
if(not(yl_speak_up.old_fs_version_show_line(pname, anz_lines))) then
return h
end
-- there has to be more room for the up and down arrows
button_dimensions = "1.2,"..(h+1)..";52.3,0.9;"
button_dimensions = "1.2,"..(h+1)..";52.3,"..tostring(0.9*anz_lines)..";"
label_start_pos = "1.4"
end
h = h + 1
if(show_main_not_alternate) then
h = h + anz_lines
if(multi_line_content) then
table.insert(formspec, "container[1.2,"..(h+1 - anz_lines).."]")
table.insert(formspec, multi_line_content)
table.insert(formspec, "container_end[]")
elseif(show_main_not_alternate) then
if(is_exit_button) then
table.insert(formspec, "button_exit["..button_dimensions..tostring(button_name)..";]")
else

View File

@ -792,7 +792,7 @@ yl_speak_up.get_fs_talkdialog = function(player, n_id, d_id, alternate_text, rec
local oid = minetest.formspec_escape(sb_v.o_id)
local res = {}
-- in edit_mode: show all options
if(edit_mode and yl_speak_up.old_fs_version_show_line(pname_for_old_fs)) then
if(edit_mode) then
res = yl_speak_up.get_fs_talkdialog_line_in_edit_mode(
formspec, h, pname_for_old_fs, oid, sb_v,
dialog, active_dialog, dialog_list, d_id_to_dropdown_index)

View File

@ -12,6 +12,22 @@ yl_speak_up.input_trade_list = function(player, formname, fields)
dialog.trades = {}
end
-- pressing up and down buttons needs to be handled here
if(fields.button_up) then
yl_speak_up.speak_to[pname].option_index =
yl_speak_up.speak_to[pname].option_index + yl_speak_up.max_number_of_buttons
yl_speak_up.show_fs(player, "trade_list", fields.show_dialog_option_trades)
return
elseif(fields.button_down) then
yl_speak_up.speak_to[pname].option_index =
yl_speak_up.speak_to[pname].option_index - yl_speak_up.max_number_of_buttons
if yl_speak_up.speak_to[pname].option_index < 0 then
yl_speak_up.speak_to[pname].option_index = 1
end
yl_speak_up.show_fs(player, "trade_list", fields.show_dialog_option_trades)
return
end
-- the player wants to add a new trade
if(fields.trade_list_add_trade) then
-- show the trade config dialog for a new trade
@ -115,6 +131,7 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades)
-- how much stock does the NPC have?
local counted_npc_inv = yl_speak_up.count_npc_inv(n_id)
local row_content = {}
for i, k in ipairs(sorted_trades) do
local v = dialog.trades[ k ]
-- needs both to be negated because show_dialog_option_trades will most of the time be nil
@ -150,9 +167,8 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades)
-- -- indicate that the shop will never run out of stock with a special color
-- color = "#999999"
end
table.insert(formspec,
"container["..tostring(0.5+(col*4.5))..","..
tostring(0.3+(row*6)).."]"..
table.insert(row_content,
"container["..((col*4.5)-0.6)..",0]"..
"style_type[button;bgcolor=#FF4444]"..
"box[0,0;4.2,5.6;"..color.."]"..
"item_image_button[1.0,0.2;3,3;"..
@ -163,28 +179,35 @@ yl_speak_up.get_fs_trade_list = function(player, show_dialog_option_trades)
-- to the same result) just above
tostring(v.pay[1])..";"..kstr.."_;]")
if(sold_out) then
-- table.insert(formspec, "label[0,1.9;Sold out]\ncontainer_end[]")
table.insert(formspec, "button[0,1.0;4.3,1.0;"..kstr.."__;Sold out]"..
table.insert(row_content, "button[0,1.0;4.3,1.0;"..kstr.."__;Sold out]"..
"\ncontainer_end[]")
else
-- how often can this trade be done?
table.insert(formspec, "label[0,0.8;Stock: "..tostring(amount_available).."x]")
table.insert(row_content, "label[0,0.8;Stock: "..tostring(amount_available).."x]")
-- show the price label only when the offer is in stock
table.insert(formspec, "label[0,1.9;->]"..
table.insert(row_content, "label[0,1.9;->]"..
"label[0,4.4;Price:]"..
"\ncontainer_end[]")
end
col = col + 1
if(col >= yl_speak_up.trade_max_cols) then
col = 0
row = row + 1
--row = row + 1
h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h,
"does_not_matter_"..tostring(i),
"", "", false, "", false,
pname, 6, table.concat(row_content, ''))
row_content = {}
end
end
end
h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h,
"does_not_matter_last",
"", "", false, "", false,
pname, 6, table.concat(row_content, ''))
-- set button background color back to golden/brownish
table.insert(formspec, "style_type[button;bgcolor=#a37e45]")
h = (row+1) * 6 - 0.6
h = yl_speak_up.add_edit_button_fs_talkdialog(formspec, h,
"show_inventory",
"Access and manage the inventory of the NPC. This is used for adding trade "..