1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-02-13 13:49:35 +02:00

Change paint tool to use coloured concrete.

This commit is contained in:
James Gillham 2020-09-12 20:17:29 +01:00
parent 6837fa7264
commit 5cc6ab82b6
6 changed files with 188 additions and 61 deletions

View File

@ -106,7 +106,10 @@ global.config = {
}, },
-- adds a paint brush -- adds a paint brush
paint = { paint = {
enabled = true enabled = true,
-- Sometimes the hidden tile information is lost, the fallback tile will be used when removing those tiles.
fallback_hidden_tile = 'dirt-6',
prevent_on_landfill = true
}, },
-- autofill turrets with ammo -- autofill turrets with ammo
autofill = { autofill = {

View File

@ -94,7 +94,7 @@ local function toggle_main_frame(event)
Gui.set_data(button, name) Gui.set_data(button, name)
end end
frame.add {type = 'button', name = main_button_name, caption = 'Close'} frame.add {type = 'button', name = main_button_name, caption = {'common.close_button'}}
local data = { local data = {
enabled_checkbox = enabled_checkbox, enabled_checkbox = enabled_checkbox,

View File

@ -2,32 +2,26 @@ local Event = require 'utils.event'
local Gui = require 'utils.gui' local Gui = require 'utils.gui'
local Global = require 'utils.global' local Global = require 'utils.global'
local brush_tool = 'refined-hazard-concrete' local config = global.config.paint
local default_fallback_hidden_tile = 'dirt-6'
local brush_tools = {
['refined-concrete'] = true,
['refined-hazard-concrete'] = true
}
local valid_filters = { local valid_filters = {
'dirt-1', ['acid-refined-concrete'] = true,
'dirt-2', ['black-refined-concrete'] = true,
'dirt-3', ['blue-refined-concrete'] = true,
'dirt-4', ['brown-refined-concrete'] = true,
'dirt-5', ['cyan-refined-concrete'] = true,
'dirt-6', ['green-refined-concrete'] = true,
'dirt-7', ['orange-refined-concrete'] = true,
'dry-dirt', ['pink-refined-concrete'] = true,
'grass-1', ['purple-refined-concrete'] = true,
'grass-2', ['red-refined-concrete'] = true,
'grass-3', ['yellow-refined-concrete'] = true
'grass-4',
'lab-dark-1',
'lab-dark-2',
'lab-white',
'red-desert-0',
'red-desert-1',
'red-desert-2',
'red-desert-3',
'sand-1',
'sand-2',
'sand-3',
'tutorial-grid'
} }
local main_button_name = Gui.uid_name() local main_button_name = Gui.uid_name()
@ -50,41 +44,146 @@ Global.register(
end end
) )
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
local function player_build_tile(event) local function player_build_tile(event)
local item = event.item local item = event.item
if not item then if not item then
return return
end end
if item.name ~= brush_tool then local item_name = item.name
if not brush_tools[item_name] then
return return
end end
local replace_tile = paint_brushes_by_player[event.player_index] 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]
if not replace_tile then if not replace_tile then
refund_tiles(player, event.tiles)
return return
end end
local player = game.get_player(event.player_index)
if not player.gui.left[main_frame_name] then if not player.gui.left[main_frame_name] then
refund_tiles(player, event.tiles)
return
end
local get_hidden_tile = surface.get_hidden_tile
local tile_name = event.tile.name
local tiles = event.tiles
local count = 0
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
for i = 1, #tiles do
local tile_data = tiles[i]
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
tile_data.name = replace_tile
if valid_filters[tile_data.old_tile.name] then
count = count + 1
end
if valid_filters[hidden_tile] then
hidden_tiles[#hidden_tiles + 1] = {position = tile_data.position, name = fallback_tile}
end
::continue::
end
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
if count > 0 then
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 return
end end
local tiles = event.tiles local tiles = event.tiles
local count = 0 local hidden_tiles = {}
local fallback_tile = config.fallback_hidden_tile or default_fallback_hidden_tile
for i = 1, #tiles do for i = 1, #tiles do
local tile_data = tiles[i] local tile_data = tiles[i]
tile_data.name = replace_tile local hidden_tile = surface.get_hidden_tile(tile_data.position)
if tile_data.old_tile.name == replace_tile then if valid_filters[hidden_tile] then
count = count + 1 hidden_tiles[#hidden_tiles + 1] = {position = tile_data.position, name = fallback_tile}
end end
end end
game.surfaces[event.surface_index].set_tiles(tiles) for i = 1, #hidden_tiles do
local tile = hidden_tiles[i]
surface.set_hidden_tile(tile.position, tile.name)
end
end
if count > 0 then local function get_tile_localised_name(tile_name)
player.insert {name = brush_tool, count = count} if not tile_name then
return
end
local proto = game.tile_prototypes[tile_name]
if proto then
return proto.localised_name or proto.name
end end
end end
@ -115,22 +214,29 @@ local function draw_filters_table(event)
return return
end end
local frame = center.add {type = 'frame', name = filters_table_name, direction = 'vertical', caption = 'Palette'} local frame =
center.add {type = 'frame', name = filters_table_name, direction = 'vertical', caption = {'paint.palette'}}
local t = frame.add {type = 'table', column_count = 10} local t = frame.add {type = 'table', column_count = 6}
t.style.horizontal_spacing = 0 t.style.horizontal_spacing = 0
t.style.vertical_spacing = 0 t.style.vertical_spacing = 0
for _, v in ipairs(valid_filters) do for tile_name, _ in pairs(valid_filters) do
local flow = t.add {type = 'flow'} local flow = t.add {type = 'flow'}
local b = flow.add {type = 'sprite-button', name = filter_element_name, sprite = 'tile/' .. v, tooltip = v} local button =
Gui.set_data(b, frame) flow.add {
b.style = 'slot_button' 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'
end end
local flow = frame.add {type = 'flow'} local flow = frame.add {type = 'flow'}
local close = flow.add {type = 'button', name = filter_table_close_button_name, caption = 'Close'} local close = flow.add {type = 'button', name = filter_table_close_button_name, caption = {'common.close_button'}}
Gui.set_data(close, frame) Gui.set_data(close, frame)
event.player.opened = frame event.player.opened = frame
@ -159,25 +265,34 @@ local function toggle(event)
type = 'frame', type = 'frame',
name = main_frame_name, name = main_frame_name,
direction = 'vertical', direction = 'vertical',
caption = 'Paint Brush' caption = {'paint.frame_name'}
} }
local tooltip = paint_brushes_by_player[event.player_index] or '' local top_flow = main_frame.add {type = 'flow', direction = 'horizontal'}
local tile_name = paint_brushes_by_player[event.player_index]
local brush = local brush =
main_frame.add({type = 'flow'}).add { top_flow.add({type = 'flow'}).add {
type = 'sprite-button', type = 'sprite-button',
name = filter_button_name, name = filter_button_name,
tooltip = tooltip, tooltip = get_tile_localised_name(tile_name) or {'paint.select_brush'},
sprite = tooltip ~= '' and 'tile/' .. tooltip or nil sprite = tile_name and 'tile/' .. tile_name
} }
brush.style = 'slot_button' brush.style = 'slot_button'
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
local buttons_flow = main_frame.add {type = 'flow', direction = 'horizontal'} local buttons_flow = main_frame.add {type = 'flow', direction = 'horizontal'}
buttons_flow.add {type = 'button', name = main_button_name, caption = 'Close'} buttons_flow.add {type = 'button', name = main_button_name, caption = {'common.close_button'}}
local clear_brush = buttons_flow.add {type = 'button', name = filter_clear_name, caption = 'Clear Brush'} local clear_brush =
buttons_flow.add {type = 'button', name = filter_clear_name, caption = {'paint.clear_brush'}}
Gui.set_data(clear_brush, brush) Gui.set_data(clear_brush, brush)
end end
end end
@ -191,7 +306,7 @@ Gui.on_click(
paint_brushes_by_player[event.player_index] = nil paint_brushes_by_player[event.player_index] = nil
local element = event.element local element = event.element
element.sprite = 'utility/pump_cannot_connect_icon' element.sprite = 'utility/pump_cannot_connect_icon'
element.tooltip = '' element.tooltip = {'paint.select_brush'}
else else
draw_filters_table(event) draw_filters_table(event)
end end
@ -204,7 +319,7 @@ Gui.on_click(
local brush = Gui.get_data(event.element) local brush = Gui.get_data(event.element)
brush.sprite = 'utility/pump_cannot_connect_icon' brush.sprite = 'utility/pump_cannot_connect_icon'
brush.tooltip = '' brush.tooltip = {'paint.select_brush'}
paint_brushes_by_player[event.player_index] = nil paint_brushes_by_player[event.player_index] = nil
end end
@ -218,15 +333,16 @@ Gui.on_click(
return return
end end
local frame = Gui.get_data(element) local data = Gui.get_data(element)
local frame = data.frame
local tile_name = data.tile_name
local filter_button = Gui.get_data(frame) local filter_button = Gui.get_data(frame)
paint_brushes_by_player[event.player_index] = element.tooltip paint_brushes_by_player[event.player_index] = tile_name
filter_button.sprite = element.sprite filter_button.sprite = element.sprite
filter_button.tooltip = element.tooltip filter_button.tooltip = element.tooltip
Gui.remove_data_recursively(frame) Gui.destroy(frame)
frame.destroy()
end end
) )
@ -234,8 +350,7 @@ Gui.on_click(
filter_table_close_button_name, filter_table_close_button_name,
function(event) function(event)
local frame = Gui.get_data(event.element) local frame = Gui.get_data(event.element)
Gui.remove_data_recursively(frame) Gui.destroy(frame)
frame.destroy()
end end
) )
@ -243,8 +358,7 @@ Gui.on_custom_close(
filters_table_name, filters_table_name,
function(event) function(event)
local element = event.element local element = event.element
Gui.remove_data_recursively(element) Gui.destroy(element)
element.destroy()
end end
) )
@ -252,3 +366,4 @@ Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
Event.add(defines.events.on_player_joined_game, player_joined) Event.add(defines.events.on_player_joined_game, player_joined)
Event.add(defines.events.on_player_built_tile, player_build_tile) Event.add(defines.events.on_player_built_tile, player_build_tile)
Event.add(defines.events.on_robot_built_tile, robot_built_tile)

View File

@ -169,3 +169,11 @@ research_finished=[technology=__1__] has been researched.
name=Snake name=Snake
spawn_snake_fail=Unable to spawn snake, please try again. spawn_snake_fail=Unable to spawn snake, please try again.
snake_destroyed=__1__ has been destroyed with a score of __2__. snake_destroyed=__1__ has been destroyed with a score of __2__.
[paint]
frame_name=Paint Brush
clear_brush=Clear Brush
palette=Palette
select_brush=Select Brush Tile.
instructions=Select a brush tile to replace [item=refined-concrete] and [item=refined-hazard-concrete].\nOnly works when Paint Brush window is open.
no_place_landfill=Coloured concrete can not be placed on landfill tiles.

View File

@ -92,7 +92,7 @@ empty_cursor_error_message=Click the button with a blueprint or blueprint book.
no_filters_error_message=No filters have been set. no_filters_error_message=No filters have been set.
[paint] [paint]
tooltip=Landscape painting tool tooltip=Refined concrete painting tool
[tag_group] [tag_group]
tooltip=Player tag group management tooltip=Player tag group management

View File

@ -76,5 +76,6 @@ return {
{price = 25, name = 'construction-robot'}, {price = 25, name = 'construction-robot'},
{price = 350, name = 'energy-shield-equipment'}, {price = 350, name = 'energy-shield-equipment'},
{price = 750, name = 'personal-laser-defense-equipment'}, {price = 750, name = 'personal-laser-defense-equipment'},
{price = 1, name = 'refined-concrete'},
{price = 1, name = 'refined-hazard-concrete'}, {price = 1, name = 'refined-hazard-concrete'},
} }