1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-24 03:47:58 +02:00

312 lines
8.8 KiB
Lua
Raw Normal View History

2020-08-21 13:56:01 +02:00
local Public = {}
local ICT = require 'maps.mountain_fortress_v3.ic.table'
local Functions = require 'maps.mountain_fortress_v3.ic.functions'
local Gui = require 'maps.mountain_fortress_v3.ic.gui'
2023-11-12 00:04:54 +01:00
local CoreGui = require 'utils.gui'
2020-08-21 13:56:01 +02:00
2024-05-31 22:47:33 +02:00
local function get_top_frame_custom(player, name)
if CoreGui.get_mod_gui_top_frame() then
return CoreGui.get_button_flow(player)[name]
else
return player.gui.top[name]
end
end
2020-08-21 13:56:01 +02:00
local function validate_player(player)
if not player then
return false
end
if not player.valid then
return false
end
if not player.character then
return false
end
if not player.connected then
return false
end
if not game.players[player.name] then
return false
end
return true
end
2024-05-27 20:30:03 +02:00
local function get_top_frame(player)
if CoreGui.get_mod_gui_top_frame() then
return CoreGui.get_button_flow(player)['minimap_button']
else
return player.gui.top['minimap_button']
end
end
2020-08-21 13:56:01 +02:00
local function create_button(player)
2024-05-27 20:30:03 +02:00
if CoreGui.get_mod_gui_top_frame() then
local b =
CoreGui.add_mod_button(
player,
{
type = 'sprite-button',
name = 'minimap_button',
sprite = 'utility/map',
tooltip = 'Open or close minimap.',
2024-05-27 20:30:03 +02:00
style = Gui.button_style
}
)
2024-05-27 20:30:03 +02:00
if b then
b.style.font_color = {165, 165, 165}
b.style.font = 'heading-3'
b.style.minimal_height = 36
b.style.maximal_height = 36
b.style.minimal_width = 40
b.style.padding = -2
end
else
local button =
player.gui.top['minimap_button'] or
player.gui.top.add(
{
type = 'sprite-button',
name = 'minimap_button',
sprite = 'utility/map',
tooltip = 'Open or close minimap.',
style = CoreGui.button_style
}
)
button.style.minimal_height = 38
button.style.maximal_height = 38
end
2020-08-21 13:56:01 +02:00
end
function Public.toggle_button(player)
2024-05-27 20:30:03 +02:00
if not get_top_frame(player) then
2020-08-21 13:56:01 +02:00
create_button(player)
end
2024-05-27 20:30:03 +02:00
local button = get_top_frame(player)
if Functions.get_player_surface(player) then
create_button(player)
2020-08-21 13:56:01 +02:00
else
if button and button.valid then
button.destroy()
end
2020-08-21 13:56:01 +02:00
end
end
local function get_player_data(player)
local minimap = ICT.get('minimap')
local player_data = minimap[player.index]
if minimap[player.index] then
2020-08-21 13:56:01 +02:00
return player_data
end
minimap[player.index] = {
2020-08-21 13:56:01 +02:00
zoom = 0.30,
map_size = 360,
2021-01-11 15:58:12 +01:00
auto = true,
state = 'left'
2020-08-21 13:56:01 +02:00
}
return minimap[player.index]
2020-08-21 13:56:01 +02:00
end
function Public.toggle_auto(player)
2021-01-11 15:58:12 +01:00
local player_data = get_player_data(player)
local switch = player.gui.left.minimap_toggle_frame['ic_auto_switch']
2020-08-21 13:56:01 +02:00
if switch.switch_state == 'left' then
2021-01-11 15:58:12 +01:00
player_data.auto = true
player_data.state = 'left'
2020-08-21 13:56:01 +02:00
elseif switch.switch_state == 'right' then
2021-01-11 15:58:12 +01:00
player_data.auto = false
player_data.state = 'right'
2020-08-21 13:56:01 +02:00
end
end
local function kill_minimap(player)
2020-08-27 13:27:34 +02:00
local frame = player.gui.left.minimap_toggle_frame
if not frame or not frame.valid then
2020-08-21 13:56:01 +02:00
return
end
2020-08-27 13:27:34 +02:00
if frame.visible then
frame.destroy()
2020-08-21 13:56:01 +02:00
end
end
local function kill_frame(player)
2020-08-27 13:27:34 +02:00
if player.gui.left.minimap_toggle_frame then
local element = player.gui.left.minimap_toggle_frame.minimap_frame
2020-08-21 13:56:01 +02:00
if not element or not element.valid then
return
end
element.destroy()
end
end
2020-08-27 13:27:34 +02:00
local function draw_minimap(player, surface, position)
local allowed_surface = ICT.get('allowed_surface')
surface = surface or game.surfaces[allowed_surface]
2020-08-21 13:56:01 +02:00
if not surface or not surface.valid then
return
end
local cars = ICT.get('cars')
2020-08-21 13:56:01 +02:00
local entity = Functions.get_entity_from_player_surface(cars, player)
2020-08-27 13:27:34 +02:00
if not position then
if not entity or not entity.valid then
kill_minimap(player)
kill_frame(player)
return
end
2020-08-21 13:56:01 +02:00
end
2020-08-27 13:27:34 +02:00
position = position or entity.position
2020-08-21 13:56:01 +02:00
local player_data = get_player_data(player)
2020-08-27 13:27:34 +02:00
local frame = player.gui.left.minimap_toggle_frame
2020-08-21 13:56:01 +02:00
if not frame then
2021-01-11 15:58:12 +01:00
frame = player.gui.left.add({type = 'frame', direction = 'vertical', name = 'minimap_toggle_frame', caption = 'Minimap'})
2020-08-21 13:56:01 +02:00
end
frame.visible = true
2021-01-11 15:58:12 +01:00
if not frame.ic_auto_switch then
frame.add(
{
type = 'switch',
name = 'ic_auto_switch',
switch_state = player_data.state,
allow_none_state = false,
left_label_caption = {'gui.map_on'},
right_label_caption = {'gui.map_off'}
}
)
end
2020-08-21 13:56:01 +02:00
local element = frame['minimap_frame']
if not element then
element =
2020-08-27 13:27:34 +02:00
player.gui.left.minimap_toggle_frame.add(
2020-08-21 13:56:01 +02:00
{
type = 'camera',
name = 'minimap_frame',
position = position,
surface_index = surface.index,
2021-11-23 22:59:40 +01:00
zoom = player_data.zoom,
2020-08-21 13:56:01 +02:00
tooltip = 'LMB: Increase zoom level.\nRMB: Decrease zoom level.\nMMB: Toggle camera size.'
}
)
element.style.margin = 1
element.style.minimal_height = player_data.map_size
element.style.minimal_width = player_data.map_size
return
end
element.position = position
end
2020-08-27 13:27:34 +02:00
function Public.minimap(player, surface, position)
local frame = player.gui.left['minimap_toggle_frame']
2020-08-21 13:56:01 +02:00
if frame and frame.visible then
kill_minimap(player)
else
if Functions.get_player_surface(player) and not surface and not position then
2020-08-27 13:27:34 +02:00
draw_minimap(player)
else
draw_minimap(player, surface, position)
2020-08-21 13:56:01 +02:00
end
end
end
function Public.update_minimap()
2024-05-27 20:30:03 +02:00
for _, player in pairs(game.connected_players) do
2021-01-11 15:58:12 +01:00
local player_data = get_player_data(player)
if Functions.get_player_surface(player) and player.gui.left.minimap_toggle_frame and player_data.auto then
2020-08-21 13:56:01 +02:00
kill_frame(player)
draw_minimap(player)
2021-01-12 21:53:19 +01:00
else
kill_minimap(player)
2020-08-21 13:56:01 +02:00
end
end
end
function Public.toggle_minimap(event)
local element = event.element
if not element then
return
end
if not element.valid then
return
end
if element.name ~= 'minimap_frame' then
return
end
local player = game.players[event.player_index]
local player_data = get_player_data(player)
if event.button == defines.mouse_button_type.right then
player_data.zoom = player_data.zoom - 0.07
if player_data.zoom < 0.07 then
player_data.zoom = 0.07
end
2021-11-23 22:59:40 +01:00
element.zoom = player_data.zoom
2020-08-21 13:56:01 +02:00
return
end
if event.button == defines.mouse_button_type.left then
player_data.zoom = player_data.zoom + 0.07
if player_data.zoom > 2 then
player_data.zoom = 2
end
2021-11-23 22:59:40 +01:00
element.zoom = player_data.zoom
2020-08-21 13:56:01 +02:00
return
end
if event.button == defines.mouse_button_type.middle then
player_data.map_size = player_data.map_size + 50
if player_data.map_size > 650 then
player_data.map_size = 250
end
element.style.minimal_height = player_data.map_size
element.style.minimal_width = player_data.map_size
element.style.maximal_height = player_data.map_size
element.style.maximal_width = player_data.map_size
return
end
end
function Public.changed_surface(event)
local player = game.players[event.player_index]
if not validate_player(player) then
return
end
local allowed_surface = ICT.get('allowed_surface')
local surface = game.surfaces[allowed_surface]
2020-08-21 13:56:01 +02:00
if not surface or not surface.valid then
return
end
2024-05-31 22:47:33 +02:00
local wd = get_top_frame_custom(player, 'wave_defense')
local diff = get_top_frame_custom(player, 'difficulty_gui')
2021-01-11 15:58:12 +01:00
local player_data = get_player_data(player)
2020-08-21 13:56:01 +02:00
if Functions.get_player_surface(player) then
2020-08-21 13:56:01 +02:00
Public.toggle_button(player)
2021-01-11 15:58:12 +01:00
if player_data.auto then
Public.minimap(player, surface)
end
2020-08-21 13:56:01 +02:00
if wd and wd.visible then
wd.visible = false
end
if diff and diff.visible then
diff.visible = false
end
elseif player.surface.index == surface.index then
Gui.remove_toolbar(player)
Public.toggle_button(player)
kill_minimap(player)
if wd and not wd.visible then
wd.visible = true
end
if diff and not diff.visible then
diff.visible = true
end
end
end
2020-08-27 13:27:34 +02:00
Public.kill_minimap = kill_minimap
2020-08-21 13:56:01 +02:00
return Public