summaryrefslogtreecommitdiff
path: root/waypoints.lua
blob: 70239926f030b0d29d71a1cf22df46b2c218d111 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
local S
if intllib then
	S = intllib.Getter()
else
	S = function(s) return s end
end

unified_inventory.hud_colors = {
			{"#FFFFFF", 0xFFFFFF, S("White")},
			{"#DBBB00", 0xf1d32c, S("Yellow")},
			{"#DD0000", 0xDD0000, S("Red")},
			{"#2cf136", 0x2cf136, S("Green")},
			{"#2c4df1", 0x2c4df1, S("Blue")},		
			}
unified_inventory.hud_colors_max = #unified_inventory.hud_colors
			
unified_inventory.register_page("waypoints", {
	get_formspec = function(player)
		local waypoints = datastorage.get_container (player, "waypoints")
		local formspec = "background[0,4.5;8,4;ui_main_inventory.png]"..
			"image[0,0;1,1;ui_waypoints_icon.png]"..
			"label[1,0;"..S("Waypoints").."]" 

		-- Tabs buttons:
		local i
		for i = 1, 5, 1 do
			formspec = formspec ..
				"image_button[0.0,".. 0.2 + i*0.7 ..";.8,.8;"..
				(i == waypoints.selected and "ui_blue_icon_background.png^" or "")..
				"ui_"..i.."_icon.png;"..
				"select_waypoint"..i..";;;;;"..
				minetest.formspec_escape(S("Select Waypoint #%d"):format(i)).."]"
		end
		
		i = waypoints.selected
		
		-- Main buttons:
		formspec = formspec .. 
			"image_button[4.5,3.7;.8,.8;"..
			"ui_waypoint_set_icon.png;"..
			"set_waypoint"..i..";;;;;"..
			minetest.formspec_escape(S("Set waypoint to current location")).."]"

		formspec = formspec ..
			"image_button[5.2,3.7;.8,.8;"..
			(waypoints[i].active and "ui_on_icon.png" or "ui_off_icon.png")..";"..
			"toggle_waypoint"..i..";;;;;"..
			minetest.formspec_escape(S("Make waypoint "..(waypoints[i].active and "invisible" or "visible"))).."]"

		formspec = formspec .. 
			"image_button[5.9,3.7;.8,.8;"..
			(waypoints[i].display_pos and "ui_green_icon_background.png" or "ui_red_icon_background.png").."^ui_xyz_icon.png;"..
			"toggle_display_pos"..i..";;;;;"..
			minetest.formspec_escape(S((waypoints[i].display_pos and "Disable" or "Enable").." display of waypoint coordinates")).."]"

		formspec = formspec ..
			"image_button[6.6,3.7;.8,.8;"..
			"ui_circular_arrows_icon.png;"..
			"toggle_color"..i..";;;;;"..
			minetest.formspec_escape(S("Change color of waypoint display")).."]"
		formspec = formspec ..
			"image_button[7.3,3.7;.8,.8;"..
			"ui_pencil_icon.png;"..
			"rename_waypoint"..i..";;;;;"..
			minetest.formspec_escape(S("Edit waypoint name")).."]"
		
		-- Waypoint's info:	
		if waypoints[i].active then
			formspec = formspec .. 	"label[1,0.8;"..S("Waypoint active").."]"
		else 
			formspec = formspec .. 	"label[1,0.8;"..S("Waypoint inactive").."]"
		end

		if waypoints[i].edit then
			formspec = formspec ..
				"field[1.3,3.2;6,.8;rename_box" .. i .. ";;"..waypoints[i].name.."]" ..
				"image_button[7.3,2.9;.8,.8;"..
				"ui_ok_icon.png;"..
				"confirm_rename"..i.. ";;;;;"..
				minetest.formspec_escape(S("Finish editing")).."]"
		end
		
		formspec = formspec .. "label[1,1.3;"..S("World position")..": " .. 
			minetest.pos_to_string(waypoints[i].world_pos) .. "]" ..
			"label[1,1.8;"..S("Name")..": ".. waypoints[i].name .. "]" ..
			"label[1,2.3;"..S("HUD text color")..": " ..
			unified_inventory.hud_colors[waypoints[i].color][3] .. "]"
			
		return {formspec=formspec}
	end,
})

unified_inventory.register_button("waypoints", {
	type = "image",
	image = "ui_waypoints_icon.png",
	tooltip = S("Waypoints"),
})

unified_inventory.update_hud = function (player, waypoint)	
	local name
	if waypoint.display_pos then
		name = "(".. 
			waypoint.world_pos.x .. "," ..
			waypoint.world_pos.y .. "," ..
			waypoint.world_pos.z .. ")"
		if waypoint.name ~= "" then 	 
			name = name .. ", " ..
			waypoint.name
		end
	else
		name = waypoint.name
	end
	if waypoint.hud then
		player:hud_remove(waypoint.hud)
	end
	if waypoint.active then
		waypoint.hud = player:hud_add({
			hud_elem_type = "waypoint",
			number = unified_inventory.hud_colors[waypoint.color][2] ,
			name = name,
			text = "m",
			world_pos = waypoint.world_pos
		})
	else 
		waypoint.hud = nil
	end
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
	if formname ~= "" then
		return
	end

	local update_formspec = false
	local update_hud = false
	
	local waypoints = datastorage.get_container (player, "waypoints")		
	for i = 1, 5, 1 do

		if fields["select_waypoint"..i] then
			waypoints.selected = i
			update_formspec = true
		end

		if fields["toggle_waypoint"..i] then
			waypoints[i].active = not (waypoints[i].active)
			update_hud = true
			update_formspec = true
		end
		
		if fields["set_waypoint"..i] then
			local pos = player:getpos()
			pos.x = math.floor(pos.x)
			pos.y = math.floor(pos.y)
			pos.z = math.floor(pos.z)
			waypoints[i].world_pos = pos
			update_hud = true
			update_formspec = true
		end
		
		if fields["rename_waypoint"..i] then
			waypoints[i].edit = true
			update_formspec = true
		end

		if fields["toggle_display_pos"..i] then
			waypoints[i].display_pos = not waypoints[i].display_pos
			update_hud = true
			update_formspec = true
		end

		if fields["toggle_color"..i] then
			local color = waypoints[i].color
			color = color + 1
			if color > unified_inventory.hud_colors_max then
				color = 1
			end
			waypoints[i].color = color
			update_hud = true
			update_formspec = true
		end

		if fields["confirm_rename"..i] then
			waypoints[i].edit = false
			waypoints[i].name = fields["rename_box"..i] 
			update_hud = true
			update_formspec = true
		end

		if update_hud then
			unified_inventory.update_hud (player, waypoints[i])
		end
	
		if update_formspec then
			unified_inventory.set_inventory_formspec(player, "waypoints")
		end
	
	end
end)

minetest.register_on_joinplayer(function(player)
	local waypoints = datastorage.get_container (player, "waypoints")
	local need_save = false
	-- Create new waypoints data
		for i = 1, 5, 1 do
			if waypoints[i] == nil then 
				need_save = true
				waypoints[i] = {
					edit = false,
					active = false,
					display_pos = true,
					color = 1,
					name = "Waypoint ".. i,
					world_pos = {x = 0, y = 0, z = 0},
				}
			end	
		end
	if need_save then datastorage.save_container (player) end

	-- Initialize waypoints
	minetest.after(0.5, function()
		waypoints.selected = 1
		for i = 1, 5, 1 do
			waypoints[i].edit = false
			unified_inventory.update_hud (player, waypoints[i])		
		end
	end)
end)