1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/features/gui/popup.lua
Matthew a84dcfdd91 Overhaul commands (#576)
* Split admin-only commands out of custom commands
Remove free item logging

* Overhaul commands remaining in custom_commands

* Overhaul commands in remaining files

* Change custom_commands to redmew_commands

* Remove follow.lua
2019-01-02 16:34:17 +01:00

180 lines
4.9 KiB
Lua

local Gui = require 'utils.gui'
local Utils = require 'utils.core'
local Game = require 'utils.game'
local Command = require 'utils.command'
local close_name = Gui.uid_name()
local function show_popup(player, message)
local frame = player.gui.center.add {type = 'frame', direction = 'vertical', style = 'captionless_frame'}
frame.style.minimal_width = 300
local top_flow = frame.add {type = 'flow', direction = 'horizontal'}
local title_flow = top_flow.add {type = 'flow'}
title_flow.style.align = 'center'
title_flow.style.left_padding = 32
title_flow.style.top_padding = 8
title_flow.style.horizontally_stretchable = true
local title = title_flow.add {type = 'label', caption = 'Attention!'}
title.style.font = 'default-large-bold'
local close_button_flow = top_flow.add {type = 'flow'}
close_button_flow.style.align = 'right'
local close_button = close_button_flow.add {type = 'button', name = close_name, caption = 'X'}
Gui.set_data(close_button, frame)
local content_flow = frame.add {type = 'flow', direction = 'horizontal'}
content_flow.style.top_padding = 16
content_flow.style.bottom_padding = 16
content_flow.style.left_padding = 24
content_flow.style.right_padding = 24
content_flow.style.horizontally_stretchable = true
local sprite_flow = content_flow.add {type = 'flow'}
sprite_flow.style.vertical_align = 'center'
sprite_flow.style.vertically_stretchable = true
sprite_flow.add {type = 'sprite', sprite = 'utility/warning_icon'}
local label_flow = content_flow.add {type = 'flow'}
label_flow.style.align = 'left'
label_flow.style.top_padding = 10
label_flow.style.left_padding = 24
label_flow.style.horizontally_stretchable = true
local label = label_flow.add {type = 'label', caption = message}
label.style.single_line = false
label.style.font = 'default-large-bold'
local ok_button_flow = frame.add {type = 'flow'}
ok_button_flow.style.horizontally_stretchable = true
ok_button_flow.style.align = 'center'
local ok_button = ok_button_flow.add {type = 'button', name = close_name, caption = 'OK'}
Gui.set_data(ok_button, frame)
end
Gui.on_click(
close_name,
function(event)
local frame = Gui.get_data(event.element)
Gui.remove_data_recursively(frame)
frame.destroy()
end
)
-- Creates a popup dialog for all players
local function popup(args)
local message = args.message:gsub('\\n', '\n')
for _, p in ipairs(game.connected_players) do
show_popup(p, message)
end
Game.player_print('Popup sent')
Utils.print_admins(Utils.get_actor() .. ' sent a popup to all players', nil)
end
-- Creates a popup dialog for all players, specifically for the server upgrading factorio versions
local function popup_update(args)
local message = '\nServer updating to ' .. args.version .. ', back in one minute.'
for _, p in ipairs(game.connected_players) do
show_popup(p, message)
end
Game.player_print('Popup sent')
Utils.print_admins(Utils.get_actor() .. ' sent a popup to all players', nil)
end
-- Creates a popup dialog for the specifically targetted player
local function popup_player(args)
local target_name = args.player
local target = game.players[target_name]
if not target then
Game.player_print('Player ' .. target_name .. ' not found.')
return
end
local message = args.message:gsub('\\n', '\n')
show_popup(target, message)
Game.player_print('Popup sent')
end
Command.add(
'popup',
{
description = 'Shows a popup to all connected players',
arguments = {'message'},
admin_only = true,
capture_excess_arguments = true,
allowed_by_server = true
},
popup
)
Command.add(
'popup-update',
{
description = 'Shows an update popup to all connected players',
arguments = {'version'},
admin_only = true,
capture_excess_arguments = true,
allowed_by_server = true
},
popup_update
)
Command.add(
'popup-player',
{
description = 'Shows a popup to the player.',
arguments = {'player', 'message'},
admin_only = true,
capture_excess_arguments = true,
allowed_by_server = true
},
popup_player
)
local Public = {}
--[[--
Shows a popup dialog.
@param player LuaPlayer
@param message string
]]
function Public.player(player, message)
show_popup(player, message)
end
--[[--
Shows a popup dialog to all connected players.
@param message string
]]
function Public.all_online(message)
for _, p in ipairs(game.connected_players) do
show_popup(p, message)
end
end
--[[--
Shows a popup dialog to all players.
@param message string
]]
function Public.all(message)
for _, p in pairs(game.players) do
show_popup(p, message)
end
end
return Public