formspecs.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. local esc = core.formspec_escape
  2. function asrs.loading_screen()
  3. local formspec =
  4. 'formspec_version[7]'..
  5. 'size[13.25,10.5]'..
  6. 'no_prepend[]'..
  7. 'bgcolor[;true;#080808BB]'..
  8. 'style_type[box;colors=#484848;bordercolors=#224c9a;borderwidths=-6]'..
  9. 'box[0,0;13.25,10.5;]'.. --Background
  10. 'style_type[box;colors=#363636;bordercolors=#565656;borderwidths=-2]'..
  11. 'box[.375,.375;12.5,9.5;]'.. --Secondary Background
  12. 'box[.375,.375;12.5,3.75;]'.. --Node Inventory
  13. 'box[.375,5..;2.5,5;]'.. --Buttons
  14. 'box[2.875,5;10,5;]'.. --Player Inventory
  15. 'image[.375,.375;12.5,3.75;asrs_logo.png]'..
  16. 'hypertext[.375,4.125;12.5,.9;;<style size=30><global halign=center valign=middle>Connecting to system...</style>]'
  17. return formspec
  18. end
  19. local function base(inv_rows)
  20. local inv_rows = inv_rows or 0
  21. local height = 10.5
  22. local offset = 4
  23. if inv_rows ~= 0 then
  24. height = 6.75 + (1.25*inv_rows)
  25. offset = (1.25*inv_rows) + .25
  26. end
  27. local formspec =
  28. 'formspec_version[7]'..
  29. 'size[13.25,'..height..']'..
  30. 'no_prepend[]'..
  31. 'bgcolor[;true;#080808BB]'..
  32. 'style_type[box;colors=#484848;bordercolors=#224c9a;borderwidths=-6]'..
  33. 'box[0,0;13.25,'..height..';]'.. --Background
  34. 'style_type[box;colors=#363636;bordercolors=#565656;borderwidths=-2]'..
  35. 'box[.375,.375;12.5,'..5.5+offset..';]'.. --Secondary Background
  36. 'box[.375,.375;12.5,'..offset-.25 ..';]'.. --Node Inventory
  37. 'box[.375,'..offset+1 ..'..;2.5,5;]'.. --Buttons
  38. 'box[2.875,'..offset+1 ..';10,5;]'.. --Player Inventory
  39. 'style_type[image_button;border=false]'..
  40. 'image_button[.5,'..1.125+offset..';1,1;asrs_main.png;main;]'..
  41. 'tooltip[main;Main Inventory Screen]'..
  42. 'image_button[1.75,'..1.125+offset..';1,1;asrs_sort.png;sort;]'..
  43. 'tooltip[sort;Sort System Inventory]'..
  44. 'image_button[.5,'..2.3755+offset..';1,1;asrs_settings.png;settings;]'..
  45. 'tooltip[settings;Settings]'..
  46. 'image_button[1.75,'..2.375+offset..';1,1;asrs_debug.png;debug;]'..
  47. 'tooltip[debug;Debug Information]'..
  48. 'image_button[.5,'..3.625+offset..';1,1;asrs_help.png;help;]'..
  49. 'tooltip[help;How to use the system.]'..
  50. 'image_button[.5,'..4.875+offset..';1,1;asrs_exit.png;exit;]'..
  51. 'tooltip[exit;Exit this dialog]'
  52. return formspec
  53. end
  54. function asrs.main(pos, node) --The main inventory screen.
  55. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  56. local meta = core.get_meta(pos)
  57. local inv = meta:get_inventory()
  58. local inv_name = 'storage'
  59. if inv:get_size('search') > 0 then
  60. inv_name = 'search'
  61. end
  62. local inv_rows = math.max(meta:get_int('rows'), 3)
  63. local offset = (1.25*inv_rows) + .375
  64. local index = meta:get_int('inv_page') or 0
  65. local sys_id = meta:get_string('system_id')
  66. local sys_data = asrs.data[sys_id]
  67. local sys_inv_max = sys_data.max_inv
  68. local quantity = meta:get_int('rows') or 3
  69. local total_pages = math.floor(sys_inv_max/(quantity*10))
  70. local formspec =
  71. base(inv_rows)..
  72. 'field[.4,'..(offset+.0625)..';3.75,.75;inv_search_filter;;]'..
  73. 'field_close_on_enter[inv_search_filter;false]'..
  74. 'image_button[4.25,'..(offset+.0625)..';.75,.75;asrs_search.png;inv_search;]'..
  75. 'tooltip[inv_search;Search for an item.]'..
  76. 'image_button[5.25,'..(offset+.0625)..';.75,.75;asrs_clear_search.png;reset_search;]'..
  77. 'tooltip[reset_search;Clear search filter/show all items.]'..
  78. 'image_button[9,'..(offset+.0625)..';.75,.75;asrs_first.png;first_page;]'..
  79. 'tooltip[first_page;Jump to first page.]'..
  80. 'image_button[10,'..(offset+.0625)..';.75,.75;asrs_previous.png;prev_page;]'..
  81. 'tooltip[prev_page;Go back one page.]'..
  82. 'image_button[11,'..(offset+.0625)..';.75,.75;asrs_next.png;next_page;]'..
  83. 'tooltip[next_page;Go forward one page.]'..
  84. 'image_button[12,'..(offset+.0625)..';.75,.75;asrs_last.png;last_page;]'..
  85. 'tooltip[last_page;Jump to last page.]'..
  86. 'hypertext[6.25,'..(offset+.0625)..';2.75,.75;;<style size=24><global halign=center valign=middle>page: '..((index/(quantity*10))+1)..'/'..total_pages..'</style>]'..
  87. 'list[nodemeta:'..spos..';'..inv_name..';.5,.5;10,'..inv_rows..';'..index..']'..
  88. 'list[current_player;main;3,'..1+offset..';8,4]'..
  89. 'listring[]'
  90. if node == 'remote' then
  91. formspec = formspec ..
  92. 'box[.375,'..offset+.875 ..'..;2.5,5;]'.. --Masks buttons
  93. 'image_button[1.75,'..offset+1 ..';1,1;asrs_sort.png;sort;]'..
  94. 'tooltip[sort;Sort System Inventory]'
  95. end
  96. return formspec
  97. end
  98. function asrs.settings(pos)
  99. local meta = core.get_meta(pos)
  100. local name = meta:get_string('infotext')
  101. local players = meta:get_string('players') or ''
  102. local owner = meta:get_string('owner') or ''
  103. local inv_rows = meta:get_int('rows')
  104. local formspec =
  105. base()..
  106. 'image[.375,.375;12.5,3.75;asrs_logo.png]'..
  107. 'field[3,5.25;9.75,.5;name;System Name:;'..esc(name)..']'..
  108. 'field[3,6.25;9.75,.5;players;Player Access: (These players can access inventory. Separate with a space.);'..esc(players)..']'..
  109. 'field[3,7.25;9.75,.5;inv_rows;Inventory Rows: (How many rows of the A.S.R.S inventory should be shown.);'..inv_rows..']'..
  110. 'button[3,9;3,.75;settings_remove;Remove node]'..
  111. 'tooltip[settings_remove;Removes the ASRS controller. Useful if you get an error when trying to dig the node.\nWARNING!!! Any inventory in the system will be lost!]'..
  112. 'button[10.75,9;2,.75;settings_save;Save]'
  113. return formspec
  114. end
  115. function asrs.debug(pos) --Any information that would be useful for debugging errors.
  116. local meta = core.get_meta(pos)
  117. local sys_id = meta:get_string('system_id')
  118. local sys_data = asrs.data[sys_id]
  119. local sys_inv_max = sys_data.max_inv or 0
  120. local con_nodes = sys_data.nodes-1 or 0
  121. local inv_count = asrs.count_inventory(pos)
  122. local formspec =
  123. base()..
  124. 'image[.375,.375;12.5,3.75;asrs_logo.png]'..
  125. 'textarea[3,5;9.75,4.75;;;The system currently has '..con_nodes..' connected nodes. '..
  126. 'This includes storage cells and lifts.\n'..inv_count..' of '..sys_inv_max..' slots are filled.\n'..
  127. 'The internal system ID is: '..sys_id..']'
  128. return formspec
  129. end
  130. local asrs_help =
  131. base()..
  132. 'image[.375,.375;12.5,3.75;asrs_logo.png]'..
  133. 'textarea[3,5;9.75,4.75;;;To get started place some lift nodes to the left of the controller. '..
  134. 'You can connect more lift nodes to these nodes, to increase the amount of storage you can link up. '..
  135. 'Add some storage cells that connect to the lift nodes.'..
  136. 'Each storage cell will give the system an extra twenty slots of inventory.\n'..
  137. 'You can change how many rows of system inventory are displayed in the configuration tab. Between 3 and 20 are allowed.'..
  138. 'You can sort the system inventory by clicking the sort button to the left. You can click sort on any screen.\n'..
  139. 'If you are playing with the pipeworks mod enabled you can add inventory to the system by connecting tubes to the back and bottom of this node. You can also use the Pipework interface node.'..
  140. 'Likewise you can remove inventory from the back and bottom with an injector.\n'..
  141. 'If you are playing with techpack or techage you can do the same with the appropriate interface nodes. These interface nodes will also allow you to remove inventory.]'
  142. core.register_on_player_receive_fields(function(player, formname, fields)
  143. if formname == 'asrs:control_panel' then
  144. local name = player:get_player_name()
  145. local pos = asrs.clicker[name].pos
  146. local node = asrs.clicker[name].node
  147. if fields.inv_search or fields.key_enter_field == 'inv_search_filter' then
  148. local meta = core.get_meta(pos)
  149. local inv = meta:get_inventory()
  150. local items = inv:get_list('storage')
  151. local filter = fields.inv_search_filter:lower()
  152. asrs.clicker[name].filter = filter
  153. local preserve = {}
  154. if filter and filter ~= "" then
  155. for _, v in pairs(items) do
  156. if v:get_name():find(filter) then
  157. preserve[#preserve + 1] = v
  158. end
  159. end
  160. end
  161. inv:set_size('search', #preserve)
  162. inv:set_list('search', preserve)
  163. core.show_formspec(name, 'asrs:control_panel', asrs.main(pos, node))
  164. elseif fields.reset_search then
  165. local meta = core.get_meta(pos)
  166. local inv = meta:get_inventory()
  167. inv:set_size('search', 0)
  168. inv:set_list('search', {})
  169. core.show_formspec(name, 'asrs:control_panel', asrs.main(pos, node))
  170. elseif fields.first_page then
  171. local meta = core.get_meta(pos)
  172. meta:set_int('inv_page', 0)
  173. core.show_formspec(name, 'asrs:control_panel', asrs.main(pos, node))
  174. elseif fields.prev_page then
  175. local meta = core.get_meta(pos)
  176. local index = meta:get_int('inv_page')
  177. local quantity = meta:get_int('rows')*10
  178. meta:set_int('inv_page', math.max(index - quantity, 0))
  179. core.show_formspec(name, 'asrs:control_panel', asrs.main(pos, node))
  180. elseif fields.next_page then
  181. local meta = core.get_meta(pos)
  182. local index = meta:get_int('inv_page')
  183. local quantity = meta:get_int('rows')*10
  184. local sys_id = meta:get_string('system_id')
  185. local sys_data = asrs.data[sys_id]
  186. local sys_inv_max = sys_data.max_inv
  187. meta:set_int('inv_page', math.min((index + quantity), (sys_inv_max - quantity)))
  188. core.show_formspec(name, 'asrs:control_panel', asrs.main(pos, node))
  189. elseif fields.last_page then
  190. local meta = core.get_meta(pos)
  191. local index = meta:get_int('inv_page')
  192. local quantity = meta:get_int('rows')*10
  193. local sys_id = meta:get_string('system_id')
  194. local sys_data = asrs.data[sys_id]
  195. local sys_inv_max = sys_data.max_inv
  196. meta:set_int('inv_page', sys_inv_max - quantity)
  197. core.show_formspec(name, 'asrs:control_panel', asrs.main(pos, node))
  198. elseif fields.main then
  199. core.show_formspec(name, 'asrs:control_panel', asrs.main(pos, node))
  200. elseif fields.settings then
  201. local meta = core.get_meta(pos)
  202. local owner = meta:get_string('owner')
  203. if owner == name then
  204. core.show_formspec(name, 'asrs:control_panel', asrs.settings(pos))
  205. end
  206. elseif fields.sort then
  207. asrs.sort_inventory(pos)
  208. elseif fields.debug then
  209. core.show_formspec(name, 'asrs:control_panel', asrs.debug(pos))
  210. elseif fields.help then
  211. core.show_formspec(name, 'asrs:control_panel', asrs_help)
  212. elseif fields.settings_save then
  213. local meta = core.get_meta(pos)
  214. local owner = meta:get_string('owner')
  215. if owner == name then
  216. local sys_id = meta:get_string('system_id')
  217. meta:set_string('infotext', fields.name)
  218. asrs.data[sys_id].infotext = fields.name
  219. meta:set_string('players', fields.players)
  220. if tonumber(fields.inv_rows) then
  221. meta:set_int('rows', math.max(3,(math.min(fields.inv_rows,20))))
  222. else
  223. meta:set_int('rows', 3)
  224. end
  225. else
  226. core.chat_send_player(name, 'Only the owner can change these options.')
  227. end
  228. elseif fields.settings_remove then
  229. local meta = core.get_meta(pos)
  230. local owner = meta:get_string('owner')
  231. local node = core.get_node(pos)
  232. if owner == name then
  233. asrs.remove_side_node(pos, node)
  234. core.remove_node(pos)
  235. local sys_id = meta:get_string('system_id')
  236. if sys_id ~= '' then
  237. asrs.data[sys_id] = nil
  238. end
  239. local player_inv = player:get_inventory()
  240. if player_inv:room_for_item('main', 'asrs:controller') then
  241. player_inv:add_item('main', 'asrs:controller')
  242. else
  243. local drop_pos = player:get_pos()
  244. core.add_item(drop_pos, 'asrs:controller')
  245. end
  246. core.close_formspec(name, 'asrs:control_panel')
  247. end
  248. elseif fields.exit then
  249. core.close_formspec(name, 'asrs:control_panel')
  250. end
  251. end
  252. end)