]> git.somenet.org - factorio/some-zoom.git/blob - control.lua
RELEASE 2.0.0 - Initial compatibility with Factorio 2.0
[factorio/some-zoom.git] / control.lua
1 debug_mod_name = "Zoom"
2 require("utils")
3
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
8 local player_mem = {}
9
10 --------------------------------------------------------------------------------------
11 local function init_player(player)
12     if player_mem == nil then return end
13
14     -- initialize or update per player globals of the mod, and reset the gui
15     debug_print( "init_player ", player.name, " connected=", player.connected )
16
17     player_mem[player.index] = player_mem[player.index] or {}
18
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
22
23     if player.connected then
24         build_bar(player)
25     end
26 end
27
28 --------------------------------------------------------------------------------------
29 local function init_players()
30     for _, player in pairs(game.players) do
31         init_player(player)
32     end
33 end
34
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" )
39     init_players()
40 end
41
42 script.on_init(on_init)
43
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) )
51             init_players()
52         end
53     end
54 end
55
56 script.on_configuration_changed(on_configuration_changed)
57
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 )
63
64     init_player(player)
65 end
66
67 script.on_event(defines.events.on_player_created, on_player_created )
68
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 )
74
75     init_player(player)
76 end
77
78 script.on_event(defines.events.on_player_joined_game, on_player_joined_game )
79
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)
86
87     debug_print( "on_gui_click ", player.name, " ", event_name )
88
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
94         end
95
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
101         end
102     end
103 end
104
105 script.on_event(defines.events.on_gui_click, on_gui_click)
106
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
113 end
114
115 script.on_event("zoom_hotkey", on_zoom_hotkey)
116
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
121
122     if guif == nil then
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"})
127     end
128 end
129
130 --------------------------------------------------------------------------------------
131 local interface = {}
132
133 function interface.reset()
134     debug_print( "reset" )
135
136     for _, player in pairs(game.players) do
137         if player.gui.top.flw_zoom then player.gui.top.flw_zoom.destroy() end
138         init_player(player)
139     end
140 end
141
142 remote.add_interface( "zoom", interface )
143
144 -- /c remote.call( "zoom", "reset" )
145