/
control.lua
342 lines (298 loc) · 8.16 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
require( "defines" )
-------------------------------
-- Tables to keep track of data
-------------------------------
local TimeLapses = {}
local flashback = -1
-------------------------------
-- Helper functions
-------------------------------
local function FindByName (arr, name)
for i, v in ipairs(arr) do
if arr.name == name then
return i
end
end
return nil
end
-------------------------------
-- Button callbacks
-------------------------------
--add new
local function addTimeLapse(event)
game.get_player(event.player_index).gui.left["TimeLapseMaker_MainMenu_frame"].destroy()
local centerGUI = game.get_player(event.player_index).gui.center
if not centerGUI["TimeLapseMaker_AddNew_frame"] then
local AddMenu = centerGUI.add({
type = "frame",
caption = "Add New TimeLapse",
direction = "vertical",
name = "TimeLapseMaker_AddNew_frame"
})
AddMenu.add{
type = "label",
name = "info",
caption = "The Images will be taken from your current position"
}
NameFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_Name_flow"
}
NameFlow.add{
type = "label",
caption = "Name of This TimeLapse",
name = "TimeLapseMaker_Name_label"
}
NameFlow.add{
type = "textfield",
name = "TimeLapseMaker_Name_textfield"
}
PathFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_Path_flow"
}
PathFlow.add{
type = "label",
caption = "Folder for screenshots",
name = "TimeLapseMaker_Path_label"
}
PathFlow.add{
type = "textfield",
name = "TimeLapseMaker_Path_textfield"
}
DelayFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_Delay_flow"
}
DelayFlow.add{
type = "label",
caption = "Time Between Screenshots (sec)",
name = "TimeLapseMaker_Delay_label"
}
DelayFlow.add{
type = "textfield",
name = "TimeLapseMaker_Delay_textfield"
}
ZoomFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_Zoom_flow"
}
ZoomFlow.add{
type = "label",
caption = "Zoom Level (all the way in = 3, all the wat out = 0.18",
name = "TimeLapseMaker_Zoom_label"
}
ZoomFlow.add{
type = "textfield",
name = "TimeLapseMaker_Zoom_textfield"
}
DurationFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_Duration_flow"
}
DurationFlow.add{
type = "label",
caption = "duration (sec)(0 = forever)",
name = "TimeLapseMaker_Duration_label"
}
DurationFlow.add{
type = "textfield",
name = "TimeLapseMaker_Duration_textfield"
}
AltModeFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_AltMode_flow"
}
AltModeFlow.add{
type = "checkbox",
state = false,
name = "TimeLapseMaker_AltMode_checkbox"
}
AltModeFlow.add{
type = "label",
caption = "Show Entity Info? (Alt-Mode)",
name = "TimeLapseMaker_AltMode_label"
}
NightlessFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_Nightless_flow"
}
NightlessFlow.add{
type = "checkbox",
state = false,
name = "TimeLapseMaker_Nightless_checkbox"
}
NightlessFlow.add{
type = "label",
caption = "Nightless (always day in screenshots)",
name = "TimeLapseMaker_Nightless_label"
}
ButtonFlow = AddMenu.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_Buttons_flow"
}
ButtonFlow.add{
type = "button",
caption = "Accept",
name = "TimeLapseMaker_AcceptNew"
}
ButtonFlow.add{
type = "button",
caption = "Cancel",
name = "TimeLapseMaker_CancelNew"
}
end
end
local function AcceptNew(event)
local ply = game.get_player(event.player_index)
local frame = ply.gui.center["TimeLapseMaker_AddNew_frame"]
local nm = frame["TimeLapseMaker_Name_flow"]["TimeLapseMaker_Name_textfield"].text
local zm = tonumber( frame["TimeLapseMaker_Zoom_flow"]["TimeLapseMaker_Zoom_textfield"].text)
local pth = frame["TimeLapseMaker_Path_flow"]["TimeLapseMaker_Path_textfield"].text
local del = tonumber(frame["TimeLapseMaker_Delay_flow"]["TimeLapseMaker_Delay_textfield"].text)
local dur = tonumber(frame["TimeLapseMaker_Duration_flow"]["TimeLapseMaker_Duration_textfield"].text)
local alt = frame["TimeLapseMaker_AltMode_flow"]["TimeLapseMaker_AltMode_checkbox"].state
local night = frame["TimeLapseMaker_Nightless_flow"]["TimeLapseMaker_Nightless_checkbox"].state
local pos = ply.position
if not nm or nm == "" then
ply.print("invalid name")
elseif FindByName(TimeLapses[event.player_index], nm) then
ply.print("Name already used")
elseif not zm or zm <= 0 then
ply.print("invalid zoom")
elseif not pth or path == "" then
ply.print("invalid path")
elseif not del or del <= 0 then
ply.print("invalid delay")
elseif not dur or dur < 0 then
ply.print("invalid duration")
else
table.insert(TimeLapses[event.player_index], {
path = pth,
delay = math.floor(del * 60 + .5),
duration = math.floor(dur * 60 + .5),
zoom = zm,
altMode = alt,
position = pos,
name = nm,
nightless = night,
start = game.tick
})
frame.destroy()
end
end
local function CancelNew(event)
game.get_player(event.player_index).gui.center["TimeLapseMaker_AddNew_frame"].destroy()
end
local function MainMenu(event)
local ply = game.get_player(event.player_index)
if ply.gui.left["TimeLapseMaker_MainMenu_frame"] then
ply.gui.left["TimeLapseMaker_MainMenu_frame"].destroy()
else
local Menu = ply.gui.left.add({
type = "frame",
caption = "TimeLapses",
direction = "vertical",
name = "TimeLapseMaker_MainMenu_frame"
})
Menu.add{
type = "button",
caption = "Add New",
name = "TimeLapseMaker_MainMenu_AddNew"
}
local List = Menu.add{
type = "flow",
direction = "vertical",
name = "TimeLapseMaker_MainMenu_Listflow"
}
for i, v in ipairs(TimeLapses[event.player_index]) do
local flow = List.add{
type = "flow",
direction = "horizontal",
name = "TimeLapseMaker_" .. i .. "_flow"
}
flow.add{
type = "button",
caption = "x",
name = "TimeLapseMaker_" .. i .. "_delete"
--index = i
}
flow.add{
type = "label",
caption = v.name,
name = "TimeLapseMaker_" .. i .. "_label"
}
end
end
end
local function deleteTimeLapse (event)
table.remove(TimeLapses[event.player_index], event.element.index)
event.element.parent.destroy()
end
-------------------------------
-- Events
-------------------------------
script.on_event( defines.events.on_player_created, function( event )
game.get_player( event.player_index ).gui.left.add{
type = "button",
caption = "TimeLapses",
name = "TimeLapseMaker_MainMenu"
}
TimeLapses[event.player_index] = {} -- create player's table of TimeLapses
end)
local gui_element_callbacks = {
TimeLapseMaker_MainMenu_AddNew = addTimeLapse,
TimeLapseMaker_AcceptNew = AcceptNew,
TimeLapseMaker_CancelNew = CancelNew,
TimeLapseMaker_MainMenu = MainMenu
}
-- Handle callbacks
script.on_event(defines.events.on_gui_click ,function(event)
local element_name = event.element.name
for k,v in pairs( gui_element_callbacks ) do
if string.find( element_name, k ) then
v( event )
return
end
end
if string.find(element_name, "TimeLapseMaker_%d+_delete") then
deleteTimeLapse(event)
return
end
end)
script.on_event(defines.events.on_tick, function(event)
--can you tell i don't understand the precedence of "not"???
if ((not (flashback == nil)) and (not (flashback == -1))) then -- -1 is dummmy value
game.daytime = flashback
flashback = -1
end
for i, p in ipairs(TimeLapses) do
for j, v in ipairs(TimeLapses[i]) do
local dif = game.tick - v.start
if dif % v.delay == 0 then
if v.nightless then
flashback = game.daytime
game.daytime = 0
end
game.take_screenshot{
player = game.get_player(i),
position = v.position,
zoom = v.zoom,
path = v.path .. "/" .. string.format("%03d", dif / v.delay ) .. ".png",
show_entity_info = v.altMode
}
end
if (not (v.duration == 0)) and dif >= v.duration then -- if duration is 0, never remove it
table.remove(TimeLapses[i], j)
end
end
end
end)