-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.lua
259 lines (230 loc) · 8.38 KB
/
gui.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
local mod_gui = require("mod-gui")
local history_properties = require("history-properties")
-- Helpers
function show_time(label, ticks)
label.caption = string.format("%.1f", ticks / 60.0)
end
-- gui module
local gui = {}
gui.kill = function(player)
if global.gui_frames and global.gui_frames[player.index] then
for _, frame in pairs(global.gui_frames[player.index]) do
frame.destroy()
end
end
global.gui_frames = global.gui_frames or {}
global.gui_frames[player.index] = global.gui_frames[player.index] or {}
global.gui_frames[player.index] = {}
global.gui_elements = global.gui_elements or {}
global.gui_elements[player.index] = global.gui_elements[player.index] or {}
global.gui_elements[player.index] = {}
end
gui.init = function()
global.gui_elements = {}
global.gui_frames = {}
end
gui.regen = function(player)
gui.kill(player)
local gui_frames = global.gui_frames[player.index]
local gui_elements = global.gui_elements[player.index]
local frame_flow = mod_gui.get_frame_flow(player)
gui_frames.control = frame_flow.add {
name = "srt-control-frame",
type = "frame",
direction = "vertical",
caption = "Speedrun Trainer"
}
gui_elements.label_status = gui_frames.control.add {
type = "label",
caption = ""
}
local table_info = gui_frames.control.add {type = "table", column_count = 2}
width = 160
table_info.add {type = "label", caption = "Task"}
gui_elements.input_task = table_info.add {
type = "textfield",
text = "default task"
}
gui_elements.input_task.style.width = width
table_info.add {type = "label", caption = "Method"}
gui_elements.input_method = table_info.add {
type = "textfield",
text = "default method"
}
gui_elements.input_method.style.width = width
table_info.add {type = "label", caption = "Entities"}
gui_elements.label_entities = table_info.add {
type = "label",
style = "caption_label"
}
gui_elements.label_entities.style.horizontal_align = "right"
gui_elements.label_entities.style.width = width
table_info.add {type = "label", caption = "Mistakes"}
gui_elements.label_mistakes = table_info.add {
type = "label",
style = "caption_label"
}
gui_elements.label_mistakes.style.horizontal_align = "right"
gui_elements.label_mistakes.style.width = width
table_info.add {type = "label", caption = "Time"}
gui_elements.label_time = table_info.add {
type = "label",
style = "caption_label"
}
gui_elements.label_time.style.horizontal_align = "right"
gui_elements.label_time.style.width = width
flow_buttons = gui_frames.control.add {
type = "flow",
direction = "horizontal"
}
button_width = 70
gui_elements.button_history = flow_buttons.add {
type = "button",
caption = "history"
}
gui_elements.button_history.style.width = button_width
gui_elements.button_start = flow_buttons.add {
type = "button",
caption = "start"
}
gui_elements.button_start.style.width = button_width
gui_elements.button_cancel = flow_buttons.add {
type = "button",
caption = "cancel"
}
gui_elements.button_cancel.style.width = button_width
gui_elements.button_stop = flow_buttons.add {
type = "button",
caption = "stop"
}
gui_elements.button_stop.style.width = button_width
gui_elements.button_reset = flow_buttons.add {
type = "button",
caption = "reset"
}
gui_elements.button_reset.style.width = button_width
gui.render_controls(player)
-- History gui
gui_frames.history = player.gui.screen.add {
type = "frame",
direction = "vertical",
visible = "false",
caption = "Training History"
}
gui_elements.pane_history = gui_frames.history.add {type = "scroll-pane"}
gui_elements.pane_history.style.maximal_height = 800
local gui_flow_history_controls = gui_frames.history.add {
type = "flow",
direction = "horizontal"
}
gui_elements.button_history_clear = gui_flow_history_controls.add {
type = "button",
caption = "clear"
}
gui.render_history(player)
gui_frames.history.force_auto_center()
end
gui.render_controls = function(player)
local gui_elements = global.gui_elements[player.index]
local state = global.state[player.index]
if state.running then
show_time(gui_elements.label_time, game.tick - state.tick_start)
else
show_time(gui_elements.label_time, state.tick_active - state.tick_start)
end
gui_elements.label_mistakes.caption = string.format("%d", state.mistakes)
gui_elements.label_entities.caption =
string.format("%d", table_size(state.entities))
if state.waiting_for_events then
gui_elements.button_start.visible = false
gui_elements.button_stop.visible = false
gui_elements.button_cancel.visible = true
gui_elements.button_reset.visible = false
gui_elements.label_status.caption = "waiting for events"
elseif state.running then
gui_elements.button_start.visible = false
gui_elements.button_cancel.visible = true
gui_elements.button_stop.visible = true
gui_elements.button_reset.visible = false
gui_elements.label_status.caption = "GO!"
elseif table_size(state.entities) > 0 then
gui_elements.button_start.visible = false
gui_elements.button_cancel.visible = false
gui_elements.button_stop.visible = false
gui_elements.button_reset.visible = true
gui_elements.label_status.caption =
string.format("placed %d entities", table_size(state.entities))
else
gui_elements.button_start.visible = true
gui_elements.button_cancel.visible = false
gui_elements.button_stop.visible = false
gui_elements.button_reset.visible = false
gui_elements.label_status.caption = "ready"
end
end
gui.render_history = function(player)
local gui_elements = global.gui_elements[player.index]
local history_grouping = global.history_grouping[player.index]
if gui_elements.table_history then
gui_elements.table_history.destroy()
end
gui_elements.table_history = gui_elements.pane_history.add {
type = "table",
column_count = #history_properties,
style = "bordered_table"
}
gui_elements.table_history_grouping = {}
for _, property in pairs(history_properties) do
if property.groupable then
gui_elements.table_history_grouping[property] =
gui_elements.table_history.add {
type = "checkbox",
caption = property.name,
-- toboolean would be too hard to be in the standard library
state = (history_grouping[property.name] and true) or false
}
else
gui_elements.table_history.add {
type = "label",
caption = property.name,
style = "caption_label"
}
end
end
if table_size(history_grouping) == 0 then
for _, entry in global.history:pairs() do
for _, property in pairs(history_properties) do
local caption = entry[property.name]
if property.format ~= nil then
caption = property.format(caption)
end
gui_elements.table_history.add {
type = "label",
caption = caption
}
end
end
else
for _, entry in pairs(global.history:group_by(history_grouping)) do
for _, property in pairs(history_properties) do
local caption = entry:summary(property.name, property.format)
gui_elements.table_history.add {
type = "label",
caption = caption
}
end
end
end
end
gui.toggle_history = function(player)
local gui_frames = global.gui_frames[player.index]
gui_frames.history.visible = not gui_frames.history.visible
end
gui.input_properties = function(player)
local gui_elements = global.gui_elements[player.index]
return {
task = gui_elements.input_task.text,
method = gui_elements.input_method.text
}
end
return gui