2 debug_mod_name = "Zoom"
3 debug_file = debug_mod_name .. "-debug.txt"
6 local zoom_factor = 1.5
7 local zoom_wide_max = 0.01
8 local zoom_wide_hotkey = 1 / (zoom_factor^7)
9 local zoom_close_max = 16.00
11 --------------------------------------------------------------------------------------
12 local function init_globals()
13 -- initialize or update general globals of the mod
14 debug_print( "init_globals " )
16 global.ticks = global.ticks or 0
17 global.force_mem = global.force_mem or {}
18 global.player_mem = global.player_mem or {}
21 --------------------------------------------------------------------------------------
22 local function init_player(player)
23 if global.player_mem == nil then return end
25 -- initialize or update per player globals of the mod, and reset the gui
26 debug_print( "init_player ", player.name, " connected=", player.connected )
28 global.player_mem[player.index] = global.player_mem[player.index] or {}
30 local player_mem = global.player_mem[player.index]
31 player_mem.player = player_mem.player or player
32 player_mem.zoom = player_mem.zoom or 1
34 if player.connected then
39 --------------------------------------------------------------------------------------
40 local function init_players()
41 for _, player in pairs(game.players) do
46 --------------------------------------------------------------------------------------
47 local function on_init()
48 -- called once, the first time the mod is loaded on a game (new or existing game)
49 debug_print( "on_init" )
54 script.on_init(on_init)
56 --------------------------------------------------------------------------------------
57 local function on_configuration_changed(data)
58 -- detect any mod or game version change
59 if data.mod_changes ~= nil then
60 local changes = data.mod_changes[debug_mod_name]
61 if changes ~= nil then
62 debug_print( "update mod: ", debug_mod_name, " ", tostring(changes.old_version), " to ", tostring(changes.new_version) )
67 if changes.old_version and older_version(changes.old_version, "1.0.4") then
68 message_all("Zoom: you can now use ALT-Z to directly zoom out wide !")
75 script.on_configuration_changed(on_configuration_changed)
77 --------------------------------------------------------------------------------------
78 local function on_player_created(event)
79 -- called at player creation
80 local player = game.players[event.player_index]
81 debug_print( "player created ", player.name )
86 script.on_event(defines.events.on_player_created, on_player_created )
88 --------------------------------------------------------------------------------------
89 local function on_player_joined_game(event)
90 -- called in SP(once) and MP(at every connect), eventually after on_player_created
91 local player = game.players[event.player_index]
92 debug_print( "player joined ", player.name )
97 script.on_event(defines.events.on_player_joined_game, on_player_joined_game )
99 --------------------------------------------------------------------------------------
100 local function on_gui_click(event)
101 local player = game.players[event.player_index]
102 local event_name = event.element.name
103 local prefix = string.sub(event_name,1,14)
104 local suffix = string.sub(event_name,15)
106 debug_print( "on_gui_click ", player.name, " ", event_name )
108 if event_name == "but_zoom_zin" then
109 local player_mem = global.player_mem[player.index]
110 if player_mem.zoom < zoom_close_max then
111 player_mem.zoom = player_mem.zoom * zoom_factor
112 player.zoom = player_mem.zoom
115 elseif event_name == "but_zoom_zout" then
116 local player_mem = global.player_mem[player.index]
117 if player_mem.zoom > zoom_wide_max then
118 player_mem.zoom = player_mem.zoom / zoom_factor
119 player.zoom = player_mem.zoom
124 script.on_event(defines.events.on_gui_click, on_gui_click)
126 --------------------------------------------------------------------------------------
127 local function on_zoom_hotkey(event)
128 local player = game.players[event.player_index]
129 local player_mem = global.player_mem[player.index]
130 player_mem.zoom = zoom_wide_hotkey
131 player.zoom = player_mem.zoom
134 script.on_event("zoom_hotkey", on_zoom_hotkey)
136 --------------------------------------------------------------------------------------
137 function build_bar( player )
138 -- here only build gui, but not try to update it with player data that is not always already available (in init_player for example)
139 local guif = player.gui.top.flw_zoom
142 debug_print("create gui player" .. player.name)
143 guif = player.gui.top.add({type = "flow", name = "flw_zoom", direction = "horizontal", style = "zoom_flow_style"})
144 guif.add({type = "sprite-button", name = "but_zoom_zout", sprite = "sprite_zout", style = "zoom_sprite_style"})
145 guif.add({type = "sprite-button", name = "but_zoom_zin", sprite = "sprite_zin", style = "zoom_sprite_style"})
149 --------------------------------------------------------------------------------------
152 function interface.reset()
153 debug_print( "reset" )
155 for _, player in pairs(game.players) do
156 if player.gui.top.flw_zoom then player.gui.top.flw_zoom.destroy() end
161 remote.add_interface( "zoom", interface )
163 -- /c remote.call( "zoom", "reset" )