z_mirror/CursorEnhancements

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

Caleb HeuerFixesb1fdad5

trunk
1.8 KiB66 linesraw
1local flib_table = require("__flib__.table")
2
3local util = require("scripts.util")
4
5--- @type table<string, LuaItemPrototype[]>
6local subgroups = {}
7for subgroup_name in pairs(prototypes.item_subgroup) do
8  local subgroup = {}
9  local prototypes = prototypes.get_item_filtered({ { filter = "subgroup", subgroup = subgroup_name } })
10  for _, item in pairs(prototypes) do
11    if not item.has_flag("spawnable") and not item.hidden then
12      subgroup[#subgroup + 1] = item
13    end
14  end
15  subgroups[subgroup_name] = subgroup
16end
17
18--- @param e EventData.CustomInputEvent
19--- @param delta integer
20local function scroll_item(e, delta)
21  local player = game.get_player(e.player_index)
22  if not player then
23    return
24  end
25  local item = util.get_cursor_item(player)
26  if not item then
27    return
28  end
29  local subgroup_name = item.name.subgroup.name
30  if not subgroup_name then
31    return
32  end
33  local subgroup = subgroups[subgroup_name]
34  local index = flib_table.find(subgroup, item.name)
35  if not index then
36    return
37  end
38  local new_item = subgroup[index + delta]
39  if not new_item then
40    return
41  end
42  if not util.set_cursor(player, { name = new_item, quality = item.quality }) then
43    return
44  end
45  --- @type LocalisedString
46  local text = { "", "[img=item/" .. new_item.name .. "]", new_item.localised_name }
47  if item.quality ~= prototypes.quality.normal then
48    text[#text + 1] = { "", " (", item.quality.localised_name, ")" }
49  end
50  player.create_local_flying_text({ text = text, create_at_cursor = true })
51end
52
53local scroll_subgroup = {}
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