-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutil.lua
98 lines (89 loc) · 3.07 KB
/
util.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
--- @class Util
local util = {}
local coreutil = require("__core__.lualib.util")
util.parse_energy = coreutil.parse_energy
--- @param handler flib.GuiElemHandler
function util.close_button(handler)
return {
type = "sprite-button",
style = "frame_action_button",
sprite = "utility/close",
-- hovered_sprite = "utility/close_black",
-- clicked_sprite = "utility/close_black",
tooltip = { "gui.close-instruction" },
mouse_button_filter = { "left" },
handler = { [defines.events.on_gui_click] = handler },
}
end
--- @param mode string
--- @param handler flib.GuiElemHandler
--- @return flib.GuiElemDef
function util.mode_radio_button(mode, handler)
return {
type = "radiobutton",
name = "mode_radio_button_" .. string.gsub(mode, "%-", "_"),
caption = { "gui-infinity-container." .. mode },
tooltip = { "gui-infinity-pipe." .. mode .. "-tooltip" },
state = false,
tags = { mode = mode },
handler = { [defines.events.on_gui_checked_state_changed] = handler },
}
end
function util.pusher()
return { type = "empty-widget", style = "flib_horizontal_pusher", ignored_by_interaction = true }
end
--- @return boolean
function util.in_debug_world()
if not settings.global["ee-override-debug-world"].value then
return false
end
if script.level.mod_name ~= "base" or script.level.level_name ~= "freeplay" then
return false
end
local nauvis = game.get_surface("nauvis")
if not nauvis then
return false
end
local mps = nauvis.map_gen_settings
return mps.height == 50 and mps.width == 50
end
--- @return boolean
function util.in_testing_scenario()
return script.level.mod_name == "EditorExtensions" and script.level.level_name == "testing"
end
--- @param player LuaPlayer
--- @param message LocalisedString
--- @param play_sound boolean?
--- @param position MapPosition?
function util.flying_text(player, message, play_sound, position)
player.create_local_flying_text({
text = message,
create_at_cursor = not position,
position = position,
})
if play_sound then
player.play_sound({ path = "utility/cannot_build" })
end
end
--- @param player LuaPlayer
--- @param to_state boolean?
--- @return boolean
function util.player_can_use_editor(player, to_state)
local can_use_editor = to_state
if can_use_editor == nil then
local permission_group = player.permission_group
if permission_group then
can_use_editor = permission_group.allows_action(defines.input_action.toggle_map_editor)
if not can_use_editor and player.controller_type == defines.controllers.editor then
player.print({ "message.ee-cannot-use-map-editor" })
-- XXX: We need to re-enable the capability in order to get them out of the editor
permission_group.set_allows_action(defines.input_action.toggle_map_editor, true)
player.toggle_map_editor()
permission_group.set_allows_action(defines.input_action.toggle_map_editor, false)
end
end
end
player.set_shortcut_available("ee-toggle-map-editor", can_use_editor or false)
return can_use_editor and player.admin or false
end
return util