z_mirror/RecipeBook
git clone https://git.raiguard.me/z_mirror/RecipeBook
a65a064
trunk
1local grouped =require ( "scripts.database.grouped" ) 2local util =require ( "scripts.util" ) 3 4--- @class History: {[integer]: GenericPrototype, index: integer} 5local history ={} 6local mt ={ __index =history } 7script . register_metatable ( "history" , mt ) 8 9function history . new () 10return setmetatable ( { index =0 } , mt ) 11end 12 13--- @param prototype GenericPrototype 14--- @param grouping_mode GroupingMode 15--- @return boolean 16function history : push ( prototype , grouping_mode ) 17if grouping_mode ~= "none" then 18if 19( prototype . object_name == "LuaRecipePrototype" and grouping_mode == "all" ) 20or ( prototype . object_name ~= "LuaRecipePrototype" and grouping_mode ~= "none" ) 21then 22local base =grouped . material [ util . get_path ( prototype )] 23if base then 24prototype =base 25end 26end 27end 28if self : current () == prototype then 29return false 30end 31for i =self . index + 1 , # self do 32self [ i ] =nil 33end 34for i =self . index , 1 , - 1 do 35if self [ i ] == prototype then 36table . remove ( self , i ) 37end 38end 39self [ # self + 1 ] =prototype 40self . index =# self 41return true 42end 43 44function history : prev () 45self . index =math . max ( 1 , self . index - 1 ) 46end 47 48function history : next () 49self . index =math . min ( # self , self . index + 1 ) 50end 51 52function history : current () 53return self [ self . index ] 54end 55 56function history : at_front () 57return self . index <= 1 58end 59 60function history : at_back () 61return self . index == # self 62end 63 64return history