z_mirror/CursorEnhancements
git clone https://git.raiguard.me/z_mirror/CursorEnhancements
b1fdad5
trunk
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 8local subgroup ={} 9local prototypes =prototypes . get_item_filtered ( { { filter ="subgroup" , subgroup =subgroup_name } } ) 10for _ , item in pairs ( prototypes ) do 11if not item . has_flag ( "spawnable" ) and not item . hidden then 12subgroup [ # subgroup + 1 ] =item 13end 14end 15subgroups [ subgroup_name ] =subgroup 16end 17 18--- @param e EventData.CustomInputEvent 19--- @param delta integer 20local function scroll_item ( e , delta ) 21local player =game . get_player ( e . player_index ) 22if not player then 23return 24end 25local item =util . get_cursor_item ( player ) 26if not item then 27return 28end 29local subgroup_name =item . name . subgroup . name 30if not subgroup_name then 31return 32end 33local subgroup =subgroups [ subgroup_name ] 34local index =flib_table . find ( subgroup , item . name ) 35if not index then 36return 37end 38local new_item =subgroup [ index + delta ] 39if not new_item then 40return 41end 42if not util . set_cursor ( player , { name =new_item , quality =item . quality } ) then 43return 44end 45--- @type LocalisedString 46local text ={ "" , "[img=item/" .. new_item . name .. "]" , new_item . localised_name } 47if item . quality ~= prototypes . quality . normal then 48text [ # text + 1 ] ={ "" , " (" , item . quality . localised_name , ")" } 49end 50player . 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 ) 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