forked from minetest-mods/unified_inventory
		
	Use 9-slicing to build inventory-type backgrounds
This way the slots are all nice and crisp regardless of GUI scale or
image size, and we only need the single slot and its bright version.
This also makes the standard crafting grid into a style table entry that
can be referenced to insert the crafting grid at its proper
style-specific position in any formspec.
And it also makes the craft grid arrow, its X position, and the crafting
grid's result slot X position into style table entries.
Includes a few public helper functions to do most of the work:
`ui.single_slot(xpos, ypos, bright)`
    Does just what it sounds like: it returns a single slot image.
    `xpos` and `ypos` are normal coordinates in slots, as you'd use in
    `image[]` element.  `bright` is a flag that switches to the brighter
    version of the slot image.
`ui.make_trash_slot(xpos, ypos)`
    Creates a single slot, with a one-item `list[]` and a trash can icon
    overlay.
`ui.make_inv_img_grid(xpos, ypos, width, height, bright)`
    Generates a `width` by `height` grid of slot images, using the
    single_slot function above, starting at (`xpos`,`ypos`) for the
    top-left.  Position is as in any `image[]` element, and dimensions
    are in integer numbers of slots (so 8,4 would be a standard inventory).
    `bright` is as above.
All three return a string that can be directly inserted into a formspec.
			
			
This commit is contained in:
		
							
								
								
									
										35
									
								
								bags.lua
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								bags.lua
									
									
									
									
									
								
							| @@ -8,14 +8,16 @@ License: GPLv3 | ||||
| local S = minetest.get_translator("unified_inventory") | ||||
| local F = minetest.formspec_escape | ||||
| local ui = unified_inventory | ||||
| local bags_inv_bg = "image[0.3,1.5;"..(ui.imgscale*8)..",%f;%s]" | ||||
|  | ||||
| ui.register_page("bags", { | ||||
| 	get_formspec = function(player) | ||||
| 		local player_name = player:get_player_name() | ||||
| 		return { formspec = table.concat({ | ||||
| 			ui.style_full.standard_inv_bg, | ||||
| 			string.format(bags_inv_bg, ui.imgscale, "ui_bags_header.png"), | ||||
| 			ui.single_slot(0.925, 1.5), | ||||
| 			ui.single_slot(3.425, 1.5), | ||||
| 			ui.single_slot(5.925, 1.5), | ||||
| 			ui.single_slot(8.425, 1.5), | ||||
| 			"label["..ui.style_full.form_header_x..","..ui.style_full.form_header_y..";" .. F(S("Bags")) .. "]", | ||||
| 			"button[0.6125,2.75;1.875,0.75;bag1;" .. F(S("Bag @1", 1)) .. "]", | ||||
| 			"button[3.1125,2.75;1.875,0.75;bag2;" .. F(S("Bag @1", 2)) .. "]", | ||||
| @@ -49,34 +51,27 @@ for bag_i = 1, 4 do | ||||
| 		get_formspec = function(player) | ||||
| 			local stack = get_player_bag_stack(player, bag_i) | ||||
| 			local image = stack:get_definition().inventory_image | ||||
| 			local slots = stack:get_definition().groups.bagslots | ||||
|  | ||||
| 			local formspec = { | ||||
| 				ui.style_full.standard_inv_bg, | ||||
| 				ui.make_inv_img_grid(0.3, 1.5, 8, slots/8), | ||||
| 				"image[9.2,0.4;1,1;" .. image .. "]", | ||||
| 				"label[0.3,0.65;" .. F(S("Bag @1", bag_i)) .. "]", | ||||
| 				"listcolors[#00000000;#00000000]", | ||||
| 				"listring[current_player;main]", | ||||
| 				string.format("list[current_player;bag%icontents;%f,%f;8,3;]", | ||||
| 				    bag_i, 0.3 + ui.list_img_offset, 1.5 + ui.list_img_offset), | ||||
| 				"listring[current_name;bag" .. bag_i .. "contents]", | ||||
| 			} | ||||
| 			local n = 6 | ||||
|  | ||||
| 			local slots = stack:get_definition().groups.bagslots | ||||
| 			if slots == 8 then | ||||
| 					formspec[n] = string.format(bags_inv_bg, ui.imgscale, "ui_bags_inv_small.png") | ||||
| 			elseif slots == 16 then | ||||
| 					formspec[n] = string.format(bags_inv_bg, ui.imgscale*2, "ui_bags_inv_medium.png") | ||||
| 			elseif slots == 24 then | ||||
| 					formspec[n] = string.format(bags_inv_bg, ui.imgscale*3, "ui_bags_inv_large.png") | ||||
| 			end | ||||
| 			formspec[n+1] = "list[current_player;bag" .. bag_i .. "contents;0.45,1.65;8,3;]" | ||||
| 			formspec[n+2] = "listring[current_name;bag" .. bag_i .. "contents]" | ||||
| 			n = n + 3 | ||||
| 			local n = #formspec + 1 | ||||
|  | ||||
| 			local player_name = player:get_player_name() -- For if statement. | ||||
| 			if ui.trash_enabled | ||||
| 					or ui.is_creative(player_name) | ||||
| 					or minetest.get_player_privs(player_name).give then | ||||
| 					formspec[n] = "image[7.8,0.25;"..ui.trash_slot_img.."]" | ||||
| 					formspec[n+1] = "list[detached:trash;main;7.95,0.25;1,1;]" | ||||
| 				n = n + 2 | ||||
| 				or ui.is_creative(player_name) | ||||
| 				or minetest.get_player_privs(player_name).give then | ||||
| 					formspec[n] = ui.make_trash_slot(7.8, 0.25) | ||||
| 					n = n + 1 | ||||
| 			end | ||||
| 			local inv = player:get_inventory() | ||||
| 			for i = 1, 4 do | ||||
|   | ||||
		Reference in New Issue
	
	Block a user