formspecs.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. local esc = minetest.formspec_escape
  2. local base =
  3. 'formspec_version[4]'..
  4. 'size[13,10]'..
  5. 'no_prepend[]'..
  6. 'bgcolor[;true;#080808BB]'..
  7. 'background[-.5,-.5;14,11;teleport_fs_bg_1.png]'..
  8. 'style_type[image_button;border=false]'
  9. function teleport.formspec_overview(pos, name)
  10. local stations = teleport.other_stations
  11. local key = minetest.pos_to_string(pos)
  12. local owner = teleport.data[key].owner
  13. local station_name = teleport.data[key].name
  14. local desc = teleport.data[key].desc
  15. if #desc > 137 then
  16. desc = desc:sub(1, 134) .. '...'
  17. end
  18. local formspec =
  19. base..
  20. 'textarea[.25,.25;2.75,8;;;Station Data\n'..
  21. 'Owner: '..esc(owner)..'\n'..
  22. 'Name: '..esc(station_name)..'\n'..
  23. 'Description: '..esc(desc)..']'..
  24. 'style_type[button;bgimg=teleport_fs_blank.png]'..
  25. 'scroll_container[3,.25;9.75,8.5;stations;horizontal;.1]'
  26. local voffset = -.5
  27. local hoffset = 0
  28. for k, v in pairs(stations) do
  29. local name, strpos = string.match(v, "(.*)|(.*)")
  30. voffset = voffset + 1
  31. if voffset > 8 then
  32. voffset = .5
  33. hoffset = hoffset + 3.25
  34. end
  35. formspec = formspec..
  36. 'button['.. .25+hoffset..','..voffset..';3,.75;'..strpos..';'..esc(name)..']'
  37. end
  38. formspec = formspec.. 'scroll_container_end[]'
  39. if #stations > 24 then
  40. local total = (math.ceil(#stations/8)) * 3.25
  41. local scrollboxsize = (total - 9.5)/.1
  42. formspec = formspec..
  43. 'scrollbaroptions[max='..scrollboxsize..']'..
  44. 'scrollbar[3.25,9;9.5,.5;horizontal;stations;]'
  45. end
  46. if name == owner then
  47. formspec = formspec..
  48. 'image_button[.25,8.75;2.75,1;teleport_fs_edit.png;station_edit;]'
  49. end
  50. return formspec
  51. end
  52. function teleport.formspec_station_edit(pos, name)
  53. local key = minetest.pos_to_string(pos)
  54. local owner = teleport.data[key].owner
  55. local name = teleport.data[key].name
  56. local desc = teleport.data[key].desc
  57. local formspec = base..
  58. 'textarea[.25,.25;2.75,7;;;Feel free to spin up a mystic story about your destination.]'..
  59. 'textarea[3.25,.5;9.5,1;name;Station Name;'..esc(name)..']'..
  60. 'textarea[3.25,2;9.5,5.25;desc;Description;'..esc(desc)..']'..
  61. 'image_button[.25,7.5;2.75,1;teleport_fs_back.png;back;]'..
  62. 'image_button[.25,8.75;2.75,1;teleport_fs_save.png;save;]'
  63. return formspec
  64. end
  65. function teleport.formspec_station_info(pos, name)
  66. local key = minetest.pos_to_string(pos)
  67. local owner = teleport.data[key].owner
  68. local station_name = teleport.data[key].name
  69. local desc = teleport.data[key].desc
  70. local pos1 = minetest.string_to_pos(key)
  71. local pos2 = teleport.local_pos[name]
  72. local distance = vector.distance(pos1, pos2)
  73. local formspec = base..
  74. 'background[-.5,-.5;14,11;teleport_fs_bg_2.png]'..
  75. 'hypertext[.25,-.3125;12.5,1.25;;<global valign=middle><center><style color=white size=40>'..esc(station_name)..'</style></center>]'..
  76. 'textarea[.25,2;12.5,5.25;;Description;'..esc(desc)..']'..
  77. 'textarea[.25,7.5;12.5,1;;;Distance is '..math.ceil(distance)..' nodes, estimated time to teleport '..math.ceil(distance/100)..' seconds]'..
  78. 'image_button[.25,8.75;2.75,1;teleport_fs_back.png;back;]'
  79. if distance < 1000 then
  80. formspec = formspec..
  81. 'image_button[4.5,8.75;4,1;teleport_fs_teleport.png;teleport;]'
  82. else
  83. formspec = formspec..
  84. 'image_button[4.5,8.75;4,1;teleport_fs_no_teleport.png;sorry;]'..
  85. 'tooltip[sorry;You can teleport 1000 nodes max.\nConsider teleporting to a closer location.]'
  86. end
  87. if name == owner then
  88. formspec = formspec ..
  89. 'image_button[10,8.75;2.75,1;teleport_fs_edit.png;edit;]'
  90. end
  91. return formspec
  92. end
  93. minetest.register_on_player_receive_fields(function(player, formname, fields)
  94. if formname == 'teleport:formspec' then
  95. local name = player:get_player_name()
  96. if fields.quit then
  97. minetest.close_formspec(name, 'teleport:formspec')
  98. elseif fields.station_edit then
  99. local pos = teleport.local_pos[name]
  100. teleport.remote_pos[name] = pos
  101. minetest.show_formspec(name, 'teleport:formspec_station_edit', teleport.formspec_station_edit(pos, name))
  102. end
  103. for k,v in pairs(fields) do
  104. local pos = minetest.string_to_pos(k)
  105. if pos then
  106. teleport.remote_pos[name] = pos
  107. minetest.show_formspec(name, 'teleport:formspec_station_info', teleport.formspec_station_info(pos, name))
  108. end
  109. end
  110. elseif formname == 'teleport:formspec_station_edit' then
  111. local name = player:get_player_name()
  112. if fields.save then
  113. teleport.find_stations()
  114. local pos = teleport.remote_pos[name]
  115. local key = minetest.pos_to_string(pos)
  116. local meta = minetest.get_meta(pos)
  117. meta:set_string('infotext', fields.name)
  118. teleport.data[key].name = fields.name
  119. teleport.data[key].desc = fields.desc
  120. elseif fields.back then
  121. local pos = teleport.local_pos[name]
  122. minetest.show_formspec(name, 'teleport:formspec', teleport.formspec_overview(pos, name))
  123. end
  124. elseif formname == 'teleport:formspec_station_info' then
  125. local name = player:get_player_name()
  126. if fields.teleport then
  127. minetest.close_formspec(name, 'teleport:formspec_station_info')
  128. local pos1 = teleport.remote_pos[name]
  129. local pos2 = teleport.local_pos[name]
  130. local distance = vector.distance(pos1, pos2)
  131. minetest.after(distance/100, function()-- This needs to black the screen, and prevent player from moving or taking damage.
  132. player:set_pos(pos1)
  133. end)
  134. elseif fields.back then
  135. local pos = teleport.local_pos[name]
  136. minetest.show_formspec(name, 'teleport:formspec', teleport.formspec_overview(pos, name))
  137. elseif fields.edit then
  138. local pos = teleport.remote_pos[name]
  139. minetest.show_formspec(name, 'teleport:formspec_station_edit', teleport.formspec_station_edit(pos, name))
  140. end
  141. end
  142. end)