summaryrefslogtreecommitdiff
path: root/itemwheel.lua
blob: 2e98241b5195478c0d46f2958a3c91184eedb962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
local hb = {}
local scale = tonumber(core.setting_get("hud_scaling")) or 1

local function update_wheel(player)
	local name = player:get_player_name()
	if not player or not name then
		return
	end

	local i = player:get_wield_index()
	local i1 = i - 1
	local i3 = i + 1

	-- it's a wheel
	if i1 < 1 then
		i1 = HUD_IW_MAX
	end
	if i3 > HUD_IW_MAX then
		i3 = 1
	end

	-- get the displayed items
	local inv = player:get_inventory()
	local item = hb[name].item
	local item2 = player:get_wielded_item():get_name()

	-- update all items when wielded has changed
	if item and item2 and item ~= item2 or item == "wheel_init" then
		local items = {}
		items[1] = inv:get_stack("main", i1):get_name() or nil
		items[2] = item2
		items[3] = inv:get_stack("main", i3):get_name() or nil
		local num = player:get_wielded_item():get_count()
		local wear = player:get_wielded_item():get_wear()
		if num < 2 then
			num = ""
		else
			num = tostring(num)
		end
		if wear > 0 then
			num = tostring(100 - math.floor((wear/65535)*100)) .. "%"
		end

		for n, m in pairs(items) do
			-- some default values
			local image = "hud_wielded.png"
			local need_scale = false
			local s1 = {x = 1*scale, y = 1*scale}
			local s2 = {x = 3*scale, y = 3*scale}
			if n ~= 2 then
				s1 = {x = 0.6*scale, y = 0.6*scale}
				s2 = {x = 2*scale, y = 2*scale}
			end

			-- get the images
			local def = minetest.registered_items[m]
			if def then
				if def.tiles and (def.tiles[1] and not def.tiles[1].name) then
					image = minetest.inventorycube(def.tiles[1], def.tiles[6] or def.tiles[3] or def.tiles[1], def.tiles[3] or def.tiles[1])
					need_scale = true
				end
				if def.inventory_image and def.inventory_image ~= "" then
					image = def.inventory_image
					need_scale = false
				end
				if def.wielded_image and def.wielded_image ~= "" then
					image = def.wielded_image
					need_scale = false
				end
				-- needed for nodes with inventory cube inv imges, e.g. glass
				if string.find(image, 'inventorycube') then
					need_scale = true
				end
			end

			-- get the id and update hud elements
			local id = hb[name].id[n]
			if id and image then
				if need_scale then
					player:hud_change(id, "scale", s1)
				else
					player:hud_change(id, "scale", s2)
				end
				-- make previous and next item darker
				--if n ~= 2 then
					--image = image .. "^[colorize:#0005"
				--end
				player:hud_change(id, "text", image)
			end
		end
		if hb[name].id[4] then
			player:hud_change(hb[name].id[4], "text", num)
		end
	end

	-- update wielded buffer
	if hb[name].id[2] ~= nil then
		hb[name].item = item2
	end
end

minetest.register_on_joinplayer(function(player)
    local name = player:get_player_name()
    hb[name]= {}
    hb[name].id = {}
    hb[name].item = "wheel_init"

    minetest.after(0.1, function()

	-- hide builtin hotbar
	local hud_flags = player:hud_get_flags()
	hud_flags.hotbar = false
	player:hud_set_flags(hud_flags)

	player:hud_add({
		hud_elem_type = "image",
		text = "hud_new.png",
		position = {x = 0.5, y = 1},
		scale = {x = 1*scale, y = 1*scale},
		alignment = {x = 0, y = -1},
		offset = {x = 0, y = 0}
	})

	hb[name].id[1] = player:hud_add({
		hud_elem_type = "image",
		text = "hud_wielded.png",
		position = {x = 0.5, y = 1},
		scale = {x = 1*scale, y = 1*scale},
		alignment = {x = 0, y = -1},
		offset = {x = -75*scale, y = -8*scale}
	})

	hb[name].id[2] = player:hud_add({
		hud_elem_type = "image",
		text = "hud_wielded.png",
		position = {x = 0.5, y = 1},
		scale = {x = 3*scale, y = 3*scale},
		alignment = {x = 0, y = -1},
		offset = {x = 0, y = -12*scale}
	})

	hb[name].id[3] = player:hud_add({
		hud_elem_type = "image",
		text = "hud_wielded.png",
		position = {x = 0.5, y = 1},
		scale = {x = 1*scale, y = 1*scale},
		alignment = {x = 0, y = -1},
		offset = {x = 75*scale, y = -8*scale}
	})

	hb[name].id[4] = player:hud_add({
		hud_elem_type = "text",
		position = {x = 0.5, y = 1},
		offset = {x = 35*scale, y = -55*scale},
		alignment = {x = 0, y = -1},
		number = 0xffffff,
		text = "",
	})

	-- init item wheel
	minetest.after(0, function()
		hb[name].item = "wheel_init"
		update_wheel(player)
	end)
    end)
end)

local function update_wrapper(a, b, player)
	local name = player:get_player_name()
	if not name then
		return
	end
	minetest.after(0, function()
		hb[name].item = "wheel_init"
		update_wheel(player)
	end)
end

minetest.register_on_placenode(update_wrapper)
minetest.register_on_dignode(update_wrapper)


local timer = 0
minetest.register_globalstep(function(dtime)
	timer = timer + dtime
	if timer >= HUD_IW_TICK then
		timer = 0
		for _, player in ipairs(minetest.get_connected_players()) do
			update_wheel(player)
		end
	end--timer
end)