2018-06-08 19:22:23 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Gui = require 'utils.gui'
|
2019-02-03 09:02:54 +02:00
|
|
|
local Global = require 'utils.global'
|
2018-06-08 19:22:23 +02:00
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local config = global.config.paint
|
|
|
|
local default_fallback_hidden_tile = 'dirt-6'
|
|
|
|
|
|
|
|
local brush_tools = {
|
|
|
|
['refined-concrete'] = true,
|
|
|
|
['refined-hazard-concrete'] = true
|
|
|
|
}
|
2018-06-08 19:22:23 +02:00
|
|
|
|
|
|
|
local valid_filters = {
|
2020-09-12 21:17:29 +02:00
|
|
|
['acid-refined-concrete'] = true,
|
|
|
|
['black-refined-concrete'] = true,
|
|
|
|
['blue-refined-concrete'] = true,
|
|
|
|
['brown-refined-concrete'] = true,
|
|
|
|
['cyan-refined-concrete'] = true,
|
|
|
|
['green-refined-concrete'] = true,
|
|
|
|
['orange-refined-concrete'] = true,
|
|
|
|
['pink-refined-concrete'] = true,
|
|
|
|
['purple-refined-concrete'] = true,
|
|
|
|
['red-refined-concrete'] = true,
|
|
|
|
['yellow-refined-concrete'] = true
|
2018-06-08 19:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
local main_button_name = Gui.uid_name()
|
|
|
|
local main_frame_name = Gui.uid_name()
|
|
|
|
|
|
|
|
local filter_button_name = Gui.uid_name()
|
2018-06-08 23:47:14 +02:00
|
|
|
local filter_clear_name = Gui.uid_name()
|
2018-06-08 19:22:23 +02:00
|
|
|
local filter_element_name = Gui.uid_name()
|
|
|
|
local filters_table_name = Gui.uid_name()
|
|
|
|
local filter_table_close_button_name = Gui.uid_name()
|
|
|
|
|
2019-02-03 09:02:54 +02:00
|
|
|
local paint_brushes_by_player = {}
|
|
|
|
|
|
|
|
Global.register(
|
|
|
|
{
|
|
|
|
paint_brushes_by_player = paint_brushes_by_player
|
|
|
|
},
|
|
|
|
function(tbl)
|
|
|
|
paint_brushes_by_player = tbl.paint_brushes_by_player
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local function refund_tiles(player, tiles)
|
|
|
|
local count = 0
|
|
|
|
local set_hidden_tile = player.surface.set_hidden_tile
|
|
|
|
local fallback_tile = config.fallback_hidden_tile or default_fallback_hidden_tile
|
|
|
|
for i = 1, #tiles do
|
|
|
|
local tile_data = tiles[i]
|
|
|
|
if valid_filters[tile_data.old_tile.name] then
|
|
|
|
count = count + 1
|
|
|
|
set_hidden_tile(tile_data.position, fallback_tile)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if count > 0 then
|
|
|
|
player.insert {name = 'refined-concrete', count = count}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-08 19:22:23 +02:00
|
|
|
local function player_build_tile(event)
|
2019-02-25 04:40:08 +02:00
|
|
|
local item = event.item
|
|
|
|
if not item then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local item_name = item.name
|
|
|
|
if not brush_tools[item_name] then
|
2018-06-08 23:47:14 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local player_index = event.player_index
|
|
|
|
local player = game.get_player(player_index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = game.surfaces[event.surface_index]
|
|
|
|
if not surface or not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local replace_tile = paint_brushes_by_player[player_index]
|
2018-06-08 23:47:14 +02:00
|
|
|
if not replace_tile then
|
2020-09-12 21:17:29 +02:00
|
|
|
refund_tiles(player, event.tiles)
|
2018-06-08 23:47:14 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-08-03 18:31:10 +02:00
|
|
|
if not Gui.get_left_element(player, main_frame_name) then
|
2020-09-12 21:17:29 +02:00
|
|
|
refund_tiles(player, event.tiles)
|
2018-06-08 23:47:14 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local get_hidden_tile = surface.get_hidden_tile
|
|
|
|
local tile_name = event.tile.name
|
2018-06-08 23:47:14 +02:00
|
|
|
local tiles = event.tiles
|
|
|
|
local count = 0
|
2020-09-12 21:17:29 +02:00
|
|
|
local hidden_tiles = {}
|
|
|
|
local prevent_on_landfill = config.prevent_on_landfill
|
|
|
|
local print_no_landfill_message = false
|
|
|
|
local fallback_tile = config.fallback_hidden_tile or default_fallback_hidden_tile
|
2019-02-25 04:40:08 +02:00
|
|
|
for i = 1, #tiles do
|
|
|
|
local tile_data = tiles[i]
|
2020-09-12 21:17:29 +02:00
|
|
|
|
|
|
|
local hidden_tile = get_hidden_tile(tile_data.position)
|
|
|
|
if prevent_on_landfill and hidden_tile == 'landfill' then
|
|
|
|
tile_data.name = tile_name
|
|
|
|
print_no_landfill_message = true
|
|
|
|
goto continue
|
|
|
|
end
|
|
|
|
|
2018-06-08 23:47:14 +02:00
|
|
|
tile_data.name = replace_tile
|
2019-02-25 04:40:08 +02:00
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
if valid_filters[tile_data.old_tile.name] then
|
2018-06-08 23:47:14 +02:00
|
|
|
count = count + 1
|
|
|
|
end
|
2020-09-12 21:17:29 +02:00
|
|
|
|
|
|
|
if valid_filters[hidden_tile] then
|
|
|
|
hidden_tiles[#hidden_tiles + 1] = {position = tile_data.position, name = fallback_tile}
|
|
|
|
end
|
|
|
|
|
|
|
|
::continue::
|
2018-06-08 23:47:14 +02:00
|
|
|
end
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
surface.set_tiles(tiles)
|
|
|
|
|
|
|
|
local set_hidden_tile = surface.set_hidden_tile
|
|
|
|
for i = 1, #hidden_tiles do
|
|
|
|
local tile = hidden_tiles[i]
|
|
|
|
set_hidden_tile(tile.position, tile.name)
|
|
|
|
end
|
2018-06-08 23:47:14 +02:00
|
|
|
|
|
|
|
if count > 0 then
|
2020-09-12 21:17:29 +02:00
|
|
|
player.insert {name = item_name, count = count}
|
|
|
|
end
|
|
|
|
|
|
|
|
if print_no_landfill_message then
|
|
|
|
player.print({'paint.no_place_landfill'})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function robot_built_tile(event)
|
|
|
|
local item = event.item
|
|
|
|
if not item then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local item_name = item.name
|
|
|
|
if not brush_tools[item_name] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local surface = game.surfaces[event.surface_index]
|
|
|
|
if not surface or not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local tiles = event.tiles
|
|
|
|
local hidden_tiles = {}
|
|
|
|
local fallback_tile = config.fallback_hidden_tile or default_fallback_hidden_tile
|
|
|
|
for i = 1, #tiles do
|
|
|
|
local tile_data = tiles[i]
|
|
|
|
local hidden_tile = surface.get_hidden_tile(tile_data.position)
|
|
|
|
|
|
|
|
if valid_filters[hidden_tile] then
|
|
|
|
hidden_tiles[#hidden_tiles + 1] = {position = tile_data.position, name = fallback_tile}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1, #hidden_tiles do
|
|
|
|
local tile = hidden_tiles[i]
|
|
|
|
surface.set_hidden_tile(tile.position, tile.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_tile_localised_name(tile_name)
|
|
|
|
if not tile_name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local proto = game.tile_prototypes[tile_name]
|
|
|
|
if proto then
|
|
|
|
return proto.localised_name or proto.name
|
2018-06-08 23:47:14 +02:00
|
|
|
end
|
2018-06-08 19:22:23 +02:00
|
|
|
end
|
|
|
|
|
2020-09-14 22:14:15 +02:00
|
|
|
local function player_created(event)
|
2019-05-16 12:10:56 +02:00
|
|
|
local player = game.get_player(event.player_index)
|
2018-06-08 19:22:23 +02:00
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-08-03 18:31:10 +02:00
|
|
|
local b = Gui.add_top_element(player,
|
2019-06-20 13:39:18 +02:00
|
|
|
{
|
|
|
|
name = main_button_name,
|
|
|
|
type = 'sprite-button',
|
|
|
|
sprite = 'utility/spray_icon',
|
2024-08-03 18:31:10 +02:00
|
|
|
tooltip = {'paint.tooltip'},
|
|
|
|
auto_toggle = true,
|
2019-06-20 13:39:18 +02:00
|
|
|
}
|
|
|
|
)
|
2024-08-03 18:31:10 +02:00
|
|
|
b.style.padding = 2
|
2018-06-08 19:22:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function draw_filters_table(event)
|
|
|
|
local center = event.player.gui.center
|
|
|
|
|
|
|
|
if center[filters_table_name] then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local frame =
|
|
|
|
center.add {type = 'frame', name = filters_table_name, direction = 'vertical', caption = {'paint.palette'}}
|
2018-06-08 19:22:23 +02:00
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local t = frame.add {type = 'table', column_count = 6}
|
2018-06-08 19:22:23 +02:00
|
|
|
t.style.horizontal_spacing = 0
|
|
|
|
t.style.vertical_spacing = 0
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
for tile_name, _ in pairs(valid_filters) do
|
2018-06-08 23:47:14 +02:00
|
|
|
local flow = t.add {type = 'flow'}
|
2020-09-12 21:17:29 +02:00
|
|
|
local button =
|
|
|
|
flow.add {
|
|
|
|
type = 'sprite-button',
|
|
|
|
name = filter_element_name,
|
|
|
|
sprite = 'tile/' .. tile_name,
|
|
|
|
tooltip = get_tile_localised_name(tile_name)
|
|
|
|
}
|
|
|
|
Gui.set_data(button, {frame = frame, tile_name = tile_name})
|
|
|
|
button.style = 'slot_button'
|
2018-06-08 19:22:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local flow = frame.add {type = 'flow'}
|
|
|
|
|
2020-10-04 22:13:09 +02:00
|
|
|
local close_button = Gui.make_close_button(flow, filter_table_close_button_name)
|
|
|
|
Gui.set_data(close_button, frame)
|
2018-06-08 19:22:23 +02:00
|
|
|
|
|
|
|
event.player.opened = frame
|
|
|
|
|
|
|
|
Gui.set_data(frame, event.element)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function toggle(event)
|
2019-06-20 13:39:18 +02:00
|
|
|
local player = event.player
|
2024-08-03 18:31:10 +02:00
|
|
|
local main_frame = Gui.get_left_element(player, main_frame_name)
|
2018-06-08 19:22:23 +02:00
|
|
|
|
|
|
|
if main_frame and main_frame.valid then
|
2019-06-20 13:39:18 +02:00
|
|
|
Gui.destroy(main_frame)
|
2024-08-03 18:31:10 +02:00
|
|
|
local main_button = Gui.get_top_element(player, main_button_name)
|
|
|
|
main_button.toggled = false
|
2018-06-08 19:22:23 +02:00
|
|
|
else
|
2024-08-03 18:31:10 +02:00
|
|
|
main_frame = Gui.add_left_element(player, {
|
2018-06-08 19:22:23 +02:00
|
|
|
type = 'frame',
|
|
|
|
name = main_frame_name,
|
|
|
|
direction = 'vertical',
|
2020-09-12 21:17:29 +02:00
|
|
|
caption = {'paint.frame_name'}
|
2024-08-03 18:31:10 +02:00
|
|
|
})
|
2018-06-08 19:22:23 +02:00
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local top_flow = main_frame.add {type = 'flow', direction = 'horizontal'}
|
|
|
|
|
|
|
|
local tile_name = paint_brushes_by_player[event.player_index]
|
2018-06-08 19:22:23 +02:00
|
|
|
|
|
|
|
local brush =
|
2020-09-12 21:17:29 +02:00
|
|
|
top_flow.add({type = 'flow'}).add {
|
2018-06-08 19:22:23 +02:00
|
|
|
type = 'sprite-button',
|
|
|
|
name = filter_button_name,
|
2020-09-12 21:17:29 +02:00
|
|
|
tooltip = get_tile_localised_name(tile_name) or {'paint.select_brush'},
|
|
|
|
sprite = tile_name and 'tile/' .. tile_name
|
2018-06-08 23:47:14 +02:00
|
|
|
}
|
2018-06-08 19:22:23 +02:00
|
|
|
brush.style = 'slot_button'
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local label = top_flow.add {type = 'label', caption = {'paint.instructions'}}
|
|
|
|
local label_style = label.style
|
|
|
|
label_style.font = 'default-bold'
|
|
|
|
label_style.single_line = false
|
|
|
|
label_style.left_padding = 10
|
|
|
|
|
2018-06-08 23:47:14 +02:00
|
|
|
local buttons_flow = main_frame.add {type = 'flow', direction = 'horizontal'}
|
|
|
|
|
2020-10-04 22:13:09 +02:00
|
|
|
Gui.make_close_button(buttons_flow, main_button_name)
|
2018-06-08 23:47:14 +02:00
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local clear_brush =
|
|
|
|
buttons_flow.add {type = 'button', name = filter_clear_name, caption = {'paint.clear_brush'}}
|
2018-12-28 18:21:09 +02:00
|
|
|
Gui.set_data(clear_brush, brush)
|
2018-06-08 19:22:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Gui.on_click(main_button_name, toggle)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
filter_button_name,
|
|
|
|
function(event)
|
|
|
|
if event.button == defines.mouse_button_type.right then
|
2019-02-03 09:02:54 +02:00
|
|
|
paint_brushes_by_player[event.player_index] = nil
|
2018-06-08 19:22:23 +02:00
|
|
|
local element = event.element
|
|
|
|
element.sprite = 'utility/pump_cannot_connect_icon'
|
2020-09-12 21:17:29 +02:00
|
|
|
element.tooltip = {'paint.select_brush'}
|
2018-06-08 19:22:23 +02:00
|
|
|
else
|
|
|
|
draw_filters_table(event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-06-08 23:47:14 +02:00
|
|
|
Gui.on_click(
|
|
|
|
filter_clear_name,
|
|
|
|
function(event)
|
|
|
|
local brush = Gui.get_data(event.element)
|
|
|
|
|
|
|
|
brush.sprite = 'utility/pump_cannot_connect_icon'
|
2020-09-12 21:17:29 +02:00
|
|
|
brush.tooltip = {'paint.select_brush'}
|
2018-06-08 23:47:14 +02:00
|
|
|
|
2019-02-03 09:02:54 +02:00
|
|
|
paint_brushes_by_player[event.player_index] = nil
|
2018-06-08 23:47:14 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-06-08 19:22:23 +02:00
|
|
|
Gui.on_click(
|
|
|
|
filter_element_name,
|
|
|
|
function(event)
|
|
|
|
local element = event.element
|
2019-03-17 18:42:07 +02:00
|
|
|
if not element or not element.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
local data = Gui.get_data(element)
|
|
|
|
local frame = data.frame
|
|
|
|
local tile_name = data.tile_name
|
2018-06-08 19:22:23 +02:00
|
|
|
local filter_button = Gui.get_data(frame)
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
paint_brushes_by_player[event.player_index] = tile_name
|
2018-06-08 19:22:23 +02:00
|
|
|
filter_button.sprite = element.sprite
|
|
|
|
filter_button.tooltip = element.tooltip
|
|
|
|
|
2020-09-12 21:17:29 +02:00
|
|
|
Gui.destroy(frame)
|
2018-06-08 19:22:23 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_click(
|
|
|
|
filter_table_close_button_name,
|
|
|
|
function(event)
|
|
|
|
local frame = Gui.get_data(event.element)
|
2020-09-12 21:17:29 +02:00
|
|
|
Gui.destroy(frame)
|
2018-06-08 19:22:23 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
Gui.on_custom_close(
|
|
|
|
filters_table_name,
|
|
|
|
function(event)
|
|
|
|
local element = event.element
|
2020-09-12 21:17:29 +02:00
|
|
|
Gui.destroy(element)
|
2018-06-08 19:22:23 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2018-12-16 03:28:00 +02:00
|
|
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
|
|
|
|
2020-09-14 22:14:15 +02:00
|
|
|
Event.add(defines.events.on_player_created, player_created)
|
2018-06-08 19:22:23 +02:00
|
|
|
Event.add(defines.events.on_player_built_tile, player_build_tile)
|
2020-09-12 21:17:29 +02:00
|
|
|
Event.add(defines.events.on_robot_built_tile, robot_built_tile)
|