1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/comfy_panel/main.lua

138 lines
4.0 KiB
Lua
Raw Normal View History

2019-10-20 11:48:14 +02:00
--[[
Comfy Panel
To add a tab, insert into the "comfy_panel_tabs" table.
Example: comfy_panel_tabs["mapscores"] = draw_map_scores
2019-10-28 18:38:36 +02:00
2019-10-20 11:48:14 +02:00
draw_map_scores would be a function with the player and the frame as arguments
]]
2019-10-28 18:38:36 +02:00
local event = require 'utils.event'
2019-10-20 11:48:14 +02:00
comfy_panel_tabs = {}
2019-10-28 18:38:36 +02:00
local Public = {}
function Public.get_tabs(data)
return comfy_panel_tabs
end
function Public.comfy_panel_clear_left_gui(player)
2019-10-24 08:01:39 +02:00
for _, child in pairs(player.gui.left.children) do
child.destroy()
end
end
2019-10-28 18:38:36 +02:00
function Public.comfy_panel_get_active_frame(player)
2019-10-20 11:48:14 +02:00
if not player.gui.left.comfy_panel then return false end
if not player.gui.left.comfy_panel.tabbed_pane.selected_tab_index then return player.gui.left.comfy_panel.tabbed_pane.tabs[1].content end
return player.gui.left.comfy_panel.tabbed_pane.tabs[player.gui.left.comfy_panel.tabbed_pane.selected_tab_index].content
end
2019-10-28 18:38:36 +02:00
function Public.comfy_panel_refresh_active_tab(player)
local frame = Public.comfy_panel_get_active_frame(player)
2019-10-20 11:48:14 +02:00
if not frame then return end
comfy_panel_tabs[frame.name](player, frame)
end
local function top_button(player)
if player.gui.top["comfy_panel_top_button"] then return end
local button = player.gui.top.add({type = "sprite-button", name = "comfy_panel_top_button", sprite = "item/raw-fish"})
button.style.minimal_height = 38
button.style.minimal_width = 38
2019-10-28 18:38:36 +02:00
button.style.padding = -2
2019-10-20 11:48:14 +02:00
end
local function main_frame(player)
2019-10-28 18:38:36 +02:00
local tabs = comfy_panel_tabs
Public.comfy_panel_clear_left_gui(player)
2019-10-20 11:48:14 +02:00
local frame = player.gui.left.add({type = "frame", name = "comfy_panel"})
2019-10-20 14:25:22 +02:00
frame.style.margin = 6
2019-10-28 18:38:36 +02:00
2019-10-20 11:48:14 +02:00
local tabbed_pane = frame.add({type = "tabbed-pane", name = "tabbed_pane"})
2019-10-28 18:38:36 +02:00
for name, func in pairs(tabs) do
2019-10-20 11:48:14 +02:00
if name == "Admin" then
if player.admin then
local tab = tabbed_pane.add({type = "tab", caption = name})
local frame = tabbed_pane.add({type = "frame", name = name, direction = "vertical"})
frame.style.minimal_height = 480
2019-10-24 08:01:39 +02:00
frame.style.maximal_height = 480
2019-10-20 11:48:14 +02:00
frame.style.minimal_width = 800
frame.style.maximal_width = 800
tabbed_pane.add_tab(tab, frame)
end
else
local tab = tabbed_pane.add({type = "tab", caption = name})
local frame = tabbed_pane.add({type = "frame", name = name, direction = "vertical"})
frame.style.minimal_height = 480
2019-10-24 08:01:39 +02:00
frame.style.maximal_height = 480
2019-10-20 11:48:14 +02:00
frame.style.minimal_width = 800
frame.style.maximal_width = 800
tabbed_pane.add_tab(tab, frame)
end
end
2019-10-28 18:38:36 +02:00
2019-10-22 08:27:00 +02:00
local tab = tabbed_pane.add({type = "tab", name = "comfy_panel_close", caption = "X"})
tab.style.maximal_width = 32
local frame = tabbed_pane.add({type = "frame", name = name, direction = "vertical"})
tabbed_pane.add_tab(tab, frame)
2019-10-28 18:38:36 +02:00
2019-10-22 08:27:00 +02:00
for _, child in pairs(tabbed_pane.children) do
child.style.padding = 8
child.style.left_padding = 2
child.style.right_padding = 2
end
2019-10-28 18:38:36 +02:00
Public.comfy_panel_refresh_active_tab(player)
2019-10-20 14:25:22 +02:00
end
2019-10-28 18:38:36 +02:00
function Public.comfy_panel_call_tab(player, name)
2019-10-20 14:25:22 +02:00
main_frame(player)
local tabbed_pane = player.gui.left.comfy_panel.tabbed_pane
for key, v in pairs(tabbed_pane.tabs) do
if v.tab.caption == name then
tabbed_pane.selected_tab_index = key
2019-10-28 18:38:36 +02:00
Public.comfy_panel_refresh_active_tab(player)
end
2019-10-20 14:25:22 +02:00
end
2019-10-20 11:48:14 +02:00
end
local function on_player_joined_game(event)
top_button(game.players[event.player_index])
end
local function on_gui_click(event)
if not event.element then return end
if not event.element.valid then return end
local player = game.players[event.player_index]
2019-10-28 18:38:36 +02:00
2019-10-20 11:48:14 +02:00
if event.element.name == "comfy_panel_top_button" then
if player.gui.left.comfy_panel then
player.gui.left.comfy_panel.destroy()
return
else
main_frame(player)
return
2019-10-28 18:38:36 +02:00
end
2019-10-20 11:48:14 +02:00
end
2019-10-28 18:38:36 +02:00
2019-10-22 08:27:00 +02:00
if event.element.caption == "X" and event.element.name == "comfy_panel_close" then
player.gui.left.comfy_panel.destroy()
return
end
2019-10-28 18:38:36 +02:00
2019-10-20 11:48:14 +02:00
if not event.element.caption then return end
if event.element.type ~= "tab" then return end
2019-10-28 18:38:36 +02:00
Public.comfy_panel_refresh_active_tab(player)
2019-10-20 11:48:14 +02:00
end
event.add(defines.events.on_player_joined_game, on_player_joined_game)
2019-10-28 18:38:36 +02:00
event.add(defines.events.on_gui_click, on_gui_click)
return Public