summaryrefslogtreecommitdiff
path: root/register.lua
diff options
context:
space:
mode:
authorZefram <zefram@fysh.org>2014-05-01 14:22:25 +0100
committerRealBadAngel <maciej.kasatkin@yahoo.com>2014-05-02 12:14:48 +0200
commit5d34b235ddf975a3fbcf1123b176c94731126dea (patch)
tree40dc517ab2ac413f68e91c934016206b1540f61d /register.lua
parentb3d83bc953905190e60eaf60c1dba3cec3def1a7 (diff)
Handle ingredient quantities in craft guide
Alloy cooking recipes have quantities for the ingredients, which need to be shown. The buttons on which the ingredients are shown don't natively support showing an item count, so hack it up with the label facility. Also, the button names, supporting clicking to see recipes recursively, need to be based only on the item name part of the ingredient, dropping the quantity part.
Diffstat (limited to 'register.lua')
-rw-r--r--register.lua48
1 files changed, 34 insertions, 14 deletions
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