-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcore.lua
More file actions
206 lines (175 loc) · 5.19 KB
/
core.lua
File metadata and controls
206 lines (175 loc) · 5.19 KB
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
local Event = require 'utils.event'
local Gui = require 'utils.gui'
local Global = require 'utils.global'
local Config = require 'config'.admin_panel
local main_button_name = Gui.uid_name()
local main_frame_name = Gui.uid_name()
local close_button_name = Gui.uid_name()
local pages = {
--[[
{
type = 'sprite-button',
sprite = 'item/programmable-speaker',
tooltip = 'First page',
},
{
type = 'sprite-button',
sprite = 'utility/spawn_flag',
tooltip = 'Second page',
},
{
type = 'sprite-button',
sprite = 'utility/scripting_editor_icon',
tooltip = 'Third page',
},
{
type = 'sprite-button',
sprite = 'utility/surface_editor_icon',
tooltip = 'Fourth page',
},
]]
}
Global.register(pages, function(tbl) pages = tbl end)
local Public = {}
function Public.get_pages()
return pages
end
function Public.get_canvas(player)
return Gui.get_data(Public.get_main_frame(player)).right
end
function Public.get_main_frame(player)
local frame = player.gui.screen[main_frame_name]
if frame and frame.valid then
return frame
end
frame = player.gui.screen.add {
type = 'frame',
name = main_frame_name,
direction = 'vertical',
style = 'frame',
tags = { [Gui.event_tag] = main_frame_name },
}
frame.auto_center = true
player.opened = frame
Gui.set_style(frame, {
horizontally_stretchable = true,
vertically_stretchable = true,
natural_width = 400,
natural_height = 400,
top_padding = 8,
bottom_padding = 8,
})
local data = {}
do -- title
local flow = frame.add { type = 'flow', direction = 'horizontal' }
Gui.set_style(flow, { horizontal_spacing = 8, vertical_align = 'center', bottom_padding = 4 })
local label = flow.add { type = 'label', caption = 'Admin panel', style = 'frame_title' }
label.drag_target = frame
local dragger = flow.add { type = 'empty-widget', style = 'draggable_space_header' }
dragger.drag_target = frame
Gui.set_style(dragger, { height = 24, horizontally_stretchable = true })
flow.add {
type = 'sprite-button',
sprite = 'utility/close',
clicked_sprite = 'utility/close_black',
style = 'close_button',
tooltip = {'gui.close-instruction'},
tags = { [Gui.event_tag] = close_button_name },
}
end
local main_flow = frame.add { type = 'flow', name = 'flow', direction = 'horizontal' }
Gui.set_style(main_flow, { horizontal_spacing = 12 })
do -- left
local left = main_flow
.add { type = 'flow', name = 'left', direction = 'vertical' }
.add { type = 'frame', direction = 'vertical', style = 'inside_deep_frame' }
.add { type = 'flow', direction = 'vertical' }
Gui.set_style(left, {
vertically_stretchable = true,
horizontal_align = 'center',
padding = 10,
vertical_spacing = 5,
})
for _, page in pairs(pages) do
left.add(page)
end
data.left = left
end
do -- right
local right = main_flow
.add { type = 'frame', name = 'right', style = 'inside_shallow_frame_with_padding' }
.add { type = 'flow', name = 'flow', direction = 'vertical' }
Gui.set_style(right, {
minimal_width = 300,
minimal_height = 300,
vertically_stretchable = true,
horizontally_stretchable = true,
})
data.right = right
end
Gui.set_data(frame, data)
end
function Public.update_top_button(player)
if not Config.enabled then
return
end
local button = Gui.add_top_element(player, {
type = 'sprite-button',
name = main_button_name,
sprite = 'item/power-armor-mk2',
tooltip = {'admin_panel.info_tooltip'},
tags = { [Gui.event_tag] = main_button_name },
})
button.visible = player.admin
end
function Public.toggle_main_button(player)
local main_frame = player.gui.screen[main_frame_name]
if main_frame then
Gui.destroy(main_frame)
else
Public.get_main_frame(player)
end
end
function Public.close_all_pages(player)
local frame = player.gui.screen[main_frame_name]
if not (frame and frame.valid) then
return
end
for _, button in pairs(Gui.get_data(frame).left.children) do
if button.type == 'button' or button.type == 'sprite-button' then
button.toggled = false
end
end
end
Event.add(defines.events.on_player_created, function(event)
local player = game.get_player(event.player_index)
if not (player and player.valid) then
return
end
Public.update_top_button(player)
end)
Event.add(defines.events.on_player_joined_game, function(event)
local player = game.get_player(event.player_index)
if not (player and player.valid) then
return
end
Public.update_top_button(player)
local frame = player.gui.screen[main_frame_name]
if (frame and frame.valid and not player.admin) then
Gui.destroy(frame)
end
end)
Gui.on_custom_close(main_frame_name, function(event)
Public.toggle_main_button(event.player)
end)
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
Gui.on_click(main_button_name, function(event)
Public.toggle_main_button(event.player)
end)
Gui.on_click(close_button_name, function(event)
Public.toggle_main_button(event.player)
end)
Gui.on_player_show_top(main_button_name, function(event)
Public.update_top_button(event.player)
end)
return Public