]> git.somenet.org - factorio/some-zoom.git/blob - control.lua
e0f4b9e9868346095acf0ecc13cd579da9fa39dc
[factorio/some-zoom.git] / control.lua
1 -- debug_status = 1
2 debug_mod_name = "Zoom"
3 debug_file = debug_mod_name .. "-debug.txt"
4 require("utils")
5
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
10
11 --------------------------------------------------------------------------------------
12 local function init_globals()
13     -- initialize or update general globals of the mod
14     debug_print( "init_globals " )
15
16     global.ticks = global.ticks or 0
17     global.force_mem = global.force_mem or {}
18     global.player_mem = global.player_mem or {}
19 end
20
21 --------------------------------------------------------------------------------------
22 local function init_player(player)
23     if global.player_mem == nil then return end
24
25     -- initialize or update per player globals of the mod, and reset the gui
26     debug_print( "init_player ", player.name, " connected=", player.connected )
27
28     global.player_mem[player.index] = global.player_mem[player.index] or {}
29
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
33
34     if player.connected then
35         build_bar(player)
36     end
37 end
38
39 --------------------------------------------------------------------------------------
40 local function init_players()
41     for _, player in pairs(game.players) do
42         init_player(player)
43     end
44 end
45
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" )
50     init_globals()
51     init_players()
52 end
53
54 script.on_init(on_init)
55
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) )
63
64             init_globals()
65             init_players()
66
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 !")
69             end
70
71         end
72     end
73 end
74
75 script.on_configuration_changed(on_configuration_changed)
76
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 )
82
83     init_player(player)
84 end
85
86 script.on_event(defines.events.on_player_created, on_player_created )
87
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 )
93
94     init_player(player)
95 end
96
97 script.on_event(defines.events.on_player_joined_game, on_player_joined_game )
98
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)
105
106     debug_print( "on_gui_click ", player.name, " ", event_name )
107
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
113         end
114
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
120         end
121     end
122 end
123
124 script.on_event(defines.events.on_gui_click, on_gui_click)
125
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
132 end
133
134 script.on_event("zoom_hotkey", on_zoom_hotkey)
135
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
140
141     if guif == nil then
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"})
146     end
147 end
148
149 --------------------------------------------------------------------------------------
150 local interface = {}
151
152 function interface.reset()
153     debug_print( "reset" )
154
155     for _, player in pairs(game.players) do
156         if player.gui.top.flw_zoom then player.gui.top.flw_zoom.destroy() end
157         init_player(player)
158     end
159 end
160
161 remote.add_interface( "zoom", interface )
162
163 -- /c remote.call( "zoom", "reset" )
164