waypoints.lua 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. local S = unified_inventory.gettext
  2. local hud_colors = {
  3. {"#FFFFFF", 0xFFFFFF, S("White")},
  4. {"#DBBB00", 0xf1d32c, S("Yellow")},
  5. {"#DD0000", 0xDD0000, S("Red")},
  6. {"#2cf136", 0x2cf136, S("Green")},
  7. {"#2c4df1", 0x2c4df1, S("Blue")},
  8. }
  9. local hud_colors_max = #hud_colors
  10. -- Stores temporary player data (persists until player leaves)
  11. local waypoints_temp = {}
  12. unified_inventory.register_page("waypoints", {
  13. get_formspec = function(player)
  14. local player_name = player:get_player_name()
  15. -- build a "fake" temp entry if the server took too long
  16. -- during sign-on and returned an empty entry
  17. if not waypoints_temp[player_name] then waypoints_temp[player_name] = {hud = 1} end
  18. local waypoints = datastorage.get(player_name, "waypoints")
  19. local formspec = "background[0,4.5;8,4;ui_main_inventory.png]" ..
  20. "image[0,0;1,1;ui_waypoints_icon.png]" ..
  21. "label[1,0;" .. S("Waypoints") .. "]"
  22. -- Tabs buttons:
  23. for i = 1, 5, 1 do
  24. formspec = formspec ..
  25. "image_button[0.0," .. 0.2 + i * 0.7 .. ";.8,.8;" ..
  26. (i == waypoints.selected and "ui_blue_icon_background.png^" or "") ..
  27. "ui_" .. i .. "_icon.png;" ..
  28. "select_waypoint" .. i .. ";]" ..
  29. "tooltip[select_waypoint" .. i .. ";"
  30. .. minetest.formspec_escape(S("Select Waypoint #%d"):format(i)).."]"
  31. end
  32. local i = waypoints.selected or 1
  33. local waypoint = waypoints[i] or {}
  34. local temp = waypoints_temp[player_name][i] or {}
  35. local default_name = "Waypoint "..i
  36. -- Main buttons:
  37. formspec = formspec ..
  38. "image_button[4.5,3.7;.8,.8;"..
  39. "ui_waypoint_set_icon.png;"..
  40. "set_waypoint"..i..";]"..
  41. "tooltip[set_waypoint" .. i .. ";"
  42. .. minetest.formspec_escape(S("Set waypoint to current location")).."]"
  43. formspec = formspec ..
  44. "image_button[5.2,3.7;.8,.8;"..
  45. (waypoint.active and "ui_on_icon.png" or "ui_off_icon.png")..";"..
  46. "toggle_waypoint"..i..";]"..
  47. "tooltip[toggle_waypoint" .. i .. ";"
  48. .. minetest.formspec_escape(S("Make waypoint "
  49. ..(waypoint.active and "invisible" or "visible"))).."]"
  50. formspec = formspec ..
  51. "image_button[5.9,3.7;.8,.8;"..
  52. (waypoint.display_pos and "ui_green_icon_background.png" or "ui_red_icon_background.png").."^ui_xyz_icon.png;"..
  53. "toggle_display_pos" .. i .. ";]"..
  54. "tooltip[toggle_display_pos" .. i .. ";"
  55. .. minetest.formspec_escape(S((waypoint.display_pos and "Disable" or "Enable")
  56. .." display of waypoint coordinates")).."]"
  57. formspec = formspec ..
  58. "image_button[6.6,3.7;.8,.8;"..
  59. "ui_circular_arrows_icon.png;"..
  60. "toggle_color"..i..";]"..
  61. "tooltip[toggle_color" .. i .. ";"
  62. .. minetest.formspec_escape(S("Change color of waypoint display")).."]"
  63. formspec = formspec ..
  64. "image_button[7.3,3.7;.8,.8;"..
  65. "ui_pencil_icon.png;"..
  66. "rename_waypoint"..i..";]"..
  67. "tooltip[rename_waypoint" .. i .. ";"
  68. .. minetest.formspec_escape(S("Edit waypoint name")).."]"
  69. -- Waypoint's info:
  70. if waypoint.active then
  71. formspec = formspec .. "label[1,0.8;"..S("Waypoint active").."]"
  72. else
  73. formspec = formspec .. "label[1,0.8;"..S("Waypoint inactive").."]"
  74. end
  75. if temp.edit then
  76. formspec = formspec ..
  77. "field[1.3,3.2;6,.8;rename_box" .. i .. ";;"
  78. ..(waypoint.name or default_name).."]" ..
  79. "image_button[7.3,2.9;.8,.8;"..
  80. "ui_ok_icon.png;"..
  81. "confirm_rename"..i.. ";]"..
  82. "tooltip[confirm_rename" .. i .. ";"
  83. .. minetest.formspec_escape(S("Finish editing")).."]"
  84. end
  85. formspec = formspec .. "label[1,1.3;"..S("World position")..": " ..
  86. minetest.pos_to_string(waypoint.world_pos or vector.new()) .. "]" ..
  87. "label[1,1.8;"..S("Name")..": ".. (waypoint.name or default_name) .. "]" ..
  88. "label[1,2.3;"..S("HUD text color")..": " ..
  89. hud_colors[waypoint.color or 1][3] .. "]"
  90. return {formspec=formspec}
  91. end,
  92. })
  93. unified_inventory.register_button("waypoints", {
  94. type = "image",
  95. image = "ui_waypoints_icon.png",
  96. tooltip = S("Waypoints"),
  97. hide_lite=true
  98. })
  99. local function update_hud(player, waypoints, temp, i)
  100. local waypoint = waypoints[i]
  101. if not waypoint then return end
  102. temp[i] = temp[i] or {}
  103. temp = temp[i]
  104. local pos = waypoint.world_pos or vector.new()
  105. local name
  106. if waypoint.display_pos then
  107. name = minetest.pos_to_string(pos)
  108. if waypoint.name then
  109. name = name..", "..waypoint.name
  110. end
  111. else
  112. name = waypoint.name or "Waypoint "..i
  113. end
  114. if temp.hud then
  115. player:hud_remove(temp.hud)
  116. end
  117. if waypoint.active then
  118. temp.hud = player:hud_add({
  119. hud_elem_type = "waypoint",
  120. number = hud_colors[waypoint.color or 1][2] ,
  121. name = name,
  122. text = "m",
  123. world_pos = pos
  124. })
  125. else
  126. temp.hud = nil
  127. end
  128. end
  129. minetest.register_on_player_receive_fields(function(player, formname, fields)
  130. if formname ~= "" then return end
  131. local player_name = player:get_player_name()
  132. local update_formspec = false
  133. local need_update_hud = false
  134. local hit = false
  135. local waypoints = datastorage.get(player_name, "waypoints")
  136. local temp = waypoints_temp[player_name]
  137. for i = 1, 5, 1 do
  138. if fields["select_waypoint"..i] then
  139. hit = true
  140. waypoints.selected = i
  141. update_formspec = true
  142. end
  143. if fields["toggle_waypoint"..i] then
  144. hit = true
  145. waypoints[i] = waypoints[i] or {}
  146. waypoints[i].active = not (waypoints[i].active)
  147. need_update_hud = true
  148. update_formspec = true
  149. end
  150. if fields["set_waypoint"..i] then
  151. hit = true
  152. local pos = player:getpos()
  153. pos.x = math.floor(pos.x)
  154. pos.y = math.floor(pos.y)
  155. pos.z = math.floor(pos.z)
  156. waypoints[i] = waypoints[i] or {}
  157. waypoints[i].world_pos = pos
  158. need_update_hud = true
  159. update_formspec = true
  160. end
  161. if fields["rename_waypoint"..i] then
  162. hit = true
  163. temp[i] = temp[i] or {}
  164. temp[i].edit = true
  165. update_formspec = true
  166. end
  167. if fields["toggle_display_pos"..i] then
  168. hit = true
  169. waypoints[i] = waypoints[i] or {}
  170. waypoints[i].display_pos = not waypoints[i].display_pos
  171. need_update_hud = true
  172. update_formspec = true
  173. end
  174. if fields["toggle_color"..i] then
  175. hit = true
  176. waypoints[i] = waypoints[i] or {}
  177. local color = waypoints[i].color or 1
  178. color = color + 1
  179. if color > hud_colors_max then
  180. color = 1
  181. end
  182. waypoints[i].color = color
  183. need_update_hud = true
  184. update_formspec = true
  185. end
  186. if fields["confirm_rename"..i] then
  187. hit = true
  188. waypoints[i] = waypoints[i] or {}
  189. temp[i].edit = false
  190. waypoints[i].name = fields["rename_box"..i]
  191. need_update_hud = true
  192. update_formspec = true
  193. end
  194. if need_update_hud then
  195. update_hud(player, waypoints, temp, i)
  196. end
  197. if update_formspec then
  198. unified_inventory.set_inventory_formspec(player, "waypoints")
  199. end
  200. if hit then return end
  201. end
  202. end)
  203. minetest.register_on_joinplayer(function(player)
  204. local player_name = player:get_player_name()
  205. local waypoints = datastorage.get(player_name, "waypoints")
  206. local temp = {}
  207. waypoints_temp[player_name] = temp
  208. for i = 1, 5 do
  209. update_hud(player, waypoints, temp, i)
  210. end
  211. end)
  212. minetest.register_on_leaveplayer(function(player)
  213. waypoints_temp[player:get_player_name()] = nil
  214. end)