formspecs.lua 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. local esc = minetest.formspec_escape
  2. local base =
  3. 'formspec_version[4]'..
  4. 'size[13.25,10.25]'..
  5. 'no_prepend[]'..
  6. 'bgcolor[;true;#080808BB]'..
  7. 'background[0,0;13.25,10.25;asrs_formspec_bg.png]'..
  8. 'style_type[image_button;border=false]'..
  9. 'image_button[.5,5;1,1;asrs_main.png;main;]'..
  10. 'tooltip[main;Main Inventory Screen]'..
  11. 'image_button[1.75,5;1,1;asrs_settings.png;settings;]'..
  12. 'tooltip[settings;Settings]'..
  13. 'image_button[.5,6.25;1,1;asrs_sort.png;sort;]'..
  14. 'tooltip[sort;Sort System Inventory]'..
  15. 'image_button[1.75,6.25;1,1;asrs_debug.png;debug;]'..
  16. 'tooltip[debug;Debug Information]'..
  17. 'image_button[.5,7.5;1,1;asrs_help.png;help;]'..
  18. 'tooltip[help;How to use the system.]'..
  19. 'image_button[.5,8.75;1,1;asrs_exit.png;exit;]'..
  20. 'tooltip[exit;Exit this dialog]'
  21. function asrs.main(pos) --The main inventory screen.
  22. local spos = pos.x .. "," .. pos.y .. "," .. pos.z
  23. local meta = minetest.get_meta(pos)
  24. local inv = meta:get_inventory()
  25. local inv_name = 'storage'
  26. if inv:get_size('search') > 0 then
  27. inv_name = 'search'
  28. end
  29. local index = meta:get_int('inv_page') or 0
  30. local formspec =
  31. base..
  32. 'background[0,0;13.25,10.25;asrs_formspec_bg_inv.png]'..
  33. 'field[.5,4.125;3.5,.75;inv_search_filter;;]'..
  34. 'field_close_on_enter[inv_search_filter;false]'..
  35. 'image_button[4.25,4.125;.75,.75;asrs_search.png;inv_search;]'..
  36. 'tooltip[inv_search;Search for an item.]'..
  37. 'image_button[5.25,4.125;.75,.75;asrs_clear_search.png;reset_search;]'..
  38. 'tooltip[reset_search;Clear search filter/show all items.]'..
  39. 'image_button[9,4.125;.75,.75;asrs_first.png;first_page;]'..
  40. 'tooltip[first_page;Jump to first page.]'..
  41. 'image_button[10,4.125;.75,.75;asrs_previous.png;prev_page;]'..
  42. 'tooltip[prev_page;Go back one page.]'..
  43. 'image_button[11,4.125;.75,.75;asrs_next.png;next_page;]'..
  44. 'tooltip[next_page;Go forward one page.]'..
  45. 'image_button[12,4.125;.75,.75;asrs_last.png;last_page;]'..
  46. 'tooltip[last_page;Jump to last page.]'..
  47. 'list[nodemeta:'..spos..';'..inv_name..';.5,.5;10,3;'..index..']'..
  48. 'list[current_player;main;3,5;8,4]'..
  49. 'listring[]'
  50. return formspec
  51. end
  52. function asrs.settings(pos)
  53. local meta = minetest.get_meta(pos)
  54. local name = meta:get_string('infotext')
  55. local players = meta:get_string('players') or ''
  56. local owner = meta:get_string('owner') or ''
  57. local formspec =
  58. base..
  59. 'field[3,5.25;9.75,.5;name;System Name:;'..esc(name)..']'..
  60. 'field[3,6.25;9.75,.5;players;Player Access: (These players can access inventory. Separate with a space.);'..esc(players)..']'..
  61. 'button[3,9;3,.75;settings_remove;Remove node]'..
  62. '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!]'..
  63. 'button[10.75,9;2,.75;settings_save;Save]'
  64. return formspec
  65. end
  66. function asrs.debug(pos) --Any information that would be useful for debugging errors.
  67. local meta = minetest.get_meta(pos)
  68. local sys_id = meta:get_string('system_id')
  69. local sys_data = asrs.data[sys_id]
  70. local sys_inv_max = sys_data.max_inv or 0
  71. local con_nodes = sys_data.nodes-1 or 0
  72. local inv_count = asrs.count_inventory(pos)
  73. local formspec =
  74. base..
  75. 'textarea[3,5;9.75,4.75;;;The system currently has '..con_nodes..' connected nodes. '..
  76. 'This includes storage cells and lifts.\n'..inv_count..' of '..sys_inv_max..' slots are filled.\n'..
  77. 'The internal system ID is: '..sys_id..']'
  78. return formspec
  79. end
  80. local asrs_help =
  81. base..
  82. 'textarea[3,5;9.75,4.75;;;To get started place some lift nodes to the left of the controller. '..
  83. 'You can connect more lift nodes to these nodes, to increase the amount of storage you can link up. '..
  84. 'Add some storage cells that connect to the lift nodes.'..
  85. 'Each storage cell will give the system an extra twenty slots of inventory.\n'..
  86. 'You can sort the system inventory by clicking the sort button to the left. You can click sort on any screen.\n'..
  87. '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.'..
  88. 'Likewise you can remove inventory from the back and bottom with an injector.\n'..
  89. '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.]'
  90. minetest.register_on_player_receive_fields(function(player, formname, fields)
  91. if formname == 'asrs:control_panel' then
  92. local name = player:get_player_name()
  93. local pos = asrs.clicker[name].pos
  94. if fields.inv_search or fields.key_enter_field == 'inv_search_filter' then
  95. local meta = minetest.get_meta(pos)
  96. local inv = meta:get_inventory()
  97. local items = inv:get_list('storage')
  98. local filter = fields.inv_search_filter:lower()
  99. asrs.clicker[name].filter = filter
  100. local preserve = {}
  101. if filter and filter ~= "" then
  102. for _, v in pairs(items) do
  103. if v:get_name():find(filter) then
  104. preserve[#preserve + 1] = v
  105. end
  106. end
  107. end
  108. inv:set_size('search', #preserve)
  109. inv:set_list('search', preserve)
  110. minetest.show_formspec(name, 'asrs:control_panel', asrs.main(pos))
  111. elseif fields.reset_search then
  112. local meta = minetest.get_meta(pos)
  113. local inv = meta:get_inventory()
  114. inv:set_size('search', 0)
  115. inv:set_list('search', {})
  116. minetest.show_formspec(name, 'asrs:control_panel', asrs.main(pos))
  117. elseif fields.first_page then
  118. local meta = minetest.get_meta(pos)
  119. meta:set_int('inv_page', 0)
  120. minetest.show_formspec(name, 'asrs:control_panel', asrs.main(pos))
  121. elseif fields.prev_page then
  122. local meta = minetest.get_meta(pos)
  123. local index = meta:get_int('inv_page')
  124. meta:set_int('inv_page', math.max(index - 30, 0))
  125. minetest.show_formspec(name, 'asrs:control_panel', asrs.main(pos))
  126. elseif fields.next_page then
  127. local meta = minetest.get_meta(pos)
  128. local index = meta:get_int('inv_page')
  129. local sys_id = meta:get_string('system_id')
  130. local sys_data = asrs.data[sys_id]
  131. local sys_inv_max = sys_data.max_inv
  132. meta:set_int('inv_page', math.min((index + 30), (sys_inv_max - 30)))
  133. minetest.show_formspec(name, 'asrs:control_panel', asrs.main(pos))
  134. elseif fields.last_page then
  135. local meta = minetest.get_meta(pos)
  136. local index = meta:get_int('inv_page')
  137. local sys_id = meta:get_string('system_id')
  138. local sys_data = asrs.data[sys_id]
  139. local sys_inv_max = sys_data.max_inv
  140. meta:set_int('inv_page', sys_inv_max - 30)
  141. minetest.show_formspec(name, 'asrs:control_panel', asrs.main(pos))
  142. elseif fields.main then
  143. minetest.show_formspec(name, 'asrs:control_panel', asrs.main(pos))
  144. elseif fields.settings then
  145. local meta = minetest.get_meta(pos)
  146. local owner = meta:get_string('owner')
  147. if owner == name then
  148. minetest.show_formspec(name, 'asrs:control_panel', asrs.settings(pos))
  149. end
  150. elseif fields.sort then
  151. asrs.sort_inventory(pos)
  152. elseif fields.debug then
  153. minetest.show_formspec(name, 'asrs:control_panel', asrs.debug(pos))
  154. elseif fields.help then
  155. minetest.show_formspec(name, 'asrs:control_panel', asrs_help)
  156. elseif fields.settings_save then
  157. local meta = minetest.get_meta(pos)
  158. local owner = meta:get_string('owner')
  159. if owner == name then
  160. meta:set_string('infotext', fields.name)
  161. meta:set_string('players', fields.players)
  162. else
  163. minetest.chat_send_player(name, 'Only the owner can change these options.')
  164. end
  165. elseif fields.settings_remove then
  166. local meta = minetest.get_meta(pos)
  167. local owner = meta:get_string('owner')
  168. local node = minetest.get_node(pos)
  169. if owner == name then
  170. asrs.remove_side_node(pos, node)
  171. minetest.remove_node(pos)
  172. local sys_id = meta:get_string('system_id')
  173. if sys_id ~= '' then
  174. asrs.data[sys_id] = nil
  175. end
  176. local player_inv = player:get_inventory()
  177. if player_inv:room_for_item('main', 'asrs:controller') then
  178. player_inv:add_item('main', 'asrs:controller')
  179. else
  180. local drop_pos = player:get_pos()
  181. minetest.add_item(drop_pos, 'asrs:controller')
  182. end
  183. minetest.close_formspec(name, 'asrs:control_panel')
  184. end
  185. elseif fields.exit then
  186. minetest.close_formspec(name, 'asrs:control_panel')
  187. end
  188. end
  189. end)