add basic dialog for editing compass settings
This commit is contained in:
parent
0357e8a267
commit
ac9d3f2368
138
init.lua
138
init.lua
@ -1,7 +1,7 @@
|
||||
local player_waypoints = {}
|
||||
local update_interval = 1.31415
|
||||
local compass_precision = 10 -- set to 1 to show whole number or 10 for 1 decimal
|
||||
local waypoint_color = 0xFC7D0A
|
||||
local default_waypoint_color = 0xFFFFFF
|
||||
local target_above = false -- place waypoint inside the block or above it
|
||||
local point_to_objects = false -- unimplemented
|
||||
local point_to_liquids = true
|
||||
@ -13,14 +13,14 @@ local compass_range = 180
|
||||
-- end
|
||||
|
||||
|
||||
local function get_compass_meta_label(meta)
|
||||
local label = meta:get_string("waypoint_compass:label")
|
||||
return label ~= "" and label or "destination"
|
||||
local function set_compass_meta_label(meta, label)
|
||||
meta:set_string("waypoint_compass:label", label)
|
||||
end
|
||||
|
||||
|
||||
local function set_compass_meta_label(meta, label)
|
||||
meta:set_string("waypoint_compass:label", label)
|
||||
local function get_compass_meta_label(meta)
|
||||
local label = meta:get_string("waypoint_compass:label")
|
||||
return label ~= "" and label or "destination"
|
||||
end
|
||||
|
||||
|
||||
@ -34,6 +34,22 @@ local function get_compass_meta_pos(meta)
|
||||
return minetest.deserialize(meta:get_string("waypoint_compass:position"))
|
||||
end
|
||||
|
||||
|
||||
local function set_compass_meta_color(meta, color)
|
||||
meta:set_int("waypoint_compass:color", color)
|
||||
end
|
||||
|
||||
|
||||
local function get_compass_meta_color(meta)
|
||||
local color = meta:get_int("waypoint_compass:color")
|
||||
if color > 0 then
|
||||
return color
|
||||
else
|
||||
return default_waypoint_color
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- local function set_compass_meta_pos(meta, pos)
|
||||
-- local pos_hash = minetest.hash_node_position(pos)
|
||||
-- meta:set_int("waypoint_compass:position", pos_hash)
|
||||
@ -50,7 +66,8 @@ local function set_waypoint_at_pointed_place(itemstack, pointed_thing)
|
||||
local meta = itemstack:get_meta()
|
||||
meta:set_string("description", string.format("Waypoint compass %s", minetest.pos_to_string(pointed_pos)))
|
||||
set_compass_meta_pos(meta, pointed_pos)
|
||||
set_compass_meta_label(meta, minetest.pos_to_string(pointed_pos))
|
||||
-- TODO change only if default?
|
||||
--set_compass_meta_label(meta, minetest.pos_to_string(pointed_pos))
|
||||
return itemstack
|
||||
end
|
||||
-- TODO handle entities?
|
||||
@ -85,6 +102,7 @@ local function show_hud_waypoint(player, compass_item_meta)
|
||||
local player_name = player:get_player_name()
|
||||
-- Show this waypoint
|
||||
local waypoint_name = get_compass_meta_label(compass_item_meta)
|
||||
local waypoint_color = get_compass_meta_color(compass_item_meta)
|
||||
local hud_id = player:hud_add({
|
||||
hud_elem_type = "waypoint",
|
||||
name = waypoint_name,
|
||||
@ -104,7 +122,8 @@ end
|
||||
|
||||
|
||||
-- if item is a compass, then show stored waypoint
|
||||
local function update_hud_waypoint(player, itemstack)
|
||||
-- force - force hud update even if position is unchanged
|
||||
local function update_hud_waypoint(player, itemstack, force)
|
||||
if not player:is_player() then
|
||||
return
|
||||
end
|
||||
@ -114,7 +133,7 @@ local function update_hud_waypoint(player, itemstack)
|
||||
local meta = itemstack:get_meta()
|
||||
local waypoint_pos = get_compass_meta_pos(meta)
|
||||
-- remove different waypoint if it exists
|
||||
if player_waypoints[player_name] and
|
||||
if player_waypoints[player_name] or force and
|
||||
(player_waypoints[player_name].pos ~= waypoint_pos) then
|
||||
hide_hud_waypoint(player)
|
||||
end
|
||||
@ -144,19 +163,108 @@ minetest.register_globalstep(function(dtime)
|
||||
end)
|
||||
|
||||
|
||||
local function coords_to_string(pos)
|
||||
-- strip "()"
|
||||
if not pos then
|
||||
pos = {x = 0, y = 0, z = 0}
|
||||
end
|
||||
return string.sub(minetest.pos_to_string(pos), 2, -2)
|
||||
end
|
||||
|
||||
|
||||
local function coords_from_string(str)
|
||||
-- numbers are separated by ","
|
||||
local tmp = {}
|
||||
for n in string.gmatch(str, "([^,]+)") do
|
||||
table.insert(tmp, n)
|
||||
end
|
||||
tmp[1] = tonumber(tmp[1]) or 0
|
||||
tmp[2] = tonumber(tmp[2]) or 0
|
||||
tmp[3] = tonumber(tmp[3]) or 0
|
||||
return {x = tmp[1], y = tmp[2], z = tmp[3]}
|
||||
end
|
||||
|
||||
local compass_dialog_context = {}
|
||||
|
||||
local function show_basic_dialog(itemstack, player)
|
||||
local meta = itemstack:get_meta()
|
||||
local formspec_head = "formspec_version[4]size[10,3]set_focus[close;]"
|
||||
local coords = coords_to_string(get_compass_meta_pos(meta))
|
||||
local field_coords = ("field[0.5,0.6;2.5,0.8;coords;Coords;%s]"):format(minetest.formspec_escape(coords))
|
||||
local label = get_compass_meta_label(meta)
|
||||
local field_destination = ("field[3,0.6;5,0.8;label;Label;%s]"):format(minetest.formspec_escape(label))
|
||||
local color = ("%06X"):format(get_compass_meta_color(meta))
|
||||
local field_color = ("field[8,0.6;1.5,0.8;color;Color;%s]"):format(minetest.formspec_escape(color))
|
||||
minetest.show_formspec(player:get_player_name(), "waypoint_compass:basic",
|
||||
formspec_head ..
|
||||
field_coords ..
|
||||
field_destination ..
|
||||
field_color ..
|
||||
"button_exit[1,1.7;3,0.8;save;save]" ..
|
||||
"button_exit[6,1.7;3,0.8;close;close]")
|
||||
local player_name = player:get_player_name()
|
||||
compass_dialog_context[player_name] = itemstack
|
||||
end
|
||||
|
||||
|
||||
local function compass_use_callback(itemstack, user, pointed_thing)
|
||||
if pointed_thing.type == "nothing" then
|
||||
if user and user:is_player() then
|
||||
pointed_thing = raycast_crosshair(user, compass_range)
|
||||
if user:get_player_control()["sneak"] then
|
||||
if pointed_thing.type == "nothing" then
|
||||
if user and user:is_player() then
|
||||
pointed_thing = raycast_crosshair(user, compass_range)
|
||||
end
|
||||
end
|
||||
set_waypoint_at_pointed_place(itemstack, pointed_thing)
|
||||
update_hud_waypoint(user, itemstack)
|
||||
else
|
||||
if user:is_player() then
|
||||
show_basic_dialog(itemstack, user)
|
||||
end
|
||||
end
|
||||
set_waypoint_at_pointed_place(itemstack, pointed_thing)
|
||||
update_hud_waypoint(user, itemstack)
|
||||
end
|
||||
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= "waypoint_compass:basic" then
|
||||
return
|
||||
end
|
||||
if fields.save or (fields.key_enter_field and fields.quit) then
|
||||
local compass_item = compass_dialog_context[player:get_player_name()]
|
||||
if not compass_item then
|
||||
-- should not happen normally
|
||||
minetest.log("error","[MOD] waypoint_compass: " .. player:get_player_name() .. " Closed compass dialog without opening it?")
|
||||
return
|
||||
end
|
||||
local meta = compass_item:get_meta()
|
||||
|
||||
local color = tonumber("0x"..fields.color, 16)
|
||||
if color then
|
||||
set_compass_meta_color(meta, color)
|
||||
end
|
||||
|
||||
local coords = coords_from_string(fields.coords)
|
||||
if coords then
|
||||
set_compass_meta_pos(meta, coords)
|
||||
end
|
||||
|
||||
local label = fields.label
|
||||
set_compass_meta_label(meta, label)
|
||||
|
||||
local success = player:set_wielded_item(compass_item)
|
||||
if not success then
|
||||
-- no idea why this can happen
|
||||
minetest.log("error","[MOD] waypoint_compass: " .. player:get_player_name() .. " Failed to swap compass after editing it")
|
||||
end
|
||||
-- assume dialog is closed and reset context
|
||||
compass_dialog_context[player:get_player_name()] = nil
|
||||
update_hud_waypoint(player, compass_item, 'force')
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
minetest.register_tool(
|
||||
"waypoint_compass:compass", {
|
||||
description = "Waypoint compass",
|
||||
description = "Waypoint compass\n(sneak+place to set point)",
|
||||
short_description = "Waypoint compass",
|
||||
inventory_image = "Waypoint_compass_inventory_image.png",
|
||||
wield_scale = {x = 0.5, y = 0.5, z = 0.5},
|
||||
|
Loading…
Reference in New Issue
Block a user