diff --git a/callbacks.lua b/callbacks.lua index a1e2396..f1646ed 100644 --- a/callbacks.lua +++ b/callbacks.lua @@ -123,12 +123,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) for name, value in pairs(fields) do if string.sub(name, 1, 12) == "item_button_" then clicked_item = string.sub(name, 13) + if string.sub(clicked_item, 1, 6) == "group:" then + minetest.sound_play("click", {to_player=player_name, gain = 0.1}) + unified_inventory.apply_filter(player, clicked_item) + return + end break - elseif string.sub(name, 1, 11) == "item_group_" then - minetest.sound_play("click", - {to_player=player_name, gain = 0.1}) - unified_inventory.apply_filter(player, "group:"..string.sub(name, 12)) - return end end if clicked_item then diff --git a/register.lua b/register.lua index ce4ef3c..cf9c923 100644 --- a/register.lua +++ b/register.lua @@ -134,6 +134,39 @@ unified_inventory.register_page("craft", { end, }) +-- stack_image_button(): generate a form button displaying a stack of items +-- +-- Normally a simple item_image_button[] is used. If the stack contains +-- more than one item, item_image_button[] doesn't have an option to +-- display an item count in the way that an inventory slot does, so +-- we have to fake it using the label facility. This doesn't let us +-- specify that the count should appear at bottom right, so we use some +-- dodgy whitespace to shift it away from the centre of the button. +-- Unfortunately the correct amount of whitespace depends on display +-- resolution, so the results from this will be variable. This should be +-- replaced as soon as the engine adds support for a proper item count, +-- or at least label placement control, on buttons. +-- +-- The specified item may be a group. In that case, an image_button[] +-- is used, displaying an image that just indicates grouping, with a +-- label giving the name of the specific group. It very often happens +-- that the group name doesn't fit within the confines of the button and +-- gets cropped. Group names are also not brilliantly readable against +-- the background of the group image. +local function stack_image_button(x, y, w, h, buttonname_prefix, stackstring) + local st = ItemStack(stackstring) + local n = st:get_name() + local c = st:get_count() + local clab = c == 1 and "" or string.format("%9d", c) + local buttonname = buttonname_prefix..n + local xywh = x..","..y..";"..w..","..h + if string.sub(n, 1, 6) == "group:" then + return "image_button["..xywh..";".."ui_group.png;"..minetest.formspec_escape(buttonname)..";"..minetest.formspec_escape(string.sub(n, 7)).."\n\n"..clab.."]" + else + return "item_image_button["..xywh..";"..minetest.formspec_escape(n)..";"..minetest.formspec_escape(buttonname_prefix..n)..";\n\n"..clab.."]" + end +end + unified_inventory.register_page("craftguide", { get_formspec = function(player) local player_name = player:get_player_name() @@ -188,20 +221,7 @@ unified_inventory.register_page("craftguide", { for x = 1, width do local item = craft.items[i] if item then - if string.sub(item, 1, 6) == "group:" then - local group = string.sub(item, 7) - formspec = formspec.."image_button[" - ..(1.0 + x)..","..(0.0 + y)..";1.1,1.1;" - .."ui_group.png;" - .."item_group_"..minetest.formspec_escape(group)..";" - ..minetest.formspec_escape(group).."]" - else - formspec = formspec.."item_image_button[" - ..(1.0 + x)..","..(0.0 + y)..";1.1,1.1;" - ..minetest.formspec_escape(item)..";" - .."item_button_" - ..minetest.formspec_escape(item)..";]" - end + formspec = formspec..stack_image_button(1.0+x, 0.0+y, 1.1, 1.1, "item_button_", item) end i = i + 1 end