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