waypoints.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. unified_inventory.hud_colors = {
  2. {"#FFFFFF", 0xFFFFFF, "White"},
  3. {"#DBBB00", 0xf1d32c, "Yellow"},
  4. {"#DD0000", 0xDD0000, "Red"},
  5. {"#2cf136", 0x2cf136, "Green"},
  6. {"#2c4df1", 0x2c4df1, "Blue"},
  7. }
  8. unified_inventory.hud_colors_max = #unified_inventory.hud_colors
  9. unified_inventory.register_page("waypoints", {
  10. get_formspec = function(player)
  11. local waypoints = datastorage.get_container (player, "waypoints")
  12. local formspec = "background[0,4.5;8,4;ui_main_inventory.png]"..
  13. "image[0,0;1,1;ui_waypoints_icon.png]"..
  14. "label[1,0;Waypoints]"
  15. -- Tabs buttons:
  16. local i
  17. for i = 1, 5, 1 do
  18. if i == waypoints.selected then
  19. formspec = formspec ..
  20. "image_button[0.0,".. 0.2 + i*0.7 ..";.8,.8;ui_blue_icon_background.png^ui_"..
  21. i .."_icon.png;select_waypoint".. i .. ";]"
  22. else
  23. formspec = formspec ..
  24. "image_button[0.0,".. 0.2 + i*0.7 ..";.8,.8;ui_"..
  25. i .."_icon.png;select_waypoint".. i .. ";]"
  26. end
  27. end
  28. i = waypoints.selected
  29. -- Main buttons:
  30. formspec = formspec ..
  31. "image_button[4.5,3.7;.8,.8;ui_waypoint_set_icon.png;set_waypoint".. i .. ";]"
  32. if waypoints[i].active then
  33. formspec = formspec ..
  34. "image_button[5.2,3.7;.8,.8;ui_on_icon.png;toggle_waypoint".. i .. ";]"
  35. else
  36. formspec = formspec ..
  37. "image_button[5.2,3.7;.8,.8;ui_off_icon.png;toggle_waypoint".. i .. ";]"
  38. end
  39. if waypoints[i].display_pos then
  40. formspec = formspec ..
  41. "image_button[5.9,3.7;.8,.8;ui_green_icon_background.png^ui_xyz_icon.png;toggle_display_pos".. i .. ";]"
  42. else
  43. formspec = formspec ..
  44. "image_button[5.9,3.7;.8,.8;ui_red_icon_background.png^ui_xyz_icon.png;toggle_display_pos".. i .. ";]"
  45. end
  46. formspec = formspec ..
  47. "image_button[6.6,3.7;.8,.8;ui_circular_arrows_icon.png;toggle_color".. i .. ";]"..
  48. "image_button[7.3,3.7;.8,.8;ui_pencil_icon.png;rename_waypoint".. i .. ";]"
  49. -- Waypoint's info:
  50. if waypoints[i].active then
  51. formspec = formspec .. "label[1,0.8;Waypoint active]"
  52. else
  53. formspec = formspec .. "label[1,0.8;Waypoint inactive]"
  54. end
  55. formspec = formspec .. "label[1,1.3;World position: " ..
  56. minetest.pos_to_string(waypoints[i].world_pos) .. "]" ..
  57. "label[1,1.8;Name: ]".. waypoints[i].name .. "]" ..
  58. "label[1,2.3;Hud text color: " ..
  59. unified_inventory.hud_colors[waypoints[i].color][3] .. "]"
  60. return {formspec=formspec}
  61. end,
  62. })
  63. unified_inventory.register_button("waypoints", {
  64. type = "image",
  65. image = "ui_waypoints_icon.png",
  66. })
  67. unified_inventory.update_hud = function (player, waypoint)
  68. local name
  69. if waypoint.display_pos then
  70. name = "("..
  71. waypoint.world_pos.x .. "," ..
  72. waypoint.world_pos.y .. "," ..
  73. waypoint.world_pos.z .. ")"
  74. if waypoint.name ~= "" then
  75. name = name .. ", " ..
  76. waypoint.name
  77. end
  78. else
  79. name = waypoint.name
  80. end
  81. if waypoint.active then
  82. player:hud_remove(waypoint.hud)
  83. waypoint.hud = player:hud_add({
  84. hud_elem_type = "waypoint",
  85. number = unified_inventory.hud_colors[waypoint.color][2] ,
  86. name = name,
  87. text = "m",
  88. world_pos = waypoint.world_pos
  89. })
  90. else
  91. if waypoint.hud ~= nil then
  92. player:hud_remove(waypoint.hud)
  93. waypoint.hud = nil
  94. end
  95. end
  96. end
  97. minetest.register_on_player_receive_fields(function(player, formname, fields)
  98. if formname ~= "" then
  99. return
  100. end
  101. local update_formspec = false
  102. local update_hud = false
  103. local waypoints = datastorage.get_container (player, "waypoints")
  104. for i = 1, 5, 1 do
  105. if fields["select_waypoint"..i] then
  106. waypoints.selected = i
  107. update_formspec = true
  108. end
  109. if fields["toggle_waypoint"..i] then
  110. waypoints[i].active = not (waypoints[i].active)
  111. update_hud = true
  112. update_formspec = true
  113. end
  114. if fields["set_waypoint"..i] then
  115. local pos = player:getpos()
  116. pos.x = math.floor(pos.x)
  117. pos.y = math.floor(pos.y)
  118. pos.z = math.floor(pos.z)
  119. waypoints[i].world_pos = pos
  120. update_hud = true
  121. update_formspec = true
  122. end
  123. if fields["rename_waypoint"..i] then
  124. waypoints[i].edit = true
  125. update_formspec = true
  126. end
  127. if fields["toggle_display_pos"..i] then
  128. waypoints[i].display_pos = not waypoints[i].display_pos
  129. update_hud = true
  130. update_formspec = true
  131. end
  132. if fields["toggle_color"..i] then
  133. local color = waypoints[i].color
  134. color = color + 1
  135. if color > unified_inventory.hud_colors_max then
  136. color = 1
  137. end
  138. waypoints[i].color = color
  139. update_hud = true
  140. update_formspec = true
  141. end
  142. if fields["confirm_rename"..i] then
  143. waypoints[i].edit = false
  144. waypoints[i].name = fields["rename_box"..i]
  145. update_hud = true
  146. update_formspec = true
  147. end
  148. if update_hud then
  149. unified_inventory.update_hud (player, waypoints[i])
  150. end
  151. if update_formspec then
  152. unified_inventory.set_inventory_formspec(player, "waypoints")
  153. end
  154. end
  155. end)
  156. minetest.register_on_joinplayer(function(player)
  157. local waypoints = datastorage.get_container (player, "waypoints")
  158. -- Create new waypoints data
  159. if waypoints[1] == nil then
  160. for i = 1, 5, 1 do
  161. waypoints[i] = {
  162. edit = false,
  163. active = false,
  164. display_pos = true,
  165. color = 2,
  166. name = "Waypoint ".. i,
  167. world_pos = {x = 0, y = 0, z = 0},
  168. }
  169. end
  170. datastorage.save_container(player)
  171. end
  172. -- Initialize waypoints
  173. minetest.after(0.5, function()
  174. waypoints.selected = 1
  175. for i = 1, 5, 1 do
  176. waypoints[i].edit = false
  177. unified_inventory.update_hud (player, waypoints[i])
  178. end
  179. end)
  180. end)