z_mirror/CursorEnhancements

git clone https://git.raiguard.me/z_mirror/CursorEnhancements

Caleb HeuerMove scripts into a scripts subfolderb4ff864

smart-quickbar
1.7 KiB66 linesraw
1local table = require("__flib__/table")
2
3local util = require("__CursorEnhancements__/scripts/util")
4
5local function build_subgroups()
6  --- @type table<string, string[]>
7  local subgroups = {}
8
9  for subgroup_name in pairs(game.item_subgroup_prototypes) do
10    local subgroup = {}
11    local prototypes = game.get_filtered_item_prototypes({ { filter = "subgroup", subgroup = subgroup_name } })
12    for name, item in pairs(prototypes) do
13      if name ~= "dummy-steel-axe" and not item.has_flag("spawnable") and not item.has_flag("hidden") then
14        subgroup[#subgroup + 1] = name
15      end
16    end
17    subgroups[subgroup_name] = subgroup
18  end
19
20  global.subgroups = subgroups
21end
22
23--- @param e EventData.CustomInputEvent
24--- @param delta integer
25local function scroll_item(e, delta)
26  local player = game.get_player(e.player_index)
27  if not player then
28    return
29  end
30  local item = util.get_cursor_item(player)
31  if not item then
32    return
33  end
34  local subgroup_name = game.item_prototypes[item].subgroup.name
35  if not subgroup_name then
36    return
37  end
38  local subgroup = global.subgroups[subgroup_name]
39  local index = table.find(subgroup, item)
40  if not index then
41    return
42  end
43  local new_item = subgroup[index + delta]
44  if not new_item then
45    return
46  end
47  util.set_cursor(player, new_item)
48end
49
50local scroll_subgroup = {}
51
52scroll_subgroup.on_init = build_subgroups
53scroll_subgroup.on_configuration_changed = build_subgroups
54
55scroll_subgroup.events = {
56  --- @param e EventData.CustomInputEvent
57  ["cen-scroll-next"] = function(e)
58    scroll_item(e, 1)
59  end,
60  --- @param e EventData.CustomInputEvent
61  ["cen-scroll-previous"] = function(e)
62    scroll_item(e, -1)
63  end,
64}
65
66return scroll_subgroup