waypoints.lua 6.8 KB

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