1 debug_mod_name = "Zoom"
4 local zoom_factor = 1.5
5 local zoom_wide_max = 0.01
6 local zoom_wide_hotkey = 1 / (zoom_factor^7)
7 local zoom_close_max = 16.00
10 --------------------------------------------------------------------------------------
11 local function init_player(player)
12 if player_mem == nil then return end
14 -- initialize or update per player globals of the mod, and reset the gui
15 debug_print( "init_player ", player.name, " connected=", player.connected )
17 player_mem[player.index] = player_mem[player.index] or {}
19 local player_mem = player_mem[player.index]
20 player_mem.player = player_mem.player or player
21 player_mem.zoom = player_mem.zoom or 1
23 if player.connected then
28 --------------------------------------------------------------------------------------
29 local function init_players()
30 for _, player in pairs(game.players) do
35 --------------------------------------------------------------------------------------
36 local function on_init()
37 -- called once, the first time the mod is loaded on a game (new or existing game)
38 debug_print( "on_init" )
42 script.on_init(on_init)
44 --------------------------------------------------------------------------------------
45 local function on_configuration_changed(data)
46 -- detect any mod or game version change
47 if data.mod_changes ~= nil then
48 local changes = data.mod_changes[debug_mod_name]
49 if changes ~= nil then
50 debug_print( "update mod: ", debug_mod_name, " ", tostring(changes.old_version), " to ", tostring(changes.new_version) )
56 script.on_configuration_changed(on_configuration_changed)
58 --------------------------------------------------------------------------------------
59 local function on_player_created(event)
60 -- called at player creation
61 local player = game.players[event.player_index]
62 debug_print( "player created ", player.name )
67 script.on_event(defines.events.on_player_created, on_player_created )
69 --------------------------------------------------------------------------------------
70 local function on_player_joined_game(event)
71 -- called in SP(once) and MP(at every connect), eventually after on_player_created
72 local player = game.players[event.player_index]
73 debug_print( "player joined ", player.name )
78 script.on_event(defines.events.on_player_joined_game, on_player_joined_game )
80 --------------------------------------------------------------------------------------
81 local function on_gui_click(event)
82 local player = game.players[event.player_index]
83 local event_name = event.element.name
84 local prefix = string.sub(event_name,1,14)
85 local suffix = string.sub(event_name,15)
87 debug_print( "on_gui_click ", player.name, " ", event_name )
89 if event_name == "but_zoom_zin" then
90 local player_mem = player_mem[player.index]
91 if player_mem.zoom < zoom_close_max then
92 player_mem.zoom = player_mem.zoom * zoom_factor
93 player.zoom = player_mem.zoom
96 elseif event_name == "but_zoom_zout" then
97 local player_mem = player_mem[player.index]
98 if player_mem.zoom > zoom_wide_max then
99 player_mem.zoom = player_mem.zoom / zoom_factor
100 player.zoom = player_mem.zoom
105 script.on_event(defines.events.on_gui_click, on_gui_click)
107 --------------------------------------------------------------------------------------
108 local function on_zoom_hotkey(event)
109 local player = game.players[event.player_index]
110 local player_mem = player_mem[player.index]
111 player_mem.zoom = zoom_wide_hotkey
112 player.zoom = player_mem.zoom
115 script.on_event("zoom_hotkey", on_zoom_hotkey)
117 --------------------------------------------------------------------------------------
118 function build_bar( player )
119 -- here only build gui, but not try to update it with player data that is not always already available (in init_player for example)
120 local guif = player.gui.top.flw_zoom
123 debug_print("create gui player" .. player.name)
124 guif = player.gui.top.add({type = "flow", name = "flw_zoom", direction = "horizontal", style = "zoom_flow_style"})
125 guif.add({type = "sprite-button", name = "but_zoom_zout", sprite = "sprite_zout", style = "zoom_sprite_style"})
126 guif.add({type = "sprite-button", name = "but_zoom_zin", sprite = "sprite_zin", style = "zoom_sprite_style"})
130 --------------------------------------------------------------------------------------
133 function interface.reset()
134 debug_print( "reset" )
136 for _, player in pairs(game.players) do
137 if player.gui.top.flw_zoom then player.gui.top.flw_zoom.destroy() end
142 remote.add_interface( "zoom", interface )
144 -- /c remote.call( "zoom", "reset" )