z_mirror/RecipeBook
git clone https://git.raiguard.me/z_mirror/RecipeBook
214f243
trunk
1local flib_gui =require ( "__flib__.gui" ) 2 3--- @class ContextMenu 4--- @field overlay LuaGuiElement 5--- @field window LuaGuiElement 6local context_menu ={} 7local mt ={ __index =context_menu } 8script . register_metatable ( "context_menu" , mt ) 9 10--- @param player LuaPlayer 11--- @param items LocalisedString[] 12--- @param location GuiLocation 13--- @return ContextMenu 14function context_menu . new ( player , items , location ) 15local resolution =player . display_resolution 16local scale =player . display_scale 17local overlay_size ={ resolution . width / scale , resolution . height / scale } 18 19local overlay =player . gui . screen . add ( { 20type ="frame" , 21name ="rb_context_overlay" , 22style ="invisible_frame" , 23tags =flib_gui . format_handlers ( { [ defines . events . on_gui_click ] =context_menu . on_overlay_clicked } ), 24} ) 25overlay . style . size =overlay_size 26overlay . bring_to_front () 27 28local window =player . gui . screen . add ( { 29type ="frame" , 30name ="rb_context_menu" , 31style ="invisible_frame" , 32direction ="vertical" , 33} ) 34window . location =location 35for i =1 , # items do 36window . add ( { 37type ="button" , 38style ="list_box_item" , 39caption =items [ i ], 40tags =flib_gui . format_handlers ( { [ defines . events . on_gui_click ] =context_menu . on_result_clicked } ), 41} ) 42end 43window . bring_to_front () 44 45--- @type ContextMenu 46local self ={ 47overlay =overlay , 48window =window , 49} 50return setmetatable ( self , mt ) 51end 52 53--- @param e EventData.on_gui_click 54function context_menu . on_result_clicked ( e ) 55game . ( e . element . caption ) 56context_menu . on_overlay_clicked ( e ) 57end 58 59--- @param e EventData.on_gui_click 60function context_menu . on_overlay_clicked ( e ) 61local player =game . get_player ( e . player_index ) 62if not player then 63return 64end 65player . gui . screen . rb_context_overlay . destroy () 66player . gui . screen . rb_context_menu . destroy () 67end 68 69flib_gui . add_handlers ( context_menu , nil , "context_menu" ) 70 71return context_menu